Debian Codegraph @

tests/phpunit/maintenance/DumpTestCase.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Maintenance;
4
5 use DOMDocument;
6 use ExecutableFinder;
7 use MediaWiki\CommentStore\CommentStoreComment;
8 use MediaWiki\Content\Content;
9 use MediaWiki\MediaWikiServices;
10 use MediaWiki\Revision\RevisionAccessException;
11 use MediaWiki\Revision\RevisionRecord;
12 use MediaWiki\Revision\SlotRecord;
13 use MediaWikiLangTestCase;
14 use RuntimeException;
15 use WikiExporter;
16 use WikiPage;
17
18 /**
19 * Base TestCase for dumps
20 */
21 abstract class DumpTestCase extends MediaWikiLangTestCase {
22
23 /**
24 * exception to be rethrown once in sound PHPUnit surrounding
25 *
26 * As the current MediaWikiIntegrationTestCase::run is not robust enough to recover
27 * from thrown exceptions directly, we cannot throw frow within
28 * self::addDBData, although it would be appropriate. Hence, we catch the
29 * exception and store it until we are in setUp and may finally rethrow
30 * the exception without crashing the test suite.
31 *
32 * @var \Exception|null
33 */
34 protected $exceptionFromAddDBData = null;
35
36 /** @var bool|null Whether the 'gzip' utility is available */
37 protected static $hasGzip = null;
38
39 /**
40 * Skip the test if 'gzip' is not in $PATH.
41 *
42 * @return bool
43 */
44 protected function checkHasGzip() {
45 if ( self::$hasGzip === null ) {
46 self::$hasGzip = ( ExecutableFinder::findInDefaultPaths( 'gzip' ) !== false );
47 }
48
49 if ( !self::$hasGzip ) {
50 $this->markTestSkipped( "Skip test, requires the gzip utility in PATH" );
51 }
52
53 return self::$hasGzip;
54 }
55
56 /**
57 * Adds a revision to a page, while returning the resuting revision's id text id.
58 *
59 * @param WikiPage $page Page to add the revision to
60 * @param string $text Revisions text
61 * @param string $summary Revisions summary
62 * @param string $model The model ID (defaults to wikitext)
63 * @return array
64 */
65 protected function addRevision(
66 WikiPage $page,
67 $text,
68 $summary,
69 $model = CONTENT_MODEL_WIKITEXT
70 ) {
71 $contentHandler = $this->getServiceContainer()
72 ->getContentHandlerFactory()->getContentHandler( $model );
73
74 $content = $contentHandler->unserializeContent( $text );
75
76 $rev = $this->addMultiSlotRevision( $page, [ SlotRecord::MAIN => $content ], $summary );
77
78 if ( !$rev ) {
79 throw new RuntimeException( "Could not create revision" );
80 }
81
82 $text_id = $this->getSlotTextId( $rev->getSlot( SlotRecord::MAIN ) );
83 return [ $rev->getId(), $text_id, $rev ];
84 }
85
86 /**
87 * @param SlotRecord $slot
88 *
89 * @return string|null
90 */
91 protected function getSlotText( SlotRecord $slot ) {
92 try {
93 return $slot->getContent()->serialize();
94 } catch ( RevisionAccessException $ex ) {
95 return null;
96 }
97 }
98
99 /**
100 * @param SlotRecord $slot
101 *
102 * @return int
103 */
104 protected function getSlotTextId( SlotRecord $slot ) {
105 return (int)preg_replace( '/^tt:/', '', $slot->getAddress() );
106 }
107
108 /**
109 * @param SlotRecord $slot
110 *
111 * @return string
112 */
113 protected function getSlotFormat( SlotRecord $slot ) {
114 $contentHandler = $this->getServiceContainer()
115 ->getContentHandlerFactory()->getContentHandler( $slot->getModel() );
116
117 return $contentHandler->getDefaultFormat();
118 }
119
120 /**
121 * Adds a revision to a page, while returning the resulting revision's id and text id.
122 *
123 * @param WikiPage $page Page to add the revision to
124 * @param Content[] $slots A mapping of slot names to Content objects
125 * @param string $summary Revisions summary
126 * @return RevisionRecord
127 */
128 protected function addMultiSlotRevision(
129 WikiPage $page,
130 array $slots,
131 $summary
132 ) {
133 $slotRoleRegistry = MediaWikiServices::getInstance()->getSlotRoleRegistry();
134
135 $updater = $page->newPageUpdater( $this->getTestUser()->getUser() );
136
137 foreach ( $slots as $role => $content ) {
138 if ( !$slotRoleRegistry->isDefinedRole( $role ) ) {
139 $slotRoleRegistry->defineRoleWithModel( $role, $content->getModel() );
140 }
141
142 $updater->setContent( $role, $content );
143 }
144
145 $updater->saveRevision( CommentStoreComment::newUnsavedComment( trim( $summary ) ) );
146 return $updater->getNewRevision();
147 }
148
149 /**
150 * gunzips the given file and stores the result in the original file name
151 *
152 * @param string $fname Filename to read the gzipped data from and stored
153 * the gunzipped data into
154 */
155 protected function gunzip( $fname ) {
156 $gzipped_contents = file_get_contents( $fname );
157 if ( $gzipped_contents === false ) {
158 $this->fail( "Could not get contents of $fname" );
159 }
160
161 $contents = gzdecode( $gzipped_contents );
162
163 $this->assertEquals(
164 strlen( $contents ),
165 file_put_contents( $fname, $contents ),
166 '# bytes written'
167 );
168 }
169
170 public static function setUpBeforeClass(): void {
171 parent::setUpBeforeClass();
172
173 if ( !function_exists( 'libxml_set_external_entity_loader' ) ) {
174 return;
175 }
176
177 // The W3C is intentionally slow about returning schema files,
178 // see <https://www.w3.org/Help/Webmaster#slowdtd>.
179 // To work around that, we keep our own copies of the relevant schema files.
180 libxml_set_external_entity_loader(
181 static function ( $public, $system, $context ) {
182 switch ( $system ) {
183 // if more schema files are needed, add them here.
184 case 'http://www.w3.org/2001/xml.xsd':
185 $file = __DIR__ . '/xml.xsd';
186 break;
187 default:
188 if ( is_file( $system ) ) {
189 $file = $system;
190 } else {
191 return null;
192 }
193 }
194
195 return $file;
196 }
197 );
198 }
199
200 /**
201 * Default set up function.
202 *
203 * Reports errors from addDBData to PHPUnit
204 */
205 protected function setUp(): void {
206 parent::setUp();
207
208 // Check if any Exception is stored for rethrowing from addDBData
209 // @see self::exceptionFromAddDBData
210 if ( $this->exceptionFromAddDBData !== null ) {
211 throw $this->exceptionFromAddDBData;
212 }
213 }
214
215 /**
216 * Returns the path to the XML schema file for the given schema version.
217 *
218 * @param string|null $schemaVersion
219 *
220 * @return string
221 */
222 protected function getXmlSchemaPath( $schemaVersion = null ) {
223 global $IP, $wgXmlDumpSchemaVersion;
224
225 $schemaVersion = $schemaVersion ?: $wgXmlDumpSchemaVersion;
226
227 return "$IP/docs/export-$schemaVersion.xsd";
228 }
229
230 /**
231 * Checks for test output consisting only of lines containing ETA announcements
232 */
233 protected function expectETAOutput() {
234 // Newer PHPUnits require assertion about the output using PHPUnit's own
235 // expectOutput[...] functions. However, the PHPUnit shipped prediactes
236 // do not allow to check /each/ line of the output using /readable/ REs.
237 // So we ...
238
239 // 1. ... add a dummy output checking to make PHPUnit not complain
240 // about unchecked test output
241 $this->expectOutputRegex( '//' );
242
243 // 2. Do the real output checking on our own.
244 $lines = explode( "\n", $this->getActualOutput() );
245 $this->assertGreaterThan( 1, count( $lines ), "Minimal lines of produced output" );
246 $this->assertSame( '', array_pop( $lines ), "Output ends in LF" );
247 $timestamp_re = "[0-9]{4}-[01][0-9]-[0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-6][0-9]";
248 foreach ( $lines as $line ) {
249 $this->assertMatchesRegularExpression(
250 "/$timestamp_re: .* \(ID [0-9]+\) [0-9]* pages .*, [0-9]* revs .*, ETA/",
251 $line
252 );
253 }
254 }
255
256 /**
257 * @param null|string $schemaVersion
258 *
259 * @return DumpAsserter
260 */
261 protected function getDumpAsserter( $schemaVersion = null ) {
262 $schemaVersion = $schemaVersion ?: WikiExporter::schemaVersion();
263 return new DumpAsserter( $schemaVersion );
264 }
265
266 /**
267 * Checks an XML file against an XSD schema.
268 * @param string $fname
269 * @param string $schemaFile
270 */
271 protected function assertDumpSchema( $fname, $schemaFile ) {
272 if ( !function_exists( 'libxml_use_internal_errors' ) ) {
273 // Would be nice to leave a warning somehow.
274 // We don't want to skip all of the test case that calls this, though.
275 $this->markAsRisky();
276 return;
277 }
278 $xml = new DOMDocument();
279 $this->assertTrue( $xml->load( $fname ),
280 "Opening temporary file $fname via DOMDocument failed" );
281
282 // Don't throw
283 $oldLibXmlInternalErrors = libxml_use_internal_errors( true );
284
285 // NOTE: if this reports "Invalid Schema", the schema may be referencing an external
286 // entity (typically, another schema) that needs to be mapped in the
287 // libxml_set_external_entity_loader callback defined in setUpBeforeClass() above!
288 // Or $schemaFile doesn't point to a schema file, or the schema is indeed just broken.
289 if ( !$xml->schemaValidate( $schemaFile ) ) {
290 $errorText = '';
291
292 foreach ( libxml_get_errors() as $error ) {
293 $errorText .= "\nline {$error->line}: {$error->message}";
294 }
295
296 libxml_clear_errors();
297
298 $this->fail(
299 "Failed asserting that $fname conforms to the schema in $schemaFile:\n$errorText"
300 );
301 }
302
303 libxml_use_internal_errors( $oldLibXmlInternalErrors );
304 }
305
306 }