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