You are here

trait KeyTestTrait in Key 8

Used for key access tests.

@group key

Hierarchy

File

tests/src/Functional/KeyTestTrait.php, line 14

Namespace

Drupal\Tests\key\Functional
View source
trait KeyTestTrait {

  /**
   * A key entity to use for testing.
   *
   * @var \Drupal\key\KeyInterface
   */
  protected $testKey;

  /**
   * A key configuration override entity to use for testing.
   *
   * @var \Drupal\key\KeyConfigOverrideInterface
   */
  protected $testKeyConfigOverride;

  /**
   * Tests each route for the currently signed-in user.
   */
  protected function routeAccessTest($routes, $response) {
    foreach ($routes as $route => $parameters) {
      $url = Url::fromRoute($route, $parameters);
      $this
        ->drupalGet($url);
      $this
        ->assertSession()
        ->statusCodeEquals($response);
    }
  }

  /**
   * Make a key for testing operations that require a key.
   */
  protected function createTestKey($id, $type = NULL, $provider = NULL) {
    $keyArgs = [
      'id' => $id,
      'label' => 'Test key',
    ];
    if ($type != NULL) {
      $keyArgs['key_type'] = $type;
    }
    if ($provider != NULL) {
      $keyArgs['key_provider'] = $provider;
    }
    $this->testKey = Key::create($keyArgs);
    $this->testKey
      ->save();
    return $this->testKey;
  }

  /**
   * Make a key configuration override for testing operations.
   */
  protected function createTestKeyConfigOverride($override_id, $key_id) {
    $this->testKeyConfigOverride = KeyConfigOverride::create([
      'id' => $override_id,
      'label' => 'Test key configuration override',
      'config_type' => 'system.simple',
      'config_prefix' => '',
      'config_name' => 'system.site',
      'config_item' => 'name',
      'key_id' => $key_id,
    ]);
    $this->testKeyConfigOverride
      ->save();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
KeyTestTrait::$testKey protected property A key entity to use for testing.
KeyTestTrait::$testKeyConfigOverride protected property A key configuration override entity to use for testing.
KeyTestTrait::createTestKey protected function Make a key for testing operations that require a key.
KeyTestTrait::createTestKeyConfigOverride protected function Make a key configuration override for testing operations.
KeyTestTrait::routeAccessTest protected function Tests each route for the currently signed-in user.