You are here

public function Select2EntityReferenceWidgetTest::testMultipleValueWidget in Select 2 8

Test autocomplete in a multiple value field.

@dataProvider providerTestMultiValueWidget

File

tests/src/FunctionalJavascript/FieldWidget/Select2EntityReferenceWidgetTest.php, line 119

Class

Select2EntityReferenceWidgetTest
Tests select2 entity reference widget.

Namespace

Drupal\Tests\select2\FunctionalJavascript\FieldWidget

Code

public function testMultipleValueWidget($autocomplete, $autocreate) {
  $this
    ->createField('select2', 'node', 'test', 'entity_reference', [
    'target_type' => 'entity_test_mulrevpub',
    'cardinality' => -1,
  ], [
    'handler' => 'default:entity_test_mulrevpub',
    'handler_settings' => [
      'auto_create' => $autocreate,
    ],
  ], 'select2_entity_reference', [
    'autocomplete' => $autocomplete,
  ]);
  EntityTestMulRevPub::create([
    'name' => 'foo',
  ])
    ->save();
  EntityTestMulRevPub::create([
    'name' => 'bar',
  ])
    ->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');
  $assert_session
    ->waitForElement('xpath', '//li[contains(@class, "select2-results__option") and text()="foo"]');
  $page
    ->find('xpath', '//li[contains(@class, "select2-results__option") and text()="foo"]')
    ->click();
  $this
    ->click('.form-item-select2 .select2-selection.select2-selection--multiple');
  $page
    ->find('css', '.select2-search__field')
    ->setValue('ga');
  $assert_session
    ->waitForElement('xpath', '//li[contains(@class, "select2-results__option") and text()="gaga"]');
  $page
    ->find('xpath', '//li[contains(@class, "select2-results__option") and text()="gaga"]')
    ->click();
  $page
    ->pressButton('Save');
  $node = $this
    ->getNodeByTitle('Test node', TRUE);
  $this
    ->assertEquals([
    [
      'target_id' => 1,
    ],
    [
      'target_id' => 3,
    ],
  ], $node->select2
    ->getValue());
  if ($autocreate) {
    $this
      ->drupalGet($node
      ->toUrl('edit-form'));
    $this
      ->click('.form-item-select2 .select2-selection.select2-selection--multiple');
    $page
      ->find('css', '.select2-search__field')
      ->setValue('Preview value');
    $assert_session
      ->waitForElement('css', '.select2-results__option--highlighted');
    $page
      ->find('css', '.select2-results__option--highlighted')
      ->click();
    $page
      ->pressButton('Preview');
    $page
      ->clickLink('Back to content editing');
    $page
      ->pressButton('Save');
    $node = $this
      ->getNodeByTitle('Test node', TRUE);
    $this
      ->assertEquals([
      [
        'target_id' => 1,
      ],
      [
        'target_id' => 3,
      ],
      [
        'target_id' => 4,
      ],
    ], $node->select2
      ->getValue());
    $this
      ->assertNotEmpty(EntityTestMulRevPub::load(4));
    $this
      ->drupalGet($node
      ->toUrl('edit-form'));
    $this
      ->click('.form-item-select2 .select2-selection.select2-selection--multiple');
    $page
      ->find('css', '.select2-search__field')
      ->setValue('New value');
    $assert_session
      ->waitForElement('css', '.select2-results__option--highlighted');
    $page
      ->find('css', '.select2-results__option--highlighted')
      ->click();
    $page
      ->pressButton('Save');
    $node = $this
      ->getNodeByTitle('Test node', TRUE);
    $this
      ->assertEquals([
      [
        'target_id' => 1,
      ],
      [
        'target_id' => 3,
      ],
      [
        'target_id' => 4,
      ],
      [
        'target_id' => 5,
      ],
    ], $node->select2
      ->getValue());
    $this
      ->assertNotEmpty(EntityTestMulRevPub::load(5));
  }
}