annotate core/modules/language/tests/src/Functional/ConfigurableLanguageManagerTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@17 1 <?php
Chris@17 2
Chris@17 3 namespace Drupal\Tests\language\Functional;
Chris@17 4
Chris@17 5 use Drupal\Core\Cache\Cache;
Chris@17 6 use Drupal\language\Entity\ConfigurableLanguage;
Chris@17 7 use Drupal\language\Entity\ContentLanguageSettings;
Chris@17 8 use Drupal\node\Entity\Node;
Chris@17 9 use Drupal\node\Entity\NodeType;
Chris@17 10 use Drupal\Tests\BrowserTestBase;
Chris@17 11
Chris@17 12 /**
Chris@17 13 * Tests Language Negotiation.
Chris@17 14 *
Chris@17 15 * Uses different negotiators for content and interface.
Chris@17 16 *
Chris@17 17 * @group language
Chris@17 18 */
Chris@17 19 class ConfigurableLanguageManagerTest extends BrowserTestBase {
Chris@17 20
Chris@17 21 /**
Chris@17 22 * {@inheritdoc}
Chris@17 23 */
Chris@17 24 public static $modules = [
Chris@17 25 'language',
Chris@17 26 'content_translation',
Chris@17 27 'node',
Chris@17 28 'locale',
Chris@17 29 'block',
Chris@17 30 'system',
Chris@17 31 'user',
Chris@17 32 ];
Chris@17 33
Chris@17 34 /**
Chris@17 35 * {@inheritdoc}
Chris@17 36 */
Chris@17 37 protected function setUp() {
Chris@17 38 parent::setUp();
Chris@17 39
Chris@17 40 /** @var \Drupal\user\UserInterface $user */
Chris@17 41 $user = $this->createUser([], '', TRUE);
Chris@17 42 $this->drupalLogin($user);
Chris@17 43 ConfigurableLanguage::createFromLangcode('es')->save();
Chris@17 44
Chris@17 45 // Create a page node type and make it translatable.
Chris@17 46 NodeType::create([
Chris@17 47 'type' => 'page',
Chris@17 48 'name' => t('Page'),
Chris@17 49 ])->save();
Chris@17 50
Chris@17 51 $config = ContentLanguageSettings::loadByEntityTypeBundle('node', 'page');
Chris@17 52 $config->setDefaultLangcode('en')
Chris@17 53 ->setLanguageAlterable(TRUE)
Chris@17 54 ->save();
Chris@17 55
Chris@17 56 // Create a Node with title 'English' and translate it to Spanish.
Chris@17 57 $node = Node::create([
Chris@17 58 'type' => 'page',
Chris@17 59 'title' => 'English',
Chris@17 60 ]);
Chris@17 61 $node->save();
Chris@17 62 $node->addTranslation('es', ['title' => 'Español']);
Chris@17 63 $node->save();
Chris@17 64
Chris@17 65 // Enable both language_interface and language_content language negotiation.
Chris@17 66 \Drupal::getContainer()->get('language_negotiator')->updateConfiguration([
Chris@17 67 'language_interface',
Chris@17 68 'language_content',
Chris@17 69 ]);
Chris@17 70
Chris@17 71 // Set the preferred language of the user for admin pages to English.
Chris@17 72 $user->set('preferred_admin_langcode', 'en')->save();
Chris@17 73
Chris@17 74 // Make sure node edit pages are administration pages.
Chris@17 75 $this->config('node.settings')->set('use_admin_theme', '1')->save();
Chris@17 76 $this->container->get('router.builder')->rebuild();
Chris@17 77
Chris@17 78 // Place a Block with a translatable string on the page.
Chris@17 79 $this->placeBlock('system_powered_by_block', ['region' => 'content']);
Chris@17 80
Chris@17 81 // Load the Spanish Node page once, to register the translatable string.
Chris@17 82 $this->drupalGet('/es/node/1');
Chris@17 83
Chris@17 84 // Translate the Powered by string.
Chris@17 85 /** @var \Drupal\locale\StringStorageInterface $string_storage */
Chris@17 86 $string_storage = \Drupal::getContainer()->get('locale.storage');
Chris@17 87 $source = $string_storage->findString(['source' => 'Powered by <a href=":poweredby">Drupal</a>']);
Chris@17 88 $string_storage->createTranslation([
Chris@17 89 'lid' => $source->lid,
Chris@17 90 'language' => 'es',
Chris@17 91 'translation' => 'Funciona con ...',
Chris@17 92 ])->save();
Chris@17 93 // Invalidate caches so that the new translation will be used.
Chris@17 94 Cache::invalidateTags(['rendered', 'locale']);
Chris@17 95 }
Chris@17 96
Chris@17 97 /**
Chris@17 98 * Test translation with URL and Preferred Admin Language negotiators.
Chris@17 99 *
Chris@17 100 * The interface language uses the preferred language for admin pages of the
Chris@17 101 * user and after that the URL. The Content uses just the URL.
Chris@17 102 */
Chris@17 103 public function testUrlContentTranslationWithPreferredAdminLanguage() {
Chris@17 104 $assert_session = $this->assertSession();
Chris@17 105 // Set the interface language to use the preferred administration language
Chris@17 106 // and then the URL.
Chris@17 107 /** @var \Drupal\language\LanguageNegotiatorInterface $language_negotiator */
Chris@17 108 $language_negotiator = \Drupal::getContainer()->get('language_negotiator');
Chris@17 109 $language_negotiator->saveConfiguration('language_interface', [
Chris@17 110 'language-user-admin' => 1,
Chris@17 111 'language-url' => 2,
Chris@17 112 'language-selected' => 3,
Chris@17 113 ]);
Chris@17 114 // Set Content Language Negotiator to use just the URL.
Chris@17 115 $language_negotiator->saveConfiguration('language_content', [
Chris@17 116 'language-url' => 4,
Chris@17 117 'language-selected' => 5,
Chris@17 118 ]);
Chris@17 119
Chris@17 120 // See if the full view of the node in english is present and the
Chris@17 121 // string in the Powered By Block is in English.
Chris@17 122 $this->drupalGet('/node/1');
Chris@17 123 $assert_session->pageTextContains('English');
Chris@17 124 $assert_session->pageTextContains('Powered by');
Chris@17 125
Chris@17 126 // Load the spanish node page again and see if both the node and the string
Chris@17 127 // are translated.
Chris@17 128 $this->drupalGet('/es/node/1');
Chris@17 129 $assert_session->pageTextContains('Español');
Chris@17 130 $assert_session->pageTextContains('Funciona con');
Chris@17 131 $assert_session->pageTextNotContains('Powered by');
Chris@17 132
Chris@17 133 // Check if the Powered by string is shown in English on an
Chris@17 134 // administration page, and the node content is shown in Spanish.
Chris@17 135 $this->drupalGet('/es/node/1/edit');
Chris@17 136 $assert_session->pageTextContains('Español');
Chris@17 137 $assert_session->pageTextContains('Powered by');
Chris@17 138 $assert_session->pageTextNotContains('Funciona con');
Chris@17 139 }
Chris@17 140
Chris@17 141 /**
Chris@17 142 * Test translation with URL and Session Language Negotiators.
Chris@17 143 */
Chris@17 144 public function testUrlContentTranslationWithSessionLanguage() {
Chris@17 145 $assert_session = $this->assertSession();
Chris@17 146 /** @var \Drupal\language\LanguageNegotiatorInterface $language_negotiator */
Chris@17 147 $language_negotiator = \Drupal::getContainer()->get('language_negotiator');
Chris@17 148 // Set Interface Language Negotiator to Session.
Chris@17 149 $language_negotiator->saveConfiguration('language_interface', [
Chris@17 150 'language-session' => 1,
Chris@17 151 'language-url' => 2,
Chris@17 152 'language-selected' => 3,
Chris@17 153 ]);
Chris@17 154
Chris@17 155 // Set Content Language Negotiator to URL.
Chris@17 156 $language_negotiator->saveConfiguration('language_content', [
Chris@17 157 'language-url' => 4,
Chris@17 158 'language-selected' => 5,
Chris@17 159 ]);
Chris@17 160
Chris@17 161 // See if the full view of the node in english is present and the
Chris@17 162 // string in the Powered By Block is in English.
Chris@17 163 $this->drupalGet('/node/1');
Chris@17 164 $assert_session->pageTextContains('English');
Chris@17 165 $assert_session->pageTextContains('Powered by');
Chris@17 166
Chris@17 167 // The language session variable has not been set yet, so
Chris@17 168 // The string should be in Spanish.
Chris@17 169 $this->drupalGet('/es/node/1');
Chris@17 170 $assert_session->pageTextContains('Español');
Chris@17 171 $assert_session->pageTextNotContains('Powered by');
Chris@17 172 $assert_session->pageTextContains('Funciona con');
Chris@17 173
Chris@17 174 // Set the session language to Spanish but load the English node page.
Chris@17 175 $this->drupalGet('/node/1', ['query' => ['language' => 'es']]);
Chris@17 176 $assert_session->pageTextContains('English');
Chris@17 177 $assert_session->pageTextNotContains('Español');
Chris@17 178 $assert_session->pageTextContains('Funciona con');
Chris@17 179 $assert_session->pageTextNotContains('Powered by');
Chris@17 180
Chris@17 181 // Set the session language to English but load the node page in Spanish.
Chris@17 182 $this->drupalGet('/es/node/1', ['query' => ['language' => 'en']]);
Chris@17 183 $assert_session->pageTextNotContains('English');
Chris@17 184 $assert_session->pageTextContains('Español');
Chris@17 185 $assert_session->pageTextNotContains('Funciona con');
Chris@17 186 $assert_session->pageTextContains('Powered by');
Chris@17 187 }
Chris@17 188
Chris@17 189 }