You are here

public function OgStandardReferenceRelationshipTest::testDataTableRelationship in Organic groups 8

Tests views data generated for relationship.

See also

entity_reference_field_views_data()

File

tests/src/Kernel/EntityReference/Views/OgStandardReferenceRelationshipTest.php, line 178

Class

OgStandardReferenceRelationshipTest
Tests OG standard reference relationship data.

Namespace

Drupal\Tests\og\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
    ->assertEqual($entity->field_data_test[0]->entity
    ->id(), $referenced_entity
    ->id());
  $this->entities[] = $entity;
  $entity = EntityTestMul::create();
  $entity->field_data_test->target_id = $referenced_entity
    ->id();
  $entity
    ->save();
  $this
    ->assertEqual($entity->field_data_test[0]->entity
    ->id(), $referenced_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
    ->assertEqual($views_data['field_data_test']['relationship']['id'], 'standard');
  $this
    ->assertEqual($views_data['field_data_test']['relationship']['base'], 'entity_test');
  $this
    ->assertEqual($views_data['field_data_test']['relationship']['base field'], 'id');
  $this
    ->assertEqual($views_data['field_data_test']['relationship']['relationship field'], 'field_data_test_target_id');
  $this
    ->assertEqual($views_data['field_data_test']['relationship']['entity type'], 'entity_test');

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

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

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

  // Check the backwards reference view.
  $view = Views::getView('test_og_standard_reference_reverse_entity_test_mul_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_data_test_entity_test_id, $this->entities[$index]
      ->id());

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