3 namespace Drupal\Tests\locale\Unit;
5 use Drupal\Core\DependencyInjection\ContainerBuilder;
6 use Drupal\Core\StringTranslation\PluralTranslatableMarkup;
7 use Drupal\locale\LocaleLookup;
8 use Drupal\Tests\UnitTestCase;
9 use Symfony\Component\HttpFoundation\Request;
10 use Symfony\Component\HttpFoundation\RequestStack;
13 * @coversDefaultClass \Drupal\locale\LocaleLookup
16 class LocaleLookupTest extends UnitTestCase {
19 * A mocked storage to use when instantiating LocaleTranslation objects.
21 * @var \Drupal\locale\StringStorageInterface|\PHPUnit_Framework_MockObject_MockObject
26 * A mocked cache object.
28 * @var \Drupal\Core\Cache\CacheBackendInterface|\PHPUnit_Framework_MockObject_MockObject
33 * A mocked lock object.
35 * @var \Drupal\Core\Lock\LockBackendInterface|\PHPUnit_Framework_MockObject_MockObject
40 * A mocked user object built from AccountInterface.
42 * @var \Drupal\Core\Session\AccountInterface|\PHPUnit_Framework_MockObject_MockObject
47 * A mocked config factory built with UnitTestCase::getConfigFactoryStub().
49 * @var \Drupal\Core\Config\ConfigFactory|\PHPUnit_Framework_MockObject_MockBuilder
51 protected $configFactory;
54 * A mocked language manager built from LanguageManagerInterface.
56 * @var \Drupal\Core\Language\LanguageManagerInterface|\PHPUnit_Framework_MockObject_MockObject
58 protected $languageManager;
63 * @var \Symfony\Component\HttpFoundation\RequestStack
65 protected $requestStack;
70 protected function setUp() {
71 $this->storage = $this->getMock('Drupal\locale\StringStorageInterface');
72 $this->cache = $this->getMock('Drupal\Core\Cache\CacheBackendInterface');
73 $this->lock = $this->getMock('Drupal\Core\Lock\LockBackendInterface');
74 $this->lock->expects($this->never())
75 ->method($this->anything());
77 $this->user = $this->getMock('Drupal\Core\Session\AccountInterface');
78 $this->user->expects($this->any())
80 ->will($this->returnValue(['anonymous']));
82 $this->configFactory = $this->getConfigFactoryStub(['locale.settings' => ['cache_strings' => FALSE]]);
84 $this->languageManager = $this->getMock('Drupal\Core\Language\LanguageManagerInterface');
85 $this->requestStack = new RequestStack();
87 $container = new ContainerBuilder();
88 $container->set('current_user', $this->user);
89 \Drupal::setContainer($container);
93 * Tests locale lookups without fallback.
95 * @covers ::resolveCacheMiss
97 public function testResolveCacheMissWithoutFallback() {
101 'context' => 'irrelevant',
105 'translation' => 'test',
108 $this->cache->expects($this->once())
110 ->with('locale:en:irrelevant:anonymous', FALSE);
112 $this->storage->expects($this->once())
113 ->method('findTranslation')
114 ->with($this->equalTo($args))
115 ->will($this->returnValue($result));
117 $locale_lookup = $this->getMockBuilder('Drupal\locale\LocaleLookup')
118 ->setConstructorArgs(['en', 'irrelevant', $this->storage, $this->cache, $this->lock, $this->configFactory, $this->languageManager, $this->requestStack])
119 ->setMethods(['persist'])
121 $locale_lookup->expects($this->never())
123 $this->assertSame('test', $locale_lookup->get('test'));
127 * Tests locale lookups with fallback.
129 * Note that context is irrelevant here. It is not used but it is required.
131 * @covers ::resolveCacheMiss
133 * @dataProvider resolveCacheMissWithFallbackProvider
135 public function testResolveCacheMissWithFallback($langcode, $string, $context, $expected) {
136 // These are fake words!
141 'missing pl' => 'missing pl',
142 'missing cs' => 'missing cs',
143 'missing both' => 'missing both',
146 'test' => 'test po polsku',
148 'missing cs' => 'zaginiony czech',
151 'test' => 'test v české',
153 'missing pl' => 'chybějící pl',
156 $this->storage->expects($this->any())
157 ->method('findTranslation')
158 ->will($this->returnCallback(function ($argument) use ($translations) {
159 if (isset($translations[$argument['language']][$argument['source']])) {
160 return (object) ['translation' => $translations[$argument['language']][$argument['source']]];
165 $this->languageManager->expects($this->any())
166 ->method('getFallbackCandidates')
167 ->will($this->returnCallback(function (array $context = []) {
168 switch ($context['langcode']) {
180 $this->cache->expects($this->once())
182 ->with('locale:' . $langcode . ':' . $context . ':anonymous', FALSE);
184 $locale_lookup = new LocaleLookup($langcode, $context, $this->storage, $this->cache, $this->lock, $this->configFactory, $this->languageManager, $this->requestStack);
185 $this->assertSame($expected, $locale_lookup->get($string));
189 * Provides test data for testResolveCacheMissWithFallback().
191 public function resolveCacheMissWithFallbackProvider() {
193 ['cs', 'test', 'irrelevant', 'test v české'],
194 ['cs', 'fake', 'irrelevant', 'falešný'],
195 ['cs', 'missing pl', 'irrelevant', 'chybějící pl'],
196 ['cs', 'missing cs', 'irrelevant', 'missing cs'],
197 ['cs', 'missing both', 'irrelevant', 'missing both'],
199 // Testing PL with fallback to cs, en.
200 ['pl', 'test', 'irrelevant', 'test po polsku'],
201 ['pl', 'fake', 'irrelevant', 'ściema'],
202 ['pl', 'missing pl', 'irrelevant', 'chybějící pl'],
203 ['pl', 'missing cs', 'irrelevant', 'zaginiony czech'],
204 ['pl', 'missing both', 'irrelevant', 'missing both'],
209 * Tests locale lookups with persistent tracking.
211 * @covers ::resolveCacheMiss
213 public function testResolveCacheMissWithPersist() {
217 'context' => 'irrelevant',
221 'translation' => 'test',
224 $this->storage->expects($this->once())
225 ->method('findTranslation')
226 ->with($this->equalTo($args))
227 ->will($this->returnValue($result));
229 $this->configFactory = $this->getConfigFactoryStub(['locale.settings' => ['cache_strings' => TRUE]]);
230 $locale_lookup = $this->getMockBuilder('Drupal\locale\LocaleLookup')
231 ->setConstructorArgs(['en', 'irrelevant', $this->storage, $this->cache, $this->lock, $this->configFactory, $this->languageManager, $this->requestStack])
232 ->setMethods(['persist'])
234 $locale_lookup->expects($this->once())
237 $this->assertSame('test', $locale_lookup->get('test'));
241 * Tests locale lookups without a found translation.
243 * @covers ::resolveCacheMiss
245 public function testResolveCacheMissNoTranslation() {
246 $string = $this->getMock('Drupal\locale\StringInterface');
247 $string->expects($this->once())
248 ->method('addLocation')
249 ->will($this->returnSelf());
250 $this->storage->expects($this->once())
251 ->method('findTranslation')
252 ->will($this->returnValue(NULL));
253 $this->storage->expects($this->once())
254 ->method('createString')
255 ->will($this->returnValue($string));
257 $request = Request::create('/test');
258 $this->requestStack->push($request);
260 $locale_lookup = $this->getMockBuilder('Drupal\locale\LocaleLookup')
261 ->setConstructorArgs(['en', 'irrelevant', $this->storage, $this->cache, $this->lock, $this->configFactory, $this->languageManager, $this->requestStack])
262 ->setMethods(['persist'])
264 $locale_lookup->expects($this->never())
267 $this->assertTrue($locale_lookup->get('test'));
271 * Tests locale lookups with old plural style of translations.
273 * @param array $translations
274 * The source with translations.
275 * @param string $langcode
276 * The language code of translation string.
277 * @param string $string
278 * The string for translation.
279 * @param bool $is_fix
280 * The flag about expected fix translation.
282 * @covers ::resolveCacheMiss
283 * @dataProvider providerFixOldPluralTranslationProvider
285 public function testFixOldPluralStyleTranslations($translations, $langcode, $string, $is_fix) {
286 $this->storage->expects($this->any())
287 ->method('findTranslation')
288 ->will($this->returnCallback(function ($argument) use ($translations) {
289 if (isset($translations[$argument['language']][$argument['source']])) {
290 return (object) ['translation' => $translations[$argument['language']][$argument['source']]];
294 $this->languageManager->expects($this->any())
295 ->method('getFallbackCandidates')
296 ->will($this->returnCallback(function (array $context = []) {
297 switch ($context['langcode']) {
302 $this->cache->expects($this->once())
304 ->with('locale:' . $langcode . '::anonymous', FALSE);
306 $locale_lookup = new LocaleLookup($langcode, '', $this->storage, $this->cache, $this->lock, $this->configFactory, $this->languageManager, $this->requestStack);
307 $this->assertSame($is_fix, strpos($locale_lookup->get($string), '@count[2]') === FALSE);
311 * Provides test data for testResolveCacheMissWithFallback().
313 public function providerFixOldPluralTranslationProvider() {
316 'word1' => '@count[2] word-by',
317 'word2' => implode(PluralTranslatableMarkup::DELIMITER, ['word-by', '@count[2] word-by']),
320 'word3' => '@count[2] word-ru',
321 'word4' => implode(PluralTranslatableMarkup::DELIMITER, ['word-ru', '@count[2] word-ru']),
325 'no-plural' => [$translations, 'by', 'word1', FALSE],
326 'no-plural from other language' => [$translations, 'by', 'word3', FALSE],
327 'plural' => [$translations, 'by', 'word2', TRUE],
328 'plural from other language' => [$translations, 'by', 'word4', TRUE],