Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests\language\Unit;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Cache\Cache;
|
Chris@0
|
6 use Drupal\Core\Language\LanguageInterface;
|
Chris@0
|
7 use Drupal\Core\Render\BubbleableMetadata;
|
Chris@0
|
8 use Drupal\Tests\UnitTestCase;
|
Chris@0
|
9 use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl;
|
Chris@0
|
10 use Symfony\Component\DependencyInjection\ContainerBuilder;
|
Chris@0
|
11 use Symfony\Component\HttpFoundation\Request;
|
Chris@0
|
12
|
Chris@0
|
13 /**
|
Chris@0
|
14 * @coversDefaultClass \Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl
|
Chris@0
|
15 * @group language
|
Chris@0
|
16 */
|
Chris@0
|
17 class LanguageNegotiationUrlTest extends UnitTestCase {
|
Chris@0
|
18
|
Chris@0
|
19 protected $languageManager;
|
Chris@0
|
20 protected $user;
|
Chris@0
|
21
|
Chris@0
|
22 /**
|
Chris@0
|
23 * {@inheritdoc}
|
Chris@0
|
24 */
|
Chris@0
|
25 protected function setUp() {
|
Chris@0
|
26
|
Chris@0
|
27 // Set up some languages to be used by the language-based path processor.
|
Chris@0
|
28 $language_de = $this->getMock('\Drupal\Core\Language\LanguageInterface');
|
Chris@0
|
29 $language_de->expects($this->any())
|
Chris@0
|
30 ->method('getId')
|
Chris@0
|
31 ->will($this->returnValue('de'));
|
Chris@0
|
32 $language_en = $this->getMock('\Drupal\Core\Language\LanguageInterface');
|
Chris@0
|
33 $language_en->expects($this->any())
|
Chris@0
|
34 ->method('getId')
|
Chris@0
|
35 ->will($this->returnValue('en'));
|
Chris@0
|
36 $languages = [
|
Chris@0
|
37 'de' => $language_de,
|
Chris@0
|
38 'en' => $language_en,
|
Chris@0
|
39 ];
|
Chris@0
|
40 $this->languages = $languages;
|
Chris@0
|
41
|
Chris@0
|
42 // Create a language manager stub.
|
Chris@0
|
43 $language_manager = $this->getMockBuilder('Drupal\language\ConfigurableLanguageManagerInterface')
|
Chris@0
|
44 ->getMock();
|
Chris@0
|
45 $language_manager->expects($this->any())
|
Chris@0
|
46 ->method('getLanguages')
|
Chris@0
|
47 ->will($this->returnValue($languages));
|
Chris@0
|
48 $this->languageManager = $language_manager;
|
Chris@0
|
49
|
Chris@0
|
50 // Create a user stub.
|
Chris@0
|
51 $this->user = $this->getMockBuilder('Drupal\Core\Session\AccountInterface')
|
Chris@0
|
52 ->getMock();
|
Chris@0
|
53
|
Chris@0
|
54 $cache_contexts_manager = $this->getMockBuilder('Drupal\Core\Cache\Context\CacheContextsManager')
|
Chris@0
|
55 ->disableOriginalConstructor()
|
Chris@0
|
56 ->getMock();
|
Chris@0
|
57 $cache_contexts_manager->method('assertValidTokens')->willReturn(TRUE);
|
Chris@0
|
58 $container = new ContainerBuilder();
|
Chris@0
|
59 $container->set('cache_contexts_manager', $cache_contexts_manager);
|
Chris@0
|
60 \Drupal::setContainer($container);
|
Chris@0
|
61 }
|
Chris@0
|
62
|
Chris@0
|
63 /**
|
Chris@0
|
64 * Test path prefix language negotiation and outbound path processing.
|
Chris@0
|
65 *
|
Chris@0
|
66 * @dataProvider providerTestPathPrefix
|
Chris@0
|
67 */
|
Chris@0
|
68 public function testPathPrefix($prefix, $prefixes, $expected_langcode) {
|
Chris@0
|
69 $this->languageManager->expects($this->any())
|
Chris@0
|
70 ->method('getCurrentLanguage')
|
Chris@0
|
71 ->will($this->returnValue($this->languages[(in_array($expected_langcode, ['en', 'de'])) ? $expected_langcode : 'en']));
|
Chris@0
|
72
|
Chris@0
|
73 $config = $this->getConfigFactoryStub([
|
Chris@0
|
74 'language.negotiation' => [
|
Chris@0
|
75 'url' => [
|
Chris@0
|
76 'source' => LanguageNegotiationUrl::CONFIG_PATH_PREFIX,
|
Chris@0
|
77 'prefixes' => $prefixes,
|
Chris@0
|
78 ],
|
Chris@0
|
79 ],
|
Chris@0
|
80 ]);
|
Chris@0
|
81
|
Chris@0
|
82 $request = Request::create('/' . $prefix . '/foo', 'GET');
|
Chris@0
|
83 $method = new LanguageNegotiationUrl();
|
Chris@0
|
84 $method->setLanguageManager($this->languageManager);
|
Chris@0
|
85 $method->setConfig($config);
|
Chris@0
|
86 $method->setCurrentUser($this->user);
|
Chris@0
|
87 $this->assertEquals($expected_langcode, $method->getLangcode($request));
|
Chris@0
|
88
|
Chris@0
|
89 $cacheability = new BubbleableMetadata();
|
Chris@0
|
90 $options = [];
|
Chris@0
|
91 $method->processOutbound('foo', $options, $request, $cacheability);
|
Chris@0
|
92 $expected_cacheability = new BubbleableMetadata();
|
Chris@0
|
93 if ($expected_langcode) {
|
Chris@0
|
94 $this->assertSame($prefix . '/', $options['prefix']);
|
Chris@0
|
95 $expected_cacheability->setCacheContexts(['languages:' . LanguageInterface::TYPE_URL]);
|
Chris@0
|
96 }
|
Chris@0
|
97 else {
|
Chris@0
|
98 $this->assertFalse(isset($options['prefix']));
|
Chris@0
|
99 }
|
Chris@0
|
100 $this->assertEquals($expected_cacheability, $cacheability);
|
Chris@0
|
101 }
|
Chris@0
|
102
|
Chris@0
|
103 /**
|
Chris@0
|
104 * Provides data for the path prefix test.
|
Chris@0
|
105 *
|
Chris@0
|
106 * @return array
|
Chris@0
|
107 * An array of data for checking path prefix negotiation.
|
Chris@0
|
108 */
|
Chris@0
|
109 public function providerTestPathPrefix() {
|
Chris@0
|
110 $path_prefix_configuration[] = [
|
Chris@0
|
111 'prefix' => 'de',
|
Chris@0
|
112 'prefixes' => [
|
Chris@0
|
113 'de' => 'de',
|
Chris@0
|
114 'en-uk' => 'en',
|
Chris@0
|
115 ],
|
Chris@0
|
116 'expected_langcode' => 'de',
|
Chris@0
|
117 ];
|
Chris@0
|
118 $path_prefix_configuration[] = [
|
Chris@0
|
119 'prefix' => 'en-uk',
|
Chris@0
|
120 'prefixes' => [
|
Chris@0
|
121 'de' => 'de',
|
Chris@0
|
122 'en' => 'en-uk',
|
Chris@0
|
123 ],
|
Chris@0
|
124 'expected_langcode' => 'en',
|
Chris@0
|
125 ];
|
Chris@0
|
126 // No configuration.
|
Chris@0
|
127 $path_prefix_configuration[] = [
|
Chris@0
|
128 'prefix' => 'de',
|
Chris@0
|
129 'prefixes' => [],
|
Chris@0
|
130 'expected_langcode' => FALSE,
|
Chris@0
|
131 ];
|
Chris@0
|
132 // Non-matching prefix.
|
Chris@0
|
133 $path_prefix_configuration[] = [
|
Chris@0
|
134 'prefix' => 'de',
|
Chris@0
|
135 'prefixes' => [
|
Chris@0
|
136 'en-uk' => 'en',
|
Chris@0
|
137 ],
|
Chris@0
|
138 'expected_langcode' => FALSE,
|
Chris@0
|
139 ];
|
Chris@0
|
140 // Non-existing language.
|
Chris@0
|
141 $path_prefix_configuration[] = [
|
Chris@0
|
142 'prefix' => 'it',
|
Chris@0
|
143 'prefixes' => [
|
Chris@0
|
144 'it' => 'it',
|
Chris@0
|
145 'en-uk' => 'en',
|
Chris@0
|
146 ],
|
Chris@0
|
147 'expected_langcode' => FALSE,
|
Chris@0
|
148 ];
|
Chris@0
|
149 return $path_prefix_configuration;
|
Chris@0
|
150 }
|
Chris@0
|
151
|
Chris@0
|
152 /**
|
Chris@0
|
153 * Test domain language negotiation and outbound path processing.
|
Chris@0
|
154 *
|
Chris@0
|
155 * @dataProvider providerTestDomain
|
Chris@0
|
156 */
|
Chris@0
|
157 public function testDomain($http_host, $domains, $expected_langcode) {
|
Chris@0
|
158 $this->languageManager->expects($this->any())
|
Chris@0
|
159 ->method('getCurrentLanguage')
|
Chris@0
|
160 ->will($this->returnValue($this->languages['en']));
|
Chris@0
|
161
|
Chris@0
|
162 $config = $this->getConfigFactoryStub([
|
Chris@0
|
163 'language.negotiation' => [
|
Chris@0
|
164 'url' => [
|
Chris@0
|
165 'source' => LanguageNegotiationUrl::CONFIG_DOMAIN,
|
Chris@0
|
166 'domains' => $domains,
|
Chris@0
|
167 ],
|
Chris@0
|
168 ],
|
Chris@0
|
169 ]);
|
Chris@0
|
170
|
Chris@0
|
171 $request = Request::create('', 'GET', [], [], [], ['HTTP_HOST' => $http_host]);
|
Chris@0
|
172 $method = new LanguageNegotiationUrl();
|
Chris@0
|
173 $method->setLanguageManager($this->languageManager);
|
Chris@0
|
174 $method->setConfig($config);
|
Chris@0
|
175 $method->setCurrentUser($this->user);
|
Chris@0
|
176 $this->assertEquals($expected_langcode, $method->getLangcode($request));
|
Chris@0
|
177
|
Chris@0
|
178 $cacheability = new BubbleableMetadata();
|
Chris@0
|
179 $options = [];
|
Chris@0
|
180 $this->assertSame('foo', $method->processOutbound('foo', $options, $request, $cacheability));
|
Chris@0
|
181 $expected_cacheability = new BubbleableMetadata();
|
Chris@0
|
182 if ($expected_langcode !== FALSE && count($domains) > 1) {
|
Chris@0
|
183 $expected_cacheability->setCacheMaxAge(Cache::PERMANENT)->setCacheContexts(['languages:' . LanguageInterface::TYPE_URL, 'url.site']);
|
Chris@0
|
184 }
|
Chris@0
|
185 $this->assertEquals($expected_cacheability, $cacheability);
|
Chris@0
|
186 }
|
Chris@0
|
187
|
Chris@0
|
188 /**
|
Chris@0
|
189 * Provides data for the domain test.
|
Chris@0
|
190 *
|
Chris@0
|
191 * @return array
|
Chris@0
|
192 * An array of data for checking domain negotiation.
|
Chris@0
|
193 */
|
Chris@0
|
194 public function providerTestDomain() {
|
Chris@0
|
195
|
Chris@0
|
196 $domain_configuration[] = [
|
Chris@0
|
197 'http_host' => 'example.de',
|
Chris@0
|
198 'domains' => [
|
Chris@0
|
199 'de' => 'http://example.de',
|
Chris@0
|
200 ],
|
Chris@0
|
201 'expected_langcode' => 'de',
|
Chris@0
|
202 ];
|
Chris@0
|
203 // No configuration.
|
Chris@0
|
204 $domain_configuration[] = [
|
Chris@0
|
205 'http_host' => 'example.de',
|
Chris@0
|
206 'domains' => [],
|
Chris@0
|
207 'expected_langcode' => FALSE,
|
Chris@0
|
208 ];
|
Chris@0
|
209 // HTTP host with a port.
|
Chris@0
|
210 $domain_configuration[] = [
|
Chris@0
|
211 'http_host' => 'example.de:8080',
|
Chris@0
|
212 'domains' => [
|
Chris@0
|
213 'de' => 'http://example.de',
|
Chris@0
|
214 ],
|
Chris@0
|
215 'expected_langcode' => 'de',
|
Chris@0
|
216 ];
|
Chris@0
|
217 // Domain configuration with https://.
|
Chris@0
|
218 $domain_configuration[] = [
|
Chris@0
|
219 'http_host' => 'example.de',
|
Chris@0
|
220 'domains' => [
|
Chris@0
|
221 'de' => 'https://example.de',
|
Chris@0
|
222 ],
|
Chris@0
|
223 'expected_langcode' => 'de',
|
Chris@0
|
224 ];
|
Chris@0
|
225 // Non-matching HTTP host.
|
Chris@0
|
226 $domain_configuration[] = [
|
Chris@0
|
227 'http_host' => 'example.com',
|
Chris@0
|
228 'domains' => [
|
Chris@0
|
229 'de' => 'http://example.com',
|
Chris@0
|
230 ],
|
Chris@0
|
231 'expected_langcode' => 'de',
|
Chris@0
|
232 ];
|
Chris@0
|
233 // Testing a non-existing language.
|
Chris@0
|
234 $domain_configuration[] = [
|
Chris@0
|
235 'http_host' => 'example.com',
|
Chris@0
|
236 'domains' => [
|
Chris@0
|
237 'it' => 'http://example.it',
|
Chris@0
|
238 ],
|
Chris@0
|
239 'expected_langcode' => FALSE,
|
Chris@0
|
240 ];
|
Chris@0
|
241 // Multiple domain configurations.
|
Chris@0
|
242 $domain_configuration[] = [
|
Chris@0
|
243 'http_host' => 'example.com',
|
Chris@0
|
244 'domains' => [
|
Chris@0
|
245 'de' => 'http://example.de',
|
Chris@0
|
246 'en' => 'http://example.com',
|
Chris@0
|
247 ],
|
Chris@0
|
248 'expected_langcode' => 'en',
|
Chris@0
|
249 ];
|
Chris@0
|
250 return $domain_configuration;
|
Chris@0
|
251 }
|
Chris@0
|
252
|
Chris@0
|
253 }
|
Chris@0
|
254
|
Chris@0
|
255 // @todo Remove as part of https://www.drupal.org/node/2481833.
|
Chris@0
|
256 namespace Drupal\language\Plugin\LanguageNegotiation;
|
Chris@0
|
257
|
Chris@0
|
258 if (!function_exists('base_path')) {
|
Chris@17
|
259
|
Chris@0
|
260 function base_path() {
|
Chris@0
|
261 return '/';
|
Chris@0
|
262 }
|
Chris@17
|
263
|
Chris@0
|
264 }
|