You are here

public function ExporterIntegrationTest::testExportWithReferences 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::testExportWithReferences()

Tests exportContentWithReferences().

File

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

Class

ExporterIntegrationTest
Tests export functionality.

Namespace

Drupal\Tests\default_content\Kernel

Code

public function testExportWithReferences() {
  \Drupal::service('module_installer')
    ->install([
    'node',
    'default_content',
  ]);
  \Drupal::service('router.builder')
    ->rebuild();
  $this->exporter = \Drupal::service('default_content.exporter');
  $user = User::create([
    'name' => 'my username',
  ]);
  $user
    ->save();

  // Reload the user to get the proper casted values from the DB.
  $user = User::load($user
    ->id());
  $node_type = NodeType::create([
    'type' => 'test',
  ]);
  $node_type
    ->save();
  $node = Node::create([
    'type' => $node_type
      ->id(),
    'title' => 'test node',
    'uid' => $user
      ->id(),
  ]);
  $node
    ->save();

  // Reload the node to get the proper casted values from the DB.
  $node = Node::load($node
    ->id());

  /** @var \Symfony\Component\Serializer\Serializer $serializer */
  $serializer = \Drupal::service('serializer');
  \Drupal::service('hal.link_manager')
    ->setLinkDomain($this->container
    ->getParameter('default_content.link_domain'));
  \Drupal::service('account_switcher')
    ->switchTo(User::load(1));
  $expected_node = $serializer
    ->serialize($node, 'hal_json', [
    'json_encode_options' => JSON_PRETTY_PRINT,
  ]);
  $expected_user = $serializer
    ->serialize($user, 'hal_json', [
    'json_encode_options' => JSON_PRETTY_PRINT,
  ]);
  $exported_by_entity_type = $this->exporter
    ->exportContentWithReferences('node', $node
    ->id());

  // Ensure that the node type is not tryed to be exported.
  $this
    ->assertEqual(array_keys($exported_by_entity_type), [
    'node',
    'user',
  ]);

  // Ensure the right UUIDs are exported.
  $this
    ->assertEqual([
    $node
      ->uuid(),
  ], array_keys($exported_by_entity_type['node']));
  $this
    ->assertEqual([
    $user
      ->uuid(),
  ], array_keys($exported_by_entity_type['user']));

  // Compare the actual serialized data.
  $this
    ->assertEqual(reset($exported_by_entity_type['node']), $expected_node);
  $this
    ->assertEqual(reset($exported_by_entity_type['user']), $expected_user);

  // Ensure no recursion on export.
  $field_name = 'field_test_self_ref';
  $this
    ->createEntityReferenceField('node', $node_type
    ->id(), $field_name, 'Self reference field', 'node');
  $node1 = Node::create([
    'type' => $node_type
      ->id(),
    'title' => 'ref 1->3',
  ]);
  $node1
    ->save();
  $node2 = Node::create([
    'type' => $node_type
      ->id(),
    'title' => 'ref 2->1',
    $field_name => $node1
      ->id(),
  ]);
  $node2
    ->save();
  $node3 = Node::create([
    'type' => $node_type
      ->id(),
    'title' => 'ref 3->2',
    $field_name => $node2
      ->id(),
  ]);
  $node3
    ->save();

  // Loop reference.
  $node1->{$field_name}->target_id = $node3
    ->id();
  $node1
    ->save();
  $exported_by_entity_type = $this->exporter
    ->exportContentWithReferences('node', $node3
    ->id());

  // Ensure all 3 nodes are exported.
  $this
    ->assertEquals(3, count($exported_by_entity_type['node']));
}