You are here

public function DisplayEntityReferenceTest::testEntityReferenceDisplay in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/views/tests/src/Functional/Plugin/DisplayEntityReferenceTest.php \Drupal\Tests\views\Functional\Plugin\DisplayEntityReferenceTest::testEntityReferenceDisplay()
  2. 10 core/modules/views/tests/src/Functional/Plugin/DisplayEntityReferenceTest.php \Drupal\Tests\views\Functional\Plugin\DisplayEntityReferenceTest::testEntityReferenceDisplay()

Tests the entity reference display plugin.

File

core/modules/views/tests/src/Functional/Plugin/DisplayEntityReferenceTest.php, line 132

Class

DisplayEntityReferenceTest
Tests the entity reference display plugin.

Namespace

Drupal\Tests\views\Functional\Plugin

Code

public function testEntityReferenceDisplay() {

  // Test that the 'title' settings are not shown.
  $this
    ->drupalGet('admin/structure/views/view/test_display_entity_reference/edit/entity_reference_1');
  $this
    ->assertSession()
    ->linkByHrefNotExists('admin/structure/views/nojs/display/test_display_entity_reference/entity_reference_1/title');

  // Add the new field to the fields.
  $this
    ->drupalPostForm('admin/structure/views/nojs/add-handler/test_display_entity_reference/default/field', [
    'name[entity_test__' . $this->fieldName . '.' . $this->fieldName . ']' => TRUE,
  ], t('Add and configure fields'));
  $this
    ->drupalPostForm(NULL, [], t('Apply'));

  // Test that the right fields are shown on the display settings form.
  $this
    ->drupalGet('admin/structure/views/nojs/display/test_display_entity_reference/entity_reference_1/style_options');
  $this
    ->assertText('Test entity: Name');
  $this
    ->assertText('Test entity: ' . $this->field
    ->label());

  // Add the new field to the search fields.
  $this
    ->drupalPostForm(NULL, [
    'style_options[search_fields][' . $this->fieldName . ']' => $this->fieldName,
  ], t('Apply'));
  $this
    ->drupalPostForm(NULL, [], t('Save'));
  $view = Views::getView('test_display_entity_reference');
  $view
    ->setDisplay('entity_reference_1');

  // Add the required settings to test a search operation.
  $options = [
    'match' => '1',
    'match_operator' => 'CONTAINS',
    'limit' => 0,
    'ids' => NULL,
  ];
  $view->display_handler
    ->setOption('entity_reference_options', $options);
  $this
    ->executeView($view);

  // Test that we have searched in both fields.
  $this
    ->assertCount(2, $view->result, 'Search returned two rows');
  $view
    ->destroy();

  // Test the 'CONTAINS' match_operator.
  $view = Views::getView('test_display_entity_reference');
  $view
    ->setDisplay('entity_reference_1');
  $options = [
    'match' => 'tex',
    'match_operator' => 'CONTAINS',
    'limit' => 0,
    'ids' => NULL,
  ];
  $view->display_handler
    ->setOption('entity_reference_options', $options);
  $this
    ->executeView($view);
  $this
    ->assertCount(13, $view->result, 'Search returned thirteen rows');
  $view
    ->destroy();

  // Test the 'STARTS_WITH' match_operator.
  $view = Views::getView('test_display_entity_reference');
  $view
    ->setDisplay('entity_reference_1');
  $options = [
    'match' => 'tex',
    'match_operator' => 'STARTS_WITH',
    'limit' => 0,
    'ids' => NULL,
  ];
  $view->display_handler
    ->setOption('entity_reference_options', $options);
  $this
    ->executeView($view);
  $this
    ->assertCount(12, $view->result, 'Search returned twelve rows');
  $view
    ->destroy();

  // Test the '=' match_operator.
  $view = Views::getView('test_display_entity_reference');
  $view
    ->setDisplay('entity_reference_1');
  $options = [
    'match' => 'tex',
    'match_operator' => '=',
    'limit' => 0,
    'ids' => NULL,
  ];
  $view->display_handler
    ->setOption('entity_reference_options', $options);
  $this
    ->executeView($view);
  $this
    ->assertCount(2, $view->result, 'Search returned two rows');
  $view
    ->destroy();

  // Add a relationship and a field using that relationship.
  $this
    ->drupalPostForm('admin/structure/views/nojs/add-handler/test_display_entity_reference/default/relationship', [
    'name[entity_test.user_id]' => TRUE,
  ], t('Add and configure relationships'));
  $this
    ->drupalPostForm(NULL, [], t('Apply'));
  $this
    ->drupalPostForm('admin/structure/views/nojs/add-handler/test_display_entity_reference/default/field', [
    'name[users_field_data.uid]' => TRUE,
  ], t('Add and configure fields'));
  $this
    ->drupalPostForm(NULL, [], t('Apply'));

  // Add the new field to the search fields.
  $this
    ->drupalPostForm('admin/structure/views/nojs/display/test_display_entity_reference/entity_reference_1/style_options', [
    'style_options[search_fields][uid]' => 'uid',
  ], t('Apply'));
  $this
    ->drupalPostForm(NULL, [], t('Save'));

  // Test that the search still works with the related field.
  $view = Views::getView('test_display_entity_reference');
  $view
    ->setDisplay('entity_reference_1');

  // Add the required settings to test a search operation.
  $options = [
    'match' => '2',
    'match_operator' => 'CONTAINS',
    'limit' => 0,
    'ids' => NULL,
  ];
  $view->display_handler
    ->setOption('entity_reference_options', $options);
  $this
    ->executeView($view);

  // Run validation when using a relationship to the same base table.
  $this
    ->assertCount(2, $view->result, 'Search returned two rows');
  $view
    ->destroy();
  $this
    ->drupalPostForm('admin/structure/views/nojs/add-handler/test_display_entity_reference/default/relationship', [
    'name[entity_test__field_test_entity_ref_entity_ref.field_test_entity_ref_entity_ref]' => TRUE,
  ], t('Add and configure relationships'));
  $this
    ->drupalPostForm(NULL, [], t('Apply'));
  $this
    ->drupalPostForm(NULL, [], t('Save'));

  // Test that the search still works with the related field.
  $view = Views::getView('test_display_entity_reference');
  $view
    ->setDisplay('entity_reference_1');

  // Add IDs to trigger validation.
  $options = [
    'match' => '1',
    'match_operator' => 'CONTAINS',
    'limit' => 0,
    'ids' => [
      1,
      2,
    ],
  ];
  $view->display_handler
    ->setOption('entity_reference_options', $options);
  $this
    ->executeView($view);
  $this
    ->assertCount(2, $view->result, 'Search returned two rows');

  // Test that the render() return empty array for empty result.
  $view = Views::getView('test_display_entity_reference');
  $view
    ->setDisplay('entity_reference_1');
  $render = $view->display_handler
    ->render();
  $this
    ->assertSame([], $render, 'Render returned empty array');
}