ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
ilObjUserPasswordTest.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2014 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 require_once 'Services/User/classes/class.ilUserPasswordManager.php';
5 require_once 'Services/User/classes/class.ilUserPasswordEncoderFactory.php';
6 require_once 'Services/Password/classes/class.ilBasePasswordEncoder.php';
7 require_once 'Services/Utilities/classes/class.ilUtil.php';
8 require_once 'Services/User/classes/class.ilObjUser.php';
9 require_once 'Services/User/exceptions/class.ilUserException.php';
10 require_once 'Services/User/test/ilUserBaseTest.php';
11 
18 {
22  const PASSWORD = 'password';
23 
27  const ENCODED_PASSWORD = 'encoded';
28 
33  protected function setUp()
34  {
35  if(!defined('CLIENT_DATA_DIR'))
36  {
37  define('CLIENT_DATA_DIR', '/tmp');
38  }
39 
40  parent::setUp();
41  }
42 
47  {
48  $this->assertException(ilUserException::class);
50  }
51 
56  {
57  $this->assertException(ilUserException::class);
59  array(
60  'password_encoder' => 'md5'
61  )
62  );
63  }
64 
69  {
70  $this->assertException(PHPUnit_Framework_Error::class);
71  try
72  {
74  array(
75  'password_encoder' => 'md5',
76  'encoder_factory' => 'test'
77  )
78  );
79  }
80  catch(TypeError $e)
81  {
82  throw new PHPUnit_Framework_Error($e->getMessage(), $e->getCode(), $e->getFile(), $e->getLine());
83  }
84  }
85 
89  public function testInstanceCanBeCreated()
90  {
91  $factory_mock = $this->getMockBuilder('ilUserPasswordEncoderFactory')->disableOriginalConstructor()->getMock();
92  $factory_mock->expects($this->exactly(2))->method('getSupportedEncoderNames')->will($this->onConsecutiveCalls(
93  array(
94  'mockencoder', 'second_mockencoder'
95  ),
96  array(
97  'mockencoder'
98  )
99  ));
100 
101  $password_manager = new ilUserPasswordManager(
102  array(
103  'password_encoder' => 'md5',
104  'encoder_factory' => $factory_mock
105  )
106  );
107  $this->assertInstanceOf('ilUserPasswordManager', $password_manager);
108  $this->assertEquals('md5', $password_manager->getEncoderName());
109  $this->assertEquals($factory_mock, $password_manager->getEncoderFactory());
110 
111  $this->assertTrue($password_manager->isEncodingTypeSupported('second_mockencoder'));
112  $this->assertFalse($password_manager->isEncodingTypeSupported('second_mockencoder'));
113  }
114 
119  {
120  $user_mock = $this->getMockBuilder('ilObjUser')->disableOriginalConstructor()->getMock();
121  $encoder = $this->getMockBuilder('ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
122  $factory_mock = $this->getMockBuilder('ilUserPasswordEncoderFactory')->disableOriginalConstructor()->getMock();
123 
124  $user_mock->expects($this->once())->method('setPasswordSalt')->with($this->isType('string'));
125  $user_mock->expects($this->once())->method('getPasswordSalt')->will($this->returnValue('asuperrandomsalt'));
126  $user_mock->expects($this->once())->method('setPasswordEncodingType')->with($this->equalTo('mockencoder'));
127  $user_mock->expects($this->once())->method('setPasswd')->with($this->equalTo(self::ENCODED_PASSWORD), $this->equalTo(IL_PASSWD_CRYPTED));
128 
129  $encoder->expects($this->once())->method('getName')->will($this->returnValue('mockencoder'));
130  $encoder->expects($this->once())->method('requiresSalt')->will($this->returnValue(true));
131  $encoder->expects($this->once())->method('encodePassword')->with($this->equalTo(self::PASSWORD), $this->isType('string'))->will($this->returnValue(self::ENCODED_PASSWORD));
132 
133  $factory_mock->expects($this->once())->method('getEncoderByName')->will($this->returnValue($encoder));
134 
135  $password_manager = new ilUserPasswordManager(
136  array(
137  'password_encoder' => 'mockencoder',
138  'encoder_factory' => $factory_mock
139  )
140  );
141 
142  $password_manager->encodePassword($user_mock, self::PASSWORD);
143  }
144 
149  {
150  $user_mock = $this->getMockBuilder('ilObjUser')->disableOriginalConstructor()->getMock();
151  $encoder = $this->getMockBuilder('ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
152  $factory_mock = $this->getMockBuilder('ilUserPasswordEncoderFactory')->disableOriginalConstructor()->getMock();
153 
154  $user_mock->expects($this->once())->method('setPasswordSalt')->with($this->equalTo(null));
155  $user_mock->expects($this->once())->method('getPasswordSalt')->will($this->returnValue(null));
156  $user_mock->expects($this->once())->method('setPasswordEncodingType')->with($this->equalTo('mockencoder'));
157  $user_mock->expects($this->once())->method('setPasswd')->with($this->equalTo(self::ENCODED_PASSWORD), $this->equalTo(IL_PASSWD_CRYPTED));
158 
159  $encoder->expects($this->once())->method('getName')->will($this->returnValue('mockencoder'));
160  $encoder->expects($this->once())->method('requiresSalt')->will($this->returnValue(false));
161  $encoder->expects($this->once())->method('encodePassword')->with($this->equalTo(self::PASSWORD), $this->equalTo(null))->will($this->returnValue(self::ENCODED_PASSWORD));
162 
163  $factory_mock->expects($this->once())->method('getEncoderByName')->will($this->returnValue($encoder));
164 
165  $password_manager = new ilUserPasswordManager(
166  array(
167  'password_encoder' => 'mockencoder',
168  'encoder_factory' => $factory_mock
169  )
170  );
171 
172  $password_manager->encodePassword($user_mock, self::PASSWORD);
173  }
174 
179  {
180  $user_mock = $this->getMockBuilder('ilObjUser')->disableOriginalConstructor()->getMock();
181  $encoder = $this->getMockBuilder('ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
182  $factory_mock = $this->getMockBuilder('ilUserPasswordEncoderFactory')->disableOriginalConstructor()->getMock();
183 
184  $user_mock->expects($this->atLeast(1))->method('getPasswordSalt')->will($this->returnValue('asuperrandomsalt'));
185  $user_mock->expects($this->atLeast(1))->method('getPasswordEncodingType')->will($this->returnValue('mockencoder'));
186  $user_mock->expects($this->atLeast(1))->method('getPasswd')->will($this->returnValue(self::ENCODED_PASSWORD));
187  $user_mock->expects($this->never())->method('resetPassword');
188 
189  $encoder->expects($this->once())->method('getName')->will($this->returnValue('mockencoder'));
190  $encoder->expects($this->once())->method('isPasswordValid')->with($this->equalTo(self::ENCODED_PASSWORD), $this->equalTo(self::PASSWORD), $this->isType('string'))->will($this->returnValue(true));
191  $encoder->expects($this->once())->method('requiresReencoding')->with($this->equalTo(self::ENCODED_PASSWORD))->will($this->returnValue(false));
192 
193  $factory_mock->expects($this->once())->method('getEncoderByName')->will($this->returnValue($encoder));
194 
195  $password_manager = new ilUserPasswordManager(
196  array(
197  'password_encoder' => 'mockencoder',
198  'encoder_factory' => $factory_mock
199  )
200  );
201 
202  $this->assertTrue($password_manager->verifyPassword($user_mock, self::PASSWORD));
203  }
204 
209  {
210  $user_mock = $this->getMockBuilder('ilObjUser')->disableOriginalConstructor()->getMock();
211  $encoder = $this->getMockBuilder('ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
212  $factory_mock = $this->getMockBuilder('ilUserPasswordEncoderFactory')->disableOriginalConstructor()->getMock();
213 
214  $user_mock->expects($this->once())->method('getPasswordSalt')->will($this->returnValue('asuperrandomsalt'));
215  $user_mock->expects($this->once())->method('getPasswordEncodingType')->will($this->returnValue('second_mockencoder'));
216  $user_mock->expects($this->once())->method('getPasswd')->will($this->returnValue(self::ENCODED_PASSWORD));
217  $user_mock->expects($this->once())->method('resetPassword')->with($this->equalTo(self::PASSWORD), $this->equalTo(self::PASSWORD));
218 
219  $encoder->expects($this->once())->method('getName')->will($this->returnValue('second_mockencoder'));
220  $encoder->expects($this->once())->method('isPasswordValid')->with($this->equalTo(self::ENCODED_PASSWORD), $this->equalTo(self::PASSWORD), $this->isType('string'))->will($this->returnValue(true));
221  $encoder->expects($this->never())->method('requiresReencoding')->with($this->equalTo(self::ENCODED_PASSWORD))->will($this->returnValue(false));
222 
223  $factory_mock->expects($this->once())->method('getEncoderByName')->will($this->returnValue($encoder));
224 
225  $password_manager = new ilUserPasswordManager(
226  array(
227  'password_encoder' => 'mockencoder',
228  'encoder_factory' => $factory_mock
229  )
230  );
231 
232  $this->assertTrue($password_manager->verifyPassword($user_mock, self::PASSWORD));
233  }
234 
239  {
240  $user_mock = $this->getMockBuilder('ilObjUser')->disableOriginalConstructor()->getMock();
241  $encoder = $this->getMockBuilder('ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
242  $factory_mock = $this->getMockBuilder('ilUserPasswordEncoderFactory')->disableOriginalConstructor()->getMock();
243 
244  $user_mock->expects($this->once())->method('getPasswordSalt')->will($this->returnValue('asuperrandomsalt'));
245  $user_mock->expects($this->once())->method('getPasswordEncodingType')->will($this->returnValue('mockencoder'));
246  $user_mock->expects($this->exactly(2))->method('getPasswd')->will($this->returnValue(self::ENCODED_PASSWORD));
247  $user_mock->expects($this->once())->method('resetPassword')->with($this->equalTo(self::PASSWORD), $this->equalTo(self::PASSWORD));
248 
249  $encoder->expects($this->once())->method('getName')->will($this->returnValue('mockencoder'));
250  $encoder->expects($this->once())->method('isPasswordValid')->with($this->equalTo(self::ENCODED_PASSWORD), $this->equalTo(self::PASSWORD), $this->isType('string'))->will($this->returnValue(true));
251  $encoder->expects($this->once())->method('requiresReencoding')->with($this->equalTo(self::ENCODED_PASSWORD))->will($this->returnValue(true));
252 
253  $factory_mock->expects($this->once())->method('getEncoderByName')->will($this->returnValue($encoder));
254 
255  $password_manager = new ilUserPasswordManager(
256  array(
257  'password_encoder' => 'mockencoder',
258  'encoder_factory' => $factory_mock
259  )
260  );
261 
262  $this->assertTrue($password_manager->verifyPassword($user_mock, self::PASSWORD));
263  }
264 
269  {
270  $user_mock = $this->getMockBuilder('ilObjUser')->disableOriginalConstructor()->getMock();
271  $encoder = $this->getMockBuilder('ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
272  $factory_mock = $this->getMockBuilder('ilUserPasswordEncoderFactory')->disableOriginalConstructor()->getMock();
273 
274  $user_mock->expects($this->once())->method('getPasswordSalt')->will($this->returnValue('asuperrandomsalt'));
275  $user_mock->expects($this->once())->method('getPasswordEncodingType')->will($this->returnValue('second_mockencoder'));
276  $user_mock->expects($this->once())->method('getPasswd')->will($this->returnValue(self::ENCODED_PASSWORD));
277  $user_mock->expects($this->never())->method('resetPassword');
278 
279  $encoder->expects($this->once())->method('getName')->will($this->returnValue('second_mockencoder'));
280  $encoder->expects($this->never())->method('requiresReencoding');
281  $encoder->expects($this->once())->method('isPasswordValid')->with($this->equalTo(self::ENCODED_PASSWORD), $this->equalTo(self::PASSWORD), $this->isType('string'))->will($this->returnValue(false));
282 
283  $factory_mock->expects($this->once())->method('getEncoderByName')->will($this->returnValue($encoder));
284 
285  $password_manager = new ilUserPasswordManager(
286  array(
287  'password_encoder' => 'mockencoder',
288  'encoder_factory' => $factory_mock
289  )
290  );
291 
292  $this->assertFalse($password_manager->verifyPassword($user_mock, self::PASSWORD));
293  }
294 
298  public function testFactoryCanBeCreated()
299  {
300  $factory = new ilUserPasswordEncoderFactory();
301  $this->assertInstanceOf('ilUserPasswordEncoderFactory', $factory);
302  }
303 
308  {
309  $factory = new ilUserPasswordEncoderFactory(array('default_password_encoder' => 'md5'));
310  $this->assertEquals('md5', $factory->getDefaultEncoder());
311 
312  $encoder = $this->getMockBuilder('ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
313  $encoder->expects($this->atLeastOnce())->method('getName')->will($this->returnValue('mockencoder'));
314 
315  $second_mockencoder = $this->getMockBuilder('ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
316  $second_mockencoder->expects($this->atLeastOnce())->method('getName')->will($this->returnValue('second_mockencoder'));
317 
318  $factory->setEncoders(array($encoder, $second_mockencoder));
319  $this->assertCount(2, $factory->getEncoders());
320  $this->assertCount(2, $factory->getSupportedEncoderNames());
321  $this->assertCount(0, array_diff(array('mockencoder', 'second_mockencoder'), $factory->getSupportedEncoderNames()));
322  $this->assertCount(0, array_diff($factory->getSupportedEncoderNames(), array('mockencoder', 'second_mockencoder')));
323  }
324 
329  {
330  $this->assertException(ilUserException::class);
331  $factory = new ilUserPasswordEncoderFactory();
332  $factory->setEncoders(array('phpunit'));
333  }
334 
339  {
340  $this->assertException(ilUserException::class);
341  $factory = new ilUserPasswordEncoderFactory(array('default_password_encoder' => 'md5'));
342  $factory->getEncoderByName('phpunit');
343  }
344 
349  {
350  $this->assertException(ilUserException::class);
351  $factory = new ilUserPasswordEncoderFactory();
352  $factory->getEncoderByName('phpunit', true);
353  }
354 
359  {
360  $this->assertException(ilUserException::class);
361  $factory = new ilUserPasswordEncoderFactory(array('default_password_encoder' => 'phpunit'));
362  $factory->getEncoderByName('phpunit', true);
363  }
364 
369  {
370  $encoder = $this->getMockBuilder('ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
371  $encoder->expects($this->atLeastOnce())->method('getName')->will($this->returnValue('mockencoder'));
372 
373  $factory = new ilUserPasswordEncoderFactory(array('default_password_encoder' => $encoder->getName()));
374  $factory->setEncoders(array($encoder));
375  $this->assertEquals($encoder, $factory->getEncoderByName('phpunit', true));
376  }
377 
382  {
383  $encoder = $this->getMockBuilder('ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
384  $encoder->expects($this->atLeastOnce())->method('getName')->will($this->returnValue('mockencoder'));
385 
386  $factory = new ilUserPasswordEncoderFactory(array('default_password_encoder' => $encoder->getName()));
387  $factory->setEncoders(array($encoder));
388  $this->assertEquals($encoder, $factory->getEncoderByName('mockencoder', true));
389  }
390 }
testPasswordManagerNeverMigratesPasswordOnFailedVerificationWithVariantEncoders()
testExceptionIsRaisedIfPasswordManagerIsCreatedWithoutValidFactory()
PHPUnit_Framework_Error
testFactoryReturnsTheDefaultEncoderIfAnUnsupportedEncoderIsRequestedAndASupportedDefaultEncoderWasSpecifiedInFallbackMode()
setUp()
Sets up the fixture, for example, open a network connection.
testFactoryRaisesAnExceptionIfAnUnsupportedEncoderWasInjected()
ilUserException
testExceptionIsRaisedIfAnUnsupportedEncoderIsRequestedFromFactory()
ilUserException
assertException($exception_class)
const IL_PASSWD_CRYPTED
testExceptionIsRaisedIfPasswordManagerIsCreatedWithoutEncoderInformation()
ilUserException
Create styles array
The data for the language used.
testFactoryRaisesAnExceptionIfAnUnsupportedEncoderIsRequestedAndNoDefaultEncoderWasSpecifiedInFallbackMode()
ilUserException
testFactoryRaisesAnExceptionIfAnUnsupportedEncoderIsRequestedAndTheDefaultEncoderDoesNotMatchOneOfTheSupportedEncodersInFallbackMode()
ilUserException
testPasswordManagerMigratesPasswordOnVerificationWithVariantEncoders()
testExceptionIsRaisedIfPasswordManagerIsCreatedWithoutFactory()
ilUserException
defined( 'APPLICATION_ENV')||define( 'APPLICATION_ENV'
Definition: bootstrap.php:27