PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/tine20/Tinebase/GroupTest.php

https://gitlab.com/rsilveira1987/Expresso
PHP | 320 lines | 175 code | 58 blank | 87 comment | 4 complexity | b65bbad2fabaec2a531a04acae1b66f8 MD5 | raw file
  1. <?php
  2. /**
  3. * Tine 2.0 - http://www.tine20.org
  4. *
  5. * @package Tinebase
  6. * @subpackage Group
  7. * @license http://www.gnu.org/licenses/agpl.html
  8. * @copyright Copyright (c) 2008-2015 Metaways Infosystems GmbH (http://www.metaways.de)
  9. * @author Philipp Schüle <p.schuele@metaways.de>
  10. */
  11. /**
  12. * Test class for Tinebase_Group
  13. */
  14. class Tinebase_GroupTest extends TestCase
  15. {
  16. /**
  17. * @var array test objects
  18. */
  19. protected $objects = array();
  20. /**
  21. * remove group members, too when deleting groups
  22. *
  23. * @var boolean
  24. */
  25. protected $_removeGroupMembers = false;
  26. /**
  27. * set up tests
  28. */
  29. protected function setUp()
  30. {
  31. parent::setUp();
  32. Tinebase_Group::getInstance()->resetClassCache();
  33. }
  34. /**
  35. * try to add a group
  36. *
  37. * @return Tinebase_Model_Group
  38. */
  39. public function testAddGroup()
  40. {
  41. $group = Tinebase_Group::getInstance()->addGroup(new Tinebase_Model_Group(array(
  42. 'name' => 'tine20phpunit' . Tinebase_Record_Abstract::generateUID(),
  43. 'description' => 'Group from test testAddGroup'
  44. )));
  45. $this->_groupIdsToDelete[] = $group->getId();
  46. $this->assertEquals(Tinebase_Model_Group::VISIBILITY_HIDDEN, $group->visibility);
  47. return $group;
  48. }
  49. /**
  50. * try to get all groups containing Managers in their name
  51. */
  52. public function testGetGroups()
  53. {
  54. $group = $this->testAddGroup();
  55. $groups = Tinebase_Group::getInstance()->getGroups($group->name);
  56. $this->assertEquals(1, count($groups));
  57. }
  58. /**
  59. * try to get the group with the name tine20phpunit
  60. *
  61. */
  62. public function testGetGroupByName()
  63. {
  64. $group = $this->testAddGroup();
  65. $fetchedGroup = Tinebase_Group::getInstance()->getGroupByName($group->name);
  66. $this->assertEquals($group->name, $fetchedGroup->name);
  67. }
  68. /**
  69. * try to get a group by
  70. *
  71. */
  72. public function testGetGroupById()
  73. {
  74. $adminGroup = Tinebase_Group::getInstance()->getGroupByName('Administrators');
  75. $group = Tinebase_Group::getInstance()->getGroupById($adminGroup->id);
  76. $this->assertEquals($adminGroup->id, $group->id);
  77. }
  78. /**
  79. * try to update a group
  80. *
  81. */
  82. public function testUpdateGroup()
  83. {
  84. $testGroup = $this->testAddGroup();
  85. $testGroup->visibility = 'displayed';
  86. $testGroup->list_id = null;
  87. $group = Tinebase_Group::getInstance()->updateGroup($testGroup);
  88. $this->assertEquals($testGroup->name, $group->name);
  89. $this->assertEquals($testGroup->description, $group->description);
  90. $this->assertEquals('hidden', $group->visibility);
  91. }
  92. /**
  93. * try to set/get group members
  94. *
  95. * @return Tinebase_Model_Group
  96. */
  97. public function testSetGroupMembers($testGroup = null, $testGroupMembersArray = null)
  98. {
  99. if ($testGroup === null) {
  100. $testGroup = $this->testAddGroup();
  101. }
  102. if ($testGroupMembersArray === null) {
  103. $testGroupMembersArray = array($this->_personas['sclever']->accountId, $this->_personas['pwulf']->accountId);
  104. }
  105. Tinebase_Group::getInstance()->setGroupMembers($testGroup->getId(), $testGroupMembersArray);
  106. $getGroupMembersArray = Tinebase_Group::getInstance()->getGroupMembers($testGroup->getId());
  107. $this->assertEquals(sort($testGroupMembersArray), sort($getGroupMembersArray));
  108. return $testGroup;
  109. }
  110. /**
  111. * try to add a group member
  112. */
  113. public function testAddGroupMember()
  114. {
  115. $testGroup = $this->testSetGroupMembers();
  116. Tinebase_Group::getInstance()->addGroupMember($testGroup->getId(), $this->_personas['jmcblack']->accountId);
  117. $getGroupMembersArray = Tinebase_Group::getInstance()->getGroupMembers($testGroup->getId());
  118. $expectedValues = array($this->_personas['sclever']->accountId, $this->_personas['pwulf']->accountId, $this->_personas['jmcblack']->accountId);
  119. $this->assertEquals(sort($expectedValues), sort($getGroupMembersArray));
  120. }
  121. /**
  122. * try to remove a group member
  123. */
  124. public function testRemoveGroupMember()
  125. {
  126. $testGroup = $this->testSetGroupMembers();
  127. Tinebase_Group::getInstance()->removeGroupMember($testGroup->getId(), $this->_personas['sclever']->accountId);
  128. $getGroupMembersArray = Tinebase_Group::getInstance()->getGroupMembers($testGroup->getId());
  129. $this->assertEquals(1, count($getGroupMembersArray));
  130. $expectedValues = array($this->_personas['pwulf']->accountId);
  131. $this->assertEquals(sort($expectedValues), sort($getGroupMembersArray));
  132. }
  133. /**
  134. * try to delete a group
  135. */
  136. public function testDeleteGroup()
  137. {
  138. $testGroup = $this->testAddGroup();
  139. Tinebase_Group::getInstance()->deleteGroups($testGroup);
  140. $this->setExpectedException('Tinebase_Exception_Record_NotDefined');
  141. $group = Tinebase_Group::getInstance()->getGroupById($testGroup);
  142. }
  143. /**
  144. * try to convert group id and check if correct exceptions are thrown
  145. */
  146. public function testConvertGroupIdToInt()
  147. {
  148. $this->setExpectedException('Tinebase_Exception_InvalidArgument');
  149. Tinebase_Model_Group::convertGroupIdToInt (0);
  150. }
  151. /**
  152. * try to convert id of group object and check if correct exceptions are thrown
  153. */
  154. public function testConvertGroupIdToIntWithGroup()
  155. {
  156. $this->setExpectedException('Tinebase_Exception_InvalidArgument');
  157. Tinebase_Model_Group::convertGroupIdToInt(new Tinebase_Model_Group(array(
  158. 'name' => 'tine20phpunit noid',
  159. 'description' => 'noid group'
  160. )));
  161. }
  162. /**
  163. * testGetDefaultGroup
  164. */
  165. public function testGetDefaultGroup()
  166. {
  167. $group = Tinebase_Group::getInstance()->getDefaultGroup();
  168. $expectedGroupName = Tinebase_User::getBackendConfiguration(Tinebase_User::DEFAULT_USER_GROUP_NAME_KEY);
  169. $this->assertEquals($expectedGroupName, $group->name);
  170. }
  171. /**
  172. * testGetDefaultAdminGroup
  173. */
  174. public function testGetDefaultAdminGroup()
  175. {
  176. $group = Tinebase_Group::getInstance()->getDefaultAdminGroup();
  177. $expectedGroupName = Tinebase_User::getBackendConfiguration(Tinebase_User::DEFAULT_ADMIN_GROUP_NAME_KEY);
  178. $this->assertEquals($expectedGroupName, $group->name);
  179. }
  180. /**
  181. * testSetGroupMemberships
  182. */
  183. public function testSetGroupMemberships()
  184. {
  185. $currentGroupMemberships = $this->_personas['pwulf']->getGroupMemberships();
  186. $newGroupMemberships = current($currentGroupMemberships);
  187. Tinebase_Group::getInstance()->setGroupMemberships($this->_personas['pwulf'], array($newGroupMemberships));
  188. $newGroupMemberships = $this->_personas['pwulf']->getGroupMemberships();
  189. if (count($currentGroupMemberships) > 1) {
  190. $this->assertNotEquals($currentGroupMemberships, $newGroupMemberships);
  191. }
  192. Tinebase_Group::getInstance()->setGroupMemberships($this->_personas['pwulf'], $currentGroupMemberships);
  193. $newGroupMemberships = $this->_personas['pwulf']->getGroupMemberships();
  194. $this->assertEquals(sort($currentGroupMemberships), sort($newGroupMemberships));
  195. }
  196. /**
  197. * testSyncLists
  198. *
  199. * @see 0005768: create addressbook lists when migrating users
  200. *
  201. * @todo make this work for LDAP accounts backend: currently the user is not present in sync backend but in sql
  202. */
  203. public function testSyncLists()
  204. {
  205. if (Tinebase_Group::getInstance() instanceof Tinebase_Group_Ldap) {
  206. $this->markTestSkipped('@todo make this work for LDAP accounts backend');
  207. }
  208. $testGroup = $this->testAddGroup();
  209. // don't use any existing persona here => will break other tests
  210. $testUser = new Tinebase_Model_FullUser(array(
  211. 'accountLoginName' => Tinebase_Record_Abstract::generateUID(),
  212. 'accountPrimaryGroup' => $testGroup->getId(),
  213. 'accountDisplayName' => Tinebase_Record_Abstract::generateUID(),
  214. 'accountLastName' => Tinebase_Record_Abstract::generateUID(),
  215. 'accountFullName' => Tinebase_Record_Abstract::generateUID(),
  216. 'visibility' => Tinebase_Model_User::VISIBILITY_DISPLAYED
  217. ));
  218. $contact = Admin_Controller_User::getInstance()->createOrUpdateContact($testUser);
  219. $testUser->contact_id = $contact->getId();
  220. $testUser = Tinebase_User::getInstance()->addUserInSqlBackend($testUser);
  221. Tinebase_User::createContactForSyncedUser($testUser);
  222. Tinebase_User::getInstance()->updateUserInSqlBackend($testUser);
  223. $this->testSetGroupMembers($testGroup, array($testUser->accountId));
  224. Tinebase_Group::syncListsOfUserContact(array($testGroup->getId()), $testUser->contact_id);
  225. $group = Tinebase_Group::getInstance()->getGroupById($testGroup);
  226. $this->assertTrue(! empty($group->list_id), 'list id empty: ' . print_r($group->toArray(), TRUE));
  227. $list = Addressbook_Controller_List::getInstance()->get($group->list_id);
  228. $this->assertEquals($group->getId(), $list->group_id);
  229. $this->assertEquals($group->name, $list->name);
  230. $this->assertTrue(! empty($list->members), 'list members empty: ' . print_r($list->toArray(), TRUE)
  231. . ' should contain: ' . print_r($testUser->toArray(), TRUE));
  232. $this->assertContains($testUser->contact_id, $list->members);
  233. $appConfigDefaults = Admin_Controller::getInstance()->getConfigSettings();
  234. $this->assertTrue(! empty($appConfigDefaults), 'app config defaults empty');
  235. $internal = $appConfigDefaults[Admin_Model_Config::DEFAULTINTERNALADDRESSBOOK];
  236. $this->assertEquals($internal, $list->container_id, 'did not get correct internal container');
  237. // sync again -> should not change anything
  238. Tinebase_Group::syncListsOfUserContact(array($group->getId()), $testUser->contact_id);
  239. $listAgain = Addressbook_Controller_List::getInstance()->get($group->list_id);
  240. $this->assertEquals($list->toArray(), $listAgain->toArray());
  241. // change list id -> should get list by (group) name
  242. $group->list_id = NULL;
  243. $group = Tinebase_Group::getInstance()->updateGroup($group);
  244. Tinebase_Group::syncListsOfUserContact(array($group->getId()), $testUser->contact_id);
  245. $this->assertEquals($list->getId(), Tinebase_Group::getInstance()->getGroupById($group)->list_id);
  246. }
  247. /**
  248. * testRemoveAccountPrimaryGroup
  249. *
  250. * @see 0007384: deleting a group that is an accounts primary group fails
  251. */
  252. public function testRemoveAccountPrimaryGroup()
  253. {
  254. $testGroup = $this->testAddGroup();
  255. $sclever = Tinebase_User::getInstance()->getFullUserByLoginName('sclever');
  256. $sclever->accountPrimaryGroup = $testGroup->getId();
  257. Tinebase_User::getInstance()->updateUser($sclever);
  258. Tinebase_Group::getInstance()->deleteGroups($testGroup);
  259. $sclever = Tinebase_User::getInstance()->getFullUserByLoginName('sclever');
  260. $this->assertEquals(Tinebase_Group::getInstance()->getDefaultGroup()->getId(), $sclever->accountPrimaryGroup);
  261. }
  262. }