entity_translation_i18n_menu.test 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. /**
  3. * @file
  4. * Tests for Entity translation module.
  5. */
  6. /**
  7. * Tests for the translation of menu items on entity forms.
  8. */
  9. class EntityTranslationMenuTranslationTestCase extends EntityTranslationTestCase {
  10. /**
  11. * Return the test information.
  12. */
  13. public static function getInfo() {
  14. return array(
  15. 'name' => 'Menu translation',
  16. 'description' => 'Tests for the translation of menu items on entity forms.',
  17. 'group' => 'Entity translation',
  18. 'dependencies' => array('i18n_menu'),
  19. );
  20. }
  21. function setUp() {
  22. parent::setUp('locale', 'entity_translation', 'i18n_menu', 'entity_translation_i18n_menu');
  23. $this->login($this->getAdminUser(array('administer menu')));
  24. $this->addLanguage('en');
  25. $this->addLanguage('es');
  26. $this->addLanguage('it');
  27. $this->configureContentType();
  28. $this->configureMenu();
  29. $this->enableUrlLanguageDetection();
  30. $this->login($this->getTranslatorUser(array('administer menu')));
  31. }
  32. /**
  33. * Configure the "Main Menu" for multilingual menu items ("Translate & Localize").
  34. */
  35. function configureMenu() {
  36. $edit = array();
  37. $edit['i18n_mode'] = I18N_MODE_MULTIPLE; // Translate & Localize.
  38. $this->drupalPost('admin/structure/menu/manage/main-menu/edit', $edit, t('Save'));
  39. $this->assertRaw(t('Your configuration has been saved.'), t('Menu settings have been saved.'));
  40. }
  41. /**
  42. * Create page with menu item.
  43. */
  44. function createPage($link_title, $description, $langcode) {
  45. $edit = array();
  46. $edit['title'] = $this->randomName();
  47. $edit['language'] = $langcode;
  48. $edit['menu[enabled]'] = TRUE;
  49. $edit['menu[link_title]'] = $link_title;
  50. $edit['menu[description]'] = $description ? $description : 'link language = ' . $langcode;
  51. $this->drupalPost('node/add/page', $edit, t('Save'));
  52. $this->assertRaw(t('Basic page %title has been created.', array('%title' => $edit['title'])), t('Basic page created.'));
  53. // Check to make sure the node was created.
  54. $node = $this->drupalGetNodeByTitle($edit['title']);
  55. $this->assertTrue($node, t('Node found in database.'));
  56. // Check to make sure menu link was created.
  57. $this->get($langcode, '<front>');
  58. $this->assertText($link_title, 'New menu link found.');
  59. return $node;
  60. }
  61. /**
  62. * Create a translation with menu item.
  63. */
  64. function createTranslation($node, $link_title, $description, $langcode, $use_tsets = FALSE) {
  65. $this->drupalGet('node/' . $node->nid . '/edit/add/' . $node->language . '/' . $langcode);
  66. $edit = array();
  67. $edit['menu[enabled]'] = TRUE;
  68. $edit['menu[link_title]'] = $link_title;
  69. $edit['menu[description]'] = $description ? $description : 'link language = ' . $langcode;
  70. if ($use_tsets != NULL) {
  71. $edit['menu[tset]'] = $use_tsets;
  72. }
  73. $this->drupalPost(NULL, $edit, t('Save'));
  74. $this->drupalGet('node/' . $node->nid . '/translate');
  75. $this->assertLinkByHref('node/' . $node->nid . '/edit/' . $langcode, 0, t('Translation edit link found. Translation created.'));
  76. // Check to make sure menu link was created.
  77. $this->get($langcode, '<front>');
  78. $this->assertText($link_title, 'Translation menu link found.');
  79. return $node;
  80. }
  81. /**
  82. * Remove translation in given langcode from node.
  83. */
  84. function removeTranslation($node, $langcode) {
  85. $this->drupalGet('node/' . $node->nid . '/translate/delete/' . $langcode);
  86. $this->drupalPost(NULL, array(), t('Delete'));
  87. // Check to make sure translation was deleted.
  88. $this->drupalGet('node/' . $node->nid . '/edit/add/' . $node->language . '/' . $langcode);
  89. $this->assertResponse(200, 'Translation add page found. Old translation deleted.');
  90. }
  91. /**
  92. * Edit a page menu item.
  93. *
  94. * Check that node form contains old menu link title, then replace with given
  95. * new title.
  96. */
  97. function editPage($node, $old_link_title, $link_title, $langcode) {
  98. $this->drupalGet('node/' . $node->nid . '/edit/' . $langcode);
  99. $this->assertFieldByXPath("//input[@name='menu[link_title]']", $old_link_title, 'Old link title value correctly populated: ' . $old_link_title);
  100. $edit = array();
  101. $edit['menu[link_title]'] = $link_title;
  102. $this->drupalPost(NULL, $edit, t('Save'));
  103. // Check to make sure menu link was updated.
  104. $this->get($langcode, '<front>');
  105. $this->assertNoText($old_link_title, 'Old menu link title not found: ' . $old_link_title);
  106. $this->assertText($link_title, 'New menu link title found: ' . $link_title);
  107. }
  108. /**
  109. * Test if menu localization works.
  110. */
  111. function testMenuLocalization() {
  112. // Create Basic page in English.
  113. $link_title_en = $this->randomName();
  114. $node = $this->createPage($link_title_en, NULL, 'en');
  115. // Submit translation in Spanish.
  116. $link_title_es = $this->randomName();
  117. $node_translation = $this->createTranslation($node, $link_title_es, NULL, 'es');
  118. // Check menu links in both languages.
  119. $this->get('en', '<front>');
  120. $this->assertText($link_title_en);
  121. $this->get('es', '<front>');
  122. $this->assertText($link_title_es);
  123. // Edit English menu link.
  124. $link_title_en2 = $this->randomName();
  125. $this->editPage($node, $link_title_en, $link_title_en2, 'en');
  126. // Check that Spanish menu link has not changed.
  127. $this->get('es', '<front>');
  128. $this->assertText($link_title_es);
  129. // Edit Spanish menu link.
  130. $link_title_es2 = $this->randomName();
  131. $this->editPage($node, $link_title_es, $link_title_es2, 'es');
  132. // Check that English menu link has not changed.
  133. $this->get('en', '<front>');
  134. $this->assertText($link_title_en2);
  135. // Delete Spanish translation and check that the respective menu item has
  136. // been deleted as well.
  137. $this->removeTranslation($node, 'es');
  138. $this->get('es', '<front>');
  139. $this->assertNoText($link_title_es2);
  140. }
  141. /**
  142. * Test if menu localization works (source language != default language).
  143. */
  144. function testMenuLocalizationCustomSourceLanguage() {
  145. // Create Basic page in Spanish.
  146. $link_title_es = $this->randomName();
  147. $node = $this->createPage($link_title_es, NULL, 'es');
  148. // Submit translation in English.
  149. $link_title_en = $this->randomName();
  150. $node_translation = $this->createTranslation($node, $link_title_en, NULL, 'en');
  151. // Check menu links in both languages.
  152. $this->get('es', '<front>');
  153. $this->assertText($link_title_es);
  154. $this->get('en', '<front>');
  155. $this->assertText($link_title_en);
  156. // Edit Spanish menu link.
  157. $link_title_es2 = $this->randomName();
  158. $this->editPage($node, $link_title_es, $link_title_es2, 'es');
  159. // Check that English menu link has not changed.
  160. $this->get('en', '<front>');
  161. $this->assertText($link_title_en);
  162. // Edit English menu link.
  163. $link_title_en2 = $this->randomName();
  164. $this->editPage($node, $link_title_en, $link_title_en2, 'en');
  165. // Check that Spanish menu link has not changed.
  166. $this->get('es', '<front>');
  167. $this->assertText($link_title_es2);
  168. }
  169. /**
  170. * Test if menu translation works with separate menu items.
  171. */
  172. function testMenuTranslation() {
  173. // Create Basic page in English.
  174. $link_title_en = $this->randomName();
  175. $node = $this->createPage($link_title_en, NULL, 'en');
  176. // Submit translation in Spanish.
  177. $link_title_es = $this->randomName();
  178. $node_translation = $this->createTranslation($node, $link_title_es, NULL, 'es', TRUE);
  179. // Check menu links in both languages.
  180. $this->get('en', '<front>');
  181. $this->assertText($link_title_en);
  182. $this->get('es', '<front>');
  183. $this->assertText($link_title_es);
  184. // Edit English menu link.
  185. $link_title_en2 = $this->randomName();
  186. $this->editPage($node, $link_title_en, $link_title_en2, 'en');
  187. // Check that Spanish menu link has not changed.
  188. $this->get('es', '<front>');
  189. $this->assertText($link_title_es);
  190. // Edit Spanish menu link.
  191. $link_title_es2 = $this->randomName();
  192. $this->editPage($node, $link_title_es, $link_title_es2, 'es');
  193. // Check that English menu link has not changed.
  194. $this->get('en', '<front>');
  195. $this->assertText($link_title_en2);
  196. // Add another translation, and check that other menu items are not
  197. // affected. See https://drupal.org/node/1982140
  198. $link_title_it = $this->randomName();
  199. $node_translation = $this->createTranslation($node, $link_title_it, NULL, 'it', NULL);
  200. // Check that Spanish and English menu links have not changed.
  201. $this->get('es', '<front>');
  202. $this->assertText($link_title_es2);
  203. $this->get('en', '<front>');
  204. $this->assertText($link_title_en2);
  205. // Delete Spanish translation and check that the respective menu item has
  206. // been deleted as well.
  207. $this->removeTranslation($node, 'es');
  208. $this->get('es', '<front>');
  209. $this->assertNoText($link_title_es2);
  210. }
  211. }