ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
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 
16 {
20  const PASSWORD = 'password';
21 
25  const ENCODED_PASSWORD = 'encoded';
26 
31  {
33  }
34 
39  {
41  array(
42  'password_encoder' => 'md5'
43  )
44  );
45  }
46 
51  {
53  array(
54  'password_encoder' => 'md5',
55  'encoder_factory' => 'test'
56  )
57  );
58  }
59 
63  public function testInstanceCanBeCreated()
64  {
65  $factory_mock = $this->getMockBuilder('ilUserPasswordEncoderFactory')->disableOriginalConstructor()->getMock();
66  $factory_mock->expects($this->exactly(2))->method('getSupportedEncoderNames')->will($this->onConsecutiveCalls(
67  array(
68  'mockencoder', 'second_mockencoder'
69  ),
70  array(
71  'mockencoder'
72  )
73  ));
74 
75  $password_manager = new ilUserPasswordManager(
76  array(
77  'password_encoder' => 'md5',
78  'encoder_factory' => $factory_mock
79  )
80  );
81  $this->assertInstanceOf('ilUserPasswordManager', $password_manager);
82  $this->assertEquals('md5', $password_manager->getEncoderName());
83  $this->assertEquals($factory_mock, $password_manager->getEncoderFactory());
84 
85  $this->assertTrue($password_manager->isEncodingTypeSupported('second_mockencoder'));
86  $this->assertFalse($password_manager->isEncodingTypeSupported('second_mockencoder'));
87  }
88 
93  {
94  $user_mock = $this->getMockBuilder('ilObjUser')->disableOriginalConstructor()->getMock();
95  $encoder = $this->getMockBuilder('ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
96  $factory_mock = $this->getMockBuilder('ilUserPasswordEncoderFactory')->disableOriginalConstructor()->getMock();
97 
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));
102 
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));
106 
107  $factory_mock->expects($this->once())->method('getEncoderByName')->will($this->returnValue($encoder));
108 
109  $password_manager = new ilUserPasswordManager(
110  array(
111  'password_encoder' => 'mockencoder',
112  'encoder_factory' => $factory_mock
113  )
114  );
115 
116  $password_manager->encodePassword($user_mock, self::PASSWORD);
117  }
118 
123  {
124  $user_mock = $this->getMockBuilder('ilObjUser')->disableOriginalConstructor()->getMock();
125  $encoder = $this->getMockBuilder('ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
126  $factory_mock = $this->getMockBuilder('ilUserPasswordEncoderFactory')->disableOriginalConstructor()->getMock();
127 
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));
132 
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));
136 
137  $factory_mock->expects($this->once())->method('getEncoderByName')->will($this->returnValue($encoder));
138 
139  $password_manager = new ilUserPasswordManager(
140  array(
141  'password_encoder' => 'mockencoder',
142  'encoder_factory' => $factory_mock
143  )
144  );
145 
146  $password_manager->encodePassword($user_mock, self::PASSWORD);
147  }
148 
153  {
154  $user_mock = $this->getMockBuilder('ilObjUser')->disableOriginalConstructor()->getMock();
155  $encoder = $this->getMockBuilder('ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
156  $factory_mock = $this->getMockBuilder('ilUserPasswordEncoderFactory')->disableOriginalConstructor()->getMock();
157 
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));
161 
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));
164 
165  $factory_mock->expects($this->once())->method('getEncoderByName')->will($this->returnValue($encoder));
166 
167  $password_manager = new ilUserPasswordManager(
168  array(
169  'password_encoder' => 'mockencoder',
170  'encoder_factory' => $factory_mock
171  )
172  );
173 
174  $this->assertTrue($password_manager->verifyPassword($user_mock, self::PASSWORD));
175  }
176 
181  {
182  $user_mock = $this->getMockBuilder('ilObjUser')->disableOriginalConstructor()->getMock();
183  $encoder = $this->getMockBuilder('ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
184  $factory_mock = $this->getMockBuilder('ilUserPasswordEncoderFactory')->disableOriginalConstructor()->getMock();
185 
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));
190 
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));
193 
194  $factory_mock->expects($this->once())->method('getEncoderByName')->will($this->returnValue($encoder));
195 
196  $password_manager = new ilUserPasswordManager(
197  array(
198  'password_encoder' => 'mockencoder',
199  'encoder_factory' => $factory_mock
200  )
201  );
202 
203  $this->assertTrue($password_manager->verifyPassword($user_mock, self::PASSWORD));
204  }
205 
210  {
211  $user_mock = $this->getMockBuilder('ilObjUser')->disableOriginalConstructor()->getMock();
212  $encoder = $this->getMockBuilder('ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
213  $factory_mock = $this->getMockBuilder('ilUserPasswordEncoderFactory')->disableOriginalConstructor()->getMock();
214 
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');
219 
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));
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->assertFalse($password_manager->verifyPassword($user_mock, self::PASSWORD));
233  }
234 
238  public function testFactoryCanBeCreated()
239  {
240  $factory = new ilUserPasswordEncoderFactory();
241  $this->assertInstanceOf('ilUserPasswordEncoderFactory', $factory);
242  }
243 
248  {
249  $factory = new ilUserPasswordEncoderFactory(array('default_password_encoder' => 'md5'));
250  $this->assertEquals('md5', $factory->getDefaultEncoder());
251 
252  $encoder = $this->getMockBuilder('ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
253  $encoder->expects($this->atLeastOnce())->method('getName')->will($this->returnValue('mockencoder'));
254 
255  $second_mockencoder = $this->getMockBuilder('ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
256  $second_mockencoder->expects($this->atLeastOnce())->method('getName')->will($this->returnValue('second_mockencoder'));
257 
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')));
263  }
264 
269  {
270  $factory = new ilUserPasswordEncoderFactory();
271  $factory->setEncoders(array('phpunit'));
272  }
273 
278  {
279  $factory = new ilUserPasswordEncoderFactory(array('default_password_encoder' => 'md5'));
280  $factory->getEncoderByName('phpunit');
281  }
282 
287  {
288  $factory = new ilUserPasswordEncoderFactory();
289  $factory->getEncoderByName('phpunit', true);
290  }
291 
296  {
297  $factory = new ilUserPasswordEncoderFactory(array('default_password_encoder' => 'phpunit'));
298  $factory->getEncoderByName('phpunit', true);
299  }
300 
305  {
306  $encoder = $this->getMockBuilder('ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
307  $encoder->expects($this->atLeastOnce())->method('getName')->will($this->returnValue('mockencoder'));
308 
309  $factory = new ilUserPasswordEncoderFactory(array('default_password_encoder' => $encoder->getName()));
310  $factory->setEncoders(array($encoder));
311  $this->assertEquals($encoder, $factory->getEncoderByName('phpunit', true));
312  }
313 
318  {
319  $encoder = $this->getMockBuilder('ilBasePasswordEncoder')->disableOriginalConstructor()->getMock();
320  $encoder->expects($this->atLeastOnce())->method('getName')->will($this->returnValue('mockencoder'));
321 
322  $factory = new ilUserPasswordEncoderFactory(array('default_password_encoder' => $encoder->getName()));
323  $factory->setEncoders(array($encoder));
324  $this->assertEquals($encoder, $factory->getEncoderByName('mockencoder', true));
325  }
326 }