fixes/improves some more tests for ldap backend
[tine20] / tests / tine20 / Tinebase / Group / LdapTest.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-2016 Metaways Infosystems GmbH (http://www.metaways.de)
9  * @author      Lars Kneschke <l.kneschke@metaways.de>
10  *
11  * TODO extend TestCase to use generic cleanup
12  */
13
14 /**
15  * Test helper
16  */
17 require_once dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'TestHelper.php';
18
19 /**
20  * Test class for Tinebase_Group
21  */
22 class Tinebase_Group_LdapTest extends PHPUnit_Framework_TestCase
23 {
24     /**
25      * sql group backend
26      *
27      * @var Tinebase_Group_Sql
28      */
29     protected $_groupSQL = NULL;
30     
31     /**
32      * ldap group backend
33      *
34      * @var Tinebase_Group_LDAP
35      */
36     protected $_groupLDAP = NULL;
37     
38     /**
39      * ldap user backend
40      *
41      * @var Tinebase_User_Ldap
42      */
43     protected $_userLDAP = NULL;
44     
45     /**
46      * @var array test objects
47      */
48     protected $objects = array();
49
50     /**
51      * Sets up the fixture.
52      * This method is called before a test is executed.
53      *
54      * @access protected
55      */
56     protected function setUp()
57     {
58         if (Tinebase_User::getConfiguredBackend() !== Tinebase_User::LDAP) {
59             $this->markTestSkipped('LDAP backend not enabled');
60         }
61
62         $this->_groupLDAP = Tinebase_Group::factory(Tinebase_Group::LDAP);
63         $this->_userLDAP  = Tinebase_User::factory(Tinebase_User::LDAP);
64         $this->_groupSQL  = Tinebase_Group::factory(Tinebase_Group::SQL);
65                 
66         $this->objects['initialGroup'] = new Tinebase_Model_Group(array(
67             'name'          => 'tine20phpunit',
68             'description'   => 'initial group'
69         ));
70         
71         $this->objects['updatedGroup'] = new Tinebase_Model_Group(array(
72             'name'          => 'tine20phpunit updated',
73             'description'   => 'updated group'
74         ));
75          
76         $this->objects['initialAccount'] = new Tinebase_Model_FullUser(array(
77             'accountLoginName'      => 'tine20phpunit',
78             'accountStatus'         => 'enabled',
79             'accountExpires'        => NULL,
80             'accountPrimaryGroup'   => 'must be set to valid groupid',
81             'accountLastName'       => 'Tine 2.0',
82             'accountFirstName'      => 'PHPUnit',
83             'accountEmailAddress'   => 'phpunit@metaways.de'
84         ));
85
86         $this->objects['groups'] = new Tinebase_Record_RecordSet('Tinebase_Model_Group');
87         $this->objects['users'] = new Tinebase_Record_RecordSet('Tinebase_Model_FullUser');
88     }
89
90     /**
91      * Tears down the fixture
92      * This method is called after a test is executed.
93      *
94      * @access protected
95      */
96     protected function tearDown()
97     {
98         if (Tinebase_User::getConfiguredBackend() !== Tinebase_User::LDAP) {
99             return;
100         }
101
102         $this->_groupLDAP->deleteGroups($this->objects['groups']);
103         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__
104             . ' Deleting users: ' . print_r($this->objects['users']->toArray(), true));
105
106         try {
107             $this->_userLDAP->deleteUsers($this->objects['users']->getArrayOfIds());
108         } catch (Tinebase_Exception_NotFound $tenf) {
109
110         }
111     }
112     
113     /**
114      * try to add a group
115      *
116      * @return Tinebase_Model_Group
117      */
118     public function testAddGroup()
119     {
120         $group = $this->_groupLDAP->addGroup($this->objects['initialGroup']);
121         $this->objects['groups']->addRecord($group);
122         
123         $this->assertNotNull($group->id);
124         $this->assertEquals($this->objects['initialGroup']->name, $group->name);
125         return $group;
126     }
127     
128     /**
129      * try to get all groups containing phpunit in their name
130      *
131      */
132     public function testGetGroups()
133     {
134         $this->testAddGroup();
135         $groups = $this->_groupLDAP->getGroups('phpunit');
136         
137         $this->assertEquals(1, count($groups));
138     }
139     
140     /**
141      * try to get the group with the name tine20phpunit
142      *
143      */
144     public function testGetGroupByName()
145     {
146         $this->testAddGroup();
147         $group = $this->_groupLDAP->getGroupByName('tine20phpunit');
148         
149         $this->assertEquals($this->objects['initialGroup']->name, $group->name);
150     }
151     
152     /**
153      * try to get a group by
154      *
155      */
156     public function testGetGroupById()
157     {
158         $this->testAddGroup();
159         $group = $this->_groupLDAP->getGroupByName($this->objects['initialGroup']->name);
160         
161         $group = $this->_groupLDAP->getGroupById($group->getId());
162         
163         $this->assertEquals($this->objects['initialGroup']->name, $group->name);
164     }
165     
166     /**
167      * test setting group members
168      *
169      */
170     public function testSetGroupMembers()
171     {
172         $group = $this->testAddGroup();
173
174         $user = $this->_addUserToGroup($group);
175         $this->_groupLDAP->setGroupMembers($group, array($user));
176         
177         $groupMemberships = $this->_groupLDAP->getGroupMembershipsFromSyncBackend($user);
178         
179         $this->assertEquals(1, count($groupMemberships));
180         
181         $this->_groupLDAP->removeGroupMember($group, $user);
182         
183         $this->_userLDAP->deleteUser($user);
184     }
185
186     protected function _addUserToGroup($group)
187     {
188         $this->objects['initialAccount']->accountPrimaryGroup = $group->getId();
189         $user = $this->_userLDAP->addUser($this->objects['initialAccount']);
190         $this->objects['users']->addRecord($user);
191         return $user;
192     }
193     
194     /**
195      * test setting no group members
196      *
197      */
198     public function testSetNoGroupMembers()
199     {
200         $group = $this->testAddGroup();
201
202         $this->objects['initialAccount']->accountPrimaryGroup = $group->getId();
203         $user = $this->_userLDAP->addUser($this->objects['initialAccount']);
204         $this->objects['users']->addRecord($user);
205
206         $this->_groupLDAP->setGroupMembers($group, array($user));
207         
208         $this->_groupLDAP->setGroupMembers($group, array());
209         
210         $groupMembers = $this->_groupLDAP->getGroupMembers($group);
211         
212         $this->assertEquals(0, count($groupMembers));
213         
214         $this->_userLDAP->deleteUser($user);
215     }
216     
217     /**
218      * test adding group members
219      *
220      */
221     public function testAddGroupMember()
222     {
223         $group = $this->testAddGroup();
224
225         $this->objects['initialAccount']->accountPrimaryGroup = $group->getId();
226         $user = $this->_userLDAP->addUser($this->objects['initialAccount']);
227         $this->objects['users']->addRecord($user);
228         
229         $this->_groupLDAP->addGroupMember($group, $user);
230         
231         $groupMembers = $this->_groupLDAP->getGroupMembers($group);
232         
233         $this->assertEquals(1, count($groupMembers));
234         
235         $this->_groupLDAP->removeGroupMember($group, $user);
236         
237         $this->_userLDAP->deleteUser($user);
238     }
239     
240     /**
241      * test deleting groupmembers
242      *
243      */
244     public function testRemoveGroupMember()
245     {
246         $group = $this->testAddGroup();
247
248         $this->objects['initialAccount']->accountPrimaryGroup = $group->getId();
249         $user = $this->_userLDAP->addUser($this->objects['initialAccount']);
250         $this->objects['users']->addRecord($user);
251         
252         $this->_groupLDAP->addGroupMember($group, $user);
253                 
254         $this->_groupLDAP->removeGroupMember($group, $user);
255         
256         $groupMembers = $this->_groupLDAP->getGroupMembers($group);
257         
258         $this->assertEquals(0, count($groupMembers));
259         
260         $this->_userLDAP->deleteUser($user);
261     }
262     
263     /**
264      * try to update a group
265      *
266      */
267     public function testUpdateGroup()
268     {
269         $group = $this->testAddGroup();
270
271         $this->objects['updatedGroup']->id = $group->getId();
272         $group = $this->_groupLDAP->updateGroup($this->objects['updatedGroup']);
273         
274         $this->assertEquals($this->objects['updatedGroup']->name, $group->name);
275         $this->assertEquals($this->objects['updatedGroup']->description, $group->description);
276     }
277     
278     /**
279      * try to delete a group
280      *
281      */
282     public function testDeleteGroups()
283     {
284         $group = $this->testAddGroup();
285         $this->_groupLDAP->deleteGroups($group->getId());
286
287         $this->setExpectedException('Exception');
288
289         $group = $this->_groupLDAP->getGroupById($group->getId());
290     }
291
292     /**
293      * @see 0009852: improve cache cleaning after LDAP sync
294      */
295     public function testSyncGroups()
296     {
297         $defaultUserGroup = Tinebase_Group::getInstance()->getDefaultGroup();
298
299         $group = $this->testAddGroup();
300         $user = $this->_addUserToGroup($group);
301
302         // add user to group (only in LDAP)
303         $this->_groupLDAP->addGroupMemberInSyncBackend($defaultUserGroup->getId(), $user);
304
305         // trigger caching
306         $memberships = $this->_groupLDAP->getGroupMembers($defaultUserGroup);
307         $this->assertFalse(in_array($user->getId(), $memberships));
308
309         // sync users
310         Tinebase_User::syncUsers(array('syncContactData' => TRUE));
311
312         // check group memberships
313         $memberships = $this->_groupLDAP->getGroupMembers($defaultUserGroup);
314         $this->assertTrue(in_array($user->getId(), $memberships), 'group memberships not updated: ' . print_r($memberships, true));
315     }
316 }