]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - tests/unit/test.php
Properly save and restore the request args for each test.
[SourceForge/phpwiki.git] / tests / unit / test.php
1 #!/usr/local/bin/php -Cq
2 <?php  
3 /* Copyright (C) 2004 Dan Frankowski <dfrankow@cs.umn.edu>
4  * Copyright (C) 2004 Reini Urban <rurban@x-ray.at>
5  *
6  * This file is part of PhpWiki.
7  * 
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.
12  * 
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.
17  * 
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
21  */
22
23 /**
24  * Unit tests for PhpWiki. 
25  *
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.
30  */
31
32 ####################################################################
33 #
34 # Preamble needed to get the tests to run.
35 #
36 ####################################################################
37
38
39 $cur_dir = getcwd();
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);
46
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);
53
54 # Other needed files
55 require_once $rootdir.'index.php';
56 require_once $rootdir.'lib/main.php';
57
58 define('DEBUG', _DEBUG_TRACE);
59
60 function printSimpleTrace($bt) {
61     //print_r($bt);
62     echo "Traceback:\n";
63     foreach ($bt as $i => $elem) {
64         if (!array_key_exists('file', $elem)) {
65             continue;
66         }
67         print "  " . $elem['file'] . ':' . $elem['line'] . "\n";
68     }
69 }
70
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," :";
74    echo "$message";
75    echo "Traceback:\n";
76    printSimpleTrace(debug_backtrace());
77    exit;
78 }
79 $foo = assert_options( ASSERT_CALLBACK, 'assert_callback');
80
81 #
82 # Get error reporting to call back, too
83 #
84 // set the error reporting level for this script
85 error_reporting(E_ALL);
86 // This is too strict, fails on every notice and warning. 
87 /*
88 function myErrorHandler$errno, $errstr, $errfile, $errline) {
89    echo "$errfile: $errline: error# $errno: $errstr\n";
90    echo "Traceback:\n";
91    printSimpleTrace(debug_backtrace());
92 }
93 // The ErrorManager version
94 function _ErrorHandler_CB(&$error) {
95    echo "Traceback:\n";
96    printSimpleTrace(debug_backtrace());
97    if ($error->isFatal()) {
98         $error->errno = E_USER_WARNING;
99         return true; // ignore error
100    }
101    return true;
102 }
103 // set to the user defined error handler
104 // $old_error_handler = set_error_handler("myErrorHandler");
105 // This is already done via _DEBUG_TRACE
106 //$ErrorManager->pushErrorHandler(new WikiFunctionCb('_ErrorHandler_CB'));
107 */
108
109 # This is the test DB backend
110 $db_params                         = array();
111 $db_params['directory']            = $cur_dir . '/.testbox';
112 $db_params['dbtype']               = 'file';
113
114 if (ENABLE_USER_NEW) {
115     class MockUser extends _WikiUser {
116         function MockUser($name, $isSignedIn) {
117             $this->_userid = $name;
118             $this->_isSignedIn = $isSignedIn;
119         }
120         function isSignedIn() {
121             return $this->_isSignedIn;
122         }
123     }
124 } else {
125     class MockUser extends WikiUser {
126         function MockUser($name, $isSignedIn) {
127             $this->_userid = $name;
128             $this->_isSignedIn = $isSignedIn;
129         }
130         function isSignedIn() {
131             return $this->_isSignedIn;
132         }
133     }
134 }
135
136 //FIXME: ignore cached requests (if-modified-since) from cli
137 class MockRequest extends WikiRequest {
138     function MockRequest(&$dbparams) {
139         $this->_dbi = WikiDB::open($dbparams);
140         $this->_user = new MockUser("a_user", true);
141         $this->_group = WikiGroup::getGroup();
142         $this->_args = array('pagename' => 'HomePage', 'action' => 'browse');
143         $this->Request();
144     }
145     function getGroup() {
146         if (is_object($this->_group))
147             return $this->_group;
148         else // FIXME: this is set to "/f:" somewhere.
149             return WikiGroup::getGroup();
150     }
151 }
152
153 function purge_dir($dir) {
154     static $finder;
155     if (!isset($finder)) {
156         $finder = new FileFinder;
157     }
158     $fileSet = new fileSet($dir);
159     assert($dir);
160     foreach ($fileSet->getFiles() as $f) {
161         unlink("$dir/$f");
162     }
163 }
164
165 function purge_testbox() {
166     global $db_params;  
167     $dir = $db_params['directory'];
168     assert($dir);
169     foreach (array('latest_ver','links','page_data','ver_data') as $d) {
170         purge_dir("$dir/$d");
171     }
172     if (isset($GLOBALS['request'])) {
173         $dbi = $GLOBALS['request']->getDbh();
174         $dbi->_cache->close();
175         $dbi->_backend->_latest_versions = array();
176     }
177 }
178
179 global $ErrorManager;
180 $ErrorManager->setPostponedErrorMask(E_FATAL|E_NOTICE|E_USER_NOTICE|E_USER_WARNING|E_WARNING);
181 $request = new MockRequest($db_params);
182
183 /*
184 if (ENABLE_USER_NEW)
185     $request->_user = WikiUser('AnonUser');
186 else {
187     $request->_user = new WikiUser($request, 'AnonUser');
188     $request->_prefs = $request->_user->getPreferences();
189 }
190 */
191 include_once("themes/" . THEME . "/themeinfo.php");
192
193 ####################################################################
194 #
195 # End of preamble, run the test suite ..
196 #
197 ####################################################################
198
199 # Test files
200 require_once 'PHPUnit.php';
201 # lib/config.php might do a cwd()
202
203 if (isset($HTTP_SERVER_VARS['REQUEST_METHOD']))
204     echo "<pre>\n";
205 // purge the testbox
206     
207 print "Run tests .. ";
208
209 $suite  = new PHPUnit_TestSuite("phpwiki");
210
211 // save and restore all args for each test.
212 class phpwiki_TestCase extends PHPUnit_TestCase {
213     function setUp() { 
214         global $request;
215         $this->_savedargs = $request->_args;
216         $request->_args = array();
217     }
218     function tearDown() {
219         global $request;
220         $request->_args = $this->_savedargs;
221     }
222 }
223
224 // use argv (from cli) or tests (from browser) params to run only certain tests
225 $alltests = array('InlineParserTest','HtmlParserTest','PageListTest','ListPagesTest',
226                   'SetupWiki','DumpHtml','AllPagesTest','AllUsersTest','OrphanedPagesTest');
227 if (isset($HTTP_SERVER_VARS['REQUEST_METHOD']) and !empty($HTTP_GET_VARS['tests']))
228     $argv = explode(',',$HTTP_GET_VARS['tests']);
229 if (!empty($argv) and preg_match("/test\.php$/", $argv[0]))
230     array_shift($argv);
231 if (!empty($argv)) {
232     $runtests = array();
233     foreach ($argv as $test) {
234         if (in_array($test,$alltests))
235             $runtests[] = $test;
236     }
237     $alltests = $runtests;
238     print_r($runtests);
239     flush();
240 }
241
242 foreach ($alltests as $test) {
243     if (file_exists(dirname(__FILE__).'/lib/'.$test.'.php'))
244         require_once dirname(__FILE__).'/lib/'.$test.'.php';
245     else    
246         require_once dirname(__FILE__).'/lib/plugin/'.$test.'.php';
247     $suite->addTest( new PHPUnit_TestSuite($test) );
248 }
249 $result = PHPUnit::run($suite); 
250
251 echo "ran " . $result->runCount() . " tests, " . $result->failureCount() . " failures.\n";
252 flush();
253
254 if ($result->failureCount() > 0) {
255     echo "More detail:\n";
256     echo $result->toString();
257 }
258
259 if (isset($HTTP_SERVER_VARS['REQUEST_METHOD']))
260     echo "</pre>\n";
261
262 // (c-file-style: "gnu")
263 // Local Variables:
264 // mode: php
265 // tab-width: 8
266 // c-basic-offset: 4
267 // c-hanging-comment-ender-p: nil
268 // indent-tabs-mode: nil
269 // End:   
270 ?>