entity_translation_i18n_menu.test 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. * Asserts a link with a given label and title is found in the page.
  110. */
  111. function assertLinkWithTitleAttribute($label, $title) {
  112. $links = $this->xpath('//a[normalize-space(text())=:label and normalize-space(@title)=:title]', array(
  113. ':label' => $label,
  114. ':title' => $title,
  115. ));
  116. $this->assert(isset($links[0]));
  117. }
  118. /**
  119. * Test if menu localization works.
  120. */
  121. function testMenuLocalization() {
  122. // Create Basic page in English.
  123. $link_title_en = $this->randomName();
  124. $link_description_en = $this->randomName() . ' & htmlentity';
  125. $node = $this->createPage($link_title_en, $link_description_en, 'en');
  126. // Submit translation in Spanish.
  127. $link_title_es = $this->randomName();
  128. $link_description_es = $this->randomName() . ' & htmlentity';
  129. $node_translation = $this->createTranslation($node, $link_title_es, $link_description_es, 'es');
  130. // Check menu links in both languages.
  131. $this->get('en', '<front>');
  132. $this->assertLinkWithTitleAttribute($link_title_en, $link_description_en);
  133. $this->get('es', '<front>');
  134. $this->assertLinkWithTitleAttribute($link_title_es, $link_description_es);
  135. // Edit English menu link.
  136. $link_title_en2 = $this->randomName();
  137. $this->editPage($node, $link_title_en, $link_title_en2, 'en');
  138. // Check that Spanish menu link has not changed.
  139. $this->get('es', '<front>');
  140. $this->assertLinkWithTitleAttribute($link_title_es, $link_description_es);
  141. // Edit Spanish menu link.
  142. $link_title_es2 = $this->randomName();
  143. $this->editPage($node, $link_title_es, $link_title_es2, 'es');
  144. // Check that English menu link has changed.
  145. $this->get('en', '<front>');
  146. $this->assertLinkWithTitleAttribute($link_title_en2, $link_description_en);
  147. // Check that Spanish menu link has changed.
  148. $this->get('es', '<front>');
  149. $this->assertLinkWithTitleAttribute($link_title_es2, $link_description_es);
  150. // Delete Spanish translation and check that the respective menu item has
  151. // been deleted as well.
  152. $this->removeTranslation($node, 'es');
  153. $this->get('es', '<front>');
  154. $this->assertNoText($link_title_es2);
  155. }
  156. /**
  157. * Test if menu localization works (source language != default language).
  158. */
  159. function testMenuLocalizationCustomSourceLanguage() {
  160. // Create Basic page in Spanish.
  161. $link_title_es = $this->randomName();
  162. $node = $this->createPage($link_title_es, NULL, 'es');
  163. // Submit translation in English.
  164. $link_title_en = $this->randomName();
  165. $node_translation = $this->createTranslation($node, $link_title_en, NULL, 'en');
  166. // Check menu links in both languages.
  167. $this->get('es', '<front>');
  168. $this->assertText($link_title_es);
  169. $this->get('en', '<front>');
  170. $this->assertText($link_title_en);
  171. // Edit Spanish menu link.
  172. $link_title_es2 = $this->randomName();
  173. $this->editPage($node, $link_title_es, $link_title_es2, 'es');
  174. // Check that English menu link has not changed.
  175. $this->get('en', '<front>');
  176. $this->assertText($link_title_en);
  177. // Edit English menu link.
  178. $link_title_en2 = $this->randomName();
  179. $this->editPage($node, $link_title_en, $link_title_en2, 'en');
  180. // Check that Spanish menu link has not changed.
  181. $this->get('es', '<front>');
  182. $this->assertText($link_title_es2);
  183. }
  184. /**
  185. * Test if menu translation works with separate menu items.
  186. */
  187. function testMenuTranslation() {
  188. // Create Basic page in English.
  189. $link_title_en = $this->randomName();
  190. $node = $this->createPage($link_title_en, NULL, 'en');
  191. // Submit translation in Spanish.
  192. $link_title_es = $this->randomName();
  193. $node_translation = $this->createTranslation($node, $link_title_es, NULL, 'es', TRUE);
  194. // Check menu links in both languages.
  195. $this->get('en', '<front>');
  196. $this->assertText($link_title_en);
  197. $this->get('es', '<front>');
  198. $this->assertText($link_title_es);
  199. // Edit English menu link.
  200. $link_title_en2 = $this->randomName();
  201. $this->editPage($node, $link_title_en, $link_title_en2, 'en');
  202. // Check that Spanish menu link has not changed.
  203. $this->get('es', '<front>');
  204. $this->assertText($link_title_es);
  205. // Edit Spanish menu link.
  206. $link_title_es2 = $this->randomName();
  207. $this->editPage($node, $link_title_es, $link_title_es2, 'es');
  208. // Check that English menu link has not changed.
  209. $this->get('en', '<front>');
  210. $this->assertText($link_title_en2);
  211. // Add another translation, and check that other menu items are not
  212. // affected. See https://drupal.org/node/1982140
  213. $link_title_it = $this->randomName();
  214. $node_translation = $this->createTranslation($node, $link_title_it, NULL, 'it', NULL);
  215. // Check that Spanish and English menu links have not changed.
  216. $this->get('es', '<front>');
  217. $this->assertText($link_title_es2);
  218. $this->get('en', '<front>');
  219. $this->assertText($link_title_en2);
  220. // Delete Spanish translation and check that the respective menu item has
  221. // been deleted as well.
  222. $this->removeTranslation($node, 'es');
  223. $this->get('es', '<front>');
  224. $this->assertNoText($link_title_es2);
  225. }
  226. }