You are here

public function Select2BoxesTests::testMultipleWidget in Select2 Boxes 8

Test multiple widget for entity reference fields.

File

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

Class

Select2BoxesTests
Class for testing Select2Boxes.

Namespace

Drupal\Tests\select2boxes\FunctionalJavascript

Code

public function testMultipleWidget() {
  $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 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 if the submission has been successful.
  $this
    ->assertSession()
    ->pageTextContains('Your settings have been saved.');

  // Now let's check the node creation page.
  $this
    ->drupalGet('node/add/select2boxes_test_content_type');
  $select = $assert_session
    ->selectExists('edit-field-tags');

  // Check if all required html attributes are exist
  // for the entity reference field.
  $assert_session
    ->elementAttributeExists('css', '#edit-field-tags', 'data-jquery-once-autocomplete');
  $assert_session
    ->elementAttributeExists('css', '#edit-field-tags', 'data-select2-multiple');
  $assert_session
    ->elementAttributeExists('css', '#edit-field-tags', 'data-autocomplete-path');
  $assert_session
    ->elementAttributeExists('css', '#edit-field-tags', 'data-field-name');
  $this
    ->assertTrue($select
    ->hasClass('select2-widget'));
  $this
    ->assertTrue($select
    ->hasClass('select2-boxes-widget'));

  // Generate dummy terms.
  $terms = $this
    ->generateDummyTerms('tags', 10);

  // Generate one node for testing.
  $nodes = $this
    ->generateDummyContent(1);

  /** @var \Drupal\node\Entity\Node $node */
  $node = reset($nodes);
  $node
    ->set('field_tags', $terms[random_int(0, 9)]
    ->id())
    ->save();

  // Go to the node's edit page.
  $this
    ->drupalGet('node/' . $node
    ->id() . '/edit');

  // Check that the select element exists.
  $select = $assert_session
    ->selectExists('edit-field-tags');

  // Check that only the selected tag item has been rendered as a dropdown
  // option.
  $this
    ->assertCount(1, $this
    ->getOptions($select));
}