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