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