translation.test 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. <?php
  2. /**
  3. * @file
  4. * Tests for the Translation module.
  5. */
  6. /**
  7. * Functional tests for the Translation module.
  8. */
  9. class TranslationTestCase extends DrupalWebTestCase {
  10. protected $book;
  11. protected $admin_user;
  12. protected $translator;
  13. public static function getInfo() {
  14. return array(
  15. 'name' => 'Translation functionality',
  16. 'description' => 'Create a basic page with translation, modify the page outdating translation, and update translation.',
  17. 'group' => 'Translation'
  18. );
  19. }
  20. function setUp() {
  21. parent::setUp('locale', 'translation', 'translation_test');
  22. // Setup users.
  23. $this->admin_user = $this->drupalCreateUser(array('bypass node access', 'administer nodes', 'administer languages', 'administer content types', 'administer blocks', 'access administration pages', 'translate content'));
  24. $this->translator = $this->drupalCreateUser(array('create page content', 'edit own page content', 'translate content'));
  25. $this->drupalLogin($this->admin_user);
  26. // Add languages.
  27. $this->addLanguage('en');
  28. $this->addLanguage('es');
  29. $this->addLanguage('it');
  30. // Disable Italian to test the translation behavior with disabled languages.
  31. $edit = array('enabled[it]' => FALSE);
  32. $this->drupalPost('admin/config/regional/language', $edit, t('Save configuration'));
  33. // Set "Basic page" content type to use multilingual support with
  34. // translation.
  35. $this->drupalGet('admin/structure/types/manage/page');
  36. $edit = array();
  37. $edit['language_content_type'] = 2;
  38. $this->drupalPost('admin/structure/types/manage/page', $edit, t('Save content type'));
  39. $this->assertRaw(t('The content type %type has been updated.', array('%type' => 'Basic page')), 'Basic page content type has been updated.');
  40. // Enable the language switcher block.
  41. $language_type = LANGUAGE_TYPE_INTERFACE;
  42. $edit = array("blocks[locale_$language_type][region]" => 'sidebar_first');
  43. $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
  44. // Enable URL language detection and selection to make the language switcher
  45. // block appear.
  46. $edit = array('language[enabled][locale-url]' => TRUE);
  47. $this->drupalPost('admin/config/regional/language/configure', $edit, t('Save settings'));
  48. $this->assertRaw(t('Language negotiation configuration saved.'), 'URL language detection enabled.');
  49. $this->resetCaches();
  50. $this->drupalLogin($this->translator);
  51. }
  52. /**
  53. * Creates, modifies, and updates a basic page with a translation.
  54. */
  55. function testContentTranslation() {
  56. // Create Basic page in English.
  57. $node_title = $this->randomName();
  58. $node_body = $this->randomName();
  59. $node = $this->createPage($node_title, $node_body, 'en');
  60. // Unpublish the original node to check that this has no impact on the
  61. // translation overview page, publish it again afterwards.
  62. $this->drupalLogin($this->admin_user);
  63. $this->drupalPost('node/' . $node->nid . '/edit', array('status' => FALSE), t('Save'));
  64. $this->drupalGet('node/' . $node->nid . '/translate');
  65. $this->drupalPost('node/' . $node->nid . '/edit', array('status' => NODE_PUBLISHED), t('Save'));
  66. $this->drupalLogin($this->translator);
  67. // Check that the "add translation" link uses a localized path.
  68. $languages = language_list();
  69. $this->drupalGet('node/' . $node->nid . '/translate');
  70. $this->assertLinkByHref($languages['es']->prefix . '/node/add/' . str_replace('_', '-', $node->type), 0, format_string('The "add translation" link for %language points to the localized path of the target language.', array('%language' => $languages['es']->name)));
  71. // Submit translation in Spanish.
  72. $node_translation_title = $this->randomName();
  73. $node_translation_body = $this->randomName();
  74. $node_translation = $this->createTranslation($node, $node_translation_title, $node_translation_body, 'es');
  75. // Check that the "edit translation" and "view node" links use localized
  76. // paths.
  77. $this->drupalGet('node/' . $node->nid . '/translate');
  78. $this->assertLinkByHref($languages['es']->prefix . '/node/' . $node_translation->nid . '/edit', 0, format_string('The "edit" link for the translation in %language points to the localized path of the translation language.', array('%language' => $languages['es']->name)));
  79. $this->assertLinkByHref($languages['es']->prefix . '/node/' . $node_translation->nid, 0, format_string('The "view" link for the translation in %language points to the localized path of the translation language.', array('%language' => $languages['es']->name)));
  80. // Attempt to submit a duplicate translation by visiting the node/add page
  81. // with identical query string.
  82. $this->drupalGet('node/add/page', array('query' => array('translation' => $node->nid, 'target' => 'es')));
  83. $this->assertRaw(t('A translation of %title in %language already exists', array('%title' => $node_title, '%language' => $languages['es']->name)), 'Message regarding attempted duplicate translation is displayed.');
  84. // Attempt a resubmission of the form - this emulates using the back button
  85. // to return to the page then resubmitting the form without a refresh.
  86. $edit = array();
  87. $langcode = LANGUAGE_NONE;
  88. $edit["title"] = $this->randomName();
  89. $edit["body[$langcode][0][value]"] = $this->randomName();
  90. $this->drupalPost('node/add/page', $edit, t('Save'), array('query' => array('translation' => $node->nid, 'language' => 'es')));
  91. $duplicate = $this->drupalGetNodeByTitle($edit["title"]);
  92. $this->assertEqual($duplicate->tnid, 0, 'The node does not have a tnid.');
  93. // Update original and mark translation as outdated.
  94. $node_body = $this->randomName();
  95. $node->body[LANGUAGE_NONE][0]['value'] = $node_body;
  96. $edit = array();
  97. $edit["body[$langcode][0][value]"] = $node_body;
  98. $edit['translation[retranslate]'] = TRUE;
  99. $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
  100. $this->assertRaw(t('Basic page %title has been updated.', array('%title' => $node_title)), 'Original node updated.');
  101. // Check to make sure that interface shows translation as outdated.
  102. $this->drupalGet('node/' . $node->nid . '/translate');
  103. $this->assertRaw('<span class="marker">' . t('outdated') . '</span>', 'Translation marked as outdated.');
  104. // Update translation and mark as updated.
  105. $edit = array();
  106. $edit["body[$langcode][0][value]"] = $this->randomName();
  107. $edit['translation[status]'] = FALSE;
  108. $this->drupalPost('node/' . $node_translation->nid . '/edit', $edit, t('Save'));
  109. $this->assertRaw(t('Basic page %title has been updated.', array('%title' => $node_translation_title)), 'Translated node updated.');
  110. // Confirm that disabled languages are an option for translators when
  111. // creating nodes.
  112. $this->drupalGet('node/add/page');
  113. $this->assertFieldByXPath('//select[@name="language"]//option', 'it', 'Italian (disabled) is available in language selection.');
  114. $translation_it = $this->createTranslation($node, $this->randomName(), $this->randomName(), 'it');
  115. $this->assertRaw($translation_it->body[LANGUAGE_NONE][0]['value'], 'Content created in Italian (disabled).');
  116. // Confirm that language neutral is an option for translators when there are
  117. // disabled languages.
  118. $this->drupalGet('node/add/page');
  119. $this->assertFieldByXPath('//select[@name="language"]//option', LANGUAGE_NONE, 'Language neutral is available in language selection with disabled languages.');
  120. $node2 = $this->createPage($this->randomName(), $this->randomName(), LANGUAGE_NONE);
  121. $this->assertRaw($node2->body[LANGUAGE_NONE][0]['value'], 'Language neutral content created with disabled languages available.');
  122. // Leave just one language enabled and check that the translation overview
  123. // page is still accessible.
  124. $this->drupalLogin($this->admin_user);
  125. $edit = array('enabled[es]' => FALSE);
  126. $this->drupalPost('admin/config/regional/language', $edit, t('Save configuration'));
  127. $this->drupalLogin($this->translator);
  128. $this->drupalGet('node/' . $node->nid . '/translate');
  129. $this->assertRaw(t('Translations of %title', array('%title' => $node->title)), 'Translation overview page available with only one language enabled.');
  130. }
  131. /**
  132. * Checks that the language switch links behave properly.
  133. */
  134. function testLanguageSwitchLinks() {
  135. // Create a Basic page in English and its translations in Spanish and
  136. // Italian.
  137. $node = $this->createPage($this->randomName(), $this->randomName(), 'en');
  138. $translation_es = $this->createTranslation($node, $this->randomName(), $this->randomName(), 'es');
  139. $translation_it = $this->createTranslation($node, $this->randomName(), $this->randomName(), 'it');
  140. // Check that language switch links are correctly shown only for enabled
  141. // languages.
  142. $this->assertLanguageSwitchLinks($node, $translation_es);
  143. $this->assertLanguageSwitchLinks($translation_es, $node);
  144. $this->assertLanguageSwitchLinks($node, $translation_it, FALSE);
  145. // Check that links to the displayed translation appear only in the language
  146. // switcher block.
  147. $this->assertLanguageSwitchLinks($node, $node, FALSE, 'node');
  148. $this->assertLanguageSwitchLinks($node, $node, TRUE, 'block-locale');
  149. // Unpublish the Spanish translation to check that the related language
  150. // switch link is not shown.
  151. $this->drupalLogin($this->admin_user);
  152. $edit = array('status' => FALSE);
  153. $this->drupalPost("node/$translation_es->nid/edit", $edit, t('Save'));
  154. $this->drupalLogin($this->translator);
  155. $this->assertLanguageSwitchLinks($node, $translation_es, FALSE);
  156. // Check that content translation links are shown even when no language
  157. // negotiation is configured.
  158. $this->drupalLogin($this->admin_user);
  159. $edit = array('language[enabled][locale-url]' => FALSE);
  160. $this->drupalPost('admin/config/regional/language/configure', $edit, t('Save settings'));
  161. $this->resetCaches();
  162. $edit = array('status' => TRUE);
  163. $this->drupalPost("node/$translation_es->nid/edit", $edit, t('Save'));
  164. $this->drupalLogin($this->translator);
  165. $this->assertLanguageSwitchLinks($node, $translation_es, TRUE, 'node');
  166. }
  167. /**
  168. * Tests that the language switcher block alterations work as intended.
  169. */
  170. function testLanguageSwitcherBlockIntegration() {
  171. // Enable Italian to have three items in the language switcher block.
  172. $this->drupalLogin($this->admin_user);
  173. $edit = array('enabled[it]' => TRUE);
  174. $this->drupalPost('admin/config/regional/language', $edit, t('Save configuration'));
  175. $this->drupalLogin($this->translator);
  176. // Create a Basic page in English.
  177. $type = 'block-locale';
  178. $node = $this->createPage($this->randomName(), $this->randomName(), 'en');
  179. $this->assertLanguageSwitchLinks($node, $node, TRUE, $type);
  180. $this->assertLanguageSwitchLinks($node, $this->emptyNode('es'), TRUE, $type);
  181. $this->assertLanguageSwitchLinks($node, $this->emptyNode('it'), TRUE, $type);
  182. // Create the Spanish translation.
  183. $translation_es = $this->createTranslation($node, $this->randomName(), $this->randomName(), 'es');
  184. $this->assertLanguageSwitchLinks($node, $node, TRUE, $type);
  185. $this->assertLanguageSwitchLinks($node, $translation_es, TRUE, $type);
  186. $this->assertLanguageSwitchLinks($node, $this->emptyNode('it'), TRUE, $type);
  187. // Create the Italian translation.
  188. $translation_it = $this->createTranslation($node, $this->randomName(), $this->randomName(), 'it');
  189. $this->assertLanguageSwitchLinks($node, $node, TRUE, $type);
  190. $this->assertLanguageSwitchLinks($node, $translation_es, TRUE, $type);
  191. $this->assertLanguageSwitchLinks($node, $translation_it, TRUE, $type);
  192. // Create a language neutral node and check that the language switcher is
  193. // left untouched.
  194. $node2 = $this->createPage($this->randomName(), $this->randomName(), LANGUAGE_NONE);
  195. $node2_en = (object) array('nid' => $node2->nid, 'language' => 'en');
  196. $node2_es = (object) array('nid' => $node2->nid, 'language' => 'es');
  197. $node2_it = (object) array('nid' => $node2->nid, 'language' => 'it');
  198. $this->assertLanguageSwitchLinks($node2_en, $node2_en, TRUE, $type);
  199. $this->assertLanguageSwitchLinks($node2_en, $node2_es, TRUE, $type);
  200. $this->assertLanguageSwitchLinks($node2_en, $node2_it, TRUE, $type);
  201. // Disable translation support to check that the language switcher is left
  202. // untouched only for new nodes.
  203. $this->drupalLogin($this->admin_user);
  204. $edit = array('language_content_type' => 0);
  205. $this->drupalPost('admin/structure/types/manage/page', $edit, t('Save content type'));
  206. $this->drupalLogin($this->translator);
  207. // Existing translations trigger alterations even if translation support is
  208. // disabled.
  209. $this->assertLanguageSwitchLinks($node, $node, TRUE, $type);
  210. $this->assertLanguageSwitchLinks($node, $translation_es, TRUE, $type);
  211. $this->assertLanguageSwitchLinks($node, $translation_it, TRUE, $type);
  212. // Check that new nodes with a language assigned do not trigger language
  213. // switcher alterations when translation support is disabled.
  214. $node = $this->createPage($this->randomName(), $this->randomName());
  215. $node_es = (object) array('nid' => $node->nid, 'language' => 'es');
  216. $node_it = (object) array('nid' => $node->nid, 'language' => 'it');
  217. $this->assertLanguageSwitchLinks($node, $node, TRUE, $type);
  218. $this->assertLanguageSwitchLinks($node, $node_es, TRUE, $type);
  219. $this->assertLanguageSwitchLinks($node, $node_it, TRUE, $type);
  220. }
  221. /**
  222. * Resets static caches to make the test code match the client-side behavior.
  223. */
  224. function resetCaches() {
  225. drupal_static_reset('locale_url_outbound_alter');
  226. }
  227. /**
  228. * Returns an empty node data structure.
  229. *
  230. * @param $langcode
  231. * The language code.
  232. *
  233. * @return
  234. * An empty node data structure.
  235. */
  236. function emptyNode($langcode) {
  237. return (object) array('nid' => NULL, 'language' => $langcode);
  238. }
  239. /**
  240. * Installs the specified language, or enables it if it is already installed.
  241. *
  242. * @param $language_code
  243. * The language code to check.
  244. */
  245. function addLanguage($language_code) {
  246. // Check to make sure that language has not already been installed.
  247. $this->drupalGet('admin/config/regional/language');
  248. if (strpos($this->drupalGetContent(), 'enabled[' . $language_code . ']') === FALSE) {
  249. // Doesn't have language installed so add it.
  250. $edit = array();
  251. $edit['langcode'] = $language_code;
  252. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
  253. // Make sure we are not using a stale list.
  254. drupal_static_reset('language_list');
  255. $languages = language_list('language');
  256. $this->assertTrue(array_key_exists($language_code, $languages), 'Language was installed successfully.');
  257. if (array_key_exists($language_code, $languages)) {
  258. $this->assertRaw(t('The language %language has been created and can now be used. More information is available on the <a href="@locale-help">help screen</a>.', array('%language' => $languages[$language_code]->name, '@locale-help' => url('admin/help/locale'))), 'Language has been created.');
  259. }
  260. }
  261. elseif ($this->xpath('//input[@type="checkbox" and @name=:name and @checked="checked"]', array(':name' => 'enabled[' . $language_code . ']'))) {
  262. // It's installed and enabled. No need to do anything.
  263. $this->assertTrue(true, 'Language [' . $language_code . '] already installed and enabled.');
  264. }
  265. else {
  266. // It's installed but not enabled. Enable it.
  267. $this->assertTrue(true, 'Language [' . $language_code . '] already installed.');
  268. $this->drupalPost(NULL, array('enabled[' . $language_code . ']' => TRUE), t('Save configuration'));
  269. $this->assertRaw(t('Configuration saved.'), 'Language successfully enabled.');
  270. }
  271. }
  272. /**
  273. * Creates a "Basic page" in the specified language.
  274. *
  275. * @param $title
  276. * The title of a basic page in the specified language.
  277. * @param $body
  278. * The body of a basic page in the specified language.
  279. * @param $language
  280. * (optional) Language code.
  281. *
  282. * @return
  283. * A node object.
  284. */
  285. function createPage($title, $body, $language = NULL) {
  286. $edit = array();
  287. $langcode = LANGUAGE_NONE;
  288. $edit["title"] = $title;
  289. $edit["body[$langcode][0][value]"] = $body;
  290. if (!empty($language)) {
  291. $edit['language'] = $language;
  292. }
  293. $this->drupalPost('node/add/page', $edit, t('Save'));
  294. $this->assertRaw(t('Basic page %title has been created.', array('%title' => $title)), 'Basic page created.');
  295. // Check to make sure the node was created.
  296. $node = $this->drupalGetNodeByTitle($title);
  297. $this->assertTrue($node, 'Node found in database.');
  298. return $node;
  299. }
  300. /**
  301. * Creates a translation for a basic page in the specified language.
  302. *
  303. * @param $node
  304. * The basic page to create the translation for.
  305. * @param $title
  306. * The title of a basic page in the specified language.
  307. * @param $body
  308. * The body of a basic page in the specified language.
  309. * @param $language
  310. * Language code.
  311. *
  312. * @return
  313. * Translation object.
  314. */
  315. function createTranslation($node, $title, $body, $language) {
  316. $this->drupalGet('node/add/page', array('query' => array('translation' => $node->nid, 'target' => $language)));
  317. $langcode = LANGUAGE_NONE;
  318. $body_key = "body[$langcode][0][value]";
  319. $this->assertFieldByXPath('//input[@id="edit-title"]', $node->title, "Original title value correctly populated.");
  320. $this->assertFieldByXPath("//textarea[@name='$body_key']", $node->body[LANGUAGE_NONE][0]['value'], "Original body value correctly populated.");
  321. $edit = array();
  322. $edit["title"] = $title;
  323. $edit[$body_key] = $body;
  324. $this->drupalPost(NULL, $edit, t('Save'));
  325. $this->assertRaw(t('Basic page %title has been created.', array('%title' => $title)), 'Translation created.');
  326. // Check to make sure that translation was successful.
  327. $translation = $this->drupalGetNodeByTitle($title);
  328. $this->assertTrue($translation, 'Node found in database.');
  329. $this->assertTrue($translation->tnid == $node->nid, 'Translation set id correctly stored.');
  330. return $translation;
  331. }
  332. /**
  333. * Asserts an element identified by the given XPath has the given content.
  334. *
  335. * @param $xpath
  336. * The XPath used to find the element.
  337. * @param array $arguments
  338. * An array of arguments with keys in the form ':name' matching the
  339. * placeholders in the query. The values may be either strings or numeric
  340. * values.
  341. * @param $value
  342. * The text content of the matched element to assert.
  343. * @param $message
  344. * The message to display.
  345. * @param $group
  346. * The group this message belongs to.
  347. *
  348. * @return
  349. * TRUE on pass, FALSE on fail.
  350. */
  351. function assertContentByXPath($xpath, array $arguments = array(), $value = NULL, $message = '', $group = 'Other') {
  352. $found = $this->findContentByXPath($xpath, $arguments, $value);
  353. return $this->assertTrue($found, $message, $group);
  354. }
  355. /**
  356. * Tests whether the specified language switch links are found.
  357. *
  358. * @param $node
  359. * The node to display.
  360. * @param $translation
  361. * The translation whose link has to be checked.
  362. * @param $find
  363. * TRUE if the link must be present in the node page.
  364. * @param $types
  365. * The page areas to be checked.
  366. *
  367. * @return
  368. * TRUE if the language switch links are found, FALSE if not.
  369. */
  370. function assertLanguageSwitchLinks($node, $translation, $find = TRUE, $types = NULL) {
  371. if (empty($types)) {
  372. $types = array('node', 'block-locale');
  373. }
  374. elseif (is_string($types)) {
  375. $types = array($types);
  376. }
  377. $result = TRUE;
  378. $languages = language_list();
  379. $page_language = $languages[entity_language('node', $node)];
  380. $translation_language = $languages[$translation->language];
  381. $url = url("node/$translation->nid", array('language' => $translation_language));
  382. $this->drupalGet("node/$node->nid", array('language' => $page_language));
  383. foreach ($types as $type) {
  384. $args = array('%translation_language' => $translation_language->native, '%page_language' => $page_language->native, '%type' => $type);
  385. if ($find) {
  386. $message = format_string('[%page_language] Language switch item found for %translation_language language in the %type page area.', $args);
  387. }
  388. else {
  389. $message = format_string('[%page_language] Language switch item not found for %translation_language language in the %type page area.', $args);
  390. }
  391. if (!empty($translation->nid)) {
  392. $xpath = '//div[contains(@class, :type)]//a[@href=:url]';
  393. }
  394. else {
  395. $xpath = '//div[contains(@class, :type)]//span[contains(@class, "locale-untranslated")]';
  396. }
  397. $found = $this->findContentByXPath($xpath, array(':type' => $type, ':url' => $url), $translation_language->native);
  398. $result = $this->assertTrue($found == $find, $message) && $result;
  399. }
  400. return $result;
  401. }
  402. /**
  403. * Searches for elements matching the given xpath and value.
  404. *
  405. * @param $xpath
  406. * The XPath used to find the element.
  407. * @param array $arguments
  408. * An array of arguments with keys in the form ':name' matching the
  409. * placeholders in the query. The values may be either strings or numeric
  410. * values.
  411. * @param $value
  412. * The text content of the matched element to assert.
  413. *
  414. * @return
  415. * TRUE if found, otherwise FALSE.
  416. */
  417. function findContentByXPath($xpath, array $arguments = array(), $value = NULL) {
  418. $elements = $this->xpath($xpath, $arguments);
  419. $found = TRUE;
  420. if ($value && $elements) {
  421. $found = FALSE;
  422. foreach ($elements as $element) {
  423. if ((string) $element == $value) {
  424. $found = TRUE;
  425. break;
  426. }
  427. }
  428. }
  429. return $elements && $found;
  430. }
  431. }