You are here

public function Select2BoxesTests::testUnlimitedPreloading in Select2 Boxes 8

Test unlimited preloading behavior.

File

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

Class

Select2BoxesTests
Class for testing Select2Boxes.

Namespace

Drupal\Tests\select2boxes\FunctionalJavascript

Code

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

  // Firstly generate fake vocabulary terms.
  $this
    ->generateDummyTerms('tags', 10);

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

  // Set multiple widget for the Tags field.
  $assert_session
    ->elementExists('xpath', '//select[@name="fields[field_tags][type]"]')
    ->setValue(static::$pluginIds[2]);
  $assert_session
    ->assertWaitOnAjaxRequest();

  // Check for the summary text about preloading option.
  $assert_session
    ->pageTextContains('Preloading disabled');

  // Open the settings form for the Tags field.
  $this
    ->click('input[name="field_tags_settings_edit"]');
  $assert_session
    ->assertWaitOnAjaxRequest();

  // Enable preloading via checking the checkbox field.
  $this
    ->click('input[name="fields[field_tags][settings_edit_form][third_party_settings][select2boxes][enable_preload]"]');
  $assert_session
    ->assertWaitOnAjaxRequest();

  // Do not specify the preload count value!
  // Submit the settings form.
  $this
    ->click('input[name="field_tags_plugin_settings_update"]');
  $assert_session
    ->assertWaitOnAjaxRequest();

  // Check for summary text updates,
  // according to the NON-specified number of preload entries.
  $assert_session
    ->pageTextContains('Number of preloaded entries: all');

  // Submit the entity form display settings.
  $page
    ->pressButton('Save');

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

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

  // Trigger the opening dropdown via click on the search input field.
  $this
    ->click('input[class="select2-search__field"]');
  $assert_session
    ->assertWaitOnAjaxRequest();

  // Find the list of results element on the page.
  $fake_select = $assert_session
    ->elementExists('css', '#select2-edit-field-tags-results');

  // Check if the number of results is equals to the 10 rows (as was
  // generated).
  $this
    ->assertCount(10, $fake_select
    ->findAll('xpath', '//li'));
}