You are here

public function EntityReferenceRelationshipTest::testNoDataTableRelationship 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::testNoDataTableRelationship()
  2. 9 core/modules/field/tests/src/Kernel/EntityReference/Views/EntityReferenceRelationshipTest.php \Drupal\Tests\field\Kernel\EntityReference\Views\EntityReferenceRelationshipTest::testNoDataTableRelationship()

Tests using the views relationship.

File

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

Class

EntityReferenceRelationshipTest
Tests entity reference relationship data.

Namespace

Drupal\Tests\field\Kernel\EntityReference\Views

Code

public function testNoDataTableRelationship() {

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

  // Check the generated views data.
  $views_data = Views::viewsData()
    ->get('entity_test__field_test_data');
  $this
    ->assertEquals('standard', $views_data['field_test_data']['relationship']['id']);
  $this
    ->assertEquals('entity_test_mul_property_data', $views_data['field_test_data']['relationship']['base']);
  $this
    ->assertEquals('id', $views_data['field_test_data']['relationship']['base field']);
  $this
    ->assertEquals('field_test_data_target_id', $views_data['field_test_data']['relationship']['relationship field']);
  $this
    ->assertEquals('entity_test_mul', $views_data['field_test_data']['relationship']['entity type']);

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

  // Check an actual test view.
  $view = Views::getView('test_entity_reference_entity_test_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_mul_property_data_entity_test__field_test_data_i);

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

  // Check the backwards reference view.
  $view = Views::getView('test_entity_reference_reverse_entity_test_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_test_data_entity_test_mul_property_data_id);

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