You are here

public function PublishTest::testMultipleSelection in Select 2 8

Test autocomplete in a single value field.

File

modules/select2_publish/tests/src/FunctionalJavascript/PublishTest.php, line 25

Class

PublishTest
Tests select2_publish integration.

Namespace

Drupal\Tests\select2_publish\FunctionalJavascript

Code

public function testMultipleSelection() {
  $this
    ->createField('select2', 'node', 'test', 'entity_reference', [
    'target_type' => 'entity_test_mulrevpub',
    'cardinality' => -1,
  ], [
    'handler' => 'default:entity_test_mulrevpub',
    'handler_settings' => [
      'target_bundles' => [
        'entity_test_mulrevpub' => 'entity_test_mulrevpub',
      ],
      'auto_create' => FALSE,
    ],
  ], 'select2_entity_reference');
  EntityTestMulRevPub::create([
    'name' => 'foo',
  ])
    ->save();
  EntityTestMulRevPub::create([
    'name' => 'bar',
    'status' => FALSE,
  ])
    ->save();
  EntityTestMulRevPub::create([
    'name' => 'gaga',
  ])
    ->save();
  $page = $this
    ->getSession()
    ->getPage();
  $assert_session = $this
    ->assertSession();
  $this
    ->drupalGet('/node/add/test');
  $page
    ->fillField('title[0][value]', 'Test node');
  $this
    ->click('.form-item-select2 .select2-selection.select2-selection--multiple');
  $page
    ->find('css', '.select2-search__field')
    ->setValue('fo');
  $this
    ->assertNotEmpty($assert_session
    ->waitForElement('xpath', '//li[@class="select2-results__option published select2-results__option--highlighted" and text()="foo"]'));
  $page
    ->find('xpath', '//li[@class="select2-results__option published select2-results__option--highlighted" and text()="foo"]')
    ->click();
  $this
    ->assertNotEmpty($assert_session
    ->waitForElement('xpath', '//li[@class="select2-selection__choice published" and text()="foo"]'));
  $page
    ->find('css', '.select2-search__field')
    ->setValue('ba');
  $this
    ->assertNotEmpty($assert_session
    ->waitForElement('xpath', '//li[@class="select2-results__option unpublished select2-results__option--highlighted" and text()="bar"]'));
  $page
    ->find('xpath', '//li[@class="select2-results__option unpublished select2-results__option--highlighted" and text()="bar"]')
    ->click();
  $this
    ->assertNotEmpty($assert_session
    ->waitForElement('xpath', '//li[@class="select2-selection__choice unpublished" and text()="bar"]'));
}