You are here

public function SearchApiSolrTest::testSearchResultSorts in Search API Solr 8

Same name and namespace in other branches
  1. 8.2 tests/src/Kernel/SearchApiSolrTest.php \Drupal\Tests\search_api_solr\Kernel\SearchApiSolrTest::testSearchResultSorts()

Tests search result sorts.

File

tests/src/Kernel/SearchApiSolrTest.php, line 741

Class

SearchApiSolrTest
Tests index and search capabilities using the Solr search backend.

Namespace

Drupal\Tests\search_api_solr\Kernel

Code

public function testSearchResultSorts() {

  // Only run the tests if we have a Solr core available.
  if ($this->solrAvailable) {
    $this
      ->insertExampleContent();

    // Add node with body length just above the solr limit for search fields.
    // It's exceeded by just a single char to simulate an edge case.
    $this
      ->addTestEntity(6, [
      'name' => 'Long text',
      'body' => $this
        ->getLongText(32767),
      'type' => 'article',
    ]);

    // Add another node with body length equal to the limit.
    $this
      ->addTestEntity(7, [
      'name' => 'Z long',
      'body' => $this
        ->getLongText(32766),
      'type' => 'article',
    ]);
    $this
      ->indexItems($this->indexId);

    // Type text.
    $results = $this
      ->buildSearch(NULL, [], [], FALSE)
      ->sort('name')
      ->sort('search_api_id')
      ->execute();
    $this
      ->assertResults([
      3,
      5,
      1,
      4,
      2,
      6,
      7,
    ], $results, 'Sort by name.');
    $results = $this
      ->buildSearch(NULL, [], [], FALSE)
      ->sort('name', QueryInterface::SORT_DESC)
      ->sort('search_api_id')
      ->execute();
    $this
      ->assertResults([
      7,
      6,
      2,
      4,
      1,
      5,
      3,
    ], $results, 'Sort by name descending.');

    // Type string.
    $results = $this
      ->buildSearch(NULL, [], [], FALSE)
      ->sort('type')
      ->sort('search_api_id')
      ->execute();
    $this
      ->assertResults([
      4,
      5,
      6,
      7,
      1,
      2,
      3,
    ], $results, 'Sort by type.');
    $results = $this
      ->buildSearch(NULL, [], [], FALSE)
      ->sort('type', QueryInterface::SORT_DESC)
      ->sort('search_api_id')
      ->execute();
    $this
      ->assertResults([
      1,
      2,
      3,
      4,
      5,
      6,
      7,
    ], $results, 'Sort by type descending.');

    // Type multi-value string. Uses first value.
    $results = $this
      ->buildSearch(NULL, [], [], FALSE)
      ->sort('keywords')
      ->sort('search_api_id')
      ->execute();
    $this
      ->assertResults([
      3,
      6,
      7,
      4,
      1,
      2,
      5,
    ], $results, 'Sort by keywords.');
    $results = $this
      ->buildSearch(NULL, [], [], FALSE)
      ->sort('keywords', QueryInterface::SORT_DESC)
      ->sort('search_api_id')
      ->execute();
    $this
      ->assertResults([
      1,
      2,
      5,
      4,
      3,
      6,
      7,
    ], $results, 'Sort by keywords descending.');

    // Type decimal.
    $results = $this
      ->buildSearch(NULL, [], [], FALSE)
      ->sort('width')
      ->sort('search_api_id')
      ->execute();
    $this
      ->assertResults([
      1,
      2,
      3,
      6,
      7,
      4,
      5,
    ], $results, 'Sort by width.');
    $results = $this
      ->buildSearch(NULL, [], [], FALSE)
      ->sort('width', QueryInterface::SORT_DESC)
      ->sort('search_api_id')
      ->execute();
    $this
      ->assertResults([
      5,
      4,
      1,
      2,
      3,
      6,
      7,
    ], $results, 'Sort by width descending.');
    $results = $this
      ->buildSearch(NULL, [], [], FALSE)
      ->sort('changed')
      ->execute();
    $this
      ->assertResults([
      1,
      2,
      3,
      4,
      5,
      6,
      7,
    ], $results, 'Sort by last update date');
    $results = $this
      ->buildSearch(NULL, [], [], FALSE)
      ->sort('changed', QueryInterface::SORT_DESC)
      ->execute();
    $this
      ->assertResults([
      7,
      6,
      5,
      4,
      3,
      2,
      1,
    ], $results, 'Sort by last update date descending');
  }
  else {
    $this
      ->assertTrue(TRUE, 'Error: The Solr instance could not be found. Please enable a multi-core one on http://localhost:8983/solr/drupal');
  }
}