You are here

protected function PagesIntegrationTest::checkAutocompleteFunctionality in Search API Autocomplete 8

Checks that autocomplete works correctly.

1 call to PagesIntegrationTest::checkAutocompleteFunctionality()
PagesIntegrationTest::testModule in tests/src/FunctionalJavascript/PagesIntegrationTest.php
Tests autocomplete for search pages.

File

tests/src/FunctionalJavascript/PagesIntegrationTest.php, line 161

Class

PagesIntegrationTest
Tests integration with the Search API Pages module.

Namespace

Drupal\Tests\search_api_autocomplete\FunctionalJavascript

Code

protected function checkAutocompleteFunctionality() {
  $assert_session = $this
    ->assertSession();
  $this
    ->drupalGet('test-search');
  $assert_session
    ->elementAttributeContains('css', 'input[data-drupal-selector="edit-keys"]', 'data-search-api-autocomplete-search', $this->searchId);
  $elements = $this
    ->getAutocompleteSuggestions();
  $suggestions = [];
  foreach ($elements as $element) {
    $label = $this
      ->getElementText($element, '.autocomplete-suggestion-label');
    $user_input = $this
      ->getElementText($element, '.autocomplete-suggestion-user-input');
    $suffix = $this
      ->getElementText($element, '.autocomplete-suggestion-suggestion-suffix');
    $count = $this
      ->getElementText($element, '.autocomplete-suggestion-results-count');
    $keys = $label . $user_input . $suffix;
    $suggestions[] = [
      'keys' => $keys,
      'count' => $count,
    ];
  }
  $expected = [
    [
      'keys' => 'Tést-suggester-1',
      'count' => 1,
    ],
    [
      'keys' => 'Tést-suggester-2',
      'count' => 2,
    ],
    [
      'keys' => 'Tést-suggester-url',
      'count' => NULL,
    ],
    [
      'keys' => 'Tést-backend-1',
      'count' => 1,
    ],
    [
      'keys' => 'Tést-backend-2',
      'count' => 2,
    ],
  ];
  $this
    ->assertEquals($expected, $suggestions);

  /** @var \Drupal\search_api\Query\QueryInterface $query */
  list($query) = $this
    ->getMethodArguments('backend', 'getAutocompleteSuggestions');
  $this
    ->assertEquals([
    'name',
  ], $query
    ->getFulltextFields());
  $this
    ->drupalGet($this
    ->getAdminPath('edit'));
  $page = $this
    ->getSession()
    ->getPage();
  $page
    ->find('css', '#edit-suggesters-settings-server > summary')
    ->click();
  $edit = [
    'suggesters[settings][server][fields][body]' => TRUE,
  ];
  $this
    ->drupalPostForm(NULL, $edit, 'Save');
  $this
    ->drupalGet('test-search');
  $elements = $this
    ->getAutocompleteSuggestions();
  $this
    ->assertCount(5, $elements);
  list($query) = $this
    ->getMethodArguments('backend', 'getAutocompleteSuggestions');
  $this
    ->assertEquals([
    'body',
  ], $query
    ->getFulltextFields());
}