entity_translation_upgrade.test 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * @file
  4. * Tests for Entity Translation module.
  5. */
  6. /**
  7. * Tests for the upgrade translation process.
  8. */
  9. class EntityTranslationUpgradeTestCase extends EntityTranslationTestCase {
  10. /**
  11. * Return the test information.
  12. */
  13. public static function getInfo() {
  14. return array(
  15. 'name' => 'Translation upgrade',
  16. 'description' => 'Tests for the upgrade from Content Translation to Entity Translation.',
  17. 'group' => 'Entity translation',
  18. 'dependencies' => array(),
  19. );
  20. }
  21. function setUp() {
  22. parent::setUp('locale', 'translation', 'translation_test', 'entity_translation', 'entity_translation_upgrade');
  23. $this->getAdminUser(array(
  24. 'toggle field translatability',
  25. 'administer entity translation',
  26. ));
  27. $this->getTranslatorUser(array(
  28. 'translate content',
  29. ));
  30. $this->login($this->getAdminUser());
  31. $this->addLanguage('en');
  32. $this->addLanguage('es');
  33. $this->configureContentTypeForContentTranslation();
  34. $this->enableUrlLanguageDetection();
  35. $this->login($this->getTranslatorUser());
  36. }
  37. /**
  38. * Configure the "Basic page" content type for entity translation tests.
  39. */
  40. public function configureContentTypeForContentTranslation() {
  41. // Configure the "Basic page" content type to use multilingual support with
  42. // content translation.
  43. $edit = array();
  44. $edit['language_content_type'] = TRANSLATION_ENABLED;
  45. $this->drupalPost('admin/structure/types/manage/page', $edit, t('Save content type'));
  46. $this->assertRaw(t('The content type %type has been updated.', array('%type' => 'Basic page')), t('Basic page content type has been updated.'));
  47. }
  48. /**
  49. * Toggle body field's translatability.
  50. */
  51. public function makeBodyFieldTranslatable() {
  52. $edit = array();
  53. $this->drupalGet('admin/structure/types/manage/page/fields/body');
  54. $this->clickLink('Enable translation');
  55. $this->drupalPost(NULL, array(), t('Confirm'));
  56. $this->assertRaw(t('Data successfully processed.'), t('Body field have been made translatable.'));
  57. }
  58. /**
  59. * @see TranslationTestCase::createPage
  60. */
  61. function createContentTranslationPage($title, $body, $language = NULL) {
  62. $edit = array();
  63. $langcode = LANGUAGE_NONE;
  64. $edit["title"] = $title;
  65. $edit["body[$langcode][0][value]"] = $body;
  66. if (!empty($language)) {
  67. $edit['language'] = $language;
  68. }
  69. $this->drupalPost('node/add/page', $edit, t('Save'));
  70. $this->assertRaw(t('Basic page %title has been created.', array('%title' => $title)), 'Basic page created.');
  71. // Check to make sure the node was created.
  72. $node = $this->drupalGetNodeByTitle($title);
  73. $this->assertTrue($node, 'Node found in database.');
  74. return $node;
  75. }
  76. /**
  77. * @see TranslationTestCase::createTranslation
  78. */
  79. function createContentTranslationTranslation($node, $title, $body, $language) {
  80. $this->drupalGet('node/add/page', array('query' => array('translation' => $node->nid, 'target' => $language)));
  81. $langcode = LANGUAGE_NONE;
  82. $body_key = "body[$langcode][0][value]";
  83. $this->assertFieldByXPath('//input[@id="edit-title"]', $node->title, "Original title value correctly populated.");
  84. $this->assertFieldByXPath("//textarea[@name='$body_key']", $node->body[LANGUAGE_NONE][0]['value'], "Original body value correctly populated.");
  85. $edit = array();
  86. $edit["title"] = $title;
  87. $edit[$body_key] = $body;
  88. $this->drupalPost(NULL, $edit, t('Save'));
  89. $this->assertRaw(t('Basic page %title has been created.', array('%title' => $title)), 'Translation created.');
  90. // Check to make sure that translation was successful.
  91. $translation = $this->drupalGetNodeByTitle($title);
  92. $this->assertTrue($translation, 'Node found in database.');
  93. $this->assertTrue($translation->tnid == $node->nid, 'Translation set id correctly stored.');
  94. return $translation;
  95. }
  96. /**
  97. * Tests copying of source node's body value in the add translation form page.
  98. */
  99. public function testUpgradeContentToEntityTranslation() {
  100. // Create Basic page in English.
  101. $node_title = $this->randomName();
  102. $node_body = $this->randomName();
  103. $node = $this->createContentTranslationPage($node_title, $node_body, 'en');
  104. // Submit translation in Spanish.
  105. $this->drupalGet('node/' . $node->nid . '/translate');
  106. $node_translation_title = $this->randomName();
  107. $node_translation_body = $this->randomName();
  108. $node_translation = $this->createContentTranslationTranslation($node, $node_translation_title, $node_translation_body, 'es');
  109. // Make Body field translatable before we run the upgrade.
  110. $this->login($this->getAdminUser());
  111. $this->makeBodyFieldTranslatable();
  112. // Run the upgrade for all Page nodes.
  113. $edit = array(
  114. 'types[page]' => 'page',
  115. );
  116. $this->drupalPost('admin/config/regional/entity_translation', $edit, t('Upgrade'));
  117. // Switch to our translator user.
  118. $this->login($this->getTranslatorUser());
  119. // Check that the unpublished target node triggers a redirect.
  120. $this->drupalGet('node/' . $node_translation->nid);
  121. $headers = $this->drupalGetHeaders(TRUE);
  122. list(, $status) = explode(' ', $headers[0][':status'], 3);
  123. $this->assertEqual($status, 301, 'Expected response code was sent.');
  124. $languages = language_list();
  125. $this->assertEqual($this->getUrl(), url('node/' . $node->nid, array('absolute' => TRUE, 'language' => $languages['es'])), 'entity_translation_upgrade_redirect() redirected to expected URL.');
  126. // Check that the body is displayed when the active language is English.
  127. $this->drupalGet('node/' . $node->nid);
  128. $this->assertRaw($node_body, t('Body field displayed correctly in the source language.'));
  129. // Check that the translated body is displayed when the active language is Spanish.
  130. $this->drupalGet('es/node/' . $node->nid);
  131. $this->assertRaw($node_translation_body, t('Body field displayed correctly in the target language.'));
  132. // Check that the edit forms are initialized correctly in the target language.
  133. $this->drupalGet('node/' . $node->nid . '/edit');
  134. $this->assertFieldByXPath("//textarea[@name='body[en][0][value]']", $node_body, "Body field correctly instantiated with the value of the source language.");
  135. $this->drupalGet('es/node/' . $node->nid . '/edit');
  136. $this->assertFieldByXPath("//textarea[@name='body[es][0][value]']", $node_translation_body, "Body field correctly instantiated with the value of the target language.");
  137. }
  138. }