annotate core/modules/locale/tests/src/Functional/LocaleConfigTranslationTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\locale\Functional;
Chris@0 4
Chris@0 5 use Drupal\Tests\BrowserTestBase;
Chris@0 6 use Drupal\Core\Language\LanguageInterface;
Chris@0 7
Chris@0 8 /**
Chris@0 9 * Tests translation of configuration strings.
Chris@0 10 *
Chris@0 11 * @group locale
Chris@0 12 */
Chris@0 13 class LocaleConfigTranslationTest extends BrowserTestBase {
Chris@0 14
Chris@0 15 /**
Chris@0 16 * The language code used.
Chris@0 17 *
Chris@0 18 * @var string
Chris@0 19 */
Chris@0 20 protected $langcode;
Chris@0 21
Chris@0 22 /**
Chris@0 23 * Modules to enable.
Chris@0 24 *
Chris@0 25 * @var array
Chris@0 26 */
Chris@0 27 public static $modules = ['locale', 'contact', 'contact_test'];
Chris@0 28
Chris@0 29 /**
Chris@0 30 * {@inheritdoc}
Chris@0 31 */
Chris@0 32 protected function setUp() {
Chris@0 33 parent::setUp();
Chris@0 34 // Add a default locale storage for all these tests.
Chris@0 35 $this->storage = $this->container->get('locale.storage');
Chris@0 36
Chris@0 37 // Enable import of translations. By default this is disabled for automated
Chris@0 38 // tests.
Chris@0 39 $this->config('locale.settings')
Chris@0 40 ->set('translation.import_enabled', TRUE)
Chris@16 41 ->set('translation.use_source', LOCALE_TRANSLATION_USE_SOURCE_LOCAL)
Chris@0 42 ->save();
Chris@0 43
Chris@0 44 // Add custom language.
Chris@0 45 $this->langcode = 'xx';
Chris@0 46 $admin_user = $this->drupalCreateUser(['administer languages', 'access administration pages', 'translate interface', 'administer modules', 'access site-wide contact form', 'administer contact forms', 'administer site configuration']);
Chris@0 47 $this->drupalLogin($admin_user);
Chris@0 48 $name = $this->randomMachineName(16);
Chris@0 49 $edit = [
Chris@0 50 'predefined_langcode' => 'custom',
Chris@0 51 'langcode' => $this->langcode,
Chris@0 52 'label' => $name,
Chris@0 53 'direction' => LanguageInterface::DIRECTION_LTR,
Chris@0 54 ];
Chris@0 55 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
Chris@0 56 // Set path prefix.
Chris@0 57 $edit = ["prefix[$this->langcode]" => $this->langcode];
Chris@0 58 $this->drupalPostForm('admin/config/regional/language/detection/url', $edit, t('Save configuration'));
Chris@0 59 }
Chris@0 60
Chris@0 61 /**
Chris@0 62 * Tests basic configuration translation.
Chris@0 63 */
Chris@0 64 public function testConfigTranslation() {
Chris@0 65 // Check that the maintenance message exists and create translation for it.
Chris@0 66 $source = '@site is currently under maintenance. We should be back shortly. Thank you for your patience.';
Chris@0 67 $string = $this->storage->findString(['source' => $source, 'context' => '', 'type' => 'configuration']);
Chris@0 68 $this->assertTrue($string, 'Configuration strings have been created upon installation.');
Chris@0 69
Chris@0 70 // Translate using the UI so configuration is refreshed.
Chris@0 71 $message = $this->randomMachineName(20);
Chris@0 72 $search = [
Chris@0 73 'string' => $string->source,
Chris@0 74 'langcode' => $this->langcode,
Chris@0 75 'translation' => 'all',
Chris@0 76 ];
Chris@0 77 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
Chris@0 78 $textareas = $this->xpath('//textarea');
Chris@0 79 $textarea = current($textareas);
Chris@0 80 $lid = $textarea->getAttribute('name');
Chris@0 81 $edit = [
Chris@0 82 $lid => $message,
Chris@0 83 ];
Chris@0 84 $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
Chris@0 85
Chris@0 86 // Get translation and check we've only got the message.
Chris@0 87 $translation = \Drupal::languageManager()->getLanguageConfigOverride($this->langcode, 'system.maintenance')->get();
Chris@0 88 $this->assertEqual(count($translation), 1, 'Got the right number of properties after translation.');
Chris@0 89 $this->assertEqual($translation['message'], $message);
Chris@0 90
Chris@0 91 // Check default medium date format exists and create a translation for it.
Chris@0 92 $string = $this->storage->findString(['source' => 'D, m/d/Y - H:i', 'context' => 'PHP date format', 'type' => 'configuration']);
Chris@0 93 $this->assertTrue($string, 'Configuration date formats have been created upon installation.');
Chris@0 94
Chris@0 95 // Translate using the UI so configuration is refreshed.
Chris@0 96 $search = [
Chris@0 97 'string' => $string->source,
Chris@0 98 'langcode' => $this->langcode,
Chris@0 99 'translation' => 'all',
Chris@0 100 ];
Chris@0 101 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
Chris@0 102 $textareas = $this->xpath('//textarea');
Chris@0 103 $textarea = current($textareas);
Chris@0 104 $lid = $textarea->getAttribute('name');
Chris@0 105 $edit = [
Chris@0 106 $lid => 'D',
Chris@0 107 ];
Chris@0 108 $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
Chris@0 109
Chris@0 110 $translation = \Drupal::languageManager()->getLanguageConfigOverride($this->langcode, 'core.date_format.medium')->get();
Chris@0 111 $this->assertEqual($translation['pattern'], 'D', 'Got the right date format pattern after translation.');
Chris@0 112
Chris@0 113 // Formatting the date 8 / 27 / 1985 @ 13:37 EST with pattern D should
Chris@0 114 // display "Tue".
Chris@18 115 $formatted_date = $this->container->get('date.formatter')->format(494015820, $type = 'medium', NULL, 'America/New_York', $this->langcode);
Chris@0 116 $this->assertEqual($formatted_date, 'Tue', 'Got the right formatted date using the date format translation pattern.');
Chris@0 117
Chris@0 118 // Assert strings from image module config are not available.
Chris@0 119 $string = $this->storage->findString(['source' => 'Medium (220×220)', 'context' => '', 'type' => 'configuration']);
Chris@0 120 $this->assertFalse($string, 'Configuration strings have been created upon installation.');
Chris@0 121
Chris@0 122 // Enable the image module.
Chris@0 123 $this->drupalPostForm('admin/modules', ['modules[image][enable]' => "1"], t('Install'));
Chris@0 124 $this->rebuildContainer();
Chris@0 125
Chris@0 126 $string = $this->storage->findString(['source' => 'Medium (220×220)', 'context' => '', 'type' => 'configuration']);
Chris@0 127 $this->assertTrue($string, 'Configuration strings have been created upon installation.');
Chris@0 128 $locations = $string->getLocations();
Chris@0 129 $this->assertTrue(isset($locations['configuration']) && isset($locations['configuration']['image.style.medium']), 'Configuration string has been created with the right location');
Chris@0 130
Chris@0 131 // Check the string is unique and has no translation yet.
Chris@0 132 $translations = $this->storage->getTranslations(['language' => $this->langcode, 'type' => 'configuration', 'name' => 'image.style.medium']);
Chris@0 133 $this->assertEqual(count($translations), 1);
Chris@0 134 $translation = reset($translations);
Chris@0 135 $this->assertEqual($translation->source, $string->source);
Chris@0 136 $this->assertTrue(empty($translation->translation));
Chris@0 137
Chris@0 138 // Translate using the UI so configuration is refreshed.
Chris@0 139 $image_style_label = $this->randomMachineName(20);
Chris@0 140 $search = [
Chris@0 141 'string' => $string->source,
Chris@0 142 'langcode' => $this->langcode,
Chris@0 143 'translation' => 'all',
Chris@0 144 ];
Chris@0 145 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
Chris@0 146 $textarea = current($this->xpath('//textarea'));
Chris@0 147 $lid = $textarea->getAttribute('name');
Chris@0 148 $edit = [
Chris@0 149 $lid => $image_style_label,
Chris@0 150 ];
Chris@0 151 $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
Chris@0 152
Chris@0 153 // Check the right single translation has been created.
Chris@0 154 $translations = $this->storage->getTranslations(['language' => $this->langcode, 'type' => 'configuration', 'name' => 'image.style.medium']);
Chris@0 155 $translation = reset($translations);
Chris@0 156 $this->assertTrue(count($translations) == 1 && $translation->source == $string->source && $translation->translation == $image_style_label, 'Got only one translation for image configuration.');
Chris@0 157
Chris@0 158 // Try more complex configuration data.
Chris@0 159 $translation = \Drupal::languageManager()->getLanguageConfigOverride($this->langcode, 'image.style.medium')->get();
Chris@0 160 $this->assertEqual($translation['label'], $image_style_label, 'Got the right translation for image style name after translation');
Chris@0 161
Chris@0 162 // Uninstall the module.
Chris@0 163 $this->drupalPostForm('admin/modules/uninstall', ['uninstall[image]' => "image"], t('Uninstall'));
Chris@0 164 $this->drupalPostForm(NULL, [], t('Uninstall'));
Chris@0 165
Chris@0 166 // Ensure that the translated configuration has been removed.
Chris@0 167 $override = \Drupal::languageManager()->getLanguageConfigOverride('xx', 'image.style.medium');
Chris@0 168 $this->assertTrue($override->isNew(), 'Translated configuration for image module removed.');
Chris@0 169
Chris@0 170 // Translate default category using the UI so configuration is refreshed.
Chris@0 171 $category_label = $this->randomMachineName(20);
Chris@0 172 $search = [
Chris@0 173 'string' => 'Website feedback',
Chris@0 174 'langcode' => $this->langcode,
Chris@0 175 'translation' => 'all',
Chris@0 176 ];
Chris@0 177 $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
Chris@0 178 $textarea = current($this->xpath('//textarea'));
Chris@0 179 $lid = $textarea->getAttribute('name');
Chris@0 180 $edit = [
Chris@0 181 $lid => $category_label,
Chris@0 182 ];
Chris@0 183 $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
Chris@0 184
Chris@0 185 // Check if this category displayed in this language will use the
Chris@0 186 // translation. This test ensures the entity loaded from the request
Chris@0 187 // upcasting will already work.
Chris@0 188 $this->drupalGet($this->langcode . '/contact/feedback');
Chris@0 189 $this->assertText($category_label);
Chris@0 190
Chris@0 191 // Check if the UI does not show the translated String.
Chris@0 192 $this->drupalGet('admin/structure/contact/manage/feedback');
Chris@0 193 $this->assertFieldById('edit-label', 'Website feedback', 'Translation is not loaded for Edit Form.');
Chris@0 194 }
Chris@0 195
Chris@0 196 /**
Chris@0 197 * Test translatability of optional configuration in locale.
Chris@0 198 */
Chris@0 199 public function testOptionalConfiguration() {
Chris@0 200 $this->assertNodeConfig(FALSE, FALSE);
Chris@0 201 // Enable the node module.
Chris@0 202 $this->drupalPostForm('admin/modules', ['modules[node][enable]' => "1"], t('Install'));
Chris@0 203 $this->drupalPostForm(NULL, [], t('Continue'));
Chris@0 204 $this->rebuildContainer();
Chris@0 205 $this->assertNodeConfig(TRUE, FALSE);
Chris@0 206 // Enable the views module (which node provides some optional config for).
Chris@0 207 $this->drupalPostForm('admin/modules', ['modules[views][enable]' => "1"], t('Install'));
Chris@0 208 $this->rebuildContainer();
Chris@0 209 $this->assertNodeConfig(TRUE, TRUE);
Chris@0 210 }
Chris@0 211
Chris@0 212 /**
Chris@0 213 * Check that node configuration source strings are made available in locale.
Chris@0 214 *
Chris@0 215 * @param bool $required
Chris@0 216 * Whether to assume a sample of the required default configuration is
Chris@0 217 * present.
Chris@0 218 * @param bool $optional
Chris@0 219 * Whether to assume a sample of the optional default configuration is
Chris@0 220 * present.
Chris@0 221 */
Chris@0 222 protected function assertNodeConfig($required, $optional) {
Chris@0 223 // Check the required default configuration in node module.
Chris@0 224 $string = $this->storage->findString(['source' => 'Make content sticky', 'context' => '', 'type' => 'configuration']);
Chris@0 225 if ($required) {
Chris@0 226 $this->assertFalse($this->config('system.action.node_make_sticky_action')->isNew());
Chris@0 227 $this->assertTrue($string, 'Node action text can be found with node module.');
Chris@0 228 }
Chris@0 229 else {
Chris@0 230 $this->assertTrue($this->config('system.action.node_make_sticky_action')->isNew());
Chris@0 231 $this->assertFalse($string, 'Node action text can not be found without node module.');
Chris@0 232 }
Chris@0 233
Chris@0 234 // Check the optional default configuration in node module.
Chris@14 235 $string = $this->storage->findString(['source' => 'No front page content has been created yet.<br/>Follow the <a target="_blank" href="https://www.drupal.org/docs/user_guide/en/index.html">User Guide</a> to start building your site.', 'context' => '', 'type' => 'configuration']);
Chris@0 236 if ($optional) {
Chris@0 237 $this->assertFalse($this->config('views.view.frontpage')->isNew());
Chris@0 238 $this->assertTrue($string, 'Node view text can be found with node and views modules.');
Chris@0 239 }
Chris@0 240 else {
Chris@0 241 $this->assertTrue($this->config('views.view.frontpage')->isNew());
Chris@0 242 $this->assertFalse($string, 'Node view text can not be found without node and/or views modules.');
Chris@0 243 }
Chris@0 244 }
Chris@0 245
Chris@0 246 }