View source
<?php
namespace Drupal\language\Tests;
use Drupal\Core\Language\Language;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Url;
use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl;
use Drupal\simpletest\WebTestBase;
use Symfony\Component\HttpFoundation\Request;
class LanguageUrlRewritingTest extends WebTestBase {
public static $modules = array(
'language',
'language_test',
);
protected $webUser;
protected function setUp() {
parent::setUp();
$this->webUser = $this
->drupalCreateUser(array(
'administer languages',
'access administration pages',
));
$this
->drupalLogin($this->webUser);
$edit = array();
$edit['predefined_langcode'] = 'fr';
$this
->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
$edit = array(
'language_interface[enabled][language-url]' => 1,
);
$this
->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));
$this
->drupalGet('fr/admin/config/regional/language/detection');
$this
->assertRaw('"pathPrefix":"fr\\/"', 'drupalSettings path prefix contains language code.');
}
function testUrlRewritingEdgeCases() {
$non_existing = new Language(array(
'id' => $this
->randomMachineName(),
));
$this
->checkUrl($non_existing, 'Path language is ignored if language is not installed.', 'URL language negotiation does not work with non-installed languages');
$this
->drupalGet('language_test/subrequest');
$this
->assertText($this->webUser
->getUsername(), 'Page correctly retrieved');
}
private function checkUrl(LanguageInterface $language, $message1, $message2) {
$options = array(
'language' => $language,
'script' => '',
);
$base_path = trim(base_path(), '/');
$rewritten_path = trim(str_replace($base_path, '', \Drupal::url('<front>', array(), $options)), '/');
$segments = explode('/', $rewritten_path, 2);
$prefix = $segments[0];
$path = isset($segments[1]) ? $segments[1] : $prefix;
$prefixes = language_negotiation_url_prefixes();
$stored_prefix = isset($prefixes[$language
->getId()]) ? $prefixes[$language
->getId()] : $this
->randomMachineName();
if ($this
->assertNotEqual($stored_prefix, $prefix, $message1)) {
$prefix = $stored_prefix;
}
$this
->drupalGet("{$prefix}/{$path}");
$this
->assertResponse(404, $message2);
}
function testDomainNameNegotiationPort() {
global $base_url;
$language_domain = 'example.fr';
$base_url_host = parse_url($base_url, PHP_URL_HOST);
$edit = array(
'language_negotiation_url_part' => LanguageNegotiationUrl::CONFIG_DOMAIN,
'domain[en]' => $base_url_host,
'domain[fr]' => $language_domain,
);
$this
->drupalPostForm('admin/config/regional/language/detection/url', $edit, t('Save configuration'));
$this
->rebuildContainer();
$this
->config('language.negotiation')
->set('url.source', LanguageNegotiationUrl::CONFIG_DOMAIN)
->save();
$this->container
->get('language_manager')
->reset();
$index_php = strpos(\Drupal::url('<front>', array(), array(
'absolute' => TRUE,
)), 'index.php') !== FALSE;
$request = Request::createFromGlobals();
$server = $request->server
->all();
$request = $this
->prepareRequestForGenerator(TRUE, array(
'HTTP_HOST' => $server['HTTP_HOST'] . ':88',
));
$language = \Drupal::languageManager()
->getLanguage('fr');
$url = Url::fromRoute('<front>', [], [
'absolute' => TRUE,
'language' => $language,
])
->toString();
$expected = ($index_php ? 'http://example.fr:88/index.php' : 'http://example.fr:88') . rtrim(base_path(), '/') . '/';
$this
->assertEqual($url, $expected, 'The right port is used.');
$url = Url::fromRoute('<front>', [], [
'absolute' => TRUE,
'language' => $language,
'base_url' => $request
->getBaseUrl() . ':90',
])
->toString();
$expected = $index_php ? 'http://example.fr:90/index.php' : 'http://example.fr:90' . rtrim(base_path(), '/') . '/';
$this
->assertEqual($url, $expected, 'A given port is not overridden.');
}
}