You are here

public function FieldUITest::testBooleanFilterHandler in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/field/tests/src/Functional/Views/FieldUITest.php \Drupal\Tests\field\Functional\Views\FieldUITest::testBooleanFilterHandler()

Tests adding a boolean field filter handler.

File

core/modules/field/tests/src/Functional/Views/FieldUITest.php, line 130

Class

FieldUITest
Tests the UI of the field field handler.

Namespace

Drupal\Tests\field\Functional\Views

Code

public function testBooleanFilterHandler() {

  // Create a boolean field.
  $field_name = 'field_boolean';
  $field_storage = FieldStorageConfig::create([
    'field_name' => $field_name,
    'entity_type' => 'node',
    'type' => 'boolean',
  ]);
  $field_storage
    ->save();
  $field = FieldConfig::create([
    'field_storage' => $field_storage,
    'bundle' => 'page',
  ]);
  $field
    ->save();
  $url = "admin/structure/views/nojs/add-handler/test_view_fieldapi/default/filter";
  $this
    ->drupalGet($url);
  $this
    ->submitForm([
    'name[node__' . $field_name . '.' . $field_name . '_value]' => TRUE,
  ], 'Add and configure filter criteria');
  $this
    ->assertSession()
    ->statusCodeEquals(200);

  // Verify that using a boolean field as a filter also results in using the
  // boolean plugin.
  $this
    ->assertSession()
    ->elementTextEquals('xpath', '//label[@for="edit-options-value-1"]', 'True');
  $this
    ->assertSession()
    ->elementTextEquals('xpath', '//label[@for="edit-options-value-0"]', 'False');

  // Expose the filter and see if the 'Any' option is added and if we can save
  // it.
  $this
    ->submitForm([], 'Expose filter');
  $this
    ->assertSession()
    ->elementTextEquals('xpath', '//label[@for="edit-options-value-all"]', '- Any -');
  $this
    ->submitForm([
    'options[value]' => 'All',
    'options[expose][required]' => FALSE,
  ], 'Apply');
  $this
    ->submitForm([], 'Save');
  $this
    ->drupalGet('/admin/structure/views/nojs/handler/test_view_fieldapi/default/filter/field_boolean_value');
  $this
    ->assertSession()
    ->checkboxChecked('edit-options-value-all');
}