You are here

public function ImportExportTestBase::contentEntityImportExport in Acquia Content Hub 8.2

Import and export content.

Parameters

int $delta: Fixture delta.

array $validate_data: Data.

string $export_type: Exported entity type.

string $export_uuid: Entity UUID.

bool $compare_exports: Runs extended fixture/export comparison. FALSE for mismatched uuids.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

\Drupal\Core\Entity\EntityStorageException

16 calls to ImportExportTestBase::contentEntityImportExport()
BlockTest::testBlock in tests/src/Kernel/BlockTest.php
Tests "block_content" Drupal entity.
FileImportExportTest::testFileImportExport in tests/src/Kernel/FileImportExportTest.php
Tests import/export of node with file.
FileImportExportTest::testFileImportExportNoCopy in tests/src/Kernel/FileImportExportTest.php
Tests import/export of files with schemes where file copy isn't expected.
MediaImportExportTest::testMediaImportExport in tests/src/Kernel/MediaImportExportTest.php
Tests import/export of node with media.
MenuTest::testMenuImportExport in tests/src/Kernel/MenuTest.php
The menu import export test.

... See full list

File

tests/src/Kernel/ImportExportTestBase.php, line 402

Class

ImportExportTestBase
Base for testing exports and imports.

Namespace

Drupal\Tests\acquia_contenthub\Kernel

Code

public function contentEntityImportExport(int $delta, array $validate_data, $export_type, $export_uuid, $compare_exports = TRUE) {
  $expectations = $this
    ->importFixture($delta);

  /** @var \Drupal\Core\Entity\EntityRepositoryInterface $repository */
  $repository = $this->container
    ->get('entity.repository');
  foreach ($validate_data as $datum) {
    $entity_type = $datum['type'];
    $validate_uuid = $datum['uuid'];

    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    $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));
    }

    /** @var \Drupal\Tests\acquia_contenthub\Kernel\Stubs\CdfExpectations $expectation */
    $expectation = $expectations[$validate_uuid];
    foreach ($entity
      ->getTranslationLanguages() as $language) {
      $trans = $entity
        ->getTranslation($language
        ->getId());

      /** @var \Drupal\Core\Field\FieldItemListInterface $field */
      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);

  /** @var \Acquia\ContentHubClient\CDF\CDFObject[] $objects */
  $objects = [];

  // Reindex objects for easier assertions.
  foreach ($document
    ->getEntities() as $object) {
    if ($object
      ->getType() !== 'drupal8_content_entity') {
      continue;
    }
    $objects[$object
      ->getUuid()] = $object;
  }
  if (!$compare_exports) {
    return;
  }

  // Exclusively check content entities because configuration will need
  // separate test coverage.
  $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));
}