i18n_string.test 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. }
  50. /**
  51. * Test base i18n_string caching.
  52. */
  53. function testCaching() {
  54. // Create a bunch of strings for all languages.
  55. $textgroup = 'test_cached';
  56. $strings = $this->stringCreateArray(2);
  57. $translations = array();
  58. $textgroup_object = i18n_string_textgroup($textgroup);
  59. // Save source strings and store translations.
  60. foreach ($strings as $key => $string) {
  61. $name = "$textgroup:item:$key:title";
  62. i18n_string_update($name, $string);
  63. $translations[$key] = $this->createStringTranslation($textgroup, $string);
  64. }
  65. // Now fetch the strings to fill the cache.
  66. foreach ($textgroup_object->strings as $context => $string_object) {
  67. $this->drupalGet('tests/i18n/i18n_string_build/' . $textgroup . ':' . $context);
  68. }
  69. foreach ($strings as $key => $string) {
  70. $this->drupalGet('tests/i18n/i18n_string_translation_search/' . $textgroup . ':item:' . $key . ':*/es');
  71. }
  72. // Check the persistent cache for contents.
  73. $cache = cache_get('i18n:string:tgroup:' . $textgroup . ':strings');
  74. if ($this->assertNotEqual($cache, FALSE, 'Textgroup strings cache found')) {
  75. foreach ($textgroup_object->strings as $context => $string_object) {
  76. if ($this->assertTrue(isset($cache->data[$context]), format_string('Cached string %context found', array('%context' => $context)))) {
  77. $this->assertEqual($cache->data[$context], $context, 'Cached string is a string and not an object');
  78. }
  79. // Check if the string object cache is also available.
  80. $string_cache = cache_get($string_object->get_cid());
  81. if ($this->assertNotEqual($string_cache, FALSE, format_string('Cached string object %cid found', array('%cid' => $string_object->get_cid())))) {
  82. $this->assertTrue(is_array($string_cache->data), 'Cached string object is an array.');
  83. }
  84. }
  85. }
  86. $cache = cache_get('i18n:string:tgroup:' . $textgroup . ':cache_multiple');
  87. if ($this->assertNotEqual($cache, FALSE, 'Textgroup cache_multiple cache found')) {
  88. foreach ($strings as $key => $string) {
  89. $pattern = 'item:' . $key . ':*:es';
  90. if ($this->assertTrue(isset($cache->data[$pattern]), format_string('Cached multiple cache for pattern %pattern found', array('%pattern_key' => $pattern)))) {
  91. $property_pattern = 'item:' . $key . ':title';
  92. if ($this->assertTrue(isset($cache->data[$pattern][$property_pattern]), format_string('Cached multiple property title found', array('%pattern_key' => $pattern)))) {
  93. $this->assertEqual($cache->data[$pattern][$property_pattern], $property_pattern);
  94. }
  95. }
  96. }
  97. }
  98. // Test cache injection.
  99. foreach ($textgroup_object->strings as $context => $string_object) {
  100. // Check if the string object cache is also available.
  101. $string_cache = cache_get($string_object->get_cid());
  102. if (isset($string_cache->data)) {
  103. // Modify cache.
  104. $string_cache->data['string'] = "Injected value.";
  105. cache_set($string_object->get_cid(), $string_cache->data, 'cache', CACHE_TEMPORARY);
  106. // Check if modification is reflected on the next page call.
  107. $this->drupalGet('tests/i18n/i18n_string_build/' . $textgroup . ':' . $context);
  108. $this->assertText($string_cache->data['string']);
  109. }
  110. }
  111. // Test that un-translated strings are cached correctly.
  112. $textgroup = 'test_cached';
  113. $key = 3;
  114. $string = self::randomName(100);
  115. $name = "$textgroup:item:$key:title";
  116. i18n_string_update($name, $string);
  117. // Generate the cache entry.
  118. $string_object = i18n_string_build($name, $string);
  119. $langcode = i18n_langcode();
  120. $string_object->get_translation($langcode);
  121. // Destroy the textgroup object to write the cache entry.
  122. $textgroup_object = i18n_string_textgroup($textgroup);
  123. $textgroup_object->__destruct();
  124. $this->assertTrue(cache_get($string_object->get_cid()) !== FALSE, "Cache entry created.");
  125. drupal_static_reset('i18n_string_textgroup');
  126. // Reset the loaded translation variable.
  127. variable_del('i18n_loaded_translations');
  128. $loaded_translations = variable_get('i18n_loaded_translations', array());
  129. $this->verbose(var_export($loaded_translations, TRUE));
  130. // Rebuild the string.
  131. $string_object = i18n_string_build($name, $string);
  132. $string_object->get_translation($langcode);
  133. // Check that the string hasn't been loaded.
  134. $loaded_translations = variable_get('i18n_loaded_translations', array());
  135. $this->verbose(var_export($loaded_translations, TRUE));
  136. $this->assertFalse(isset($loaded_translations['test_cached:item:3:title']), "The untranslated string was correctly cached.");
  137. }
  138. /**
  139. * Create strings for all languages
  140. */
  141. public static function stringCreateAll($number = 10, $length = 100) {
  142. foreach (language_list() as $lang => $language) {
  143. $strings[$lang] = self::stringCreateArray($number, $length);
  144. }
  145. return $strings;
  146. }
  147. /**
  148. * Create a bunch of random strings to test the API
  149. */
  150. public static function stringCreateArray($number = 10, $length = 100) {
  151. for ($i=1 ; $i <= $number ; $i++) {
  152. $strings[$i] = self::randomName($length);
  153. }
  154. return $strings;
  155. }
  156. /**
  157. * Create and store one translation into the db
  158. */
  159. public function stringCreateTranslation($name, $lang, $length = 20) {
  160. $translation = $this->randomName($length);
  161. if (self::stringSaveTranslation($name, $lang, $translation)) {
  162. return $translation;
  163. }
  164. }
  165. /**
  166. * Translate one string into the db
  167. */
  168. public static function stringSaveTranslation($name, $lang, $translation) {
  169. list($textgroup, $context) = i18n_string_context($name);
  170. return i18n_string_textgroup($textgroup)->update_translation($context, $lang, $translation);
  171. }
  172. }