You are here

public function SearchApiSolrMultilingualTest::testLanguageLimitedByOptions in Search API Multilingual Solr Search 8

Tests language limiting via options.

File

tests/src/Kernel/SearchApiSolrMultilingualTest.php, line 186

Class

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

Namespace

Drupal\Tests\search_api_solr_multilingual\Kernel

Code

public function testLanguageLimitedByOptions() {

  // Only run further tests if we have a Solr core available.
  if ($this->solrAvailable) {
    $this
      ->insertMultilingualExampleContent();
    $this
      ->indexItems($this->indexId);
    $server = $this
      ->getIndex()
      ->getServerInstance();
    $config = $server
      ->getBackendConfig();
    $config['sasm_limit_search_page_to_content_language'] = FALSE;
    $server
      ->setBackendConfig($config)
      ->save();
    $this
      ->assertFalse($this
      ->getIndex()
      ->getServerInstance()
      ->getBackendConfig()['sasm_limit_search_page_to_content_language']);
    $config['sasm_search_page_include_language_independent'] = FALSE;
    $server
      ->setBackendConfig($config)
      ->save();
    $this
      ->assertFalse($this
      ->getIndex()
      ->getServerInstance()
      ->getBackendConfig()['sasm_search_page_include_language_independent']);

    // Stemming "en":
    // gene => gene
    // genes => gene
    //
    // Stemming "de":
    // Gen => gen
    // Gene => gen
    $results = $this
      ->buildSearch('gene', [], [
      'body',
    ])
      ->execute();
    $this
      ->assertResults([
      1 => 'en',
      2 => 'en',
      3 => 'de',
      4 => 'de',
      5 => 'de-at',
      6 => 'de-at',
    ], $results, 'Search all languages for "gene".');
    $config['sasm_limit_search_page_to_content_language'] = TRUE;
    $server
      ->setBackendConfig($config)
      ->save();
    $this
      ->assertTrue($this
      ->getIndex()
      ->getServerInstance()
      ->getBackendConfig()['sasm_limit_search_page_to_content_language']);

    // Current content language is "en".
    $results = $this
      ->buildSearch('gene', [], [
      'body',
    ])
      ->execute();
    $this
      ->assertResults([
      1 => 'en',
      2 => 'en',
    ], $results, 'Search content language for "gene".');

    // A query created by Views must not be overruled.
    $results = $this
      ->buildSearch('gene', [], [
      'body',
    ])
      ->addTag('views')
      ->execute();
    $this
      ->assertResults([
      1 => 'en',
      2 => 'en',
      3 => 'de',
      4 => 'de',
      5 => 'de-at',
      6 => 'de-at',
    ], $results, 'Search all languages for "gene".');
    $config['sasm_search_page_include_language_independent'] = TRUE;
    $server
      ->setBackendConfig($config)
      ->save();
    $this
      ->assertTrue($this
      ->getIndex()
      ->getServerInstance()
      ->getBackendConfig()['sasm_search_page_include_language_independent']);
    $results = $this
      ->buildSearch('gene', [], [
      'body',
    ])
      ->execute();
    $this
      ->assertResults([
      1 => 'en',
      2 => 'en',
      7 => LanguageInterface::LANGCODE_NOT_SPECIFIED,
      8 => LanguageInterface::LANGCODE_NOT_APPLICABLE,
    ], $results, 'Search content and unspecified language for "gene".');
    $config['sasm_limit_search_page_to_content_language'] = FALSE;
    $server
      ->setBackendConfig($config)
      ->save();
    $this
      ->assertFalse($this
      ->getIndex()
      ->getServerInstance()
      ->getBackendConfig()['sasm_limit_search_page_to_content_language']);
    $results = $this
      ->buildSearch('gene', [], [
      'body',
    ])
      ->execute();
    $this
      ->assertResults([
      1 => 'en',
      2 => 'en',
      3 => 'de',
      4 => 'de',
      5 => 'de-at',
      6 => 'de-at',
      7 => LanguageInterface::LANGCODE_NOT_SPECIFIED,
      8 => LanguageInterface::LANGCODE_NOT_APPLICABLE,
    ], $results, 'Search all and unspecified languages for "gene".');
  }
  else {
    $this
      ->assertTrue(TRUE, 'Error: The Solr instance could not be found. Please enable a multi-core one on http://localhost:8983/solr/d8');
  }
}