cloud-8.x-2.0-beta1/modules/cloud_service_providers/aws_cloud/tests/src/Functional/Config/AwsCloudConfigTest.php

modules/cloud_service_providers/aws_cloud/tests/src/Functional/Config/AwsCloudConfigTest.php
<?php

namespace Drupal\Tests\aws_cloud\Functional\Config;

use Drupal\Tests\BrowserTestBase;
use Drupal\Component\Utility\Random;
use Drupal\Tests\aws_cloud\Traits\AwsCloudTestEntityTrait;
use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;
use Drupal\Tests\aws_cloud\Traits\AwsCloudTestFormDataTrait;

/**
 * Tests cloud service provider (CloudConfig).
 *
 * @group AWS Cloud
 */
class AwsCloudConfigTest extends BrowserTestBase {

  use AwsCloudTestEntityTrait;
  use AwsCloudTestFormDataTrait;

  const AWS_CLOUD_CONFIG_REPEAT_COUNT = 1;

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = [
    'cloud',
    'aws_cloud',
  ];

  /**
   * The profile to install as a basis for testing.
   *
   * @var string
   */
  protected $profile = 'minimal';

  /**
   * The random string.
   *
   * @var string
   */
  protected $random;

  /**
   * Set up test.
   */
  protected function setUp() {

    parent::setUp();

    if (!$this->random) {
      $this->random = new Random();
    }

    $config = \Drupal::configFactory()->getEditable('aws_cloud.settings');
    $config->set('aws_cloud_test_mode', TRUE)
      ->save();

    $this->initMockInstanceTypes();

    $web_user = $this->drupalCreateUser([
      'administer cloud service providers',
      'add cloud service providers',
      'edit cloud service providers',
      'edit own cloud service providers',
      'delete cloud service providers',
      'delete own cloud service providers',
      'view unpublished cloud service providers',
      'view own unpublished cloud service providers',
      'view published cloud service providers',
      'view own published cloud service providers',
      'access dashboard',
      'view cloud service provider admin list',
      'list cloud server template',
      'administer aws_cloud',
    ]);

    $this->drupalLogin($web_user);
  }

  /**
   * Tests CRUD for cloud service provider (CloudConfig) information.
   */
  public function testCloudConfig() {

    // Get all regions.
    $regions = \Drupal::service('aws_cloud.ec2')->getRegions();

    // List AWS Cloud for Amazon EC2.
    $this->drupalGet('/admin/structure/cloud_config');
    $this->assertResponse(200, t('HTTP 200: List | AWS Cloud'));
    $this->assertNoText(t('Notice'), t('Make sure w/o Notice'));
    $this->assertNoText(t('Warning'), t('Make sure w/o Warnings'));

    // Add a new Config information.
    $add = $this->createCloudConfigTestFormData();
    for ($i = 0; $i < self::AWS_CLOUD_CONFIG_REPEAT_COUNT; $i++) {

      // We need to specify the 'view <CLOUD_CONTEXT>' permission.
      $labels = [];
      foreach ($regions as $region_name => $region_display_name) {
        $labels[] = "{$add[$i]['name[0][value]']} {$region_display_name}";
        $this->grantPermissions(
          Role::load(
            RoleInterface::AUTHENTICATED_ID),
            [
              'view ' . aws_cloud_form_cloud_config_aws_ec2_add_form_create_cloud_context(
                $add[$i]['name[0][value]'],
                $region_name),
            ]
        );
      }

      // These variable that are sent from a browser are specified in the form
      // in the browser, therefore we don't use the auto-generated test values
      // by createCloudConfigTestFormData.
      unset($add[$i]['field_region']);
      unset($add[$i]['field_api_endpoint_uri']);
      unset($add[$i]['field_image_upload_url']);

      $this->drupalGet('/admin/structure/cloud_config/add');
      $this->assertResponse(200, t('HTTP 200: Add | AWS Cloud cloud service provider (CloudConfig) form'));
      $this->assertNoText(t('Notice'), t('Make sure w/o Notice'));
      $this->assertNoText(t('Warning'), t('Make sure w/o Warnings'));

      $this->drupalPostForm('/admin/structure/cloud_config/add',
        $add[$i],
        t('Save'));

      $this->assertResponse(200, t('HTTP 200: Saved | AWS cloud service provider form'));
      $this->assertNoText(t('Notice'), t('Make sure w/o Notice'));
      $this->assertNoText(t('Warning'), t('Make sure w/o Warnings'));
      $this->assertText(t('Creating cloud service provider was performed successfully.'), t('Create cloud service provider'));

      foreach ($labels as $label) {
        $this->assertText($label, t('The cloud service provider (CloudConfig) name: @label', ['@label' => $label]));
      }

      // Make sure listing for '/admin/structure/cloud_config'.
      $this->drupalGet('/admin/structure/cloud_config');
      $this->assertResponse(200, t('HTTP 200: List | AWS Cloud'));
      $this->assertNoText(t('Notice'), t('Make sure w/o Notice'));
      $this->assertNoText(t('Warning'), t('Make sure w/o Warnings'));
      foreach ($labels as $label) {
        $this->assertLink($label);
      }

      // Make sure listing for '/clouds'.
      $this->drupalGet('/clouds');
      $this->assertResponse(200, t('HTTP 200: List | AWS Cloud'));
      $this->assertNoText(t('Notice'), t('Make sure w/o Notice'));
      $this->assertNoText(t('Warning'), t('Make sure w/o Warnings'));
      foreach ($labels as $label) {
        $this->assertLink($label);
      }

      // Make sure listing for '/admin/config/services/cloud/aws_cloud'.
      $this->drupalGet('/admin/config/services/cloud/aws_cloud');
      $this->assertResponse(200, t('HTTP 200: List | AWS Cloud'));
      $this->assertNoText(t('Notice'), t('Make sure w/o Notice'));
      $this->assertNoText(t('Warning'), t('Make sure w/o Warnings'));
      foreach ($labels as $label) {
        $this->assertLink($label);
      }
    }

    // Edit Config case.
    $labels = [];
    $edit = $this->createCloudConfigTestFormData();
    $cloud_config_prefix = $edit[0]['name[0][value]'];
    for ($i = 0; $i < self::AWS_CLOUD_CONFIG_REPEAT_COUNT; $i++) {

      $num = 1;
      foreach ($regions as $region_name => $region_display_name) {

        $label = "${cloud_config_prefix} ${region_display_name}";
        $edit[$i]['name[0][value]'] = $label;
        $labels[] = $label;

        unset($edit[$i]['regions[us-east-1]']);

        $this->drupalPostForm("/admin/structure/cloud_config/${num}/edit",
          $edit[$i],
          t('Save'));
        $this->assertResponse(200, t('HTTP 200: Edit | AWS Cloud Form'));
        $this->assertNoText(t('Notice'), t('Make sure w/o Notice'));
        $this->assertNoText(t('Warning'), t('Make sure w/o Warnings'));

        $this->assertText($label, t('The cloud service provider (CloudConfig) name: @label', ['@label' => $label]));

        // Make sure listing for '/admin/structure/cloud_config'.
        $this->drupalGet('/admin/structure/cloud_config');
        $this->assertResponse(200, t('HTTP 200: List | AWS Cloud'));
        $this->assertNoText(t('Notice'), t('Make sure w/o Notice'));
        $this->assertNoText(t('Warning'), t('Make sure w/o Warnings'));
        foreach ($labels as $label) {
          $this->assertLink($label);
        }

        // Make sure listing for '/clouds'.
        $this->drupalGet('/clouds');
        $this->assertResponse(200, t('HTTP 200: List | AWS Cloud'));
        $this->assertNoText(t('Notice'), t('Make sure w/o Notice'));
        $this->assertNoText(t('Warning'), t('Make sure w/o Warnings'));
        foreach ($labels as $label) {
          $this->assertLink($label);
        }

        // Make sure listing for '/admin/config/services/cloud/aws_cloud'.
        $this->drupalGet('/admin/config/services/cloud/aws_cloud');
        $this->assertResponse(200, t('HTTP 200: List | AWS Cloud'));
        $this->assertNoText(t('Notice'), t('Make sure w/o Notice'));
        $this->assertNoText(t('Warning'), t('Make sure w/o Warnings'));
        foreach ($labels as $label) {
          $this->assertLink($label);
        }

        $num++;
      }
    }

    // Delete Config Items.
    for ($i = 0; $i < self::AWS_CLOUD_CONFIG_REPEAT_COUNT; $i++) {

      for ($j = 0, $num = 1; $j < count($regions); $j++, $num++) {

        $this->drupalGet("/admin/structure/cloud_config/${num}/delete");
        $this->drupalPostForm("/admin/structure/cloud_config/${num}/delete",
          [],
          t('Delete'));
        $this->assertResponse(200, t('HTTP 200: Delete | AWS Cloud'));
        $this->assertNoText(t('Notice'), t('Make sure w/o Notice'));
        $this->assertNoText(t('Warning'), t('Make sure w/o Warnings'));
        $this->assertText(t('The cloud service provider @label has been deleted.', ['@label' => $labels[$j]]),
          t('Deleted Message:') . ' '
          . t('The cloud service provider @label has been deleted.', ['@label' => $labels[$j]]));

        // Because $cloud_context has been deleted.
        // Make sure listing.
        for ($k = 0; $k < $num; $k++) {

          // Make sure listing for '/admin/structure/cloud_config'.
          $this->drupalGet('/admin/structure/cloud_config');
          $this->assertResponse(200, t('HTTP 200: List | AWS Cloud'));
          $this->assertNoText(t('Notice'), t('Make sure w/o Notice'));
          $this->assertNoText(t('Warning'), t('Make sure w/o Warnings'));
          $this->assertNoLink($labels[$k],
            t('The cloud service provider (CloudConfig) name @num: @label', ['@label' => $labels[$k]])
          );

          // Make sure listing for '/clouds'.
          $this->drupalGet('/clouds');
          $this->assertResponse(200, t('HTTP 200: List | AWS Cloud'));
          $this->assertNoText(t('Notice'), t('Make sure w/o Notice'));
          $this->assertNoText(t('Warning'), t('Make sure w/o Warnings'));
          $this->assertNoLink($labels[$k]);

          // Make sure listing for '/admin/config/services/cloud/aws_cloud'.
          $this->drupalGet('/admin/config/services/cloud/aws_cloud');
          $this->assertResponse(200, t('HTTP 200: List | AWS Cloud'));
          $this->assertNoText(t('Notice'), t('Make sure w/o Notice'));
          $this->assertNoText(t('Warning'), t('Make sure w/o Warnings'));
          $this->assertNoLink($labels[$k]);
        }
      }
    }
  }

  /**
   * Tests deleting cloud service provider (CloudConfig) with bulk operation.
   */
  public function testCloudConfigBulk() {

    for ($i = 0; $i < self::AWS_CLOUD_CONFIG_REPEAT_COUNT; $i++) {
      // Create cloud service providers.
      $cloud_configs = $this->createCloudConfigRandomTestFormData();
      $index = 0;
      $entities = [];
      foreach ($cloud_configs as $cloud_config) {
        $entity = $this->createCloudConfigTestEntity($index++, $cloud_config['CloudContext'], $cloud_config['Name']);

        $this->grantPermissions(
          Role::load(
            RoleInterface::AUTHENTICATED_ID),
          ["view {$entity->getCloudContext()}"]
        );
        $entities[] = $entity;
      }

      $this->drupalGet('/admin/structure/cloud_config');
      $this->doTestEntityBulk(
        'cloud_config',
        $entities,
        'delete',
        'deleted',
        '/admin/structure'
      );

      $this->drupalGet('/admin/structure/cloud_config');
      foreach ($cloud_configs as $cloud_config) {
        $this->assertNoLink($cloud_config['Name']);
      }

      $this->drupalGet('/clouds');
      foreach ($cloud_configs as $cloud_config) {
        $this->assertNoLink($cloud_config['Name']);
      }
    }
  }

  /**
   * Tests Redirect for cloud service provider (CloudConfig) information.
   */
  public function testCloudConfigRedirect() {
    $this->repeatTestCloudConfigRedirect(self::AWS_CLOUD_CONFIG_REPEAT_COUNT);
  }

  /**
   * Repeating test cloud service provider redirect.
   *
   * @param int $max_count
   *   Max test repeating count.
   */
  private function repeatTestCloudConfigRedirect($max_count) {
    $paths = [
      '/clouds',
      '/clouds/design',
    ];

    foreach ($paths as $path) {
      for ($i = 0; $i < $max_count; $i++) {
        $this->drupalGet($path);
        $this->assertResponse(200, t('HTTP 200: clouds | AWS Cloud'));
        $this->assertNoText(t('Notice'), t('Make sure w/o Notice'));
        $this->assertNoText(t('Warning'), t('Make sure w/o Warnings'));

        $this->assertText(t('Add cloud service provider'), t('Add cloud service provider'));
        $this->assertText(
          t('There is no cloud service provider. Please create a new one.'),
          t('Guid message')
        );
      }
    }
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc