entity_translation.test 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. <?php
  2. /**
  3. * @file
  4. * Tests for Entity translation module.
  5. */
  6. /**
  7. * Base class for entity translation module tests.
  8. */
  9. class EntityTranslationTestCase extends DrupalWebTestCase {
  10. protected $current_user;
  11. protected $admin_user;
  12. protected $translator_user;
  13. function setUp() {
  14. $args = func_get_args();
  15. call_user_func_array(array('parent', 'setUp'), $args);
  16. // Reset user fields to make test object reusable.
  17. unset($this->current_user);
  18. unset($this->admin_user);
  19. unset($this->translator_user);
  20. }
  21. /**
  22. * Retrieves a Drupal path or an absolute path with language.
  23. *
  24. * @param $language
  25. * Language code or language object.
  26. */
  27. function get($language, $path = '', array $options = array(), array $headers = array()) {
  28. $options['language'] = $this->getLanguage($language);
  29. return $this->drupalGet($path, $options, $headers);
  30. }
  31. /**
  32. * Posts to a Drupal path with language.
  33. */
  34. function post($language, $path, $edit, $submit, array $options = array(), array $headers = array(), $form_html_id = NULL, $extra_post = NULL) {
  35. $options['language'] = $this->getLanguage($language);
  36. $this->drupalPost($path, $edit, $submit, $options, $headers, $form_html_id, $extra_post);
  37. }
  38. /**
  39. * Login the given user only if she has not changed.
  40. */
  41. function login($user) {
  42. if (!isset($this->current_user) || $this->current_user->uid != $user->uid) {
  43. $this->current_user = $user;
  44. $this->drupalLogin($user);
  45. }
  46. }
  47. /**
  48. * Returns a user with administration rights.
  49. *
  50. * @param $permissions
  51. * Additional permissions for administrative user.
  52. */
  53. function getAdminUser(array $permissions = array()) {
  54. if (!isset($this->admin_user)) {
  55. $this->admin_user = $this->drupalCreateUser(array_merge(array(
  56. 'bypass node access',
  57. 'administer nodes',
  58. 'administer languages',
  59. 'administer content types',
  60. 'administer blocks',
  61. 'access administration pages',
  62. 'administer site configuration',
  63. ), $permissions));
  64. }
  65. return $this->admin_user;
  66. }
  67. /**
  68. * Returns a user with minimal translation rights.
  69. *
  70. * @param $permissions
  71. * Additional permissions for administrative user.
  72. */
  73. function getTranslatorUser(array $permissions = array()) {
  74. if (!isset($this->translator_user)) {
  75. $this->translator_user = $this->drupalCreateUser(array_merge(array(
  76. 'create page content',
  77. 'edit own page content',
  78. 'delete own page content',
  79. 'translate any entity',
  80. ), $permissions));
  81. }
  82. return $this->translator_user;
  83. }
  84. /**
  85. * Make sure the clean urls are enabled.
  86. */
  87. function enableCleanUrls() {
  88. $this->drupalGet('admin/config/search/clean-urls');
  89. $edit = array();
  90. $edit['clean_url'] = TRUE;
  91. $this->drupalPost(NULL, $edit, t('Save configuration'));
  92. }
  93. /**
  94. * Enable URL language detection.
  95. */
  96. function enableUrlLanguageDetection() {
  97. // Enable URL language detection and selection.
  98. $edit = array(
  99. 'language[enabled][locale-url]' => TRUE,
  100. 'language_content[enabled][locale-interface]' => TRUE
  101. );
  102. $this->drupalPost('admin/config/regional/language/configure', $edit, t('Save settings'));
  103. $this->assertRaw(t('Language negotiation configuration saved.'), t('URL language detection enabled.'));
  104. $this->drupalGet('admin/config/regional/language/configure');
  105. // Reset caches.
  106. drupal_static_reset('locale_url_outbound_alter');
  107. drupal_static_reset('language_list');
  108. }
  109. /**
  110. * Get a language object from a language code.
  111. */
  112. public function getLanguage($langcode) {
  113. if (is_object($langcode)) {
  114. return $langcode;
  115. }
  116. else {
  117. $language_list = language_list();
  118. return $language_list[$langcode];
  119. }
  120. }
  121. /**
  122. * Install a specified language if it has not been already, otherwise make sure that the language is enabled.
  123. *
  124. * @param $langcode
  125. * The language code to check.
  126. */
  127. function addLanguage($langcode) {
  128. // Check to make sure that language has not already been installed.
  129. $this->drupalGet('admin/config/regional/language');
  130. if (strpos($this->drupalGetContent(), 'enabled[' . $langcode . ']') === FALSE) {
  131. // Doesn't have language installed so add it.
  132. $edit = array();
  133. $edit['langcode'] = $langcode;
  134. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
  135. // Make sure we are not using a stale list.
  136. drupal_static_reset('language_list');
  137. $languages = language_list('language');
  138. $this->assertTrue(array_key_exists($langcode, $languages), t('Language was installed successfully.'));
  139. if (array_key_exists($langcode, $languages)) {
  140. $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[$langcode]->name, '@locale-help' => url('admin/help/locale'))), t('Language has been created.'));
  141. }
  142. }
  143. elseif ($this->xpath('//input[@type="checkbox" and @name=:name and @checked="checked"]', array(':name' => 'enabled[' . $langcode . ']'))) {
  144. // It is installed and enabled. No need to do anything.
  145. $this->assertTrue(TRUE, 'Language [' . $langcode . '] already installed and enabled.');
  146. }
  147. else {
  148. // It is installed but not enabled. Enable it.
  149. $this->assertTrue(TRUE, 'Language [' . $langcode . '] already installed.');
  150. $this->drupalPost(NULL, array('enabled[' . $langcode . ']' => TRUE), t('Save configuration'));
  151. $this->assertRaw(t('Configuration saved.'), t('Language successfully enabled.'));
  152. }
  153. }
  154. /**
  155. * Configure the "Basic page" content type for entity translation tests.
  156. */
  157. function configureContentType() {
  158. // Configure the "Basic page" content type to use multilingual support with
  159. // translation.
  160. $edit = array();
  161. $edit['language_content_type'] = ENTITY_TRANSLATION_ENABLED;
  162. $this->drupalPost('admin/structure/types/manage/page', $edit, t('Save content type'));
  163. $this->assertRaw(t('The content type %type has been updated.', array('%type' => 'Basic page')), t('Basic page content type has been updated.'));
  164. // Set body field's cardinality to unlimited and toggle translatability.
  165. $edit = array();
  166. $edit['field[cardinality]'] = FIELD_CARDINALITY_UNLIMITED;
  167. $edit['field[translatable]'] = 1;
  168. $this->drupalPost('admin/structure/types/manage/page/fields/body', $edit, t('Save settings'));
  169. $this->assertRaw(t('Saved %field configuration.', array('%field' => 'Body')), t('Body field settings have been updated.'));
  170. // Check if the setting works.
  171. $this->drupalGet('node/add/page');
  172. $this->assertFieldById('edit-body-und-add-more', t('Add another item'), t('Add another item button found.'));
  173. }
  174. /**
  175. * Create a "Basic page" in the specified language.
  176. *
  177. * @param $title
  178. * Title of the basic page in the specified language.
  179. * @param $body
  180. * Body of the basic page in the specified language.
  181. * @param $langcode
  182. * The language code to be assigned to the specified values.
  183. */
  184. function createPage($title, $body, $langcode) {
  185. $edit = array();
  186. $language_none = LANGUAGE_NONE;
  187. $edit["title"] = $title;
  188. $edit["body[$language_none][0][value]"] = $body;
  189. $edit['language'] = $langcode;
  190. $this->drupalPost('node/add/page', $edit, t('Save'));
  191. $this->assertRaw(t('Basic page %title has been created.', array('%title' => $title)), t('Basic page created.'));
  192. // Check to make sure the node was created.
  193. $node = $this->drupalGetNodeByTitle($title);
  194. $this->assertTrue($node, t('Node found in database.'));
  195. return $node;
  196. }
  197. /**
  198. * Create a translation.
  199. *
  200. * @param $node
  201. * Node of the basic page to create translation for.
  202. * @param $title
  203. * Title of the basic page in the specified language.
  204. * @param $body
  205. * Body of the basic page in the specified language.
  206. * @param $langcode
  207. * The language code to be assigned to the specified values.
  208. */
  209. function createTranslation($node, $title, $body, $langcode, $source_langcode = 'en') {
  210. $this->drupalGet('node/' . $node->nid . '/edit/add/' . $source_langcode . '/' . $langcode);
  211. $body_key = "body[$langcode][0][value]";
  212. $this->assertFieldByXPath("//textarea[@name='$body_key']", $node->body[$source_langcode][0]['value'], 'Original body value correctly populated.');
  213. $this->assertFieldById('edit-body-' . $langcode . '-add-more', t('Add another item'), t('Add another item button found.'));
  214. $edit = array();
  215. $edit[$body_key] = $body;
  216. $this->drupalPost(NULL, $edit, t('Save'));
  217. $this->drupalGet('node/' . $node->nid . '/translate');
  218. $this->assertLinkByHref('node/' . $node->nid . '/edit/' . $langcode, 0, t('Translation edit link found. Translation created.'));
  219. return $node;
  220. }
  221. }
  222. /**
  223. * Basic tests for the translation creation/editing workflow.
  224. */
  225. class EntityTranslationTranslationTestCase extends EntityTranslationTestCase {
  226. /**
  227. * Return the test information.
  228. */
  229. public static function getInfo() {
  230. return array(
  231. 'name' => 'Entity translation workflow',
  232. 'description' => 'Basic tests for the translation creation/editing workflow.',
  233. 'group' => 'Entity translation',
  234. );
  235. }
  236. function setUp() {
  237. parent::setUp('locale', 'entity_translation');
  238. $this->login($this->getAdminUser());
  239. $this->addLanguage('en');
  240. $this->addLanguage('es');
  241. $this->configureContentType();
  242. $this->login($this->getTranslatorUser());
  243. }
  244. /**
  245. * Test if field based translation works.
  246. *
  247. * Enable field based translation for basic pages. Add a field with a
  248. * cardinality higher than 1, to test if field_default_extract_form_values()
  249. * is invoked. Create a basic page and translate it.
  250. */
  251. function testFieldTranslation() {
  252. // Create Basic page in English.
  253. $node_title = $this->randomName();
  254. $node_body = $this->randomName();
  255. $node = $this->createPage($node_title, $node_body, 'en');
  256. // Submit translation in Spanish.
  257. $node_translation_title = $this->randomName();
  258. $node_translation_body = $this->randomName();
  259. $node_translation = $this->createTranslation($node, $node_translation_title, $node_translation_body, 'es');
  260. }
  261. }
  262. /**
  263. * Basic tests for comment related things.
  264. *
  265. * @todo Add tests for comment translation workflow.
  266. */
  267. class EntityTranslationCommentTestCase extends EntityTranslationTestCase {
  268. protected $comment_user;
  269. /**
  270. * Return the test information.
  271. */
  272. public static function getInfo() {
  273. return array(
  274. 'name' => 'Comment translation',
  275. 'description' => 'Basic tests for comment translation/filtering.',
  276. 'group' => 'Entity translation',
  277. );
  278. }
  279. function setUp() {
  280. parent::setUp('locale', 'entity_translation', 'comment');
  281. $this->login($this->getAdminUser());
  282. $this->addLanguage('en');
  283. $this->addLanguage('es');
  284. $this->enableUrlLanguageDetection();
  285. $this->configureContentType();
  286. $this->configureComments(FALSE);
  287. $this->login($this->getTranslatorUser());
  288. }
  289. function tearDown() {
  290. unset($this->comment_user);
  291. parent::tearDown();
  292. }
  293. function getCommentUser() {
  294. if (empty($this->comment_user)) {
  295. $this->comment_user = $this->drupalCreateUser(array(
  296. 'access comments',
  297. 'post comments',
  298. 'edit own comments',
  299. ));
  300. }
  301. return $this->comment_user;
  302. }
  303. /**
  304. * Enable comments and comment filtering by language.
  305. */
  306. function configureComments($filter_by_language = TRUE) {
  307. $edit = array();
  308. $edit['comment'] = COMMENT_NODE_OPEN;
  309. $edit['entity_translation_comment_filter'] = $filter_by_language;
  310. $this->drupalPost('admin/structure/types/manage/page', $edit, t('Save content type'));
  311. $this->assertRaw(t('The content type %type has been updated.', array('%type' => 'Basic page')));
  312. $this->drupalGet('admin/structure/types/manage/page');
  313. if ($filter_by_language) {
  314. $this->assertFieldChecked('edit-entity-translation-comment-filter', 'Comment filtering is enabled.');
  315. }
  316. else {
  317. $this->assertNoFieldChecked('edit-entity-translation-comment-filter', 'Comment filtering is disabled.');
  318. }
  319. }
  320. /**
  321. * Add a comment for the given node.
  322. *
  323. * @param $node
  324. * The node for which to add the comment.
  325. * @param $comment_body
  326. * The comment body text.
  327. * @param $language
  328. * The comment language.
  329. */
  330. function postComment($node, $comment_body, $language) {
  331. $edit = array();
  332. $edit['comment_body[' . LANGUAGE_NONE . '][0][value]'] = $comment_body;
  333. $this->post($language, 'comment/reply/' . $node->nid, $edit, t('Save'));
  334. }
  335. /**
  336. * Test comment filtering by language.
  337. */
  338. function testCommentLanguageFiltering() {
  339. $node = $this->createPage($this->randomName(), $this->randomName(), 'en');
  340. $this->login($this->getCommentUser());
  341. // Create comments in different languages.
  342. $comment_en = $this->randomName();
  343. $this->postComment($node, $comment_en, 'en');
  344. $comment_es = $this->randomName();
  345. $this->postComment($node, $comment_es, 'es');
  346. // Check that ALL comments are being displayed when comment language filter
  347. // is disabled (default behavior).
  348. $this->get('en', 'node/' . $node->nid);
  349. $this->assertText($comment_en, 'English comment found.');
  350. $this->assertText($comment_es, 'Spanish comment found.');
  351. // Enable comment filtering by language.
  352. $this->login($this->getAdminUser());
  353. $this->configureComments(TRUE);
  354. $this->login($this->getCommentUser());
  355. // Load page in different languages. Check that only comments matching
  356. // current language are being displayed.
  357. $this->get('en', 'node/' . $node->nid);
  358. $this->assertText($comment_en, 'English comment found.');
  359. $this->assertNoText($comment_es, 'Spanish comment not found.');
  360. $this->get('es', 'node/' . $node->nid);
  361. $this->assertNoText($comment_en, 'English comment not found.');
  362. $this->assertText($comment_es, 'Spanish comment found.');
  363. }
  364. }
  365. /**
  366. * Test CRUD hook invocation.
  367. */
  368. class EntityTranslationHookTestCase extends EntityTranslationTestCase {
  369. /**
  370. * Return the test information.
  371. */
  372. public static function getInfo() {
  373. return array(
  374. 'name' => 'Entity translation hooks',
  375. 'description' => 'Test that entity translation hooks are properly fired.',
  376. 'group' => 'Entity translation',
  377. );
  378. }
  379. function setUp() {
  380. parent::setUp('locale', 'entity_translation', 'entity_translation_test');
  381. $this->login($this->getAdminUser());
  382. $this->addLanguage('it');
  383. $this->addLanguage('es');
  384. $this->configureContentType();
  385. $this->login($this->getTranslatorUser());
  386. }
  387. /**
  388. * Test whether hooks are properly fired in the regular form workflow.
  389. */
  390. function testFormWorkflow() {
  391. // Create Basic page in English.
  392. $node_title = $this->randomName();
  393. $node_body = $this->randomName();
  394. $node = $this->createPage($node_title, $node_body, 'en');
  395. // Submit translation in Italian.
  396. $node_translation_body = $this->randomName();
  397. $this->createTranslation($node, NULL, $node_translation_body, 'it');
  398. $info = $this->getHookInfo();
  399. $this->assertTrue(!empty($info['insert']), t('Insert hook has been properly fired.'));
  400. // Edit translation in Italian.
  401. $edit = array("body[it][0][value]" => $this->randomName());
  402. $this->drupalPost('node/' . $node->nid . '/edit/it', $edit, t('Save'));
  403. $info = $this->getHookInfo();
  404. $this->assertTrue(!empty($info['update']), t('Update hook has been properly fired.'));
  405. // Delete the Basic page.
  406. $edit = array();
  407. $this->drupalPost('node/' . $node->nid . '/delete', $edit, t('Delete'));
  408. $info = $this->getHookInfo('delete');
  409. $this->assertTrue(count($info) == 2 && !empty($info['en']) && !empty($info['it']), t('Delete hook has been properly fired.'));
  410. }
  411. /**
  412. * Test whether hooks are properly fired when using the API.
  413. */
  414. function testAPI() {
  415. // Create Basic page in English.
  416. $node = $this->createPage($this->randomName(), $this->randomName(), 'en');
  417. $handler = entity_translation_get_handler('node', $node);
  418. // Create a translation in Italian.
  419. $translation = array('source' => 'en', 'language' => 'it');
  420. $handler->setTranslation($translation);
  421. $handler->saveTranslations();
  422. $node = node_load($node->nid, NULL, TRUE);
  423. $handler = entity_translation_get_handler('node', $node);
  424. $translations = $handler->getTranslations();
  425. $this->assertTrue(!empty($translations->data['it']), t('An Italian translation has been created'));
  426. $info = $this->getHookInfo();
  427. $this->assertTrue(!empty($info['insert']), t('Insert hook has been properly fired.'));
  428. // Check that the update hook is properly fired.
  429. $translation['status'] = 1;
  430. $handler->setTranslation($translation);
  431. $handler->saveTranslations();
  432. $info = $this->getHookInfo();
  433. $this->assertTrue(!empty($info['update']), t('Update hook has been properly fired.'));
  434. // Create a Spanish translation and update it before saving it.
  435. $translation = array('source' => 'it', 'language' => 'es');
  436. $handler->setTranslation($translation);
  437. $translation['status'] = 1;
  438. $handler->setTranslation($translation);
  439. $handler->saveTranslations();
  440. $node = node_load($node->nid, NULL, TRUE);
  441. $handler = entity_translation_get_handler('node', $node);
  442. $translations = $handler->getTranslations();
  443. $this->assertTrue(!empty($translations->data['es']), t('A Spanish translation has been created'));
  444. $info = $this->getHookInfo();
  445. $this->assertTrue(!empty($info['insert']), t('Insert hook has been properly fired.'));
  446. // Delete a translation after updating it without saving.
  447. $translation['status'] = 0;
  448. $handler->setTranslation($translation);
  449. $handler->removeTranslation('es');
  450. $handler->saveTranslations();
  451. $info = $this->getHookInfo();
  452. $this->assertTrue(empty($info['update']), t('Update hook has not been fired.'));
  453. $info = $this->getHookInfo('delete');
  454. $this->assertTrue(!empty($info['es']), t('Delete hook has been properly fired.'));
  455. }
  456. /**
  457. * Retrieve the information stored by hook implementations.
  458. */
  459. protected function getHookInfo($op = 'save') {
  460. $name = 'entity_translation_test_' . $op;
  461. $info = variable_get($name);
  462. variable_del($name);
  463. return $info;
  464. }
  465. }