View source
<?php
namespace Drupal\Tests\acquia_contenthub\Kernel;
use Acquia\ContentHubClient\CDFDocument;
use Acquia\ContentHubClient\Event\GetCDFTypeEvent;
use Acquia\ContentHubClient\EventSubscriber\DefaultCDF;
use Drupal\Component\Serialization\Json;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Serialization\Yaml;
use Drupal\depcalc\DependencyStack;
use Drupal\depcalc\DependentEntityWrapper;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\Tests\acquia_contenthub\Kernel\Stubs\CdfExpectations;
use Drupal\Tests\acquia_contenthub\Kernel\Stubs\DrupalVersion;
abstract class ImportExportTestBase extends EntityKernelTestBase {
use DrupalVersion;
const ENTITY_REFERENCE_TYPES = [
'file',
'entity_reference',
'entity_reference_revisions',
];
const ENTITY_REFERENCE_IMAGE_TYPE = 'image';
protected $fixtures = [];
public static $modules = [
'system',
'depcalc',
'acquia_contenthub',
];
public static $normalizeList = [
'content_translation_created',
'content_translation_changed',
'content_translation_outdated',
'content_translation_source',
'menu_link',
'revision_created',
'revision_translation_affected',
'revision_log',
'comment',
];
protected function setUp() {
if (version_compare(\Drupal::VERSION, '9.0', '>=')) {
static::$modules[] = 'path_alias';
}
parent::setUp();
if (version_compare(\Drupal::VERSION, '8.8.0', '>=')) {
$this
->installEntitySchema('path_alias');
}
}
protected function getFixtureString(int $delta) {
if (!empty($this->fixtures[$delta])) {
$version_directory = $this
->getDrupalVersion();
$path_to_fixture = sprintf("%s/tests/fixtures/import/{$version_directory}/%s", drupal_get_path('module', 'acquia_contenthub'), $this->fixtures[$delta]['cdf']);
return file_get_contents($path_to_fixture);
}
throw new \Exception(sprintf("Missing fixture for delta %d in class %s", $delta, __CLASS__));
}
protected function getFixtureExpectations(int $delta) {
if (!empty($this->fixtures[$delta])) {
$version_directory = $this
->getDrupalVersion();
$path_to_fixture = sprintf("%s/tests/fixtures/import/{$version_directory}/%s", drupal_get_path('module', 'acquia_contenthub'), $this->fixtures[$delta]['expectations']);
return include $path_to_fixture;
}
throw new \Exception(sprintf("Missing expectations for delta %d in class %s", $delta, __CLASS__));
}
protected function createCdfDocumentFromFixture(int $delta) : CDFDocument {
$json = $this
->getFixtureString($delta);
$data = Json::decode($json);
$document_parts = [];
foreach ($data['entities'] as $entity) {
$document_parts[] = $this
->populateCdfObject($entity);
}
$cdf_document = new CDFDocument(...$document_parts);
return $cdf_document;
}
protected function populateCdfObject(array $data) {
$event = new GetCDFTypeEvent($data);
$subscriber = new DefaultCDF();
$subscriber
->onGetCDFType($event);
return $event
->getObject();
}
protected function importFixture(int $delta) {
$expectations = $this
->getFixtureExpectations($delta);
$document = $this
->createCdfDocumentFromFixture($delta);
$stack = new DependencyStack();
$this
->getSerializer()
->unserializeEntities($document, $stack);
return $expectations;
}
public function configEntityImportExport(int $delta, array $validate_data, $export_type, $export_uuid) {
$expectations = $this
->importFixture($delta);
foreach ($validate_data as $item) {
[
'type' => $type,
'uuid' => $uuid,
] = $item;
if (!isset($expectations[$uuid])) {
throw new \Exception(sprintf('You are missing validation for the entity of type %s of uuid %s.', $type, $uuid));
}
$expectation = $expectations[$uuid];
$entity = $this
->getEntity($type, $uuid, $expectation);
$entity_type = $entity
->getEntityType();
$config_name = $entity_type
->getConfigPrefix() . '.' . $entity
->get($entity_type
->getKey('id'));
$this
->assertImportedConfigEntity($expectation, $config_name);
}
$expectation = $expectations[$export_uuid] ?? NULL;
$this
->assertExportedConfigEntities($delta, $export_type, $export_uuid, $expectation);
}
protected function getEntity($type, $uuid, CdfExpectations $expectation = NULL) : EntityInterface {
$repository = $this->container
->get('entity.repository');
$entity = $repository
->loadEntityByUuid($type, $uuid);
if ($entity) {
return $entity;
}
if ($expectation && ($entity_loader = $expectation
->getEntityLoader())) {
$entity = call_user_func($entity_loader);
if ($entity) {
return $entity;
}
}
throw new \Exception(sprintf('Failed to load entity of %s type by uuid=%s.', $type, $uuid));
}
protected function assertImportedConfigEntity(CdfExpectations $expectation, string $config_name) : void {
if (!$expectation
->getLangcodes()) {
foreach ($expectation
->getFieldNames() as $field_name) {
$actual_value = \Drupal::config($config_name)
->get($field_name);
$expected_value = $expectation
->getFieldValue($field_name);
$this
->assertEquals($expected_value, $actual_value);
}
return;
}
$language_manager = $this->container
->get('language_manager');
$default_language = $language_manager
->getCurrentLanguage();
foreach ($expectation
->getLangcodes() as $langcode) {
$language = $language_manager
->getLanguage($langcode);
$language_manager
->setConfigOverrideLanguage($language);
foreach ($expectation
->getFieldNames() as $field_name) {
if (FALSE !== strpos($field_name, ':')) {
[
$field_name_level1,
$field_name_level2,
] = explode(':', $field_name);
$actual_value = \Drupal::config($config_name)
->get($field_name_level1);
if (!isset($actual_value[$field_name_level2])) {
throw new \Exception(sprintf("Failed to get actual value for '%s' field defined in the expectation file ('%s' configuration).", $field_name, $config_name));
}
$actual_value = $actual_value[$field_name_level2];
}
else {
$actual_value = \Drupal::config($config_name)
->get($field_name);
}
$expected_value = $expectation
->getFieldValue($field_name, $langcode);
$this
->assertEquals($expected_value, $actual_value);
}
}
$language_manager
->setConfigOverrideLanguage($default_language);
}
protected function assertExportedConfigEntities(int $delta, string $type, string $uuid, CdfExpectations $expectation = NULL) : void {
$entity = $this
->getEntity($type, $uuid, $expectation);
$wrapper = new DependentEntityWrapper($entity);
$stack = new DependencyStack();
$this
->getCalculator()
->calculateDependencies($wrapper, $stack);
$entities = NestedArray::mergeDeep([
$wrapper
->getUuid() => $wrapper,
], $stack
->getDependenciesByUuid(array_keys($wrapper
->getDependencies())));
$data = $this
->getSerializer()
->serializeEntities(...array_values($entities));
$document = new CDFDocument(...$data);
$cdf_objects = [];
foreach ($document
->getEntities() as $cdf_object) {
$cdf_objects[$cdf_object
->getUuid()] = $cdf_object;
}
$count = 0;
$fixtures = json_decode($this
->getFixtureString($delta), TRUE);
foreach ($fixtures['entities'] as $fixture) {
$cdf_object = $cdf_objects[$fixture['uuid']] ?? $cdf_objects[$entity
->uuid()];
$object = $this
->decodeDataByObjectType($cdf_object
->getMetadata()['data'], $cdf_object
->getType());
$fixture = $this
->decodeDataByObjectType($fixture['metadata']['data'], $fixture['type']);
$langcode = $this->container
->get('language_manager')
->getDefaultLanguage()
->getId();
unset($object[$langcode]['uuid']);
unset($fixture[$langcode]['uuid']);
$this
->assertEquals($fixture, $object);
$count++;
}
$this
->assertEquals($count, count($cdf_objects));
}
public function contentEntityImportExport(int $delta, array $validate_data, $export_type, $export_uuid, $compare_exports = TRUE) {
$expectations = $this
->importFixture($delta);
$repository = $this->container
->get('entity.repository');
foreach ($validate_data as $datum) {
$entity_type = $datum['type'];
$validate_uuid = $datum['uuid'];
$entity = $repository
->loadEntityByUuid($entity_type, $validate_uuid);
if (!isset($expectations[$validate_uuid])) {
throw new \Exception(sprintf("You are missing validation for the entity of type %s of uuid %s.", $entity_type, $validate_uuid));
}
$expectation = $expectations[$validate_uuid];
foreach ($entity
->getTranslationLanguages() as $language) {
$trans = $entity
->getTranslation($language
->getId());
foreach ($trans as $field_name => $field) {
if ($expectation
->isExcludedField($field_name)) {
continue;
}
$actual_value = $this
->handleFieldValues($field);
$expected_value = $expectation
->getFieldValue($field_name, $language
->getId());
$message = 'File: ' . $this->fixtures[$delta]['expectations'];
$message .= "\nEntity: " . $trans
->uuid();
$message .= "\nField name: " . $field_name;
$message .= "\nExpected:\n" . print_r($expected_value, TRUE) . "\nActual:\n" . print_r($actual_value, TRUE);
$expected_value = $this
->cleanLineEndings($expected_value);
$actual_value = $this
->cleanLineEndings($actual_value);
$this
->assertEquals($expected_value, $actual_value, $message);
}
}
}
$export_entity = $repository
->loadEntityByUuid($export_type, $export_uuid);
$wrapper = new DependentEntityWrapper($export_entity);
$stack = new DependencyStack();
$this
->getCalculator()
->calculateDependencies($wrapper, $stack);
$entities = NestedArray::mergeDeep([
$wrapper
->getUuid() => $wrapper,
], $stack
->getDependenciesByUuid(array_keys($wrapper
->getDependencies())));
$data = $this
->getSerializer()
->serializeEntities(...array_values($entities));
$document = new CDFDocument(...$data);
$fixtures = json_decode($this
->getFixtureString($delta), TRUE);
$objects = [];
foreach ($document
->getEntities() as $object) {
if ($object
->getType() !== 'drupal8_content_entity') {
continue;
}
$objects[$object
->getUuid()] = $object;
}
if (!$compare_exports) {
return;
}
$count = 0;
foreach ($fixtures['entities'] as $fixture) {
if ($fixture['type'] !== 'drupal8_content_entity') {
continue;
}
$count++;
$uuid = $fixture['uuid'];
$object = $this
->decodeDataByObjectType($objects[$fixture['uuid']]
->getMetadata()['data'], 'drupal8_content_entity');
$fixture = $this
->decodeDataByObjectType($fixture['metadata']['data'], 'drupal8_content_entity');
[
$fixture,
$object,
] = $this
->normalizeFixtureAndObject($fixture, $object);
$this
->assertEquals($fixture, $object, "Testing object {$uuid}.");
}
$this
->assertEquals($count, count($objects));
}
protected function getSerializer() {
return \Drupal::service('entity.cdf.serializer');
}
protected function getCalculator() {
return \Drupal::service('entity.dependency.calculator');
}
protected function handleFieldValues(FieldItemListInterface $field) {
$values = $field
->getValue();
if (in_array($field
->getFieldDefinition()
->getType(), self::ENTITY_REFERENCE_TYPES) && $values) {
$values = [];
foreach ($field as $item_delta => $item) {
if ($item
->getValue()['target_id']) {
$values[$item_delta]['target_id'] = $item->entity
->uuid();
}
}
}
if ($field
->getFieldDefinition()
->getType() === self::ENTITY_REFERENCE_IMAGE_TYPE && $values) {
$values = [];
foreach ($field as $item_delta => $item) {
if ($item
->getValue()['target_id']) {
$values[$item_delta] = $item
->getValue();
$values[$item_delta]['target_id'] = $item->entity
->uuid();
}
}
}
if ($field
->getFieldDefinition()
->getType() === 'link') {
foreach ($field as $item_delta => $item) {
[
$uri_type,
$uri,
] = explode(':', $item
->getValue()['uri']);
if ($uri_type === 'entity') {
[
$item_entity_type,
$item_entity_id,
] = explode('/', $uri);
$uri_entity = $this->entityTypeManager
->getStorage($item_entity_type)
->load($item_entity_id);
$values[$item_delta]['uri'] = $uri_entity
->uuid();
}
elseif ($uri_type === 'internal') {
$uri = ltrim($uri, '/');
if (substr_count($uri, '/') > 0) {
[
$item_entity_type,
$item_entity_id,
] = explode('/', $uri, 2);
try {
$uri_storage = $this->entityTypeManager
->getStorage($item_entity_type);
$uri_entity = !is_null($uri_storage) ? $uri_storage
->load($item_entity_id) : NULL;
if ($uri_entity) {
$values[$item_delta]['uri'] = $uri_entity
->uuid();
}
else {
$values[$item_delta]['uri'] = $item
->getValue()['uri'];
}
} catch (\Exception $e) {
$values[$item_delta]['uri'] = $item
->getValue()['uri'];
}
}
else {
$values[$item_delta]['uri'] = $item
->getValue()['uri'];
}
}
else {
$values[$item_delta]['uri'] = $item
->getValue()['uri'];
}
}
}
return $values;
}
protected function normalizeFixtureAndObject(array $fixture, array $object) : array {
$list = self::$normalizeList;
foreach ($fixture as $key => $value) {
if (!$value) {
$list[] = $key;
}
}
foreach ($list as $item) {
if (isset($fixture[$item])) {
unset($fixture[$item]);
}
if (isset($object[$item])) {
unset($object[$item]);
}
}
return [
$fixture,
$object,
];
}
protected function cleanLineEndings($value) {
if (is_string($value)) {
$value = preg_replace('/(\\r\\n|\\n\\r|\\r)/', "\n", $value);
}
if (is_array($value)) {
array_walk_recursive($value, function (&$item) {
$item = $this
->cleanLineEndings($item);
});
}
return $value;
}
protected function decodeDataByObjectType(string $data, string $object_type) {
switch ($object_type) {
case 'drupal8_config_entity':
return Yaml::decode(base64_decode($data));
case 'drupal8_content_entity':
default:
return json_decode(base64_decode($data), TRUE);
}
}
protected function loadByUuid(string $entity_type, string $uuid) {
return current($this->entityTypeManager
->getStorage($entity_type)
->loadByProperties([
'uuid' => $uuid,
]));
}
}