cloud-8.x-2.0-beta1/modules/cloud_service_providers/k8s/tests/src/Functional/K8sTestCase.php

modules/cloud_service_providers/k8s/tests/src/Functional/K8sTestCase.php
<?php

namespace Drupal\Tests\k8s\Functional;

use Drupal\Component\Serialization\Yaml;
use Drupal\Component\Utility\Random;
use Drupal\Tests\BrowserTestBase;
use Drupal\cloud\Entity\CloudConfig;

use Drupal\Tests\k8s\Traits\K8sTestFormDataTrait;
use Drupal\Tests\k8s\Traits\K8sTestMockTrait;
use Drupal\Tests\k8s\Traits\K8sTestEntityTrait;

/**
 * Base Test Case class for K8s.
 */
abstract class K8sTestCase extends BrowserTestBase {

  use K8sTestFormDataTrait;
  use K8sTestMockTrait;
  use K8sTestEntityTrait;

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

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

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

  /**
   * The cloud context for testing.
   *
   * @var string
   */
  protected $cloudContext;

  /**
   * The mock data of the latest template variables for testing.
   *
   * @var array
   */
  protected $latestTemplateVars;

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

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

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

    $this->createCloudContext();
    $this->initMockData();

    $perms = $this->getPermissions();

    if ($this->cloudContext) {
      $view_perms = ['view all cloud service providers', 'view ' . $this->cloudContext];
      $perms[] = $view_perms[array_rand($view_perms)];
    }

    $web_user = $this->drupalCreateUser($perms);
    $this->drupalLogin($web_user);
  }

  /**
   * Get permissions of login user.
   *
   * @return array
   *   permissions of login user.
   */
  abstract protected function getPermissions();

  /**
   * Get mock data.
   *
   * @return array
   *   mock data.
   */
  protected function getMockData() {
    return [];
  }

  /**
   * Get mock data from configuration.
   *
   * @return array
   *   mock data.
   */
  protected function getMockDataFromConfig() {
    $config = \Drupal::configFactory()->getEditable('k8s.settings');
    return json_decode($config->get('k8s_mock_data'), TRUE);
  }

  /**
   * Update mock data in configuration.
   */
  protected function updateMockDataToConfig($mock_data) {
    $config = \Drupal::configFactory()->getEditable('k8s.settings');
    $config->set('k8s_mock_data', json_encode($mock_data))
      ->save();
  }

  /**
   * Create cloud context.
   *
   * @param string $bundle_type
   *   The CloudConfig Bundle Type ('k8s')
   *
   * @throws \Drupal\Core\Entity\EntityStorageException
   */
  protected function createCloudContext($bundle_type = 'k8s') {
    $random = $this->random;
    $this->cloudContext = $random->name(8);
    $cloud = CloudConfig::create([
      'name'                    => "Kubernetes - " . $random->name(8, TRUE),
      'cloud_context'           => $this->cloudContext,
      'type'                    => $bundle_type,
      'field_api_server'        => "https://www.test-k8s.com",
      'field_token'             => $random->name(128, TRUE),
    ]);

    // Save.
    $cloud->save();
  }

  /**
   * Init mock data.
   */
  private function initMockData() {
    $mock_data = [];
    $this->latestTemplateVars = $this->getMockDataTemplateVars();
    foreach ([__CLASS__, get_class($this)] as $class_name) {
      $content = $this->getMockDataFileContent($class_name, $this->latestTemplateVars);
      if (!empty($content)) {
        $mock_data = array_merge($mock_data, Yaml::decode($content));
      }
    }

    $config = \Drupal::configFactory()->getEditable('k8s.settings');
    $config->set('k8s_mock_data', json_encode($mock_data))
      ->save();
  }

  /**
   * Get the content of mock data file.
   *
   * @return \Drupal\Component\Render\MarkupInterface
   *   The content of mock data file.
   */
  protected function getMockDataFileContent($class_name, $vars, $suffix = '') {
    $prefix = 'Drupal\\Tests\\';
    $module_name_start_pos = strpos($class_name, $prefix) + strlen($prefix);
    $module_name_end_pos = strpos($class_name, '\\', $module_name_start_pos);
    $module_name = substr($class_name, $module_name_start_pos,
      $module_name_end_pos - $module_name_start_pos);

    $path = \Drupal::service('file_system')->realpath(drupal_get_path('module', $module_name)) . '/tests/mock_data';
    $pos = strpos($class_name, $module_name) + strlen($module_name);
    $path .= str_replace('\\', '/', substr($class_name, $pos)) . $suffix . '.yml';

    if (!file_exists($path)) {
      return '';
    }

    $twig = \Drupal::service('twig');
    return $twig->renderInline(file_get_contents($path), $vars);
  }

  /**
   * Get variables in mock data template file.
   *
   * @return array
   *   variables in mock data template file.
   */
  protected function getMockDataTemplateVars() {
    return [];
  }

  /**
   * Reload mock data in configuration.
   */
  protected function reloadMockData() {
    $mock_data = $this->getMockDataFromConfig();
    $this->latestTemplateVars = $this->getMockDataTemplateVars();
    $file_content = $this->getMockDataFileContent(get_class($this), $this->latestTemplateVars);
    if (!empty($file_content)) {
      $mock_data = array_merge($mock_data, Yaml::decode($file_content));
    }
    $this->updateMockDataToConfig($mock_data);
  }

}

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

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