i18n.test 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. <?php
  2. /**
  3. * @file
  4. * Base class for Internationalization tests
  5. */
  6. #[\AllowDynamicProperties]
  7. class Drupali18nTestCase extends DrupalWebTestCase {
  8. protected $current_user;
  9. protected $default_language;
  10. protected $secondary_language;
  11. function setUpLanguages($admin_permissions = array()) {
  12. // Setup admin user.
  13. $this->admin_user = $this->drupalCreateUser(array_merge(array('bypass node access', 'administer nodes', 'administer languages', 'administer content types', 'administer fields', 'administer blocks', 'access administration pages', 'translate interface'), $admin_permissions));
  14. $this->drupalLogin($this->admin_user);
  15. // Add languages.
  16. $this->default_language = 'en';
  17. $this->secondary_language = 'es';
  18. $this->addLanguage($this->default_language);
  19. $this->addLanguage($this->secondary_language);
  20. // Enable URL language detection and selection to make the language switcher
  21. // block appear.
  22. $edit = array('language[enabled][locale-url]' => TRUE);
  23. $this->drupalPost('admin/config/regional/language/configure', $edit, t('Save settings'));
  24. $this->assertRaw(t('Language negotiation configuration saved.'), t('URL language detection enabled.'));
  25. $this->drupalGet('admin/config/regional/language/configure');
  26. $this->resetCaches();
  27. }
  28. /**
  29. * Set up content-type (with translation).
  30. */
  31. function setUpContentType($settings = array()) {
  32. $settings += array(
  33. 'type' => 'page',
  34. 'mode' => TRANSLATION_ENABLED,
  35. 'status' => 1,
  36. 'promote' => 0,
  37. );
  38. $type = node_type_get_type($settings['type']);
  39. // Create content editor with translation permissions.
  40. $this->content_editor = $this->drupalCreateUser(array(
  41. 'create ' . $type->type . ' content',
  42. 'edit own ' . $type->type . ' content',
  43. 'translate content',
  44. 'translate interface',
  45. ));
  46. $this->drupalLogin($this->admin_user);
  47. // Set content type to use multilingual support with translation.
  48. $this->drupalGet('admin/structure/types/manage/' . $type->type);
  49. $edit = array();
  50. $edit['language_content_type'] = $settings['mode'];
  51. // Mark status and promoted
  52. $edit['node_options[status]'] = $settings['status'];
  53. $edit['node_options[promote]'] = $settings['promote'];
  54. $this->drupalPost('admin/structure/types/manage/' . $type->type, $edit, t('Save content type'));
  55. $this->assertRaw(t('The content type %type has been updated.', array('%type' => $type->name)), t('%type content type has been updated.', array('%type' => $type->name)));
  56. $this->drupalGet('admin/structure/types/manage/' . $type->type);
  57. $this->enableLanguageBlock();
  58. }
  59. /**
  60. * Enable the language switcher block.
  61. */
  62. function enableLanguageBlock() {
  63. // Enable the language switcher block.
  64. $language_type = LANGUAGE_TYPE_INTERFACE;
  65. $edit = array("blocks[locale_$language_type][region]" => 'sidebar_first');
  66. $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
  67. }
  68. /**
  69. * Set up translation for content type (default: page).
  70. */
  71. function setUpContentTranslation($settings = array()) {
  72. $settings += array(
  73. 'mode' => TRANSLATION_ENABLED,
  74. );
  75. $this->setUpContentType($settings);
  76. }
  77. /**
  78. * Install a the specified language if it has not been already. Otherwise make sure that
  79. * the language is enabled.
  80. *
  81. * @param $language_code
  82. * The language code the check.
  83. */
  84. function addLanguage($language_code) {
  85. // Check to make sure that language has not already been installed.
  86. $this->drupalGet('admin/config/regional/language');
  87. if (strpos($this->drupalGetContent(), 'enabled[' . $language_code . ']') === FALSE) {
  88. // Doesn't have language installed so add it.
  89. $edit = array();
  90. $edit['langcode'] = $language_code;
  91. $this->drupalPost('admin/config/regional/language/add', $edit, t('Add language'));
  92. // Make sure we are not using a stale list.
  93. drupal_static_reset('language_list');
  94. $languages = language_list('language');
  95. $this->assertTrue(array_key_exists($language_code, $languages), t('Language was installed successfully.'));
  96. if (array_key_exists($language_code, $languages)) {
  97. $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'))), t('Language has been created.'));
  98. }
  99. }
  100. elseif ($this->xpath('//input[@type="checkbox" and @name=:name and @checked="checked"]', array(':name' => 'enabled[' . $language_code . ']'))) {
  101. // It's installed and enabled. No need to do anything.
  102. $this->assertTrue(true, 'Language [' . $language_code . '] already installed and enabled.');
  103. }
  104. else {
  105. // It's installed but not enabled. Enable it.
  106. $this->assertTrue(true, 'Language [' . $language_code . '] already installed.');
  107. $this->drupalPost(NULL, array('enabled[' . $language_code . ']' => TRUE), t('Save configuration'));
  108. $this->assertRaw(t('Configuration saved.'), t('Language successfully enabled.'));
  109. }
  110. }
  111. /**
  112. * Create translation set from a node
  113. *
  114. * @param $source
  115. * Source node
  116. * @param $languages
  117. * Optional list of language codes
  118. */
  119. function createNodeTranslationSet(&$source, $languages = NULL) {
  120. if (empty($source->tnid)) {
  121. $source->tnid = $source->nid;
  122. }
  123. $translations[$source->language] = $source;
  124. foreach (language_list() as $language) {
  125. if ($language->language != $source->language) {
  126. $translations[$language->language] = $this->createNodeTranslation($source, $language);
  127. }
  128. }
  129. return $translations;
  130. }
  131. /**
  132. * Create a node of the specified type in the specified language.
  133. * @param $type
  134. * The node type.
  135. * @param $title
  136. * Title of node in specified language.
  137. * @param $body
  138. * Body of node in specified language.
  139. * @param $langcode
  140. * Language code.
  141. */
  142. function createNode($type, $title, $body, $langcode, $edit = array()) {
  143. $lang = LANGUAGE_NONE;
  144. $edit["title"] = $title;
  145. $edit["body[$lang][0][value]"] = $body;
  146. $edit['language'] = $langcode;
  147. $this->drupalPost('node/add/' . $type, $edit, t('Save'));
  148. $info = node_type_load($type);
  149. $message = t('@name %title has been created.', array('@name' => $info->name, '%title' => $title));
  150. $this->assertRaw($message);
  151. // Check to make sure the node was created.
  152. $node = $this->drupalGetNodeByTitle($title);
  153. $this->assertTrue($node, t('Node found in database.'));
  154. return $node;
  155. }
  156. /**
  157. * Create a translation for the specified node in the specified language.
  158. *
  159. * @param $node
  160. * The basic page to create translation for.
  161. * @param $title
  162. * Title of node in specified language.
  163. * @param $body
  164. * Body of node in specified language.
  165. * @param $language
  166. * Language code.
  167. */
  168. function createNodeTranslation($node, $language, $title = NULL, $body = NULL) {
  169. $body = $body ? $body : $this->randomName();
  170. $title = $title ? $title : $this->randomName();
  171. $this->drupalGet('node/add/' . $node->type, array('query' => array('translation' => $node->nid, 'target' => $language->language)));
  172. $this->assertFieldByXPath('//input[@id="edit-title"]', $node->title, "Original title value correctly populated.");
  173. $field_lang = field_language('node', $node, 'body');
  174. $body_key = "body[und][0][value]";
  175. $this->assertFieldByXPath("//textarea[@name='$body_key']", $node->body[$field_lang][0]['value'], "Original body value correctly populated.");
  176. $edit = array();
  177. $edit["title"] = $title;
  178. $edit[$body_key] = $body;
  179. $this->drupalPost(NULL, $edit, t('Save'));
  180. $info = node_type_load($node->type);
  181. $message = t('@name %title has been created.', array('@name' => $info->name, '%title' => $title));
  182. $this->assertRaw($message);
  183. // Check to make sure that translation was successful.
  184. $translation = $this->drupalGetNodeByTitle($title);
  185. $this->assertTrue($translation, t('Node found in database.'));
  186. $this->assertTrue($translation->tnid == $node->nid, t('Translation set id correctly stored.'));
  187. return $translation;
  188. }
  189. /**
  190. * Retrieves a Drupal path or an absolute path with language
  191. *
  192. * @param $language
  193. * Language code or language object
  194. */
  195. protected function i18nGet($language, $path = '', array $options = array(), array $headers = array()) {
  196. $options['language'] = $this->getLanguage($language);
  197. return $this->drupalGet($path, $options, $headers);
  198. }
  199. /**
  200. * Check strings for different languages
  201. */
  202. function i18nAssertTranslations($translations, $path = '', $message = 'Translation found for language.') {
  203. foreach ($translations as $langcode => $text) {
  204. $language = $this->getLanguage($langcode);
  205. if ($language->enabled) {
  206. $this->i18nGet($language, $path);
  207. $this->assertRaw($text, $message . ' ' . $language->name . ': ' . check_plain($text));
  208. }
  209. }
  210. }
  211. /**
  212. * Create node with language
  213. */
  214. protected function i18nCreateNode($language, $settings = array()) {
  215. $language = $this->getLanguage($language);
  216. $settings += array('language' => $language->language, 'body' => array());
  217. $settings['body'] += array($language->language => array(array()));
  218. return $this->drupalCreateNode($settings);
  219. }
  220. /**
  221. * Move block to region, from block.test
  222. */
  223. function moveBlockToRegion($block, $region = 'sidebar_first') {
  224. $this->drupalLogin($this->admin_user);
  225. // Set the created block to a specific region.
  226. $edit = array();
  227. $edit['blocks[' . $block['module'] . '_' . $block['delta'] . '][region]'] = $region;
  228. $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
  229. // Confirm that the block was moved to the proper region.
  230. $this->assertText(t('The block settings have been updated.'), t('Block successfully moved to %region_name region.', array( '%region_name' => $region)));
  231. // Confirm that the block is being displayed.
  232. $this->drupalGet('node');
  233. $this->assertText(check_plain($block['title']), t('Block successfully being displayed on the page.'));
  234. // Confirm that the custom block was found at the proper region.
  235. $xpath = $this->buildXPathQuery('//div[@class=:region-class]//div[@id=:block-id]/*', array(
  236. ':region-class' => 'region region-' . str_replace('_', '-', $region),
  237. ':block-id' => 'block-' . $block['module'] . '-' . $block['delta'],
  238. ));
  239. $this->assertFieldByXPath($xpath, NULL, t('Custom block found in %region_name region.', array('%region_name' => $region)));
  240. }
  241. /**
  242. * Get language object for langcode
  243. */
  244. public function getLanguage($langcode) {
  245. if (is_object($langcode)) {
  246. return $langcode;
  247. }
  248. else {
  249. $language_list = language_list();
  250. return $language_list[$langcode];
  251. }
  252. }
  253. /**
  254. * Switch global language
  255. */
  256. public function switchLanguage($newlang = NULL) {
  257. $newlang = $newlang ? $newlang : $this->install_locale;
  258. $GLOBALS[LANGUAGE_TYPE_INTERFACE] = $this->getLanguage($newlang);
  259. }
  260. /**
  261. * Get all languages that are not default
  262. */
  263. public function getOtherLanguages() {
  264. $languages = language_list();
  265. unset($languages[language_default('language')]);
  266. return $languages;
  267. }
  268. /**
  269. * Get enabled languages
  270. */
  271. public function getEnabledLanguages() {
  272. $list = array();
  273. foreach (language_list() as $langcode => $language) {
  274. if (!empty($language->enabled)) {
  275. $list[$langcode] = $language;
  276. }
  277. }
  278. return $list;
  279. }
  280. /**
  281. * Create translation for string in textgroup
  282. *
  283. * @param $translations
  284. * Optional array of langcode => translation. If not present, it will be generated.
  285. */
  286. function createStringTranslation($textgroup, $name, $translations = NULL) {
  287. // Generate translations if not found, they will be the same length as source string
  288. if (!$translations) {
  289. $length = strlen($name);
  290. foreach ($this->getOtherLanguages() as $language) {
  291. $translations[$language->language] = $this->randomName($length);
  292. }
  293. }
  294. $this->drupalLogin($this->translator);
  295. // This is the language indicator on the translation search screen for
  296. // untranslated strings. Copied straight from locale.inc.
  297. $language_indicator = "<em class=\"locale-untranslated\">";
  298. // Search for the name and translate it.
  299. $search = array(
  300. 'string' => $name,
  301. 'language' => 'all',
  302. 'translation' => 'all',
  303. 'group' => $textgroup,
  304. );
  305. $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  306. // assertText() seems to remove the input field where $name always could be
  307. // found, so this is not a false assert. See how assertNoText succeeds
  308. // later.
  309. $this->assertText(check_plain($name), t('Search found the name.'));
  310. $this->assertRaw($language_indicator, t('Name is untranslated.'));
  311. // Assume this is the only result, given the random name.
  312. $this->clickLink(t('edit'));
  313. // We save the lid from the path.
  314. $matches = array();
  315. preg_match('!admin/config/regional/translate/edit/(\d+)!', $this->getUrl(), $matches);
  316. $lid = $matches[1];
  317. // No t() here, it's surely not translated yet.
  318. $this->assertText(check_plain($name), t('name found on edit screen.'));
  319. foreach ($translations as $langcode => $translation) {
  320. $edit["translations[$langcode]"] = $translation;
  321. }
  322. $this->drupalPost(NULL, $edit, t('Save translations'));
  323. $this->assertText(t('The string has been saved.'), t('The string has been saved.'));
  324. $this->assertEqual($this->getUrl(), url('admin/config/regional/translate/translate', array('absolute' => TRUE)), t('Correct page redirection.'));
  325. $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  326. // The indicator should not be here.
  327. $this->assertNoRaw($language_indicator, t('String is translated.'));
  328. return $translations;
  329. }
  330. /**
  331. * Reset static caches to make the test code match the client site behavior.
  332. */
  333. function resetCaches() {
  334. drupal_static_reset('locale_url_outbound_alter');
  335. drupal_static_reset('language_list');
  336. drupal_language_initialize();
  337. }
  338. /**
  339. * Print out a variable for debugging
  340. */
  341. function printDebug($data, $title = 'Debug') {
  342. $output = '<h2>' . $title . '<h2 />';
  343. $output .= '<pre>';
  344. $output .= is_array($data) || is_object($data) ? print_r($data, TRUE) : $data;
  345. $output .= '<pre>';
  346. //$this->assertTrue(TRUE, $output);
  347. $this->verbose($output);
  348. }
  349. /**
  350. * Debug dump object with some formatting
  351. */
  352. function printObject($object, $title = 'Object') {
  353. $output = $this->formatTable($object);
  354. $this->printDebug($output, $title);
  355. }
  356. /**
  357. * Print out current HTML page
  358. */
  359. function printPage() {
  360. $this->printDebug($this->drupalGetContent());
  361. }
  362. /**
  363. * Dump table contents
  364. *
  365. * @params $table1, $table2..
  366. * One or more table names
  367. */
  368. function dumpTable() {
  369. $output = '';
  370. foreach (func_get_args() as $table) {
  371. $header = $rows = array();
  372. $result = db_query('SELECT * FROM {' . $table . '}');
  373. $output .= '<h2>Table dump <i>' . $table . '</i>:</h2>';
  374. while ($row = $result->fetchAssoc()) {
  375. $rows[] = $row;
  376. if (empty($header)) {
  377. $header = array_keys($row);
  378. }
  379. }
  380. if (!empty($rows)) {
  381. $output .= theme('table', array('header' => $header, 'rows' => $rows));
  382. }
  383. else {
  384. $output .= ' No rows';
  385. }
  386. $output .= '<br />';
  387. }
  388. $this->verbose($output);
  389. }
  390. /**
  391. * Format object as table, recursive
  392. */
  393. function formatTable($object) {
  394. foreach ($object as $key => $value) {
  395. $rows[] = array(
  396. $key,
  397. is_array($value) || is_object($value) ? $this->formatTable($value) : $value,
  398. );
  399. }
  400. if (!empty($rows)) {
  401. return theme('table', array('rows' => $rows));
  402. }
  403. else {
  404. return 'No properties';
  405. }
  406. }
  407. }
  408. #[\AllowDynamicProperties]
  409. class Drupali18nConfigTestCase extends Drupali18nTestCase {
  410. public static function getInfo() {
  411. return array(
  412. 'name' => 'Multilingual configuration',
  413. 'group' => 'Internationalization',
  414. 'description' => 'Basic configuration for the i18n module',
  415. );
  416. }
  417. function setUp() {
  418. parent::setUp('translation', 'i18n_node');
  419. parent::setUpLanguages();
  420. }
  421. function testEnableLanguages() {
  422. // A language with two letter code may help too
  423. $this->addLanguage('pt-br');
  424. // Disable Italian to test the translation behavior with disabled languages.
  425. $this->addLanguage('it');
  426. $edit = array('enabled[it]' => FALSE);
  427. $this->drupalPost('admin/config/regional/language', $edit, t('Save configuration'));
  428. }
  429. }