LocaleConfigTranslationTest.php 9.14 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 18 19 20 21 22 23

/**
 * Tests Metadata for configuration objects.
 */
class LocaleConfigTranslationTest extends WebTestBase {

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

  public static function getInfo() {
    return array(
      'name' => 'Configuration translation',
      'description' => 'Tests translation of configuration strings.',
      'group' => 'Locale',
    );
  }

  public function setUp() {
    parent::setUp();
    // Add a default locale storage for all these tests.
37
    $this->storage = $this->container->get('locale.storage');
38 39 40 41 42 43

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

  /**
   * Tests basic configuration translation.
   */
  function testConfigTranslation() {
    // Add custom language.
    $langcode = 'xx';
52
    $admin_user = $this->drupalCreateUser(array('administer languages', 'access administration pages', 'translate interface', 'administer modules', 'access site-wide contact form', 'administer contact forms'));
53 54 55 56 57 58
    $this->drupalLogin($admin_user);
    $name = $this->randomName(16);
    $edit = array(
      'predefined_langcode' => 'custom',
      'langcode' => $langcode,
      'name' => $name,
59
      'direction' => LanguageInterface::DIRECTION_LTR,
60
    );
61
    $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
62 63
    // Set path prefix.
    $edit = array( "prefix[$langcode]" => $langcode );
64
    $this->drupalPostForm('admin/config/regional/language/detection/url', $edit, t('Save configuration'));
65 66 67 68 69 70 71 72 73 74 75 76

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

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

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
    // 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'));

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

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

128 129 130 131 132
    // 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.
133
    $this->drupalPostForm('admin/modules', array('modules[Field types][image][enable]' => "1"), t('Save configuration'));
134
    $this->rebuildContainer();
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152

    $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.
    $image_style_label = $this->randomName(20);
    $search = array(
      'string' => $string->source,
      'langcode' => $langcode,
      'translation' => 'all',
    );
153
    $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
154 155 156 157 158
    $textarea = current($this->xpath('//textarea'));
    $lid = (string) $textarea[0]['name'];
    $edit = array(
      $lid => $image_style_label,
    );
159
    $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
160 161 162 163 164 165 166 167 168 169 170 171 172 173

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

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

    // Ensure that the translated configuration has been removed.
182 183
    $override = \Drupal::languageManager()->getLanguageConfigOverride('xx', 'image.style.medium');
    $this->assertTrue($override->isNew(), 'Translated configuration for image module removed.');
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204

    // Translate default category using the UI so configuration is refreshed.
    $category_label = $this->randomName(20);
    $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);
205 206 207 208

    // 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.');
209 210 211
  }

}