/tests/tine20/ActiveSync/Controller/ControllerTest.php

https://github.com/tine20/Tine-2.0-Open-Source-Groupware-and-CRM · PHP · 248 lines · 125 code · 55 blank · 68 comment · 1 complexity · f729d86c9e20a34ad344f434bd0ff7c4 MD5 · raw file

  1. <?php
  2. /**
  3. * Tine 2.0 - http://www.tine20.org
  4. *
  5. * @package ActiveSync
  6. * @license http://www.gnu.org/licenses/agpl.html AGPL Version 3
  7. * @copyright Copyright (c) 2010-2015 Metaways Infosystems GmbH (http://www.metaways.de)
  8. * @author Lars Kneschke <l.kneschke@metaways.de>
  9. */
  10. /**
  11. * abstract test class for activesync controller tests
  12. *
  13. * @package ActiveSync
  14. */
  15. abstract class ActiveSync_Controller_ControllerTest extends ActiveSync_TestCase
  16. {
  17. /**
  18. * name of the controller
  19. *
  20. * @var string
  21. */
  22. protected $_controllerName;
  23. /**
  24. * @var ActiveSync_Controller_Abstract controller
  25. */
  26. protected $_controller;
  27. /**
  28. *
  29. * @return Syncroton_Model_Folder
  30. */
  31. public function testCreateFolder($name = null)
  32. {
  33. $controller = Syncroton_Data_Factory::factory($this->_class, $this->_getDevice(Syncroton_Model_Device::TYPE_IPHONE), new Tinebase_DateTime(null, null, 'de_DE'));
  34. $syncrotonFolder = $controller->createFolder(new Syncroton_Model_Folder(array(
  35. 'parentId' => 0,
  36. 'displayName' => $name ?: 'TestFolder'
  37. )));
  38. $this->assertTrue(!empty($syncrotonFolder->serverId));
  39. return $syncrotonFolder;
  40. }
  41. /**
  42. * testUpdateFolder (iPhone)
  43. *
  44. * @return Syncroton_Model_Folder
  45. */
  46. public function testUpdateFolder()
  47. {
  48. return $this->_testUpdateFolderForDeviceType(Syncroton_Model_Device::TYPE_IPHONE);
  49. }
  50. /**
  51. * @param string $type
  52. * @return Syncroton_Model_Folder
  53. */
  54. protected function _testUpdateFolderForDeviceType($type)
  55. {
  56. $syncrotonFolder = $this->testCreateFolder();
  57. $syncrotonFolder->displayName = 'RenamedTestFolder';
  58. $controller = Syncroton_Data_Factory::factory($this->_class, $this->_getDevice($type), new Tinebase_DateTime(null, null, 'de_DE'));
  59. $updatedSyncrotonFolder = $controller->updateFolder($syncrotonFolder);
  60. $allFolders = $controller->getAllFolders();
  61. $this->assertArrayHasKey($syncrotonFolder->serverId, $allFolders);
  62. $this->assertEquals('RenamedTestFolder', $allFolders[$syncrotonFolder->serverId]->displayName);
  63. return $updatedSyncrotonFolder;
  64. }
  65. /**
  66. * @return Syncroton_Model_Folder
  67. */
  68. public function testUpdateFolderAndroid()
  69. {
  70. return $this->_testUpdateFolderForDeviceType(self::TYPE_ANDROID_6);
  71. }
  72. /**
  73. * test if changed folders got returned
  74. */
  75. public function testGetChangedFolders()
  76. {
  77. $syncrotonFolder = $this->testUpdateFolder();
  78. $controller = Syncroton_Data_Factory::factory($this->_class, $this->_getDevice(Syncroton_Model_Device::TYPE_IPHONE), new Tinebase_DateTime(null, null, 'de_DE'));
  79. $changedFolders = $controller->getChangedFolders(Tinebase_DateTime::now()->subMinute(1), Tinebase_DateTime::now());
  80. $this->assertGreaterThanOrEqual(1, count($changedFolders));
  81. $this->assertArrayHasKey($syncrotonFolder->serverId, $changedFolders);
  82. }
  83. public function testDeleteFolder()
  84. {
  85. $syncrotonFolder = $this->testCreateFolder();
  86. $controller = Syncroton_Data_Factory::factory($this->_class, $this->_getDevice(Syncroton_Model_Device::TYPE_IPHONE), new Tinebase_DateTime(null, null, 'de_DE'));
  87. $controller->deleteFolder($syncrotonFolder);
  88. }
  89. public function testGetAllFoldersIPhone()
  90. {
  91. $this->_testGetAllFoldersForDeviceType(Syncroton_Model_Device::TYPE_IPHONE);
  92. }
  93. /**
  94. * get all folders test for given device type
  95. *
  96. * @param $type
  97. */
  98. protected function _testGetAllFoldersForDeviceType($type)
  99. {
  100. $syncrotonFolder = $this->testCreateFolder();
  101. $controller = Syncroton_Data_Factory::factory($this->_class, $this->_getDevice($type),
  102. new Tinebase_DateTime(null, null, 'de_DE'));
  103. $allSyncrotonFolders = $controller->getAllFolders();
  104. $this->assertArrayHasKey($syncrotonFolder->serverId, $allSyncrotonFolders);
  105. $this->assertArrayNotHasKey($this->_specialFolderName, $allSyncrotonFolders);
  106. $this->assertTrue($allSyncrotonFolders[$syncrotonFolder->serverId] instanceof Syncroton_Model_Folder);
  107. $this->assertEquals($syncrotonFolder->serverId, $allSyncrotonFolders[$syncrotonFolder->serverId]->serverId, 'serverId mismatch');
  108. $this->assertEquals($syncrotonFolder->parentId, $allSyncrotonFolders[$syncrotonFolder->serverId]->parentId, 'parentId mismatch');
  109. $this->assertEquals($syncrotonFolder->displayName, $allSyncrotonFolders[$syncrotonFolder->serverId]->displayName);
  110. $this->assertTrue(!empty($allSyncrotonFolders[$syncrotonFolder->serverId]->type));
  111. }
  112. public function testGetAllFoldersPalm()
  113. {
  114. $syncrotonFolder = $this->testCreateFolder();
  115. $controller = Syncroton_Data_Factory::factory($this->_class, $this->_getDevice(Syncroton_Model_Device::TYPE_WEBOS), new Tinebase_DateTime(null, null, 'de_DE'));
  116. $allSyncrotonFolders = $controller->getAllFolders();
  117. $this->assertArrayHasKey($this->_specialFolderName, $allSyncrotonFolders, "key {$this->_specialFolderName} not found in " . print_r($allSyncrotonFolders, true));
  118. }
  119. /**
  120. * @see 0012634: ActiveSync: Add android to multiple folders devices
  121. */
  122. public function testGetAllFoldersAndroid()
  123. {
  124. $this->_testGetAllFoldersForDeviceType(self::TYPE_ANDROID_6);
  125. }
  126. /**
  127. * testDeleteEntry
  128. */
  129. public function testDeleteEntry()
  130. {
  131. $syncrotonFolder = $this->testCreateFolder();
  132. $controller = Syncroton_Data_Factory::factory($this->_class, $this->_getDevice(Syncroton_Model_Device::TYPE_IPHONE), new Tinebase_DateTime(null, null, 'de_DE'));
  133. list($serverId, $syncrotonContact) = $this->testCreateEntry($syncrotonFolder);
  134. $controller->deleteEntry($syncrotonFolder->serverId, $serverId, null);
  135. try {
  136. $syncrotonContact = $controller->getEntry(new Syncroton_Model_SyncCollection(array('collectionId' => $syncrotonFolder->serverId)), $serverId);
  137. $this->fail('should have thrown Syncroton_Exception_NotFound: '
  138. . var_export($syncrotonContact, TRUE)
  139. . ' tine contact: ' . print_r(Addressbook_Controller_Contact::getInstance()->get($serverId)->toArray(), TRUE));
  140. } catch (Syncroton_Exception_NotFound $senf) {
  141. $this->assertEquals('Syncroton_Exception_NotFound', get_class($senf));
  142. }
  143. }
  144. public function testGetInvalidEntry()
  145. {
  146. $syncrotonFolder = $this->testCreateFolder();
  147. $controller = Syncroton_Data_Factory::factory($this->_class, $this->_getDevice(Syncroton_Model_Device::TYPE_IPHONE), new Tinebase_DateTime(null, null, 'de_DE'));
  148. $this->expectException('Syncroton_Exception_NotFound');
  149. $syncrotonContact = $controller->getEntry(new Syncroton_Model_SyncCollection(array('collectionId' => $syncrotonFolder->serverId)), 'jdszfegd63gfrk');
  150. }
  151. /**
  152. * test get changed entries
  153. */
  154. public function testGetChangedEntries()
  155. {
  156. $syncrotonFolder = $this->testCreateFolder();
  157. list($serverId, $syncrotonContact) = $this->testUpdateEntry($syncrotonFolder);
  158. $controller = Syncroton_Data_Factory::factory($this->_class, $this->_getDevice(Syncroton_Model_Device::TYPE_IPHONE), new Tinebase_DateTime(null, null, 'de_DE'));
  159. $changedEntries = $controller->getChangedEntries($syncrotonFolder->serverId, new DateTime('2000-01-01'));
  160. $this->assertContains($serverId, $changedEntries, 'did not get changed record id in ' . print_r($changedEntries, TRUE));
  161. }
  162. /**
  163. * test get changed entries for android
  164. */
  165. public function testGetChangedEntriesAndroid()
  166. {
  167. $syncrotonFolder = $this->testCreateFolder();
  168. list($serverId, $syncrotonContact) = $this->testUpdateEntry($syncrotonFolder);
  169. $controller = Syncroton_Data_Factory::factory($this->_class, $this->_getDevice(Syncroton_Model_Device::TYPE_ANDROID), Tinebase_DateTime::now());
  170. $changedEntries = $controller->getChangedEntries($this->_specialFolderName, new Tinebase_DateTime('2000-01-01'));
  171. $this->assertContains($serverId, $changedEntries, 'did not get changed record id in ' . print_r($changedEntries, TRUE));
  172. }
  173. /**
  174. * test convert from XML to Tine 2.0 model
  175. */
  176. abstract public function testCreateEntry($syncrotonFolder = null);
  177. /**
  178. * test xml generation for sync to client
  179. */
  180. abstract public function testUpdateEntry($syncrotonFolder = null);
  181. /**
  182. * get application activesync controller
  183. *
  184. * @param ActiveSync_Model_Device $_device
  185. */
  186. protected function _getController(ActiveSync_Model_Device $_device)
  187. {
  188. if ($this->_controller === null) {
  189. $this->_controller = Syncroton_Data_Factory::factory($this->_class, $_device, new Tinebase_DateTime(null, null, 'de_DE'));
  190. }
  191. return $this->_controller;
  192. }
  193. }