You are here

protected function SearchApiSolrTest::checkRetrieveData 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::checkRetrieveData()

Tests retrieve_data options.

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

File

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

Class

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

Namespace

Drupal\Tests\search_api_solr\Kernel

Code

protected function checkRetrieveData() {
  $server = $this
    ->getIndex()
    ->getServerInstance();
  $config = $server
    ->getBackendConfig();
  $backend = $server
    ->getBackend();
  $this
    ->indexItems($this->indexId);

  // Retrieve just required fields.
  $query = $this
    ->buildSearch('foobar');
  $results = $query
    ->execute();
  $this
    ->assertEquals(1, $results
    ->getResultCount(), 'Search for »foobar« returned correct number of results.');

  /** @var \Drupal\search_api\Item\ItemInterface $result */
  foreach ($results as $result) {

    /** @var \Solarium\QueryType\Select\Result\Document $solr_document */
    $solr_document = $result
      ->getExtraData('search_api_solr_document', NULL);
    $fields = $solr_document
      ->getFields();
    $this
      ->assertEquals('entity:entity_test_mulrev_changed/3:en', $fields['ss_search_api_id']);
    $this
      ->assertEquals('en', $fields['ss_search_api_language']);
    $this
      ->assertArrayHasKey('score', $fields);
    $this
      ->assertArrayNotHasKey('tm_X3b_en_body', $fields);
    $this
      ->assertArrayNotHasKey('id', $fields);
    $this
      ->assertArrayNotHasKey('its_id', $fields);
    $this
      ->assertArrayNotHasKey('twm_suggest', $fields);
  }

  // Retrieve all fields.
  $config['retrieve_data'] = TRUE;
  $server
    ->setBackendConfig($config);
  $server
    ->save();
  $query = $this
    ->buildSearch('foobar');
  $results = $query
    ->execute();
  $this
    ->assertEquals(1, $results
    ->getResultCount(), 'Search for »foobar« returned correct number of results.');

  /** @var \Drupal\search_api\Item\ItemInterface $result */
  foreach ($results as $result) {

    /** @var \Solarium\QueryType\Select\Result\Document $solr_document */
    $solr_document = $result
      ->getExtraData('search_api_solr_document', NULL);
    $fields = $solr_document
      ->getFields();
    $this
      ->assertEquals('entity:entity_test_mulrev_changed/3:en', $fields['ss_search_api_id']);
    $this
      ->assertEquals('en', $fields['ss_search_api_language']);
    $this
      ->assertArrayHasKey('score', $fields);
    $this
      ->assertArrayHasKey('tm_X3b_en_body', $fields);
    $this
      ->assertStringContainsString('search_index-entity:entity_test_mulrev_changed/3:en', $fields['id']);
    $this
      ->assertEquals('3', $fields['its_id']);
    $this
      ->assertArrayHasKey('twm_suggest', $fields);
  }

  // Retrieve list of fields in addition to required fields.
  $query = $this
    ->buildSearch('foobar');
  $query
    ->setOption('search_api_retrieved_field_values', [
    'body' => 'body',
  ]);
  $results = $query
    ->execute();
  $this
    ->assertEquals(1, $results
    ->getResultCount(), 'Search for »foobar« returned correct number of results.');

  /** @var \Drupal\search_api\Item\ItemInterface $result */
  foreach ($results as $result) {

    /** @var \Solarium\QueryType\Select\Result\Document $solr_document */
    $solr_document = $result
      ->getExtraData('search_api_solr_document', NULL);
    $fields = $solr_document
      ->getFields();
    $this
      ->assertEquals('entity:entity_test_mulrev_changed/3:en', $fields['ss_search_api_id']);
    $this
      ->assertEquals('en', $fields['ss_search_api_language']);
    $this
      ->assertArrayHasKey('score', $fields);
    $this
      ->assertArrayHasKey('tm_X3b_en_body', $fields);
    $this
      ->assertArrayNotHasKey('id', $fields);
    $this
      ->assertArrayNotHasKey('its_id', $fields);
    $this
      ->assertArrayNotHasKey('twm_suggest', $fields);
  }
  $fulltext_fields = array_flip($this
    ->invokeMethod($backend, 'getQueryFulltextFields', [
    $query,
  ]));
  $this
    ->assertArrayHasKey('name', $fulltext_fields);
  $this
    ->assertArrayHasKey('body', $fulltext_fields);
  $this
    ->assertArrayHasKey('body_unstemmed', $fulltext_fields);
  $this
    ->assertArrayHasKey('category_edge', $fulltext_fields);

  // body_suggest should be removed by getQueryFulltextFields().
  $this
    ->assertArrayNotHasKey('body_suggest', $fulltext_fields);
}