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