You are here

public function SearchApiSolrLocationTest::testBackend in Search API Solr 8

Same name and namespace in other branches
  1. 8.3 tests/src/Kernel/SearchApiSolrLocationTest.php \Drupal\Tests\search_api_solr\Kernel\SearchApiSolrLocationTest::testBackend()
  2. 8.2 tests/src/Kernel/SearchApiSolrLocationTest.php \Drupal\Tests\search_api_solr\Kernel\SearchApiSolrLocationTest::testBackend()
  3. 4.x tests/src/Kernel/SearchApiSolrLocationTest.php \Drupal\Tests\search_api_solr\Kernel\SearchApiSolrLocationTest::testBackend()

Tests location searches and distance facets.

Overrides BackendTestBase::testBackend

File

tests/src/Kernel/SearchApiSolrLocationTest.php, line 188

Class

SearchApiSolrLocationTest
Tests location searches and distance facets using the Solr search backend.

Namespace

Drupal\Tests\search_api_solr\Kernel

Code

public function testBackend() {
  $solr_version = $this
    ->getServer()
    ->getBackend()
    ->getSolrConnector()
    ->getSolrVersion();

  // Search 500km from Antwerp.
  $location_options = [
    [
      'field' => 'location',
      'lat' => '51.260197',
      'lon' => '4.402771',
      'radius' => '500',
    ],
  ];

  /** @var \Drupal\search_api\Query\ResultSet $result */
  $query = $this
    ->buildSearch(NULL, [], NULL, FALSE)
    ->sort('location__distance');
  $query
    ->setOption('search_api_location', $location_options);
  $result = $query
    ->execute();
  $this
    ->assertResults([
    3,
    1,
  ], $result, 'Search for 500km from Antwerp ordered by distance');

  /** @var \Drupal\search_api\Item\Item $item */
  $item = $result
    ->getResultItems()['entity:entity_test_mulrev_changed/3:en'];
  $distance = $item
    ->getField('location__distance')
    ->getValues()[0];
  $this
    ->assertEquals(42.5263374675, $distance, 'The distance is correctly returned');

  // Search between 100km and 6000km from Antwerp.
  $location_options = [
    [
      'field' => 'location',
      'lat' => '51.260197',
      'lon' => '4.402771',
    ],
  ];
  $query = $this
    ->buildSearch(NULL, [], NULL, FALSE)
    ->addCondition('location', [
    '100',
    '6000',
  ], 'BETWEEN')
    ->sort('location__distance', 'DESC');
  $query
    ->setOption('search_api_location', $location_options);
  $result = $query
    ->execute();
  $this
    ->assertResults([
    2,
    1,
  ], $result, 'Search between 100 and 6000km from Antwerp ordered by distance descending');
  $facets_options['location__distance'] = [
    'field' => 'location__distance',
    'limit' => 10,
    'min_count' => 0,
    'missing' => TRUE,
  ];

  // Search 1000km from Antwerp.
  $location_options = [
    [
      'field' => 'location',
      'lat' => '51.260197',
      'lon' => '4.402771',
      'radius' => '1000',
    ],
  ];
  $query = $this
    ->buildSearch(NULL, [], NULL, FALSE)
    ->sort('location__distance');
  $query
    ->setOption('search_api_location', $location_options);
  $query
    ->setOption('search_api_facets', $facets_options);
  $result = $query
    ->execute();
  $facets = $result
    ->getExtraData('search_api_facets', [])['location__distance'];
  $expected = [
    [
      'filter' => '[0 199]',
      'count' => 1,
    ],
    [
      'filter' => '[200 399]',
      'count' => 1,
    ],
    [
      'filter' => '[400 599]',
      'count' => 0,
    ],
    [
      'filter' => '[600 799]',
      'count' => 0,
    ],
    [
      'filter' => '[800 999]',
      'count' => 0,
    ],
  ];
  $this
    ->assertEquals($expected, $facets, 'The correct location facets are returned');
  $facets_options['location__distance'] = [
    'field' => 'location__distance',
    'limit' => 3,
    'min_count' => 1,
    'missing' => TRUE,
  ];

  // Search between 100km and 1000km from Antwerp.
  $location_options = [
    [
      'field' => 'location',
      'lat' => '51.260197',
      'lon' => '4.402771',
      'radius' => '1000',
    ],
  ];
  $query = $this
    ->buildSearch(NULL, [], NULL, FALSE)
    ->addCondition('location', [
    '100',
    '1000',
  ], 'BETWEEN')
    ->sort('location__distance');
  $query
    ->setOption('search_api_location', $location_options);
  $query
    ->setOption('search_api_facets', $facets_options);
  $result = $query
    ->execute();
  $facets = $result
    ->getExtraData('search_api_facets', [])['location__distance'];
  $expected = [
    [
      'filter' => '[100 399]',
      'count' => 1,
    ],
  ];
  $this
    ->assertEquals($expected, $facets, 'The correct location facets are returned');

  // Tests the RPT data type of SearchApiSolrBackend.
  $query = $this
    ->buildSearch(NULL, [], NULL, FALSE);
  $options =& $query
    ->getOptions();
  $options['search_api_facets']['rpt'] = [
    'field' => 'rpt',
    'limit' => 3,
    'operator' => 'and',
    'min_count' => 1,
    'missing' => FALSE,
  ];
  $options['search_api_rpt']['rpt'] = [
    'field' => 'rpt',
    'geom' => '["-180 -90" TO "180 90"]',
    'gridLevel' => '2',
    'maxCells' => '35554432',
    'distErrPct' => '',
    'distErr' => '',
    'format' => 'ints2D',
  ];
  $result = $query
    ->execute();
  $expected = [
    [
      'filter' => [
        "gridLevel",
        2,
        "columns",
        32,
        "rows",
        32,
        "minX",
        -180.0,
        "maxX",
        180.0,
        "minY",
        -90.0,
        "maxY",
        90.0,
        "counts_ints2D",
        [
          NULL,
          NULL,
          NULL,
          NULL,
          NULL,
          NULL,
          [
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            1,
            1,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
          ],
          NULL,
          [
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            1,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
          ],
          NULL,
          NULL,
          NULL,
          NULL,
          NULL,
          NULL,
          NULL,
          NULL,
          NULL,
          NULL,
          NULL,
          NULL,
          NULL,
          NULL,
          NULL,
          NULL,
          NULL,
          NULL,
          NULL,
          NULL,
          NULL,
          NULL,
          NULL,
        ],
      ],
      'count' => 3,
    ],
  ];
  if (version_compare($solr_version, 5.1, '>=')) {
    $facets = $result
      ->getExtraData('search_api_facets', [])['rpt'];
    $this
      ->assertEquals($expected, $facets, 'The correct location facets are returned');
  }
  else {
    $this
      ->assertLogMessage(LOG_ERR, 'Rpt data type feature is only supported by Solr version 5.1 or higher.');
  }
  $query = $this
    ->buildSearch(NULL, [], NULL, FALSE);
  $options =& $query
    ->getOptions();
  $options['search_api_facets']['rpt'] = [
    'field' => 'rpt',
    'limit' => 4,
    'operator' => 'or',
    'min_count' => 1,
    'missing' => FALSE,
  ];
  $options['search_api_rpt']['rpt'] = [
    'field' => 'rpt',
    'geom' => '["-60 -85" TO "130 70"]',
    'gridLevel' => '2',
    'maxCells' => '35554432',
    'distErrPct' => '',
    'distErr' => '',
    'format' => 'ints2D',
  ];
  $result = $query
    ->execute();
  $expected = [
    [
      'filter' => [
        "gridLevel",
        2,
        "columns",
        18,
        "rows",
        29,
        "minX",
        -67.5,
        "maxX",
        135.0,
        "minY",
        -90.0,
        "maxY",
        73.125,
        "counts_ints2D",
        [
          NULL,
          NULL,
          NULL,
          [
            0,
            0,
            0,
            0,
            0,
            1,
            1,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
          ],
          NULL,
          NULL,
          NULL,
          NULL,
          NULL,
          NULL,
          NULL,
          NULL,
          NULL,
          NULL,
          NULL,
          NULL,
          NULL,
          NULL,
          NULL,
          NULL,
          NULL,
          NULL,
          NULL,
          NULL,
          NULL,
          NULL,
          NULL,
          NULL,
          NULL,
        ],
      ],
      'count' => 2,
    ],
  ];
  if (version_compare($solr_version, 5.1, '>=')) {
    $facets = $result
      ->getExtraData('search_api_facets', [])['rpt'];
    $this
      ->assertEquals($expected, $facets, 'The correct location facets are returned');
  }
  else {
    $this
      ->assertLogMessage(LOG_ERR, 'Rpt data type feature is only supported by Solr version 5.1 or higher.');
  }
}