0011192: LDAP sync should delete contacts
[tine20] / tests / tine20 / Tinebase / User / LdapTest.php
1 <?php
2 /**
3  * Tine 2.0 - http://www.tine20.org
4  * 
5  * @package     Tinebase
6  * @subpackage  User
7  * @license     http://www.gnu.org/licenses/agpl.html
8  * @copyright   Copyright (c) 2008-2014 Metaways Infosystems GmbH (http://www.metaways.de)
9  * @author      Lars Kneschke <l.kneschke@metaways.de>
10  */
11
12 /**
13  * Test helper
14  */
15 require_once dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'TestHelper.php';
16
17 /**
18  * Test class for Tinebase_User_Ldap
19  */
20 class Tinebase_User_LdapTest extends TestCase
21 {
22     /**
23      * ldap group backend
24      *
25      * @var Tinebase_User_LDAP
26      */
27     protected $_backend = NULL;
28         
29     /**
30      * Sets up the fixture.
31      * This method is called before a test is executed.
32      *
33      * @access protected
34      */
35     protected function setUp()
36     {
37         if (Tinebase_User::getConfiguredBackend() !== Tinebase_User::LDAP) {
38             $this->markTestSkipped('LDAP backend not enabled');
39         }
40         $this->_backend = Tinebase_User::factory(Tinebase_User::LDAP);
41     }
42     
43     /**
44      * try to add an user
45      * 
46      * @return Tinebase_Model_FullUser
47      */
48     public function testAddUser()
49     {
50         $user = $this->getTestRecord();
51
52         $this->_usernamesToDelete[] = $user->accountLoginName;
53         $testUser = $this->_backend->addUser($user);
54
55         $this->assertEquals($user->accountLoginName, $testUser->accountLoginName);
56         $this->assertEquals('Tinebase_Model_FullUser', get_class($testUser), 'wrong type');
57         
58         return $user;
59     }
60     
61     /**
62      * try to get all users containing phpunit in there name
63      */
64     public function testGetUsers()
65     {
66         $user = $this->testAddUser();
67         
68         $users = $this->_backend->getUsers('phpunit', 'accountStatus');
69         
70         $this->assertGreaterThanOrEqual(1, count($users));
71     }
72     
73     /**
74      * try to get an user by loginname
75      *
76      */
77     public function testGetUserByLoginName()
78     {
79         // add a test user
80         $user = $this->testAddUser();
81         
82         $testUser = $this->_backend->getFullUserByLoginName($user->accountLoginName);
83         
84         $this->assertEquals($user->accountLoginName, $testUser->accountLoginName);
85         $this->assertEquals('Tinebase_Model_FullUser', get_class($testUser), 'wrong type');
86     }
87     
88     /**
89      * try to get an user by userId
90      *
91      */
92     public function testGetUserById()
93     {
94         $user = $this->testAddUser();
95         
96         $testUser = $this->_backend->getFullUserById($user->getId());
97         
98         $this->assertEquals($user->accountLoginName, $testUser->accountLoginName);
99         $this->assertEquals('Tinebase_Model_FullUser', get_class($testUser), 'wrong type');
100     }
101         
102     /**
103      * try to update an user
104      *
105      */
106     public function testUpdateUser()
107     {
108         $groupsBackend = Tinebase_Group::factory(Tinebase_Group::LDAP);
109         
110         $user = $this->testAddUser();
111         $groupsBackend->addGroupMemberInSyncBackend($user->accountPrimaryGroup, $user);
112         $groupsBeforeUpdate = $groupsBackend->getGroupMembershipsFromSyncBackend($user);
113         
114         $user->accountLoginName = 'tine20phpunituser-updated';
115         $testUser = $this->_backend->updateUser($user);
116         $groupsAfterUpdate = $groupsBackend->getGroupMembershipsFromSyncBackend($testUser);
117         
118         sort($groupsBeforeUpdate);
119         sort($groupsAfterUpdate);
120         
121         $this->assertEquals($user->accountLoginName, $testUser->accountLoginName);
122         $this->assertEquals($groupsBeforeUpdate, $groupsAfterUpdate);
123     }
124     
125     /**
126      * try to enable an account
127      *
128      */
129     public function testSetStatus()
130     {
131         $user = $this->testAddUser();
132
133         $this->_backend->setStatus($user, Tinebase_User::STATUS_DISABLED);
134         
135         $testUser = $this->_backend->getUserById($user, 'Tinebase_Model_FullUser');
136         
137         $this->assertEquals(Tinebase_User::STATUS_DISABLED, $testUser->accountStatus);
138         
139         
140         $this->_backend->setStatus($user, Tinebase_User::STATUS_ENABLED);
141         
142         $testUser = $this->_backend->getUserById($user, 'Tinebase_Model_FullUser');
143         
144         $this->assertEquals(Tinebase_User::STATUS_ENABLED, $testUser->accountStatus);
145     }
146     
147     /**
148      * try to update the logintimestamp
149      *
150      */
151     public function testSetLoginTime()
152     {
153         $user = $this->testAddUser();
154         
155         $this->_backend->setLoginTime($user, '127.0.0.1');
156         
157         $testUser = $this->_backend->getUserById($user, 'Tinebase_Model_FullUser');
158         
159         $this->assertNotEquals($user->accountLastLogin, $testUser->accountLastLogin);
160     }
161     
162     /**
163      * try to set password
164      *
165      */
166     public function testSetPassword()
167     {
168         $user = $this->testAddUser();
169
170         $this->_backend->setPassword($user, Tinebase_Record_Abstract::generateUID());
171         
172         $testUser = $this->_backend->getUserById($user, 'Tinebase_Model_FullUser');
173         
174         $this->assertNotEquals($user->accountLastPasswordChange, $testUser->accountLastPasswordChange);
175     }
176     
177     /**
178      * try to set the expirydate
179      *
180      */
181     public function testSetExpiryDate()
182     {
183         $user = $this->testAddUser();
184         
185         $this->_backend->setExpiryDate($user, Tinebase_DateTime::now()->subDay(1));
186         
187         $testUser = $this->_backend->getUserById($user, 'Tinebase_Model_FullUser');
188         
189         $this->assertEquals('Tinebase_DateTime', get_class($testUser->accountExpires), 'wrong type');
190         $this->assertEquals(Tinebase_User::STATUS_EXPIRED, $testUser->accountStatus);
191
192         $this->_backend->setExpiryDate($user, NULL);
193         
194         $testUser = $this->_backend->getUserById($user, 'Tinebase_Model_FullUser');
195         
196         $this->assertEquals(NULL, $testUser->accountExpires);
197         $this->assertEquals(Tinebase_User::STATUS_ENABLED, $testUser->accountStatus);
198     }
199     
200     /**
201      * try to delete an user
202      *
203      */
204     public function testDeleteUser()
205     {
206         $user = $this->testAddUser();
207         
208         $this->_backend->deleteUser($user);
209         unset($this->objects['users']['testUser']);
210         
211         $this->setExpectedException('Tinebase_Exception_NotFound');
212         
213         $testUser = $this->_backend->getUserById($user, 'Tinebase_Model_FullUser');
214     }
215     
216     /**
217      * execute Tinebase_User::syncUser
218      */
219     public function testSyncUser()
220     {
221         $user = $this->testAddUser();
222         
223         Tinebase_User::syncUser($user, Tinebase_Application::getInstance()->isInstalled('Addressbook'));
224     }
225         
226     /**
227      * @return Tinebase_Model_FullUser
228      */
229     public static function getTestRecord()
230     {
231         $config = Zend_Registry::get('testConfig');
232         $emailDomain = ($config->maildomain) ? $config->maildomain : 'tine20.org';
233         
234         $user  = new Tinebase_Model_FullUser(array(
235             'accountLoginName'      => 'tine20phpunituser',
236             'accountStatus'         => 'enabled',
237             'accountExpires'        => NULL,
238             'accountPrimaryGroup'   => Tinebase_Group::getInstance()->getDefaultGroup()->id,
239             'accountLastName'       => 'Tine 2.0',
240             'accountFirstName'      => 'PHPUnit User',
241             'accountEmailAddress'   => 'phpunit@' . $emailDomain,
242         ));
243         
244         return $user;
245     }
246
247     /**
248      * @see 0011192: LDAP sync should delete contacts
249      */
250     public function testSyncDeleted()
251     {
252         $user = $this->testAddUser();
253
254         // add user contact
255         $contact = Admin_Controller_User::getInstance()->createOrUpdateContact($user);
256         $user->contact_id = $contact->getId();
257         Tinebase_User::getInstance()->updateUser($user);
258
259         // delete user in ldap
260         Tinebase_User::getInstance()->deleteUserInSyncBackend($user->getId());
261
262         // check if still in tine20 db
263         $sqlUser = Tinebase_User::getInstance()->getUserByPropertyFromSqlBackend('accountId', $user->getId());
264         $this->assertEquals($user->getId(), $sqlUser->getId());
265
266         // set sync config/option + start user sync
267         $syncOptions = array('deleteUsers' => true);
268         Tinebase_User::syncUsers($syncOptions);
269
270         $now = Tinebase_DateTime::now();
271         $user = Tinebase_User::getInstance()->getUserByPropertyFromSqlBackend('accountId', $user->getId(), 'Tinebase_Model_FullUser');
272         $this->assertEquals($now->toString(), $user->accountExpires->toString(), 'user should be expired');
273
274         sleep(1);
275         Tinebase_User::syncUsers($syncOptions);
276         $user = Tinebase_User::getInstance()->getUserByPropertyFromSqlBackend('accountId', $user->getId(), 'Tinebase_Model_FullUser');
277         $this->assertEquals($now->toString(), $user->accountExpires->toString(), 'expiry date should still be the same');
278
279         // set expired to -1 year -> user should be deleted
280         $user->accountExpires = $now->subYear(1);
281         Tinebase_User::getInstance()->updateUserInSqlBackend($user);
282
283         // sync again
284         sleep(1);
285         Tinebase_User::syncUsers($syncOptions);
286
287         // check if user is deleted in tine, too
288         try {
289             Tinebase_User::getInstance()->getUserByPropertyFromSqlBackend('accountId', $user->getId());
290             $this->fail('user should be deleted from tine20 db');
291         } catch (Tinebase_Exception_NotFound $tenf) {
292             $this->assertContains('User with accountId = ' . $sqlUser->getId(), $tenf->getMessage());
293         }
294
295         // check if user contact is deleted, too
296         try {
297             Addressbook_Controller_Contact::getInstance()->get($sqlUser->contact_id);
298             $this->fail('user contact should be deleted from tine20 db');
299         } catch (Tinebase_Exception_NotFound $tenf) {
300             $this->assertContains('Addressbook_Model_Contact record with id = ' . $sqlUser->contact_id, $tenf->getMessage());
301         }
302     }
303 }