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

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

namespace Drupal\locale\Tests;

use Drupal\simpletest\WebTestBase;
use Drupal\locale\LocaleTypedConfig;
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');
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 38 39 40

    // Enable import of translations. By default this is disabled for automated
    // tests.
    \Drupal::config('locale.settings')
      ->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'));
50
    $this->drupalLogin($admin_user);
51
    $name = $this->randomMachineName(16);
52 53 54 55
    $edit = array(
      'predefined_langcode' => 'custom',
      'langcode' => $langcode,
      'name' => $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 66 67

    // 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.
68
    $site_name = $this->randomMachineName(20);
69 70 71 72 73
    $search = array(
      'string' => $string->source,
      'langcode' => $langcode,
      'translation' => 'all',
    );
74
    $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
75 76 77 78 79 80
    $textareas = $this->xpath('//textarea');
    $textarea = current($textareas);
    $lid = (string) $textarea[0]['name'];
    $edit = array(
      $lid => $site_name,
    );
81
    $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
82 83 84 85 86 87 88 89 90 91 92 93

    $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);
    $properties = $translation->getProperties();
    $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.');

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

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

    // Get translation and check we've only got the site name.
    $translation = $wrapper->getTranslation($langcode);
117
    $format = $translation->get('pattern')->getValue();
118 119 120 121 122 123 124
    $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.');

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

    // Enable the image module.
130
    $this->drupalPostForm('admin/modules', array('modules[Field types][image][enable]' => "1"), t('Save configuration'));
131
    $this->rebuildContainer();
132 133 134 135 136 137 138 139 140 141 142 143

    $string = $this->storage->findString(array('source' => 'Medium (220x220)', 'context' => '', 'type' => 'configuration'));
    $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'));
    $translation = reset($translations);
    $this->assertTrue(count($translations) == 1 && $translation->source == $string->source && empty($translation->translation), 'Got only one string for image configuration and has no translation.');

    // Translate using the UI so configuration is refreshed.
144
    $image_style_label = $this->randomMachineName(20);
145 146 147 148 149
    $search = array(
      'string' => $string->source,
      'langcode' => $langcode,
      '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 159 160 161 162 163 164 165 166 167 168 169 170

    // 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.
171 172
    $override = \Drupal::languageManager()->getLanguageConfigOverride('xx', 'image.style.medium');
    $this->assertEqual($override->get('label'), $image_style_label);
173

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

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

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

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

}