You are here

public function SimplesamlphpDrupalAuthTest::testExternalLoginWithRoleMatch in simpleSAMLphp Authentication 8.3

Tests external login with role matching.

@covers ::externalLoginRegister @covers ::roleMatchSync @covers ::evalRoleRule @covers ::__construct

File

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

Class

SimplesamlphpDrupalAuthTest
SimplesamlphpDrupalAuth unit tests.

Namespace

Drupal\Tests\simplesamlphp_auth\Unit\Service

Code

public function testExternalLoginWithRoleMatch() {

  // Set up specific configuration to test external login & role matching.
  $config_factory = $this
    ->getConfigFactoryStub([
    'simplesamlphp_auth.settings' => [
      'register_users' => TRUE,
      'activate' => 1,
      'role.eval_every_time' => 1,
      'role.population' => 'student:eduPersonAffiliation,=,student',
    ],
  ]);

  // Get a Mock User object to test the external login method.
  // Expect the role "student" to be added to the user entity.
  // Expect the role "teacher" to be removed from user entity.
  $this->entityAccount
    ->expects($this
    ->once())
    ->method('getRoles')
    ->will($this
    ->returnValue([
    'teacher',
  ]));
  $this->entityAccount
    ->expects($this
    ->once())
    ->method('addRole')
    ->with($this
    ->equalTo('student'));
  $this->entityAccount
    ->expects($this
    ->once())
    ->method('removeRole')
    ->with($this
    ->equalTo('teacher'));
  $this->entityAccount
    ->expects($this
    ->once())
    ->method('save');
  $this->externalauth
    ->expects($this
    ->once())
    ->method('login')
    ->will($this
    ->returnValue($this->entityAccount));

  // Create a Mock SimplesamlphpAuthManager object.
  $simplesaml = $this
    ->getMockBuilder('\\Drupal\\simplesamlphp_auth\\Service\\SimplesamlphpAuthManager')
    ->disableOriginalConstructor()
    ->setMethods([
    'getAttributes',
  ])
    ->getMock();

  // Mock the getAttributes() method on SimplesamlphpAuthManager.
  $attributes = [
    'eduPersonAffiliation' => [
      'student',
    ],
  ];
  $simplesaml
    ->expects($this
    ->any())
    ->method('getAttributes')
    ->will($this
    ->returnValue($attributes));

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

  // Now that everything is set up, call externalLogin() and expect a User.
  $simplesaml_drupalauth
    ->externalLoginRegister("testuser");
}