You are here

public function Select2EntityReferenceWidgetTest::testAutocompleteOrdering in Select 2 8

Tests that the autocomplete ordering is alphabetically.

File

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

Class

Select2EntityReferenceWidgetTest
Tests select2 entity reference widget.

Namespace

Drupal\Tests\select2\FunctionalJavascript\FieldWidget

Code

public function testAutocompleteOrdering() {
  $this
    ->createField('select2', 'node', 'test', 'entity_reference', [
    'target_type' => 'entity_test_mulrevpub',
  ], [
    'handler' => 'default:entity_test_mulrevpub',
    'handler_settings' => [
      'auto_create' => FALSE,
    ],
  ], 'select2_entity_reference', [
    'autocomplete' => TRUE,
    'match_operator' => 'CONTAINS',
    'match_limit' => 10,
  ]);
  EntityTestMulRevPub::create([
    'name' => 'foo',
  ])
    ->save();
  EntityTestMulRevPub::create([
    'name' => 'bar',
  ])
    ->save();
  EntityTestMulRevPub::create([
    'name' => 'bar foo',
  ])
    ->save();
  EntityTestMulRevPub::create([
    'name' => 'gaga',
  ])
    ->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',
    ],
    [
      'id' => 1,
      'text' => 'foo',
    ],
  ];
  $this
    ->assertSame($expected, $results);
}