PageRenderTime 47ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/classes/simpletest/EvoUnitTestCase.class.php

https://github.com/blueyed/b2evolution
PHP | 330 lines | 149 code | 39 blank | 142 comment | 7 complexity | c41624c7a07582fc117530f5758017cc MD5 | raw file
  1. <?php
  2. /**
  3. * General EvoUnitTestCase.
  4. *
  5. * Implements common methods for testing.
  6. */
  7. /**
  8. * We use a derived reporter, which shows passes
  9. */
  10. //require_once( dirname(__FILE__).'/HtmlReporterShowPasses.class.php' );
  11. load_class( '_core/model/_log.class.php', 'Log' );
  12. load_class( 'files/model/_filerootcache.class.php', 'FileRootCache' );
  13. load_class( 'files/model/_filecache.class.php', 'FileCache' );
  14. load_class( 'files/model/_filetypecache.class.php', 'FileTypeCache' );
  15. load_class( '_core/model/_timer.class.php', 'Timer' );
  16. load_class( 'plugins/model/_plugins_admin.class.php', 'Plugins_admin' );
  17. load_class( 'plugins/model/_plugins_admin_no_db.class.php', 'Plugins_admin_no_DB' );
  18. load_class( 'settings/model/_generalsettings.class.php', 'GeneralSettings' );
  19. load_class( 'users/model/_usersettings.class.php', 'UserSettings' );
  20. load_class( 'users/model/_user.class.php', 'User' );
  21. load_class( 'users/model/_usercache.class.php', 'UserCache' );
  22. load_class( 'files/model/_file.class.php', 'File' );
  23. load_class( 'files/model/_filetype.class.php', 'FileType' );
  24. load_class( '_core/model/_module.class.php', 'Module' );
  25. foreach( $modules as $module )
  26. {
  27. require_once $inc_path.$module.'/_'.$module.'.init.php';
  28. }
  29. // Let the modules load/register what they need:
  30. modules_call_method( 'init' );
  31. /**
  32. * Class EvoUnitTestCase
  33. */
  34. class EvoUnitTestCase extends UnitTestCase
  35. {
  36. /**
  37. * Is this a slow / long running testcase?
  38. * This might be useful to skip later on.
  39. */
  40. const slow_testcase = NULL;
  41. /**
  42. * Setup required globals
  43. */
  44. function setUp()
  45. {
  46. global $FileRootCache, $FiletypeCache, $FileCache, $GroupCache, $DB, $db_config, $Debuglog, $Messages, $UserCache, $Timer, $Plugins, $Settings, $UserSettings;
  47. global $allow_evodb_reset;
  48. parent::setUp(); // just because..
  49. $Debuglog = new Log('note');
  50. $Messages = new Log('error');
  51. $FileRootCache = new FileRootCache();
  52. $UserCache = new UserCache();
  53. $FileCache = new FileCache();
  54. $FileRootCache = new FileRootCache();
  55. $FiletypeCache = new FiletypeCache();
  56. $GroupCache = new DataObjectCache( 'Group', true, 'T_groups', 'grp_', 'grp_ID' );
  57. $Timer = new Timer();
  58. $Plugins = new Plugins_admin_no_DB();
  59. $db_params = $db_config;
  60. $db_params['new_link'] = true; // needed to not interfere with the DB connection to the test DB (setup in EvoDbUnitTestCase).
  61. $DB = new DB( $db_params );
  62. /*
  63. if( $DB->query('SHOW TABLES LIKE "T_settings"') && ! get_db_version() )
  64. { // DB tables created, but no data loaded
  65. // Let's delete it and reinstall
  66. echo '<h2>'.T_('Deleting b2evolution tables from the datatase...').'</h2>';
  67. evo_flush();
  68. if( $allow_evodb_reset != 1 )
  69. {
  70. echo T_('If you have installed b2evolution tables before and wish to start anew, you must delete the b2evolution tables before you can start a new installation. b2evolution can delete its own tables for you, but for obvious security reasons, this feature is disabled by default.');
  71. echo '<p>'.sprintf( T_('To enable it, please go to the %s file and change: %s to %s'), '/conf/_basic_config.php', '<pre>$allow_evodb_reset = 0;</pre>', '<pre>$allow_evodb_reset = 1;</pre>' ).'</p>';
  72. echo '<p>'.T_('Then reload this page and a reset option will appear.').'</p>';
  73. echo '<p><a href="index.php?locale='.$default_locale.'">&laquo; '.T_('Back to install menu').'</a></p>';
  74. die();
  75. }
  76. // $this->create_current_tables();
  77. db_delete();
  78. echo '<h2>Tables deleted</h2>';
  79. }
  80. if( ! $DB->query('SHOW TABLES LIKE "T_settings"') && ! get_db_version() )
  81. { // b2evolution is not installed, let's run the installer
  82. global $inc_path, $modules, $default_locale, $evo_charset, $current_charset, $current_locale;
  83. // This initializes table name aliases and is required before trying to connect to the DB.
  84. load_class( '_core/model/_module.class.php', 'Module' );
  85. foreach( $modules as $module )
  86. {
  87. require_once $inc_path.$module.'/_'.$module.'.init.php';
  88. }
  89. // Let the modules load/register what they need:
  90. modules_call_method( 'init' );
  91. // Load all available locale defintions:
  92. locales_load_available_defs();
  93. // Activate default locale:
  94. if( ! locale_activate( $default_locale ) )
  95. { // Could not activate locale (non-existent?), fallback to en-US:
  96. $default_locale = 'en-US';
  97. locale_activate( 'en-US' );
  98. }
  99. init_charsets( $current_charset );
  100. // fp> TODO: this test should probably be made more generic and applied to upgrade too.
  101. $expected_connection_charset = $DB->php_to_mysql_charmap($evo_charset);
  102. if( $DB->connection_charset != $expected_connection_charset )
  103. {
  104. echo '<div class="error"><p class="error">'.sprintf( T_('In order to install b2evolution with the %s locale, your MySQL needs to support the %s connection charset.').' (SET NAMES %s)',
  105. $current_locale, $evo_charset, $expected_connection_charset ).'</p></div>';
  106. // sam2kb> TODO: If something is not supported we can display a message saying "do this and that, enable extension X etc. etc... or switch to a better hosting".
  107. die();
  108. }
  109. //$this->create_current_tables();
  110. // NOTE: this is the same as with install action "newdb":
  111. echo '<h2>Manual</h2>';
  112. //install_newdb();
  113. }
  114. */
  115. // A dirty workaround for GeneralSettings::_construct
  116. // where it checks $new_db_version against unexisting db_version because b2evo is not installed yet
  117. $tmp = $GLOBALS['new_db_version'];
  118. $GLOBALS['new_db_version'] = false;
  119. $Settings = new GeneralSettings();
  120. $UserSettings = new UserSettings();
  121. $GLOBALS['new_db_version'] = $tmp;
  122. // Reload conf and vars.
  123. require EVODIR.'blogs/conf/_config.php';
  124. require EVODIR.'blogs/inc/_main.inc.php';
  125. $this->_saved_globals = array();
  126. $this->_saved_server = array();
  127. }
  128. /**
  129. * Revert any changed globals.
  130. * @see change_global()
  131. * @see change_server()
  132. */
  133. function tearDown()
  134. {
  135. parent::tearDown();
  136. foreach( $this->_saved_globals as $k => $v )
  137. {
  138. $GLOBALS[$k] = $v;
  139. }
  140. foreach( $this->_saved_server as $k => $v )
  141. {
  142. $_SERVER[$k] = $v;
  143. }
  144. }
  145. /**
  146. * Temporarily change a global, which gets reset in {@link tearDown()}.
  147. * @param string Name of global
  148. * @param mixed value
  149. * @return
  150. */
  151. function change_global($global, $value)
  152. {
  153. if( ! isset($this->_saved_globals[$global]) )
  154. {
  155. $this->_saved_globals[$global] = $GLOBALS[$global];
  156. }
  157. $GLOBALS[$global] = $value;
  158. }
  159. /**
  160. * Temporarily change a $_SERVER setting, which gets reset in {@link tearDown()}.
  161. * @param string Name of $_SERVER setting, e.g. 'HTTP_ACCEPT_LANGUAGE'
  162. * @param mixed value
  163. * @return
  164. */
  165. function change_server($key, $value)
  166. {
  167. if( ! isset($this->_saved_server[$key]) )
  168. {
  169. $this->_saved_server[$key] = $_SERVER[$key];
  170. }
  171. $_SERVER[$key] = $value;
  172. }
  173. /**
  174. * Extend run() method to recognize cli mode.
  175. *
  176. * @param SimpleReporter Reporter for HTML mode
  177. * @param SimpleReporter Reporter for CLI mode
  178. * @access public
  179. */
  180. function run_html_or_cli()
  181. {
  182. if( EvoTextReporter::inCli() )
  183. {
  184. exit( parent::run( new EvoTextReporter() ) ? 0 : 1 );
  185. }
  186. parent::run( new EvoHtmlReporter() );
  187. }
  188. /**
  189. * Custom method to print a skip message.
  190. */
  191. function my_skip_message($message)
  192. {
  193. $this->reporter->paintSkip($message . $this->getAssertionLine());
  194. }
  195. /**
  196. * Get all files below a path, excluding symlinks.
  197. * @return array
  198. */
  199. function get_files_without_symlinks( $path, $pattern = '~\.(php|inc|html?)$~' )
  200. {
  201. $r = array();
  202. foreach( get_filenames( $path, array( 'inc_files' => false, 'recurse' => false ) ) as $dir )
  203. {
  204. if( is_link($dir) )
  205. {
  206. continue;
  207. }
  208. // files:
  209. $files = get_filenames( $dir, array( 'inc_dirs' => false, 'recurse' => false ) );
  210. if( $pattern )
  211. {
  212. foreach( $files as $k => $v )
  213. {
  214. if( ! preg_match( $pattern, $v ) )
  215. { // Not a PHP file (include HTM(L), because it gets parsed sometimes, too)
  216. unset($files[$k]);
  217. }
  218. }
  219. }
  220. $r = array_merge( $r, $files );
  221. // subdirs:
  222. $r = array_merge( $r, $this->get_files_without_symlinks($dir));
  223. }
  224. return $r;
  225. }
  226. }
  227. #
  228. # WORK IN PROGRESS: dh> an attempt to silence the noise from test, especially install tests.
  229. #
  230. /**
  231. * A reporter that is meant to suppress any output from the test methods.
  232. * Unfortunately, this silences simpletests error reporting, too.. :/
  233. * @todo dh> find another way.
  234. */
  235. class EvoTextReporter extends TextReporter
  236. {
  237. function paintMethodStart($test_name)
  238. {
  239. parent::paintMethodStart($test_name);
  240. //ob_start();
  241. }
  242. function paintMethodEnd($test_name)
  243. {
  244. //while(ob_get_status())
  245. // ob_end_clean();
  246. parent::paintMethodEnd($test_name);
  247. }
  248. }
  249. /**
  250. * A reporter that is meant to suppress any output from the test methods.
  251. * Unfortunately, this silences simpletests error reporting, too.. :/
  252. * @todo dh> find another way.
  253. */
  254. class EvoHtmlReporter extends HtmlReporter
  255. {
  256. function __construct($charset = NULL)
  257. {
  258. if( is_null($charset) )
  259. {
  260. global $io_charset;
  261. $charset = $io_charset;
  262. }
  263. parent::__construct($charset);
  264. }
  265. function paintMethodStart($test_name)
  266. {
  267. parent::paintMethodStart($test_name);
  268. //ob_start();
  269. }
  270. function paintMethodEnd($test_name)
  271. {
  272. //while(ob_get_status())
  273. // ob_end_clean();
  274. parent::paintMethodEnd($test_name);
  275. }
  276. }
  277. ?>