You are here

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

Tests language fallback.

File

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

Class

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

Namespace

Drupal\Tests\search_api_solr_multilingual\Kernel

Code

public function testLanguageFallback() {
  $server = $this
    ->getIndex()
    ->getServerInstance();
  $config = $server
    ->getBackendConfig();

  // $server->setBackendConfig(['solr_version' => '4.5.1'] + $server->getBackendConfig());
  // Only run further tests if we have a Solr core available.
  if ($this->solrAvailable) {
    $config['sasm_language_unspecific_fallback_on_schema_issues'] = FALSE;
    $server
      ->setBackendConfig($config)
      ->save();
    $this
      ->assertFalse($this
      ->getIndex()
      ->getServerInstance()
      ->getBackendConfig()['sasm_language_unspecific_fallback_on_schema_issues']);
    $this
      ->insertMultilingualExampleContent();
    try {
      $this
        ->indexItems($this->indexId);
      $this
        ->fail('Indexing a non-existing language without fallback enabled did not throw an exception.');
    } catch (\Exception $e) {
      $this
        ->assertEquals('logger triggered', $e
        ->getMessage());
    }
    $this
      ->clearIndex();
    $config['sasm_language_unspecific_fallback_on_schema_issues'] = TRUE;
    $server
      ->setBackendConfig($config)
      ->save();
    $this
      ->assertTrue($this
      ->getIndex()
      ->getServerInstance()
      ->getBackendConfig()['sasm_language_unspecific_fallback_on_schema_issues']);
    $this
      ->indexItems($this->indexId);
    $results = $this
      ->buildSearch()
      ->execute();
    $this
      ->assertEquals(6, $results
      ->getResultCount(), 'Number of indexed entities is correct.');

    // Stemming "en":
    // gene => gene
    // genes => gene
    //
    // Stemming "de":
    // Gen => gen
    // Gene => gen
    $query = $this
      ->buildSearch('Gen');
    $query
      ->setLanguages([
      'en',
      'de',
    ]);
    $results = $query
      ->execute();
    $this
      ->assertEquals(2, $results
      ->getResultCount(), 'Two results for "Gen" in German entities. No results for "Gen" in English entities.');
    $query = $this
      ->buildSearch('Gene');
    $query
      ->setLanguages([
      'en',
      'de',
    ]);
    $results = $query
      ->execute();
    $this
      ->assertEquals(4, $results
      ->getResultCount(), 'Two results for "Gene" in German entities. Two results for "Gene" in English entities.');

    // Stemming of "de-at" should fall back to "de".
    $query = $this
      ->buildSearch('Gen');
    $query
      ->setLanguages([
      'de-at',
    ]);
    $results = $query
      ->execute();
    $this
      ->assertEquals(2, $results
      ->getResultCount(), 'Two results for "Gen" in Austrian entities.');
    $query = $this
      ->buildSearch('Gene');
    $query
      ->setLanguages([
      'de-at',
    ]);
    $results = $query
      ->execute();
    $this
      ->assertEquals(2, $results
      ->getResultCount(), 'Two results for "Gene" in Austrian entities.');
  }
  else {
    $this
      ->assertTrue(TRUE, 'Error: The Solr instance could not be found. Please enable a multi-core one on http://localhost:8983/solr/d8');
  }
}