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