PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/sites/all/modules/i18n/tests/drupal_i18n_test_case.php

https://github.com/tommeir/mifgaim
PHP | 294 lines | 167 code | 24 blank | 103 comment | 21 complexity | 97709886aa2002cd3e884547f5a1fe62 MD5 | raw file
  1. <?php
  2. // $Id: drupal_i18n_test_case.php,v 1.1.2.5 2010/03/31 21:13:06 jareyero Exp $
  3. // Core: Id: drupal_web_test_case.php,v 1.96 2009/04/22 09:57:10 dries Exp
  4. /**
  5. * @file
  6. * Provide required modifications to Drupal 7 core DrupalWebTestCase in order
  7. * for it to function properly in Drupal 6.
  8. *
  9. * Copyright 2008-2009 by Jimmy Berry ("boombatower", http://drupal.org/user/214218)
  10. */
  11. require_once drupal_get_path('module', 'simpletest') . '/drupal_web_test_case.php';
  12. /**
  13. * Test case for typical Drupal tests.
  14. */
  15. class Drupali18nTestCase extends DrupalWebTestCase {
  16. // We may want to install the site in a different language
  17. var $install_locale = 'en';
  18. /**
  19. * Adds a language
  20. *
  21. * @param $langcode
  22. * @param $default
  23. * Whether this is the default language
  24. * @param $load
  25. * Whether to load available translations for that language
  26. */
  27. function addLanguage($langcode, $default = FALSE, $load = TRUE) {
  28. require_once './includes/locale.inc';
  29. // Enable installation language as default site language.
  30. locale_add_language($langcode, NULL, NULL, NULL, NULL, NULL, 1, $default);
  31. // Reset language list
  32. language_list('language', TRUE);
  33. // We may need to refresh default language
  34. drupal_init_language();
  35. // @todo import po files for this language
  36. }
  37. /**
  38. * Create translation set from a node
  39. *
  40. * @param $source
  41. * Source node
  42. * @param $languages
  43. * Optional list of language codes
  44. */
  45. function drupalCreateTranslations(&$source, $languages = NULL) {
  46. $languages = $languages ? $languages : array_keys(language_list());
  47. if (empty($source->tnid)) {
  48. db_query("UPDATE {node} SET tnid = %d, translate = %d WHERE nid = %d", $source->nid, 0, $source->nid);
  49. $source->tnid = $source->nid;
  50. }
  51. $translations[$source->language] = $source;
  52. foreach ($languages as $lang) {
  53. if ($lang != $source->language) {
  54. $translations[$lang] = $this->drupalCreateNode(array('type' => $source->type, 'language' => $lang, 'translation_source' => $source, 'status' => $source->status, 'promote' => $source->promote, 'uid' => $source->uid));
  55. }
  56. }
  57. return $translations;
  58. }
  59. /**
  60. * Enable language switcher block
  61. */
  62. function enableBlock($module, $delta, $region = 'left') {
  63. db_query("UPDATE {blocks} SET status = %d, weight = %d, region = '%s', throttle = %d WHERE module = '%s' AND delta = '%s' AND theme = '%s'", 1, 0, $region, 0, $module, $delta, variable_get('theme_default', 'garland'));
  64. cache_clear_all();
  65. }
  66. /**
  67. * Reset theme to default so we can play with blocks
  68. */
  69. function initTheme() {
  70. global $theme, $theme_key;
  71. unset($theme);
  72. unset($theme_key);
  73. init_theme();
  74. _block_rehash();
  75. }
  76. /**
  77. * Generates a random database prefix, runs the install scripts on the
  78. * prefixed database and enable the specified modules. After installation
  79. * many caches are flushed and the internal browser is setup so that the
  80. * page requests will run on the new prefix. A temporary files directory
  81. * is created with the same name as the database prefix.
  82. *
  83. * @param ...
  84. * List of modules to enable for the duration of the test.
  85. */
  86. function setUp() {
  87. global $db_prefix, $user, $language; // $language (Drupal 6).
  88. global $install_locale;
  89. // Store necessary current values before switching to prefixed database.
  90. $this->db_prefix_original = $db_prefix;
  91. $clean_url_original = variable_get('clean_url', 0);
  92. // Generate temporary prefixed database to ensure that tests have a clean starting point.
  93. $db_prefix = 'simpletest' . mt_rand(1000, 1000000);
  94. include_once './includes/install.inc';
  95. drupal_install_system();
  96. // Add the specified modules to the list of modules in the default profile.
  97. $args = func_get_args();
  98. // Add language and basic i18n modules
  99. $install_locale = $this->install_locale;
  100. $i18n_modules = array('locale', 'translation', 'i18n', 'i18n_test');
  101. $modules = array_unique(array_merge(drupal_verify_profile('default', $this->install_locale), $args, $i18n_modules));
  102. drupal_install_modules($modules);
  103. // Install locale
  104. if ($this->install_locale != 'en') {
  105. $this->addLanguage($this->install_locale, TRUE);
  106. }
  107. // Run default profile tasks.
  108. $task = 'profile';
  109. default_profile_tasks($task, '');
  110. // Rebuild caches.
  111. actions_synchronize();
  112. _drupal_flush_css_js();
  113. $this->refreshVariables();
  114. $this->checkPermissions(array(), TRUE);
  115. user_access(NULL, NULL, TRUE); // Drupal 6.
  116. // Log in with a clean $user.
  117. $this->originalUser = $user;
  118. // drupal_save_session(FALSE);
  119. // $user = user_load(1);
  120. session_save_session(FALSE);
  121. $user = user_load(array('uid' => 1));
  122. // Restore necessary variables.
  123. variable_set('install_profile', 'default');
  124. variable_set('install_task', 'profile-finished');
  125. variable_set('clean_url', $clean_url_original);
  126. variable_set('site_mail', 'simpletest@example.com');
  127. // Use temporary files directory with the same prefix as database.
  128. $this->originalFileDirectory = file_directory_path();
  129. variable_set('file_directory_path', file_directory_path() . '/' . $db_prefix);
  130. $directory = file_directory_path();
  131. // Create the files directory.
  132. file_check_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
  133. set_time_limit($this->timeLimit);
  134. // Some more includes
  135. require_once 'includes/language.inc';
  136. // Refresh theme
  137. $this->initTheme();
  138. // Set path languages so we can retrieve pages in different languages
  139. variable_set('language_negotiation', LANGUAGE_NEGOTIATION_PATH);
  140. }
  141. /**
  142. * Delete created files and temporary files directory, delete the tables created by setUp(),
  143. * and reset the database prefix.
  144. */
  145. protected function tearDown() {
  146. parent::tearDown();
  147. // Reset language list
  148. language_list('language', TRUE);
  149. drupal_init_language();
  150. if (module_exists('locale')) {
  151. locale(NULL, NULL, TRUE);
  152. }
  153. }
  154. /**
  155. * Retrieves a Drupal path or an absolute path with language
  156. */
  157. protected function i18nGet($langcode, $path = '', array $options = array(), array $headers = array()) {
  158. $options += array('language' => $this->getLanguage($langcode));
  159. return $this->drupalGet($path, $options, $headers);
  160. }
  161. /**
  162. * Get language object for langcode
  163. */
  164. public function getLanguage($langcode) {
  165. if (is_object($langcode)) {
  166. return $langcode;
  167. }
  168. else {
  169. $language_list = language_list();
  170. return $language_list[$langcode];
  171. }
  172. }
  173. /**
  174. * Switch global language
  175. */
  176. public function switchLanguage($newlang = NULL) {
  177. $newlang = $newlang ? $newlang : $this->install_locale;
  178. $GLOBALS['language'] = $this->getLanguage($newlang);
  179. }
  180. /**
  181. * Get all languages that are not default
  182. */
  183. public function getOtherLanguages() {
  184. $languages = language_list();
  185. unset($languages[language_default('language')]);
  186. return $languages;
  187. }
  188. /**
  189. * Create and store one translation into the db
  190. */
  191. public function i18nstringsCreateTranslation($name, $lang, $length = 20) {
  192. $translation = $this->randomName($length, "i18n-$lang-");
  193. $count = self::i18nstringsSaveTranslation($name, $lang, $translation);
  194. $this->assertTrue($count, "A translation($lang) has been created for string $name");
  195. return $translation;
  196. }
  197. /**
  198. * Translate one string into the db
  199. */
  200. public static function i18nstringsSaveTranslation($name, $lang, $translation, $update = FALSE) {
  201. $source = i18nstrings_get_source($name);
  202. if ($source) {
  203. if ($update) {
  204. db_query("UPDATE {locales_target} SET translation = '%s' WHERE lid = %d AND language = '%s'", $translation, $source->lid, $lang);
  205. } else {
  206. db_query("INSERT INTO {locales_target} (translation, lid, language) VALUES ('%s', %d, '%s')", $translation, $source->lid, $lang);
  207. }
  208. return db_affected_rows();
  209. }
  210. else {
  211. return 0;
  212. }
  213. }
  214. /**
  215. * Print out a variable for debugging
  216. */
  217. function printDebug($data, $title = '') {
  218. $string = is_array($data) || is_object($data) ? print_r($data, TRUE) : $data;
  219. $output = $title ? $title . ':' . $string : $string;
  220. //$this->assertTrue(TRUE, $output);
  221. $this->assertTrue(TRUE, $output, 'Debug');
  222. }
  223. /**
  224. * Debug dump object with some formatting
  225. */
  226. function printObject($object, $title = 'Object') {
  227. $output = $this->formatTable($object);
  228. $this->printDebug($output, $title);
  229. }
  230. /**
  231. * Print out current HTML page
  232. */
  233. function printPage() {
  234. $this->printDebug($this->drupalGetContent());
  235. }
  236. // Dump table contents
  237. function dumpTable($table) {
  238. $result = db_query('SELECT * FROM {' . $table . '}');
  239. $output = 'Table dump <strong>' . $table . '</strong>:';
  240. while ($row = db_fetch_array($result)) {
  241. $rows[] = $row;
  242. if (empty($header)) {
  243. $header = array_keys($row);
  244. }
  245. }
  246. if (!empty($rows)) {
  247. $output .= theme('table', $header, $rows);
  248. } else {
  249. $output .= ' No rows';
  250. }
  251. $this->assertTrue(TRUE, $output);
  252. }
  253. /**
  254. * Format object as table, recursive
  255. */
  256. function formatTable($object) {
  257. foreach ($object as $key => $value) {
  258. $rows[] = array(
  259. $key,
  260. is_array($value) || is_object($value) ? $this->formatTable($value) : $value,
  261. );
  262. }
  263. if (!empty($rows)) {
  264. return theme('table', array(), $rows);
  265. }
  266. else {
  267. return 'No properties';
  268. }
  269. }
  270. }