You are here

public function ExternalAuthTest::testRegister 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::testRegister()

Test the register() method.

@covers ::register @covers ::__construct

@dataProvider registerDataProvider

File

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

Class

ExternalAuthTest
ExternalAuth unit tests.

Namespace

Drupal\Tests\externalauth\Unit

Code

public function testRegister($registration_data, $expected_data) {

  // Mock the returned User object.
  $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']));

  // Mock the User storage layer to create a new user.
  $entity_storage = $this
    ->createMock('Drupal\\Core\\Entity\\EntityStorageInterface');

  // Expect the external registration to return us a user object.
  $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));

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