translation.test 22 KB

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