Version 1
[yaffs-website] / web / core / modules / user / tests / src / Unit / UserAuthTest.php
1 <?php
2
3 namespace Drupal\Tests\user\Unit;
4
5 use Drupal\Tests\UnitTestCase;
6 use Drupal\user\UserAuth;
7
8 /**
9  * @coversDefaultClass \Drupal\user\UserAuth
10  * @group user
11  */
12 class UserAuthTest extends UnitTestCase {
13
14   /**
15    * The mock user storage.
16    *
17    * @var \Drupal\Core\Entity\EntityStorageInterface|\PHPUnit_Framework_MockObject_MockObject
18    */
19   protected $userStorage;
20
21   /**
22    * The mocked password service.
23    *
24    * @var \Drupal\Core\Password\PasswordInterface|\PHPUnit_Framework_MockObject_MockObject
25    */
26   protected $passwordService;
27
28   /**
29    * The mock user.
30    *
31    * @var \Drupal\user\Entity\User|\PHPUnit_Framework_MockObject_MockObject
32    */
33   protected $testUser;
34
35   /**
36    * The user auth object under test.
37    *
38    * @var \Drupal\user\UserAuth
39    */
40   protected $userAuth;
41
42   /**
43    * The test username.
44    *
45    * @var string
46    */
47   protected $username = 'test_user';
48
49   /**
50    * The test password.
51    *
52    * @var string
53    */
54   protected $password = 'password';
55
56   /**
57    * {@inheritdoc}
58    */
59   protected function setUp() {
60     $this->userStorage = $this->getMock('Drupal\Core\Entity\EntityStorageInterface');
61
62     $entity_manager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
63     $entity_manager->expects($this->any())
64       ->method('getStorage')
65       ->with('user')
66       ->will($this->returnValue($this->userStorage));
67
68     $this->passwordService = $this->getMock('Drupal\Core\Password\PasswordInterface');
69
70     $this->testUser = $this->getMockBuilder('Drupal\user\Entity\User')
71       ->disableOriginalConstructor()
72       ->setMethods(['id', 'setPassword', 'save', 'getPassword'])
73       ->getMock();
74
75     $this->userAuth = new UserAuth($entity_manager, $this->passwordService);
76   }
77
78   /**
79    * Tests failing authentication with missing credential parameters.
80    *
81    * @covers ::authenticate
82    *
83    * @dataProvider providerTestAuthenticateWithMissingCredentials
84    */
85   public function testAuthenticateWithMissingCredentials($username, $password) {
86     $this->userStorage->expects($this->never())
87       ->method('loadByProperties');
88
89     $this->assertFalse($this->userAuth->authenticate($username, $password));
90   }
91
92   /**
93    * Data provider for testAuthenticateWithMissingCredentials().
94    *
95    * @return array
96    */
97   public function providerTestAuthenticateWithMissingCredentials() {
98     return [
99       [NULL, NULL],
100       [NULL, ''],
101       ['', NULL],
102       ['', ''],
103     ];
104   }
105
106   /**
107    * Tests the authenticate method with no account returned.
108    *
109    * @covers ::authenticate
110    */
111   public function testAuthenticateWithNoAccountReturned() {
112     $this->userStorage->expects($this->once())
113       ->method('loadByProperties')
114       ->with(['name' => $this->username])
115       ->will($this->returnValue([]));
116
117     $this->assertFalse($this->userAuth->authenticate($this->username, $this->password));
118   }
119
120   /**
121    * Tests the authenticate method with an incorrect password.
122    *
123    * @covers ::authenticate
124    */
125   public function testAuthenticateWithIncorrectPassword() {
126     $this->userStorage->expects($this->once())
127       ->method('loadByProperties')
128       ->with(['name' => $this->username])
129       ->will($this->returnValue([$this->testUser]));
130
131     $this->passwordService->expects($this->once())
132       ->method('check')
133       ->with($this->password, $this->testUser->getPassword())
134       ->will($this->returnValue(FALSE));
135
136     $this->assertFalse($this->userAuth->authenticate($this->username, $this->password));
137   }
138
139   /**
140    * Tests the authenticate method with a correct password.
141    *
142    * @covers ::authenticate
143    */
144   public function testAuthenticateWithCorrectPassword() {
145     $this->testUser->expects($this->once())
146       ->method('id')
147       ->will($this->returnValue(1));
148
149     $this->userStorage->expects($this->once())
150       ->method('loadByProperties')
151       ->with(['name' => $this->username])
152       ->will($this->returnValue([$this->testUser]));
153
154     $this->passwordService->expects($this->once())
155       ->method('check')
156       ->with($this->password, $this->testUser->getPassword())
157       ->will($this->returnValue(TRUE));
158
159     $this->assertsame(1, $this->userAuth->authenticate($this->username, $this->password));
160   }
161
162   /**
163    * Tests the authenticate method with a correct password.
164    *
165    * We discovered in https://www.drupal.org/node/2563751 that logging in with a
166    * password that is literally "0" was not possible. This test ensures that
167    * this regression can't happen again.
168    *
169    * @covers ::authenticate
170    */
171   public function testAuthenticateWithZeroPassword() {
172     $this->testUser->expects($this->once())
173       ->method('id')
174       ->will($this->returnValue(2));
175
176     $this->userStorage->expects($this->once())
177       ->method('loadByProperties')
178       ->with(['name' => $this->username])
179       ->will($this->returnValue([$this->testUser]));
180
181     $this->passwordService->expects($this->once())
182       ->method('check')
183       ->with(0, 0)
184       ->will($this->returnValue(TRUE));
185
186     $this->assertsame(2, $this->userAuth->authenticate($this->username, 0));
187   }
188
189   /**
190    * Tests the authenticate method with a correct password and new password hash.
191    *
192    * @covers ::authenticate
193    */
194   public function testAuthenticateWithCorrectPasswordAndNewPasswordHash() {
195     $this->testUser->expects($this->once())
196       ->method('id')
197       ->will($this->returnValue(1));
198     $this->testUser->expects($this->once())
199       ->method('setPassword')
200       ->with($this->password);
201     $this->testUser->expects($this->once())
202       ->method('save');
203
204     $this->userStorage->expects($this->once())
205       ->method('loadByProperties')
206       ->with(['name' => $this->username])
207       ->will($this->returnValue([$this->testUser]));
208
209     $this->passwordService->expects($this->once())
210       ->method('check')
211       ->with($this->password, $this->testUser->getPassword())
212       ->will($this->returnValue(TRUE));
213     $this->passwordService->expects($this->once())
214       ->method('needsRehash')
215       ->with($this->testUser->getPassword())
216       ->will($this->returnValue(TRUE));
217
218     $this->assertsame(1, $this->userAuth->authenticate($this->username, $this->password));
219   }
220
221 }