RouteCachingNonPathLanguageNegotiationTest.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. /**
  7. * Tests the route cache when the language is not in the path.
  8. *
  9. * @group language
  10. */
  11. class RouteCachingNonPathLanguageNegotiationTest extends BrowserTestBase {
  12. /**
  13. * Modules to enable.
  14. *
  15. * @var array
  16. */
  17. public static $modules = ['language', 'block'];
  18. /**
  19. * The admin user.
  20. *
  21. * @var \Drupal\user\UserInterface
  22. */
  23. protected $adminUser;
  24. protected function setUp() {
  25. parent::setUp();
  26. // Create and log in user.
  27. $this->adminUser = $this->drupalCreateUser(['administer blocks', 'administer languages', 'access administration pages']);
  28. $this->drupalLogin($this->adminUser);
  29. // Add language.
  30. ConfigurableLanguage::createFromLangcode('fr')->save();
  31. // Enable session language detection and selection.
  32. $edit = [
  33. 'language_interface[enabled][language-url]' => FALSE,
  34. 'language_interface[enabled][language-session]' => TRUE,
  35. ];
  36. $this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));
  37. // A more common scenario is domain-based negotiation but that can not be
  38. // tested. Session negotiation by default is not considered by the URL
  39. // language type that is used to resolve the alias. Explicitly enable
  40. // that to be able to test this scenario.
  41. // @todo Improve in https://www.drupal.org/project/drupal/issues/1125428.
  42. $this->config('language.types')
  43. ->set('negotiation.language_url.enabled', ['language-session' => 0])
  44. ->save();
  45. // Enable the language switching block.
  46. $this->drupalPlaceBlock('language_block:' . LanguageInterface::TYPE_INTERFACE, [
  47. 'id' => 'test_language_block',
  48. ]);
  49. }
  50. /**
  51. * Tests aliases when the negotiated language is not in the path.
  52. */
  53. public function testAliases() {
  54. // Switch to French and try to access the now inaccessible block.
  55. $this->drupalGet('');
  56. // Create an alias for user/UID just for en, make sure that this is a 404
  57. // on the french page exist in english, no matter which language is
  58. // checked first. Create the alias after visiting frontpage to make sure
  59. // there is no existing cache entry for this that affects the tests.
  60. \Drupal::service('path.alias_storage')->save('/user/' . $this->adminUser->id(), '/user-page', 'en');
  61. $this->clickLink('French');
  62. $this->drupalGet('user-page');
  63. $this->assertSession()->statusCodeEquals(404);
  64. // Switch to english, make sure it works now.
  65. $this->clickLink('English');
  66. $this->drupalGet('user-page');
  67. $this->assertSession()->statusCodeEquals(200);
  68. // Clear cache and repeat the check, this time with english first.
  69. $this->resetAll();
  70. $this->drupalGet('user-page');
  71. $this->assertSession()->statusCodeEquals(200);
  72. $this->clickLink('French');
  73. $this->drupalGet('user-page');
  74. $this->assertSession()->statusCodeEquals(404);
  75. }
  76. }