View source
<?php
namespace Drupal\Tests\user\Unit;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\RequestContext;
use Drupal\Core\Routing\TrustedRedirectResponse;
use Drupal\Tests\UnitTestCase;
use Drupal\user\Authentication\Provider\Cookie;
use Drupal\user\UserAuth;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
class UserAuthTest extends UnitTestCase {
protected $userStorage;
protected $passwordService;
protected $testUser;
protected $userAuth;
protected $username = 'test_user';
protected $password = 'password';
protected function setUp() : void {
$this->userStorage = $this
->createMock('Drupal\\Core\\Entity\\EntityStorageInterface');
$entity_type_manager = $this
->createMock(EntityTypeManagerInterface::class);
$entity_type_manager
->expects($this
->any())
->method('getStorage')
->with('user')
->will($this
->returnValue($this->userStorage));
$this->passwordService = $this
->createMock('Drupal\\Core\\Password\\PasswordInterface');
$this->testUser = $this
->getMockBuilder('Drupal\\user\\Entity\\User')
->disableOriginalConstructor()
->onlyMethods([
'id',
'setPassword',
'save',
'getPassword',
])
->getMock();
$this->userAuth = new UserAuth($entity_type_manager, $this->passwordService);
}
public function testAuthenticateWithMissingCredentials($username, $password) {
$this->userStorage
->expects($this
->never())
->method('loadByProperties');
$this
->assertFalse($this->userAuth
->authenticate($username, $password));
}
public function providerTestAuthenticateWithMissingCredentials() {
return [
[
NULL,
NULL,
],
[
NULL,
'',
],
[
'',
NULL,
],
[
'',
'',
],
];
}
public function testAuthenticateWithNoAccountReturned() {
$this->userStorage
->expects($this
->once())
->method('loadByProperties')
->with([
'name' => $this->username,
])
->will($this
->returnValue([]));
$this
->assertFalse($this->userAuth
->authenticate($this->username, $this->password));
}
public function testAuthenticateWithIncorrectPassword() {
$this->userStorage
->expects($this
->once())
->method('loadByProperties')
->with([
'name' => $this->username,
])
->will($this
->returnValue([
$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));
}
public function testAuthenticateWithCorrectPassword() {
$this->testUser
->expects($this
->once())
->method('id')
->will($this
->returnValue(1));
$this->userStorage
->expects($this
->once())
->method('loadByProperties')
->with([
'name' => $this->username,
])
->will($this
->returnValue([
$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));
}
public function testAuthenticateWithZeroPassword() {
$this->testUser
->expects($this
->once())
->method('id')
->will($this
->returnValue(2));
$this->userStorage
->expects($this
->once())
->method('loadByProperties')
->with([
'name' => $this->username,
])
->will($this
->returnValue([
$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));
}
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([
'name' => $this->username,
])
->will($this
->returnValue([
$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));
}
public function testAddCheckToUrlForTrustedRedirectResponse() : void {
$site_domain = 'site.com';
$frontend_url = "https://{$site_domain}";
$backend_url = "https://api.{$site_domain}";
$request = Request::create($backend_url);
$response = new TrustedRedirectResponse($frontend_url);
$request_context = $this
->createMock(RequestContext::class);
$request_context
->method('getCompleteBaseUrl')
->willReturn($backend_url);
$container = new ContainerBuilder();
$container
->set('router.request_context', $request_context);
\Drupal::setContainer($container);
$session_mock = $this
->createMock(SessionInterface::class);
$session_mock
->expects($this
->once())
->method('has')
->with('check_logged_in')
->willReturn(TRUE);
$session_mock
->expects($this
->once())
->method('remove')
->with('check_logged_in');
$event = new ResponseEvent($this
->createMock(HttpKernelInterface::class), $request, HttpKernelInterface::MAIN_REQUEST, $response);
$request
->setSession($session_mock);
$this
->getMockBuilder(Cookie::class)
->disableOriginalConstructor()
->onlyMethods([])
->getMock()
->addCheckToUrl($event);
$this
->assertSame("{$frontend_url}?check_logged_in=1", $response
->getTargetUrl());
}
}