function FilterEntityReferenceWebTest::testFilterUi

Tests the filter UI.

File

core/modules/views_ui/tests/src/Functional/FilterEntityReferenceWebTest.php, line 55

Class

FilterEntityReferenceWebTest
Tests the entity reference filter UI.

Namespace

Drupal\Tests\views_ui\Functional

Code

public function testFilterUi() : void {
  $this->drupalGet('admin/structure/views/nojs/handler/test_filter_entity_reference/default/filter/field_test_target_id');
  $options = $this->getUiOptions();
  // Should be sorted by title ASC.
  uasort($this->targetEntities, function (EntityInterface $a, EntityInterface $b) {
    return strnatcasecmp($a->getTitle(), $b->getTitle());
  });
  $i = 0;
  foreach ($this->targetEntities as $entity) {
    $this->assertEquals($options[$i]['label'], $entity->label(), "Expected target entity label found for option {$i}");
    $i++;
  }
  // Change the sort field and direction.
  $this->drupalGet('admin/structure/views/nojs/handler-extra/test_filter_entity_reference/default/filter/field_test_target_id');
  $edit = [
    'options[reference_default:node][sort][field]' => 'nid',
    'options[reference_default:node][sort][direction]' => 'DESC',
  ];
  $this->submitForm($edit, 'Apply');
  $this->drupalGet('admin/structure/views/nojs/handler/test_filter_entity_reference/default/filter/field_test_target_id');
  // Items should now be in reverse id order.
  krsort($this->targetEntities);
  $options = $this->getUiOptions();
  $i = 0;
  foreach ($this->targetEntities as $entity) {
    $this->assertEquals($options[$i]['label'], $entity->label(), "Expected target entity label found for option {$i}");
    $i++;
  }
  // Change bundle types.
  $this->drupalGet('admin/structure/views/nojs/handler-extra/test_filter_entity_reference/default/filter/field_test_target_id');
  $edit = [
    "options[reference_default:node][target_bundles][{$this->hostBundle->id()}]" => TRUE,
    "options[reference_default:node][target_bundles][{$this->targetBundle->id()}]" => TRUE,
  ];
  $this->submitForm($edit, 'Apply');
  $this->drupalGet('admin/structure/views/nojs/handler/test_filter_entity_reference/default/filter/field_test_target_id');
  $options = $this->getUiOptions();
  $i = 0;
  foreach ($this->hostEntities + $this->targetEntities as $entity) {
    $this->assertEquals($options[$i]['label'], $entity->label(), "Expected target entity label found for option {$i}");
    $i++;
  }
  // Change Reference method to the test_entity_reference entity reference
  // view and ensure options are updated to only contain the one view result.
  $this->drupalGet('admin/structure/views/nojs/handler-extra/test_filter_entity_reference/default/filter/field_test_target_id');
  $edit = [
    'options[sub_handler]' => 'views',
    'options[reference_views][view][view_and_display]' => 'test_entity_reference:entity_reference',
  ];
  $this->submitForm($edit, 'Apply');
  $this->drupalGet('admin/structure/views/nojs/handler/test_filter_entity_reference/default/filter/field_test_target_id');
  $options = $this->getUiOptions();
  $this->assertCount(1, $options);
  $this->assertEquals('Article 0', $options[0]['label']);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.