You are here

public function Select2WidgetTest::testSingleSelectRequired in Select 2 8

Test single field selection.

File

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

Class

Select2WidgetTest
Tests select2 simple widget.

Namespace

Drupal\Tests\select2\FunctionalJavascript\FieldWidget

Code

public function testSingleSelectRequired() {
  $this
    ->createField('select2', 'node', 'test', 'list_string', [
    'allowed_values' => [
      'foo' => 'Foo',
      'bar' => 'Bar',
    ],
  ], [
    'required' => TRUE,
  ], '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()
    ->elementNotExists('css', '.form-item-select2 .select2-selection__clear');
  $this
    ->selectOption('edit-select2', [
    'bar',
  ]);
  $page
    ->pressButton('Save');
  $node = $this
    ->getNodeByTitle('Test node', TRUE);
  $this
    ->assertSame([
    [
      'value' => 'bar',
    ],
  ], $node->select2
    ->getValue());
}