You are here

public function Select2WidgetTest::testMultipleSelect in Select 2 8

Test multiple field selection with unlimited items.

File

tests/src/FunctionalJavascript/FieldWidget/Select2WidgetTest.php, line 104

Class

Select2WidgetTest
Tests select2 simple widget.

Namespace

Drupal\Tests\select2\FunctionalJavascript\FieldWidget

Code

public function testMultipleSelect() {
  $this
    ->createField('select2', 'node', 'test', 'list_string', [
    'allowed_values' => [
      'foo' => 'Foo',
      'bar' => 'Bar',
      'gaga' => 'Gaga',
    ],
    'cardinality' => -1,
  ], [], 'select2', []);
  $page = $this
    ->getSession()
    ->getPage();
  $this
    ->drupalGet('/node/add/test');
  $page
    ->fillField('title[0][value]', 'Test node');
  $this
    ->selectOption('edit-select2', [
    'foo',
    'gaga',
  ]);
  $page
    ->pressButton('Save');
  $node = $this
    ->getNodeByTitle('Test node');
  $this
    ->assertSame([
    [
      'value' => 'foo',
    ],
    [
      'value' => 'gaga',
    ],
  ], $node->select2
    ->getValue());
  $this
    ->drupalGet($node
    ->toUrl('edit-form'));
  $this
    ->selectOption('edit-select2', []);
  $page
    ->pressButton('Save');
  $node = $this
    ->getNodeByTitle('Test node', TRUE);
  $this
    ->assertSame([], $node->select2
    ->getValue());
  $this
    ->drupalGet($node
    ->toUrl('edit-form'));
  $this
    ->selectOption('edit-select2', [
    'bar',
  ]);
  $page
    ->pressButton('Save');
  $node = $this
    ->getNodeByTitle('Test node', TRUE);
  $this
    ->assertSame([
    [
      'value' => 'bar',
    ],
  ], $node->select2
    ->getValue());
}