annotate core/modules/locale/tests/src/Functional/LocaleConfigTranslationTest.php @ 0:4c8ae668cc8c

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