You are here

public function PublishTest::testAutocomplete in Select 2 8

Tests that the autocomplete.

File

modules/select2_publish/tests/src/FunctionalJavascript/PublishTest.php, line 62

Class

PublishTest
Tests select2_publish integration.

Namespace

Drupal\Tests\select2_publish\FunctionalJavascript

Code

public function testAutocomplete() {
  $this
    ->createField('select2', 'node', 'test', 'entity_reference', [
    'target_type' => 'entity_test_mulrevpub',
  ], [
    'handler' => 'default:entity_test_mulrevpub',
    'handler_settings' => [
      'target_bundles' => [
        'entity_test_mulrevpub' => 'entity_test_mulrevpub',
      ],
      'auto_create' => FALSE,
    ],
  ], 'select2_entity_reference', [
    'autocomplete' => TRUE,
    'match_operator' => 'CONTAINS',
  ]);
  EntityTestMulRevPub::create([
    'name' => 'foo',
    'status' => FALSE,
  ])
    ->save();
  EntityTestMulRevPub::create([
    'name' => 'bar',
  ])
    ->save();
  EntityTestMulRevPub::create([
    'name' => 'bar foo',
  ])
    ->save();
  EntityTestMulRevPub::create([
    'name' => 'gaga',
    'status' => FALSE,
  ])
    ->save();
  $this
    ->drupalGet('/node/add/test');
  $settings = Json::decode($this
    ->getSession()
    ->getPage()
    ->findField('select2')
    ->getAttribute('data-select2-config'));
  $url = Url::fromUserInput($settings['ajax']['url']);
  $url
    ->setAbsolute(TRUE);
  $url
    ->setRouteParameter('q', 'f');
  $response = \Drupal::httpClient()
    ->get($url
    ->toString());
  $results = Json::decode($response
    ->getBody()
    ->getContents())['results'];
  $expected = [
    [
      'id' => 3,
      'text' => 'bar foo',
      'published' => TRUE,
    ],
    [
      'id' => 1,
      'text' => 'foo',
      'published' => FALSE,
    ],
  ];
  $this
    ->assertSame($expected, $results);
  $page = $this
    ->getSession()
    ->getPage();
  $assert_session = $this
    ->assertSession();
  $page
    ->fillField('title[0][value]', 'Test node');
  $this
    ->click('.form-item-select2 .select2-selection.select2-selection--single');
  $page
    ->find('css', '.select2-search__field')
    ->setValue('gag');
  $assert_session
    ->waitForElement('xpath', '//li[@class="select2-results__option unpublished select2-results__option--highlighted" and text()="gaga"]');
  $page
    ->find('xpath', '//li[@class="select2-results__option unpublished select2-results__option--highlighted" and text()="gaga"]')
    ->click();
  $page
    ->pressButton('Save');
  $node = $this
    ->getNodeByTitle('Test node', TRUE);
  $this
    ->assertEquals([
    [
      'target_id' => 4,
    ],
  ], $node->select2
    ->getValue());
}