You are here

public function DynamicEntityReferenceRelationshipTest::testStringIdReverseRelationship in Dynamic Entity Reference 8.2

Tests views relationship with string id entity with string id reference.

File

tests/src/Kernel/Views/DynamicEntityReferenceRelationshipTest.php, line 441

Class

DynamicEntityReferenceRelationshipTest
Tests dynamic entity reference relationship data.

Namespace

Drupal\Tests\dynamic_entity_reference\Kernel\Views

Code

public function testStringIdReverseRelationship() {
  \Drupal::state()
    ->set('dynamic_entity_reference_test_views.entity_test_string_id.view', TRUE);
  \Drupal::entityTypeManager()
    ->clearCachedDefinitions();
  $this
    ->installEntitySchema('entity_test_string_id');
  $field_storage = FieldStorageConfig::create([
    'entity_type' => 'entity_test_string_id',
    'field_name' => 'field_test_string_id',
    'type' => 'dynamic_entity_reference',
    'settings' => [
      'exclude_entity_types' => FALSE,
      'entity_type_ids' => [
        'entity_test',
        'entity_test_mul',
        'entity_test_string_id',
      ],
    ],
    'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
  ]);
  $field_storage
    ->save();
  $field = FieldConfig::create([
    'entity_type' => 'entity_test_string_id',
    'field_name' => 'field_test_string_id',
    'bundle' => 'entity_test_mul',
    'settings' => [],
  ]);
  $field
    ->save();

  // Clears views data caches.
  Views::viewsData()
    ->clear();

  // Check just the generated views data.
  $views_data_field_test = Views::viewsData()
    ->get('entity_test_string_id__field_test_string_id');

  // ViewsData for string id test entity referenced from field_test_string_id.
  $this
    ->assertEquals($views_data_field_test['entity_test_string_id__field_test_string_id']['relationship']['id'], 'standard');
  $this
    ->assertEquals($views_data_field_test['entity_test_string_id__field_test_string_id']['relationship']['base'], 'entity_test_string');
  $this
    ->assertEquals($views_data_field_test['entity_test_string_id__field_test_string_id']['relationship']['entity type'], 'entity_test_string_id');
  $this
    ->assertEquals($views_data_field_test['entity_test_string_id__field_test_string_id']['relationship']['base field'], 'id');
  $this
    ->assertEquals($views_data_field_test['entity_test_string_id__field_test_string_id']['relationship']['relationship field'], 'field_test_string_id_target_id');
  $this
    ->assertEquals($views_data_field_test['entity_test_string_id__field_test_string_id']['relationship']['extra'][0]['left_field'], 'field_test_string_id_target_type');
  $this
    ->assertEquals($views_data_field_test['entity_test_string_id__field_test_string_id']['relationship']['extra'][0]['value'], 'entity_test_string_id');
  $this
    ->assertEquals($views_data_field_test['field_test_string_id_target_id']['argument']['id'], 'string');
  $this
    ->assertEquals($views_data_field_test['field_test_string_id_target_id']['filter']['id'], 'string');

  // Backwards reference for string id test entity using field_test_string_id.
  $views_data_entity_test = Views::viewsData()
    ->get('entity_test_string');
  $this
    ->assertEquals($views_data_entity_test['reverse__entity_test_string_id__field_test_string_id']['relationship']['id'], 'entity_reverse');
  $this
    ->assertEquals($views_data_entity_test['reverse__entity_test_string_id__field_test_string_id']['relationship']['base'], 'entity_test_string');
  $this
    ->assertEquals($views_data_entity_test['reverse__entity_test_string_id__field_test_string_id']['relationship']['base field'], 'id');
  $this
    ->assertEquals($views_data_entity_test['reverse__entity_test_string_id__field_test_string_id']['relationship']['field table'], 'entity_test_string_id__field_test_string_id');
  $this
    ->assertEquals($views_data_entity_test['reverse__entity_test_string_id__field_test_string_id']['relationship']['field field'], 'field_test_string_id_target_id');
  $this
    ->assertEquals($views_data_entity_test['reverse__entity_test_string_id__field_test_string_id']['relationship']['join_extra'][0]['field'], 'field_test_string_id_target_type');
  $this
    ->assertEquals($views_data_entity_test['reverse__entity_test_string_id__field_test_string_id']['relationship']['join_extra'][0]['value'], 'entity_test_string_id');
  $this
    ->assertEquals($views_data_entity_test['reverse__entity_test_string_id__field_test_string_id']['relationship']['join_extra'][1]['field'], 'deleted');
  $this
    ->assertEquals($views_data_entity_test['reverse__entity_test_string_id__field_test_string_id']['relationship']['join_extra'][1]['value'], 0);
  $this
    ->assertEquals($views_data_entity_test['reverse__entity_test_string_id__field_test_string_id']['relationship']['join_extra'][1]['numeric'], TRUE);

  // Check just the generated views data.
  $views_data_field_test = Views::viewsData()
    ->get('entity_test');

  // Backwards reference for string id test entity using field_test_string_id.
  $this
    ->assertEquals($views_data_entity_test['reverse__entity_test_string_id__field_test_string_id']['relationship']['id'], 'entity_reverse');
  $this
    ->assertEquals($views_data_entity_test['reverse__entity_test_string_id__field_test_string_id']['relationship']['base'], 'entity_test_string');
  $this
    ->assertEquals($views_data_entity_test['reverse__entity_test_string_id__field_test_string_id']['relationship']['base field'], 'id');
  $this
    ->assertEquals($views_data_entity_test['reverse__entity_test_string_id__field_test_string_id']['relationship']['field table'], 'entity_test_string_id__field_test_string_id');
  $this
    ->assertEquals($views_data_entity_test['reverse__entity_test_string_id__field_test_string_id']['relationship']['field field'], 'field_test_string_id_target_id');
  $this
    ->assertEquals($views_data_entity_test['reverse__entity_test_string_id__field_test_string_id']['relationship']['join_extra'][0]['field'], 'field_test_string_id_target_type');
  $this
    ->assertEquals($views_data_entity_test['reverse__entity_test_string_id__field_test_string_id']['relationship']['join_extra'][0]['value'], 'entity_test_string_id');
  $this
    ->assertEquals($views_data_entity_test['reverse__entity_test_string_id__field_test_string_id']['relationship']['join_extra'][1]['field'], 'deleted');
  $this
    ->assertEquals($views_data_entity_test['reverse__entity_test_string_id__field_test_string_id']['relationship']['join_extra'][1]['value'], 0);
  $this
    ->assertEquals($views_data_entity_test['reverse__entity_test_string_id__field_test_string_id']['relationship']['join_extra'][1]['numeric'], TRUE);

  // Backwards reference for string id test entity using field_test_string_id.
  $views_data_entity_test = Views::viewsData()
    ->get('entity_test_mul_property_data');
  $this
    ->assertEquals($views_data_entity_test['reverse__entity_test_string_id__field_test_string_id']['relationship']['id'], 'entity_reverse');
  $this
    ->assertEquals($views_data_entity_test['reverse__entity_test_string_id__field_test_string_id']['relationship']['base'], 'entity_test_string');
  $this
    ->assertEquals($views_data_entity_test['reverse__entity_test_string_id__field_test_string_id']['relationship']['base field'], 'id');
  $this
    ->assertEquals($views_data_entity_test['reverse__entity_test_string_id__field_test_string_id']['relationship']['field table'], 'entity_test_string_id__field_test_string_id');
  $this
    ->assertEquals($views_data_entity_test['reverse__entity_test_string_id__field_test_string_id']['relationship']['field field'], 'field_test_string_id_target_id_int');
  $this
    ->assertEquals($views_data_entity_test['reverse__entity_test_string_id__field_test_string_id']['relationship']['join_extra'][0]['field'], 'field_test_string_id_target_type');
  $this
    ->assertEquals($views_data_entity_test['reverse__entity_test_string_id__field_test_string_id']['relationship']['join_extra'][0]['value'], 'entity_test_mul');
  $this
    ->assertEquals($views_data_entity_test['reverse__entity_test_string_id__field_test_string_id']['relationship']['join_extra'][1]['field'], 'deleted');
  $this
    ->assertEquals($views_data_entity_test['reverse__entity_test_string_id__field_test_string_id']['relationship']['join_extra'][1]['value'], 0);
  $this
    ->assertEquals($views_data_entity_test['reverse__entity_test_string_id__field_test_string_id']['relationship']['join_extra'][1]['numeric'], TRUE);
}