function ExposedFormCheckboxesTest::testExposedIsAllOfFilter

Same name and namespace in other branches
  1. 11.x core/modules/views/tests/src/Functional/Plugin/ExposedFormCheckboxesTest.php \Drupal\Tests\views\Functional\Plugin\ExposedFormCheckboxesTest::testExposedIsAllOfFilter()
  2. 10 core/modules/views/tests/src/Functional/Plugin/ExposedFormCheckboxesTest.php \Drupal\Tests\views\Functional\Plugin\ExposedFormCheckboxesTest::testExposedIsAllOfFilter()
  3. 8.9.x core/modules/views/tests/src/Functional/Plugin/ExposedFormCheckboxesTest.php \Drupal\Tests\views\Functional\Plugin\ExposedFormCheckboxesTest::testExposedIsAllOfFilter()
  4. main core/modules/views/tests/src/Functional/Plugin/ExposedFormCheckboxesTest.php \Drupal\Tests\views\Functional\Plugin\ExposedFormCheckboxesTest::testExposedIsAllOfFilter()

Tests that "is all of" filters work with checkboxes.

File

core/modules/views/tests/src/Functional/Plugin/ExposedFormCheckboxesTest.php, line 118

Class

ExposedFormCheckboxesTest
Tests exposed forms functionality.

Namespace

Drupal\Tests\views\Functional\Plugin

Code

public function testExposedIsAllOfFilter() {
  foreach ([
    'Term 1',
    'Term 2',
    'Term 3',
  ] as $term_name) {
    // Add a few terms to the new vocabulary.
    $term = Term::create([
      'name' => $term_name,
      'vid' => $this->vocabulary
        ->id(),
    ]);
    $term->save();
    $this->terms[] = $term;
  }
  // Create a field.
  $field_name = mb_strtolower($this->randomMachineName());
  $handler_settings = [
    'target_bundles' => [
      $this->vocabulary
        ->id() => $this->vocabulary
        ->id(),
    ],
    'auto_create' => FALSE,
  ];
  $this->createEntityReferenceField('node', 'article', $field_name, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
  // Add some test nodes.
  $this->createNode([
    'type' => 'article',
    $field_name => [
      $this->terms[0]
        ->id(),
      $this->terms[1]
        ->id(),
    ],
  ]);
  $this->createNode([
    'type' => 'article',
    $field_name => [
      $this->terms[0]
        ->id(),
      $this->terms[2]
        ->id(),
    ],
  ]);
  // Use a test theme to convert multi-select elements into checkboxes.
  \Drupal::service('theme_installer')->install([
    'views_test_checkboxes_theme',
  ]);
  $this->config('system.theme')
    ->set('default', 'views_test_checkboxes_theme')
    ->save();
  $this->drupalGet('test_exposed_form_checkboxes');
  // Ensure that all results are displayed.
  // All rows are displayed by default on the first page when no options are
  // checked.
  $this->assertSession()
    ->elementsCount('xpath', "//div[contains(@class, 'views-row')]", 8);
  $this->assertSession()
    ->pageTextNotContains('An illegal choice has been detected. Please contact the site administrator.');
  // Select one option and ensure we still have results.
  $tid = $this->terms[0]
    ->id();
  $this->submitForm([
    "tid[{$tid}]" => $tid,
  ], 'Apply');
  // Ensure only nodes tagged with $tid are displayed.
  $this->assertSession()
    ->elementsCount('xpath', "//div[contains(@class, 'views-row')]", 2);
  $this->assertSession()
    ->pageTextNotContains('An illegal choice has been detected. Please contact the site administrator.');
}

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