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