View source
<?php
namespace Drupal\Tests\externalauth\Unit;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Tests\UnitTestCase;
use Drupal\user\UserInterface;
use Drupal\externalauth\ExternalAuth;
class ExternalAuthTest extends UnitTestCase {
protected $entityTypeManager;
protected $authmap;
protected $logger;
protected $eventDispatcher;
protected function setUp() {
parent::setUp();
$this->entityTypeManager = $this
->createMock(EntityTypeManagerInterface::class);
$this->logger = $this
->getMockBuilder('\\Psr\\Log\\LoggerInterface')
->disableOriginalConstructor()
->getMock();
$this->eventDispatcher = $this
->getMockBuilder('\\Symfony\\Component\\EventDispatcher\\EventDispatcherInterface')
->disableOriginalConstructor()
->getMock();
$this->authmap = $this
->getMockBuilder('\\Drupal\\externalauth\\Authmap')
->disableOriginalConstructor()
->getMock();
}
public function testLoad() {
$authmap = $this
->getMockBuilder('\\Drupal\\externalauth\\Authmap')
->disableOriginalConstructor()
->setMethods([
'getUid',
])
->getMock();
$authmap
->expects($this
->once())
->method('getUid')
->will($this
->returnValue(2));
$account = $this
->createMock('Drupal\\user\\UserInterface');
$entity_storage = $this
->createMock('Drupal\\Core\\Entity\\EntityStorageInterface');
$entity_storage
->expects($this
->once())
->method('load')
->will($this
->returnValue($account));
$this->entityTypeManager
->expects($this
->once())
->method('getStorage')
->will($this
->returnValue($entity_storage));
$externalauth = new ExternalAuth($this->entityTypeManager, $authmap, $this->logger, $this->eventDispatcher);
$result = $externalauth
->load("test_authname", "test_provider");
$this
->assertInstanceOf(UserInterface::class, $result);
}
public function testLogin() {
$externalauth = $this
->getMockBuilder('Drupal\\externalauth\\ExternalAuth')
->setMethods([
'load',
'userLoginFinalize',
])
->setConstructorArgs([
$this->entityTypeManager,
$this->authmap,
$this->logger,
$this->eventDispatcher,
])
->getMock();
$externalauth
->expects($this
->once())
->method('load')
->will($this
->returnValue(FALSE));
$externalauth
->expects($this
->never())
->method('userLoginFinalize');
$result = $externalauth
->login("test_authname", "test_provider");
$this
->assertEquals(FALSE, $result);
}
public function testRegister($registration_data, $expected_data) {
$account = $this
->createMock('Drupal\\user\\UserInterface');
$account
->expects($this
->once())
->method('enforceIsNew');
$account
->expects($this
->once())
->method('save');
$account
->expects($this
->any())
->method('getTimeZone')
->will($this
->returnValue($expected_data['timezone']));
$entity_storage = $this
->createMock('Drupal\\Core\\Entity\\EntityStorageInterface');
$entity_storage
->expects($this
->any())
->method('create')
->will($this
->returnValue($account));
$entity_storage
->expects($this
->any())
->method('loadByProperties')
->will($this
->returnValue([]));
$this->entityTypeManager
->expects($this
->any())
->method('getStorage')
->will($this
->returnValue($entity_storage));
$authmap = $this
->getMockBuilder('\\Drupal\\externalauth\\Authmap')
->disableOriginalConstructor()
->setMethods([
'save',
])
->getMock();
$authmap
->expects($this
->once())
->method('save');
$dispatched_event = $this
->getMockBuilder('\\Drupal\\externalauth\\Event\\ExternalAuthAuthmapAlterEvent')
->disableOriginalConstructor()
->getMock();
$dispatched_event
->expects($this
->any())
->method('getUsername')
->will($this
->returnValue($expected_data['username']));
$dispatched_event
->expects($this
->any())
->method('getAuthname')
->will($this
->returnValue($expected_data['authname']));
$dispatched_event
->expects($this
->any())
->method('getData')
->will($this
->returnValue($expected_data['data']));
$this->eventDispatcher
->expects($this
->any())
->method('dispatch')
->will($this
->returnValue($dispatched_event));
$externalauth = new ExternalAuth($this->entityTypeManager, $authmap, $this->logger, $this->eventDispatcher);
$registered_account = $externalauth
->register($registration_data['authname'], $registration_data['provider'], $registration_data['account_data'], $registration_data['authmap_data']);
$this
->assertInstanceOf(UserInterface::class, $registered_account);
$this
->assertEquals($expected_data['timezone'], $registered_account
->getTimeZone());
$this
->assertEquals($expected_data['data'], $dispatched_event
->getData());
}
public function registerDataProvider() {
return [
[
[
'authname' => 'test_authname',
'provider' => 'test_provider',
'account_data' => [],
'authmap_data' => NULL,
],
[
'username' => 'test_provider-test_authname',
'authname' => 'test_authname',
'timezone' => 'Europe/Brussels',
'data' => [],
],
],
[
[
'authname' => 'test_authname',
'provider' => 'test_provider',
'account_data' => [
'timezone' => 'Europe/Prague',
],
'authmap_data' => NULL,
],
[
'username' => 'test_provider-test_authname',
'authname' => 'test_authname',
'timezone' => 'Europe/Prague',
'data' => [],
],
],
[
[
'authname' => 'test_authname',
'provider' => 'test_provider',
'account_data' => [],
'authmap_data' => [
'extra_property' => 'extra',
],
],
[
'username' => 'test_provider-test_authname',
'authname' => 'test_authname',
'timezone' => 'Europe/Brussels',
'data' => [
'extra_property' => 'extra',
],
],
],
];
}
public function testLoginRegister() {
$account = $this
->createMock('Drupal\\user\\UserInterface');
$externalauth = $this
->getMockBuilder('Drupal\\externalauth\\ExternalAuth')
->setMethods([
'login',
'register',
'userLoginFinalize',
])
->setConstructorArgs([
$this->entityTypeManager,
$this->authmap,
$this->logger,
$this->eventDispatcher,
])
->getMock();
$externalauth
->expects($this
->once())
->method('login')
->will($this
->returnValue(FALSE));
$externalauth
->expects($this
->once())
->method('register')
->will($this
->returnValue($account));
$externalauth
->expects($this
->once())
->method('userLoginFinalize')
->will($this
->returnValue($account));
$result = $externalauth
->loginRegister("test_authname", "test_provider");
$this
->assertInstanceOf(UserInterface::class, $result);
}
public function testLinkExistingAccount() {
$account = $this
->createMock('Drupal\\user\\UserInterface');
$account
->expects($this
->once())
->method('id')
->will($this
->returnValue(5));
$authmap = $this
->getMockBuilder('\\Drupal\\externalauth\\Authmap')
->disableOriginalConstructor()
->setMethods([
'save',
'get',
])
->getMock();
$authmap
->expects($this
->once())
->method('get')
->will($this
->returnValue(FALSE));
$authmap
->expects($this
->once())
->method('save');
$dispatched_event = $this
->getMockBuilder('\\Drupal\\externalauth\\Event\\ExternalAuthAuthmapAlterEvent')
->disableOriginalConstructor()
->getMock();
$dispatched_event
->expects($this
->any())
->method('getUsername')
->will($this
->returnValue("Test username"));
$dispatched_event
->expects($this
->any())
->method('getAuthname')
->will($this
->returnValue("Test authname"));
$dispatched_event
->expects($this
->any())
->method('getData')
->will($this
->returnValue("Test data"));
$this->eventDispatcher
->expects($this
->any())
->method('dispatch')
->will($this
->returnValue($dispatched_event));
$externalauth = new ExternalAuth($this->entityTypeManager, $authmap, $this->logger, $this->eventDispatcher);
$externalauth
->linkExistingAccount("test_authname", "test_provider", $account);
}
}