1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
|
<?php
declare(strict_types=1);
namespace Drupal\Tests\language\Unit;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Tests\UnitTestCase;
use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpFoundation\Request;
/**
* @coversDefaultClass \Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl
* @group language
*/
class LanguageNegotiationUrlTest extends UnitTestCase {
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* The test user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $user;
/**
* An array of languages.
*/
protected array $languages;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
// Set up some languages to be used by the language-based path processor.
$language_de = $this->createMock('\Drupal\Core\Language\LanguageInterface');
$language_de->expects($this->any())
->method('getId')
->willReturn('de');
$language_en = $this->createMock('\Drupal\Core\Language\LanguageInterface');
$language_en->expects($this->any())
->method('getId')
->willReturn('en');
$languages = [
'de' => $language_de,
'en' => $language_en,
];
$this->languages = $languages;
// Create a language manager stub.
$language_manager = $this->getMockBuilder('Drupal\language\ConfigurableLanguageManagerInterface')
->getMock();
$language_manager->expects($this->any())
->method('getLanguages')
->willReturn($languages);
$this->languageManager = $language_manager;
// Create a user stub.
$this->user = $this->getMockBuilder('Drupal\Core\Session\AccountInterface')
->getMock();
$cache_contexts_manager = $this->getMockBuilder('Drupal\Core\Cache\Context\CacheContextsManager')
->disableOriginalConstructor()
->getMock();
$cache_contexts_manager->method('assertValidTokens')->willReturn(TRUE);
$container = new ContainerBuilder();
$container->set('cache_contexts_manager', $cache_contexts_manager);
\Drupal::setContainer($container);
}
/**
* Tests path prefix language negotiation and outbound path processing.
*
* @dataProvider providerTestPathPrefix
*/
public function testPathPrefix($prefix, $prefixes, $expected_langcode): void {
$this->languageManager->expects($this->any())
->method('getCurrentLanguage')
->willReturn($this->languages[(in_array($expected_langcode, [
'en',
'de',
])) ? $expected_langcode : 'en']);
$config = $this->getConfigFactoryStub([
'language.negotiation' => [
'url' => [
'source' => LanguageNegotiationUrl::CONFIG_PATH_PREFIX,
'prefixes' => $prefixes,
],
],
]);
$request = Request::create('/' . $prefix . '/foo', 'GET');
$method = new LanguageNegotiationUrl();
$method->setLanguageManager($this->languageManager);
$method->setConfig($config);
$method->setCurrentUser($this->user);
$this->assertEquals($expected_langcode, $method->getLangcode($request));
$cacheability = new BubbleableMetadata();
$options = [];
$method->processOutbound('foo', $options, $request, $cacheability);
$expected_cacheability = new BubbleableMetadata();
if ($expected_langcode) {
$this->assertSame($prefix . '/', $options['prefix']);
$expected_cacheability->setCacheContexts(['languages:' . LanguageInterface::TYPE_URL]);
}
else {
$this->assertFalse(isset($options['prefix']));
}
$this->assertEquals($expected_cacheability, $cacheability);
}
/**
* Provides data for the path prefix test.
*
* @return array
* An array of data for checking path prefix negotiation.
*/
public static function providerTestPathPrefix() {
$path_prefix_configuration[] = [
'prefix' => 'de',
'prefixes' => [
'de' => 'de',
'en-uk' => 'en',
],
'expected_langcode' => 'de',
];
$path_prefix_configuration[] = [
'prefix' => 'en-uk',
'prefixes' => [
'de' => 'de',
'en' => 'en-uk',
],
'expected_langcode' => 'en',
];
// No configuration.
$path_prefix_configuration[] = [
'prefix' => 'de',
'prefixes' => [],
'expected_langcode' => FALSE,
];
// Non-matching prefix.
$path_prefix_configuration[] = [
'prefix' => 'de',
'prefixes' => [
'en-uk' => 'en',
],
'expected_langcode' => FALSE,
];
// Non-existing language.
$path_prefix_configuration[] = [
'prefix' => 'it',
'prefixes' => [
'it' => 'it',
'en-uk' => 'en',
],
'expected_langcode' => FALSE,
];
return $path_prefix_configuration;
}
/**
* Tests outbound path processing for neutral languages.
*
* @dataProvider providerNeutralLanguages
*/
public function testNeutralLanguages($langcode, $expected_langcode): void {
if ($expected_langcode) {
$this->languageManager->expects($this->once())
->method('getCurrentLanguage')
->willReturn($this->languages['en']);
}
$config = $this->getConfigFactoryStub([
'language.negotiation' => [
'url' => [
'source' => LanguageNegotiationUrl::CONFIG_PATH_PREFIX,
'prefixes' => [
'de' => 'de',
'en' => 'en',
],
],
],
]);
$request = Request::create('/foo', 'GET');
$method = new LanguageNegotiationUrl();
$method->setLanguageManager($this->languageManager);
$method->setConfig($config);
$method->setCurrentUser($this->user);
$this->assertNull($method->getLangcode($request));
$language = $this->createMock(LanguageInterface::class);
$language->expects($this->any())
->method('getId')
->willReturn($langcode);
$cacheability = new BubbleableMetadata();
$options = [
'language' => $language,
];
$method->processOutbound('foo', $options, $request, $cacheability);
$expected_cacheability = new BubbleableMetadata();
if ($expected_langcode) {
$this->assertSame($expected_langcode . '/', $options['prefix']);
$expected_cacheability->setCacheContexts(['languages:' . LanguageInterface::TYPE_URL]);
}
else {
$this->assertFalse(isset($options['prefix']));
}
$this->assertEquals($expected_cacheability, $cacheability);
}
/**
* Provides data for the neutral language test.
*
* @return array
* An array of data for checking path prefix negotiation for neutral
* languages.
*/
public static function providerNeutralLanguages() {
return [
[LanguageInterface::LANGCODE_NOT_APPLICABLE, NULL],
[LanguageInterface::LANGCODE_NOT_SPECIFIED, 'en'],
];
}
/**
* Tests domain language negotiation and outbound path processing.
*
* @dataProvider providerTestDomain
*/
public function testDomain($http_host, $domains, $expected_langcode): void {
$this->languageManager->expects($this->any())
->method('getCurrentLanguage')
->willReturn($this->languages['en']);
$config = $this->getConfigFactoryStub([
'language.negotiation' => [
'url' => [
'source' => LanguageNegotiationUrl::CONFIG_DOMAIN,
'domains' => $domains,
],
],
]);
$request = Request::create('', 'GET', [], [], [], ['HTTP_HOST' => $http_host]);
$method = new LanguageNegotiationUrl();
$method->setLanguageManager($this->languageManager);
$method->setConfig($config);
$method->setCurrentUser($this->user);
$this->assertEquals($expected_langcode, $method->getLangcode($request));
$cacheability = new BubbleableMetadata();
$options = [];
$this->assertSame('foo', $method->processOutbound('foo', $options, $request, $cacheability));
$expected_cacheability = new BubbleableMetadata();
if ($expected_langcode !== FALSE && count($domains) > 1) {
$expected_cacheability->setCacheMaxAge(
Cache::PERMANENT)->setCacheContexts(['languages:' . LanguageInterface::TYPE_URL, 'url.site']);
}
$this->assertEquals($expected_cacheability, $cacheability);
}
/**
* Provides data for the domain test.
*
* @return array
* An array of data for checking domain negotiation.
*/
public static function providerTestDomain() {
$domain_configuration[] = [
'http_host' => 'example.de',
'domains' => [
'de' => 'http://example.de',
],
'expected_langcode' => 'de',
];
// No configuration.
$domain_configuration[] = [
'http_host' => 'example.de',
'domains' => [],
'expected_langcode' => FALSE,
];
// HTTP host with a port.
$domain_configuration[] = [
'http_host' => 'example.de:8080',
'domains' => [
'de' => 'http://example.de',
],
'expected_langcode' => 'de',
];
// Domain configuration with https://.
$domain_configuration[] = [
'http_host' => 'example.de',
'domains' => [
'de' => 'https://example.de',
],
'expected_langcode' => 'de',
];
// Non-matching HTTP host.
$domain_configuration[] = [
'http_host' => 'example.com',
'domains' => [
'de' => 'http://example.com',
],
'expected_langcode' => 'de',
];
// Testing a non-existing language.
$domain_configuration[] = [
'http_host' => 'example.com',
'domains' => [
'it' => 'http://example.it',
],
'expected_langcode' => FALSE,
];
// Multiple domain configurations.
$domain_configuration[] = [
'http_host' => 'example.com',
'domains' => [
'de' => 'http://example.de',
'en' => 'http://example.com',
],
'expected_langcode' => 'en',
];
return $domain_configuration;
}
/**
* Tests path outbound processing correctly setting relative/absolute paths.
*/
public function testProcessOutboundOutputsRelativePathsForSameDomain(): void {
$this->languageManager->expects($this->any())
->method('getCurrentLanguage')
->willReturn($this->languages['en']);
$config = $this->getConfigFactoryStub([
'language.negotiation' => [
'url' => [
'source' => LanguageNegotiationUrl::CONFIG_DOMAIN,
'domains' => [
'de' => 'example.de',
'en' => 'example.com',
],
],
],
]);
$request = Request::create('', 'GET', [], [], [], ['HTTP_HOST' => 'example.com']);
$method = new LanguageNegotiationUrl();
$method->setLanguageManager($this->languageManager);
$method->setConfig($config);
$method->setCurrentUser($this->user);
// Check relative paths are used when the language
// is the current language.
$options = [
'language' => $this->languages['en'],
];
$method->processOutbound('foo', $options, $request);
// $options['absolute'] not set or null equals to FALSE.
$this->assertFalse($options['absolute'] ?? FALSE);
// Check absolute paths are used when the language
// is not the current language.
$options = [
'language' => $this->languages['de'],
];
$method->processOutbound('foo', $options, $request);
$this->assertTrue($options['absolute']);
}
}
// @todo Remove as part of https://www.drupal.org/node/2481833.
namespace Drupal\language\Plugin\LanguageNegotiation;
if (!function_exists('base_path')) {
/**
* Returns the base path.
*/
function base_path() {
return '/';
}
}
|