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.
31 ####################################################################
33 # Preamble needed to get the tests to run.
35 ####################################################################
39 # Add root dir to the path
40 $rootdir = $cur_dir . '/../../';
41 $ini_sep = substr(PHP_OS,0,3) == 'WIN' ? ';' : ':';
42 ini_set('include_path', ini_get('include_path') . $ini_sep . $rootdir);
44 # This quiets a warning in config.php
45 $HTTP_SERVER_VARS['REMOTE_ADDR'] = '127.0.0.1';
46 define('GROUP_METHOD', 'NONE');
49 require_once $rootdir.'index.php';
50 require_once $rootdir.'lib/stdlib.php';
52 $user_theme = 'default';
53 require_once("themes/$user_theme/themeinfo.php"); // Needed for $Theme
55 function printSimpleTrace($bt) {
57 foreach ($bt as $i => $elem) {
58 if (!array_key_exists('file', $elem)) {
61 print " " . $elem['file'] . ':' . $elem['line'] . "\n";
65 # Show lots of detail when an assert() in the code fails
66 function assert_callback( $script, $line, $message ) {
67 echo "assert failed: script ", $script," line ", $line," :";
70 printSimpleTrace(debug_backtrace());
73 $foo = assert_options( ASSERT_CALLBACK, 'assert_callback');
76 # Get error reporting to call back, too
78 // set the error reporting level for this script
79 error_reporting(E_ALL);
80 function myErrorHandler($errno, $errstr, $errfile, $errline)
82 echo "$errfile: $errline: error# $errno: $errstr\n";
85 printSimpleTrace(debug_backtrace());
89 // set to the user defined error handler
90 $old_error_handler = set_error_handler("myErrorHandler");
92 # This is the test DB backend
93 #require_once( 'lib/WikiDB/backend/cvs.php' );
95 $db_params['directory'] = $cur_dir . '/testbox';
96 $db_params['dbtype'] = 'file';
98 # Mock objects to allow tests to run
99 //require_once($rootdir.'lib/Request.php');
100 require_once($rootdir.'lib/WikiDB.php');
102 require_once($rootdir."lib/WikiUserNew.php");
104 require_once($rootdir."lib/WikiUser.php");
105 require_once($rootdir."lib/WikiGroup.php");
106 require_once($rootdir."lib/PagePerm.php");
109 function MockUser($name, $isSignedIn) {
110 $this->_name = $name;
111 $this->_isSignedIn = $isSignedIn;
116 function isSignedIn() {
122 function MockRequest(&$dbparams) {
123 global $WikiTheme, $request;
124 $this->_dbi = WikiDB::open($dbparams);
125 $this->_user = new MockUser("a_user", true);
126 $this->_group = WikiGroup::getGroup();
127 $this->_args = array('pagename' => 'HomePage', 'action' => 'browse');
130 function setArg($arg, $value) {
131 $this->_args[$arg] = $value;
133 function getArg($arg) {
135 if (array_key_exists($arg, $this->_args)) {
136 $result = $this->_args[$arg];
143 function getUser () {
144 if (isset($this->_user))
147 return $GLOBALS['ForbiddenUser'];
149 function getPage ($pagename = false) {
150 if (!isset($this->_dbi))
153 $pagename = $this->getArg('pagename');
154 return $this->_dbi->getPage($pagename);
156 function getPrefs () {
157 return $this->_prefs;
159 function getPref ($key) {
160 if (isset($this->_prefs))
161 return $this->_prefs->get($key);
163 function getGroup() {
164 return $this->_group;
168 $request = new MockRequest($db_params);
172 $request->_user = WikiUser('AnonUser');
174 $request->_user = new WikiUser($request, 'AnonUser');
175 $request->_prefs = $request->_user->getPreferences();
177 include_once("themes/" . THEME . "/themeinfo.php");
180 ####################################################################
182 # End of preamble, run the test suite ..
184 ####################################################################
187 require_once 'PHPUnit.php';
188 # lib/config.php might do a cwd()
189 require_once dirname(__FILE__).'/lib/InlineParserTest.php';
190 require_once dirname(__FILE__).'/lib/SetupWiki.php';
191 require_once dirname(__FILE__).'/lib/PageListTest.php';
192 require_once dirname(__FILE__).'/lib/DumpHtml.php';
193 require_once dirname(__FILE__).'/lib/plugin/ListPagesTest.php';
194 require_once dirname(__FILE__).'/lib/plugin/AllPagesTest.php';
195 require_once dirname(__FILE__).'/lib/plugin/AllUsersTest.php';
196 require_once dirname(__FILE__).'/lib/plugin/OrphanedPagesTest.php';
198 if (isset($HTTP_SERVER_VARS['REQUEST_METHOD']))
200 print "Run tests .. ";
202 $suite = new PHPUnit_TestSuite("phpwiki");
203 $suite->addTest( new PHPUnit_TestSuite("InlineParserTest") );
204 $suite->addTest( new PHPUnit_TestSuite("HtmlParserTest") );
205 $suite->addTest( new PHPUnit_TestSuite("PageListTest") );
206 $suite->addTest( new PHPUnit_TestSuite("ListPagesTest") );
207 $suite->addTest( new PHPUnit_TestSuite("SetupWiki") );
208 $suite->addTest( new PHPUnit_TestSuite("DumpHtml") );
209 $suite->addTest( new PHPUnit_TestSuite("AllPagesTest") );
210 $suite->addTest( new PHPUnit_TestSuite("AllUsersTest") );
211 $suite->addTest( new PHPUnit_TestSuite("OrphanedPagesTest") );
212 $result = PHPUnit::run($suite);
214 echo "ran " . $result->runCount() . " tests, " . $result->failureCount() . " failures.\n";
216 if ($result->failureCount() > 0) {
217 echo "More detail:\n";
218 echo $result->toString();
221 if (isset($HTTP_SERVER_VARS['REQUEST_METHOD']))
224 // (c-file-style: "gnu")
229 // c-hanging-comment-ender-p: nil
230 // indent-tabs-mode: nil