Chris@0: getMock('\Drupal\Core\Language\LanguageInterface'); Chris@0: $language_de->expects($this->any()) Chris@0: ->method('getId') Chris@0: ->will($this->returnValue('de')); Chris@0: $language_en = $this->getMock('\Drupal\Core\Language\LanguageInterface'); Chris@0: $language_en->expects($this->any()) Chris@0: ->method('getId') Chris@0: ->will($this->returnValue('en')); Chris@0: $languages = [ Chris@0: 'de' => $language_de, Chris@0: 'en' => $language_en, Chris@0: ]; Chris@0: $this->languages = $languages; Chris@0: Chris@0: // Create a language manager stub. Chris@0: $language_manager = $this->getMockBuilder('Drupal\language\ConfigurableLanguageManagerInterface') Chris@0: ->getMock(); Chris@0: $language_manager->expects($this->any()) Chris@0: ->method('getLanguages') Chris@0: ->will($this->returnValue($languages)); Chris@0: $this->languageManager = $language_manager; Chris@0: Chris@0: // Create a user stub. Chris@0: $this->user = $this->getMockBuilder('Drupal\Core\Session\AccountInterface') Chris@0: ->getMock(); Chris@0: Chris@0: $cache_contexts_manager = $this->getMockBuilder('Drupal\Core\Cache\Context\CacheContextsManager') Chris@0: ->disableOriginalConstructor() Chris@0: ->getMock(); Chris@0: $cache_contexts_manager->method('assertValidTokens')->willReturn(TRUE); Chris@0: $container = new ContainerBuilder(); Chris@0: $container->set('cache_contexts_manager', $cache_contexts_manager); Chris@0: \Drupal::setContainer($container); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test path prefix language negotiation and outbound path processing. Chris@0: * Chris@0: * @dataProvider providerTestPathPrefix Chris@0: */ Chris@0: public function testPathPrefix($prefix, $prefixes, $expected_langcode) { Chris@0: $this->languageManager->expects($this->any()) Chris@0: ->method('getCurrentLanguage') Chris@0: ->will($this->returnValue($this->languages[(in_array($expected_langcode, ['en', 'de'])) ? $expected_langcode : 'en'])); Chris@0: Chris@0: $config = $this->getConfigFactoryStub([ Chris@0: 'language.negotiation' => [ Chris@0: 'url' => [ Chris@0: 'source' => LanguageNegotiationUrl::CONFIG_PATH_PREFIX, Chris@0: 'prefixes' => $prefixes, Chris@0: ], Chris@0: ], Chris@0: ]); Chris@0: Chris@0: $request = Request::create('/' . $prefix . '/foo', 'GET'); Chris@0: $method = new LanguageNegotiationUrl(); Chris@0: $method->setLanguageManager($this->languageManager); Chris@0: $method->setConfig($config); Chris@0: $method->setCurrentUser($this->user); Chris@0: $this->assertEquals($expected_langcode, $method->getLangcode($request)); Chris@0: Chris@0: $cacheability = new BubbleableMetadata(); Chris@0: $options = []; Chris@0: $method->processOutbound('foo', $options, $request, $cacheability); Chris@0: $expected_cacheability = new BubbleableMetadata(); Chris@0: if ($expected_langcode) { Chris@0: $this->assertSame($prefix . '/', $options['prefix']); Chris@0: $expected_cacheability->setCacheContexts(['languages:' . LanguageInterface::TYPE_URL]); Chris@0: } Chris@0: else { Chris@0: $this->assertFalse(isset($options['prefix'])); Chris@0: } Chris@0: $this->assertEquals($expected_cacheability, $cacheability); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Provides data for the path prefix test. Chris@0: * Chris@0: * @return array Chris@0: * An array of data for checking path prefix negotiation. Chris@0: */ Chris@0: public function providerTestPathPrefix() { Chris@0: $path_prefix_configuration[] = [ Chris@0: 'prefix' => 'de', Chris@0: 'prefixes' => [ Chris@0: 'de' => 'de', Chris@0: 'en-uk' => 'en', Chris@0: ], Chris@0: 'expected_langcode' => 'de', Chris@0: ]; Chris@0: $path_prefix_configuration[] = [ Chris@0: 'prefix' => 'en-uk', Chris@0: 'prefixes' => [ Chris@0: 'de' => 'de', Chris@0: 'en' => 'en-uk', Chris@0: ], Chris@0: 'expected_langcode' => 'en', Chris@0: ]; Chris@0: // No configuration. Chris@0: $path_prefix_configuration[] = [ Chris@0: 'prefix' => 'de', Chris@0: 'prefixes' => [], Chris@0: 'expected_langcode' => FALSE, Chris@0: ]; Chris@0: // Non-matching prefix. Chris@0: $path_prefix_configuration[] = [ Chris@0: 'prefix' => 'de', Chris@0: 'prefixes' => [ Chris@0: 'en-uk' => 'en', Chris@0: ], Chris@0: 'expected_langcode' => FALSE, Chris@0: ]; Chris@0: // Non-existing language. Chris@0: $path_prefix_configuration[] = [ Chris@0: 'prefix' => 'it', Chris@0: 'prefixes' => [ Chris@0: 'it' => 'it', Chris@0: 'en-uk' => 'en', Chris@0: ], Chris@0: 'expected_langcode' => FALSE, Chris@0: ]; Chris@0: return $path_prefix_configuration; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test domain language negotiation and outbound path processing. Chris@0: * Chris@0: * @dataProvider providerTestDomain Chris@0: */ Chris@0: public function testDomain($http_host, $domains, $expected_langcode) { Chris@0: $this->languageManager->expects($this->any()) Chris@0: ->method('getCurrentLanguage') Chris@0: ->will($this->returnValue($this->languages['en'])); Chris@0: Chris@0: $config = $this->getConfigFactoryStub([ Chris@0: 'language.negotiation' => [ Chris@0: 'url' => [ Chris@0: 'source' => LanguageNegotiationUrl::CONFIG_DOMAIN, Chris@0: 'domains' => $domains, Chris@0: ], Chris@0: ], Chris@0: ]); Chris@0: Chris@0: $request = Request::create('', 'GET', [], [], [], ['HTTP_HOST' => $http_host]); Chris@0: $method = new LanguageNegotiationUrl(); Chris@0: $method->setLanguageManager($this->languageManager); Chris@0: $method->setConfig($config); Chris@0: $method->setCurrentUser($this->user); Chris@0: $this->assertEquals($expected_langcode, $method->getLangcode($request)); Chris@0: Chris@0: $cacheability = new BubbleableMetadata(); Chris@0: $options = []; Chris@0: $this->assertSame('foo', $method->processOutbound('foo', $options, $request, $cacheability)); Chris@0: $expected_cacheability = new BubbleableMetadata(); Chris@0: if ($expected_langcode !== FALSE && count($domains) > 1) { Chris@0: $expected_cacheability->setCacheMaxAge(Cache::PERMANENT)->setCacheContexts(['languages:' . LanguageInterface::TYPE_URL, 'url.site']); Chris@0: } Chris@0: $this->assertEquals($expected_cacheability, $cacheability); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Provides data for the domain test. Chris@0: * Chris@0: * @return array Chris@0: * An array of data for checking domain negotiation. Chris@0: */ Chris@0: public function providerTestDomain() { Chris@0: Chris@0: $domain_configuration[] = [ Chris@0: 'http_host' => 'example.de', Chris@0: 'domains' => [ Chris@0: 'de' => 'http://example.de', Chris@0: ], Chris@0: 'expected_langcode' => 'de', Chris@0: ]; Chris@0: // No configuration. Chris@0: $domain_configuration[] = [ Chris@0: 'http_host' => 'example.de', Chris@0: 'domains' => [], Chris@0: 'expected_langcode' => FALSE, Chris@0: ]; Chris@0: // HTTP host with a port. Chris@0: $domain_configuration[] = [ Chris@0: 'http_host' => 'example.de:8080', Chris@0: 'domains' => [ Chris@0: 'de' => 'http://example.de', Chris@0: ], Chris@0: 'expected_langcode' => 'de', Chris@0: ]; Chris@0: // Domain configuration with https://. Chris@0: $domain_configuration[] = [ Chris@0: 'http_host' => 'example.de', Chris@0: 'domains' => [ Chris@0: 'de' => 'https://example.de', Chris@0: ], Chris@0: 'expected_langcode' => 'de', Chris@0: ]; Chris@0: // Non-matching HTTP host. Chris@0: $domain_configuration[] = [ Chris@0: 'http_host' => 'example.com', Chris@0: 'domains' => [ Chris@0: 'de' => 'http://example.com', Chris@0: ], Chris@0: 'expected_langcode' => 'de', Chris@0: ]; Chris@0: // Testing a non-existing language. Chris@0: $domain_configuration[] = [ Chris@0: 'http_host' => 'example.com', Chris@0: 'domains' => [ Chris@0: 'it' => 'http://example.it', Chris@0: ], Chris@0: 'expected_langcode' => FALSE, Chris@0: ]; Chris@0: // Multiple domain configurations. Chris@0: $domain_configuration[] = [ Chris@0: 'http_host' => 'example.com', Chris@0: 'domains' => [ Chris@0: 'de' => 'http://example.de', Chris@0: 'en' => 'http://example.com', Chris@0: ], Chris@0: 'expected_langcode' => 'en', Chris@0: ]; Chris@0: return $domain_configuration; Chris@0: } Chris@0: Chris@0: } Chris@0: Chris@0: // @todo Remove as part of https://www.drupal.org/node/2481833. Chris@0: namespace Drupal\language\Plugin\LanguageNegotiation; Chris@0: Chris@0: if (!function_exists('base_path')) { Chris@17: Chris@0: function base_path() { Chris@0: return '/'; Chris@0: } Chris@17: Chris@0: }