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';
42 'password_encoder' =>
'md5'
54 'password_encoder' =>
'md5',
55 'encoder_factory' =>
'test'
65 $factory_mock = $this->getMockBuilder(
'ilUserPasswordEncoderFactory')->disableOriginalConstructor()->getMock();
66 $factory_mock->expects($this->exactly(2))->method(
'getSupportedEncoderNames')->will($this->onConsecutiveCalls(
68 'mockencoder',
'second_mockencoder'
77 'password_encoder' =>
'md5',
78 'encoder_factory' => $factory_mock
81 $this->assertInstanceOf(
'ilUserPasswordManager', $password_manager);
82 $this->assertEquals(
'md5', $password_manager->getEncoderName());
83 $this->assertEquals($factory_mock, $password_manager->getEncoderFactory());
85 $this->assertTrue($password_manager->isEncodingTypeSupported(
'second_mockencoder'));
86 $this->assertFalse($password_manager->isEncodingTypeSupported(
'second_mockencoder'));
94 $user_mock = $this->getMockBuilder(
'ilObjUser')->disableOriginalConstructor()->getMock();
95 $encoder = $this->getMockBuilder(
'ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
96 $factory_mock = $this->getMockBuilder(
'ilUserPasswordEncoderFactory')->disableOriginalConstructor()->getMock();
98 $user_mock->expects($this->once())->method(
'setPasswordSalt')->with($this->isType(
'string'));
99 $user_mock->expects($this->once())->method(
'getPasswordSalt')->will($this->returnValue(
'asuperrandomsalt'));
100 $user_mock->expects($this->once())->method(
'setPasswordEncodingType')->with($this->equalTo(
'mockencoder'));
101 $user_mock->expects($this->once())->method(
'setPasswd')->with($this->equalTo(self::ENCODED_PASSWORD), $this->equalTo(
IL_PASSWD_CRYPTED));
103 $encoder->expects($this->once())->method(
'getName')->will($this->returnValue(
'mockencoder'));
104 $encoder->expects($this->once())->method(
'requiresSalt')->will($this->returnValue(
true));
105 $encoder->expects($this->once())->method(
'encodePassword')->with($this->equalTo(self::PASSWORD), $this->isType(
'string'))->will($this->returnValue(self::ENCODED_PASSWORD));
107 $factory_mock->expects($this->once())->method(
'getEncoderByName')->will($this->returnValue($encoder));
111 'password_encoder' =>
'mockencoder',
112 'encoder_factory' => $factory_mock
116 $password_manager->encodePassword($user_mock, self::PASSWORD);
124 $user_mock = $this->getMockBuilder(
'ilObjUser')->disableOriginalConstructor()->getMock();
125 $encoder = $this->getMockBuilder(
'ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
126 $factory_mock = $this->getMockBuilder(
'ilUserPasswordEncoderFactory')->disableOriginalConstructor()->getMock();
128 $user_mock->expects($this->once())->method(
'setPasswordSalt')->with($this->equalTo(null));
129 $user_mock->expects($this->once())->method(
'getPasswordSalt')->will($this->returnValue(null));
130 $user_mock->expects($this->once())->method(
'setPasswordEncodingType')->with($this->equalTo(
'mockencoder'));
131 $user_mock->expects($this->once())->method(
'setPasswd')->with($this->equalTo(self::ENCODED_PASSWORD), $this->equalTo(
IL_PASSWD_CRYPTED));
133 $encoder->expects($this->once())->method(
'getName')->will($this->returnValue(
'mockencoder'));
134 $encoder->expects($this->once())->method(
'requiresSalt')->will($this->returnValue(
false));
135 $encoder->expects($this->once())->method(
'encodePassword')->with($this->equalTo(self::PASSWORD), $this->equalTo(null))->will($this->returnValue(self::ENCODED_PASSWORD));
137 $factory_mock->expects($this->once())->method(
'getEncoderByName')->will($this->returnValue($encoder));
141 'password_encoder' =>
'mockencoder',
142 'encoder_factory' => $factory_mock
146 $password_manager->encodePassword($user_mock, self::PASSWORD);
154 $user_mock = $this->getMockBuilder(
'ilObjUser')->disableOriginalConstructor()->getMock();
155 $encoder = $this->getMockBuilder(
'ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
156 $factory_mock = $this->getMockBuilder(
'ilUserPasswordEncoderFactory')->disableOriginalConstructor()->getMock();
158 $user_mock->expects($this->once())->method(
'getPasswordSalt')->will($this->returnValue(
'asuperrandomsalt'));
159 $user_mock->expects($this->once())->method(
'getPasswordEncodingType')->will($this->returnValue(
'mockencoder'));
160 $user_mock->expects($this->once())->method(
'getPasswd')->will($this->returnValue(self::ENCODED_PASSWORD));
162 $encoder->expects($this->once())->method(
'getName')->will($this->returnValue(
'mockencoder'));
163 $encoder->expects($this->once())->method(
'isPasswordValid')->with($this->equalTo(self::ENCODED_PASSWORD), $this->equalTo(self::PASSWORD), $this->isType(
'string'))->will($this->returnValue(
true));
165 $factory_mock->expects($this->once())->method(
'getEncoderByName')->will($this->returnValue($encoder));
169 'password_encoder' =>
'mockencoder',
170 'encoder_factory' => $factory_mock
174 $this->assertTrue($password_manager->verifyPassword($user_mock, self::PASSWORD));
182 $user_mock = $this->getMockBuilder(
'ilObjUser')->disableOriginalConstructor()->getMock();
183 $encoder = $this->getMockBuilder(
'ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
184 $factory_mock = $this->getMockBuilder(
'ilUserPasswordEncoderFactory')->disableOriginalConstructor()->getMock();
186 $user_mock->expects($this->once())->method(
'getPasswordSalt')->will($this->returnValue(
'asuperrandomsalt'));
187 $user_mock->expects($this->once())->method(
'getPasswordEncodingType')->will($this->returnValue(
'second_mockencoder'));
188 $user_mock->expects($this->once())->method(
'getPasswd')->will($this->returnValue(self::ENCODED_PASSWORD));
189 $user_mock->expects($this->once())->method(
'resetPassword')->with($this->equalTo(self::PASSWORD), $this->equalTo(self::PASSWORD));
191 $encoder->expects($this->once())->method(
'getName')->will($this->returnValue(
'second_mockencoder'));
192 $encoder->expects($this->once())->method(
'isPasswordValid')->with($this->equalTo(self::ENCODED_PASSWORD), $this->equalTo(self::PASSWORD), $this->isType(
'string'))->will($this->returnValue(
true));
194 $factory_mock->expects($this->once())->method(
'getEncoderByName')->will($this->returnValue($encoder));
198 'password_encoder' =>
'mockencoder',
199 'encoder_factory' => $factory_mock
203 $this->assertTrue($password_manager->verifyPassword($user_mock, self::PASSWORD));
211 $user_mock = $this->getMockBuilder(
'ilObjUser')->disableOriginalConstructor()->getMock();
212 $encoder = $this->getMockBuilder(
'ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
213 $factory_mock = $this->getMockBuilder(
'ilUserPasswordEncoderFactory')->disableOriginalConstructor()->getMock();
215 $user_mock->expects($this->once())->method(
'getPasswordSalt')->will($this->returnValue(
'asuperrandomsalt'));
216 $user_mock->expects($this->once())->method(
'getPasswordEncodingType')->will($this->returnValue(
'second_mockencoder'));
217 $user_mock->expects($this->once())->method(
'getPasswd')->will($this->returnValue(self::ENCODED_PASSWORD));
218 $user_mock->expects($this->never())->method(
'resetPassword');
220 $encoder->expects($this->once())->method(
'getName')->will($this->returnValue(
'second_mockencoder'));
221 $encoder->expects($this->once())->method(
'isPasswordValid')->with($this->equalTo(self::ENCODED_PASSWORD), $this->equalTo(self::PASSWORD), $this->isType(
'string'))->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->assertFalse($password_manager->verifyPassword($user_mock, self::PASSWORD));
241 $this->assertInstanceOf(
'ilUserPasswordEncoderFactory', $factory);
250 $this->assertEquals(
'md5', $factory->getDefaultEncoder());
252 $encoder = $this->getMockBuilder(
'ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
253 $encoder->expects($this->atLeastOnce())->method(
'getName')->will($this->returnValue(
'mockencoder'));
255 $second_mockencoder = $this->getMockBuilder(
'ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
256 $second_mockencoder->expects($this->atLeastOnce())->method(
'getName')->will($this->returnValue(
'second_mockencoder'));
258 $factory->setEncoders(array($encoder, $second_mockencoder));
259 $this->assertCount(2, $factory->getEncoders());
260 $this->assertCount(2, $factory->getSupportedEncoderNames());
261 $this->assertCount(0, array_diff(array(
'mockencoder',
'second_mockencoder'), $factory->getSupportedEncoderNames()));
262 $this->assertCount(0, array_diff($factory->getSupportedEncoderNames(), array(
'mockencoder',
'second_mockencoder')));
271 $factory->setEncoders(array(
'phpunit'));
280 $factory->getEncoderByName(
'phpunit');
289 $factory->getEncoderByName(
'phpunit',
true);
298 $factory->getEncoderByName(
'phpunit',
true);
306 $encoder = $this->getMockBuilder(
'ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
307 $encoder->expects($this->atLeastOnce())->method(
'getName')->will($this->returnValue(
'mockencoder'));
310 $factory->setEncoders(array($encoder));
311 $this->assertEquals($encoder, $factory->getEncoderByName(
'phpunit',
true));
319 $encoder = $this->getMockBuilder(
'ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
320 $encoder->expects($this->atLeastOnce())->method(
'getName')->will($this->returnValue(
'mockencoder'));
323 $factory->setEncoders(array($encoder));
324 $this->assertEquals($encoder, $factory->getEncoderByName(
'mockencoder',
true));