function LanguageUrlRewritingTest::checkUrl

Same name and namespace in other branches
  1. 9 core/modules/language/tests/src/Functional/LanguageUrlRewritingTest.php \Drupal\Tests\language\Functional\LanguageUrlRewritingTest::checkUrl()
  2. 8.9.x core/modules/language/tests/src/Functional/LanguageUrlRewritingTest.php \Drupal\Tests\language\Functional\LanguageUrlRewritingTest::checkUrl()
  3. 11.x core/modules/language/tests/src/Functional/LanguageUrlRewritingTest.php \Drupal\Tests\language\Functional\LanguageUrlRewritingTest::checkUrl()

Check URL rewriting for the given language.

The test is performed with a fixed URL (the default front page) to simply check that language prefixes are not added to it and that the prefixed URL is actually not working.

Parameters

\Drupal\Core\Language\LanguageInterface $language: The language object.

string $message: Message to display in assertion that language prefixes are not added.

1 call to LanguageUrlRewritingTest::checkUrl()
LanguageUrlRewritingTest::testUrlRewritingEdgeCases in core/modules/language/tests/src/Functional/LanguageUrlRewritingTest.php
Check that non-installed languages are not considered.

File

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

Class

LanguageUrlRewritingTest
Tests that URL rewriting works as expected.

Namespace

Drupal\Tests\language\Functional

Code

private function checkUrl(LanguageInterface $language, $message) {
  $options = [
    'language' => $language,
    'script' => '',
  ];
  $base_path = trim(base_path(), '/');
  $rewritten_path = trim(str_replace($base_path, '', Url::fromRoute('<front>', [], $options)->toString()), '/');
  $segments = explode('/', $rewritten_path, 2);
  $prefix = $segments[0];
  $path = $segments[1] ?? $prefix;
  // If the rewritten URL has not a language prefix we pick a random prefix so
  // we can always check the prefixed URL.
  $prefixes = $this->config('language.negotiation')
    ->get('url.prefixes');
  $stored_prefix = $prefixes[$language->getId()] ?? $this->randomMachineName();
  $this->assertNotEquals($prefix, $stored_prefix, $message);
  $prefix = $stored_prefix;
  $this->drupalGet("{$prefix}/{$path}");
  $this->assertSession()
    ->statusCodeEquals(404);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.