function LocaleLookupTest::testResolveCacheMissWithFallback

Same name in other branches
  1. 8.9.x 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()
  3. 11.x 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));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.