cloud-8.x-2.0-beta1/modules/cloud_service_providers/aws_cloud/tests/src/Functional/Ec2/CloudServerTemplateTest.php
modules/cloud_service_providers/aws_cloud/tests/src/Functional/Ec2/CloudServerTemplateTest.php
<?php
namespace Drupal\Tests\aws_cloud\Functional\Ec2;
use Drupal\Tests\aws_cloud\Functional\AwsCloudTestCase;
/**
* Tests cloud server templates (CloudServerTemplate).
*
* @group AWS Cloud
*/
class CloudServerTemplateTest extends AwsCloudTestCase {
const CLOUD_SERVER_TEMPLATES_REPEAT_COUNT = 3;
/**
* Modules to enable.
*
* @var array
*/
public static $modules = [
'cloud',
'aws_cloud',
];
/**
* {@inheritdoc}
*/
protected function getPermissions() {
return [
'add cloud server templates',
'list cloud server template',
'view any published cloud server templates',
'view any unpublished cloud server templates',
'edit any cloud server templates',
'delete any cloud server templates',
'access cloud server template revisions',
'revert all cloud server template revisions',
'delete all cloud server template revisions',
'add aws cloud image',
'list aws cloud image',
'view any aws cloud image',
'edit any aws cloud image',
'delete any aws cloud image',
];
}
/**
* Tests CRUD for server_template information.
*/
public function testCloudServerTemplate() {
$cloud_context = $this->cloudContext;
$image = $this->createImageTestEntity(0, $image_id = '', $cloud_context);
$image_id = $image->getImageId();
// Make sure if the image entity is created or not.
$this->drupalGet("/clouds/aws_cloud/${cloud_context}/image");
$this->assertResponse(200, t('HTTP 200: Image | The new AWS Cloud Image #@num', ['@num' => 1]));
$this->assertNoText(t('warning'), t('Image | Make sure w/o Warnings'));
$this->assertText($image->getName(), t('Image | Make sure w/ Image Name (TestEntity): @name', ['@name' => $image->getName()]));
$this->assertText($image->getImageId(), t('Image | Make sure w/ Image ID (TestEntity): @image_id', ['@image_id' => $image->getImageId()]));
$this->createSecurityGroupTestEntity(0, '', '', '', $this->cloudContext);
$this->createKeyPairTestEntity(0, '', '', $this->cloudContext);
// List cloud server template for AWS.
$this->drupalGet("/clouds/design/server_template/$cloud_context");
$this->assertResponse(200, t('HTTP 200: List | Cloud server template'));
$this->assertNoText(t('Notice'), t('Make sure w/o Notice'));
$this->assertNoText(t('Warning'), t('Make sure w/o Warnings'));
$vpcs = $this->createVpcsRandomTestFormData();
$subnets = $this->createSubnetsRandomTestFormData();
$this->updateVpcsAndSubnetsMockData($vpcs, $subnets);
// IAM Roles.
$iam_roles = $this->createIamRolesRandomTestFormData();
$this->updateIamRolesMockData($iam_roles);
// Add a new server_template information.
$add = $this->createServerTemplateTestFormData(self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT);
for ($i = 0; $i < self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT; $i++) {
$num = $i + 1;
$add[$i]['field_image_id'] = $image_id;
$vpc_index = array_rand($vpcs);
$add[$i]['field_vpc'] = $vpcs[$vpc_index]['VpcId'];
$vpc_name = $this->getNameFromArray($vpcs, $vpc_index, 'VpcId');
$subnet_index = array_rand($subnets);
$add[$i]['field_subnet'] = $subnets[$subnet_index]['SubnetId'];
$subnet_name = $this->getNameFromArray($subnets, $subnet_index, 'SubnetId');
$iam_role_index = array_rand($iam_roles);
$add[$i]['field_iam_role'] = $iam_roles[$iam_role_index]['Arn'];
$iam_role_name = $iam_roles[$iam_role_index]['InstanceProfileName'];
$this->drupalPostForm("/clouds/design/server_template/$cloud_context/aws_cloud/add",
$add[$i],
t('Save'));
$this->assertResponse(200, t('HTTP 200: Add | The new cloud server template Form #@num', ['@num' => $num]));
$this->assertNoText(t('Notice'), t('Make sure w/o Notice'));
$this->assertNoText(t('Warning'), t('Make sure w/o Warnings'));
$this->assertText(
t('Created the @name cloud server template', [
'@name' => $add[$i]['name[0][value]'],
]),
t('Confirm Message: Created the @name cloud server template', [
'@name' => $add[$i]['name[0][value]'],
])
);
$this->assertText($add[$i]['name[0][value]']);
$this->assertText($vpc_name);
$this->assertText($subnet_name);
$this->assertText($iam_role_name);
$this->assertText($add[$i]['field_tags[0][tag_key]']);
$this->assertText($add[$i]['field_tags[0][tag_value]']);
// Make sure listing.
$this->drupalGet("/clouds/design/server_template/$cloud_context");
$this->assertResponse(200, t('HTTP 200: List | Cloud server template #@num', ['@num' => $num]));
$this->assertNoText(t('Notice'), t('Make sure w/o Notice'));
$this->assertNoText(t('Warning'), t('Make sure w/o Warnings'));
for ($j = 0; $j < $i + 1; $j++) {
$this->assertText($add[$j]['name[0][value]'],
t("Make sure w/ Listing @num: @name", [
'@num' => $j + 1,
'@name' => $add[$j]['name[0][value]'],
]));
}
}
// Edit case.
$edit = $this->createServerTemplateTestFormData(self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT);
for ($i = 0; $i < self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT; $i++) {
$num = $i + 1;
// Name can't be changed.
$edit[$i]['name[0][value]'] = $add[$i]['name[0][value]'];
$edit[$i]['field_image_id'] = $image_id;
$vpc_index = array_rand($vpcs);
$edit[$i]['field_vpc'] = $vpcs[$vpc_index]['VpcId'];
$vpc_name = $this->getNameFromArray($vpcs, $vpc_index, 'VpcId');
$subnet_index = array_rand($subnets);
$edit[$i]['field_subnet'] = $subnets[$subnet_index]['SubnetId'];
$subnet_name = $this->getNameFromArray($subnets, $subnet_index, 'SubnetId');
$this->drupalPostForm("/clouds/design/server_template/$cloud_context/$num/edit",
$edit[$i],
t('Save'));
$this->assertResponse(200, t('HTTP 200: Edit | The new cloud server template #@num', ['@num' => $num]));
$this->assertNoText(t('Notice'), t('Make sure w/o Notice'));
$this->assertNoText(t('Warning'), t('Make sure w/o Warnings'));
$this->assertText(
t('Saved the @name cloud server template.',
[
'@name' => $edit[$i]['name[0][value]'],
]
),
t('Confirm Message: Saved the @name cloud server template.',
[
'@name' => $edit[$i]['name[0][value]'],
]
)
);
$this->assertText($edit[$i]['name[0][value]']);
$this->assertText($vpc_name);
$this->assertText($subnet_name);
// Make sure listing.
$this->drupalGet("/clouds/design/server_template/$cloud_context");
$this->assertResponse(200, t('HTTP 200: List | Cloud server template #@num', ['@num' => $num]));
$this->assertNoText(t('Notice'), t('Make sure w/o Notice'));
$this->assertNoText(t('Warning'), t('Make sure w/o Warnings'));
for ($j = 0; $j < $i + 1; $j++) {
$this->assertText($edit[$j]['name[0][value]'],
t("Make sure w/ Listing @num: @name", [
'@num' => $j + 1,
'@name' => $edit[$j]['name[0][value]'],
]));
}
}
// Delete server_template Items
// 3 times.
for ($i = 0; $i < self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT; $i++) {
$num = $i + 1;
$this->drupalGet("/clouds/design/server_template/$cloud_context/$num/delete");
$this->drupalPostForm("/clouds/design/server_template/$cloud_context/$num/delete",
[],
t('Delete'));
$this->assertResponse(200, t('HTTP 200: Delete | Cloud server template #@num', ['@num' => $num]));
$this->assertNoText(t('Notice'), t('Make sure w/o Notice'));
$this->assertNoText(t('warning'), t('Make sure w/o Warnings'));
// Make sure listing.
$this->drupalGet("/clouds/design/server_template/$cloud_context");
$this->assertResponse(200, t('HTTP 200: List | Cloud server te#@num', ['@num' => $num]));
$this->assertNoText(t('Notice'), t('Make sure w/o Notice'));
$this->assertNoText(t('Warning'), t('Make sure w/o Warnings'));
for ($j = 0; $j < $i + 1; $j++) {
$this->assertNoText($edit[$j]['name[0][value]'],
t("Make sure w/ Listing @num: @name", [
'@num' => $j + 1,
'@name' => $edit[$j]['name[0][value]'],
]));
}
}
}
/**
* Tests CRUD for server_template revision information.
*/
public function testCloudServerTemplateRevision() {
$cloud_context = $this->cloudContext;
$image = $this->createImageTestEntity(0, $image_id = '', $cloud_context);
$image_id = $image->getImageId();
// Make sure if the image entity is created or not.
$this->drupalGet("/clouds/aws_cloud/${cloud_context}/image");
$this->assertResponse(200, t('HTTP 200: Image | The new AWS Cloud Image #@num', ['@num' => 1]));
$this->assertNoText(t('warning'), t('Image | Make sure w/o Warnings'));
$this->assertText($image->getName(), t('Image | Make sure w/ Image Name (TestEntity): @name', ['@name' => $image->getName()]));
$this->assertText($image->getImageId(), t('Image | Make sure w/ Image ID (TestEntity): @image_id', ['@image_id' => $image->getImageId()]));
$this->createSecurityGroupTestEntity(0, '', '', '', $cloud_context);
$this->createKeyPairTestEntity(0, '', '', $cloud_context);
$vpcs = $this->createVpcsRandomTestFormData();
$subnets = $this->createSubnetsRandomTestFormData();
$this->updateVpcsAndSubnetsMockData($vpcs, $subnets);
// IAM Roles.
$iam_roles = $this->createIamRolesRandomTestFormData();
$this->updateIamRolesMockData($iam_roles);
// Create a cloud server template.
$add = $this->createServerTemplateTestFormData();
$add[0]['field_image_id'] = $image_id;
$vpc_index = array_rand($vpcs);
$add[0]['field_vpc'] = $vpcs[$vpc_index]['VpcId'];
$subnet_index = array_rand($subnets);
$add[0]['field_subnet'] = $subnets[$subnet_index]['SubnetId'];
$this->drupalPostForm("/clouds/design/server_template/$cloud_context/aws_cloud/add",
$add[0],
t('Save'));
$this->assertResponse(200, t('HTTP 200: Add | The new CloudServerTemplate Form #@num', ['@num' => 1]));
$this->assertNoText(t('Notice'), t('Make sure w/o Notice'));
$this->assertNoText(t('Warning'), t('Make sure w/o Warnings'));
$this->assertText(
t('Created the @name cloud server template',
[
'@name' => $add[0]['name[0][value]'],
]
),
t('Confirm Message: Created the @name cloud server template',
[
'@name' => $add[0]['name[0][value]'],
]
)
);
// Make sure listing revisions.
$this->drupalGet("/clouds/design/server_template/$cloud_context/1/revisions");
$this->assertResponse(200, t('HTTP 200: List | Cloud server template Revision'));
$this->assertNoText(t('Notice'), t('Make sure w/o Notice'));
$this->assertNoText(t('Warning'), t('Make sure w/o Warnings'));
// Create a new revision.
$edit = $add[0];
$old_description = $edit['field_description[0][value]'];
$revision_desc = $this->random->name(32, TRUE);
$edit['field_description[0][value]'] = $revision_desc;
$this->drupalPostForm("/clouds/design/server_template/$cloud_context/1/edit",
$edit,
t('Save'));
$this->assertNoText(t('Notice'), t('Make sure w/o Notice'));
$this->assertNoText(t('Warning'), t('Make sure w/o Warnings'));
$this->assertText(
t('Saved the @name cloud server template.', [
'@name' => $edit['name[0][value]'],
]),
t('Confirm Message: Saved the @name cloud server template.',
[
'@name' => $edit['name[0][value]'],
])
);
// Make sure listing revisions.
$this->drupalGet("/clouds/design/server_template/$cloud_context/1/revisions");
$this->assertResponse(200, t('HTTP 200: List | Cloud server template revision'));
$this->assertNoText(t('Notice'), t('Make sure w/o Notice'));
$this->assertNoText(t('Warning'), t('Make sure w/o Warnings'));
$this->assertSession()->linkByHrefExists("server_template/$cloud_context/1/revisions/1/view");
$this->assertSession()->linkByHrefExists("server_template/$cloud_context/1/revisions/1/revert");
$this->assertSession()->linkByHrefExists("server_template/$cloud_context/1/revisions/1/delete");
// View the revision.
$this->drupalGet("/clouds/design/server_template/$cloud_context/1/revisions/1/view");
$this->assertResponse(200, t('HTTP 200: View | Cloud server template revision'));
$this->assertNoText(t('Notice'), t('Make sure w/o Notice'));
$this->assertNoText(t('Warning'), t('Make sure w/o Warnings'));
$this->assertText($old_description);
// Revert the revision.
$this->drupalPostForm("/clouds/design/server_template/$cloud_context/1/revisions/1/revert",
[],
t('Revert'));
$this->assertResponse(200, t('HTTP 200: List | Cloud server template revision'));
$this->assertNoText(t('Notice'), t('Make sure w/o Notice'));
$this->assertNoText(t('Warning'), t('Make sure w/o Warnings'));
// Check the string: 'The cloud server template %title has been reverted to
// the revision from %revision-date.'.
$this->assertText(t('The cloud server template @name has been reverted to the revision from', [
'@name' => $edit['name[0][value]'],
]));
// The new revision is created.
$this->assertSession()->linkByHrefExists("server_template/$cloud_context/1/revisions/2/view");
// Delete the revision.
$this->drupalPostForm("/clouds/design/server_template/$cloud_context/1/revisions/1/delete",
[],
t('Delete'));
$this->assertResponse(200, t('HTTP 200: List | Cloud server template revision'));
$this->assertNoText(t('Notice'), t('Make sure w/o Notice'));
$this->assertNoText(t('Warning'), t('Make sure w/o Warnings'));
// Check the part of the string: 'Revision from %revision-date of cloud
// server template %title has been deleted.'.
$this->assertText(t('of cloud server template @name has been deleted.', [
'@name' => $edit['name[0][value]'],
]));
// The revision is deleted.
$this->assertSession()->linkByHrefNotExists("server_template/$cloud_context/1/revisions/1/view");
// Test copy function.
$this->drupalGet("/clouds/design/server_template/{$cloud_context}");
$this->clickLink($add[0]['name[0][value]']);
$this->clickLink('Copy');
$copy_url = $this->getUrl();
$this->drupalPostForm($copy_url, [], 'Copy');
$this->assertText(t('Created the copy_of_@name cloud server template', ['@name' => $add[0]['name[0][value]']]));
$this->assertText("copy_of_{$add[0]['name[0][value]']}");
}
/**
* Tests CRUD for server_template information.
*/
public function testCloudServerTemplateCopy() {
$cloud_context = $this->cloudContext;
$image = $this->createImageTestEntity(0, $image_id = '', $cloud_context);
$image_id = $image->getImageId();
// Make sure if the image entity is created or not.
$this->drupalGet("/clouds/aws_cloud/${cloud_context}/image");
$this->assertResponse(200, t('HTTP 200: Image | The new AWS Cloud Image #@num', ['@num' => 1]));
$this->assertNoText(t('warning'), t('Image | Make sure w/o Warnings'));
$this->assertText($image->getName(), t('Image | Make sure w/ Image Name (TestEntity): @name', ['@name' => $image->getName()]));
$this->assertText($image->getImageId(), t('Image | Make sure w/ Image ID (TestEntity): @image_id', ['@image_id' => $image->getImageId()]));
$this->createSecurityGroupTestEntity(0, '', '', '', $cloud_context);
$this->createKeyPairTestEntity(0, '', '', $cloud_context);
$vpcs = $this->createVpcsRandomTestFormData();
$subnets = $this->createSubnetsRandomTestFormData();
$this->updateVpcsAndSubnetsMockData($vpcs, $subnets);
// IAM Roles.
$iam_roles = $this->createIamRolesRandomTestFormData();
$this->updateIamRolesMockData($iam_roles);
// Add a new server_template information.
$add = $this->createServerTemplateTestFormData(self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT);
$copy = $this->createServerTemplateTestFormData(self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT);
$num = 1;
for ($i = 0; $i < self::CLOUD_SERVER_TEMPLATES_REPEAT_COUNT; $i++) {
$add[$i]['field_image_id'] = $image_id;
$vpc_index = array_rand($vpcs);
$add[$i]['field_vpc'] = $vpcs[$vpc_index]['VpcId'];
$vpc_name = $this->getNameFromArray($vpcs, $vpc_index, 'VpcId');
$subnet_index = array_rand($subnets);
$add[$i]['field_subnet'] = $subnets[$subnet_index]['SubnetId'];
$subnet_name = $this->getNameFromArray($subnets, $subnet_index, 'SubnetId');
$iam_role_index = array_rand($iam_roles);
$add[$i]['field_iam_role'] = $iam_roles[$iam_role_index]['Arn'];
$iam_role_name = $iam_roles[$iam_role_index]['InstanceProfileName'];
$this->drupalPostForm("/clouds/design/server_template/$cloud_context/aws_cloud/add",
$add[$i],
t('Save'));
$this->assertResponse(200, t('HTTP 200: Add | The new CloudServerTemplate Form #@num', ['@num' => $num]));
$this->assertNoText(t('Notice'), t('Make sure w/o Notice'));
$this->assertNoText(t('Warning'), t('Make sure w/o Warnings'));
$this->assertText(
t('Created the @name cloud server template', [
'@name' => $add[$i]['name[0][value]'],
]),
t('Confirm Message: Created the @name cloud server template', [
'@name' => $add[$i]['name[0][value]'],
])
);
// Access copy page.
$this->drupalGet("/clouds/design/server_template/$cloud_context/$num/copy");
$this->assertText('Copy cloud server template');
$this->assertSession()->fieldValueEquals('name[0][value]', "copy_of_{$add[$i]['name[0][value]']}");
// Submit copy.
$copy[$i]['field_image_id'] = $image_id;
$this->drupalPostForm("/clouds/design/server_template/$cloud_context/$num/copy",
$copy[$i],
t('Copy'));
$this->assertNoText(t('Notice'), t('Make sure w/o Notice'));
$this->assertNoText(t('Warning'), t('Make sure w/o Warnings'));
$this->assertText(
t('Created the @name cloud server template', [
'@name' => $copy[$i]['name[0][value]'],
]),
t('Confirm Message: Created the @name cloud server template', [
'@name' => $copy[$i]['name[0][value]'],
])
);
$this->assertNoText($add[$i]['name[0][value]']);
// Access edit page.
$num++;
$this->drupalGet("/clouds/design/server_template/$cloud_context/$num/edit");
foreach ($copy[$i] as $key => $value) {
if (strpos($key, 'field_tags') === FALSE) {
$this->assertSession()->fieldValueEquals($key, $value);
}
}
$num++;
}
}
/**
* Get name from array.
*
* @param array $array
* The VPCs or subnets array.
* @param int $index
* The index of array.
* @param string $id_key_name
* The key name of index.
*
* @return string
* value of array.
*/
private function getNameFromArray(array $array, $index, $id_key_name) {
// Get ID.
return $array[$index][$id_key_name];
}
}
