i18n.test 16 KB

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