You are here

public function FieldUITest::testBooleanFilterHandler in Drupal 8

Same name and namespace in other branches
  1. 9 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
    ->drupalPostForm($url, [
    'name[node__' . $field_name . '.' . $field_name . '_value]' => TRUE,
  ], t('Add and configure @handler', [
    '@handler' => t('filter criteria'),
  ]));
  $this
    ->assertSession()
    ->statusCodeEquals(200);

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

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