i18n.test 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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 fields', '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 node of the specified type in the specified language.
  132. * @param $type
  133. * The node type.
  134. * @param $title
  135. * Title of node in specified language.
  136. * @param $body
  137. * Body of node in specified language.
  138. * @param $langcode
  139. * Language code.
  140. */
  141. function createNode($type, $title, $body, $langcode, $edit = array()) {
  142. $lang = LANGUAGE_NONE;
  143. $edit["title"] = $title;
  144. $edit["body[$lang][0][value]"] = $body;
  145. $edit['language'] = $langcode;
  146. $this->drupalPost('node/add/' . $type, $edit, t('Save'));
  147. $info = node_type_load($type);
  148. $message = t('@name %title has been created.', array('@name' => $info->name, '%title' => $title));
  149. $this->assertRaw($message);
  150. // Check to make sure the node was created.
  151. $node = $this->drupalGetNodeByTitle($title);
  152. $this->assertTrue($node, t('Node found in database.'));
  153. return $node;
  154. }
  155. /**
  156. * Create a translation for the specified node in the specified language.
  157. *
  158. * @param $node
  159. * The basic page to create translation for.
  160. * @param $title
  161. * Title of node in specified language.
  162. * @param $body
  163. * Body of node in specified language.
  164. * @param $language
  165. * Language code.
  166. */
  167. function createNodeTranslation($node, $language, $title = NULL, $body = NULL) {
  168. $body = $body ? $body : $this->randomName();
  169. $title = $title ? $title : $this->randomName();
  170. $this->drupalGet('node/add/' . $node->type, array('query' => array('translation' => $node->nid, 'target' => $language->language)));
  171. $this->assertFieldByXPath('//input[@id="edit-title"]', $node->title, "Original title value correctly populated.");
  172. $field_lang = field_language('node', $node, 'body');
  173. $body_key = "body[und][0][value]";
  174. $this->assertFieldByXPath("//textarea[@name='$body_key']", $node->body[$field_lang][0]['value'], "Original body value correctly populated.");
  175. $edit = array();
  176. $edit["title"] = $title;
  177. $edit[$body_key] = $body;
  178. $this->drupalPost(NULL, $edit, t('Save'));
  179. $info = node_type_load($node->type);
  180. $message = t('@name %title has been created.', array('@name' => $info->name, '%title' => $title));
  181. $this->assertRaw($message);
  182. // Check to make sure that translation was successful.
  183. $translation = $this->drupalGetNodeByTitle($title);
  184. $this->assertTrue($translation, t('Node found in database.'));
  185. $this->assertTrue($translation->tnid == $node->nid, t('Translation set id correctly stored.'));
  186. return $translation;
  187. }
  188. /**
  189. * Retrieves a Drupal path or an absolute path with language
  190. *
  191. * @param $language
  192. * Language code or language object
  193. */
  194. protected function i18nGet($language, $path = '', array $options = array(), array $headers = array()) {
  195. $options['language'] = $this->getLanguage($language);
  196. return $this->drupalGet($path, $options, $headers);
  197. }
  198. /**
  199. * Check strings for different languages
  200. */
  201. function i18nAssertTranslations($translations, $path = '', $message = 'Translation found for language.') {
  202. foreach ($translations as $langcode => $text) {
  203. $language = $this->getLanguage($langcode);
  204. if ($language->enabled) {
  205. $this->i18nGet($language, $path);
  206. $this->assertRaw($text, $message . ' ' . $language->name . ': ' . check_plain($text));
  207. }
  208. }
  209. }
  210. /**
  211. * Create node with language
  212. */
  213. protected function i18nCreateNode($language, $settings = array()) {
  214. $language = $this->getLanguage($language);
  215. $settings += array('language' => $language->language, 'body' => array());
  216. $settings['body'] += array($language->language => array(array()));
  217. return $this->drupalCreateNode($settings);
  218. }
  219. /**
  220. * Move block to region, from block.test
  221. */
  222. function moveBlockToRegion($block, $region = 'sidebar_first') {
  223. $this->drupalLogin($this->admin_user);
  224. // Set the created block to a specific region.
  225. $edit = array();
  226. $edit['blocks[' . $block['module'] . '_' . $block['delta'] . '][region]'] = $region;
  227. $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
  228. // Confirm that the block was moved to the proper region.
  229. $this->assertText(t('The block settings have been updated.'), t('Block successfully moved to %region_name region.', array( '%region_name' => $region)));
  230. // Confirm that the block is being displayed.
  231. $this->drupalGet('node');
  232. $this->assertText(check_plain($block['title']), t('Block successfully being displayed on the page.'));
  233. // Confirm that the custom block was found at the proper region.
  234. $xpath = $this->buildXPathQuery('//div[@class=:region-class]//div[@id=:block-id]/*', array(
  235. ':region-class' => 'region region-' . str_replace('_', '-', $region),
  236. ':block-id' => 'block-' . $block['module'] . '-' . $block['delta'],
  237. ));
  238. $this->assertFieldByXPath($xpath, NULL, t('Custom block found in %region_name region.', array('%region_name' => $region)));
  239. }
  240. /**
  241. * Get language object for langcode
  242. */
  243. public function getLanguage($langcode) {
  244. if (is_object($langcode)) {
  245. return $langcode;
  246. }
  247. else {
  248. $language_list = language_list();
  249. return $language_list[$langcode];
  250. }
  251. }
  252. /**
  253. * Switch global language
  254. */
  255. public function switchLanguage($newlang = NULL) {
  256. $newlang = $newlang ? $newlang : $this->install_locale;
  257. $GLOBALS[LANGUAGE_TYPE_INTERFACE] = $this->getLanguage($newlang);
  258. }
  259. /**
  260. * Get all languages that are not default
  261. */
  262. public function getOtherLanguages() {
  263. $languages = language_list();
  264. unset($languages[language_default('language')]);
  265. return $languages;
  266. }
  267. /**
  268. * Get enabled languages
  269. */
  270. public function getEnabledLanguages() {
  271. $list = array();
  272. foreach (language_list() as $langcode => $language) {
  273. if (!empty($language->enabled)) {
  274. $list[$langcode] = $language;
  275. }
  276. }
  277. return $list;
  278. }
  279. /**
  280. * Create translation for string in textgroup
  281. *
  282. * @param $translations
  283. * Optional array of langcode => translation. If not present, it will be generated.
  284. */
  285. function createStringTranslation($textgroup, $name, $translations = NULL) {
  286. // Generate translations if not found, they will be the same length as source string
  287. if (!$translations) {
  288. $length = strlen($name);
  289. foreach ($this->getOtherLanguages() as $language) {
  290. $translations[$language->language] = $this->randomName($length);
  291. }
  292. }
  293. $this->drupalLogin($this->translator);
  294. // This is the language indicator on the translation search screen for
  295. // untranslated strings. Copied straight from locale.inc.
  296. $language_indicator = "<em class=\"locale-untranslated\">";
  297. // Search for the name and translate it.
  298. $search = array(
  299. 'string' => $name,
  300. 'language' => 'all',
  301. 'translation' => 'all',
  302. 'group' => $textgroup,
  303. );
  304. $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  305. // assertText() seems to remove the input field where $name always could be
  306. // found, so this is not a false assert. See how assertNoText succeeds
  307. // later.
  308. $this->assertText(check_plain($name), t('Search found the name.'));
  309. $this->assertRaw($language_indicator, t('Name is untranslated.'));
  310. // Assume this is the only result, given the random name.
  311. $this->clickLink(t('edit'));
  312. // We save the lid from the path.
  313. $matches = array();
  314. preg_match('!admin/config/regional/translate/edit/(\d+)!', $this->getUrl(), $matches);
  315. $lid = $matches[1];
  316. // No t() here, it's surely not translated yet.
  317. $this->assertText(check_plain($name), t('name found on edit screen.'));
  318. foreach ($translations as $langcode => $translation) {
  319. $edit["translations[$langcode]"] = $translation;
  320. }
  321. $this->drupalPost(NULL, $edit, t('Save translations'));
  322. $this->assertText(t('The string has been saved.'), t('The string has been saved.'));
  323. $this->assertEqual($this->getUrl(), url('admin/config/regional/translate/translate', array('absolute' => TRUE)), t('Correct page redirection.'));
  324. $this->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  325. // The indicator should not be here.
  326. $this->assertNoRaw($language_indicator, t('String is translated.'));
  327. return $translations;
  328. }
  329. /**
  330. * Reset static caches to make the test code match the client site behavior.
  331. */
  332. function resetCaches() {
  333. drupal_static_reset('locale_url_outbound_alter');
  334. drupal_static_reset('language_list');
  335. drupal_language_initialize();
  336. }
  337. /**
  338. * Print out a variable for debugging
  339. */
  340. function printDebug($data, $title = 'Debug') {
  341. $output = '<h2>' . $title . '<h2 />';
  342. $output .= '<pre>';
  343. $output .= is_array($data) || is_object($data) ? print_r($data, TRUE) : $data;
  344. $output .= '<pre>';
  345. //$this->assertTrue(TRUE, $output);
  346. $this->verbose($output);
  347. }
  348. /**
  349. * Debug dump object with some formatting
  350. */
  351. function printObject($object, $title = 'Object') {
  352. $output = $this->formatTable($object);
  353. $this->printDebug($output, $title);
  354. }
  355. /**
  356. * Print out current HTML page
  357. */
  358. function printPage() {
  359. $this->printDebug($this->drupalGetContent());
  360. }
  361. /**
  362. * Dump table contents
  363. *
  364. * @params $table1, $table2..
  365. * One or more table names
  366. */
  367. function dumpTable() {
  368. $output = '';
  369. foreach (func_get_args() as $table) {
  370. $header = $rows = array();
  371. $result = db_query('SELECT * FROM {' . $table . '}');
  372. $output .= '<h2>Table dump <i>' . $table . '</i>:</h2>';
  373. while ($row = $result->fetchAssoc()) {
  374. $rows[] = $row;
  375. if (empty($header)) {
  376. $header = array_keys($row);
  377. }
  378. }
  379. if (!empty($rows)) {
  380. $output .= theme('table', array('header' => $header, 'rows' => $rows));
  381. }
  382. else {
  383. $output .= ' No rows';
  384. }
  385. $output .= '<br />';
  386. }
  387. $this->verbose($output);
  388. }
  389. /**
  390. * Format object as table, recursive
  391. */
  392. function formatTable($object) {
  393. foreach ($object as $key => $value) {
  394. $rows[] = array(
  395. $key,
  396. is_array($value) || is_object($value) ? $this->formatTable($value) : $value,
  397. );
  398. }
  399. if (!empty($rows)) {
  400. return theme('table', array('rows' => $rows));
  401. }
  402. else {
  403. return 'No properties';
  404. }
  405. }
  406. }
  407. class Drupali18nConfigTestCase extends Drupali18nTestCase {
  408. public static function getInfo() {
  409. return array(
  410. 'name' => 'Multilingual configuration',
  411. 'group' => 'Internationalization',
  412. 'description' => 'Basic configuration for the i18n module',
  413. );
  414. }
  415. function setUp() {
  416. parent::setUp('translation', 'i18n_node');
  417. parent::setUpLanguages();
  418. }
  419. function testEnableLanguages() {
  420. // A language with two letter code may help too
  421. $this->addLanguage('pt-br');
  422. // Disable Italian to test the translation behavior with disabled languages.
  423. $this->addLanguage('it');
  424. $edit = array('enabled[it]' => FALSE);
  425. $this->drupalPost('admin/config/regional/language', $edit, t('Save configuration'));
  426. }
  427. }