You are here

trait CasTestTrait in CAS 8

Same name and namespace in other branches
  1. 2.x tests/src/Traits/CasTestTrait.php \Drupal\Tests\cas\Traits\CasTestTrait

Provides reusable code for tests.

Hierarchy

2 files declare their use of CasTestTrait
CasEventsTest.php in tests/src/Functional/CasEventsTest.php
CasUserInteractionTest.php in tests/src/Functional/CasUserInteractionTest.php

File

tests/src/Traits/CasTestTrait.php, line 11

Namespace

Drupal\Tests\cas\Traits
View source
trait CasTestTrait {

  /**
   * Creates a CAS user and starts the CAS mock server.
   *
   * @param string $authname
   *   The CAS authentication name.
   * @param string $email
   *   The CAS user email.
   * @param string $password
   *   The CAS server password.
   * @param array $attributes
   *   (optional) Additional attributes to be added to the CAS account.
   * @param \Drupal\user\UserInterface|null $local_account
   *   (optional) A user local account. If passed, the CAS user will be linked
   *   with the local user.
   */
  protected function createCasUser($authname, $email, $password, array $attributes = [], UserInterface $local_account = NULL) {
    $cas_user = [
      'username' => $authname,
      'email' => $email,
      'password' => $password,
    ] + $attributes;
    \Drupal::service('cas_mock_server.user_manager')
      ->addUser($cas_user);

    // Link with the local account if it has been requested.
    if ($local_account) {
      \Drupal::service('externalauth.externalauth')
        ->linkExistingAccount($authname, 'cas', $local_account);
    }

    // Start the CAS mock server.
    \Drupal::service('cas_mock_server.server_manager')
      ->start();
  }

  /**
   * Logs-in the user to the CAS mock server.
   *
   * @param string $email
   *   The CAS email.
   * @param string $password
   *   The CAS user password.
   */
  protected function casLogin($email, $password) {
    $query = [
      'service' => Url::fromRoute('cas.service')
        ->setAbsolute()
        ->toString(),
    ];
    $edit = [
      'email' => $email,
      'password' => $password,
    ];
    $this
      ->drupalPostForm('/cas-mock-server/login', $edit, 'Log in', [
      'query' => $query,
    ]);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CasTestTrait::casLogin protected function Logs-in the user to the CAS mock server.
CasTestTrait::createCasUser protected function Creates a CAS user and starts the CAS mock server.