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