]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - tests/unit/test.php
- printSimpleTrace() to print out a nice stack trace
[SourceForge/phpwiki.git] / tests / unit / test.php
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>
4  *
5  * This file is part of PhpWiki.
6  * 
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.
11  * 
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.
16  * 
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
20  */
21
22 /**
23  * Unit tests for PhpWiki. 
24  *
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  */
29
30 ####################################################################
31 #
32 # Preamble needed to get the tests to run.
33 #
34 ####################################################################
35
36
37 $cur_dir = getcwd();
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);
42
43 # This quiets a warning in config.php
44 $HTTP_SERVER_VARS['REMOTE_ADDR'] = '127.0.0.1';
45
46 # Other needed files
47 require_once $rootdir.'index.php';
48 require_once $rootdir.'lib/stdlib.php';
49
50 $user_theme = 'default';
51 require_once("themes/$user_theme/themeinfo.php");  // Needed for $Theme
52
53 function printSimpleTrace($bt) {
54     //print_r($bt);
55     foreach ($bt as $i => $elem) {
56         if (!array_key_exists('file', $elem)) {
57             continue;
58         }
59         print "  " . $elem['file'] . ':' . $elem['line'] . "\n";
60     }
61 }
62
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," :";
66    echo "$message";
67    echo "Traceback:\n";
68    printSimpleTrace(debug_backtrace());
69    exit;
70 }
71 $foo = assert_options( ASSERT_CALLBACK, 'assert_callback');
72
73 #
74 # Get error reporting to call back, too
75 #
76 // set the error reporting level for this script
77 error_reporting(E_ALL);
78 function myErrorHandler($errno, $errstr, $errfile, $errline)
79 {
80    echo "$errfile: $errline: error# $errno: $errstr\n";
81    // Back trace
82    echo "Traceback:\n";
83    printSimpleTrace(debug_backtrace());
84    exit;
85 }
86
87 // set to the user defined error handler
88 $old_error_handler = set_error_handler("myErrorHandler");
89
90 # This is the test DB backend
91 #require_once( 'lib/WikiDB/backend/cvs.php' );
92 $db_params                         = array();
93 $db_params['directory']            = $cur_dir . '/testbox';
94 $db_params['dbtype']               = 'file';
95
96 # Mock objects to allow tests to run
97 //require_once($rootdir.'lib/Request.php');
98 require_once($rootdir.'lib/WikiDB.php');
99 if (ENABLE_USER_NEW)
100     require_once($rootdir."lib/WikiUserNew.php");
101 else
102     require_once($rootdir."lib/WikiUser.php"); 
103 require_once($rootdir."lib/WikiGroup.php");
104 require_once($rootdir."lib/PagePerm.php");
105
106 class MockUser {
107     function MockUser($name, $isSignedIn) {
108         $this->_name = $name;
109         $this->_isSignedIn = $isSignedIn;
110     }
111     function getId() {
112         return $this->_name;
113     }
114     function isSignedIn() {
115         return true;
116     }
117 }
118
119 class MockRequest {
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');
125         //$this->Request();
126     }
127     function setArg($arg, $value) {
128         $this->_args[$arg] = $value;
129     }
130     function getArg($arg) {
131         $result = null;
132         if (array_key_exists($arg, $this->_args)) {
133             $result = $this->_args[$arg];
134         }
135         return $result;
136     }
137     function getDbh() {
138         return $this->_dbi;
139     }
140     function getUser () {
141         if (isset($this->_user))
142             return $this->_user;
143         else
144             return $GLOBALS['ForbiddenUser'];
145     }
146     function getPage ($pagename = false) {
147         if (!isset($this->_dbi))
148             $this->getDbh();
149         if (!$pagename) 
150             $pagename = $this->getArg('pagename');
151         return $this->_dbi->getPage($pagename);
152     }
153     function getPrefs () {
154         return $this->_prefs;
155     }
156     function getPref ($key) {
157         if (isset($this->_prefs))
158             return $this->_prefs->get($key);
159     }
160     function getGroup() {
161         return WikiGroup::getGroup();
162     }
163 }
164
165 $request = new MockRequest($db_params);
166
167 /*
168 if (ENABLE_USER_NEW)
169     $request->_user = WikiUser('AnonUser');
170 else {
171     $request->_user = new WikiUser($request, 'AnonUser');
172     $request->_prefs = $request->_user->getPreferences();
173 }
174 include_once("themes/" . THEME . "/themeinfo.php");
175 */
176
177 ####################################################################
178 #
179 # End of preamble, run the test suite ..
180 #
181 ####################################################################
182
183 # Test files
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'; 
192
193 if (isset($HTTP_SERVER_VARS['REQUEST_METHOD']))
194     echo "<pre>\n";
195 print "Run tests .. ";
196
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); 
206
207 echo "ran " . $result->runCount() . " tests, " . $result->failureCount() . " failures.\n";
208
209 if ($result->failureCount() > 0) {
210     echo "More detail:\n";
211     echo $result->toString();
212 }
213
214 if (isset($HTTP_SERVER_VARS['REQUEST_METHOD']))
215     echo "</pre>\n";
216
217 // (c-file-style: "gnu")
218 // Local Variables:
219 // mode: php
220 // tab-width: 8
221 // c-basic-offset: 4
222 // c-hanging-comment-ender-p: nil
223 // indent-tabs-mode: nil
224 // End:   
225 ?>