You are here

function LanguageUrlRewritingTest::testDomainNameNegotiationPort in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/language/src/Tests/LanguageUrlRewritingTest.php \Drupal\language\Tests\LanguageUrlRewritingTest::testDomainNameNegotiationPort()

Check URL rewriting when using a domain name and a non-standard port.

File

core/modules/language/src/Tests/LanguageUrlRewritingTest.php, line 109
Contains \Drupal\language\Tests\LanguageUrlRewritingTest.

Class

LanguageUrlRewritingTest
Tests that URL rewriting works as expected.

Namespace

Drupal\language\Tests

Code

function testDomainNameNegotiationPort() {
  global $base_url;
  $language_domain = 'example.fr';

  // Get the current host URI we're running on.
  $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'));

  // Rebuild the container so that the new language gets picked up by services
  // that hold the list of languages.
  $this
    ->rebuildContainer();

  // Enable domain configuration.
  $this
    ->config('language.negotiation')
    ->set('url.source', LanguageNegotiationUrl::CONFIG_DOMAIN)
    ->save();

  // Reset static caching.
  $this->container
    ->get('language_manager')
    ->reset();

  // In case index.php is part of the URLs, we need to adapt the asserted
  // URLs as well.
  $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',
  ));

  // Create an absolute French link.
  $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.');

  // If we set the port explicitly, it should not be overridden.
  $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.');
}