i18n_string.test 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /**
  3. * @file
  4. * Test case for multilingual string
  5. */
  6. /**
  7. * Class for testing i18n_string and modules using these features
  8. *
  9. * Tests basic API functions
  10. */
  11. class i18nStringTestCase extends Drupali18nTestCase {
  12. public static function getInfo() {
  13. return array(
  14. 'name' => 'String translation API',
  15. 'group' => 'Internationalization',
  16. 'description' => 'User defined strings translation functions'
  17. );
  18. }
  19. function setUp() {
  20. // We can use any of the modules that define a text group, to use it for testing
  21. parent::setUp('i18n_string', 'i18n_menu', 'i18n_test');
  22. parent::setUpLanguages();
  23. $this->translator = $this->drupalCreateUser(array('translate interface', 'translate user-defined strings'));
  24. }
  25. /**
  26. * Test base i18n_string API
  27. */
  28. function testStringsAPI() {
  29. // Create a bunch of strings for all languages
  30. $textgroup = 'menu';
  31. $strings = $this->stringCreateArray(2);
  32. $translations = array();
  33. // Save source strings and store translations
  34. foreach ($strings as $key => $string) {
  35. $name = "$textgroup:item:$key:title";
  36. i18n_string_update($name, $string);
  37. $translations[$key] = $this->createStringTranslation($textgroup, $string);
  38. }
  39. // Reset cache for text group
  40. i18n_string_textgroup($textgroup)->cache_reset();
  41. // Check translations using the API
  42. foreach ($this->getOtherLanguages() as $language) {
  43. foreach ($strings as $key => $value) {
  44. $name = "$textgroup:item:$key:title";
  45. $translation = i18n_string_translate($name, 'NOT FOUND', array('langcode' => $language->language));
  46. $this->assertEqual($translation, $translations[$key][$language->language], "The right $language->name ($language->language) translation has been retrieved for $name, $translation");
  47. }
  48. }
  49. // Test that regular strings can be translated. Use 'Built-in interface' as
  50. // filter, and translate first one.
  51. $search = array(
  52. 'language' => 'all',
  53. 'translation' => 'all',
  54. 'group' => 'default',
  55. 'string' => '',
  56. );
  57. $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  58. $this->clickLink(t('edit'));
  59. // Just add a random translation.
  60. $translation = $this->randomName();
  61. $edit = array();
  62. foreach ($this->getOtherLanguages() as $language) {
  63. $langcode = $language->language;
  64. $edit["translations[$langcode]"] = $translation;
  65. }
  66. $this->drupalPost(NULL, $edit, t('Save translations'));
  67. $this->assertText(t('The string has been saved.'), t('The string has been saved.'));
  68. $this->assertEqual($this->getUrl(), url('admin/config/regional/translate/translate', array('absolute' => TRUE)), t('Correct page redirection.'));
  69. }
  70. /**
  71. * Test base i18n_string caching.
  72. */
  73. function testCaching() {
  74. // Create a bunch of strings for all languages.
  75. $textgroup = 'test_cached';
  76. $strings = $this->stringCreateArray(2);
  77. $translations = array();
  78. $textgroup_object = i18n_string_textgroup($textgroup);
  79. // Save source strings and store translations.
  80. foreach ($strings as $key => $string) {
  81. $name = "$textgroup:item:$key:title";
  82. i18n_string_update($name, $string);
  83. $translations[$key] = $this->createStringTranslation($textgroup, $string);
  84. }
  85. // Now fetch the strings to fill the cache.
  86. foreach ($textgroup_object->strings as $context => $string_object) {
  87. $this->drupalGet('tests/i18n/i18n_string_build/' . $textgroup . ':' . $context);
  88. }
  89. foreach ($strings as $key => $string) {
  90. $this->drupalGet('tests/i18n/i18n_string_translation_search/' . $textgroup . ':item:' . $key . ':*/es');
  91. }
  92. // Check the persistent cache for contents.
  93. $cache = cache_get('i18n:string:tgroup:' . $textgroup . ':strings');
  94. if ($this->assertNotEqual($cache, FALSE, 'Textgroup strings cache found')) {
  95. foreach ($textgroup_object->strings as $context => $string_object) {
  96. if ($this->assertTrue(isset($cache->data[$context]), format_string('Cached string %context found', array('%context' => $context)))) {
  97. $this->assertEqual($cache->data[$context], $context, 'Cached string is a string and not an object');
  98. }
  99. // Check if the string object cache is also available.
  100. $string_cache = cache_get($string_object->get_cid());
  101. if ($this->assertNotEqual($string_cache, FALSE, format_string('Cached string object %cid found', array('%cid' => $string_object->get_cid())))) {
  102. $this->assertTrue(is_array($string_cache->data), 'Cached string object is an array.');
  103. }
  104. }
  105. }
  106. $cache = cache_get('i18n:string:tgroup:' . $textgroup . ':cache_multiple');
  107. if ($this->assertNotEqual($cache, FALSE, 'Textgroup cache_multiple cache found')) {
  108. foreach ($strings as $key => $string) {
  109. $pattern = 'item:' . $key . ':*:es';
  110. if ($this->assertTrue(isset($cache->data[$pattern]), format_string('Cached multiple cache for pattern %pattern found', array('%pattern_key' => $pattern)))) {
  111. $property_pattern = 'item:' . $key . ':title';
  112. if ($this->assertTrue(isset($cache->data[$pattern][$property_pattern]), format_string('Cached multiple property title found', array('%pattern_key' => $pattern)))) {
  113. $this->assertEqual($cache->data[$pattern][$property_pattern], $property_pattern);
  114. }
  115. }
  116. }
  117. }
  118. // Test cache injection.
  119. foreach ($textgroup_object->strings as $context => $string_object) {
  120. // Check if the string object cache is also available.
  121. $string_cache = cache_get($string_object->get_cid());
  122. if (isset($string_cache->data)) {
  123. // Modify cache.
  124. $string_cache->data['string'] = "Injected value.";
  125. cache_set($string_object->get_cid(), $string_cache->data, 'cache', CACHE_TEMPORARY);
  126. // Check if modification is reflected on the next page call.
  127. $this->drupalGet('tests/i18n/i18n_string_build/' . $textgroup . ':' . $context);
  128. $this->assertText($string_cache->data['string']);
  129. }
  130. }
  131. // Test that un-translated strings are cached correctly.
  132. $textgroup = 'test_cached';
  133. $key = 3;
  134. $string = self::randomName(100);
  135. $name = "$textgroup:item:$key:title";
  136. i18n_string_update($name, $string);
  137. // Generate the cache entry.
  138. $string_object = i18n_string_build($name, $string);
  139. $langcode = i18n_langcode();
  140. $string_object->get_translation($langcode);
  141. // Destroy the textgroup object to write the cache entry.
  142. $textgroup_object = i18n_string_textgroup($textgroup);
  143. $textgroup_object->__destruct();
  144. $this->assertTrue(cache_get($string_object->get_cid()) !== FALSE, "Cache entry created.");
  145. drupal_static_reset('i18n_string_textgroup');
  146. // Reset the loaded translation variable.
  147. variable_del('i18n_loaded_translations');
  148. $loaded_translations = variable_get('i18n_loaded_translations', array());
  149. $this->verbose(var_export($loaded_translations, TRUE));
  150. // Rebuild the string.
  151. $string_object = i18n_string_build($name, $string);
  152. $string_object->get_translation($langcode);
  153. // Check that the string hasn't been loaded.
  154. $loaded_translations = variable_get('i18n_loaded_translations', array());
  155. $this->verbose(var_export($loaded_translations, TRUE));
  156. $this->assertFalse(isset($loaded_translations['test_cached:item:3:title']), "The untranslated string was correctly cached.");
  157. }
  158. /**
  159. * Create strings for all languages
  160. */
  161. public static function stringCreateAll($number = 10, $length = 100) {
  162. foreach (language_list() as $lang => $language) {
  163. $strings[$lang] = self::stringCreateArray($number, $length);
  164. }
  165. return $strings;
  166. }
  167. /**
  168. * Create a bunch of random strings to test the API
  169. */
  170. public static function stringCreateArray($number = 10, $length = 100) {
  171. for ($i=1 ; $i <= $number ; $i++) {
  172. $strings[$i] = self::randomName($length);
  173. }
  174. return $strings;
  175. }
  176. /**
  177. * Create and store one translation into the db
  178. */
  179. public function stringCreateTranslation($name, $lang, $length = 20) {
  180. $translation = $this->randomName($length);
  181. if (self::stringSaveTranslation($name, $lang, $translation)) {
  182. return $translation;
  183. }
  184. }
  185. /**
  186. * Translate one string into the db
  187. */
  188. public static function stringSaveTranslation($name, $lang, $translation) {
  189. list($textgroup, $context) = i18n_string_context($name);
  190. return i18n_string_textgroup($textgroup)->update_translation($context, $lang, $translation);
  191. }
  192. }