You are here

protected function SearchApiSolrTest::checkSearchResultSorts in Search API Solr 4.x

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

Tests search result sorts.

1 call to SearchApiSolrTest::checkSearchResultSorts()
SearchApiSolrTest::checkBackendSpecificFeatures in tests/src/Kernel/SearchApiSolrTest.php
Checks backend specific features.

File

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

Class

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

Namespace

Drupal\Tests\search_api_solr\Kernel

Code

protected function checkSearchResultSorts() {

  // 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.');

  /** @var \Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend $backend */
  $backend = Server::load($this->serverId)
    ->getBackend();
  $targeted_branch = $backend
    ->getSolrConnector()
    ->getSchemaTargetedSolrBranch();
  if ('3.x' !== $targeted_branch) {

    // There's no real collated field for Solr 3.x. Therefore the sorting of
    // of "non existing" values differ.
    // 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,
    4,
    5,
    3,
    6,
    7,
  ], $results, 'Sort by last update date');
  $results = $this
    ->buildSearch(NULL, [], [], FALSE)
    ->sort('changed', QueryInterface::SORT_DESC)
    ->execute();
  $this
    ->assertResults([
    7,
    6,
    3,
    5,
    4,
    2,
    1,
  ], $results, 'Sort by last update date descending');
  $this
    ->removeTestEntity(6);
  $this
    ->removeTestEntity(7);
}