You are here

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

Tests exportContentWithReferences().

File

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

Class

ExporterIntegrationTest
Tests export functionality.

Namespace

Drupal\Tests\default_content\Kernel

Code

public function testExportWithReferences() {
  \Drupal::service('module_installer')
    ->install([
    'node',
    'default_content',
  ]);
  $this->exporter = \Drupal::service('default_content.exporter');
  $role = Role::create([
    'id' => 'example_role',
  ]);
  $role
    ->save();
  $user = User::create([
    'name' => 'my username',
    'uid' => 2,
    'roles' => $role
      ->id(),
  ]);
  $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());
  $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.
  $meta = [
    'version' => '1.0',
    'entity_type' => 'node',
    'uuid' => $node
      ->uuid(),
    'bundle' => $node
      ->bundle(),
    'default_langcode' => $node
      ->language()
      ->getId(),
    'depends' => [
      $user
        ->uuid() => 'user',
    ],
  ];
  $exported_node = Yaml::decode($exported_by_entity_type['node'][$node
    ->uuid()]);
  $this
    ->assertEquals($meta, $exported_node['_meta']);
  $this
    ->assertEquals($node
    ->label(), $exported_node['default']['title'][0]['value']);
  $meta = [
    'version' => '1.0',
    'entity_type' => 'user',
    'uuid' => $user
      ->uuid(),
    'default_langcode' => $node
      ->language()
      ->getId(),
  ];
  $exported_user = Yaml::decode($exported_by_entity_type['user'][$user
    ->uuid()]);
  $this
    ->assertEquals($meta, $exported_user['_meta']);
  $this
    ->assertEquals($user
    ->label(), $exported_user['default']['name'][0]['value']);
  $this
    ->assertEquals($role
    ->id(), $exported_user['default']['roles'][0]['target_id']);

  // 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']));
}