You are here

protected function DeveloperAppUITestTrait::assertAppCreationWithProduct in Apigee Edge 8

Creates an app and assigns products to it.

Parameters

\Drupal\apigee_edge\Entity\ApiProductInterface[] $products: API products associated with the developer app.

bool $multiple: Allow submitting multiple products.

bool $display_as_select: Display the products as a select box.

1 call to DeveloperAppUITestTrait::assertAppCreationWithProduct()
DeveloperAppUITest::testCreateAppWithProducts in tests/src/Functional/DeveloperAppUITest.php
Tests app creation with products.

File

tests/src/Functional/DeveloperAppUITestTrait.php, line 287

Class

DeveloperAppUITestTrait
Contains re-usable components for developer app UI tests.

Namespace

Drupal\Tests\apigee_edge\Functional

Code

protected function assertAppCreationWithProduct(array $products = [], bool $multiple = TRUE, bool $display_as_select = FALSE) {
  $this
    ->submitAdminForm([
    'multiple_products' => $multiple,
    'display_as_select' => $display_as_select,
  ]);
  $name = strtolower($this
    ->randomMachineName());
  $productnum = count($products);
  $formdata = [
    'name' => $name,
    'displayName[0][value]' => $name,
  ];
  if (count($products) === 1) {
    $formdata['api_products'] = reset($products)
      ->getName();
  }
  elseif (count($products) > 1) {
    foreach ($products as $product) {
      $formdata["api_products[{$product->getName()}]"] = $product
        ->getName();
    }
  }
  $this
    ->postCreateAppForm($formdata);
  $app = $this
    ->assertDeveloperAppExists($name);
  if ($app) {

    /** @var \Apigee\Edge\Api\Management\Entity\AppCredential[] $credentials */
    $credentials = $app
      ->getCredentials();
    $this
      ->assertEquals(1, count($credentials), 'Exactly one credential exists.');
    $credential = reset($credentials);
    $apiproducts = $credential
      ->getApiProducts();
    $this
      ->assertEquals($productnum, count($apiproducts), "Exactly {$productnum} product is added.");
    $expected_products = array_map(function (ApiProduct $apiProduct) : string {
      return $apiProduct
        ->getName();
    }, $products);
    $retrieved_products = array_map(function (CredentialProduct $credentialProduct) : string {
      return $credentialProduct
        ->getApiproduct();
    }, $apiproducts);
    sort($expected_products);
    sort($retrieved_products);
    $this
      ->assertEquals($expected_products, $retrieved_products);
    $app
      ->delete();
  }
}