You are here

public function WidgetJSTest::testCheckboxWidget in Facets 8

Tests checkbox widget.

File

tests/src/FunctionalJavascript/WidgetJSTest.php, line 103

Class

WidgetJSTest
Tests for the JS that transforms widgets into form elements.

Namespace

Drupal\Tests\facets\FunctionalJavascript

Code

public function testCheckboxWidget() {
  $facet_storage = \Drupal::entityTypeManager()
    ->getStorage('facets_facet');
  $id = 'llama';

  // Create and save a facet with a checkbox widget on the 'type' field.
  $facet_storage
    ->create([
    'id' => $id,
    'name' => strtoupper($id),
    'url_alias' => $id,
    'facet_source_id' => 'search_api:views_page__search_api_test_view__page_1',
    'field_identifier' => 'type',
    'empty_behavior' => [
      'behavior' => 'none',
    ],
    'widget' => [
      'type' => 'checkbox',
      'config' => [
        'show_numbers' => TRUE,
      ],
    ],
    'processor_configs' => [
      'url_processor_handler' => [
        'processor_id' => 'url_processor_handler',
        'weights' => [
          'pre_query' => -10,
          'build' => -10,
        ],
        'settings' => [],
      ],
    ],
  ])
    ->save();
  $this
    ->createBlock($id);

  // Go to the views page.
  $this
    ->drupalGet('search-api-test-fulltext');

  // Make sure the block is shown on the page.
  $page = $this
    ->getSession()
    ->getPage();
  $block = $page
    ->findById('block-llama-block');
  $this
    ->assertTrue($block
    ->isVisible());

  // The checkboxes should be wrapped in a container with a CSS class that
  // correctly identifies the widget type.
  $this
    ->assertCount(1, $block
    ->findAll('css', 'div.facets-widget-checkbox ul'));

  // The checkboxes should be wrapped in a list element that has the expected
  // CSS classes to identify it as well as the data attributes that enable the
  // JS functionality.
  $this
    ->assertCount(1, $block
    ->findAll('css', 'ul.facet-inactive.item-list__checkbox.js-facets-widget.js-facets-checkbox-links'));
  $this
    ->assertCount(1, $block
    ->findAll('css', 'ul[data-drupal-facet-id="llama"]'));
  $this
    ->assertCount(1, $block
    ->findAll('css', 'ul[data-drupal-facet-alias="llama"]'));

  // There should be two list items that can be identified by CSS class.
  $list_items = $block
    ->findAll('css', 'ul li.facet-item');
  $this
    ->assertCount(2, $list_items);

  // The list items should contain a checkbox, a label and a hidden link that
  // leads to the updated search results. None of the checkboxes should be
  // checked.
  $expected = [
    [
      'item',
      3,
      base_path() . 'search-api-test-fulltext?f%5B0%5D=llama%3Aitem',
      FALSE,
    ],
    [
      'article',
      2,
      base_path() . 'search-api-test-fulltext?f%5B0%5D=llama%3Aarticle',
      FALSE,
    ],
  ];
  $this
    ->assertListItems($expected, $list_items);

  // Checking one of the checkboxes should cause a redirect to a page with
  // updated search results.
  $checkbox = $page
    ->findField('item (3)');
  $checkbox
    ->click();
  $current_url = $this
    ->getSession()
    ->getCurrentUrl();
  $this
    ->assertStringContainsString('search-api-test-fulltext?f%5B0%5D=llama%3Aitem', $current_url);

  // Now the chosen keyword should be checked and the hidden links should be
  // updated.
  $expected = [
    [
      'item',
      3,
      base_path() . 'search-api-test-fulltext',
      TRUE,
    ],
    [
      'article',
      2,
      base_path() . 'search-api-test-fulltext?f%5B0%5D=llama%3Aarticle',
      FALSE,
    ],
  ];
  $this
    ->assertListItems($expected, $block
    ->findAll('css', 'ul li.facet-item'));

  // Unchecking a checkbox should remove the keyword from the search.
  $checkbox = $page
    ->findField('item (3)');
  $checkbox
    ->click();
  $current_url = $this
    ->getSession()
    ->getCurrentUrl();
  $this
    ->assertStringContainsString('search-api-test-fulltext', $current_url);
  $expected = [
    [
      'item',
      3,
      base_path() . 'search-api-test-fulltext?f%5B0%5D=llama%3Aitem',
      FALSE,
    ],
    [
      'article',
      2,
      base_path() . 'search-api-test-fulltext?f%5B0%5D=llama%3Aarticle',
      FALSE,
    ],
  ];
  $this
    ->assertListItems($expected, $block
    ->findAll('css', 'ul li.facet-item'));
}