tmgmt_entity.source.test 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /**
  3. * Basic Entity Source tests.
  4. */
  5. class TMGMTEntitySourceTestCase extends TMGMTEntityTestCaseUtility {
  6. public $vocabulary;
  7. static function getInfo() {
  8. return array(
  9. 'name' => 'Entity Source tests',
  10. 'description' => 'Exporting source data from entities and saving translations back to entities.',
  11. 'group' => 'Translation Management',
  12. 'dependencies' => array('entity_translation'),
  13. );
  14. }
  15. function setUp() {
  16. parent::setUp(array('tmgmt_entity', 'taxonomy', 'entity_translation'));
  17. // Admin user to perform settings on setup.
  18. $this->loginAsAdmin(array('administer entity translation'));
  19. $this->vocabulary = $this->createTaxonomyVocab(strtolower($this->randomName()), $this->randomName(), array(FALSE, TRUE, TRUE, TRUE));
  20. // Enable entity translations for taxonomy.
  21. $edit['entity_translation_entity_types[taxonomy_term]'] = 1;
  22. $this->drupalPost('admin/config/regional/entity_translation', $edit, t('Save configuration'));
  23. }
  24. /**
  25. * Tests nodes field translation.
  26. */
  27. function testEntitySourceNode() {
  28. $this->setEnvironment('de');
  29. $this->createNodeType('article', 'Article', ENTITY_TRANSLATION_ENABLED);
  30. // Create a translation job.
  31. $job = $this->createJob();
  32. $job->translator = $this->default_translator->name;
  33. $job->settings = array();
  34. $job->save();
  35. // Create some nodes.
  36. for ($i = 1; $i <= 5; $i++) {
  37. $node = $this->createNode('article');
  38. // Create a job item for this node and add it to the job.
  39. $item = $job->addItem('entity', 'node', $node->nid);
  40. $this->assertEqual(t('@type (@bundle)', array('@type' => t('Node'), '@bundle' => 'Article')), $item->getSourceType());
  41. }
  42. // Translate the job.
  43. $job->requestTranslation();
  44. // Check the translated job items.
  45. foreach ($job->getItems() as $item) {
  46. // The source is available only for en.
  47. $this->assertJobItemLangCodes($item, 'en', array('en'));
  48. $item->acceptTranslation();
  49. $this->assertTrue($item->isAccepted());
  50. $entity = entity_load_single($item->item_type, $item->item_id);
  51. $data = $item->getData();
  52. $this->checkTranslatedData($entity, $data, 'de');
  53. $this->checkUntranslatedData($entity, $this->field_names['node']['article'], $data, 'de');
  54. // The source is now available for both en and de.
  55. $this->assertJobItemLangCodes($item, 'en', array('de', 'en'));
  56. }
  57. }
  58. /**
  59. * Tests taxonomy terms field translation.
  60. */
  61. function testEntitySourceTerm() {
  62. $this->setEnvironment('de');
  63. // Create the job.
  64. $job = $this->createJob();
  65. $job->translator = $this->default_translator->name;
  66. $job->settings = array();
  67. $job->save();
  68. $term = NULL;
  69. //Create some terms.
  70. for ($i = 1; $i <= 5; $i++) {
  71. $term = $this->createTaxonomyTerm($this->vocabulary);
  72. // Create the item and assign it to the job.
  73. $item = $job->addItem('entity', 'taxonomy_term', $term->tid);
  74. $this->assertEqual(t('@type (@bundle)', array('@type' => t('Taxonomy term'), '@bundle' => $this->vocabulary->name)), $item->getSourceType());
  75. }
  76. // Request the translation and accept it.
  77. $job->requestTranslation();
  78. // Check if the fields were translated.
  79. foreach ($job->getItems() as $item) {
  80. $this->assertJobItemLangCodes($item, 'en', array('en'));
  81. $item->acceptTranslation();
  82. $entity = entity_load_single($item->item_type, $item->item_id);
  83. $data = $item->getData();
  84. $this->checkTranslatedData($entity, $data, 'de');
  85. $this->checkUntranslatedData($entity, $this->field_names['taxonomy_term'][$this->vocabulary->machine_name], $data, 'de');
  86. $this->assertJobItemLangCodes($item, 'en', array('de', 'en'));
  87. }
  88. }
  89. function testAddingJobItemsWithEmptySourceText() {
  90. $this->setEnvironment('de');
  91. // Create term with empty texts.
  92. $empty_term = new stdClass();
  93. $empty_term->name = $this->randomName();
  94. $empty_term->description = $this->randomName();
  95. $empty_term->vid = $this->vocabulary->vid;
  96. taxonomy_term_save($empty_term);
  97. // Create the job.
  98. $job = tmgmt_job_create('en', NULL);
  99. try {
  100. $job->addItem('entity', 'taxonomy_term', $empty_term->tid);
  101. $this->fail('Job item added with empty source text.');
  102. }
  103. catch (TMGMTException $e) {
  104. $this->assert(empty($job->tjid), 'After adding a job item with empty source text its tjid has to be unset.');
  105. }
  106. // Create term with populated source content.
  107. $populated_content_term = $this->createTaxonomyTerm($this->vocabulary);
  108. // Lets reuse the last created term with populated source content.
  109. $job->addItem('entity', 'taxonomy_term', $populated_content_term->tid);
  110. $this->assert(!empty($job->tjid), 'After adding another job item with populated source text its tjid must be set.');
  111. }
  112. /**
  113. * Test if the source is able to pull content in requested language.
  114. */
  115. function testRequestDataForSpecificLanguage() {
  116. $this->setEnvironment('de');
  117. $this->setEnvironment('cs');
  118. $this->createNodeType('article', 'Article', ENTITY_TRANSLATION_ENABLED);
  119. // Create a translation job.
  120. $job = $this->createJob('en', 'de');
  121. $job->translator = $this->default_translator->name;
  122. $job->settings = array();
  123. $job->save();
  124. $node = $this->createNode('article', 'cs');
  125. $node->body['en'][0]['value'] = 'en translation';
  126. node_save($node);
  127. $job->addItem('entity', 'node', $node->nid);
  128. $data = $job->getData();
  129. $this->assertEqual($data[1]['body'][0]['value']['#text'], 'en translation');
  130. }
  131. /**
  132. * Compares the data from an entity with the translated data.
  133. *
  134. * @param $tentity
  135. * The translated entity object.
  136. * @param $data
  137. * An array with the translated data.
  138. * @param $langcode
  139. * The code of the target language.
  140. */
  141. function checkTranslatedData($tentity, $data, $langcode) {
  142. foreach (element_children($data) as $field_name) {
  143. foreach (element_children($data[$field_name]) as $delta) {
  144. foreach (element_children($data[$field_name][$delta]) as $column) {
  145. $column_value = $data[$field_name][$delta][$column];
  146. if (!empty($column_value['#translate'])) {
  147. $this->assertEqual($tentity->{$field_name}[$langcode][$delta][$column], $column_value['#translation']['#text'], format_string('The field %field:%delta has been populated with the proper translated data.', array('%field' => $field_name, 'delta' => $delta)));
  148. }
  149. else {
  150. $this->assertEqual($tentity->{$field_name}[$langcode][$delta][$column], $column_value['#text'], format_string('The field %field:%delta has been populated with the proper untranslated data.', array('%field' => $field_name, 'delta' => $delta)));
  151. }
  152. }
  153. }
  154. }
  155. }
  156. /**
  157. * Checks the fields that should not be translated.
  158. *
  159. * @param $tentity
  160. * The translated entity object.
  161. * @param $fields
  162. * An array with the field names to check.
  163. * @param $translation
  164. * An array with the translated data.
  165. * @param $langcode
  166. * The code of the target language.
  167. */
  168. function checkUntranslatedData($tentity, $fields, $data, $langcode) {
  169. foreach ($fields as $field_name) {
  170. $field_info = field_info_field($field_name);
  171. if (!$field_info['translatable']) {
  172. // Avoid some PHP warnings.
  173. if (isset($data[$field_name])) {
  174. $this->assertNull($data[$field_name]['#translation']['#text'], 'The not translatable field was not translated.');
  175. }
  176. if (isset($tentity->{$field_name}[$langcode])) {
  177. $this->assertNull($tentity->{$field_name}[$langcode], 'The entity has translated data in a field that is translatable.');
  178. }
  179. }
  180. }
  181. }
  182. }