You are here

public function ExporterIntegrationTest::testExportContent in Default Content for D8 8

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

Tests exportContent().

File

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

Class

ExporterIntegrationTest
Tests export functionality.

Namespace

Drupal\Tests\default_content\Kernel

Code

public function testExportContent() {
  \Drupal::service('module_installer')
    ->install([
    'taxonomy',
    'default_content',
  ]);
  \Drupal::service('router.builder')
    ->rebuild();
  $this->exporter = \Drupal::service('default_content.exporter');
  $vocabulary = Vocabulary::create([
    'vid' => 'test',
  ]);
  $vocabulary
    ->save();
  $term = Term::create([
    'vid' => $vocabulary
      ->id(),
    'name' => 'test_name',
  ]);
  $term
    ->save();
  $term = Term::load($term
    ->id());

  /** @var \Symfony\Component\Serializer\Serializer $serializer */
  $serializer = \Drupal::service('serializer');
  \Drupal::service('hal.link_manager')
    ->setLinkDomain($this->container
    ->getParameter('default_content.link_domain'));
  $expected = $serializer
    ->serialize($term, 'hal_json', [
    'json_encode_options' => JSON_PRETTY_PRINT,
  ]);
  $exported = $this->exporter
    ->exportContent('taxonomy_term', $term
    ->id());
  $exported_decoded = json_decode($exported);

  // Ensure the proper UUID is part of it.
  $this
    ->assertEqual($exported_decoded->uuid[0]->value, $term
    ->uuid());
  $this
    ->assertEqual($exported, $expected);

  // Tests export of taxonomy parent field.
  // @todo Get rid of after https://www.drupal.org/node/2543726
  $child_term = $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());
  $relation_uri = 'http://drupal.org/rest/relation/taxonomy_term/test/parent';
  $exported_decoded = json_decode($exported);
  $this
    ->assertFalse(empty($exported_decoded->_links->{$relation_uri}));
  $this
    ->assertFalse(empty($exported_decoded->_embedded->{$relation_uri}));
}