You are here

public function SimplesamlphpDrupalAuthTest::testExternalRegisterWithAutoEnableSaml in simpleSAMLphp Authentication 8.3

Tests external register with autoenablesaml setting.

@covers ::externalRegister @covers ::__construct

File

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

Class

SimplesamlphpDrupalAuthTest
SimplesamlphpDrupalAuth unit tests.

Namespace

Drupal\Tests\simplesamlphp_auth\Unit\Service

Code

public function testExternalRegisterWithAutoEnableSaml() {
  $config_factory = $this
    ->getConfigFactoryStub([
    'simplesamlphp_auth.settings' => [
      'register_users' => TRUE,
      'activate' => TRUE,
      'autoenablesaml' => TRUE,
    ],
  ]);

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

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

  // Create a Mock ExternalAuth object.
  $externalauth = $this
    ->getMockBuilder('\\Drupal\\externalauth\\ExternalAuth')
    ->disableOriginalConstructor()
    ->setMethods([
    'register',
    'linkExistingAccount',
    'userLoginFinalize',
  ])
    ->getMock();

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

  // Set up expectations for ExternalAuth service.
  $externalauth
    ->expects($this
    ->never())
    ->method('register');

  // Set up a mock for SimplesamlphpDrupalAuth class,
  // mocking synchronizeUserAttributes() method.
  $simplesaml_drupalauth = $this
    ->getMockBuilder('Drupal\\simplesamlphp_auth\\Service\\SimplesamlphpDrupalAuth')
    ->setMethods([
    'synchronizeUserAttributes',
  ])
    ->setConstructorArgs([
    $this->simplesaml,
    $config_factory,
    $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');
  $simplesaml_drupalauth
    ->externalRegister("test_authname");
}