1 #!/usr/local/bin/php -Cq
3 /* Copyright (C) 2004 Dan Frankowski <dfrankow@cs.umn.edu>
4 * Copyright (C) 2004 Reini Urban <rurban@x-ray.at>
6 * This file is part of PhpWiki.
8 * PhpWiki is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * PhpWiki is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with PhpWiki; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * Unit tests for PhpWiki.
26 * You must have PEAR's PHPUnit package <http://pear.php.net/package/PHPUnit>.
27 * These tests are unrelated to test/maketest.pl, which do not use PHPUnit.
28 * These tests run from the command-line as well as from the browser.
29 * Use the argv (from cli) or tests (from browser) params to run only certain tests.
32 ####################################################################
34 # Preamble needed to get the tests to run.
36 ####################################################################
40 # Add root dir to the path
41 if (substr(PHP_OS,0,3) == 'WIN')
42 $cur_dir = str_replace("\\","/",$cur_dir);
43 $rootdir = $cur_dir . '/../../';
44 $ini_sep = substr(PHP_OS,0,3) == 'WIN' ? ';' : ':';
45 ini_set('include_path', ini_get('include_path') . $ini_sep . $rootdir);
47 # This quiets a warning in config.php
48 $HTTP_SERVER_VARS['REMOTE_ADDR'] = '127.0.0.1';
49 $HTTP_SERVER_VARS['HTTP_USER_AGENT'] = "PHPUnit";
50 define('GROUP_METHOD', 'NONE');
51 define('RATING_STORAGE','WIKIPAGE');
52 define('PHPWIKI_NOMAIN',true);
55 require_once $rootdir.'index.php';
56 require_once $rootdir.'lib/main.php';
58 define('DEBUG', _DEBUG_TRACE);
60 function printSimpleTrace($bt) {
63 foreach ($bt as $i => $elem) {
64 if (!array_key_exists('file', $elem)) {
67 print " " . $elem['file'] . ':' . $elem['line'] . "\n";
71 # Show lots of detail when an assert() in the code fails
72 function assert_callback( $script, $line, $message ) {
73 echo "assert failed: script ", $script," line ", $line," :";
76 printSimpleTrace(debug_backtrace());
79 $foo = assert_options( ASSERT_CALLBACK, 'assert_callback');
82 # Get error reporting to call back, too
84 // set the error reporting level for this script
85 error_reporting(E_ALL);
87 // This is too strict, fails on every notice and warning.
88 // TODO: push an errorhandler with printSimpleTrace
89 function myErrorHandler($errno, $errstr, $errfile, $errline)
91 echo "$errfile: $errline: error# $errno: $errstr\n";
94 printSimpleTrace(debug_backtrace());
97 // set to the user defined error handler
98 $old_error_handler = set_error_handler("myErrorHandler");
101 # This is the test DB backend
102 $db_params = array();
103 $db_params['directory'] = $cur_dir . '/.testbox';
104 $db_params['dbtype'] = 'file';
106 if (ENABLE_USER_NEW) {
107 class MockUser extends _WikiUser {
108 function MockUser($name, $isSignedIn) {
109 $this->_userid = $name;
110 $this->_isSignedIn = $isSignedIn;
112 function isSignedIn() {
113 return $this->_isSignedIn;
117 class MockUser extends WikiUser {
118 function MockUser($name, $isSignedIn) {
119 $this->_userid = $name;
120 $this->_isSignedIn = $isSignedIn;
122 function isSignedIn() {
123 return $this->_isSignedIn;
128 //FIXME: ignore cached requests (if-modified-since) from cli
129 class MockRequest extends WikiRequest {
130 function MockRequest(&$dbparams) {
131 $this->_dbi = WikiDB::open($dbparams);
132 $this->_user = new MockUser("a_user", true);
133 $this->_group = WikiGroup::getGroup();
134 $this->_args = array('pagename' => 'HomePage', 'action' => 'browse');
137 function getGroup() {
138 if (is_object($this->_group))
139 return $this->_group;
140 else // FIXME: this is set to "/f:" somewhere.
141 return WikiGroup::getGroup();
145 function purge_dir($dir) {
147 if (!isset($finder)) {
148 $finder = new FileFinder;
150 $fileSet = new fileSet($dir);
152 foreach ($fileSet->getFiles() as $f) {
157 function purge_testbox() {
159 $dir = $db_params['directory'];
161 foreach (array('latest_ver','links','page_data','ver_data') as $d) {
162 purge_dir("$dir/$d");
164 if (isset($GLOBALS['request'])) {
165 $dbi = $GLOBALS['request']->getDbh();
166 $dbi->_cache->close();
167 $dbi->_backend->_latest_versions = array();
171 global $ErrorManager;
172 $ErrorManager->setPostponedErrorMask(E_NOTICE|E_USER_NOTICE|E_USER_WARNING|E_WARNING);
173 $request = new MockRequest($db_params);
177 $request->_user = WikiUser('AnonUser');
179 $request->_user = new WikiUser($request, 'AnonUser');
180 $request->_prefs = $request->_user->getPreferences();
183 include_once("themes/" . THEME . "/themeinfo.php");
185 ####################################################################
187 # End of preamble, run the test suite ..
189 ####################################################################
192 require_once 'PHPUnit.php';
193 # lib/config.php might do a cwd()
195 if (isset($HTTP_SERVER_VARS['REQUEST_METHOD']))
199 print "Run tests .. ";
201 $suite = new PHPUnit_TestSuite("phpwiki");
202 // use argv (from cli) or tests (from browser) params to run only certain tests
203 $alltests = array('InlineParserTest','HtmlParserTest','PageListTest','ListPagesTest',
204 'SetupWiki','DumpHtml','AllPagesTest','AllUsersTest','OrphanedPagesTest');
205 if (isset($HTTP_SERVER_VARS['REQUEST_METHOD']) and !empty($HTTP_GET_VARS['tests']))
206 $argv = explode(',',$HTTP_GET_VARS['tests']);
208 if (count($argv) == 1 and preg_match("/test\.php$/", $argv[0]))
213 foreach ($argv as $test) {
214 if (in_array($test,$alltests))
217 $alltests = $runtests;
221 foreach ($alltests as $test) {
222 if (file_exists(dirname(__FILE__).'/lib/'.$test.'.php'))
223 require_once dirname(__FILE__).'/lib/'.$test.'.php';
225 require_once dirname(__FILE__).'/lib/plugin/'.$test.'.php';
226 $suite->addTest( new PHPUnit_TestSuite($test) );
228 $result = PHPUnit::run($suite);
230 echo "ran " . $result->runCount() . " tests, " . $result->failureCount() . " failures.\n";
233 if ($result->failureCount() > 0) {
234 echo "More detail:\n";
235 echo $result->toString();
238 if (isset($HTTP_SERVER_VARS['REQUEST_METHOD']))
241 // (c-file-style: "gnu")
246 // c-hanging-comment-ender-p: nil
247 // indent-tabs-mode: nil