private function LanguageUrlRewritingTest::checkUrl in Drupal 10
Same name and namespace in other branches
- 8 core/modules/language/tests/src/Functional/LanguageUrlRewritingTest.php \Drupal\Tests\language\Functional\LanguageUrlRewritingTest::checkUrl()
 - 9 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.
File
- core/
modules/ language/ tests/ src/ Functional/ LanguageUrlRewritingTest.php, line 89  
Class
- LanguageUrlRewritingTest
 - Tests that URL rewriting works as expected.
 
Namespace
Drupal\Tests\language\FunctionalCode
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 = isset($prefixes[$language
    ->getId()]) ? $prefixes[$language
    ->getId()] : $this
    ->randomMachineName();
  $this
    ->assertNotEquals($prefix, $stored_prefix, $message);
  $prefix = $stored_prefix;
  $this
    ->drupalGet("{$prefix}/{$path}");
  $this
    ->assertSession()
    ->statusCodeEquals(404);
}