You are here

public function SimplesamlphpDrupalAuthTest::testExternalRegister in simpleSAMLphp Authentication 8.3

Test external registration functionality.

@covers ::externalRegister @covers ::__construct

File

tests/src/Unit/Service/SimplesamlphpDrupalAuthTest.php, line 233

Class

SimplesamlphpDrupalAuthTest
SimplesamlphpDrupalAuth unit tests.

Namespace

Drupal\Tests\simplesamlphp_auth\Unit\Service

Code

public function testExternalRegister() {

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

  // Expect the entity storage to return no existing user.
  $entity_storage
    ->expects($this
    ->any())
    ->method('loadByProperties')
    ->will($this
    ->returnValue([]));
  $this->entityTypeManager
    ->expects($this
    ->any())
    ->method('getStorage')
    ->will($this
    ->returnValue($entity_storage));

  // Create a Mock ExternalAuth object.
  $externalauth = $this
    ->createMock('Drupal\\externalauth\\ExternalAuthInterface');

  // Set up expectations for ExternalAuth service.
  $externalauth
    ->expects($this
    ->once())
    ->method('register')
    ->will($this
    ->returnValue($this->entityAccount));
  $externalauth
    ->expects($this
    ->once())
    ->method('userLoginFinalize')
    ->will($this
    ->returnValue($this->entityAccount));

  // Set up a mock for SimplesamlphpDrupalAuth class,
  // mocking synchronizeUserAttributes() method.
  $simplesaml_drupalauth = $this
    ->getMockBuilder('Drupal\\simplesamlphp_auth\\Service\\SimplesamlphpDrupalAuth')
    ->setMethods([
    'synchronizeUserAttributes',
  ])
    ->setConstructorArgs([
    $this->simplesaml,
    $this->configFactory,
    $this->entityTypeManager,
    $this->logger,
    $externalauth,
    $this->entityAccount,
    $this->messenger,
    $this->moduleHandler,
  ])
    ->getMock();

  // Mock some methods on SimplesamlphpDrupalAuth, since they are out of scope
  // of this specific unit test.
  $simplesaml_drupalauth
    ->expects($this
    ->once())
    ->method('synchronizeUserAttributes');

  // Now that everything is set up, call externalRegister() and expect a User.
  $registered_account = $simplesaml_drupalauth
    ->externalRegister("testuser");
  $this
    ->assertTrue($registered_account instanceof UserInterface);
}