LocaleConfigTranslationTest.php 10.9 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\locale\Tests\LocaleConfigTranslationTest.
 */

namespace Drupal\locale\Tests;

10
use Drupal\Component\Utility\SafeMarkup;
11
use Drupal\simpletest\WebTestBase;
12
use Drupal\core\language\languageInterface;
13 14

/**
15 16 17
 * Tests translation of configuration strings.
 *
 * @group locale
18 19 20
 */
class LocaleConfigTranslationTest extends WebTestBase {

21 22 23 24 25 26 27
  /**
   * The language code used.
   *
   * @var string
   */
  protected $langcode;

28 29 30 31 32
  /**
   * Modules to enable.
   *
   * @var array
   */
33
  public static $modules = array('locale', 'contact', 'contact_test');
34

35 36 37
  /**
   * {@inheritdoc}
   */
38
  protected function setUp() {
39 40
    parent::setUp();
    // Add a default locale storage for all these tests.
41
    $this->storage = $this->container->get('locale.storage');
42 43 44

    // Enable import of translations. By default this is disabled for automated
    // tests.
45
    $this->config('locale.settings')
46 47
      ->set('translation.import_enabled', TRUE)
      ->save();
48 49

    // Add custom language.
50
    $this->langcode = 'xx';
51
    $admin_user = $this->drupalCreateUser(array('administer languages', 'access administration pages', 'translate interface', 'administer modules', 'access site-wide contact form', 'administer contact forms', 'administer site configuration'));
52
    $this->drupalLogin($admin_user);
53
    $name = $this->randomMachineName(16);
54 55
    $edit = array(
      'predefined_langcode' => 'custom',
56
      'langcode' => $this->langcode,
57
      'label' => $name,
58
      'direction' => LanguageInterface::DIRECTION_LTR,
59
    );
60
    $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
61
    // Set path prefix.
62
    $edit = ["prefix[$this->langcode]" => $this->langcode];
63
    $this->drupalPostForm('admin/config/regional/language/detection/url', $edit, t('Save configuration'));
64
  }
65

66 67 68 69
  /**
   * Tests basic configuration translation.
   */
  public function testConfigTranslation() {
70 71 72
    // Check that the maintenance message exists and create translation for it.
    $source = '@site is currently under maintenance. We should be back shortly. Thank you for your patience.';
    $string = $this->storage->findString(array('source' => $source, 'context' => '', 'type' => 'configuration'));
73 74 75
    $this->assertTrue($string, 'Configuration strings have been created upon installation.');

    // Translate using the UI so configuration is refreshed.
76
    $message = $this->randomMachineName(20);
77 78
    $search = array(
      'string' => $string->source,
79
      'langcode' => $this->langcode,
80 81
      'translation' => 'all',
    );
82
    $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
83 84 85 86
    $textareas = $this->xpath('//textarea');
    $textarea = current($textareas);
    $lid = (string) $textarea[0]['name'];
    $edit = array(
87
      $lid => $message,
88
    );
89
    $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
90

91
    // Get translation and check we've only got the message.
92
    $translation = \Drupal::languageManager()->getLanguageConfigOverride($this->langcode, 'system.maintenance')->get();
93
    $this->assertEqual(count($translation), 1, 'Got the right number of properties after translation.');
94
    $this->assertEqual($translation['message'], $message);
95

96
    // Check default medium date format exists and create a translation for it.
97
    $string = $this->storage->findString(array('source' => 'D, m/d/Y - H:i', 'context' => 'PHP date format', 'type' => 'configuration'));
98 99 100 101 102
    $this->assertTrue($string, 'Configuration date formats have been created upon installation.');

    // Translate using the UI so configuration is refreshed.
    $search = array(
      'string' => $string->source,
103
      'langcode' => $this->langcode,
104 105 106 107 108 109 110 111 112 113 114
      'translation' => 'all',
    );
    $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
    $textareas = $this->xpath('//textarea');
    $textarea = current($textareas);
    $lid = (string) $textarea[0]['name'];
    $edit = array(
      $lid => 'D',
    );
    $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));

115
    $translation = \Drupal::languageManager()->getLanguageConfigOverride($this->langcode, 'core.date_format.medium')->get();
116
    $this->assertEqual($translation['pattern'], 'D', 'Got the right date format pattern after translation.');
117 118 119

    // Formatting the date 8 / 27 / 1985 @ 13:37 EST with pattern D should
    // display "Tue".
120
    $formatted_date = format_date(494015820, $type = 'medium', NULL, NULL, $this->langcode);
121 122
    $this->assertEqual($formatted_date, 'Tue', 'Got the right formatted date using the date format translation pattern.');

123
    // Assert strings from image module config are not available.
124
    $string = $this->storage->findString(array('source' => 'Medium (220×220)', 'context' => '', 'type' => 'configuration'));
125 126 127
    $this->assertFalse($string, 'Configuration strings have been created upon installation.');

    // Enable the image module.
128
    $this->drupalPostForm('admin/modules', array('modules[Field types][image][enable]' => "1"), t('Save configuration'));
129
    $this->rebuildContainer();
130

131
    $string = $this->storage->findString(array('source' => 'Medium (220×220)', 'context' => '', 'type' => 'configuration'));
132 133 134 135 136
    $this->assertTrue($string, 'Configuration strings have been created upon installation.');
    $locations = $string->getLocations();
    $this->assertTrue(isset($locations['configuration']) && isset($locations['configuration']['image.style.medium']), 'Configuration string has been created with the right location');

    // Check the string is unique and has no translation yet.
137
    $translations = $this->storage->getTranslations(['language' => $this->langcode, 'type' => 'configuration', 'name' => 'image.style.medium']);
138
    $this->assertEqual(count($translations), 1);
139
    $translation = reset($translations);
140 141
    $this->assertEqual($translation->source, $string->source);
    $this->assertTrue(empty($translation->translation));
142 143

    // Translate using the UI so configuration is refreshed.
144
    $image_style_label = $this->randomMachineName(20);
145 146
    $search = array(
      'string' => $string->source,
147
      'langcode' => $this->langcode,
148 149
      'translation' => 'all',
    );
150
    $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
151 152 153 154 155
    $textarea = current($this->xpath('//textarea'));
    $lid = (string) $textarea[0]['name'];
    $edit = array(
      $lid => $image_style_label,
    );
156
    $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
157 158

    // Check the right single translation has been created.
159
    $translations = $this->storage->getTranslations(['language' => $this->langcode, 'type' => 'configuration', 'name' => 'image.style.medium']);
160 161 162 163
    $translation = reset($translations);
    $this->assertTrue(count($translations) == 1 && $translation->source == $string->source && $translation->translation == $image_style_label, 'Got only one translation for image configuration.');

    // Try more complex configuration data.
164
    $translation = \Drupal::languageManager()->getLanguageConfigOverride($this->langcode, 'image.style.medium')->get();
165
    $this->assertEqual($translation['label'], $image_style_label, 'Got the right translation for image style name after translation');
166

167
    // Uninstall the module.
168 169
    $this->drupalPostForm('admin/modules/uninstall', array('uninstall[image]' => "image"), t('Uninstall'));
    $this->drupalPostForm(NULL, array(), t('Uninstall'));
170 171

    // Ensure that the translated configuration has been removed.
172 173
    $override = \Drupal::languageManager()->getLanguageConfigOverride('xx', 'image.style.medium');
    $this->assertTrue($override->isNew(), 'Translated configuration for image module removed.');
174 175

    // Translate default category using the UI so configuration is refreshed.
176
    $category_label = $this->randomMachineName(20);
177 178
    $search = array(
      'string' => 'Website feedback',
179
      'langcode' => $this->langcode,
180 181 182 183 184 185 186 187 188 189 190 191 192
      'translation' => 'all',
    );
    $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
    $textarea = current($this->xpath('//textarea'));
    $lid = (string) $textarea[0]['name'];
    $edit = array(
      $lid => $category_label,
    );
    $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));

    // Check if this category displayed in this language will use the
    // translation. This test ensures the entity loaded from the request
    // upcasting will already work.
193
    $this->drupalGet($this->langcode . '/contact/feedback');
194
    $this->assertText($category_label);
195 196 197 198

    // Check if the UI does not show the translated String.
    $this->drupalGet('admin/structure/contact/manage/feedback');
    $this->assertFieldById('edit-label', 'Website feedback', 'Translation is not loaded for Edit Form.');
199 200
  }

201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
  /**
   * Test translatability of optional configuration in locale.
   */
  public function testOptionalConfiguration() {
    $this->assertNodeConfig(FALSE, FALSE);
    // Enable the node module.
    $this->drupalPostForm('admin/modules', ['modules[Core][node][enable]' => "1"], t('Save configuration'));
    $this->drupalPostForm(NULL, [], t('Continue'));
    $this->rebuildContainer();
    $this->assertNodeConfig(TRUE, FALSE);
    // Enable the views module (which node provides some optional config for).
    $this->drupalPostForm('admin/modules', ['modules[Core][views][enable]' => "1"], t('Save configuration'));
    $this->rebuildContainer();
    $this->assertNodeConfig(TRUE, TRUE);
  }

  /**
   * Check that node configuration source strings are made available in locale.
   *
   * @param bool $required
   *   Whether to assume a sample of the required default configuration is
   *   present.
   * @param bool $optional
   *   Whether to assume a sample of the optional default configuration is
   *   present.
   */
  protected function assertNodeConfig($required, $optional) {
    // Check the required default configuration in node module.
    $string = $this->storage->findString(['source' => 'Make content sticky', 'context' => '', 'type' => 'configuration']);
    if ($required) {
      $this->assertFalse($this->config('system.action.node_make_sticky_action')->isNew());
      $this->assertTrue($string, 'Node action text can be found with node module.');
    }
    else {
      $this->assertTrue($this->config('system.action.node_make_sticky_action')->isNew());
      $this->assertFalse($string, 'Node action text can not be found without node module.');
    }

    // Check the optional default configuration in node module.
    $string = $this->storage->findString(['source' => 'No front page content has been created yet.', 'context' => '', 'type' => 'configuration']);
    if ($optional) {
      $this->assertFalse($this->config('views.view.frontpage')->isNew());
      $this->assertTrue($string, 'Node view text can be found with node and views modules.');
    }
    else {
      $this->assertTrue($this->config('views.view.frontpage')->isNew());
      $this->assertFalse($string, 'Node view text can not be found without node and/or views modules.');
    }
  }

251
}