tmgmt.plugin.test 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. /*
  3. * @file
  4. * Contains tests for Translation management
  5. */
  6. /**
  7. * Tests interaction between core and the plugins.
  8. */
  9. class TMGMTPluginsTestCase extends TMGMTBaseTestCase {
  10. static function getInfo() {
  11. return array(
  12. 'name' => 'Plugin tests',
  13. 'description' => 'Verifies basic functionality of source and translator plugins',
  14. 'group' => 'Translation Management',
  15. );
  16. }
  17. function createJob($source = 'en', $target = 'de', $uid = 1) {
  18. $job = parent::createJob();
  19. for ($i = 1; $i < 3; $i++) {
  20. if ($i == 3) {
  21. // Explicitly define the data for the third item.
  22. $data['data'] = array(
  23. 'dummy' => array(
  24. 'deep_nesting' => array(
  25. '#text' => 'Stored data',
  26. ),
  27. ),
  28. );
  29. $job->addItem('test_source', 'test', $i, array($data));
  30. }
  31. $job->addItem('test_source', 'test', $i);
  32. }
  33. // Manually specify the translator for now.
  34. $job->translator = $this->default_translator->name;
  35. return $job;
  36. }
  37. function testBasicWorkflow() {
  38. // Submit a translation job.
  39. $submit_job = $this->createJob();
  40. $submit_job->settings = array('action' => 'submit');
  41. $submit_job->requestTranslation();
  42. $submit_job = tmgmt_job_load($submit_job->tjid);
  43. $this->assertTrue($submit_job->isActive());
  44. $messages = $submit_job->getMessages();
  45. $last_message = end($messages);
  46. $this->assertEqual('Test submit.', $last_message->message);
  47. // Translate a job.
  48. $translate_job = $this->createJob();
  49. $translate_job->settings = array('action' => 'translate');
  50. $translate_job->requestTranslation();
  51. $translate_job = tmgmt_job_load($translate_job->tjid);
  52. foreach ($translate_job->getItems() as $job_item) {
  53. $this->assertTrue($job_item->isNeedsReview());
  54. }
  55. $messages = $translate_job->getMessages();
  56. // array_values() results in numeric keys, which is necessary for list.
  57. list($debug, $translated, $needs_review) = array_values($messages);
  58. $this->assertEqual('Test translator called.', $debug->message);
  59. $this->assertEqual('debug', $debug->type);
  60. $this->assertEqual('Test translation created.', $translated->message);
  61. $this->assertEqual('status', $translated->type);
  62. // The third message is specific to a job item and has different state
  63. // constants.
  64. $this->assertEqual('The translation of !source to @language is finished and can now be <a href="!review_url">reviewed</a>.', $needs_review->message);
  65. $this->assertEqual('status', $needs_review->type);
  66. $i = 1;
  67. foreach ($translate_job->getItems() as $item) {
  68. // Check the translated text.
  69. if ($i != 3) {
  70. $expected_text = 'de_Text for job item with type ' . $item->item_type . ' and id ' . $item->item_id . '.';
  71. }
  72. else {
  73. // The third item has an explicitly stored data value.
  74. $expected_text = 'de_Stored data';
  75. }
  76. $item_data = $item->getData();
  77. $this->assertEqual($expected_text, $item_data['dummy']['deep_nesting']['#translation']['#text']);
  78. $i++;
  79. }
  80. foreach ($translate_job->getItems() as $job_item) {
  81. $job_item->acceptTranslation();
  82. }
  83. // @todo Accepting does not result in messages on the job anymore.
  84. // Update once there are job item messages.
  85. /*
  86. $messages = $translate_job->getMessages();
  87. $last_message = end($messages);
  88. $this->assertEqual('Job accepted', $last_message->message);
  89. $this->assertEqual('status', $last_message->type);*/
  90. // Check if the translations have been "saved".
  91. foreach ($translate_job->getItems() as $item) {
  92. $this->assertTrue(variable_get('tmgmt_test_saved_translation_' . $item->item_type . '_' . $item->item_id, FALSE));
  93. }
  94. // A rejected job.
  95. $reject_job = $this->createJob();
  96. $reject_job->settings = array('action' => 'reject');
  97. $reject_job->requestTranslation();
  98. // Still rejected.
  99. $this->assertTrue($reject_job->isRejected());
  100. $messages = $reject_job->getMessages();
  101. $last_message = end($messages);
  102. $this->assertEqual('This is not supported.', $last_message->message);
  103. $this->assertEqual('error', $last_message->type);
  104. // A failing job.
  105. $failing_job = $this->createJob();
  106. $failing_job->settings = array('action' => 'fail');
  107. $failing_job->requestTranslation();
  108. // Still new.
  109. $this->assertTrue($failing_job->isUnprocessed());
  110. $messages = $failing_job->getMessages();
  111. $last_message = end($messages);
  112. $this->assertEqual('Service not reachable.', $last_message->message);
  113. $this->assertEqual('error', $last_message->type);
  114. }
  115. /**
  116. * Tests remote languages mappings support in the tmgmt core.
  117. */
  118. function testRemoteLanguagesMappings() {
  119. $this->loginAsAdmin();
  120. $this->setEnvironment('de');
  121. $controller = $this->default_translator->getController();
  122. $mappings = $controller->getRemoteLanguagesMappings($this->default_translator);
  123. $this->assertEqual($mappings, array(
  124. 'en' => 'en-us',
  125. 'de' => 'de-ch',
  126. ));
  127. $this->assertEqual($controller->mapToRemoteLanguage($this->default_translator, 'en'), 'en-us');
  128. $this->assertEqual($controller->mapToRemoteLanguage($this->default_translator, 'de'), 'de-ch');
  129. $this->assertEqual($controller->mapToLocalLanguage($this->default_translator, 'en-us'), 'en');
  130. $this->assertEqual($controller->mapToLocalLanguage($this->default_translator, 'de-ch'), 'de');
  131. $this->default_translator->settings['remote_languages_mappings']['de'] = 'de-de';
  132. $this->default_translator->settings['remote_languages_mappings']['en'] = 'en-uk';
  133. $this->default_translator->save();
  134. $this->assertEqual($controller->mapToRemoteLanguage($this->default_translator, 'en'), 'en-uk');
  135. $this->assertEqual($controller->mapToRemoteLanguage($this->default_translator, 'de'), 'de-de');
  136. // Test the fallback.
  137. $info = &drupal_static('_tmgmt_plugin_info');
  138. $info['translator']['test_translator']['map remote languages'] = FALSE;
  139. $this->assertEqual($controller->mapToRemoteLanguage($this->default_translator, 'en'), 'en');
  140. $this->assertEqual($controller->mapToRemoteLanguage($this->default_translator, 'de'), 'de');
  141. }
  142. /**
  143. * Tests escaping and unescaping text.
  144. */
  145. function testEscaping() {
  146. $controller = $this->default_translator->getController();
  147. $tests = array(
  148. array(
  149. 'item' => array('#text' => 'no escaping'),
  150. 'expected' => 'no escaping',
  151. ),
  152. array(
  153. 'item' => array(
  154. '#text' => 'single placeholder',
  155. '#escape' => array(
  156. 7 => array('string' => 'placeholder'),
  157. ),
  158. ),
  159. 'expected' => 'single [[[placeholder]]]',
  160. ),
  161. array(
  162. 'item' => array(
  163. '#text' => 'two placeholder, the second placeholder',
  164. '#escape' => array(
  165. 4 => array('string' => 'placeholder'),
  166. 28 => array('string' => 'placeholder'),
  167. ),
  168. ),
  169. 'expected' => 'two [[[placeholder]]], the second [[[placeholder]]]',
  170. ),
  171. array(
  172. 'item' => array(
  173. '#text' => 'something, something else',
  174. '#escape' => array(
  175. 0 => array('string' => 'something'),
  176. 21 => array('string' => 'else'),
  177. ),
  178. ),
  179. 'expected' => '[[[something]]], something [[[else]]]',
  180. ),
  181. array(
  182. 'item' => array(
  183. '#text' => 'something, something else',
  184. '#escape' => array(
  185. 21 => array('string' => 'else'),
  186. 11 => array('string' => 'something'),
  187. ),
  188. ),
  189. 'expected' => 'something, [[[something]]] [[[else]]]',
  190. ),
  191. );
  192. foreach ($tests as $test) {
  193. $escaped = $controller->escapeText($test['item']);
  194. // Assert that the string was escaped as expected.
  195. $this->assertEqual($escaped, $test['expected']);
  196. // Assert that the string is the same as the original when unescaped.
  197. $this->assertEqual($controller->unescapeText($escaped), $test['item']['#text']);
  198. }
  199. }
  200. }