class UserAuthTest in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/user/tests/src/Unit/UserAuthTest.php \Drupal\Tests\user\Unit\UserAuthTest
@coversDefaultClass \Drupal\user\UserAuth @group user
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \Drupal\Tests\PHPUnit_Framework_TestCase
- class \Drupal\Tests\user\Unit\UserAuthTest
Expanded class hierarchy of UserAuthTest
File
- core/
modules/ user/ tests/ src/ Unit/ UserAuthTest.php, line 17 - Contains \Drupal\Tests\user\Unit\UserAuthTest.
Namespace
Drupal\Tests\user\UnitView source
class UserAuthTest extends UnitTestCase {
/**
* The mock user storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $userStorage;
/**
* The mocked password service.
*
* @var \Drupal\Core\Password\PasswordInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $passwordService;
/**
* The mock user.
*
* @var \Drupal\user\Entity\User|\PHPUnit_Framework_MockObject_MockObject
*/
protected $testUser;
/**
* The user auth object under test.
*
* @var \Drupal\user\UserAuth
*/
protected $userAuth;
/**
* The test username.
*
* @var string
*/
protected $username = 'test_user';
/**
* The test password.
*
* @var string
*/
protected $password = 'password';
/**
* {@inheritdoc}
*/
protected function setUp() {
$this->userStorage = $this
->getMock('Drupal\\Core\\Entity\\EntityStorageInterface');
$entity_manager = $this
->getMock('Drupal\\Core\\Entity\\EntityManagerInterface');
$entity_manager
->expects($this
->any())
->method('getStorage')
->with('user')
->will($this
->returnValue($this->userStorage));
$this->passwordService = $this
->getMock('Drupal\\Core\\Password\\PasswordInterface');
$this->testUser = $this
->getMockBuilder('Drupal\\user\\Entity\\User')
->disableOriginalConstructor()
->setMethods(array(
'id',
'setPassword',
'save',
'getPassword',
))
->getMock();
$this->userAuth = new UserAuth($entity_manager, $this->passwordService);
}
/**
* Tests failing authentication with missing credential parameters.
*
* @covers ::authenticate
*
* @dataProvider providerTestAuthenticateWithMissingCredentials
*/
public function testAuthenticateWithMissingCredentials($username, $password) {
$this->userStorage
->expects($this
->never())
->method('loadByProperties');
$this
->assertFalse($this->userAuth
->authenticate($username, $password));
}
/**
* Data provider for testAuthenticateWithMissingCredentials().
*
* @return array
*/
public function providerTestAuthenticateWithMissingCredentials() {
return array(
array(
NULL,
NULL,
),
array(
NULL,
'',
),
array(
'',
NULL,
),
array(
'',
'',
),
);
}
/**
* Tests the authenticate method with no account returned.
*
* @covers ::authenticate
*/
public function testAuthenticateWithNoAccountReturned() {
$this->userStorage
->expects($this
->once())
->method('loadByProperties')
->with(array(
'name' => $this->username,
))
->will($this
->returnValue(array()));
$this
->assertFalse($this->userAuth
->authenticate($this->username, $this->password));
}
/**
* Tests the authenticate method with an incorrect password.
*
* @covers ::authenticate
*/
public function testAuthenticateWithIncorrectPassword() {
$this->userStorage
->expects($this
->once())
->method('loadByProperties')
->with(array(
'name' => $this->username,
))
->will($this
->returnValue(array(
$this->testUser,
)));
$this->passwordService
->expects($this
->once())
->method('check')
->with($this->password, $this->testUser
->getPassword())
->will($this
->returnValue(FALSE));
$this
->assertFalse($this->userAuth
->authenticate($this->username, $this->password));
}
/**
* Tests the authenticate method with a correct password.
*
* @covers ::authenticate
*/
public function testAuthenticateWithCorrectPassword() {
$this->testUser
->expects($this
->once())
->method('id')
->will($this
->returnValue(1));
$this->userStorage
->expects($this
->once())
->method('loadByProperties')
->with(array(
'name' => $this->username,
))
->will($this
->returnValue(array(
$this->testUser,
)));
$this->passwordService
->expects($this
->once())
->method('check')
->with($this->password, $this->testUser
->getPassword())
->will($this
->returnValue(TRUE));
$this
->assertsame(1, $this->userAuth
->authenticate($this->username, $this->password));
}
/**
* Tests the authenticate method with a correct password.
*
* We discovered in https://www.drupal.org/node/2563751 that logging in with a
* password that is literally "0" was not possible. This test ensures that
* this regression can't happen again.
*
* @covers ::authenticate
*/
public function testAuthenticateWithZeroPassword() {
$this->testUser
->expects($this
->once())
->method('id')
->will($this
->returnValue(2));
$this->userStorage
->expects($this
->once())
->method('loadByProperties')
->with(array(
'name' => $this->username,
))
->will($this
->returnValue(array(
$this->testUser,
)));
$this->passwordService
->expects($this
->once())
->method('check')
->with(0, 0)
->will($this
->returnValue(TRUE));
$this
->assertsame(2, $this->userAuth
->authenticate($this->username, 0));
}
/**
* Tests the authenticate method with a correct password and new password hash.
*
* @covers ::authenticate
*/
public function testAuthenticateWithCorrectPasswordAndNewPasswordHash() {
$this->testUser
->expects($this
->once())
->method('id')
->will($this
->returnValue(1));
$this->testUser
->expects($this
->once())
->method('setPassword')
->with($this->password);
$this->testUser
->expects($this
->once())
->method('save');
$this->userStorage
->expects($this
->once())
->method('loadByProperties')
->with(array(
'name' => $this->username,
))
->will($this
->returnValue(array(
$this->testUser,
)));
$this->passwordService
->expects($this
->once())
->method('check')
->with($this->password, $this->testUser
->getPassword())
->will($this
->returnValue(TRUE));
$this->passwordService
->expects($this
->once())
->method('needsRehash')
->with($this->testUser
->getPassword())
->will($this
->returnValue(TRUE));
$this
->assertsame(1, $this->userAuth
->authenticate($this->username, $this->password));
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed in array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. | |
UserAuthTest:: |
protected | property | The test password. | |
UserAuthTest:: |
protected | property | The mocked password service. | |
UserAuthTest:: |
protected | property | The mock user. | |
UserAuthTest:: |
protected | property | The user auth object under test. | |
UserAuthTest:: |
protected | property | The test username. | |
UserAuthTest:: |
protected | property | The mock user storage. | |
UserAuthTest:: |
public | function | Data provider for testAuthenticateWithMissingCredentials(). | |
UserAuthTest:: |
protected | function |
Overrides UnitTestCase:: |
|
UserAuthTest:: |
public | function | Tests the authenticate method with a correct password. | |
UserAuthTest:: |
public | function | Tests the authenticate method with a correct password and new password hash. | |
UserAuthTest:: |
public | function | Tests the authenticate method with an incorrect password. | |
UserAuthTest:: |
public | function | Tests failing authentication with missing credential parameters. | |
UserAuthTest:: |
public | function | Tests the authenticate method with no account returned. | |
UserAuthTest:: |
public | function | Tests the authenticate method with a correct password. |