You are here

public function Select2BoxesTests::testLimitedSearch in Select2 Boxes 8

Test limited search option functionality.

File

tests/src/FunctionalJavascript/Select2BoxesTests.php, line 255

Class

Select2BoxesTests
Class for testing Select2Boxes.

Namespace

Drupal\Tests\select2boxes\FunctionalJavascript

Code

public function testLimitedSearch() {
  $page = $this
    ->getSession()
    ->getPage();
  $assert_session = $this
    ->assertSession();

  // Go the the "Manage Form Display" form.
  $this
    ->drupalGet('admin/structure/types/manage/select2boxes_test_content_type/form-display');

  // Set list widget for the Test list field.
  $assert_session
    ->elementExists('xpath', '//select[@name="fields[field_test_list][type]"]')
    ->setValue(static::$pluginIds[0]);
  $assert_session
    ->assertWaitOnAjaxRequest();
  $page
    ->pressButton('Save');

  // Check if the submission has been successful.
  $this
    ->assertSession()
    ->pageTextContains('Your settings have been saved.');

  // Enable limited search option.
  $this
    ->drupalGet('admin/config/user-interface/select2boxes');

  // Specify minimum length search to a BIGGER value
  // than we have in our "Allowed values" settings for the field.
  $edit = [
    'limited_search' => '1',
    'minimum_search_length' => '4',
  ];
  $this
    ->submitForm($edit, 'Save configuration');
  $assert_session
    ->pageTextContains('The configuration options have been saved.');

  // Go to the node's creation form.
  $this
    ->drupalGet('node/add/select2boxes_test_content_type');

  // Trigger opening dropdown.
  $this
    ->getSession()
    ->executeScript("jQuery('select[name=\"field_test_list\"]').select2('open');");

  // Check for NON-existing search input field.
  $search_input = $assert_session
    ->elementExists('xpath', '//span[contains(@class, \'select2-search--dropdown\')]');
  $this
    ->assertNotNull($search_input);
  $this
    ->assertTrue($search_input
    ->hasClass('select2-search--hide'));
  $this
    ->drupalGet('admin/config/user-interface/select2boxes');

  // Specify minimum length search to the SAME value
  // than we have in our "Allowed values" settings for the field.
  $edit = [
    'limited_search' => '1',
    'minimum_search_length' => '3',
  ];
  $this
    ->submitForm($edit, 'Save configuration');
  $assert_session
    ->pageTextContains('The configuration options have been saved.');

  // Go to the node's creation form.
  $this
    ->drupalGet('node/add/select2boxes_test_content_type');

  // Trigger opening dropdown.
  $this
    ->getSession()
    ->executeScript("jQuery('select[name=\"field_test_list\"]').select2('open');");

  // Check for EXISTING search input field.
  $search_input = $assert_session
    ->elementExists('xpath', '//span[contains(@class, \'select2-search--dropdown\')]');
  $this
    ->assertFalse($search_input
    ->hasClass('select2-search--hide'));
}