You are here

public function LanguageWithFallbackTest::testIndexing in Search API 8

Tests indexing.

Expected fallbacks: search_api_test_language_fallback.module has these:

  • no fallbacks
  • except 'fr' has fallback 'es'

Note that language_fallback_fix.module (which is a test dependency) ensures that there can be languages without fallback, which we test here.

@covers ::addFieldValues

Throws

\Drupal\Core\Entity\EntityStorageException

File

tests/src/Kernel/Processor/LanguageWithFallbackTest.php, line 80

Class

LanguageWithFallbackTest
Tests the "Language (with fallback)" processor at a higher level.

Namespace

Drupal\Tests\search_api\Kernel\Processor

Code

public function testIndexing() {
  $nodeValues = [
    'title' => 'Test',
    'type' => 'article',
  ];

  // First test with a German node.
  $node = Node::create($nodeValues + [
    'langcode' => 'de',
  ]);
  $node
    ->save();
  $this->node = $node;
  $this
    ->triggerPostRequestIndexing();
  $expected[$this
    ->getItemIdForLanguage('de')] = [
    'de',
  ];
  $this
    ->assertEquals($expected, $this
    ->getLanguageWithFallbackValues(), 'Added default translation is indexed correctly.');
  $node
    ->addTranslation('es', $nodeValues);
  $node
    ->save();
  $this
    ->triggerPostRequestIndexing();
  $expected[$this
    ->getItemIdForLanguage('es')] = [
    'es',
    'fr',
  ];
  $this
    ->assertEquals($expected, $this
    ->getLanguageWithFallbackValues(), 'Added translation with fallback is indexed correctly.');
  $node
    ->addTranslation('fr', $nodeValues);
  $node
    ->save();
  $this
    ->triggerPostRequestIndexing();
  $expected[$this
    ->getItemIdForLanguage('es')] = [
    'es',
  ];
  $expected[$this
    ->getItemIdForLanguage('fr')] = [
    'fr',
  ];
  $this
    ->assertEquals($expected, $this
    ->getLanguageWithFallbackValues(), 'Added translation is indexed correctly and former fallback removed.');
  $node
    ->removeTranslation('fr');
  $node
    ->save();
  $this
    ->triggerPostRequestIndexing();
  unset($expected[$this
    ->getItemIdForLanguage('fr')]);
  $expected[$this
    ->getItemIdForLanguage('es')] = [
    'es',
    'fr',
  ];
  $this
    ->assertEquals($expected, $this
    ->getLanguageWithFallbackValues(), 'Removed translation is unindexed correctly and fallback re-added.');
  $node
    ->removeTranslation('es');
  $node
    ->save();
  $this
    ->triggerPostRequestIndexing();
  unset($expected[$this
    ->getItemIdForLanguage('es')]);
  $this
    ->assertEquals($expected, $this
    ->getLanguageWithFallbackValues(), 'Removed translation is unindexed correctly.');
  $node
    ->delete();
  $this
    ->triggerPostRequestIndexing();
  $expected = [];
  $this
    ->assertEquals($expected, $this
    ->getLanguageWithFallbackValues(), 'Removed default translation is unindexed correctly.');

  // Then test with a Spanish node.
  $node = Node::create($nodeValues + [
    'langcode' => 'es',
  ]);
  $node
    ->save();
  $this->node = $node;
  $this
    ->triggerPostRequestIndexing();
  $expected[$this
    ->getItemIdForLanguage('es')] = [
    'es',
    'fr',
  ];
  $this
    ->assertEquals($expected, $this
    ->getLanguageWithFallbackValues(), 'Added default translation with fallback is indexed correctly.');
  $node
    ->addTranslation('de', $nodeValues);
  $node
    ->save();
  $this
    ->triggerPostRequestIndexing();
  $expected[$this
    ->getItemIdForLanguage('de')] = [
    'de',
  ];
  $this
    ->assertEquals($expected, $this
    ->getLanguageWithFallbackValues(), 'Added translation is indexed correctly.');
  $node
    ->addTranslation('fr', $nodeValues);
  $node
    ->save();
  $this
    ->triggerPostRequestIndexing();
  $expected[$this
    ->getItemIdForLanguage('es')] = [
    'es',
  ];
  $expected[$this
    ->getItemIdForLanguage('fr')] = [
    'fr',
  ];
  $this
    ->assertEquals($expected, $this
    ->getLanguageWithFallbackValues(), 'Added translation is indexed correctly and former fallback removed.');
  $node
    ->removeTranslation('de');
  $node
    ->save();
  $this
    ->triggerPostRequestIndexing();
  unset($expected[$this
    ->getItemIdForLanguage('de')]);
  $this
    ->assertEquals($expected, $this
    ->getLanguageWithFallbackValues(), 'Removed translation is unindexed correctly.');
  $node
    ->removeTranslation('fr');
  $node
    ->save();
  $this
    ->triggerPostRequestIndexing();
  unset($expected[$this
    ->getItemIdForLanguage('fr')]);
  $expected[$this
    ->getItemIdForLanguage('es')] = [
    'es',
    'fr',
  ];
  $this
    ->assertEquals($expected, $this
    ->getLanguageWithFallbackValues(), 'Removed translation is unindexed correctly and fallback re-added.');
  $node
    ->delete();
  $this
    ->triggerPostRequestIndexing();
  $expected = [];
  $this
    ->assertEquals($expected, $this
    ->getLanguageWithFallbackValues(), 'Removed default translation is unindexed correctly.');
}