You are here

public function Select2EntityReferenceWidgetTest::testSingleValueWidget in Select 2 8

Test a single value widget.

@dataProvider providerTestSingleValueWidget

File

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

Class

Select2EntityReferenceWidgetTest
Tests select2 entity reference widget.

Namespace

Drupal\Tests\select2\FunctionalJavascript\FieldWidget

Code

public function testSingleValueWidget($autocomplete, $match_operator, $count, $autocreate) {
  $this
    ->createField('select2', 'node', 'test', 'entity_reference', [
    'target_type' => 'entity_test_mulrevpub',
  ], [
    'handler' => 'default:entity_test_mulrevpub',
    'handler_settings' => [
      'auto_create' => $autocreate,
    ],
  ], 'select2_entity_reference', [
    'autocomplete' => $autocomplete,
    'match_operator' => $match_operator,
    'match_limit' => 10,
  ]);
  EntityTestMulRevPub::create([
    'name' => 'foo',
  ])
    ->save();
  EntityTestMulRevPub::create([
    'name' => 'bar',
  ])
    ->save();
  EntityTestMulRevPub::create([
    'name' => 'bar foo',
  ])
    ->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--single');
  $page
    ->find('css', '.select2-search__field')
    ->setValue('fo');
  $assert_session
    ->waitForElement('xpath', '//li[contains(@class, "select2-results__option") and text()="foo"]');
  $assert_session
    ->elementsCount('xpath', '//li[contains(@class, "select2-results__option")]', $count);
  $page
    ->find('xpath', '//li[contains(@class, "select2-results__option") and text()="foo"]')
    ->click();
  $page
    ->pressButton('Save');
  $node = $this
    ->getNodeByTitle('Test node', TRUE);
  $this
    ->assertEquals([
    [
      'target_id' => 1,
    ],
  ], $node->select2
    ->getValue());
  if ($autocreate) {
    $this
      ->drupalGet($node
      ->toUrl('edit-form'));
    $this
      ->click('.form-item-select2 .select2-selection.select2-selection--single');
    $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' => 5,
      ],
    ], $node->select2
      ->getValue());
    $this
      ->assertNotEmpty(EntityTestMulRevPub::load(5));
    $this
      ->drupalGet($node
      ->toUrl('edit-form'));
    $this
      ->click('.form-item-select2 .select2-selection.select2-selection--single');
    $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' => 6,
      ],
    ], $node->select2
      ->getValue());
    $this
      ->assertNotEmpty(EntityTestMulRevPub::load(6));
  }
}