cloud-8.x-2.0-beta1/modules/cloud_service_providers/k8s/tests/src/Traits/K8sTestEntityTrait.php
modules/cloud_service_providers/k8s/tests/src/Traits/K8sTestEntityTrait.php
<?php
namespace Drupal\Tests\k8s\Traits;
use Drupal\k8s\Entity\K8sNamespace;
use Drupal\k8s\Entity\K8sPod;
use Drupal\k8s\Entity\K8sDeployment;
use Drupal\k8s\Entity\K8sReplicaSet;
use Drupal\k8s\Entity\K8sServiceEntity;
use Drupal\k8s\Entity\K8sCronJob;
use Drupal\k8s\Entity\K8sJob;
use Drupal\k8s\Entity\K8sResourceQuota;
use Drupal\k8s\Entity\K8sLimitRange;
use Drupal\k8s\Entity\K8sSecret;
use Drupal\k8s\Entity\K8sConfigMap;
use Drupal\k8s\Entity\K8sNetworkPolicy;
/**
* The trait creating test entity for k8s testing.
*/
trait K8sTestEntityTrait {
/**
* Test bulk operation for entities.
*
* @param string $entity_type_name
* The name of the entity type. For example, instance.
* @param array $entities_data
* The data of entities.
* @param string $operation
* The operation.
* @param string $operation_passive
* The passive voice of operation.
* @param string $path_prefix
* The URL path of prefix.
*/
protected function doTestEntityBulk($entity_type_name,
array $entities_data,
$operation = 'delete',
$operation_passive = 'deleted',
$path_prefix = '/clouds/k8s') {
$cloud_context = !empty($this->cloudContext)
? '/' . $this->cloudContext
: '';
$entity_count = count($entities_data);
$entity_type_id = $entities_data[0]->getEntityTypeId();
$data = [];
$data['action'] = "${entity_type_id}_${operation}_action";
$this->drupalGet("$path_prefix$cloud_context/$entity_type_name");
$checkboxes = $this->cssSelect('input[type=checkbox]');
foreach ($checkboxes as $checkbox) {
if ($checkbox->getAttribute('name') == NULL) {
continue;
}
$data[$checkbox->getAttribute('name')] = $checkbox->getAttribute('value');
}
// Confirm.
$this->drupalPostForm(
"$path_prefix$cloud_context/$entity_type_name",
$data,
t('Apply to selected items')
);
$this->assertResponse(200);
$message = \Drupal::translation()->formatPlural($entity_count,
'Are you sure you want to @operation this @item?',
'Are you sure you want to @operation these @item entities?', [
'@operation' => $operation,
'@item' => $entities_data[0]->getEntityType()->getLowercaseLabel(),
]
);
$this->assertText($message);
foreach ($entities_data as $entity_data) {
$entity_name = $entity_data->label();
$this->assertText($entity_name);
}
// Operation.
$operation_upper = ucfirst($operation);
$this->drupalPostForm(
"$path_prefix$cloud_context/$entity_type_name/${operation}_multiple",
[],
$operation_upper
);
$this->assertResponse(200);
foreach ($entities_data as $entity_data) {
$this->assertText(
t('The @type "@label" has been @operation_passive.', [
'@type' => $entity_data->getEntityType()->getLabel(),
'@label' => $entity_data->label(),
'@operation_passive' => $operation_passive,
])
);
}
$operation_passive_upper = ucfirst($operation_passive);
$message = \Drupal::translation()->formatPlural($entity_count,
t('@operation_passive_upper @entity_count item.', [
'@operation_passive_upper' => $operation_passive_upper,
'@entity_count' => $entity_count,
]),
t('@operation_passive_upper @entity_count items.', [
'@operation_passive_upper' => $operation_passive_upper,
'@entity_count' => $entity_count,
])
);
$this->assertText($message);
}
/**
* Create a K8s Namespace test entity.
*
* @param array $namespace
* The namespace data.
*
* @return \Drupal\k8s\Entity\K8sNamespace
* The Namespace entity.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
protected function createNamespaceTestEntity(array $namespace) {
$entity = K8sNamespace::create([
'name' => $namespace['name'],
'cloud_context' => $this->cloudContext,
]);
$entity->save();
return $entity;
}
/**
* Create a K8s Pod test entity.
*
* @param array $pod
* The pod data.
*
* @return \Drupal\k8s\Entity\K8sPod
* The Pod entity.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
protected function createPodTestEntity(array $pod) {
return $this->createTestEntity(K8sPod::class, $pod);
}
/**
* Create a K8s Deployment test entity.
*
* @param array $deployment
* The deployment data.
*
* @return \Drupal\k8s\Entity\K8sDeployment
* The Deployment entity.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
protected function createDeploymentTestEntity(array $deployment) {
return $this->createTestEntity(K8sDeployment::class, $deployment);
}
/**
* Create a K8s Replica Set test entity.
*
* @param array $replica_set
* The replica set data.
*
* @return \Drupal\k8s\Entity\K8sReplicaSet
* The Replica Set entity.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
protected function createReplicaSetTestEntity(array $replica_set) {
return $this->createTestEntity(K8sReplicaSet::class, $replica_set);
}
/**
* Create a K8s Service test entity.
*
* @param array $service
* The service data.
*
* @return \Drupal\k8s\Entity\K8sService
* The Service entity.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
protected function createServiceTestEntity(array $service) {
return $this->createTestEntity(K8sServiceEntity::class, $service);
}
/**
* Create a K8s Cron Job test entity.
*
* @param array $cron_job
* The cron job data.
*
* @return \Drupal\k8s\Entity\K8sCronJob
* The Cron Job entity.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
protected function createCronJobTestEntity(array $cron_job) {
return $this->createTestEntity(K8sCronJob::class, $cron_job);
}
/**
* Create a K8s Job test entity.
*
* @param array $job
* The job data.
*
* @return \Drupal\k8s\Entity\K8sJob
* The Job entity.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
protected function createJobTestEntity(array $job) {
return $this->createTestEntity(K8sJob::class, $job);
}
/**
* Create a K8s Resource Quota test entity.
*
* @param array $resource_quota
* The resource quota data.
*
* @return \Drupal\k8s\Entity\K8sResourceQuota
* The Resource Quota entity.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
protected function createResourceQuotaTestEntity(array $resource_quota) {
return $this->createTestEntity(K8sResourceQuota::class, $resource_quota);
}
/**
* Create a K8s Limit Range test entity.
*
* @param array $limit_range
* The limit range data.
*
* @return \Drupal\k8s\Entity\K8sLimitRange
* The Limit Range entity.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
protected function createLimitRangeTestEntity(array $limit_range) {
return $this->createTestEntity(K8sLimitRange::class, $limit_range);
}
/**
* Create a K8s Secret test entity.
*
* @param array $secret
* The secret data.
*
* @return \Drupal\k8s\Entity\K8sSecret
* The secret entity.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
protected function createSecretTestEntity(array $secret) {
return $this->createTestEntity(K8sSecret::class, $secret);
}
/**
* Create a K8s Config Map test entity.
*
* @param array $config_map
* The config map data.
*
* @return \Drupal\k8s\Entity\K8sConfigMap
* The config map entity.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
protected function createConfigMapTestEntity(array $config_map) {
return $this->createTestEntity(K8sConfigMap::class, $config_map);
}
/**
* Create a K8s Network Policy test entity.
*
* @param array $network_policy
* The network policy data.
*
* @return \Drupal\k8s\Entity\K8sNetworkPolicy
* The Network Policy entity.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
protected function createNetworkPolicyTestEntity(array $network_policy) {
return $this->createTestEntity(K8sNetworkPolicy::class, $network_policy);
}
/**
* Helper function to create a K8s test entity.
*
* @param string $class_name
* The class name.
* @param array $form_data
* The form data.
*
* @return \Drupal\cloud\Entity\CloudContentEntityBase
* The entity.
*
* @throws \Drupal\Core\Entity\EntityStorageException
*/
private function createTestEntity($class_name, array $form_data) {
$entity = $class_name::create([
'name' => $form_data['name'],
'namespace' => $form_data['namespace'],
'cloud_context' => $this->cloudContext,
]);
$entity->save();
return $entity;
}
}
