You are here

public function TermAutocompleteTest::testAutocompleteCountResults in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/taxonomy/tests/src/Functional/TermAutocompleteTest.php \Drupal\Tests\taxonomy\Functional\TermAutocompleteTest::testAutocompleteCountResults()

Tests that the autocomplete method returns the good number of results.

See also

\Drupal\taxonomy\Controller\TermAutocompleteController::autocomplete()

File

core/modules/taxonomy/tests/src/Functional/TermAutocompleteTest.php, line 169

Class

TermAutocompleteTest
Tests the autocomplete implementation of the taxonomy class.

Namespace

Drupal\Tests\taxonomy\Functional

Code

public function testAutocompleteCountResults() {

  // Test that no matching term found.
  $data = $this
    ->drupalGetJson($this->autocompleteUrl, [
    'query' => [
      'q' => 'zzz',
    ],
  ]);
  $this
    ->assertTrue(empty($data), 'Autocomplete returned no results');

  // Test that only one matching term found, when only one matches.
  $data = $this
    ->drupalGetJson($this->autocompleteUrl, [
    'query' => [
      'q' => 'aaa 10',
    ],
  ]);
  $this
    ->assertCount(1, $data, 'Autocomplete returned 1 result');

  // Test the correct number of matches when multiple are partial matches.
  $data = $this
    ->drupalGetJson($this->autocompleteUrl, [
    'query' => [
      'q' => 'aaa 1',
    ],
  ]);
  $this
    ->assertCount(3, $data, 'Autocomplete returned 3 results');

  // Tests that only 10 results are returned, even if there are more than 10
  // matches.
  $data = $this
    ->drupalGetJson($this->autocompleteUrl, [
    'query' => [
      'q' => 'aaa',
    ],
  ]);
  $this
    ->assertCount(10, $data, 'Autocomplete returned only 10 results (for over 10 matches)');
}