3 namespace Drupal\FunctionalTests\Routing;
5 use Drupal\language\Entity\ConfigurableLanguage;
6 use Drupal\Core\Language\LanguageInterface;
7 use Drupal\Tests\BrowserTestBase;
10 * Tests the route cache when the language is not in the path.
14 class RouteCachingNonPathLanguageNegotiationTest extends BrowserTestBase {
21 public static $modules = ['language', 'block'];
26 * @var \Drupal\user\UserInterface
30 protected function setUp() {
33 // Create and log in user.
34 $this->adminUser = $this->drupalCreateUser(['administer blocks', 'administer languages', 'access administration pages']);
35 $this->drupalLogin($this->adminUser);
38 ConfigurableLanguage::createFromLangcode('fr')->save();
40 // Enable session language detection and selection.
42 'language_interface[enabled][language-url]' => FALSE,
43 'language_interface[enabled][language-session]' => TRUE,
45 $this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));
47 // A more common scenario is domain-based negotiation but that can not be
48 // tested. Session negotiation by default is not considered by the URL
49 // language type that is used to resolve the alias. Explicitly enable
50 // that to be able to test this scenario.
51 // @todo Improve in https://www.drupal.org/project/drupal/issues/1125428.
52 $this->config('language.types')
53 ->set('negotiation.language_url.enabled', ['language-session' => 0])
56 // Enable the language switching block.
57 $this->drupalPlaceBlock('language_block:' . LanguageInterface::TYPE_INTERFACE, [
58 'id' => 'test_language_block',
64 * Tests aliases when the negotiated language is not in the path.
66 public function testAliases() {
67 // Switch to French and try to access the now inaccessible block.
70 // Create an alias for user/UID just for en, make sure that this is a 404
71 // on the french page exist in english, no matter which language is
72 // checked first. Create the alias after visiting frontpage to make sure
73 // there is no existing cache entry for this that affects the tests.
74 \Drupal::service('path.alias_storage')->save('/user/' . $this->adminUser->id(), '/user-page', 'en');
76 $this->clickLink('French');
77 $this->drupalGet('user-page');
78 $this->assertSession()->statusCodeEquals(404);
80 // Switch to english, make sure it works now.
81 $this->clickLink('English');
82 $this->drupalGet('user-page');
83 $this->assertSession()->statusCodeEquals(200);
85 // Clear cache and repeat the check, this time with english first.
87 $this->drupalGet('user-page');
88 $this->assertSession()->statusCodeEquals(200);
90 $this->clickLink('French');
91 $this->drupalGet('user-page');
92 $this->assertSession()->statusCodeEquals(404);