You are here

public function EntityReferenceRelationshipTest::testNoDataTableRelationship in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/field/src/Tests/EntityReference/Views/EntityReferenceRelationshipTest.php \Drupal\field\Tests\EntityReference\Views\EntityReferenceRelationshipTest::testNoDataTableRelationship()

Tests using the views relationship.

File

core/modules/field/src/Tests/EntityReference/Views/EntityReferenceRelationshipTest.php, line 76
Contains \Drupal\field\Tests\EntityReference\Views\EntityReferenceRelationshipTest.

Class

EntityReferenceRelationshipTest
Tests entity reference relationship data.

Namespace

Drupal\field\Tests\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
    ->assertEqual($entity->field_test_data[0]->entity
    ->id(), $referenced_entity
    ->id());
  $this->entities[] = $entity;
  $entity = EntityTest::create();
  $entity->field_test_data->target_id = $referenced_entity
    ->id();
  $entity
    ->save();
  $this
    ->assertEqual($entity->field_test_data[0]->entity
    ->id(), $referenced_entity
    ->id());
  $this->entities[] = $entity;
  Views::viewsData()
    ->clear();

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

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

  // 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
      ->assertEqual($row->id, $this->entities[$index]
      ->id());

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

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

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

  // 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
      ->assertEqual($row->id, 1);
    $this
      ->assertEqual($row->_entity
      ->id(), 1);

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

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