i18n_string.test 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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');
  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. * Create strings for all languages
  52. */
  53. public static function stringCreateAll($number = 10, $length = 100) {
  54. foreach (language_list() as $lang => $language) {
  55. $strings[$lang] = self::stringCreateArray($number, $length);
  56. }
  57. return $strings;
  58. }
  59. /**
  60. * Create a bunch of random strings to test the API
  61. */
  62. public static function stringCreateArray($number = 10, $length = 100) {
  63. for ($i=1 ; $i <= $number ; $i++) {
  64. $strings[$i] = self::randomName($length);
  65. }
  66. return $strings;
  67. }
  68. /**
  69. * Create and store one translation into the db
  70. */
  71. public static function stringCreateTranslation($name, $lang, $length = 20) {
  72. $translation = $this->randomName($length);
  73. if (self::stringSaveTranslation($name, $lang, $translation)) {
  74. return $translation;
  75. }
  76. }
  77. /**
  78. * Translate one string into the db
  79. */
  80. public static function stringSaveTranslation($name, $lang, $translation) {
  81. list($textgroup, $context) = i18n_string_context($name);
  82. return i18n_string_textgroup($textgroup)->update_translation($context, $lang, $translation);
  83. }
  84. }