annotate core/tests/Drupal/FunctionalTests/Routing/RouteCachingNonPathLanguageNegotiationTest.php @ 19:fa3358dc1485 tip

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