You are here

public function ContentEntityReferenceTest::testEntityReference in Entity Share 8.3

Test that entity reference values are good, and that entities are created.

File

modules/entity_share_client/tests/src/Functional/ContentEntityReferenceTest.php, line 102

Class

ContentEntityReferenceTest
Functional test class for content entity reference field.

Namespace

Drupal\Tests\entity_share_client\Functional

Code

public function testEntityReference() {

  // Test that a reference entity value is maintained.
  $this
    ->pullEveryChannels();
  $this
    ->checkCreatedEntities();

  // Test that a referenced entity is pulled even if not selected.
  // Need to remove all imported content prior to that.
  $this
    ->resetImportedContent();

  // Select only the top-level referencing entity.
  $selected_entities = [
    'es_test_level_0',
  ];
  $this
    ->importSelectedEntities($selected_entities);
  $this
    ->checkCreatedEntities();

  // Test that only certain referenced entities are pulled when not selected.
  // Set recursion depth or import config plugin to 2.
  $new_plugin_configurations = [
    'entity_reference' => [
      'max_recursion_depth' => 2,
      'weights' => [
        'process_entity' => 10,
      ],
    ],
  ];
  $this
    ->mergePluginsToImportConfig($new_plugin_configurations);
  $this
    ->resetImportedContent();

  // Select only the top-level referencing entity.
  $selected_entities = [
    'es_test_level_0',
  ];
  $this
    ->importSelectedEntities($selected_entities);
  $recreated_entities = $this
    ->loadEntity('node', 'es_test_level_1');
  $this
    ->assertTrue(!empty($recreated_entities), 'The node with UUID es_test_level_1 has been recreated.');
  $recreated_entities = $this
    ->loadEntity('node', 'es_test_level_2');
  $this
    ->assertTrue(!empty($recreated_entities), 'The node with UUID es_test_level_2 has been recreated.');
  $recreated_entities = $this
    ->loadEntity('node', 'es_test_level_3');
  $this
    ->assertFalse(!empty($recreated_entities), 'The node with UUID es_test_level_3 has not been recreated.');

  // Test that only certain referenced entities are pulled when not selected.
  // Set recursion depth or import config plugin to 1.
  $new_plugin_configurations = [
    'entity_reference' => [
      'max_recursion_depth' => 1,
      'weights' => [
        'process_entity' => 10,
      ],
    ],
  ];
  $this
    ->mergePluginsToImportConfig($new_plugin_configurations);
  $this
    ->resetImportedContent();

  // Select only the top-level referencing entity.
  $selected_entities = [
    'es_test_level_0',
  ];
  $this
    ->importSelectedEntities($selected_entities);
  $recreated_entities = $this
    ->loadEntity('node', 'es_test_level_1');
  $this
    ->assertTrue(!empty($recreated_entities), 'The node with UUID es_test_level_1 has been recreated.');
  $recreated_entities = $this
    ->loadEntity('node', 'es_test_level_2');
  $this
    ->assertFalse(!empty($recreated_entities), 'The node with UUID es_test_level_2 has not been recreated.');
  $recreated_entities = $this
    ->loadEntity('node', 'es_test_level_3');
  $this
    ->assertFalse(!empty($recreated_entities), 'The node with UUID es_test_level_3 has not been recreated.');

  // Test that only certain referenced entities are pulled when not selected.
  // Set recursion depth or import config plugin to 0.
  $new_plugin_configurations = [
    'entity_reference' => [
      'max_recursion_depth' => 0,
      'weights' => [
        'process_entity' => 10,
      ],
    ],
  ];
  $this
    ->mergePluginsToImportConfig($new_plugin_configurations);
  $this
    ->resetImportedContent();

  // Select only the top-level referencing entity.
  $selected_entities = [
    'es_test_level_0',
  ];
  $this
    ->importSelectedEntities($selected_entities);
  $recreated_entities = $this
    ->loadEntity('node', 'es_test_level_1');
  $this
    ->assertFalse(!empty($recreated_entities), 'The node with UUID es_test_level_1 has not been recreated.');
  $recreated_entities = $this
    ->loadEntity('node', 'es_test_level_2');
  $this
    ->assertFalse(!empty($recreated_entities), 'The node with UUID es_test_level_2 has not been recreated.');
  $recreated_entities = $this
    ->loadEntity('node', 'es_test_level_3');
  $this
    ->assertFalse(!empty($recreated_entities), 'The node with UUID es_test_level_3 has not been recreated.');

  // Test that only certain referenced entities are pulled when not selected.
  // Activate "Skip imported" with default settings.
  $new_plugin_configurations = [
    'skip_imported' => [
      'weights' => [
        'is_entity_importable' => -5,
      ],
    ],
    // Let's test with default recursion level (ie. unlimited).
    'entity_reference' => [
      'max_recursion_depth' => 2,
      'weights' => [
        'process_entity' => 10,
      ],
    ],
  ];
  $this
    ->mergePluginsToImportConfig($new_plugin_configurations);
  $this
    ->resetImportedContent();

  // Scenario 1: first import the referencing entity and then, again,
  // the target.
  // Select only the referencing entity.
  $selected_entities = [
    'es_test_level_2',
  ];
  $this
    ->importSelectedEntities($selected_entities);
  $target_entity = $this
    ->loadEntity('node', 'es_test_level_3');
  $this
    ->assertNotNull($target_entity, 'The target node has been created in the first import.');
  $referencing_entity = $this
    ->loadEntity('node', 'es_test_level_2');
  $this
    ->assertNotNull($referencing_entity, 'The referencing node has been created in the first import.');
  $this->importService
    ->getRuntimeImportContext()
    ->clearImportedEntities();

  // Select only the target entity.
  $selected_entities = [
    'es_test_level_3',
  ];
  $this
    ->importSelectedEntities($selected_entities);
  $target_entity = $this
    ->loadEntity('node', 'es_test_level_3');
  $this
    ->assertNotNull($target_entity, 'The target node still exists after the second import.');

  // Test if the relation between the entities is maintained.
  $expected_value = $target_entity
    ->id();
  $actual_value = $referencing_entity->field_es_test_content_reference->target_id;
  $this
    ->assertEquals($actual_value, $expected_value, 'The referencing node references the target node after second import.');

  // Delete created entities before the second turn.
  $this
    ->resetImportedContent();

  // Scenario 2: first import the target and then the referencing entity.
  // Select only the target entity.
  $selected_entities = [
    'es_test_level_3',
  ];
  $this
    ->importSelectedEntities($selected_entities);
  $target_entity = $this
    ->loadEntity('node', 'es_test_level_3');
  $this
    ->assertNotNull($target_entity, 'The target node has been created in the first import.');
  $this->importService
    ->getRuntimeImportContext()
    ->clearImportedEntities();

  // Select only the referencing entity.
  $selected_entities = [
    'es_test_level_2',
  ];
  $this
    ->importSelectedEntities($selected_entities);
  $target_entity = $this
    ->loadEntity('node', 'es_test_level_3');
  $this
    ->assertNotNull($target_entity, 'The target node still exists after the second import.');
  $referencing_entity = $this
    ->loadEntity('node', 'es_test_level_2');
  $this
    ->assertNotNull($referencing_entity, 'The referencing node has been created in the second import.');

  // Test if the relation between the entities is maintained.
  $expected_value = $target_entity
    ->id();
  $actual_value = $referencing_entity->field_es_test_content_reference->target_id;
  $this
    ->assertEquals($actual_value, $expected_value, 'The referencing node references the target node after second import.');
}