0010030: shadow expire = -1 in LDAP should be mapped to "infinite"
[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 PHPUnit_Framework_TestCase
21 {
22     /**
23      * ldap group backend
24      *
25      * @var Tinebase_User_LDAP
26      */
27     protected $_backend = NULL;
28         
29     /**
30      * @var array test objects
31      */
32     protected $objects = array();
33
34     /**
35      * Sets up the fixture.
36      * This method is called before a test is executed.
37      *
38      * @access protected
39      */
40     protected function setUp()
41     {
42         if (Tinebase_User::getConfiguredBackend() !== Tinebase_User::LDAP) {
43             $this->markTestSkipped('LDAP backend not enabled');
44         }
45         $this->_backend = Tinebase_User::factory(Tinebase_User::LDAP);
46         
47         $this->objects['users'] = array();
48     }
49     
50     /**
51      * Tears down the fixture
52      * This method is called after a test is executed.
53      *
54      * @access protected
55      */
56     protected function tearDown()
57     {
58         foreach ($this->objects['users'] as $user) {
59             $this->_backend->deleteUser($user);
60         }
61     }
62     
63     /**
64      * try to add an user
65      * 
66      * @return Tinebase_Model_FullUser
67      */
68     public function testAddUser()
69     {
70         $user = $this->getTestRecord();
71         
72         $testUser = $this->_backend->addUser($user);
73         $this->objects['users']['testUser'] = $testUser;
74         
75         $this->assertEquals($user->accountLoginName, $testUser->accountLoginName);
76         $this->assertEquals('Tinebase_Model_FullUser', get_class($testUser), 'wrong type');
77         
78         return $user;
79     }
80     
81     /**
82      * try to get all users containing phpunit in there name
83      */
84     public function testGetUsers()
85     {
86         $user = $this->testAddUser();
87         
88         $users = $this->_backend->getUsers('phpunit', 'accountStatus');
89         
90         $this->assertGreaterThanOrEqual(1, count($users));
91     }
92     
93     /**
94      * try to get an user by loginname
95      *
96      */
97     public function testGetUserByLoginName()
98     {
99         // add a test user
100         $user = $this->testAddUser();
101         
102         $testUser = $this->_backend->getFullUserByLoginName($user->accountLoginName);
103         
104         $this->assertEquals($user->accountLoginName, $testUser->accountLoginName);
105         $this->assertEquals('Tinebase_Model_FullUser', get_class($testUser), 'wrong type');
106     }
107     
108     /**
109      * try to get an user by userId
110      *
111      */
112     public function testGetUserById()
113     {
114         $user = $this->testAddUser();
115         
116         $testUser = $this->_backend->getFullUserById($user->getId());
117         
118         $this->assertEquals($user->accountLoginName, $testUser->accountLoginName);
119         $this->assertEquals('Tinebase_Model_FullUser', get_class($testUser), 'wrong type');
120     }
121         
122     /**
123      * try to update an user
124      *
125      */
126     public function testUpdateUser()
127     {
128         $groupsBackend = Tinebase_Group::factory(Tinebase_Group::LDAP);
129         
130         $user = $this->testAddUser();
131         $groupsBackend->addGroupMemberInSyncBackend($user->accountPrimaryGroup, $user);
132         $groupsBeforeUpdate = $groupsBackend->getGroupMembershipsFromSyncBackend($user);
133         
134         $user->accountLoginName = 'tine20phpunituser-updated';
135         $testUser = $this->_backend->updateUser($user);
136         $groupsAfterUpdate = $groupsBackend->getGroupMembershipsFromSyncBackend($testUser);
137         
138         sort($groupsBeforeUpdate);
139         sort($groupsAfterUpdate);
140         
141         $this->assertEquals($user->accountLoginName, $testUser->accountLoginName);
142         $this->assertEquals($groupsBeforeUpdate, $groupsAfterUpdate);
143     }
144     
145     /**
146      * try to enable an account
147      *
148      */
149     public function testSetStatus()
150     {
151         $user = $this->testAddUser();
152
153         $this->_backend->setStatus($user, Tinebase_User::STATUS_DISABLED);
154         
155         $testUser = $this->_backend->getUserById($user, 'Tinebase_Model_FullUser');
156         
157         $this->assertEquals(Tinebase_User::STATUS_DISABLED, $testUser->accountStatus);
158         
159         
160         $this->_backend->setStatus($user, Tinebase_User::STATUS_ENABLED);
161         
162         $testUser = $this->_backend->getUserById($user, 'Tinebase_Model_FullUser');
163         
164         $this->assertEquals(Tinebase_User::STATUS_ENABLED, $testUser->accountStatus);
165     }
166     
167     /**
168      * try to update the logintimestamp
169      *
170      */
171     public function testSetLoginTime()
172     {
173         $user = $this->testAddUser();
174         
175         $this->_backend->setLoginTime($user, '127.0.0.1');
176         
177         $testUser = $this->_backend->getUserById($user, 'Tinebase_Model_FullUser');
178         
179         $this->assertNotEquals($user->accountLastLogin, $testUser->accountLastLogin);
180     }
181     
182     /**
183      * try to set password
184      *
185      */
186     public function testSetPassword()
187     {
188         $user = $this->testAddUser();
189
190         $this->_backend->setPassword($user, Tinebase_Record_Abstract::generateUID());
191         
192         $testUser = $this->_backend->getUserById($user, 'Tinebase_Model_FullUser');
193         
194         $this->assertNotEquals($user->accountLastPasswordChange, $testUser->accountLastPasswordChange);
195     }
196     
197     /**
198      * try to set the expirydate
199      *
200      */
201     public function testSetExpiryDate()
202     {
203         $user = $this->testAddUser();
204         
205         $this->_backend->setExpiryDate($user, Tinebase_DateTime::now()->subDay(1));
206         
207         $testUser = $this->_backend->getUserById($user, 'Tinebase_Model_FullUser');
208         
209         $this->assertEquals('Tinebase_DateTime', get_class($testUser->accountExpires), 'wrong type');
210         $this->assertEquals(Tinebase_User::STATUS_EXPIRED, $testUser->accountStatus);
211
212         $this->_backend->setExpiryDate($user, NULL);
213         
214         $testUser = $this->_backend->getUserById($user, 'Tinebase_Model_FullUser');
215         
216         $this->assertEquals(NULL, $testUser->accountExpires);
217         $this->assertEquals(Tinebase_User::STATUS_ENABLED, $testUser->accountStatus);
218     }
219     
220     /**
221      * try to delete an user
222      *
223      */
224     public function testDeleteUser()
225     {
226         $user = $this->testAddUser();
227         
228         $this->_backend->deleteUser($user);
229         unset($this->objects['users']['testUser']);
230         
231         $this->setExpectedException('Tinebase_Exception_NotFound');
232         
233         $testUser = $this->_backend->getUserById($user, 'Tinebase_Model_FullUser');
234     }
235     
236     /**
237      * execute Tinebase_User::syncUser
238      */
239     public function testSyncUser()
240     {
241         $user = $this->testAddUser();
242         
243         Tinebase_User::syncUser($user, Tinebase_Application::getInstance()->isInstalled('Addressbook'));
244     }
245         
246     /**
247      * @return Tinebase_Model_FullUser
248      */
249     public static function getTestRecord()
250     {
251         $config = Zend_Registry::get('testConfig');
252         $emailDomain = ($config->maildomain) ? $config->maildomain : 'tine20.org';
253         
254         $user  = new Tinebase_Model_FullUser(array(
255             'accountLoginName'      => 'tine20phpunituser',
256             'accountStatus'         => 'enabled',
257             'accountExpires'        => NULL,
258             'accountPrimaryGroup'   => Tinebase_Group::getInstance()->getDefaultGroup()->id,
259             'accountLastName'       => 'Tine 2.0',
260             'accountFirstName'      => 'PHPUnit User',
261             'accountEmailAddress'   => 'phpunit@' . $emailDomain
262         ));
263         
264         return $user;
265     }
266 }