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.
30 ####################################################################
32 # Preamble needed to get the tests to run.
34 ####################################################################
38 # Add root dir to the path
39 $rootdir = $cur_dir . '/../../';
40 $ini_sep = substr(PHP_OS,0,3) == 'WIN' ? ';' : ':';
41 ini_set('include_path', ini_get('include_path') . $ini_sep . $rootdir);
43 # This quiets a warning in config.php
44 $HTTP_SERVER_VARS['REMOTE_ADDR'] = '127.0.0.1';
47 require_once $rootdir.'index.php';
48 require_once $rootdir.'lib/stdlib.php';
50 $user_theme = 'default';
51 require_once("themes/$user_theme/themeinfo.php"); // Needed for $Theme
53 function printSimpleTrace($bt) {
55 foreach ($bt as $i => $elem) {
56 if (!array_key_exists('file', $elem)) {
59 print " " . $elem['file'] . ':' . $elem['line'] . "\n";
63 # Show lots of detail when an assert() in the code fails
64 function assert_callback( $script, $line, $message ) {
65 echo "assert failed: script ", $script," line ", $line," :";
68 printSimpleTrace(debug_backtrace());
71 $foo = assert_options( ASSERT_CALLBACK, 'assert_callback');
74 # Get error reporting to call back, too
76 // set the error reporting level for this script
77 error_reporting(E_ALL);
78 function myErrorHandler($errno, $errstr, $errfile, $errline)
80 echo "$errfile: $errline: error# $errno: $errstr\n";
83 printSimpleTrace(debug_backtrace());
87 // set to the user defined error handler
88 $old_error_handler = set_error_handler("myErrorHandler");
90 # This is the test DB backend
91 #require_once( 'lib/WikiDB/backend/cvs.php' );
93 $db_params['directory'] = $cur_dir . '/testbox';
94 $db_params['dbtype'] = 'file';
96 # Mock objects to allow tests to run
97 //require_once($rootdir.'lib/Request.php');
98 require_once($rootdir.'lib/WikiDB.php');
100 require_once($rootdir."lib/WikiUserNew.php");
102 require_once($rootdir."lib/WikiUser.php");
103 require_once($rootdir."lib/WikiGroup.php");
104 require_once($rootdir."lib/PagePerm.php");
107 function MockUser($name, $isSignedIn) {
108 $this->_name = $name;
109 $this->_isSignedIn = $isSignedIn;
114 function isSignedIn() {
120 function MockRequest(&$dbparams) {
121 global $WikiTheme, $request;
122 $this->_dbi = WikiDB::open($dbparams);
123 $this->_user = new MockUser("a_user", true);
124 $this->_args = array('pagename' => 'HomePage', 'action' => 'browse');
127 function setArg($arg, $value) {
128 $this->_args[$arg] = $value;
130 function getArg($arg) {
132 if (array_key_exists($arg, $this->_args)) {
133 $result = $this->_args[$arg];
140 function getUser () {
141 if (isset($this->_user))
144 return $GLOBALS['ForbiddenUser'];
146 function getPage ($pagename = false) {
147 if (!isset($this->_dbi))
150 $pagename = $this->getArg('pagename');
151 return $this->_dbi->getPage($pagename);
153 function getPrefs () {
154 return $this->_prefs;
156 function getPref ($key) {
157 if (isset($this->_prefs))
158 return $this->_prefs->get($key);
160 function getGroup() {
161 return WikiGroup::getGroup();
165 $request = new MockRequest($db_params);
169 $request->_user = WikiUser('AnonUser');
171 $request->_user = new WikiUser($request, 'AnonUser');
172 $request->_prefs = $request->_user->getPreferences();
174 include_once("themes/" . THEME . "/themeinfo.php");
177 ####################################################################
179 # End of preamble, run the test suite ..
181 ####################################################################
184 require_once 'PHPUnit.php';
185 # lib/config.php might do a cwd()
186 require_once dirname(__FILE__).'/lib/InlineParserTest.php';
187 require_once dirname(__FILE__).'/lib/PageListTest.php';
188 require_once dirname(__FILE__).'/lib/plugin/ListPagesTest.php';
189 require_once dirname(__FILE__).'/lib/plugin/AllPagesTest.php';
190 require_once dirname(__FILE__).'/lib/plugin/AllUsersTest.php';
191 require_once dirname(__FILE__).'/lib/plugin/OrphanedPagesTest.php';
193 if (isset($HTTP_SERVER_VARS['REQUEST_METHOD']))
195 print "Run tests .. ";
197 $suite = new PHPUnit_TestSuite("phpwiki");
198 $suite->addTest( new PHPUnit_TestSuite("InlineParserTest") );
199 $suite->addTest( new PHPUnit_TestSuite("HtmlParserTest") );
200 $suite->addTest( new PHPUnit_TestSuite("PageListTest") );
201 $suite->addTest( new PHPUnit_TestSuite("ListPagesTest") );
202 $suite->addTest( new PHPUnit_TestSuite("AllPagesTest") );
203 $suite->addTest( new PHPUnit_TestSuite("AllUsersTest") );
204 $suite->addTest( new PHPUnit_TestSuite("OrphanedPagesTest") );
205 $result = PHPUnit::run($suite);
207 echo "ran " . $result->runCount() . " tests, " . $result->failureCount() . " failures.\n";
209 if ($result->failureCount() > 0) {
210 echo "More detail:\n";
211 echo $result->toString();
214 if (isset($HTTP_SERVER_VARS['REQUEST_METHOD']))
217 // (c-file-style: "gnu")
222 // c-hanging-comment-ender-p: nil
223 // indent-tabs-mode: nil