You are here

public function LanguageUrlRewritingTest::testDomainNameNegotiationPort in Drupal 9

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

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

File

core/modules/language/tests/src/Functional/LanguageUrlRewritingTest.php, line 111

Class

LanguageUrlRewritingTest
Tests that URL rewriting works as expected.

Namespace

Drupal\Tests\language\Functional

Code

public 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 = [
    'language_negotiation_url_part' => LanguageNegotiationUrl::CONFIG_DOMAIN,
    'domain[en]' => $base_url_host,
    'domain[fr]' => $language_domain,
  ];
  $this
    ->drupalGet('admin/config/regional/language/detection/url');
  $this
    ->submitForm($edit, '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(Url::fromRoute('<front>', [], [
    'absolute' => TRUE,
  ])
    ->toString(), 'index.php') !== FALSE;
  $request = Request::createFromGlobals();
  $server = $request->server
    ->all();
  $request = $this
    ->prepareRequestForGenerator(TRUE, [
    '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
    ->assertEquals($expected, $url, '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
    ->assertEquals($expected, $url, 'A given port is not overridden.');
}