1 <?php // #!/usr/local/bin/php -Cq
2 /* Copyright (C) 2004 Dan Frankowski <dfrankow@cs.umn.edu>
3 * Copyright (C) 2004 Reini Urban <rurban@x-ray.at>
5 * This file is part of PhpWiki.
7 * PhpWiki is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * PhpWiki is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with PhpWiki; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Unit tests for PhpWiki.
25 * You must have PEAR's PHPUnit package <http://pear.php.net/package/PHPUnit>.
26 * These tests are unrelated to test/maketest.pl, which do not use PHPUnit.
27 * These tests run from the command-line as well as from the browser.
28 * Use the argv (from cli) or tests (from browser) params to run only certain tests.
30 * $ tests.php test=<testname1> test=<testname2> ... db=dba debug=9 level=10
32 /****************************************************************
33 User definable options
34 *****************************************************************/
35 // common cfg options are taken from config/config.ini
37 define('GROUP_METHOD', 'NONE');
38 define('USE_DB_SESSION', false);
39 define('RATING_STORAGE', 'WIKIPAGE');
41 // memory usage: (8MB limit on certain servers)
43 // cli: Mem16712 => Mem16928
44 // web: Mem21216 => Mem26332 (5MB)
47 // cli: Mem20696 => Mem31240 (with USECACHE) (10MB)
48 // cli: Mem20240 => Mem30212 (without USECACHE) (10MB)
49 // web: Mem29424 => Mem35400 (without USECACHE) (6MB)
50 //define('USECACHE', false);
52 // available database backends to test:
53 $database_backends = array(
59 // "flatfile" testing occurs in "tests/unit/.testbox/"
60 // "dba" needs the DATABASE_DBA_HANDLER, also in the .textbox directory
61 $database_dba_handler = "db3";
62 // "SQL" and "ADODB" need delete permissions to the test db
63 // You have to create that database beforehand with our schema
64 $database_dsn = "mysql://wikiuser:@localhost/phpwiki_test";
65 // For "cvs" see the seperate tests/unit_test_backend_cvs.php
67 ####################################################################
69 # Preamble needed to get the tests to run.
71 ####################################################################
74 # Add root dir to the path
75 if (substr(PHP_OS,0,3) == 'WIN')
76 $cur_dir = str_replace("\\","/", $cur_dir);
77 $rootdir = $cur_dir . '/../../';
78 $ini_sep = substr(PHP_OS,0,3) == 'WIN' ? ';' : ':';
79 ini_set('include_path', ini_get('include_path')
81 . $ini_sep . $rootdir . "lib/pear");
82 if ($HTTP_SERVER_VARS["SERVER_NAME"] == 'phpwiki.sourceforge.net') {
83 ini_set('include_path', ini_get('include_path') . ":/usr/share/pear");
86 # Quiet warnings in IniConfig.php
87 $HTTP_SERVER_VARS['REMOTE_ADDR'] = '127.0.0.1';
88 $HTTP_SERVER_VARS['HTTP_USER_AGENT'] = "PHPUnit";
90 function printMemoryUsage($msg = '') {
91 if ($msg) echo $msg,"\n";
92 if ((defined('DEBUG') and (DEBUG & 8)) or !defined('DEBUG')) {
93 echo "-- MEMORY USAGE: ";
94 if (function_exists('memory_get_usage') and memory_get_usage()) {
95 echo memory_get_usage(),"\n";
97 } elseif (function_exists('getrusage')) {
99 echo $u['ru_maxrss'],"\n";
101 } elseif (substr(PHP_OS,0,3)=='WIN') { // requires a newer cygwin
102 // what we want is the process memory only: apache or php
104 // this works only if it's a cygwin process (apache or php)
105 //echo `cat /proc/$pid/statm |cut -f1`,"\n";
107 // if it's native windows use something like this:
108 // (requires pslist from systinternals.com)
109 echo `pslist $pid|grep -A1 Mem|perl -ane"print \$F[5]"`,"\n";
112 echo `ps -o%mem,vsz,rss,pid -p $pid|sed 1d`,"\n";
117 function printSimpleTrace($bt) {
120 foreach ($bt as $i => $elem) {
121 if (!array_key_exists('file', $elem)) {
124 print " " . $elem['file'] . ':' . $elem['line'] . "\n";
127 # Show lots of detail when an assert() in the code fails
128 function assert_callback( $script, $line, $message ) {
129 echo "assert failed: script ", $script," line ", $line," :";
132 printSimpleTrace(debug_backtrace());
135 $foo = assert_options( ASSERT_CALLBACK, 'assert_callback');
138 # Get error reporting to call back, too
140 // set the error reporting level for this script
141 if (defined('E_STRICT') and (E_ALL & E_STRICT)) // strict php5?
142 error_reporting(E_ALL & ~E_STRICT); // exclude E_STRICT
144 error_reporting(E_ALL); // php4
146 // This is too strict, fails on every notice and warning.
148 function myErrorHandler$errno, $errstr, $errfile, $errline) {
149 echo "$errfile: $errline: error# $errno: $errstr\n";
151 printSimpleTrace(debug_backtrace());
153 // The ErrorManager version
154 function _ErrorHandler_CB(&$error) {
156 printSimpleTrace(debug_backtrace());
157 if ($error->isFatal()) {
158 $error->errno = E_USER_WARNING;
159 return true; // ignore error
163 // set to the user defined error handler
164 // $old_error_handler = set_error_handler("myErrorHandler");
165 // This is already done via _DEBUG_TRACE
166 //$ErrorManager->pushErrorHandler(new WikiFunctionCb('_ErrorHandler_CB'));
169 function purge_dir($dir) {
171 if (!isset($finder)) {
172 $finder = new FileFinder;
174 $fileSet = new fileSet($dir);
175 assert(!empty($dir));
176 foreach ($fileSet->getFiles() as $f) {
181 function purge_testbox() {
183 if (isset($GLOBALS['request'])) {
184 $dbi = $GLOBALS['request']->getDbh();
186 $dir = $db_params['directory'];
187 switch ($db_params['dbtype']) {
189 assert(!empty($dir));
190 foreach (array('latest_ver','links','page_data','ver_data') as $d) {
191 purge_dir("$dir/$d");
196 foreach ($dbi->_backend->_table_names as $table) {
197 $dbi->genericSqlQuery("DELETE FROM $table");
205 $dbi->_cache->close();
206 $dbi->_backend->_latest_versions = array();
210 ####################################################################
212 # End of preamble, run the test suite ..
214 ####################################################################
216 # lib/config.php might do a cwd()
218 if (isset($HTTP_SERVER_VARS['REQUEST_METHOD']))
220 elseif (!empty($HTTP_SERVER_VARS['argv']))
221 $argv = $HTTP_SERVER_VARS['argv'];
222 elseif (!ini_get("register_argc_argv"))
223 echo "Could not read cmd args (register_argc_argv=Off?)\n";
226 $debug_level = 9; //_DEBUG_VERBOSE | _DEBUG_TRACE
227 $user_level = 1; // BOGO
228 // use argv (from cli) or tests (from browser) params to run only certain tests
229 // avoid pear: Console::Getopt
230 $alltests = array('InlineParserTest','HtmlParserTest','PageListTest','ListPagesTest',
231 'SetupWiki','DumpHtml','AllPagesTest','AllUsersTest','OrphanedPagesTest');
232 if (isset($HTTP_SERVER_VARS['REQUEST_METHOD'])) {
234 foreach ($HTTP_GET_VARS as $key => $val) {
236 foreach ($val as $v) $argv[] = $key."=".$v;
237 elseif (strstr($val,",") and in_array($key,array("test","db")))
238 foreach (explode(",",$val) as $v) $argv[] = $key."=".$v;
240 $argv[] = $key."=".$val;
242 } elseif (!empty($argv) and preg_match("/test\.php$/", $argv[0]))
245 //support db=file db=dba test=SetupWiki test=DumpHtml debug=num
247 $run_database_backends = array();
248 foreach ($argv as $arg) {
249 if (preg_match("/^test=(.+)$/",$arg,$m) and in_array($m[1], $alltests))
251 elseif (preg_match("/^db=(.+)$/",$arg,$m) and in_array($m[1], $database_backends))
252 $run_database_backends[] = $m[1];
253 elseif (preg_match("/^debug=(\d+)$/",$arg,$m))
254 $debug_level = $m[1];
255 elseif (preg_match("/^level=(\d+)$/",$arg,$m))
257 elseif (in_array($arg, $alltests))
259 elseif ($debug_level & 1)
260 echo "ignored arg: ", $arg, "\n";
262 if (!empty($run_database_backends))
263 $database_backends = $run_database_backends;
264 if (!empty($runtests))
265 $alltests = $runtests;
266 if ($debug_level & 1) {
267 echo "test=", join(",",$alltests),"\n";
268 echo "db=", join(",",$database_backends),"\n";
269 echo "debug=", $debug_level,"\n";
270 echo "level=", $user_level,"\n";
271 if ($debug_level & 8) {
272 echo "pid=",getmypid(),"\n";
273 echo "USECACHE=",(defined('USECACHE') and USECACHE) ? "true" : "false","\n";
279 define('DEBUG', $debug_level);
282 printMemoryUsage("before PEAR");
285 require_once 'PHPUnit.php';
288 printMemoryUsage("after PEAR, before PhpWiki");
290 define('PHPWIKI_NOMAIN', true);
292 require_once $rootdir.'index.php';
293 require_once $rootdir.'lib/main.php';
295 global $ErrorManager;
296 $ErrorManager->setPostponedErrorMask(EM_FATAL_ERRORS|EM_WARNING_ERRORS|EM_NOTICE_ERRORS);
297 //FIXME: ignore cached requests (if-modified-since) from cli
298 class MockRequest extends WikiRequest {
299 function MockRequest(&$dbparams) {
300 $this->_dbi = WikiDB::open($dbparams);
301 $this->_user = new MockUser("a_user", $GLOBALS['user_level']);
302 $this->_group = WikiGroup::getGroup();
303 $this->_args = array('pagename' => 'HomePage', 'action' => 'browse');
306 function getGroup() {
307 if (is_object($this->_group))
308 return $this->_group;
309 else // FIXME: this is set to "/f:" somewhere.
310 return WikiGroup::getGroup();
314 if (ENABLE_USER_NEW) {
315 class MockUser extends _WikiUser {
316 function MockUser($name, $level) {
317 $this->_userid = $name;
318 $this->_isSignedIn = $level > 1;
319 $this->_level = $level;
321 function isSignedIn() {
322 return $this->_isSignedIn;
326 class MockUser extends WikiUser {
327 function MockUser($name, $level) {
328 $this->_userid = $name;
329 $this->_isSignedIn = $level > 1;
330 $this->_level = $level;
332 function isSignedIn() {
333 return $this->_isSignedIn;
340 $request->_user = WikiUser('AnonUser');
342 $request->_user = new WikiUser($request, 'AnonUser');
343 $request->_prefs = $request->_user->getPreferences();
346 include_once("themes/" . THEME . "/themeinfo.php");
348 if (DEBUG & _DEBUG_TRACE)
349 printMemoryUsage("after PhpWiki, before tests");
351 // save and restore all args for each test.
352 class phpwiki_TestCase extends PHPUnit_TestCase {
355 $this->_savedargs = $request->_args;
356 $request->_args = array();
358 echo $this->_name,"\n";
362 function tearDown() {
364 $request->_args = $this->_savedargs;
365 if (DEBUG & _DEBUG_TRACE)
370 # Test all db backends.
371 foreach ($database_backends as $dbtype) {
373 $suite = new PHPUnit_TestSuite("phpwiki");
375 $db_params = array();
376 $db_params['directory'] = $cur_dir . '/.testbox';
377 $db_params['dsn'] = $database_dsn;
378 $db_params['dba_handler'] = $database_dba_handler;
379 $db_params['dbtype'] = $dbtype;
381 echo "Testing DB Backend \"$dbtype\" ...\n";
382 $request = new MockRequest($db_params);
384 foreach ($alltests as $test) {
385 if (file_exists(dirname(__FILE__).'/lib/'.$test.'.php'))
386 require_once dirname(__FILE__).'/lib/'.$test.'.php';
388 require_once dirname(__FILE__).'/lib/plugin/'.$test.'.php';
389 $suite->addTest( new PHPUnit_TestSuite($test) );
392 $result = PHPUnit::run($suite);
393 echo "ran " . $result->runCount() . " tests, " . $result->failureCount() . " failures.\n";
396 if ($result->failureCount() > 0) {
397 echo "More detail:\n";
398 echo $result->toString();
402 if (isset($HTTP_SERVER_VARS['REQUEST_METHOD']))
405 // (c-file-style: "gnu")
410 // c-hanging-comment-ender-p: nil
411 // indent-tabs-mode: nil