You are here

protected function BackendTest::searchWithRandom in Search API 8

Tests whether random searches work.

1 call to BackendTest::searchWithRandom()
BackendTest::checkBackendSpecificFeatures in modules/search_api_db/tests/src/Kernel/BackendTest.php
Checks backend specific features.

File

modules/search_api_db/tests/src/Kernel/BackendTest.php, line 267

Class

BackendTest
Tests index and search capabilities using the Database search backend.

Namespace

Drupal\Tests\search_api_db\Kernel

Code

protected function searchWithRandom() {

  // Run the query 5 times, using random sorting as the first sort and verify
  // that the results are not always the same.
  $first_result = NULL;
  $second_result = NULL;
  for ($i = 1; $i <= 5; $i++) {
    $results = $this
      ->buildSearch('foo', [], NULL, FALSE)
      ->sort('search_api_random')
      ->sort('id')
      ->execute();
    $result_ids = array_keys($results
      ->getResultItems());
    if ($first_result === NULL) {
      $first_result = $second_result = $result_ids;
    }
    elseif ($result_ids !== $first_result) {
      $second_result = $result_ids;
    }

    // Make sure the search still returned the expected items.
    $this
      ->assertCount(4, $result_ids);
    sort($result_ids);
    $this
      ->assertEquals($this
      ->getItemIds([
      1,
      2,
      4,
      5,
    ]), $result_ids);
  }
  $this
    ->assertNotEquals($first_result, $second_result);
}