RouteCachingNonPathLanguageNegotiationTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace Drupal\FunctionalTests\Routing;
  3. use Drupal\language\Entity\ConfigurableLanguage;
  4. use Drupal\Core\Language\LanguageInterface;
  5. use Drupal\Tests\BrowserTestBase;
  6. use Drupal\Tests\Traits\Core\PathAliasTestTrait;
  7. /**
  8. * Tests the route cache when the language is not in the path.
  9. *
  10. * @group language
  11. */
  12. class RouteCachingNonPathLanguageNegotiationTest extends BrowserTestBase {
  13. use PathAliasTestTrait;
  14. /**
  15. * Modules to enable.
  16. *
  17. * @var array
  18. */
  19. public static $modules = ['language', 'block'];
  20. /**
  21. * {@inheritdoc}
  22. */
  23. protected $defaultTheme = 'stark';
  24. /**
  25. * The admin user.
  26. *
  27. * @var \Drupal\user\UserInterface
  28. */
  29. protected $adminUser;
  30. protected function setUp() {
  31. parent::setUp();
  32. // Create and log in user.
  33. $this->adminUser = $this->drupalCreateUser([
  34. 'administer blocks',
  35. 'administer languages',
  36. 'access administration pages',
  37. ]);
  38. $this->drupalLogin($this->adminUser);
  39. // Add language.
  40. ConfigurableLanguage::createFromLangcode('fr')->save();
  41. // Enable session language detection and selection.
  42. $edit = [
  43. 'language_interface[enabled][language-url]' => FALSE,
  44. 'language_interface[enabled][language-session]' => TRUE,
  45. ];
  46. $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])
  54. ->save();
  55. // Enable the language switching block.
  56. $this->drupalPlaceBlock('language_block:' . LanguageInterface::TYPE_INTERFACE, [
  57. 'id' => 'test_language_block',
  58. ]);
  59. }
  60. /**
  61. * Tests aliases when the negotiated language is not in the path.
  62. */
  63. public function testAliases() {
  64. // Switch to French and try to access the now inaccessible block.
  65. $this->drupalGet('');
  66. // Create an alias for user/UID just for en, make sure that this is a 404
  67. // on the french page exist in english, no matter which language is
  68. // checked first. Create the alias after visiting frontpage to make sure
  69. // there is no existing cache entry for this that affects the tests.
  70. $this->createPathAlias('/user/' . $this->adminUser->id(), '/user-page', 'en');
  71. $this->clickLink('French');
  72. $this->drupalGet('user-page');
  73. $this->assertSession()->statusCodeEquals(404);
  74. // Switch to english, make sure it works now.
  75. $this->clickLink('English');
  76. $this->drupalGet('user-page');
  77. $this->assertSession()->statusCodeEquals(200);
  78. // Clear cache and repeat the check, this time with english first.
  79. $this->resetAll();
  80. $this->drupalGet('user-page');
  81. $this->assertSession()->statusCodeEquals(200);
  82. $this->clickLink('French');
  83. $this->drupalGet('user-page');
  84. $this->assertSession()->statusCodeEquals(404);
  85. }
  86. }