You are here

public function Select2BoxesTests::testGlobalSelect2Widgets in Select2 Boxes 8

Test globally enabled select2 widgets.

File

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

Class

Select2BoxesTests
Class for testing Select2Boxes.

Namespace

Drupal\Tests\select2boxes\FunctionalJavascript

Code

public function testGlobalSelect2Widgets() {
  $assert_session = $this
    ->assertSession();

  // Enable select2 widgets globally.
  $this
    ->drupalGet('admin/config/user-interface/select2boxes');
  $this
    ->submitForm([
    'select2_global' => TRUE,
  ], 'Save configuration');
  $assert_session
    ->pageTextContains('The configuration options have been saved.');

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

  // Get all <select> elements on a page.
  $selects = $assert_session
    ->elementExists('xpath', '//select');

  // Check if all of them are having appropriate class and attribute values.
  foreach ($selects as $select) {

    /** @var \Behat\Mink\Element\NodeElement $select */
    $this
      ->assertTrue($select
      ->hasClass('select2-widget'));
    $this
      ->assertTrue($select
      ->hasAttribute('data-jquery-once-autocomplete'));
    $this
      ->assertTrue($select
      ->hasAttribute('data-select2-autocomplete-list-widget'));
  }
  $this
    ->drupalGet('admin/config/user-interface/select2boxes');
  $this
    ->submitForm([
    'select2_global' => FALSE,
  ], 'Save configuration');
  $assert_session
    ->pageTextContains('The configuration options have been saved.');

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

  // Get all <select> elements on a page.
  $selects = $assert_session
    ->elementExists('xpath', '//select');

  // Check if all of them are NOT having
  // appropriate class and attribute values.
  foreach ($selects as $select) {

    /** @var \Behat\Mink\Element\NodeElement $select */
    $this
      ->assertFalse($select
      ->hasClass('select2-widget'));
    $this
      ->assertFalse($select
      ->hasAttribute('data-jquery-once-autocomplete'));
    $this
      ->assertFalse($select
      ->hasAttribute('data-select2-autocomplete-list-widget'));
  }
}