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';
79 vfs\vfsStream::setup();
80 $this->
setTestDirectory(vfs\vfsStream::newDirectory(
'tests')->at(vfs\vfsStreamWrapper::getRoot()));
103 'password_encoder' =>
'md5',
118 'password_encoder' =>
'md5',
119 'encoder_factory' =>
'test',
123 }
catch (TypeError $e) {
124 throw new PHPUnit_Framework_Error($e->getMessage(), $e->getCode(), $e->getFile(), $e->getLine());
133 $factory_mock = $this->getMockBuilder(
'ilUserPasswordEncoderFactory')->disableOriginalConstructor()->getMock();
134 $factory_mock->expects($this->exactly(2))->method(
'getSupportedEncoderNames')->will($this->onConsecutiveCalls(
136 'mockencoder',
'second_mockencoder' 145 'password_encoder' =>
'md5',
146 'encoder_factory' => $factory_mock,
150 $this->assertInstanceOf(
'ilUserPasswordManager', $password_manager);
151 $this->assertEquals(
'md5', $password_manager->getEncoderName());
152 $this->assertEquals($factory_mock, $password_manager->getEncoderFactory());
154 $this->assertTrue($password_manager->isEncodingTypeSupported(
'second_mockencoder'));
155 $this->assertFalse($password_manager->isEncodingTypeSupported(
'second_mockencoder'));
163 $user_mock = $this->getMockBuilder(
'ilObjUser')->disableOriginalConstructor()->getMock();
164 $encoder = $this->getMockBuilder(
'ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
165 $factory_mock = $this->getMockBuilder(
'ilUserPasswordEncoderFactory')->disableOriginalConstructor()->getMock();
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));
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));
176 $factory_mock->expects($this->
once())->method(
'getEncoderByName')->will($this->returnValue($encoder));
180 'password_encoder' =>
'mockencoder',
181 'encoder_factory' => $factory_mock,
186 $password_manager->encodePassword($user_mock, self::PASSWORD);
194 $user_mock = $this->getMockBuilder(
'ilObjUser')->disableOriginalConstructor()->getMock();
195 $encoder = $this->getMockBuilder(
'ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
196 $factory_mock = $this->getMockBuilder(
'ilUserPasswordEncoderFactory')->disableOriginalConstructor()->getMock();
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));
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));
207 $factory_mock->expects($this->
once())->method(
'getEncoderByName')->will($this->returnValue($encoder));
211 'password_encoder' =>
'mockencoder',
212 'encoder_factory' => $factory_mock,
217 $password_manager->encodePassword($user_mock, self::PASSWORD);
225 $user_mock = $this->getMockBuilder(
'ilObjUser')->disableOriginalConstructor()->getMock();
226 $encoder = $this->getMockBuilder(
'ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
227 $factory_mock = $this->getMockBuilder(
'ilUserPasswordEncoderFactory')->disableOriginalConstructor()->getMock();
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');
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));
238 $factory_mock->expects($this->
once())->method(
'getEncoderByName')->will($this->returnValue($encoder));
242 'password_encoder' =>
'mockencoder',
243 'encoder_factory' => $factory_mock,
248 $this->assertTrue($password_manager->verifyPassword($user_mock, self::PASSWORD));
256 $user_mock = $this->getMockBuilder(
'ilObjUser')->disableOriginalConstructor()->getMock();
257 $encoder = $this->getMockBuilder(
'ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
258 $factory_mock = $this->getMockBuilder(
'ilUserPasswordEncoderFactory')->disableOriginalConstructor()->getMock();
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));
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));
269 $factory_mock->expects($this->
once())->method(
'getEncoderByName')->will($this->returnValue($encoder));
273 'password_encoder' =>
'mockencoder',
274 'encoder_factory' => $factory_mock,
279 $this->assertTrue($password_manager->verifyPassword($user_mock, self::PASSWORD));
287 $user_mock = $this->getMockBuilder(
'ilObjUser')->disableOriginalConstructor()->getMock();
288 $encoder = $this->getMockBuilder(
'ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
289 $factory_mock = $this->getMockBuilder(
'ilUserPasswordEncoderFactory')->disableOriginalConstructor()->getMock();
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));
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));
300 $factory_mock->expects($this->
once())->method(
'getEncoderByName')->will($this->returnValue($encoder));
304 'password_encoder' =>
'mockencoder',
305 'encoder_factory' => $factory_mock,
310 $this->assertTrue($password_manager->verifyPassword($user_mock, self::PASSWORD));
318 $user_mock = $this->getMockBuilder(
'ilObjUser')->disableOriginalConstructor()->getMock();
319 $encoder = $this->getMockBuilder(
'ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
320 $factory_mock = $this->getMockBuilder(
'ilUserPasswordEncoderFactory')->disableOriginalConstructor()->getMock();
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');
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));
331 $factory_mock->expects($this->
once())->method(
'getEncoderByName')->will($this->returnValue($encoder));
335 'password_encoder' =>
'mockencoder',
336 'encoder_factory' => $factory_mock,
341 $this->assertFalse($password_manager->verifyPassword($user_mock, self::PASSWORD));
352 $this->assertInstanceOf(
'ilUserPasswordEncoderFactory',
$factory);
361 'default_password_encoder' =>
'md5',
364 $this->assertEquals(
'md5',
$factory->getDefaultEncoder());
366 $encoder = $this->getMockBuilder(
'ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
367 $encoder->expects($this->atLeastOnce())->method(
'getName')->will($this->returnValue(
'mockencoder'));
369 $second_mockencoder = $this->getMockBuilder(
'ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
370 $second_mockencoder->expects($this->atLeastOnce())->method(
'getName')->will($this->returnValue(
'second_mockencoder'));
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')));
388 $factory->setEncoders(array(
'phpunit'));
398 'default_password_encoder' =>
'md5',
401 $factory->getEncoderByName(
'phpunit');
413 $factory->getEncoderByName(
'phpunit',
true);
423 'default_password_encoder' =>
'phpunit',
426 $factory->getEncoderByName(
'phpunit',
true);
434 $encoder = $this->getMockBuilder(
'ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
435 $encoder->expects($this->atLeastOnce())->method(
'getName')->will($this->returnValue(
'mockencoder'));
438 'default_password_encoder' => $encoder->getName(),
441 $factory->setEncoders(array($encoder));
442 $this->assertEquals($encoder,
$factory->getEncoderByName(
'phpunit',
true));
450 $encoder = $this->getMockBuilder(
'ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
451 $encoder->expects($this->atLeastOnce())->method(
'getName')->will($this->returnValue(
'mockencoder'));
454 'default_password_encoder' => $encoder->getName(),
457 $factory->setEncoders(array($encoder));
458 $this->assertEquals($encoder,
$factory->getEncoderByName(
'mockencoder',
true));
testPasswordManagerEncodesRawPasswordWithoutSalt()
setTestDirectory($test_directory)
testPasswordManagerNeverMigratesPasswordOnFailedVerificationWithVariantEncoders()
testExceptionIsRaisedIfPasswordManagerIsCreatedWithoutValidFactory()
PHPUnit_Framework_Error
testFactoryReturnsTheDefaultEncoderIfAnUnsupportedEncoderIsRequestedAndASupportedDefaultEncoderWasSpecifiedInFallbackMode()
testFactoryRaisesAnExceptionIfAnUnsupportedEncoderWasInjected()
ilUserException
testExceptionIsRaisedIfAnUnsupportedEncoderIsRequestedFromFactory()
ilUserException
assertException($exception_class)
testPasswordManagerVerifiesPassword()
testExceptionIsRaisedIfPasswordManagerIsCreatedWithoutEncoderInformation()
ilUserException
testInstanceCanBeCreated()
setTestDirectoryUrl($test_directory_url)
once($eventName, callable $callBack, $priority=100)
Subscribe to an event exactly once.
testPasswordManagerEncodesRawPasswordWithSalt()
testFactoryReturnsCorrectEncoderIfAMatchingEncoderWasFound()
testFactoryCanBeCreated()
testFactoryRaisesAnExceptionIfAnUnsupportedEncoderIsRequestedAndNoDefaultEncoderWasSpecifiedInFallbackMode()
ilUserException
testFactoryRaisesAnExceptionIfAnUnsupportedEncoderIsRequestedAndTheDefaultEncoderDoesNotMatchOneOfTheSupportedEncodersInFallbackMode()
ilUserException
testPasswordManagerMigratesPasswordOnVerificationWithVariantEncoders()
testExceptionIsRaisedIfPasswordManagerIsCreatedWithoutFactory()
ilUserException
testPasswordManagerReencodesPasswordIfReencodingIsNecessary()
testGettersOfFactoryShouldReturnWhatWasSetBySetters()