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