You are here

public function LocaleLookupTest::testResolveCacheMissWithFallback in Drupal 8

Same name and namespace in other branches
  1. 9 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!
  $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',
    ],
  ];
  $this->storage
    ->expects($this
    ->any())
    ->method('findTranslation')
    ->will($this
    ->returnCallback(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')
    ->will($this
    ->returnCallback(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));
}