You are here

public function RadioButtonsFilterWidgetKernelTest::testExposedCheckboxes in Better Exposed Filters 8.4

Same name and namespace in other branches
  1. 8.5 tests/src/Kernel/Plugin/filter/RadioButtonsFilterWidgetKernelTest.php \Drupal\Tests\better_exposed_filters\Kernel\Plugin\filter\RadioButtonsFilterWidgetKernelTest::testExposedCheckboxes()

Tests the exposed checkboxes filter widget.

File

tests/src/Kernel/Plugin/filter/RadioButtonsFilterWidgetKernelTest.php, line 25

Class

RadioButtonsFilterWidgetKernelTest
Tests the radio buttons/checkboxes filter widget (i.e. "bef").

Namespace

Drupal\Tests\better_exposed_filters\Kernel\Plugin\filter

Code

public function testExposedCheckboxes() {
  $view = Views::getView('bef_test');
  $display =& $view->storage
    ->getDisplay('default');

  // Ensure our filter "field_bef_integer" allows multiple values.
  $display['display_options']['filters']['field_bef_integer_value']['expose']['multiple'] = TRUE;

  // Ensure our filter "term_node_tid_depth" has show hierarchy enabled.
  $display['display_options']['filters']['term_node_tid_depth']['expose']['multiple'] = TRUE;
  $display['display_options']['filters']['term_node_tid_depth']['hierarchy'] = TRUE;

  // Change exposed filter "field_bef_integer" and "term_node_tid_depth" to
  // checkboxes (i.e. 'bef').
  $this
    ->setBetterExposedOptions($view, [
    'filter' => [
      'field_bef_integer_value' => [
        'plugin_id' => 'bef',
      ],
      'term_node_tid_depth' => [
        'plugin_id' => 'bef',
      ],
    ],
  ]);

  // Render the exposed form.
  $this
    ->renderExposedForm($view);

  // Check our "FIELD_BEF_INTEGER" filter is rendered as checkboxes.
  $actual = $this
    ->xpath('//form//input[@type="checkbox" and starts-with(@name, "field_bef_integer_value")]');
  $this
    ->assertCount(5, $actual, 'Exposed filter "FIELD_BEF_INTEGER" has correct number of exposed checkboxes.');

  // Check our "TERM_NODE_TID_DEPTH" filter is rendered as nested checkboxes.
  $actual = $this
    ->xpath("//form//div[contains(concat(' ',normalize-space(@class),' '),' bef-nested ')]");
  $this
    ->assertCount(1, $actual, 'Exposed filter "TERM_NODE_TID_DEPTH" has bef-nested class');
  $actual = $this
    ->xpath('//form//div[@id="edit-term-node-tid-depth--2"]/div/ul/li/div/input[@type="checkbox" and starts-with(@name, "term_node_tid_depth")]');
  $this
    ->assertCount(3, $actual, 'Exposed filter "TERM_NODE_TID_DEPTH" has correct number of exposed top-level checkboxes.');
  $actual = $this
    ->xpath('//form//div[@id="edit-term-node-tid-depth--2"]/div/ul/li/ul/li/div/input[@type="checkbox" and starts-with(@name, "term_node_tid_depth")]');
  $this
    ->assertCount(5, $actual, 'Exposed filter "TERM_NODE_TID_DEPTH" has correct number of exposed second-level checkboxes.');
  $actual = $this
    ->xpath('//form//div[@id="edit-term-node-tid-depth--2"]/div/ul/li/ul/li/ul/li/div/input[@type="checkbox" and starts-with(@name, "term_node_tid_depth")]');
  $this
    ->assertCount(14, $actual, 'Exposed filter "TERM_NODE_TID_DEPTH" has correct number of exposed third-level checkboxes.');
  $view
    ->destroy();
}