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