You are here

public function EntityReferenceRelationshipTest::testDataTableRelationship in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/field/tests/src/Kernel/EntityReference/Views/EntityReferenceRelationshipTest.php \Drupal\Tests\field\Kernel\EntityReference\Views\EntityReferenceRelationshipTest::testDataTableRelationship()
  2. 9 core/modules/field/tests/src/Kernel/EntityReference/Views/EntityReferenceRelationshipTest.php \Drupal\Tests\field\Kernel\EntityReference\Views\EntityReferenceRelationshipTest::testDataTableRelationship()

Tests views data generated for relationship.

See also

entity_reference_field_views_data()

File

core/modules/field/tests/src/Kernel/EntityReference/Views/EntityReferenceRelationshipTest.php, line 172

Class

EntityReferenceRelationshipTest
Tests entity reference relationship data.

Namespace

Drupal\Tests\field\Kernel\EntityReference\Views

Code

public function testDataTableRelationship() {

  // Create some test entities which link each other.
  $referenced_entity = EntityTest::create();
  $referenced_entity
    ->save();
  $entity = EntityTestMul::create();
  $entity->field_data_test->target_id = $referenced_entity
    ->id();
  $entity
    ->save();
  $this
    ->assertEquals($referenced_entity
    ->id(), $entity->field_data_test[0]->entity
    ->id());
  $this->entities[] = $entity;
  $entity = EntityTestMul::create();
  $entity->field_data_test->target_id = $referenced_entity
    ->id();
  $entity
    ->save();
  $this
    ->assertEquals($referenced_entity
    ->id(), $entity->field_data_test[0]->entity
    ->id());
  $this->entities[] = $entity;
  Views::viewsData()
    ->clear();

  // Check the generated views data.
  $views_data = Views::viewsData()
    ->get('entity_test_mul__field_data_test');
  $this
    ->assertEquals('standard', $views_data['field_data_test']['relationship']['id']);
  $this
    ->assertEquals('entity_test', $views_data['field_data_test']['relationship']['base']);
  $this
    ->assertEquals('id', $views_data['field_data_test']['relationship']['base field']);
  $this
    ->assertEquals('field_data_test_target_id', $views_data['field_data_test']['relationship']['relationship field']);
  $this
    ->assertEquals('entity_test', $views_data['field_data_test']['relationship']['entity type']);

  // Check the backwards reference.
  $views_data = Views::viewsData()
    ->get('entity_test');
  $this
    ->assertEquals('entity_reverse', $views_data['reverse__entity_test_mul__field_data_test']['relationship']['id']);
  $this
    ->assertEquals('entity_test_mul_property_data', $views_data['reverse__entity_test_mul__field_data_test']['relationship']['base']);
  $this
    ->assertEquals('id', $views_data['reverse__entity_test_mul__field_data_test']['relationship']['base field']);
  $this
    ->assertEquals('entity_test_mul__field_data_test', $views_data['reverse__entity_test_mul__field_data_test']['relationship']['field table']);
  $this
    ->assertEquals('field_data_test_target_id', $views_data['reverse__entity_test_mul__field_data_test']['relationship']['field field']);
  $this
    ->assertEquals('field_data_test', $views_data['reverse__entity_test_mul__field_data_test']['relationship']['field_name']);
  $this
    ->assertEquals('entity_test_mul', $views_data['reverse__entity_test_mul__field_data_test']['relationship']['entity_type']);
  $this
    ->assertEquals([
    'field' => 'deleted',
    'value' => 0,
    'numeric' => TRUE,
  ], $views_data['reverse__entity_test_mul__field_data_test']['relationship']['join_extra'][0]);

  // Check an actual test view.
  $view = Views::getView('test_entity_reference_entity_test_mul_view');
  $this
    ->executeView($view);

  /** @var \Drupal\views\ResultRow $row */
  foreach ($view->result as $index => $row) {

    // Check that the actual ID of the entity is the expected one.
    $this
      ->assertEquals($this->entities[$index]
      ->id(), $row->id);

    // Also check that we have the correct result entity.
    $this
      ->assertEquals($this->entities[$index]
      ->id(), $row->_entity
      ->id());

    // Test the forward relationship.
    $this
      ->assertEquals(1, $row->entity_test_entity_test_mul__field_data_test_id);

    // Test that the correct relationship entity is on the row.
    $this
      ->assertEquals(1, $row->_relationship_entities['field_data_test']
      ->id());
    $this
      ->assertEquals('entity_test', $row->_relationship_entities['field_data_test']
      ->bundle());
  }

  // Check the backwards reference view.
  $view = Views::getView('test_entity_reference_reverse_entity_test_mul_view');
  $this
    ->executeView($view);

  /** @var \Drupal\views\ResultRow $row */
  foreach ($view->result as $index => $row) {
    $this
      ->assertEquals(1, $row->id);
    $this
      ->assertEquals(1, $row->_entity
      ->id());

    // Test the backwards relationship.
    $this
      ->assertEquals($this->entities[$index]
      ->id(), $row->field_data_test_entity_test_id);

    // Test that the correct relationship entity is on the row.
    $this
      ->assertEquals($this->entities[$index]
      ->id(), $row->_relationship_entities['reverse__entity_test_mul__field_data_test']
      ->id());
    $this
      ->assertEquals('entity_test_mul', $row->_relationship_entities['reverse__entity_test_mul__field_data_test']
      ->bundle());
  }
}