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';
37 define(
'CLIENT_DATA_DIR',
'/tmp');
60 'password_encoder' =>
'md5' 75 'password_encoder' =>
'md5',
76 'encoder_factory' =>
'test' 82 throw new PHPUnit_Framework_Error($e->getMessage(), $e->getCode(), $e->getFile(), $e->getLine());
91 $factory_mock = $this->getMockBuilder(
'ilUserPasswordEncoderFactory')->disableOriginalConstructor()->getMock();
92 $factory_mock->expects($this->exactly(2))->method(
'getSupportedEncoderNames')->will($this->onConsecutiveCalls(
94 'mockencoder',
'second_mockencoder' 103 'password_encoder' =>
'md5',
104 'encoder_factory' => $factory_mock
107 $this->assertInstanceOf(
'ilUserPasswordManager', $password_manager);
108 $this->assertEquals(
'md5', $password_manager->getEncoderName());
109 $this->assertEquals($factory_mock, $password_manager->getEncoderFactory());
111 $this->assertTrue($password_manager->isEncodingTypeSupported(
'second_mockencoder'));
112 $this->assertFalse($password_manager->isEncodingTypeSupported(
'second_mockencoder'));
120 $user_mock = $this->getMockBuilder(
'ilObjUser')->disableOriginalConstructor()->getMock();
121 $encoder = $this->getMockBuilder(
'ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
122 $factory_mock = $this->getMockBuilder(
'ilUserPasswordEncoderFactory')->disableOriginalConstructor()->getMock();
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));
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));
133 $factory_mock->expects($this->once())->method(
'getEncoderByName')->will($this->returnValue($encoder));
137 'password_encoder' =>
'mockencoder',
138 'encoder_factory' => $factory_mock
142 $password_manager->encodePassword($user_mock, self::PASSWORD);
150 $user_mock = $this->getMockBuilder(
'ilObjUser')->disableOriginalConstructor()->getMock();
151 $encoder = $this->getMockBuilder(
'ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
152 $factory_mock = $this->getMockBuilder(
'ilUserPasswordEncoderFactory')->disableOriginalConstructor()->getMock();
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));
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));
163 $factory_mock->expects($this->once())->method(
'getEncoderByName')->will($this->returnValue($encoder));
167 'password_encoder' =>
'mockencoder',
168 'encoder_factory' => $factory_mock
172 $password_manager->encodePassword($user_mock, self::PASSWORD);
180 $user_mock = $this->getMockBuilder(
'ilObjUser')->disableOriginalConstructor()->getMock();
181 $encoder = $this->getMockBuilder(
'ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
182 $factory_mock = $this->getMockBuilder(
'ilUserPasswordEncoderFactory')->disableOriginalConstructor()->getMock();
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');
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));
193 $factory_mock->expects($this->once())->method(
'getEncoderByName')->will($this->returnValue($encoder));
197 'password_encoder' =>
'mockencoder',
198 'encoder_factory' => $factory_mock
202 $this->assertTrue($password_manager->verifyPassword($user_mock, self::PASSWORD));
210 $user_mock = $this->getMockBuilder(
'ilObjUser')->disableOriginalConstructor()->getMock();
211 $encoder = $this->getMockBuilder(
'ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
212 $factory_mock = $this->getMockBuilder(
'ilUserPasswordEncoderFactory')->disableOriginalConstructor()->getMock();
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));
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));
223 $factory_mock->expects($this->once())->method(
'getEncoderByName')->will($this->returnValue($encoder));
227 'password_encoder' =>
'mockencoder',
228 'encoder_factory' => $factory_mock
232 $this->assertTrue($password_manager->verifyPassword($user_mock, self::PASSWORD));
240 $user_mock = $this->getMockBuilder(
'ilObjUser')->disableOriginalConstructor()->getMock();
241 $encoder = $this->getMockBuilder(
'ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
242 $factory_mock = $this->getMockBuilder(
'ilUserPasswordEncoderFactory')->disableOriginalConstructor()->getMock();
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));
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));
253 $factory_mock->expects($this->once())->method(
'getEncoderByName')->will($this->returnValue($encoder));
257 'password_encoder' =>
'mockencoder',
258 'encoder_factory' => $factory_mock
262 $this->assertTrue($password_manager->verifyPassword($user_mock, self::PASSWORD));
270 $user_mock = $this->getMockBuilder(
'ilObjUser')->disableOriginalConstructor()->getMock();
271 $encoder = $this->getMockBuilder(
'ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
272 $factory_mock = $this->getMockBuilder(
'ilUserPasswordEncoderFactory')->disableOriginalConstructor()->getMock();
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');
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));
283 $factory_mock->expects($this->once())->method(
'getEncoderByName')->will($this->returnValue($encoder));
287 'password_encoder' =>
'mockencoder',
288 'encoder_factory' => $factory_mock
292 $this->assertFalse($password_manager->verifyPassword($user_mock, self::PASSWORD));
301 $this->assertInstanceOf(
'ilUserPasswordEncoderFactory', $factory);
310 $this->assertEquals(
'md5', $factory->getDefaultEncoder());
312 $encoder = $this->getMockBuilder(
'ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
313 $encoder->expects($this->atLeastOnce())->method(
'getName')->will($this->returnValue(
'mockencoder'));
315 $second_mockencoder = $this->getMockBuilder(
'ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
316 $second_mockencoder->expects($this->atLeastOnce())->method(
'getName')->will($this->returnValue(
'second_mockencoder'));
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')));
332 $factory->setEncoders(
array(
'phpunit'));
342 $factory->getEncoderByName(
'phpunit');
352 $factory->getEncoderByName(
'phpunit',
true);
362 $factory->getEncoderByName(
'phpunit',
true);
370 $encoder = $this->getMockBuilder(
'ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
371 $encoder->expects($this->atLeastOnce())->method(
'getName')->will($this->returnValue(
'mockencoder'));
374 $factory->setEncoders(
array($encoder));
375 $this->assertEquals($encoder, $factory->getEncoderByName(
'phpunit',
true));
383 $encoder = $this->getMockBuilder(
'ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
384 $encoder->expects($this->atLeastOnce())->method(
'getName')->will($this->returnValue(
'mockencoder'));
387 $factory->setEncoders(
array($encoder));
388 $this->assertEquals($encoder, $factory->getEncoderByName(
'mockencoder',
true));
testPasswordManagerEncodesRawPasswordWithoutSalt()
testPasswordManagerNeverMigratesPasswordOnFailedVerificationWithVariantEncoders()
testExceptionIsRaisedIfPasswordManagerIsCreatedWithoutValidFactory()
PHPUnit_Framework_Error
testFactoryReturnsTheDefaultEncoderIfAnUnsupportedEncoderIsRequestedAndASupportedDefaultEncoderWasSpecifiedInFallbackMode()
setUp()
Sets up the fixture, for example, open a network connection.
testFactoryRaisesAnExceptionIfAnUnsupportedEncoderWasInjected()
ilUserException
testExceptionIsRaisedIfAnUnsupportedEncoderIsRequestedFromFactory()
ilUserException
assertException($exception_class)
testPasswordManagerVerifiesPassword()
testExceptionIsRaisedIfPasswordManagerIsCreatedWithoutEncoderInformation()
ilUserException
testInstanceCanBeCreated()
testPasswordManagerEncodesRawPasswordWithSalt()
testFactoryReturnsCorrectEncoderIfAMatchingEncoderWasFound()
Create styles array
The data for the language used.
testFactoryCanBeCreated()
testFactoryRaisesAnExceptionIfAnUnsupportedEncoderIsRequestedAndNoDefaultEncoderWasSpecifiedInFallbackMode()
ilUserException
testFactoryRaisesAnExceptionIfAnUnsupportedEncoderIsRequestedAndTheDefaultEncoderDoesNotMatchOneOfTheSupportedEncodersInFallbackMode()
ilUserException
testPasswordManagerMigratesPasswordOnVerificationWithVariantEncoders()
testExceptionIsRaisedIfPasswordManagerIsCreatedWithoutFactory()
ilUserException
testPasswordManagerReencodesPasswordIfReencodingIsNecessary()
defined( 'APPLICATION_ENV')||define( 'APPLICATION_ENV'
testGettersOfFactoryShouldReturnWhatWasSetBySetters()