You are here

trait ApigeeEdgeUtilTestTrait in Apigee Edge 8

Provides common functionality for the Apigee Edge test classes.

Hierarchy

1 file declares its use of ApigeeEdgeUtilTestTrait
ApigeeMockApiClientHelperTrait.php in tests/modules/apigee_mock_api_client/tests/src/Traits/ApigeeMockApiClientHelperTrait.php

File

tests/src/Traits/ApigeeEdgeUtilTestTrait.php, line 28

Namespace

Drupal\Tests\apigee_edge\Traits
View source
trait ApigeeEdgeUtilTestTrait {

  /**
   * Creates a test key from environment variables, using config key storage.
   *
   * Using config storage , as opposed to environment vars, has the advantage
   * of the key values persisting in subsequent page requests.
   */
  protected function createTestKey() : void {
    $environment_variables = [];
    $definition = \Drupal::service('plugin.manager.key.key_type')
      ->getDefinition('apigee_auth');
    foreach ($definition['multivalue']['fields'] as $id => $field) {
      $env_var_name = 'APIGEE_EDGE_' . mb_strtoupper($id);
      if (getenv($env_var_name)) {
        $environment_variables[$id] = getenv($env_var_name);
      }
    }
    $key = Key::create([
      'id' => 'test',
      'label' => 'test',
      'key_type' => 'apigee_auth',
      'key_provider' => 'config',
      'key_input' => 'none',
      'key_provider_settings' => [
        'key_value' => json_encode($environment_variables),
      ],
    ]);
    try {
      $key
        ->save();
    } catch (EntityStorageException $exception) {
      $this
        ->fail('Could not create key for testing.');
    }
  }

  /**
   * Restores the active key.
   */
  protected function restoreKey() {
    $test_key_id = 'test';
    $this
      ->config('apigee_edge.auth')
      ->set('active_key', $test_key_id)
      ->save();
  }

  /**
   * Removes the active key for testing with unset API credentials.
   */
  protected function invalidateKey() {
    $this
      ->config('apigee_edge.auth')
      ->set('active_key', '')
      ->save();
  }

  /**
   * Set active authentication keys in config.
   *
   * @param string $active_key
   *   The active authentication key.
   */
  protected function setKey(string $active_key) {
    $this
      ->config('apigee_edge.auth')
      ->set('active_key', $active_key)
      ->save();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ApigeeEdgeUtilTestTrait::createTestKey protected function Creates a test key from environment variables, using config key storage.
ApigeeEdgeUtilTestTrait::invalidateKey protected function Removes the active key for testing with unset API credentials.
ApigeeEdgeUtilTestTrait::restoreKey protected function Restores the active key.
ApigeeEdgeUtilTestTrait::setKey protected function Set active authentication keys in config.