You are here

public function ExporterIntegrationTest::testExportContent in Default Content for D8 2.0.x

Same name and namespace in other branches
  1. 8 tests/src/Kernel/ExporterIntegrationTest.php \Drupal\Tests\default_content\Kernel\ExporterIntegrationTest::testExportContent()

Tests exportContent().

File

tests/src/Kernel/ExporterIntegrationTest.php, line 54

Class

ExporterIntegrationTest
Tests export functionality.

Namespace

Drupal\Tests\default_content\Kernel

Code

public function testExportContent() {
  \Drupal::service('module_installer')
    ->install([
    'taxonomy',
    'default_content',
  ]);
  $this->exporter = \Drupal::service('default_content.exporter');
  $vocabulary = Vocabulary::create([
    'vid' => 'test',
  ]);
  $vocabulary
    ->save();
  $term = Term::create([
    'vid' => $vocabulary
      ->id(),
    'name' => 'test_name',
    'description' => [
      'value' => 'The description',
      'format' => 'plain_text',
    ],
  ]);
  $term
    ->save();
  $term = Term::load($term
    ->id());
  $exported = $this->exporter
    ->exportContent('taxonomy_term', $term
    ->id());
  $exported_decoded = Yaml::decode($exported);

  // Assert the meta data and field values.
  $meta = [
    'version' => '1.0',
    'entity_type' => 'taxonomy_term',
    'uuid' => $term
      ->uuid(),
    'bundle' => $term
      ->bundle(),
    'default_langcode' => $term
      ->language()
      ->getId(),
  ];
  $this
    ->assertEquals($meta, $exported_decoded['_meta']);
  $this
    ->assertEquals($term
    ->label(), $exported_decoded['default']['name'][0]['value']);
  $expected_description = [
    [
      'value' => 'The description',
      'format' => 'plain_text',
    ],
  ];
  $this
    ->assertEqual($expected_description, $exported_decoded['default']['description']);

  // Tests export of taxonomy parent field.
  $child_term = Term::create([
    'vid' => $vocabulary
      ->id(),
    'name' => 'child_name',
    'parent' => $term
      ->id(),
  ]);
  $child_term
    ->save();

  // Make sure parent relation is exported.
  $exported = $this->exporter
    ->exportContent('taxonomy_term', $child_term
    ->id());
  $exported_decoded = Yaml::decode($exported);
  $this
    ->assertEquals($term
    ->uuid(), $exported_decoded['default']['parent'][0]['entity']);
  $this
    ->assertEquals('taxonomy_term', $exported_decoded['_meta']['depends'][$term
    ->uuid()]);
}