3 namespace Drupal\Tests\user\Unit;
5 use Drupal\Tests\UnitTestCase;
6 use Drupal\user\UserAuth;
9 * @coversDefaultClass \Drupal\user\UserAuth
12 class UserAuthTest extends UnitTestCase {
15 * The mock user storage.
17 * @var \Drupal\Core\Entity\EntityStorageInterface|\PHPUnit_Framework_MockObject_MockObject
19 protected $userStorage;
22 * The mocked password service.
24 * @var \Drupal\Core\Password\PasswordInterface|\PHPUnit_Framework_MockObject_MockObject
26 protected $passwordService;
31 * @var \Drupal\user\Entity\User|\PHPUnit_Framework_MockObject_MockObject
36 * The user auth object under test.
38 * @var \Drupal\user\UserAuth
47 protected $username = 'test_user';
54 protected $password = 'password';
59 protected function setUp() {
60 $this->userStorage = $this->getMock('Drupal\Core\Entity\EntityStorageInterface');
62 $entity_manager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
63 $entity_manager->expects($this->any())
64 ->method('getStorage')
66 ->will($this->returnValue($this->userStorage));
68 $this->passwordService = $this->getMock('Drupal\Core\Password\PasswordInterface');
70 $this->testUser = $this->getMockBuilder('Drupal\user\Entity\User')
71 ->disableOriginalConstructor()
72 ->setMethods(['id', 'setPassword', 'save', 'getPassword'])
75 $this->userAuth = new UserAuth($entity_manager, $this->passwordService);
79 * Tests failing authentication with missing credential parameters.
81 * @covers ::authenticate
83 * @dataProvider providerTestAuthenticateWithMissingCredentials
85 public function testAuthenticateWithMissingCredentials($username, $password) {
86 $this->userStorage->expects($this->never())
87 ->method('loadByProperties');
89 $this->assertFalse($this->userAuth->authenticate($username, $password));
93 * Data provider for testAuthenticateWithMissingCredentials().
97 public function providerTestAuthenticateWithMissingCredentials() {
107 * Tests the authenticate method with no account returned.
109 * @covers ::authenticate
111 public function testAuthenticateWithNoAccountReturned() {
112 $this->userStorage->expects($this->once())
113 ->method('loadByProperties')
114 ->with(['name' => $this->username])
115 ->will($this->returnValue([]));
117 $this->assertFalse($this->userAuth->authenticate($this->username, $this->password));
121 * Tests the authenticate method with an incorrect password.
123 * @covers ::authenticate
125 public function testAuthenticateWithIncorrectPassword() {
126 $this->userStorage->expects($this->once())
127 ->method('loadByProperties')
128 ->with(['name' => $this->username])
129 ->will($this->returnValue([$this->testUser]));
131 $this->passwordService->expects($this->once())
133 ->with($this->password, $this->testUser->getPassword())
134 ->will($this->returnValue(FALSE));
136 $this->assertFalse($this->userAuth->authenticate($this->username, $this->password));
140 * Tests the authenticate method with a correct password.
142 * @covers ::authenticate
144 public function testAuthenticateWithCorrectPassword() {
145 $this->testUser->expects($this->once())
147 ->will($this->returnValue(1));
149 $this->userStorage->expects($this->once())
150 ->method('loadByProperties')
151 ->with(['name' => $this->username])
152 ->will($this->returnValue([$this->testUser]));
154 $this->passwordService->expects($this->once())
156 ->with($this->password, $this->testUser->getPassword())
157 ->will($this->returnValue(TRUE));
159 $this->assertsame(1, $this->userAuth->authenticate($this->username, $this->password));
163 * Tests the authenticate method with a correct password.
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.
169 * @covers ::authenticate
171 public function testAuthenticateWithZeroPassword() {
172 $this->testUser->expects($this->once())
174 ->will($this->returnValue(2));
176 $this->userStorage->expects($this->once())
177 ->method('loadByProperties')
178 ->with(['name' => $this->username])
179 ->will($this->returnValue([$this->testUser]));
181 $this->passwordService->expects($this->once())
184 ->will($this->returnValue(TRUE));
186 $this->assertsame(2, $this->userAuth->authenticate($this->username, 0));
190 * Tests the authenticate method with a correct password and new password hash.
192 * @covers ::authenticate
194 public function testAuthenticateWithCorrectPasswordAndNewPasswordHash() {
195 $this->testUser->expects($this->once())
197 ->will($this->returnValue(1));
198 $this->testUser->expects($this->once())
199 ->method('setPassword')
200 ->with($this->password);
201 $this->testUser->expects($this->once())
204 $this->userStorage->expects($this->once())
205 ->method('loadByProperties')
206 ->with(['name' => $this->username])
207 ->will($this->returnValue([$this->testUser]));
209 $this->passwordService->expects($this->once())
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));
218 $this->assertsame(1, $this->userAuth->authenticate($this->username, $this->password));