You are here

public function Select2WidgetTest::testSingleSelect in Select 2 8

Test single field selection.

@group select2

File

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

Class

Select2WidgetTest
Tests select2 simple widget.

Namespace

Drupal\Tests\select2\FunctionalJavascript\FieldWidget

Code

public function testSingleSelect() {
  $this
    ->createField('select2', 'node', 'test', 'list_string', [
    'allowed_values' => [
      'foo' => 'Foo',
      'bar' => 'Bar',
    ],
  ], [], 'select2', []);
  $page = $this
    ->getSession()
    ->getPage();
  $this
    ->drupalGet('/node/add/test');
  $page
    ->fillField('title[0][value]', 'Test node');
  $this
    ->selectOption('edit-select2', [
    'foo',
  ]);
  $page
    ->pressButton('Save');
  $node = $this
    ->getNodeByTitle('Test node');
  $this
    ->assertSame([
    [
      'value' => 'foo',
    ],
  ], $node->select2
    ->getValue());
  $this
    ->drupalGet($node
    ->toUrl('edit-form'));
  $this
    ->assertSession()
    ->elementExists('css', '.form-item-select2 .select2-selection__clear');
  $this
    ->click('.form-item-select2 .select2-selection__clear');
  $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());
}