tmgmt_locale.test 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. /**
  3. * Basic Locale Source tests.
  4. */
  5. class TMGMTLocaleSourceTestCase extends TMGMTBaseTestCase {
  6. static function getInfo() {
  7. return array(
  8. 'name' => 'Locale Source tests',
  9. 'description' => 'Exporting source data from locale and saving translations back',
  10. 'group' => 'Translation Management',
  11. );
  12. }
  13. function setUp() {
  14. parent::setUp(array('tmgmt_locale'));
  15. $this->langcode = 'de';
  16. $this->context = 'default';
  17. $file = new stdClass();
  18. $file->uri = drupal_realpath(drupal_get_path('module', 'tmgmt_locale') . '/tests/test.xx.po');
  19. $this->pofile = file_save($file);
  20. $this->setEnvironment($this->langcode);
  21. $this->setEnvironment('es');
  22. }
  23. /**
  24. * Tests translation of a locale singular term.
  25. */
  26. function testSingularTerm() {
  27. // Load PO file to create a locale structure in the database.
  28. _locale_import_po($this->pofile, $this->langcode, LOCALE_IMPORT_OVERWRITE, $this->context);
  29. // Obtain one locale string with translation.
  30. $locale_object = db_query('SELECT * FROM {locales_source} WHERE source = :source LIMIT 1', array(':source' => 'Hello World'))->fetchObject();
  31. $source_text = $locale_object->source;
  32. // Create the new job and job item.
  33. $job = $this->createJob();
  34. $job->translator = $this->default_translator->name;
  35. $job->settings = array();
  36. $job->save();
  37. $item1 = $job->addItem('locale', 'default', $locale_object->lid);
  38. // Check the structure of the imported data.
  39. $this->assertEqual($item1->item_id, $locale_object->lid, 'Locale Strings object correctly saved');
  40. $this->assertEqual('Locale', $item1->getSourceType());
  41. $this->assertEqual('Hello World', $item1->getSourceLabel());
  42. $job->requestTranslation();
  43. foreach ($job->getItems() as $item) {
  44. /* @var $item TMGMTJobItem */
  45. $item->acceptTranslation();
  46. $this->assertTrue($item->isAccepted());
  47. // The source is now available in en and de.
  48. $this->assertJobItemLangCodes($item, 'en', array('en', 'de'));
  49. }
  50. // Check string translation.
  51. $expected_translation = $job->target_language . '_' . $source_text;
  52. $this->assertTranslation($locale_object->lid, 'de', $expected_translation);
  53. // Translate the german translation to spanish.
  54. $target_langcode = 'es';
  55. $job = $this->createJob('de', $target_langcode);
  56. $job->translator = $this->default_translator->name;
  57. $job->settings = array();
  58. $job->save();
  59. $item1 = $job->addItem('locale', 'default', $locale_object->lid);
  60. $this->assertEqual('Locale', $item1->getSourceType());
  61. $this->assertEqual($expected_translation, $item1->getSourceLabel());
  62. $job->requestTranslation();
  63. foreach ($job->getItems() as $item) {
  64. /* @var $item TMGMTJobItem */
  65. $item->acceptTranslation();
  66. $this->assertTrue($item->isAccepted());
  67. // The source should be now available for en, de and es languages.
  68. $this->assertJobItemLangCodes($item, 'en', array('en', 'de', 'es'));
  69. }
  70. // Check string translation.
  71. $this->assertTranslation($locale_object->lid, $target_langcode, $job->target_language . '_' . $expected_translation);
  72. }
  73. /**
  74. * Test if the source is able to pull content in requested language.
  75. */
  76. function testRequestDataForSpecificLanguage() {
  77. $this->setEnvironment('cs');
  78. _locale_import_po($this->pofile, $this->langcode, LOCALE_IMPORT_OVERWRITE, $this->context);
  79. $locale_object = db_query('SELECT * FROM {locales_source} WHERE source = :source LIMIT 1', array(':source' => 'Hello World'))->fetchObject();
  80. $plugin = new TMGMTLocaleSourcePluginController('locale', 'locale');
  81. $reflection_plugin = new ReflectionClass('TMGMTLocaleSourcePluginController');
  82. $updateTranslation = $reflection_plugin->getMethod('updateTranslation');
  83. $updateTranslation->setAccessible(TRUE);
  84. $updateTranslation->invoke($plugin, $locale_object->lid, 'de', 'de translation');
  85. // Create the new job and job item.
  86. $job = $this->createJob('de', 'cs');
  87. $job->save();
  88. $job->addItem('locale', 'default', $locale_object->lid);
  89. $data = $job->getData();
  90. $this->assertEqual($data[1]['singular']['#text'], 'de translation');
  91. // Create new job item with a source language for which the translation
  92. // does not exit.
  93. $job = $this->createJob('es', 'cs');
  94. $job->save();
  95. try {
  96. $job->addItem('locale', 'default', $locale_object->lid);
  97. $this->fail('The job item should not be added as there is no translation for language "es"');
  98. }
  99. catch (TMGMTException $e) {
  100. $languages = language_list();
  101. $this->assertEqual(t('Unable to load %language translation for the locale %id',
  102. array('%language' => $languages['es']->name, '%id' => $locale_object->lid)), $e->getMessage());
  103. }
  104. }
  105. /**
  106. * Verifies that strings that need escaping are correctly identified.
  107. */
  108. function testEscaping() {
  109. $lid = db_insert('locales_source')
  110. ->fields(array(
  111. 'source' => '@place-holders need %to be !esc_aped.',
  112. 'textgroup' => 'default',
  113. 'context' => '',
  114. ))
  115. ->execute();
  116. $job = $this->createJob('en', 'de');
  117. $job->translator = $this->default_translator->name;
  118. $job->settings = array();
  119. $job->save();
  120. $item = $job->addItem('locale', 'default', $lid);
  121. $data = $item->getData();
  122. $expected_escape = array(
  123. 0 => array('string' => '@place-holders'),
  124. 20 => array('string' => '%to'),
  125. 27 => array('string' => '!esc_aped'),
  126. );
  127. $this->assertEqual($data['singular']['#escape'], $expected_escape);
  128. // Invalid patterns that should be ignored.
  129. $lid = db_insert('locales_source')
  130. ->fields(array(
  131. 'source' => '@ % ! example',
  132. 'textgroup' => 'default',
  133. 'context' => '',
  134. ))
  135. ->execute();
  136. $item = $job->addItem('locale', 'default', $lid);
  137. $data = $item->getData();
  138. $this->assertTrue(empty($data[$lid]['#escape']));
  139. }
  140. /**
  141. * Tests that system behaves correctly with an non-existing locales.
  142. */
  143. function testInexistantSource() {
  144. // Create inexistant locale object.
  145. $locale_object = new stdClass();
  146. $locale_object->lid = 0;
  147. // Create the job.
  148. $job = $this->createJob();
  149. $job->translator = $this->default_translator->name;
  150. $job->settings = array();
  151. $job->save();
  152. // Create the job item.
  153. try {
  154. $job->addItem('locale', 'default', $locale_object->lid);
  155. $this->fail('Job item add with an inexistant locale.');
  156. }
  157. catch (TMGMTException $e) {
  158. $this->pass('Exception thrown when trying to translate non-existing locale string');
  159. }
  160. // Try to translate a source string without translation from german to
  161. // spanish.
  162. $lid = db_insert('locales_source')
  163. ->fields(array(
  164. 'source' => 'No translation',
  165. 'textgroup' => 'default',
  166. 'context' => '',
  167. ))
  168. ->execute();
  169. $job = $this->createJob('de', 'fr');
  170. $job->translator = $this->default_translator->name;
  171. $job->settings = array();
  172. $job->save();
  173. try {
  174. $job->addItem('locale', 'default', $lid);
  175. $this->fail('Job item add with an non-existing locale did not fail.');
  176. }
  177. catch (TMGMTException $e) {
  178. $this->pass('Job item add with an non-existing locale did fail.');
  179. }
  180. }
  181. /**
  182. * Asserts a locale translation.
  183. *
  184. * @param int $lid
  185. * The locale source id.
  186. * @param string $target_langcode
  187. * The target language code.
  188. * @param string $expected_translation
  189. * The expected translation.
  190. */
  191. public function assertTranslation($lid, $target_langcode, $expected_translation) {
  192. $actual_translation = db_query('SELECT translation FROM {locales_target} WHERE lid = :lid AND language = :language', array(
  193. ':lid' => $lid,
  194. ':language' => $target_langcode
  195. ))->fetchField();
  196. $this->assertEqual($actual_translation, $expected_translation);
  197. }
  198. }