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