3 namespace Drupal\Tests\language\Unit;
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;
14 * @coversDefaultClass \Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl
17 class LanguageNegotiationUrlTest extends UnitTestCase {
19 protected $languageManager;
25 protected function setUp() {
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())
31 ->will($this->returnValue('de'));
32 $language_en = $this->getMock('\Drupal\Core\Language\LanguageInterface');
33 $language_en->expects($this->any())
35 ->will($this->returnValue('en'));
40 $this->languages = $languages;
42 // Create a language manager stub.
43 $language_manager = $this->getMockBuilder('Drupal\language\ConfigurableLanguageManagerInterface')
45 $language_manager->expects($this->any())
46 ->method('getLanguages')
47 ->will($this->returnValue($languages));
48 $this->languageManager = $language_manager;
50 // Create a user stub.
51 $this->user = $this->getMockBuilder('Drupal\Core\Session\AccountInterface')
54 $cache_contexts_manager = $this->getMockBuilder('Drupal\Core\Cache\Context\CacheContextsManager')
55 ->disableOriginalConstructor()
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);
64 * Test path prefix language negotiation and outbound path processing.
66 * @dataProvider providerTestPathPrefix
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']));
73 $config = $this->getConfigFactoryStub([
74 'language.negotiation' => [
76 'source' => LanguageNegotiationUrl::CONFIG_PATH_PREFIX,
77 'prefixes' => $prefixes,
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));
89 $cacheability = new BubbleableMetadata();
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]);
98 $this->assertFalse(isset($options['prefix']));
100 $this->assertEquals($expected_cacheability, $cacheability);
104 * Provides data for the path prefix test.
107 * An array of data for checking path prefix negotiation.
109 public function providerTestPathPrefix() {
110 $path_prefix_configuration[] = [
116 'expected_langcode' => 'de',
118 $path_prefix_configuration[] = [
124 'expected_langcode' => 'en',
127 $path_prefix_configuration[] = [
130 'expected_langcode' => FALSE,
132 // Non-matching prefix.
133 $path_prefix_configuration[] = [
138 'expected_langcode' => FALSE,
140 // Non-existing language.
141 $path_prefix_configuration[] = [
147 'expected_langcode' => FALSE,
149 return $path_prefix_configuration;
153 * Test domain language negotiation and outbound path processing.
155 * @dataProvider providerTestDomain
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']));
162 $config = $this->getConfigFactoryStub([
163 'language.negotiation' => [
165 'source' => LanguageNegotiationUrl::CONFIG_DOMAIN,
166 'domains' => $domains,
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));
178 $cacheability = new BubbleableMetadata();
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']);
185 $this->assertEquals($expected_cacheability, $cacheability);
189 * Provides data for the domain test.
192 * An array of data for checking domain negotiation.
194 public function providerTestDomain() {
196 $domain_configuration[] = [
197 'http_host' => 'example.de',
199 'de' => 'http://example.de',
201 'expected_langcode' => 'de',
204 $domain_configuration[] = [
205 'http_host' => 'example.de',
207 'expected_langcode' => FALSE,
209 // HTTP host with a port.
210 $domain_configuration[] = [
211 'http_host' => 'example.de:8080',
213 'de' => 'http://example.de',
215 'expected_langcode' => 'de',
217 // Domain configuration with https://.
218 $domain_configuration[] = [
219 'http_host' => 'example.de',
221 'de' => 'https://example.de',
223 'expected_langcode' => 'de',
225 // Non-matching HTTP host.
226 $domain_configuration[] = [
227 'http_host' => 'example.com',
229 'de' => 'http://example.com',
231 'expected_langcode' => 'de',
233 // Testing a non-existing language.
234 $domain_configuration[] = [
235 'http_host' => 'example.com',
237 'it' => 'http://example.it',
239 'expected_langcode' => FALSE,
241 // Multiple domain configurations.
242 $domain_configuration[] = [
243 'http_host' => 'example.com',
245 'de' => 'http://example.de',
246 'en' => 'http://example.com',
248 'expected_langcode' => 'en',
250 return $domain_configuration;
255 // @todo Remove as part of https://www.drupal.org/node/2481833.
256 namespace Drupal\language\Plugin\LanguageNegotiation;
258 if (!function_exists('base_path')) {
260 function base_path() {