You are here

public function Select2BoxesTests::testPreloading in Select2 Boxes 8

Test preloading functionality for the multiple entity reference widgets.

File

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

Class

Select2BoxesTests
Class for testing Select2Boxes.

Namespace

Drupal\Tests\select2boxes\FunctionalJavascript

Code

public function testPreloading() {
  $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();
  $page
    ->pressButton('Save');

  // 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();

  // Set 5 rows to be preloaded.
  $page
    ->fillField('fields[field_tags][settings_edit_form][third_party_settings][select2boxes][preload_count]', '5');

  // Submit the settings form.
  $this
    ->click('input[name="field_tags_plugin_settings_update"]');
  $assert_session
    ->assertWaitOnAjaxRequest();

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

  // 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 5 rows
  // (as was specified in the widget settings).
  $this
    ->assertCount(5, $fake_select
    ->findAll('xpath', '//li'));
}