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

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

namespace Drupal\locale\Tests;

use Drupal\simpletest\WebTestBase;
11
use Drupal\core\language\languageInterface;
12 13

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

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

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

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

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

    // Check site name string exists and create translation for it.
    $string = $this->storage->findString(array('source' => 'Drupal', 'context' => '', 'type' => 'configuration'));
    $this->assertTrue($string, 'Configuration strings have been created upon installation.');

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

    $wrapper = $this->container->get('locale.config.typed')->get('system.site');

    // Get translation and check we've only got the site name.
    $translation = $wrapper->getTranslation($langcode);
86
    $properties = $translation->getElements();
87 88 89 90 91 92
    $this->assertEqual(count($properties), 1, 'Got the right number of properties after translation');

    // Check the translated site name is displayed.
    $this->drupalGet($langcode);
    $this->assertText($site_name, 'The translated site name is displayed after translations refreshed.');

93
    // Check default medium date format exists and create a translation for it.
94
    $string = $this->storage->findString(array('source' => 'D, m/d/Y - H:i', 'context' => 'PHP date format', 'type' => 'configuration'));
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
    $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'));

112
    $wrapper = $this->container->get('locale.config.typed')->get('core.date_format.medium');
113 114 115

    // Get translation and check we've only got the site name.
    $translation = $wrapper->getTranslation($langcode);
116
    $format = $translation->get('pattern')->getValue();
117 118 119 120 121 122 123
    $this->assertEqual($format, 'D', 'Got the right date format pattern after translation.');

    // 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.');

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

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

132
    $string = $this->storage->findString(array('source' => 'Medium (220×220)', 'context' => '', 'type' => 'configuration'));
133 134 135 136 137 138
    $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'));
139
    $this->assertEqual(count($translations), 1);
140
    $translation = reset($translations);
141 142
    $this->assertEqual($translation->source, $string->source);
    $this->assertTrue(empty($translation->translation));
143 144

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

    // 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.
    $wrapper = $this->container->get('locale.config.typed')->get('image.style.medium');

    $translation = $wrapper->getTranslation($langcode);
    $property = $translation->get('label');
    $this->assertEqual($property->getValue(), $image_style_label, 'Got the right translation for image style name after translation');

    // Quick test to ensure translation file exists.
172 173
    $override = \Drupal::languageManager()->getLanguageConfigOverride('xx', 'image.style.medium');
    $this->assertEqual($override->get('label'), $image_style_label);
174

175
    // Uninstall the module.
176 177
    $this->drupalPostForm('admin/modules/uninstall', array('uninstall[image]' => "image"), t('Uninstall'));
    $this->drupalPostForm(NULL, array(), t('Uninstall'));
178 179

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

    // Translate default category using the UI so configuration is refreshed.
184
    $category_label = $this->randomMachineName(20);
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
    $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);
203 204 205 206

    // 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.');
207 208 209
  }

}