cloud-8.x-2.0-beta1/modules/cloud_service_providers/aws_cloud/tests/src/Functional/AwsCloudTestCase.php
modules/cloud_service_providers/aws_cloud/tests/src/Functional/AwsCloudTestCase.php
<?php
namespace Drupal\Tests\aws_cloud\Functional;
use Drupal\Component\Serialization\Yaml;
use Drupal\Component\Utility\Random;
use Drupal\Tests\aws_cloud\Traits\AwsCloudTestEntityTrait;
use Drupal\Tests\aws_cloud\Traits\AwsCloudTestFormDataTrait;
use Drupal\Tests\aws_cloud\Traits\AwsCloudTestMockTrait;
use Drupal\Tests\BrowserTestBase;
use Drupal\cloud\Entity\CloudConfig;
use Drupal\aws_cloud\Service\Pricing\GoogleSpreadsheetService;
/**
* Base Test Case class for AWS cloud.
*/
abstract class AwsCloudTestCase extends BrowserTestBase {
use AwsCloudTestEntityTrait;
use AwsCloudTestFormDataTrait;
use AwsCloudTestMockTrait;
/**
* 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 object.
*
* @var string
*/
protected $random;
/**
* The cloud context.
*
* @var string
*/
protected $cloudContext;
/**
* The region.
*
* @var string
*/
protected $cloudRegion;
/**
* The latest template variables.
*
* @var array
*/
protected $latestTemplateVars;
/**
* 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();
$this->initMockGoogleSpreadsheetService();
$this->createCloudContext();
$this->initMockData();
$perms = $this->getPermissions();
$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('aws_cloud.settings');
return json_decode($config->get('aws_cloud_mock_data'), TRUE);
}
/**
* Update mock data in configuration.
*/
protected function updateMockDataToConfig($mock_data) {
$config = \Drupal::configFactory()->getEditable('aws_cloud.settings');
$config->set('aws_cloud_mock_data', json_encode($mock_data))
->save();
}
/**
* Create cloud context.
*
* @param string $bundle_type
* The cloud service provide bundle type ('aws_ec2').
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
private function createCloudContext($bundle_type = 'aws_ec2') {
$random = $this->random;
$this->cloudContext = $random->name(8);
$num = 1;
$cloud = CloudConfig::create([
'label' => "Amazon EC2 US West ($num) - {$random->name(8, TRUE)}",
'cloud_context' => $this->cloudContext,
'type' => $bundle_type,
'field_description' => "#$num: " . date('Y/m/d H:i:s - D M j G:i:s T Y') . $random->string(64, TRUE),
'field_api_version' => 'latest',
'field_region' => "us-west-$num",
'field_api_endpoint_uri' => "https://ec2.us-west-${num}.amazonaws.com",
'field_access_key' => $random->name(20, TRUE),
'field_secret_key' => $random->name(40, TRUE),
'field_account_id' => $random->name(16, TRUE),
'field_image_upload_url' => "https://ec2.us-west-${num}.amazonaws.com",
'field_x_509_certificate' => $random->string(255, TRUE),
'field_automatically_assign_vpc' => '0',
]);
// Set the cloud region so it is available.
$this->cloudRegion = "us-west-$num";
// Save.
$cloud->save();
}
/**
* Mock the GoogleSpreadsheetService.
*
* The mock up will return an spreadsheet URL.
*/
private function initMockGoogleSpreadsheetService() {
$mock_spreadsheet_service = $this
->getMockBuilder(GoogleSpreadsheetService::class)
->disableOriginalConstructor()
->getMock();
$spreadsheet_id = $this->random->name(44, TRUE);
$spreadsheet_url = "https://docs.google.com/spreadsheets/d/${spreadsheet_id}/edit";
$mock_spreadsheet_service->expects($this->any())
->method('createOrUpdate')
->willReturn($spreadsheet_url);
// Provide a mock service container, for the services our module uses.
$container = \Drupal::getContainer();
$container->set('aws_cloud.google_spreadsheet', $mock_spreadsheet_service);
}
/**
* 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('aws_cloud.settings');
$config->set('aws_cloud_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);
}
}
