annotate core/modules/locale/tests/src/Unit/LocaleLookupTest.php @ 2:92f882872392

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