Skip to content
Snippets Groups Projects
Commit 42db4679 authored by Yutong Li's avatar Yutong Li Committed by Yas Naoi
Browse files

Issue #3232932 by yutong.li, yas: Refactor test cases for AWS Cloud Admin Setting forms

parent 4eb7e94f
No related branches found
No related tags found
No related merge requests found
......@@ -31,4 +31,71 @@ trait AwsCloudFormTrait {
return '[A-Za-z0-9' . preg_quote(self::$awsCloudSecurityGroupDescriptionSpecialCharacters, NULL) . ']*';
}
/**
* Get the AWS Cloud service providers as an array.
*
* @return array
* An array of AWS Cloud service providers.
*/
protected function getCloudServiceProviderOptions(): array {
$entities = \Drupal::service('plugin.manager.cloud_config_plugin')->loadConfigEntities('aws_cloud');
$cloud_configs = [];
foreach ($entities ?: [] as $entity) {
$cloud_configs[] = $entity->getCloudContext();
}
return $cloud_configs;
}
/**
* Get all other criteria options on AWS Cloud Notifications.
*
* @return array
* Array of criteria.
*/
protected function getCriteriaOptions(): array {
return [30, 60, 90, 180, 365];
}
/**
* Get the frequency options on AWS Cloud Notifications.
*
* @return array
* Array of frequency.
*/
protected function getFrequencyOptions(): array {
return [86400, 604800, 2592000, 5184000, 7776000, 15552000, 31104000];
}
/**
* Get the items per page on Pager options, Views, AWS Cloud Settings.
*
* @return array
* Array of item per page.
*/
protected function getItemsPerPageOptions(): array {
return [10, 15, 20, 25, 50, 100];
}
/**
* Get the notification criteria options on AWS Cloud Notifications.
*
* @return array
* Array of notification criteria.
*/
protected function getNotificationCriteriaOptions(): array {
return [1, 30, 60, 90, 180, 365];
}
/**
* Get all period options on AWS Cloud Notifications.
*
* @return array
* Array of period.
*/
protected function getPeriodOptions(): array {
return ['4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14'];
}
}
......@@ -3,6 +3,7 @@
namespace Drupal\Tests\aws_cloud\Functional\Config;
use Drupal\Tests\aws_cloud\Functional\AwsCloudTestBase;
use Drupal\Tests\aws_cloud\Traits\AwsCloudTestFormDataTrait;
/**
* Test Case class for AWS Cloud Admin Setting forms.
......@@ -11,6 +12,8 @@ use Drupal\Tests\aws_cloud\Functional\AwsCloudTestBase;
*/
class AwsCloudAdminSettings extends AwsCloudTestBase {
use AwsCloudTestFormDataTrait;
public const AWS_CLOUD_ADMIN_SETTINGS_REPEAT_COUNT = 2;
/**
......@@ -31,70 +34,63 @@ class AwsCloudAdminSettings extends AwsCloudTestBase {
*
* @param string $setting_type
* The setting type id.
* @param array $edit
* The array of input data.
*
* @throws \Behat\Mink\Exception\ResponseTextException
*/
protected function runAwsCloudAdminSettings($setting_type): void {
for ($i = 0; $i < self::AWS_CLOUD_ADMIN_SETTINGS_REPEAT_COUNT; $i++) {
$this->drupalGet("/admin/config/services/cloud/aws_cloud/{$setting_type}");
protected function runAwsCloudAdminSettings($setting_type, array $edit): void {
$this->drupalGet("/admin/config/services/cloud/aws_cloud/{$setting_type}");
$this->assertNoErrorMessage();
// Asserting no warning or error message.
$this->assertNoErrorMessage();
}
$this->submitForm(
$edit,
$this->t('Save')
);
$this->assertNoErrorMessage();
}
/**
* Test AWS Cloud Admin Setting forms on location.
* Test AWS Cloud Admin Setting forms on Locations.
*/
public function testAwsCloudAdminLocationSettings(): void {
$this->runAwsCloudAdminSettings('location');
public function testAwsCloudAdminLocationsSettings(): void {
$edit = $this->createAwsCloudLocationsFormData(self::AWS_CLOUD_ADMIN_SETTINGS_REPEAT_COUNT);
// Input some value of the fields and save.
$config_factory = \Drupal::configFactory();
$config = $config_factory->getEditable('aws_cloud.settings');
$config->set('aws_cloud_region_locations',
$config->get(strtolower('aws_cloud_region_locations'))
);
$config->save();
$this->runAwsCloudAdminSettings('location');
for ($i = 0; $i < self::AWS_CLOUD_ADMIN_SETTINGS_REPEAT_COUNT; $i++) {
$edit[$i] = array_intersect_key($edit[$i], array_flip(
(array) array_rand($edit[$i], random_int(1, count($edit[$i])))
));
$this->runAwsCloudAdminSettings('location', $edit[$i]);
}
}
/**
* Test AWS Cloud Admin Setting forms on notification.
* Test AWS Cloud Admin Setting forms on Notifications.
*/
public function testAwsCloudAdminNotificationSettings(): void {
$this->runAwsCloudAdminSettings('notification');
public function testAwsCloudAdminNotificationsSettings(): void {
$edit = $this->createAwsCloudNotificationsFormData(self::AWS_CLOUD_ADMIN_SETTINGS_REPEAT_COUNT);
// Input some value of the fields and save.
$config_factory = \Drupal::configFactory();
$config = $config_factory->getEditable('aws_cloud.settings');
$config->set('aws_cloud_long_running_instance_notification',
!$config->get('aws_cloud_long_running_instance_notification')
);
$config->save();
$this->runAwsCloudAdminSettings('notification');
for ($i = 0; $i < self::AWS_CLOUD_ADMIN_SETTINGS_REPEAT_COUNT; $i++) {
$edit[$i] = array_intersect_key($edit[$i], array_flip(
(array) array_rand($edit[$i], random_int(1, count($edit[$i])))
));
$this->runAwsCloudAdminSettings('notification', $edit[$i]);
}
}
/**
* Test AWS Cloud Admin Setting forms on settings.
* Test AWS Cloud Admin Setting forms on Settings.
*/
public function testAwsCloudAdminSettings(): void {
$this->runAwsCloudAdminSettings('settings');
$edit = $this->createAwsCloudSettingsFormData(self::AWS_CLOUD_ADMIN_SETTINGS_REPEAT_COUNT);
// Input some value of the fields and save.
$config_factory = \Drupal::configFactory();
$config = $config_factory->getEditable('aws_cloud.settings');
$config->set('aws_cloud_instance_terminate',
!$config->get('aws_cloud_instance_terminate')
);
$config->save();
$this->runAwsCloudAdminSettings('settings');
for ($i = 0; $i < self::AWS_CLOUD_ADMIN_SETTINGS_REPEAT_COUNT; $i++) {
$edit[$i] = array_intersect_key($edit[$i], array_flip(
(array) array_rand($edit[$i], random_int(1, count($edit[$i])))
));
$this->runAwsCloudAdminSettings('settings', $edit[$i]);
}
}
}
......@@ -8,6 +8,7 @@ use Drupal\cloud\Plugin\Field\FieldType\KeyValue;
use Drupal\Component\Utility\Random;
use Drupal\Tests\cloud\Functional\Utils;
use Drupal\cloud\Entity\CloudLaunchTemplateInterface;
use Drupal\Core\Datetime\DateHelper;
/**
* The trait creating form data for aws cloud testing.
......@@ -1527,4 +1528,152 @@ trait AwsCloudTestFormDataTrait {
return $tags;
}
/**
* Create an array of random input data for Locations.
*
* @return array
* The array including random input data.
*/
public function createAwsCloudLocationsFormData($repeat_count = 1): array {
$regions = \Drupal::service('aws_cloud.ec2')->getRegions();
$edit = [];
for ($i = 0; $i < $repeat_count; $i++) {
foreach (array_keys($regions) ?: [] as $region) {
$latitude = rand(-90, 90);
$longitude = rand(-180, 180);
array_push($edit,
["aws_cloud_region_locations[$region][city]" => $this->random->name(128, TRUE)],
["aws_cloud_region_locations[$region][latitude]" => $latitude],
["aws_cloud_region_locations[$region][longitude]" => $longitude],
);
}
}
return $edit;
}
/**
* Create an array of random input data for Notifications.
*
* @return array
* The array including random input data.
*/
public function createAwsCloudNotificationsFormData($repeat_count = 1): array {
$config_factory = \Drupal::configFactory();
$config = $config_factory->getEditable('aws_cloud.settings');
$frequency_options = $this->getFrequencyOptions();
$notification_criteria_options = $this->getNotificationCriteriaOptions();
$hour_options = DateHelper::hours();
$minute_options = DateHelper::minutes();
$period_options = $this->getPeriodOptions();
$criteria_options = $this->getCriteriaOptions();
$edit = [];
for ($i = 0; $i < $repeat_count; $i++) {
$edit = [
['aws_cloud_notification_frequency' => $frequency_options[array_rand($frequency_options)]],
['aws_cloud_instance_notification_hour' => $hour_options[array_rand($hour_options)]],
['aws_cloud_instance_notification_minutes' => $minute_options[array_rand($minute_options)]],
['aws_cloud_long_running_instance_notification' => !$config->get('aws_cloud_long_running_instance_notification')],
['aws_cloud_long_running_instance_notify_owner' => !$config->get('aws_cloud_long_running_instance_notify_owner')],
['aws_cloud_long_running_instance_notification_criteria' => $notification_criteria_options[array_rand($notification_criteria_options)]],
['aws_cloud_long_running_instance_notification_emails' => $this->random->name(10, TRUE)],
['aws_cloud_long_running_instance_notification_subject' => $this->random->name(10, TRUE)],
['aws_cloud_long_running_instance_notification_msg' => $this->random->name(10, TRUE)],
['aws_cloud_long_running_instance_notification_instance_info' => $this->random->name(10, TRUE)],
['aws_cloud_low_utilization_instance_notification' => !$config->get('aws_cloud_low_utilization_instance_notification')],
['aws_cloud_low_utilization_instance_notify_owner' => !$config->get('aws_cloud_low_utilization_instance_notify_owner')],
['aws_cloud_low_utilization_instance_cpu_utilization_threshold' => rand(1, 100)],
['aws_cloud_low_utilization_instance_network_io_threshold' => rand(1, 1024)],
['aws_cloud_low_utilization_instance_period' => $period_options[array_rand($period_options)]],
['aws_cloud_low_utilization_instance_notification_emails' => $this->random->name(10, TRUE)],
['aws_cloud_low_utilization_instance_notification_subject' => $this->random->name(10, TRUE)],
['aws_cloud_low_utilization_instance_notification_msg' => $this->random->name(10, TRUE)],
['aws_cloud_low_utilization_instance_notification_instance_info' => $this->random->name(10, TRUE)],
['aws_cloud_volume_notification' => !$config->get('aws_cloud_volume_notification')],
['aws_cloud_volume_notify_owner' => !$config->get('aws_cloud_volume_notify_owner')],
['aws_cloud_volume_notification_frequency' => $frequency_options[array_rand($frequency_options)]],
['aws_cloud_unused_volume_criteria' => $criteria_options[array_rand($criteria_options)]],
['aws_cloud_volume_notification_hour' => $hour_options[array_rand($hour_options)]],
['aws_cloud_volume_notification_minutes' => $minute_options[array_rand($minute_options)]],
['aws_cloud_volume_notification_emails' => $this->random->name(10, TRUE)],
['aws_cloud_volume_notification_subject' => $this->random->name(10, TRUE)],
['aws_cloud_volume_notification_msg' => $this->random->name(10, TRUE)],
['aws_cloud_volume_notification_volume_info' => $this->random->name(10, TRUE)],
['aws_cloud_snapshot_notification' => !$config->get('aws_cloud_snapshot_notification')],
['aws_cloud_snapshot_notify_owner' => !$config->get('aws_cloud_snapshot_notify_owner')],
['aws_cloud_snapshot_notification_frequency' => $frequency_options[array_rand($frequency_options)]],
['aws_cloud_stale_snapshot_criteria' => $criteria_options[array_rand($criteria_options)]],
['aws_cloud_snapshot_notification_hour' => $hour_options[array_rand($hour_options)]],
['aws_cloud_snapshot_notification_minutes' => $minute_options[array_rand($minute_options)]],
['aws_cloud_snapshot_notification_emails' => $this->random->name(10, TRUE)],
['aws_cloud_snapshot_notification_subject' => $this->random->name(10, TRUE)],
['aws_cloud_snapshot_notification_msg' => $this->random->name(10, TRUE)],
['aws_cloud_snapshot_notification_snapshot_info' => $this->random->name(10, TRUE)],
['aws_cloud_elastic_ip_notification' => !$config->get('aws_cloud_elastic_ip_notification')],
['aws_cloud_elastic_ip_notify_owner' => !$config->get('aws_cloud_elastic_ip_notify_owner')],
['aws_cloud_elastic_ip_notification_frequency' => $frequency_options[array_rand($frequency_options)]],
['aws_cloud_elastic_ip_notification_hour' => $hour_options[array_rand($hour_options)]],
['aws_cloud_elastic_ip_notification_minutes' => $minute_options[array_rand($minute_options)]],
['aws_cloud_elastic_ip_notification_emails' => $this->random->name(10, TRUE)],
['aws_cloud_elastic_ip_notification_subject' => $this->random->name(10, TRUE)],
['aws_cloud_elastic_ip_notification_msg' => $this->random->name(10, TRUE)],
['aws_cloud_elastic_ip_notification_elastic_ip_info' => $this->random->name(10, TRUE)],
['aws_cloud_launch_template_notification_request_emails' => $this->random->name(10, TRUE)],
['aws_cloud_launch_template_notification_request_subject' => $this->random->name(10, TRUE)],
['aws_cloud_launch_template_notification_request_msg' => $this->random->name(10, TRUE)],
['aws_cloud_launch_template_notification_launch_template_request_info' => $this->random->name(10, TRUE)],
['aws_cloud_launch_template_notification_approved_subject' => $this->random->name(10, TRUE)],
['aws_cloud_launch_template_notification_approved_msg' => $this->random->name(10, TRUE)],
['aws_cloud_launch_template_notification_restore_subject' => $this->random->name(10, TRUE)],
['aws_cloud_launch_template_notification_restore_msg' => $this->random->name(10, TRUE)],
['aws_cloud_launch_template_notification_launch_template_info' => $this->random->name(10, TRUE)],
];
}
return $edit;
}
/**
* Create an array of random input data for Settings.
*
* @return array
* The array including random input data.
*/
public function createAwsCloudSettingsFormData($repeat_count = 1): array {
$config_factory = \Drupal::configFactory();
$config = $config_factory->getEditable('aws_cloud.settings');
$items_per_page = $this->getItemsPerPageOptions();
$cloud_service_providers = $this->getCloudServiceProviderOptions();
$edit = [];
for ($i = 0; $i < $repeat_count; $i++) {
$edit = [
['aws_cloud_test_mode' => !$config->get('aws_cloud_test_mode')],
['aws_cloud_view_refresh_interval' => rand(1, 9999)],
['aws_cloud_view_expose_items_per_page' => !$config->get('aws_cloud_view_expose_items_per_page')],
['aws_cloud_view_items_per_page' => $items_per_page[array_rand($items_per_page)]],
['aws_cloud_instance_terminate' => !$config->get('aws_cloud_instance_terminate')],
['aws_cloud_scheduler_tag' => $this->random->name(10, TRUE)],
['aws_cloud_scheduler_periods' => $this->random->name(10, TRUE)],
['aws_cloud_instance_type_prices' => !$config->get('aws_cloud_instance_type_prices')],
['aws_cloud_instance_type_prices_spreadsheet' => !$config->get('aws_cloud_instance_type_prices_spreadsheet')],
['aws_cloud_instance_type_cost' => !$config->get('aws_cloud_instance_type_cost')],
['aws_cloud_instance_type_cost_list' => !$config->get('aws_cloud_instance_type_cost_list')],
['aws_cloud_instance_list_cost_column' => !$config->get('aws_cloud_instance_list_cost_column')],
['aws_cloud_ec2_pricing_endpoint' => $this->random->name(10, TRUE)],
['aws_cloud_service_provider' => $cloud_service_providers[array_rand($cloud_service_providers)]],
['aws_cloud_price_rate_ec2' => rand(1, 100)],
['aws_cloud_monitor_refresh_interval' => rand(1, 9999)],
['aws_cloud_update_resources_queue_cron_time' => rand(1, 9999)],
['aws_cloud_queue_limit' => rand(1, 50)],
];
}
return $edit;
}
}
......@@ -7,8 +7,6 @@ namespace Drupal\cloud\Traits;
*/
trait CloudResourceTrait {
use CloudContentEntityTrait;
/**
* Get a count of all aws resources.
*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment