You are here

trait CredsUtilsTrait in Apigee Edge 8

A trait to common functions of Apigee Edge credential entity tests.

Hierarchy

2 files declare their use of CredsUtilsTrait
AppListBuilderTest.php in tests/src/Kernel/Entity/ListBuilder/AppListBuilderTest.php
AppWarningsCheckerTest.php in tests/src/Kernel/Entity/AppWarningsCheckerTest.php

File

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

Namespace

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

  /**
   * API product to test.
   *
   * @var \Drupal\apigee_edge\Entity\ApiProductInterface
   */
  protected $apiProduct;

  /**
   * Returns the developer app credential controller.
   *
   * @param string $owner
   *   The developer id (UUID), email address or team (company) name.
   * @param string $app_name
   *   The name of an app.
   *
   * @return \Drupal\apigee_edge\Entity\Controller\AppCredentialControllerInterface
   *   The app credential controller.
   */
  protected function getAppCredentialController(string $owner, string $app_name) : AppCredentialControllerInterface {
    return \Drupal::service('apigee_edge.controller.developer_app_credential_factory')
      ->developerAppCredentialController($owner, $app_name);
  }

  /**
   * Perform an operation on the given credential (by index) of the app.
   *
   * @param \Drupal\apigee_edge\Entity\DeveloperAppInterface $app
   *   The app.
   * @param string $op
   *   The operation to perform (revoke,  delete, generate).
   * @param int $cred_index
   *   The index of the credential (only applies to revoke/delete operations).
   * @param int $expires_in
   *   The milliseconds from now that the cred should expire (only applies for
   *   generate operation). Defaults to "-1" (never).
   */
  protected function operationOnCredential(DeveloperAppInterface $app, $op = 'revoke', $cred_index = 0, $expires_in = -1) {
    $controller = $this
      ->getAppCredentialController($app
      ->getAppOwner(), $app
      ->getName());
    if ($op == 'generate') {
      $controller
        ->generate([
        $this->apiProduct
          ->id(),
      ], $app
        ->getAttributes(), '', [], $expires_in);
      return;
    }
    $key = $app
      ->getCredentials()[$cred_index]
      ->getConsumerKey();
    if ($op == 'revoke') {
      $controller
        ->setStatus($key, AppCredentialControllerInterface::STATUS_REVOKE);
    }
    elseif ($op == 'delete') {
      $controller
        ->delete($key);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CredsUtilsTrait::$apiProduct protected property API product to test.
CredsUtilsTrait::getAppCredentialController protected function Returns the developer app credential controller.
CredsUtilsTrait::operationOnCredential protected function Perform an operation on the given credential (by index) of the app.