You are here

public function CasUserManagerTest::testAutomaticRegistration in CAS 8

Same name and namespace in other branches
  1. 2.x tests/src/Unit/Service/CasUserManagerTest.php \Drupal\Tests\cas\Unit\Service\CasUserManagerTest::testAutomaticRegistration()

User account doesn't exist but is auto-registered and logged in.

@dataProvider automaticRegistrationDataProvider

@covers ::login

File

tests/src/Unit/Service/CasUserManagerTest.php, line 267

Class

CasUserManagerTest
CasUserManager unit tests.

Namespace

Drupal\Tests\cas\Unit\Service

Code

public function testAutomaticRegistration($email_assignment_strategy) {
  $config_factory = $this
    ->getConfigFactoryStub([
    'cas.settings' => [
      'user_accounts.auto_register' => TRUE,
      'user_accounts.email_assignment_strategy' => $email_assignment_strategy,
      'user_accounts.email_hostname' => 'sample.com',
      'user_accounts.email_attribute' => 'email',
    ],
  ]);
  $cas_user_manager = $this
    ->getMockBuilder('Drupal\\cas\\Service\\CasUserManager')
    ->setMethods([
    'storeLoginSessionData',
    'randomPassword',
  ])
    ->setConstructorArgs([
    $this->externalAuth,
    $this->authmap,
    $config_factory,
    $this->session,
    $this->connection,
    $this->eventDispatcher,
    $this->casHelper,
    $this->casProxyHelper
      ->reveal(),
  ])
    ->getMock();
  $this->externalAuth
    ->method('load')
    ->willReturn(FALSE);
  $this->account
    ->method('isactive')
    ->willReturn(TRUE);

  // The email address assigned to the user differs depending on the settings.
  // If CAS is configured to use "standard" assignment, it should combine the
  // username with the specifed email hostname. If it's configured to use
  // "attribute" assignment, it should use the value of the specified CAS
  // attribute.
  if ($email_assignment_strategy === CasUserManager::EMAIL_ASSIGNMENT_STANDARD) {
    $expected_assigned_email = 'test@sample.com';
  }
  else {
    $expected_assigned_email = 'test_email@foo.com';
  }
  $this->externalAuth
    ->expects($this
    ->once())
    ->method('register')
    ->with('test', 'cas', [
    'name' => 'test',
    'mail' => $expected_assigned_email,
    'pass' => NULL,
  ])
    ->willReturn($this->account);
  $this->externalAuth
    ->expects($this
    ->once())
    ->method('userLoginFinalize')
    ->willReturn($this->account);
  $cas_property_bag = new CasPropertyBag('test');
  $cas_property_bag
    ->setAttributes([
    'email' => 'test_email@foo.com',
  ]);
  $cas_user_manager
    ->login($cas_property_bag, 'ticket');
}