You are here

protected function IntegrationTest::checkLiveResultsAutocomplete in Search API Autocomplete 8

Tests autocomplete with the "Live results" suggester.

1 call to IntegrationTest::checkLiveResultsAutocomplete()
IntegrationTest::testModule in tests/src/FunctionalJavascript/IntegrationTest.php
Tests the complete functionality of the module via the UI.

File

tests/src/FunctionalJavascript/IntegrationTest.php, line 292

Class

IntegrationTest
Tests the functionality of the whole module from a user's perspective.

Namespace

Drupal\Tests\search_api_autocomplete\FunctionalJavascript

Code

protected function checkLiveResultsAutocomplete() {
  $assert_session = $this
    ->assertSession();

  // First, enable "Live results" as the only suggester.
  $this
    ->drupalGet($this
    ->getAdminPath('edit'));
  $page = $this
    ->getSession()
    ->getPage();
  $this
    ->click('input[name="suggesters[enabled][live_results]"]');
  $page
    ->find('css', 'details[data-drupal-selector="edit-suggesters-settings-live-results"] > summary')
    ->click();
  $edit = [
    'suggesters[enabled][live_results]' => TRUE,
    'suggesters[enabled][search_api_autocomplete_test]' => FALSE,
    'suggesters[enabled][server]' => FALSE,
    'suggesters[settings][live_results][fields][name]' => FALSE,
    'suggesters[settings][live_results][fields][body]' => TRUE,
  ];
  $this
    ->submitForm($edit, 'Save');
  $assert_session
    ->pageTextContains('The autocompletion settings for the search have been saved.');

  // Then, set an appropriate search method for the test backend.
  $callback = [
    TestsHelper::class,
    'search',
  ];
  $this
    ->setMethodOverride('backend', 'search', $callback);

  // Get the autocompletion results.
  $this
    ->drupalGet('search-api-autocomplete-test');
  $suggestions = [];
  foreach ($this
    ->getAutocompleteSuggestions() as $element) {
    $label = $this
      ->getElementText($element, '.autocomplete-suggestion-label');
    $suggestions[$label] = $element;
  }

  // Make sure the suggestions are as expected.
  $expected = [
    $this->entities[3]
      ->label(),
    $this->entities[4]
      ->label(),
    $this->entities[2]
      ->label(),
  ];
  $this
    ->assertEquals($expected, array_keys($suggestions));

  // Make sure all the search query settings were as expected.

  /** @var \Drupal\search_api\Query\QueryInterface $query */
  $query = $this
    ->getMethodArguments('backend', 'search')[0];
  $this
    ->assertInstanceOf(QueryInterface::class, $query);
  $this
    ->assertEquals(0, $query
    ->getOption('offset'));
  $this
    ->assertEquals(5, $query
    ->getOption('limit'));
  $this
    ->assertEquals([
    'body',
  ], $query
    ->getFulltextFields());
  $this
    ->assertEquals('Tést', $query
    ->getOriginalKeys());

  // Click on one of the suggestions and verify it takes us to the expected
  // page.
  $suggestions[$this->entities[3]
    ->label()]
    ->click();
  $this
    ->logPageChange();
  $path = $this->entities[3]
    ->toUrl()
    ->getInternalPath();
  $assert_session
    ->addressEquals('/' . $path);
}