tmgmt_node.test 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * Basic Node Source tests.
  4. */
  5. class TMGMTNodeSourceTestCase extends TMGMTEntityTestCaseUtility {
  6. static function getInfo() {
  7. return array(
  8. 'name' => 'Node Source tests',
  9. 'description' => 'Exporting source data from nodes and saving translations back to nodes',
  10. 'group' => 'Translation Management',
  11. );
  12. }
  13. function setUp() {
  14. parent::setUp(array('tmgmt_node', 'translation'));
  15. $this->loginAsAdmin();
  16. $this->setEnvironment('de');
  17. $this->createNodeType('page', 'Basic page', TRANSLATION_ENABLED, FALSE);
  18. $this->attachFields('node', 'page', array(TRUE, TRUE, FALSE, FALSE));
  19. }
  20. /**
  21. * Tests nodes field translation.
  22. */
  23. function testNodeSource() {
  24. // Create a translation job.
  25. $job = $this->createJob();
  26. $job->translator = $this->default_translator->name;
  27. $job->settings = array();
  28. $job->save();
  29. for ($i = 0; $i < 2; $i++) {
  30. $node = $this->createNode('page');
  31. // Create a job item for this node and add it to the job.
  32. $item = $job->addItem('node', 'node', $node->nid);
  33. $this->assertEqual('Basic page', $item->getSourceType());
  34. }
  35. // Translate the job.
  36. $job->requestTranslation();
  37. foreach ($job->getItems() as $item) {
  38. // The source is only available in en.
  39. $this->assertJobItemLangCodes($item, 'en', array('en'));
  40. $item->acceptTranslation();
  41. $node = node_load($item->item_id);
  42. // Check if the tnid attribute is bigger than 0.
  43. $this->assertTrue($node->tnid > 0, 'The source node is part of a translation set.');
  44. // The translations may be statically cached, so make make sure
  45. // to reset the cache before loading the node translations.
  46. $cached_translations = & drupal_static('translation_node_get_translations', array());
  47. unset($cached_translations[$node->tnid]);
  48. // Load the translation set of the source node.
  49. $translations = translation_node_get_translations($node->tnid);
  50. $this->assertNotNull($translations['de'], 'Translation found for the source node.');
  51. if (isset($translations['de'])) {
  52. $tnode = node_load($translations['de']->nid, NULL, TRUE);
  53. $this->checkTranslatedData($tnode, $item->getData(), 'de');
  54. }
  55. // The source should be now available for en and de.
  56. $this->assertJobItemLangCodes($item, 'en', array('de', 'en'));
  57. }
  58. }
  59. /**
  60. * Test if the source is able to pull content in requested language.
  61. */
  62. function testRequestDataForSpecificLanguage() {
  63. $this->setEnvironment('sk');
  64. $this->setEnvironment('es');
  65. $content_type = $this->drupalCreateContentType();
  66. $node = $this->drupalCreateNode(array(
  67. 'title' => $this->randomName(),
  68. 'language' => 'sk',
  69. 'body' => array('sk' => array(array())),
  70. 'type' => $content_type->type,
  71. ));
  72. $this->drupalCreateNode(array(
  73. 'title' => 'en translation',
  74. 'language' => 'en',
  75. 'tnid' => $node->nid,
  76. 'body' => array('en' => array(array())),
  77. 'type' => $content_type->type,
  78. ));
  79. // Create a translation job.
  80. $job = $this->createJob('en', 'de');
  81. $job->save();
  82. $job->addItem('node', 'node', $node->nid);
  83. $data = $job->getData();
  84. $this->assertEqual($data[1]['node_title']['#text'], 'en translation');
  85. // Create new job item with a source language for which the translation
  86. // does not exit.
  87. $job = $this->createJob('es', 'cs');
  88. $job->save();
  89. try {
  90. $job->addItem('node', 'node', $node->nid);
  91. $this->fail('The job item should not be added as there is no translation for language "es"');
  92. }
  93. catch (TMGMTException $e) {
  94. $languages = language_list();
  95. $this->assertEqual(t('Unable to load %language translation for the node %title',
  96. array('%language' => $languages['es']->name, '%title' => $node->title)), $e->getMessage());
  97. }
  98. }
  99. /**
  100. * Compares the data from an entity with the translated data.
  101. *
  102. * @param $node
  103. * The translated node object.
  104. * @param $data
  105. * An array with the translated data.
  106. * @param $langcode
  107. * The code of the target language.
  108. */
  109. function checkTranslatedData($node, $data, $langcode) {
  110. foreach (element_children($data) as $field_name) {
  111. if ($field_name == 'node_title') {
  112. $this->assertEqual($node->title, $data['node_title']['#translation']['#text'], 'The title of the translated node matches the translated data.');
  113. continue;
  114. }
  115. foreach (element_children($data[$field_name]) as $delta) {
  116. $field_langcode = field_is_translatable('node', field_info_field($field_name)) ? $langcode : LANGUAGE_NONE;
  117. foreach (element_children($data[$field_name][$delta]) as $column) {
  118. $column_value = $data[$field_name][$delta][$column];
  119. if (!isset($column_value['#translate']) || $column_value['#translate']) {
  120. $this->assertEqual($node->{$field_name}[$field_langcode][$delta][$column], $column_value['#translation']['#text'], format_string('The translatable field %field:%delta has been populated with the proper translated data.', array(
  121. '%field' => $field_name,
  122. 'delta' => $delta
  123. )));
  124. }
  125. }
  126. }
  127. }
  128. }
  129. }