You are here

public function LocaleLookupTest::testResolveCacheMissWithFallback in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/locale/tests/src/Unit/LocaleLookupTest.php \Drupal\Tests\locale\Unit\LocaleLookupTest::testResolveCacheMissWithFallback()
  2. 10 core/modules/locale/tests/src/Unit/LocaleLookupTest.php \Drupal\Tests\locale\Unit\LocaleLookupTest::testResolveCacheMissWithFallback()

Tests locale lookups with fallback.

Note that context is irrelevant here. It is not used but it is required.

@covers ::resolveCacheMiss

@dataProvider resolveCacheMissWithFallbackProvider

File

core/modules/locale/tests/src/Unit/LocaleLookupTest.php, line 135

Class

LocaleLookupTest
@coversDefaultClass \Drupal\locale\LocaleLookup @group locale

Namespace

Drupal\Tests\locale\Unit

Code

public function testResolveCacheMissWithFallback($langcode, $string, $context, $expected) {

  // These are fake words!
  // cSpell:disable
  $translations = [
    'en' => [
      'test' => 'test',
      'fake' => 'fake',
      'missing pl' => 'missing pl',
      'missing cs' => 'missing cs',
      'missing both' => 'missing both',
    ],
    'pl' => [
      'test' => 'test po polsku',
      'fake' => 'ściema',
      'missing cs' => 'zaginiony czech',
    ],
    'cs' => [
      'test' => 'test v české',
      'fake' => 'falešný',
      'missing pl' => 'chybějící pl',
    ],
  ];

  // cSpell:enable
  $this->storage
    ->expects($this
    ->any())
    ->method('findTranslation')
    ->willReturnCallback(function ($argument) use ($translations) {
    if (isset($translations[$argument['language']][$argument['source']])) {
      return (object) [
        'translation' => $translations[$argument['language']][$argument['source']],
      ];
    }
    return TRUE;
  });
  $this->languageManager
    ->expects($this
    ->any())
    ->method('getFallbackCandidates')
    ->willReturnCallback(function (array $context = []) {
    switch ($context['langcode']) {
      case 'pl':
        return [
          'cs',
          'en',
        ];
      case 'cs':
        return [
          'en',
        ];
      default:
        return [];
    }
  });
  $this->cache
    ->expects($this
    ->once())
    ->method('get')
    ->with('locale:' . $langcode . ':' . $context . ':anonymous', FALSE);
  $locale_lookup = new LocaleLookup($langcode, $context, $this->storage, $this->cache, $this->lock, $this->configFactory, $this->languageManager, $this->requestStack);
  $this
    ->assertSame($expected, $locale_lookup
    ->get($string));
}