ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 
5 
6 require_once 'libs/composer/vendor/autoload.php';
7 require_once 'Services/User/classes/class.ilUserPasswordManager.php';
8 require_once 'Services/User/classes/class.ilUserPasswordEncoderFactory.php';
9 require_once 'Services/Password/classes/class.ilBasePasswordEncoder.php';
10 require_once 'Services/Utilities/classes/class.ilUtil.php';
11 require_once 'Services/User/classes/class.ilObjUser.php';
12 require_once 'Services/User/exceptions/class.ilUserException.php';
13 require_once 'Services/User/test/ilUserBaseTest.php';
14 
21 {
25  const PASSWORD = 'password';
26 
30  const ENCODED_PASSWORD = 'encoded';
31 
35  protected $test_directory;
36 
41 
45  public function getTestDirectory()
46  {
47  return $this->test_directory;
48  }
49 
54  {
55  $this->test_directory = $test_directory;
56  }
57 
61  public function getTestDirectoryUrl()
62  {
64  }
65 
70  {
71  $this->test_directory_url = $test_directory_url;
72  }
73 
77  protected function setUp()
78  {
79  vfs\vfsStream::setup();
80  $this->setTestDirectory(vfs\vfsStream::newDirectory('tests')->at(vfs\vfsStreamWrapper::getRoot()));
81  $this->setTestDirectoryUrl(vfs\vfsStream::url('root/tests'));
82 
83  parent::setUp();
84  }
85 
90  {
91  $this->assertException(ilUserException::class);
92  new ilUserPasswordManager(array('data_directory' => $this->getTestDirectoryUrl()));
93  }
94 
99  {
100  $this->assertException(ilUserException::class);
102  array(
103  'password_encoder' => 'md5',
104  'data_directory' => $this->getTestDirectoryUrl()
105  )
106  );
107  }
108 
113  {
114  $this->assertException(PHPUnit_Framework_Error::class);
115  try {
117  array(
118  'password_encoder' => 'md5',
119  'encoder_factory' => 'test',
120  'data_directory' => $this->getTestDirectoryUrl()
121  )
122  );
123  } catch (TypeError $e) {
124  throw new PHPUnit_Framework_Error($e->getMessage(), $e->getCode(), $e->getFile(), $e->getLine());
125  }
126  }
127 
131  public function testInstanceCanBeCreated()
132  {
133  $factory_mock = $this->getMockBuilder('ilUserPasswordEncoderFactory')->disableOriginalConstructor()->getMock();
134  $factory_mock->expects($this->exactly(2))->method('getSupportedEncoderNames')->will($this->onConsecutiveCalls(
135  array(
136  'mockencoder', 'second_mockencoder'
137  ),
138  array(
139  'mockencoder'
140  )
141  ));
142 
143  $password_manager = new ilUserPasswordManager(
144  array(
145  'password_encoder' => 'md5',
146  'encoder_factory' => $factory_mock,
147  'data_directory' => $this->getTestDirectoryUrl()
148  )
149  );
150  $this->assertInstanceOf('ilUserPasswordManager', $password_manager);
151  $this->assertEquals('md5', $password_manager->getEncoderName());
152  $this->assertEquals($factory_mock, $password_manager->getEncoderFactory());
153 
154  $this->assertTrue($password_manager->isEncodingTypeSupported('second_mockencoder'));
155  $this->assertFalse($password_manager->isEncodingTypeSupported('second_mockencoder'));
156  }
157 
162  {
163  $user_mock = $this->getMockBuilder('ilObjUser')->disableOriginalConstructor()->getMock();
164  $encoder = $this->getMockBuilder('ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
165  $factory_mock = $this->getMockBuilder('ilUserPasswordEncoderFactory')->disableOriginalConstructor()->getMock();
166 
167  $user_mock->expects($this->once())->method('setPasswordSalt')->with($this->isType('string'));
168  $user_mock->expects($this->once())->method('getPasswordSalt')->will($this->returnValue('asuperrandomsalt'));
169  $user_mock->expects($this->once())->method('setPasswordEncodingType')->with($this->equalTo('mockencoder'));
170  $user_mock->expects($this->once())->method('setPasswd')->with($this->equalTo(self::ENCODED_PASSWORD), $this->equalTo(IL_PASSWD_CRYPTED));
171 
172  $encoder->expects($this->once())->method('getName')->will($this->returnValue('mockencoder'));
173  $encoder->expects($this->once())->method('requiresSalt')->will($this->returnValue(true));
174  $encoder->expects($this->once())->method('encodePassword')->with($this->equalTo(self::PASSWORD), $this->isType('string'))->will($this->returnValue(self::ENCODED_PASSWORD));
175 
176  $factory_mock->expects($this->once())->method('getEncoderByName')->will($this->returnValue($encoder));
177 
178  $password_manager = new ilUserPasswordManager(
179  array(
180  'password_encoder' => 'mockencoder',
181  'encoder_factory' => $factory_mock,
182  'data_directory' => $this->getTestDirectoryUrl()
183  )
184  );
185 
186  $password_manager->encodePassword($user_mock, self::PASSWORD);
187  }
188 
193  {
194  $user_mock = $this->getMockBuilder('ilObjUser')->disableOriginalConstructor()->getMock();
195  $encoder = $this->getMockBuilder('ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
196  $factory_mock = $this->getMockBuilder('ilUserPasswordEncoderFactory')->disableOriginalConstructor()->getMock();
197 
198  $user_mock->expects($this->once())->method('setPasswordSalt')->with($this->equalTo(null));
199  $user_mock->expects($this->once())->method('getPasswordSalt')->will($this->returnValue(null));
200  $user_mock->expects($this->once())->method('setPasswordEncodingType')->with($this->equalTo('mockencoder'));
201  $user_mock->expects($this->once())->method('setPasswd')->with($this->equalTo(self::ENCODED_PASSWORD), $this->equalTo(IL_PASSWD_CRYPTED));
202 
203  $encoder->expects($this->once())->method('getName')->will($this->returnValue('mockencoder'));
204  $encoder->expects($this->once())->method('requiresSalt')->will($this->returnValue(false));
205  $encoder->expects($this->once())->method('encodePassword')->with($this->equalTo(self::PASSWORD), $this->equalTo(null))->will($this->returnValue(self::ENCODED_PASSWORD));
206 
207  $factory_mock->expects($this->once())->method('getEncoderByName')->will($this->returnValue($encoder));
208 
209  $password_manager = new ilUserPasswordManager(
210  array(
211  'password_encoder' => 'mockencoder',
212  'encoder_factory' => $factory_mock,
213  'data_directory' => $this->getTestDirectoryUrl()
214  )
215  );
216 
217  $password_manager->encodePassword($user_mock, self::PASSWORD);
218  }
219 
224  {
225  $user_mock = $this->getMockBuilder('ilObjUser')->disableOriginalConstructor()->getMock();
226  $encoder = $this->getMockBuilder('ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
227  $factory_mock = $this->getMockBuilder('ilUserPasswordEncoderFactory')->disableOriginalConstructor()->getMock();
228 
229  $user_mock->expects($this->atLeast(1))->method('getPasswordSalt')->will($this->returnValue('asuperrandomsalt'));
230  $user_mock->expects($this->atLeast(1))->method('getPasswordEncodingType')->will($this->returnValue('mockencoder'));
231  $user_mock->expects($this->atLeast(1))->method('getPasswd')->will($this->returnValue(self::ENCODED_PASSWORD));
232  $user_mock->expects($this->never())->method('resetPassword');
233 
234  $encoder->expects($this->once())->method('getName')->will($this->returnValue('mockencoder'));
235  $encoder->expects($this->once())->method('isPasswordValid')->with($this->equalTo(self::ENCODED_PASSWORD), $this->equalTo(self::PASSWORD), $this->isType('string'))->will($this->returnValue(true));
236  $encoder->expects($this->once())->method('requiresReencoding')->with($this->equalTo(self::ENCODED_PASSWORD))->will($this->returnValue(false));
237 
238  $factory_mock->expects($this->once())->method('getEncoderByName')->will($this->returnValue($encoder));
239 
240  $password_manager = new ilUserPasswordManager(
241  array(
242  'password_encoder' => 'mockencoder',
243  'encoder_factory' => $factory_mock,
244  'data_directory' => $this->getTestDirectoryUrl()
245  )
246  );
247 
248  $this->assertTrue($password_manager->verifyPassword($user_mock, self::PASSWORD));
249  }
250 
255  {
256  $user_mock = $this->getMockBuilder('ilObjUser')->disableOriginalConstructor()->getMock();
257  $encoder = $this->getMockBuilder('ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
258  $factory_mock = $this->getMockBuilder('ilUserPasswordEncoderFactory')->disableOriginalConstructor()->getMock();
259 
260  $user_mock->expects($this->once())->method('getPasswordSalt')->will($this->returnValue('asuperrandomsalt'));
261  $user_mock->expects($this->once())->method('getPasswordEncodingType')->will($this->returnValue('second_mockencoder'));
262  $user_mock->expects($this->once())->method('getPasswd')->will($this->returnValue(self::ENCODED_PASSWORD));
263  $user_mock->expects($this->once())->method('resetPassword')->with($this->equalTo(self::PASSWORD), $this->equalTo(self::PASSWORD));
264 
265  $encoder->expects($this->once())->method('getName')->will($this->returnValue('second_mockencoder'));
266  $encoder->expects($this->once())->method('isPasswordValid')->with($this->equalTo(self::ENCODED_PASSWORD), $this->equalTo(self::PASSWORD), $this->isType('string'))->will($this->returnValue(true));
267  $encoder->expects($this->never())->method('requiresReencoding')->with($this->equalTo(self::ENCODED_PASSWORD))->will($this->returnValue(false));
268 
269  $factory_mock->expects($this->once())->method('getEncoderByName')->will($this->returnValue($encoder));
270 
271  $password_manager = new ilUserPasswordManager(
272  array(
273  'password_encoder' => 'mockencoder',
274  'encoder_factory' => $factory_mock,
275  'data_directory' => $this->getTestDirectoryUrl()
276  )
277  );
278 
279  $this->assertTrue($password_manager->verifyPassword($user_mock, self::PASSWORD));
280  }
281 
286  {
287  $user_mock = $this->getMockBuilder('ilObjUser')->disableOriginalConstructor()->getMock();
288  $encoder = $this->getMockBuilder('ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
289  $factory_mock = $this->getMockBuilder('ilUserPasswordEncoderFactory')->disableOriginalConstructor()->getMock();
290 
291  $user_mock->expects($this->once())->method('getPasswordSalt')->will($this->returnValue('asuperrandomsalt'));
292  $user_mock->expects($this->once())->method('getPasswordEncodingType')->will($this->returnValue('mockencoder'));
293  $user_mock->expects($this->exactly(2))->method('getPasswd')->will($this->returnValue(self::ENCODED_PASSWORD));
294  $user_mock->expects($this->once())->method('resetPassword')->with($this->equalTo(self::PASSWORD), $this->equalTo(self::PASSWORD));
295 
296  $encoder->expects($this->once())->method('getName')->will($this->returnValue('mockencoder'));
297  $encoder->expects($this->once())->method('isPasswordValid')->with($this->equalTo(self::ENCODED_PASSWORD), $this->equalTo(self::PASSWORD), $this->isType('string'))->will($this->returnValue(true));
298  $encoder->expects($this->once())->method('requiresReencoding')->with($this->equalTo(self::ENCODED_PASSWORD))->will($this->returnValue(true));
299 
300  $factory_mock->expects($this->once())->method('getEncoderByName')->will($this->returnValue($encoder));
301 
302  $password_manager = new ilUserPasswordManager(
303  array(
304  'password_encoder' => 'mockencoder',
305  'encoder_factory' => $factory_mock,
306  'data_directory' => $this->getTestDirectoryUrl()
307  )
308  );
309 
310  $this->assertTrue($password_manager->verifyPassword($user_mock, self::PASSWORD));
311  }
312 
317  {
318  $user_mock = $this->getMockBuilder('ilObjUser')->disableOriginalConstructor()->getMock();
319  $encoder = $this->getMockBuilder('ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
320  $factory_mock = $this->getMockBuilder('ilUserPasswordEncoderFactory')->disableOriginalConstructor()->getMock();
321 
322  $user_mock->expects($this->once())->method('getPasswordSalt')->will($this->returnValue('asuperrandomsalt'));
323  $user_mock->expects($this->once())->method('getPasswordEncodingType')->will($this->returnValue('second_mockencoder'));
324  $user_mock->expects($this->once())->method('getPasswd')->will($this->returnValue(self::ENCODED_PASSWORD));
325  $user_mock->expects($this->never())->method('resetPassword');
326 
327  $encoder->expects($this->once())->method('getName')->will($this->returnValue('second_mockencoder'));
328  $encoder->expects($this->never())->method('requiresReencoding');
329  $encoder->expects($this->once())->method('isPasswordValid')->with($this->equalTo(self::ENCODED_PASSWORD), $this->equalTo(self::PASSWORD), $this->isType('string'))->will($this->returnValue(false));
330 
331  $factory_mock->expects($this->once())->method('getEncoderByName')->will($this->returnValue($encoder));
332 
333  $password_manager = new ilUserPasswordManager(
334  array(
335  'password_encoder' => 'mockencoder',
336  'encoder_factory' => $factory_mock,
337  'data_directory' => $this->getTestDirectoryUrl()
338  )
339  );
340 
341  $this->assertFalse($password_manager->verifyPassword($user_mock, self::PASSWORD));
342  }
343 
347  public function testFactoryCanBeCreated()
348  {
350  'data_directory' => $this->getTestDirectoryUrl()
351  ));
352  $this->assertInstanceOf('ilUserPasswordEncoderFactory', $factory);
353  }
354 
359  {
361  'default_password_encoder' => 'md5',
362  'data_directory' => $this->getTestDirectoryUrl()
363  ));
364  $this->assertEquals('md5', $factory->getDefaultEncoder());
365 
366  $encoder = $this->getMockBuilder('ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
367  $encoder->expects($this->atLeastOnce())->method('getName')->will($this->returnValue('mockencoder'));
368 
369  $second_mockencoder = $this->getMockBuilder('ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
370  $second_mockencoder->expects($this->atLeastOnce())->method('getName')->will($this->returnValue('second_mockencoder'));
371 
372  $factory->setEncoders(array($encoder, $second_mockencoder));
373  $this->assertCount(2, $factory->getEncoders());
374  $this->assertCount(2, $factory->getSupportedEncoderNames());
375  $this->assertCount(0, array_diff(array('mockencoder', 'second_mockencoder'), $factory->getSupportedEncoderNames()));
376  $this->assertCount(0, array_diff($factory->getSupportedEncoderNames(), array('mockencoder', 'second_mockencoder')));
377  }
378 
383  {
384  $this->assertException(ilUserException::class);
386  'data_directory' => $this->getTestDirectoryUrl()
387  ));
388  $factory->setEncoders(array('phpunit'));
389  }
390 
395  {
396  $this->assertException(ilUserException::class);
398  'default_password_encoder' => 'md5',
399  'data_directory' => $this->getTestDirectoryUrl()
400  ));
401  $factory->getEncoderByName('phpunit');
402  }
403 
408  {
409  $this->assertException(ilUserException::class);
411  'data_directory' => $this->getTestDirectoryUrl()
412  ));
413  $factory->getEncoderByName('phpunit', true);
414  }
415 
420  {
421  $this->assertException(ilUserException::class);
423  'default_password_encoder' => 'phpunit',
424  'data_directory' => $this->getTestDirectoryUrl()
425  ));
426  $factory->getEncoderByName('phpunit', true);
427  }
428 
433  {
434  $encoder = $this->getMockBuilder('ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
435  $encoder->expects($this->atLeastOnce())->method('getName')->will($this->returnValue('mockencoder'));
436 
438  'default_password_encoder' => $encoder->getName(),
439  'data_directory' => $this->getTestDirectoryUrl()
440  ));
441  $factory->setEncoders(array($encoder));
442  $this->assertEquals($encoder, $factory->getEncoderByName('phpunit', true));
443  }
444 
449  {
450  $encoder = $this->getMockBuilder('ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
451  $encoder->expects($this->atLeastOnce())->method('getName')->will($this->returnValue('mockencoder'));
452 
454  'default_password_encoder' => $encoder->getName(),
455  'data_directory' => $this->getTestDirectoryUrl()
456  ));
457  $factory->setEncoders(array($encoder));
458  $this->assertEquals($encoder, $factory->getEncoderByName('mockencoder', true));
459  }
460 }
setTestDirectory($test_directory)
testPasswordManagerNeverMigratesPasswordOnFailedVerificationWithVariantEncoders()
testExceptionIsRaisedIfPasswordManagerIsCreatedWithoutValidFactory()
PHPUnit_Framework_Error
testFactoryReturnsTheDefaultEncoderIfAnUnsupportedEncoderIsRequestedAndASupportedDefaultEncoderWasSpecifiedInFallbackMode()
testFactoryRaisesAnExceptionIfAnUnsupportedEncoderWasInjected()
ilUserException
testExceptionIsRaisedIfAnUnsupportedEncoderIsRequestedFromFactory()
ilUserException
assertException($exception_class)
const IL_PASSWD_CRYPTED
$factory
Definition: metadata.php:47
testExceptionIsRaisedIfPasswordManagerIsCreatedWithoutEncoderInformation()
ilUserException
setTestDirectoryUrl($test_directory_url)
Create styles array
The data for the language used.
testFactoryRaisesAnExceptionIfAnUnsupportedEncoderIsRequestedAndNoDefaultEncoderWasSpecifiedInFallbackMode()
ilUserException
testFactoryRaisesAnExceptionIfAnUnsupportedEncoderIsRequestedAndTheDefaultEncoderDoesNotMatchOneOfTheSupportedEncodersInFallbackMode()
ilUserException
testPasswordManagerMigratesPasswordOnVerificationWithVariantEncoders()
testExceptionIsRaisedIfPasswordManagerIsCreatedWithoutFactory()
ilUserException