Mercurial > hg > isophonics-drupal-site
comparison core/modules/locale/tests/src/Unit/LocaleLookupTest.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 7a779792577d |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\Tests\locale\Unit; | |
4 | |
5 use Drupal\Core\DependencyInjection\ContainerBuilder; | |
6 use Drupal\locale\LocaleLookup; | |
7 use Drupal\Tests\UnitTestCase; | |
8 use Symfony\Component\HttpFoundation\Request; | |
9 use Symfony\Component\HttpFoundation\RequestStack; | |
10 | |
11 /** | |
12 * @coversDefaultClass \Drupal\locale\LocaleLookup | |
13 * @group locale | |
14 */ | |
15 class LocaleLookupTest extends UnitTestCase { | |
16 | |
17 /** | |
18 * A mocked storage to use when instantiating LocaleTranslation objects. | |
19 * | |
20 * @var \Drupal\locale\StringStorageInterface|\PHPUnit_Framework_MockObject_MockObject | |
21 */ | |
22 protected $storage; | |
23 | |
24 /** | |
25 * A mocked cache object. | |
26 * | |
27 * @var \Drupal\Core\Cache\CacheBackendInterface|\PHPUnit_Framework_MockObject_MockObject | |
28 */ | |
29 protected $cache; | |
30 | |
31 /** | |
32 * A mocked lock object. | |
33 * | |
34 * @var \Drupal\Core\Lock\LockBackendInterface|\PHPUnit_Framework_MockObject_MockObject | |
35 */ | |
36 protected $lock; | |
37 | |
38 /** | |
39 * A mocked user object built from AccountInterface. | |
40 * | |
41 * @var \Drupal\Core\Session\AccountInterface|\PHPUnit_Framework_MockObject_MockObject | |
42 */ | |
43 protected $user; | |
44 | |
45 /** | |
46 * A mocked config factory built with UnitTestCase::getConfigFactoryStub(). | |
47 * | |
48 * @var \Drupal\Core\Config\ConfigFactory|\PHPUnit_Framework_MockObject_MockBuilder | |
49 */ | |
50 protected $configFactory; | |
51 | |
52 /** | |
53 * A mocked language manager built from LanguageManagerInterface. | |
54 * | |
55 * @var \Drupal\Core\Language\LanguageManagerInterface|\PHPUnit_Framework_MockObject_MockObject | |
56 */ | |
57 protected $languageManager; | |
58 | |
59 /** | |
60 * The request stack. | |
61 * | |
62 * @var \Symfony\Component\HttpFoundation\RequestStack | |
63 */ | |
64 protected $requestStack; | |
65 | |
66 /** | |
67 * {@inheritdoc} | |
68 */ | |
69 protected function setUp() { | |
70 $this->storage = $this->getMock('Drupal\locale\StringStorageInterface'); | |
71 $this->cache = $this->getMock('Drupal\Core\Cache\CacheBackendInterface'); | |
72 $this->lock = $this->getMock('Drupal\Core\Lock\LockBackendInterface'); | |
73 $this->lock->expects($this->never()) | |
74 ->method($this->anything()); | |
75 | |
76 $this->user = $this->getMock('Drupal\Core\Session\AccountInterface'); | |
77 $this->user->expects($this->any()) | |
78 ->method('getRoles') | |
79 ->will($this->returnValue(['anonymous'])); | |
80 | |
81 $this->configFactory = $this->getConfigFactoryStub(['locale.settings' => ['cache_strings' => FALSE]]); | |
82 | |
83 $this->languageManager = $this->getMock('Drupal\Core\Language\LanguageManagerInterface'); | |
84 $this->requestStack = new RequestStack(); | |
85 | |
86 $container = new ContainerBuilder(); | |
87 $container->set('current_user', $this->user); | |
88 \Drupal::setContainer($container); | |
89 } | |
90 | |
91 /** | |
92 * Tests locale lookups without fallback. | |
93 * | |
94 * @covers ::resolveCacheMiss | |
95 */ | |
96 public function testResolveCacheMissWithoutFallback() { | |
97 $args = [ | |
98 'language' => 'en', | |
99 'source' => 'test', | |
100 'context' => 'irrelevant', | |
101 ]; | |
102 | |
103 $result = (object) [ | |
104 'translation' => 'test', | |
105 ]; | |
106 | |
107 $this->cache->expects($this->once()) | |
108 ->method('get') | |
109 ->with('locale:en:irrelevant:anonymous', FALSE); | |
110 | |
111 $this->storage->expects($this->once()) | |
112 ->method('findTranslation') | |
113 ->with($this->equalTo($args)) | |
114 ->will($this->returnValue($result)); | |
115 | |
116 $locale_lookup = $this->getMockBuilder('Drupal\locale\LocaleLookup') | |
117 ->setConstructorArgs(['en', 'irrelevant', $this->storage, $this->cache, $this->lock, $this->configFactory, $this->languageManager, $this->requestStack]) | |
118 ->setMethods(['persist']) | |
119 ->getMock(); | |
120 $locale_lookup->expects($this->never()) | |
121 ->method('persist'); | |
122 $this->assertSame('test', $locale_lookup->get('test')); | |
123 } | |
124 | |
125 /** | |
126 * Tests locale lookups with fallback. | |
127 * | |
128 * Note that context is irrelevant here. It is not used but it is required. | |
129 * | |
130 * @covers ::resolveCacheMiss | |
131 * | |
132 * @dataProvider resolveCacheMissWithFallbackProvider | |
133 */ | |
134 public function testResolveCacheMissWithFallback($langcode, $string, $context, $expected) { | |
135 // These are fake words! | |
136 $translations = [ | |
137 'en' => [ | |
138 'test' => 'test', | |
139 'fake' => 'fake', | |
140 'missing pl' => 'missing pl', | |
141 'missing cs' => 'missing cs', | |
142 'missing both' => 'missing both', | |
143 ], | |
144 'pl' => [ | |
145 'test' => 'test po polsku', | |
146 'fake' => 'ściema', | |
147 'missing cs' => 'zaginiony czech', | |
148 ], | |
149 'cs' => [ | |
150 'test' => 'test v české', | |
151 'fake' => 'falešný', | |
152 'missing pl' => 'chybějící pl', | |
153 ], | |
154 ]; | |
155 $this->storage->expects($this->any()) | |
156 ->method('findTranslation') | |
157 ->will($this->returnCallback(function ($argument) use ($translations) { | |
158 if (isset($translations[$argument['language']][$argument['source']])) { | |
159 return (object) ['translation' => $translations[$argument['language']][$argument['source']]]; | |
160 } | |
161 return TRUE; | |
162 })); | |
163 | |
164 $this->languageManager->expects($this->any()) | |
165 ->method('getFallbackCandidates') | |
166 ->will($this->returnCallback(function (array $context = []) { | |
167 switch ($context['langcode']) { | |
168 case 'pl': | |
169 return ['cs', 'en']; | |
170 | |
171 case 'cs': | |
172 return ['en']; | |
173 | |
174 default: | |
175 return []; | |
176 } | |
177 })); | |
178 | |
179 $this->cache->expects($this->once()) | |
180 ->method('get') | |
181 ->with('locale:' . $langcode . ':' . $context . ':anonymous', FALSE); | |
182 | |
183 $locale_lookup = new LocaleLookup($langcode, $context, $this->storage, $this->cache, $this->lock, $this->configFactory, $this->languageManager, $this->requestStack); | |
184 $this->assertSame($expected, $locale_lookup->get($string)); | |
185 } | |
186 | |
187 /** | |
188 * Provides test data for testResolveCacheMissWithFallback(). | |
189 */ | |
190 public function resolveCacheMissWithFallbackProvider() { | |
191 return [ | |
192 ['cs', 'test', 'irrelevant', 'test v české'], | |
193 ['cs', 'fake', 'irrelevant', 'falešný'], | |
194 ['cs', 'missing pl', 'irrelevant', 'chybějící pl'], | |
195 ['cs', 'missing cs', 'irrelevant', 'missing cs'], | |
196 ['cs', 'missing both', 'irrelevant', 'missing both'], | |
197 | |
198 // Testing PL with fallback to cs, en. | |
199 ['pl', 'test', 'irrelevant', 'test po polsku'], | |
200 ['pl', 'fake', 'irrelevant', 'ściema'], | |
201 ['pl', 'missing pl', 'irrelevant', 'chybějící pl'], | |
202 ['pl', 'missing cs', 'irrelevant', 'zaginiony czech'], | |
203 ['pl', 'missing both', 'irrelevant', 'missing both'], | |
204 ]; | |
205 } | |
206 | |
207 /** | |
208 * Tests locale lookups with persistent tracking. | |
209 * | |
210 * @covers ::resolveCacheMiss | |
211 */ | |
212 public function testResolveCacheMissWithPersist() { | |
213 $args = [ | |
214 'language' => 'en', | |
215 'source' => 'test', | |
216 'context' => 'irrelevant', | |
217 ]; | |
218 | |
219 $result = (object) [ | |
220 'translation' => 'test', | |
221 ]; | |
222 | |
223 $this->storage->expects($this->once()) | |
224 ->method('findTranslation') | |
225 ->with($this->equalTo($args)) | |
226 ->will($this->returnValue($result)); | |
227 | |
228 $this->configFactory = $this->getConfigFactoryStub(['locale.settings' => ['cache_strings' => TRUE]]); | |
229 $locale_lookup = $this->getMockBuilder('Drupal\locale\LocaleLookup') | |
230 ->setConstructorArgs(['en', 'irrelevant', $this->storage, $this->cache, $this->lock, $this->configFactory, $this->languageManager, $this->requestStack]) | |
231 ->setMethods(['persist']) | |
232 ->getMock(); | |
233 $locale_lookup->expects($this->once()) | |
234 ->method('persist'); | |
235 | |
236 $this->assertSame('test', $locale_lookup->get('test')); | |
237 } | |
238 | |
239 /** | |
240 * Tests locale lookups without a found translation. | |
241 * | |
242 * @covers ::resolveCacheMiss | |
243 */ | |
244 public function testResolveCacheMissNoTranslation() { | |
245 $string = $this->getMock('Drupal\locale\StringInterface'); | |
246 $string->expects($this->once()) | |
247 ->method('addLocation') | |
248 ->will($this->returnSelf()); | |
249 $this->storage->expects($this->once()) | |
250 ->method('findTranslation') | |
251 ->will($this->returnValue(NULL)); | |
252 $this->storage->expects($this->once()) | |
253 ->method('createString') | |
254 ->will($this->returnValue($string)); | |
255 | |
256 $request = Request::create('/test'); | |
257 $this->requestStack->push($request); | |
258 | |
259 $locale_lookup = $this->getMockBuilder('Drupal\locale\LocaleLookup') | |
260 ->setConstructorArgs(['en', 'irrelevant', $this->storage, $this->cache, $this->lock, $this->configFactory, $this->languageManager, $this->requestStack]) | |
261 ->setMethods(['persist']) | |
262 ->getMock(); | |
263 $locale_lookup->expects($this->never()) | |
264 ->method('persist'); | |
265 | |
266 $this->assertTrue($locale_lookup->get('test')); | |
267 } | |
268 | |
269 } |