You are here

public function LocaleLookupTest::testResolveCacheMissWithFallback in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 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 139
Contains \Drupal\Tests\locale\Unit\LocaleLookupTest.

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 = array(
    'en' => array(
      'test' => 'test',
      'fake' => 'fake',
      'missing pl' => 'missing pl',
      'missing cs' => 'missing cs',
      'missing both' => 'missing both',
    ),
    'pl' => array(
      'test' => 'test po polsku',
      'fake' => 'ściema',
      'missing cs' => 'zaginiony czech',
    ),
    'cs' => array(
      '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) array(
        'translation' => $translations[$argument['language']][$argument['source']],
      );
    }
    return TRUE;
  }));
  $this->languageManager
    ->expects($this
    ->any())
    ->method('getFallbackCandidates')
    ->will($this
    ->returnCallback(function (array $context = array()) {
    switch ($context['langcode']) {
      case 'pl':
        return array(
          'cs',
          'en',
        );
      case 'cs':
        return array(
          'en',
        );
      default:
        return array();
    }
  }));
  $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));
}