PHPUnit now sees individual parser tests as separate tests. This
[lhc/web/wiklou.git] / maintenance / tests / MediaWikiParserTest.php
1 <?php
2
3 global $IP;
4 define( "NO_COMMAND_LINE", 1 );
5 define( "PARSER_TESTS", "$IP/maintenance/parserTests.txt" );
6
7 require_once( "$IP/maintenance/parserTests.inc" );
8
9 class PHPUnitTestRecorder extends TestRecorder {
10
11 function record( $test, $result ) {
12 $this->total++;
13 $this->success += $result;
14
15 }
16
17 function reportPercentage( $success, $total ) {}
18 }
19
20 class MediaWikiParserTestSuite extends PHPUnit_Framework_TestSuite {
21 #implements PHPUnit_Framework_SelfDescribing {
22 static private $count;
23 static public $parser;
24 static public $iter;
25
26 public static function suite() {
27 $suite = new PHPUnit_Framework_TestSuite();
28
29 self::$iter = new TestFileIterator( PARSER_TESTS );
30
31 foreach(self::$iter as $i => $test) {
32 $suite->addTest(new ParserUnitTest($i, $test['test']));
33 self::$count++;
34 }
35 unset($tests);
36
37 self::$parser = new PTShell;
38 self::$iter->setParser(self::$parser);
39 self::$parser->recorder->start();
40 self::$parser->setupDatabase();
41 self::$iter->rewind();
42 /* } */
43 /* function setUp() { */
44 global $wgParser, $wgParserConf, $IP, $messageMemc, $wgMemc, $wgDeferredUpdateList,
45 $wgUser, $wgLang, $wgOut, $wgRequest, $wgStyleDirectory, $wgEnableParserCache,
46 $wgMessageCache, $wgUseDatabaseMessages, $wgMsgCacheExpiry, $parserMemc,
47 $wgNamespaceAliases, $wgNamespaceProtection, $wgLocalFileRepo,
48 $wgNamespacesWithSubpages, $wgThumbnailScriptPath, $wgScriptPath,
49 $wgArticlePath, $wgStyleSheetPath, $wgScript, $wgStylePath;
50
51 $wgScript = '/index.php';
52 $wgScriptPath = '/';
53 $wgArticlePath = '/wiki/$1';
54 $wgStyleSheetPath = '/skins';
55 $wgStylePath = '/skins';
56 $wgThumbnailScriptPath = false;
57 $wgLocalFileRepo = array(
58 'class' => 'LocalRepo',
59 'name' => 'local',
60 'directory' => '',
61 'url' => 'http://example.com/images',
62 'hashLevels' => 2,
63 'transformVia404' => false,
64 );
65 $wgNamespaceProtection[NS_MEDIAWIKI] = 'editinterface';
66 $wgNamespaceAliases['Image'] = NS_FILE;
67 $wgNamespaceAliases['Image_talk'] = NS_FILE_TALK;
68
69
70 $wgEnableParserCache = false;
71 $wgDeferredUpdateList = array();
72 $wgMemc =& wfGetMainCache();
73 $messageMemc =& wfGetMessageCacheStorage();
74 $parserMemc =& wfGetParserCacheStorage();
75
76 $wgContLang = new StubContLang;
77 $wgUser = new StubUser;
78 $wgLang = new StubUserLang;
79 $wgOut = new StubObject( 'wgOut', 'OutputPage' );
80 $wgParser = new StubObject( 'wgParser', $wgParserConf['class'], array( $wgParserConf ) );
81 $wgRequest = new WebRequest;
82
83 $wgMessageCache = new StubObject( 'wgMessageCache', 'MessageCache',
84 array( $messageMemc, $wgUseDatabaseMessages,
85 $wgMsgCacheExpiry, wfWikiID() ) );
86 if( $wgStyleDirectory === false) $wgStyleDirectory = "$IP/skins";
87
88 return $suite;
89 }
90
91 public function tearDown() {
92 $this->teardownDatabase();
93 $this->recorder->report();
94 $this->recorder->end();
95 $this->teardownUploadDir($this->uploadDir);
96 }
97
98 public function count() {return self::$count;}
99
100 public function toString() {
101 return "MediaWiki Parser Tests";
102 }
103
104
105 private $db;
106 private $uploadDir;
107 private $keepUploads;
108 /**
109 * Remove the dummy uploads directory
110 */
111 private function teardownUploadDir( $dir ) {
112 if ( $this->keepUploads ) {
113 return;
114 }
115
116 // delete the files first, then the dirs.
117 self::deleteFiles(
118 array (
119 "$dir/3/3a/Foobar.jpg",
120 "$dir/thumb/3/3a/Foobar.jpg/180px-Foobar.jpg",
121 "$dir/thumb/3/3a/Foobar.jpg/200px-Foobar.jpg",
122 "$dir/thumb/3/3a/Foobar.jpg/640px-Foobar.jpg",
123 "$dir/thumb/3/3a/Foobar.jpg/120px-Foobar.jpg",
124
125 "$dir/0/09/Bad.jpg",
126 )
127 );
128
129 self::deleteDirs(
130 array (
131 "$dir/3/3a",
132 "$dir/3",
133 "$dir/thumb/6/65",
134 "$dir/thumb/6",
135 "$dir/thumb/3/3a/Foobar.jpg",
136 "$dir/thumb/3/3a",
137 "$dir/thumb/3",
138
139 "$dir/0/09/",
140 "$dir/0/",
141
142 "$dir/thumb",
143 "$dir",
144 )
145 );
146 }
147
148 /**
149 * Delete the specified files, if they exist.
150 * @param array $files full paths to files to delete.
151 */
152 private static function deleteFiles( $files ) {
153 foreach( $files as $file ) {
154 if( file_exists( $file ) ) {
155 unlink( $file );
156 }
157 }
158 }
159 /**
160 * Delete the specified directories, if they exist. Must be empty.
161 * @param array $dirs full paths to directories to delete.
162 */
163 private static function deleteDirs( $dirs ) {
164 foreach( $dirs as $dir ) {
165 if( is_dir( $dir ) ) {
166 rmdir( $dir );
167 }
168 }
169 }
170
171 /**
172 * Create a dummy uploads directory which will contain a couple
173 * of files in order to pass existence tests.
174 * @return string The directory
175 */
176 private function setupUploadDir() {
177 global $IP;
178 if ( $this->keepUploads ) {
179 $dir = wfTempDir() . '/mwParser-images';
180 if ( is_dir( $dir ) ) {
181 return $dir;
182 }
183 } else {
184 $dir = wfTempDir() . "/mwParser-" . mt_rand() . "-images";
185 }
186
187 wfDebug( "Creating upload directory $dir\n" );
188 if ( file_exists( $dir ) ) {
189 wfDebug( "Already exists!\n" );
190 return $dir;
191 }
192 wfMkdirParents( $dir . '/3/3a' );
193 copy( "$IP/skins/monobook/headbg.jpg", "$dir/3/3a/Foobar.jpg" );
194
195 wfMkdirParents( $dir . '/0/09' );
196 copy( "$IP/skins/monobook/headbg.jpg", "$dir/0/09/Bad.jpg" );
197 return $dir;
198 }
199 }
200
201 class ParserUnitTest extends PHPUnit_Framework_TestCase {
202 private $number = 0;
203 private $test = "";
204
205 public function __construct($number, $test) {
206 $this->number = $number;
207 $this->test = $test;
208 }
209
210 function count() {return 1;}
211
212 public function run(PHPUnit_Framework_TestResult $result = NULL) {
213 PHPUnit_Framework_Assert::resetCount();
214 if ($result === NULL) {
215 $result = new PHPUnit_Framework_TestResult;
216 }
217
218 $t = MediaWikiParserTestSuite::$iter->current();
219 $k = MediaWikiParserTestSuite::$iter->key();
220
221 if(!MediaWikiParserTestSuite::$iter->valid()) {
222 return;
223 }
224
225 // The only way this should happen is if the parserTest.txt
226 // file were modified while the script is running.
227 if($k != $this->number) {
228 $i = $this->number;
229 wfDie("I got confused!\n");
230 }
231
232 $result->startTest($this);
233 PHPUnit_Util_Timer::start();
234
235 $r = false;
236 try {
237 $r = MediaWikiParserTestSuite::$parser->runTest(
238 $t['test'], $t['input'], $t['result'], $t['options'], $t['config']
239 );
240 PHPUnit_Framework_Assert::assertTrue(true, $t['test']);
241 }
242 catch (PHPUnit_Framework_AssertionFailedError $e) {
243 $result->addFailure($this, $e, PHPUnit_Util_Timer::stop());
244 }
245 catch (Exception $e) {
246 $result->addError($this, $e, PHPUnit_Util_Timer::stop());
247 }
248 PHPUnit_Framework_Assert::assertTrue(true, $t['test']);
249
250 $result->endTest($this, PHPUnit_Util_Timer::stop());
251
252 MediaWikiParserTestSuite::$parser->recorder->record($t['test'], $r);
253 MediaWikiParserTestSuite::$iter->next();
254 $this->addToAssertionCount(PHPUnit_Framework_Assert::getCount());
255
256 return $result;
257 }
258
259 }
260
261 class PTShell extends ParserTest {
262 function showTesting( $desc ) {
263 }
264
265 function showRunFile( $path ) {
266 }
267
268 function showSuccess( $desc ) {
269 PHPUnit_Framework_Assert::assertTrue(true, $desc);
270 return true;
271 }
272
273 function showFailure( $desc, $expected, $got ) {
274 PHPUnit_Framework_Assert::assertEquals($expected, $got, $desc);
275 }
276
277 }
278
279