LocaleConfigTranslationTest.php 8.55 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 21 22 23 24 25
 */
class LocaleConfigTranslationTest extends WebTestBase {

  /**
   * Modules to enable.
   *
   * @var array
   */
26
  public static $modules = array('locale', 'contact', 'contact_test');
27

28 29 30
  /**
   * {@inheritdoc}
   */
31
  protected function setUp() {
32 33
    parent::setUp();
    // Add a default locale storage for all these tests.
34
    $this->storage = $this->container->get('locale.storage');
35 36 37

    // Enable import of translations. By default this is disabled for automated
    // tests.
38
    $this->config('locale.settings')
39 40
      ->set('translation.import_enabled', TRUE)
      ->save();
41 42 43 44 45
  }

  /**
   * Tests basic configuration translation.
   */
46
  public function testConfigTranslation() {
47 48
    // Add custom language.
    $langcode = 'xx';
49
    $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'));
50
    $this->drupalLogin($admin_user);
51
    $name = $this->randomMachineName(16);
52 53 54
    $edit = array(
      'predefined_langcode' => 'custom',
      'langcode' => $langcode,
55
      'label' => $name,
56
      'direction' => LanguageInterface::DIRECTION_LTR,
57
    );
58
    $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
59
    // Set path prefix.
60
    $edit = array("prefix[$langcode]" => $langcode);
61
    $this->drupalPostForm('admin/config/regional/language/detection/url', $edit, t('Save configuration'));
62

63 64 65
    // 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'));
66 67 68
    $this->assertTrue($string, 'Configuration strings have been created upon installation.');

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

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

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

    // Translate using the UI so configuration is refreshed.
    $search = array(
      'string' => $string->source,
      'langcode' => $langcode,
      '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'));

108 109
    $translation = \Drupal::languageManager()->getLanguageConfigOverride($langcode, 'core.date_format.medium')->get();
    $this->assertEqual($translation['pattern'], 'D', 'Got the right date format pattern after translation.');
110 111 112 113 114 115

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

116
    // Assert strings from image module config are not available.
117
    $string = $this->storage->findString(array('source' => 'Medium (220×220)', 'context' => '', 'type' => 'configuration'));
118 119 120
    $this->assertFalse($string, 'Configuration strings have been created upon installation.');

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

124
    $string = $this->storage->findString(array('source' => 'Medium (220×220)', 'context' => '', 'type' => 'configuration'));
125 126 127 128 129 130
    $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.
    $translations = $this->storage->getTranslations(array('language' => $langcode, 'type' => 'configuration', 'name' => 'image.style.medium'));
131
    $this->assertEqual(count($translations), 1);
132
    $translation = reset($translations);
133 134
    $this->assertEqual($translation->source, $string->source);
    $this->assertTrue(empty($translation->translation));
135 136

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

    // Check the right single translation has been created.
    $translations = $this->storage->getTranslations(array('language' => $langcode, 'type' => 'configuration', 'name' => 'image.style.medium'));
    $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.
157 158
    $translation = \Drupal::languageManager()->getLanguageConfigOverride($langcode, 'image.style.medium')->get();
    $this->assertEqual($translation['label'], $image_style_label, 'Got the right translation for image style name after translation');
159

160
    // Uninstall the module.
161 162
    $this->drupalPostForm('admin/modules/uninstall', array('uninstall[image]' => "image"), t('Uninstall'));
    $this->drupalPostForm(NULL, array(), t('Uninstall'));
163 164

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

    // Translate default category using the UI so configuration is refreshed.
169
    $category_label = $this->randomMachineName(20);
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
    $search = array(
      'string' => 'Website feedback',
      'langcode' => $langcode,
      '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.
    $this->drupalGet($langcode . '/contact/feedback');
    $this->assertText($category_label);
188 189 190 191

    // 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.');
192 193 194
  }

}