You are here

public function ExternalAuthTest::testLinkExistingAccount in External Authentication 8

Same name and namespace in other branches
  1. 2.0.x tests/src/Unit/ExternalAuthTest.php \Drupal\Tests\externalauth\Unit\ExternalAuthTest::testLinkExistingAccount()

Test linking an existing account.

File

tests/src/Unit/ExternalAuthTest.php, line 312

Class

ExternalAuthTest
ExternalAuth unit tests.

Namespace

Drupal\Tests\externalauth\Unit

Code

public function testLinkExistingAccount() {
  $account = $this
    ->createMock('Drupal\\user\\UserInterface');
  $account
    ->expects($this
    ->once())
    ->method('id')
    ->will($this
    ->returnValue(5));

  // Set up a mock for Authmap class,
  // mocking get() & save() methods.
  $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);
}