4 * @group ContentHandler
6 class RevisionTest
extends MediaWikiTestCase
{
8 protected function setUp() {
13 $this->setMwGlobals( [
14 'wgContLang' => Language
::factory( 'en' ),
15 'wgLanguageCode' => 'en',
16 'wgLegacyEncoding' => false,
17 'wgCompressRevisions' => false,
19 'wgContentHandlerTextFallback' => 'ignore',
22 $this->mergeMwGlobalArrayValue(
26 12313 => 'Dummy_talk',
30 $this->mergeMwGlobalArrayValue(
31 'wgNamespaceContentModels',
37 $this->mergeMwGlobalArrayValue(
40 'testing' => 'DummyContentHandlerForTesting',
41 'RevisionTestModifyableContent' => 'RevisionTestModifyableContentHandler',
45 MWNamespace
::clearCaches();
46 // Reset namespace cache
47 $wgContLang->resetNamespaces();
50 protected function tearDown() {
53 MWNamespace
::clearCaches();
54 // Reset namespace cache
55 $wgContLang->resetNamespaces();
60 public function provideConstruct() {
61 yield
'with text' => [
63 'text' => 'hello world.',
64 'content_model' => CONTENT_MODEL_JAVASCRIPT
67 yield
'with content' => [
69 'content' => ContentHandler
::makeContent(
71 Title
::newFromText( 'RevisionTest_testConstructWithContent' ),
72 CONTENT_MODEL_JAVASCRIPT
79 * @dataProvider provideConstruct
81 public function testConstruct( $rowArray ) {
82 $rev = new Revision( $rowArray );
83 $this->assertNotNull( $rev->getContent(), 'no content object available' );
84 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContent()->getModel() );
85 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContentModel() );
88 public function provideGetRevisionText() {
89 yield
'Generic test' => [
90 'This is a goat of revision text.',
93 'old_text' => 'This is a goat of revision text.',
99 * @covers Revision::getRevisionText
100 * @dataProvider provideGetRevisionText
102 public function testGetRevisionText( $expected, $rowData, $prefix = 'old_', $wiki = false ) {
105 Revision
::getRevisionText( (object)$rowData, $prefix, $wiki ) );
108 public function provideGetRevisionTextWithZlibExtension() {
109 yield
'Generic gzip test' => [
110 'This is a small goat of revision text.',
112 'old_flags' => 'gzip',
113 'old_text' => gzdeflate( 'This is a small goat of revision text.' ),
119 * @covers Revision::getRevisionText
120 * @dataProvider provideGetRevisionTextWithZlibExtension
122 public function testGetRevisionWithZlibExtension( $expected, $rowData ) {
123 $this->checkPHPExtension( 'zlib' );
124 $this->testGetRevisionText( $expected, $rowData );
127 public function provideGetRevisionTextWithLegacyEncoding() {
128 yield
'Utf8Native' => [
129 "Wiki est l'\xc3\xa9cole superieur !",
132 'old_flags' => 'utf-8',
133 'old_text' => "Wiki est l'\xc3\xa9cole superieur !",
136 yield
'Utf8Legacy' => [
137 "Wiki est l'\xc3\xa9cole superieur !",
141 'old_text' => "Wiki est l'\xe9cole superieur !",
147 * @covers Revision::getRevisionText
148 * @dataProvider provideGetRevisionTextWithLegacyEncoding
150 public function testGetRevisionWithLegacyEncoding( $expected, $encoding, $rowData ) {
151 $GLOBALS['wgLegacyEncoding'] = $encoding;
152 $this->testGetRevisionText( $expected, $rowData );
155 public function provideGetRevisionTextWithGzipAndLegacyEncoding() {
156 yield
'Utf8NativeGzip' => [
157 "Wiki est l'\xc3\xa9cole superieur !",
160 'old_flags' => 'gzip,utf-8',
161 'old_text' => gzdeflate( "Wiki est l'\xc3\xa9cole superieur !" ),
164 yield
'Utf8LegacyGzip' => [
165 "Wiki est l'\xc3\xa9cole superieur !",
168 'old_flags' => 'gzip',
169 'old_text' => gzdeflate( "Wiki est l'\xe9cole superieur !" ),
175 * @covers Revision::getRevisionText
176 * @dataProvider provideGetRevisionTextWithGzipAndLegacyEncoding
178 public function testGetRevisionWithGzipAndLegacyEncoding( $expected, $encoding, $rowData ) {
179 $this->checkPHPExtension( 'zlib' );
180 $GLOBALS['wgLegacyEncoding'] = $encoding;
181 $this->testGetRevisionText( $expected, $rowData );
185 * @covers Revision::compressRevisionText
187 public function testCompressRevisionTextUtf8() {
189 $row->old_text
= "Wiki est l'\xc3\xa9cole superieur !";
190 $row->old_flags
= Revision
::compressRevisionText( $row->old_text
);
191 $this->assertTrue( false !== strpos( $row->old_flags
, 'utf-8' ),
192 "Flags should contain 'utf-8'" );
193 $this->assertFalse( false !== strpos( $row->old_flags
, 'gzip' ),
194 "Flags should not contain 'gzip'" );
195 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
196 $row->old_text
, "Direct check" );
197 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
198 Revision
::getRevisionText( $row ), "getRevisionText" );
202 * @covers Revision::compressRevisionText
204 public function testCompressRevisionTextUtf8Gzip() {
205 $this->checkPHPExtension( 'zlib' );
206 $this->setMwGlobals( 'wgCompressRevisions', true );
209 $row->old_text
= "Wiki est l'\xc3\xa9cole superieur !";
210 $row->old_flags
= Revision
::compressRevisionText( $row->old_text
);
211 $this->assertTrue( false !== strpos( $row->old_flags
, 'utf-8' ),
212 "Flags should contain 'utf-8'" );
213 $this->assertTrue( false !== strpos( $row->old_flags
, 'gzip' ),
214 "Flags should contain 'gzip'" );
215 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
216 gzinflate( $row->old_text
), "Direct check" );
217 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
218 Revision
::getRevisionText( $row ), "getRevisionText" );
222 * @param string $text
223 * @param string $title
224 * @param string $model
225 * @param string $format
229 private function newTestRevision( $text, $title = "Test",
230 $model = CONTENT_MODEL_WIKITEXT
, $format = null
232 if ( is_string( $title ) ) {
233 $title = Title
::newFromText( $title );
236 $content = ContentHandler
::makeContent( $text, $title, $model, $format );
244 'content' => $content,
245 'length' => $content->getSize(),
246 'comment' => "testing",
247 'minor_edit' => false,
249 'content_format' => $format,
256 public function provideGetContentModel() {
257 // NOTE: we expect the help namespace to always contain wikitext
259 [ 'hello world', 'Help:Hello', null, null, CONTENT_MODEL_WIKITEXT
],
260 [ 'hello world', 'User:hello/there.css', null, null, CONTENT_MODEL_CSS
],
261 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, "testing" ],
267 * @dataProvider provideGetContentModel
268 * @covers Revision::getContentModel
270 public function testGetContentModel( $text, $title, $model, $format, $expectedModel ) {
271 $rev = $this->newTestRevision( $text, $title, $model, $format );
273 $this->assertEquals( $expectedModel, $rev->getContentModel() );
276 public function provideGetContentFormat() {
277 // NOTE: we expect the help namespace to always contain wikitext
279 [ 'hello world', 'Help:Hello', null, null, CONTENT_FORMAT_WIKITEXT
],
280 [ 'hello world', 'Help:Hello', CONTENT_MODEL_CSS
, null, CONTENT_FORMAT_CSS
],
281 [ 'hello world', 'User:hello/there.css', null, null, CONTENT_FORMAT_CSS
],
282 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, "testing" ],
288 * @dataProvider provideGetContentFormat
289 * @covers Revision::getContentFormat
291 public function testGetContentFormat( $text, $title, $model, $format, $expectedFormat ) {
292 $rev = $this->newTestRevision( $text, $title, $model, $format );
294 $this->assertEquals( $expectedFormat, $rev->getContentFormat() );
297 public function provideGetContentHandler() {
298 // NOTE: we expect the help namespace to always contain wikitext
300 [ 'hello world', 'Help:Hello', null, null, 'WikitextContentHandler' ],
301 [ 'hello world', 'User:hello/there.css', null, null, 'CssContentHandler' ],
302 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, 'DummyContentHandlerForTesting' ],
308 * @dataProvider provideGetContentHandler
309 * @covers Revision::getContentHandler
311 public function testGetContentHandler( $text, $title, $model, $format, $expectedClass ) {
312 $rev = $this->newTestRevision( $text, $title, $model, $format );
314 $this->assertEquals( $expectedClass, get_class( $rev->getContentHandler() ) );
317 public function provideGetContent() {
318 // NOTE: we expect the help namespace to always contain wikitext
320 [ 'hello world', 'Help:Hello', null, null, Revision
::FOR_PUBLIC
, 'hello world' ],
322 serialize( 'hello world' ),
326 Revision
::FOR_PUBLIC
,
327 serialize( 'hello world' )
330 serialize( 'hello world' ),
334 Revision
::FOR_PUBLIC
,
335 serialize( 'hello world' )
342 * @dataProvider provideGetContent
343 * @covers Revision::getContent
345 public function testGetContent( $text, $title, $model, $format,
346 $audience, $expectedSerialization
348 $rev = $this->newTestRevision( $text, $title, $model, $format );
349 $content = $rev->getContent( $audience );
352 $expectedSerialization,
353 is_null( $content ) ?
null : $content->serialize( $format )
357 public function provideGetSize() {
359 [ "hello world.", CONTENT_MODEL_WIKITEXT
, 12 ],
360 [ serialize( "hello world." ), "testing", 12 ],
365 * @covers Revision::getSize
367 * @dataProvider provideGetSize
369 public function testGetSize( $text, $model, $expected_size ) {
370 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSize', $model );
371 $this->assertEquals( $expected_size, $rev->getSize() );
374 public function provideGetSha1() {
376 [ "hello world.", CONTENT_MODEL_WIKITEXT
, Revision
::base36Sha1( "hello world." ) ],
378 serialize( "hello world." ),
380 Revision
::base36Sha1( serialize( "hello world." ) )
386 * @covers Revision::getSha1
388 * @dataProvider provideGetSha1
390 public function testGetSha1( $text, $model, $expected_hash ) {
391 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSha1', $model );
392 $this->assertEquals( $expected_hash, $rev->getSha1() );
396 * Tests whether $rev->getContent() returns a clone when needed.
399 * @covers Revision::getContent
401 public function testGetContentClone() {
402 $content = new RevisionTestModifyableContent( "foo" );
408 'title' => Title
::newFromText( "testGetContentClone_dummy" ),
410 'content' => $content,
411 'length' => $content->getSize(),
412 'comment' => "testing",
413 'minor_edit' => false,
417 /** @var RevisionTestModifyableContent $content */
418 $content = $rev->getContent( Revision
::RAW
);
419 $content->setText( "bar" );
421 /** @var RevisionTestModifyableContent $content2 */
422 $content2 = $rev->getContent( Revision
::RAW
);
423 // content is mutable, expect clone
424 $this->assertNotSame( $content, $content2, "expected a clone" );
425 // clone should contain the original text
426 $this->assertEquals( "foo", $content2->getText() );
428 $content2->setText( "bla bla" );
429 // clones should be independent
430 $this->assertEquals( "bar", $content->getText() );
434 * Tests whether $rev->getContent() returns the same object repeatedly if appropriate.
437 * @covers Revision::getContent
439 public function testGetContentUncloned() {
440 $rev = $this->newTestRevision( "hello", "testGetContentUncloned_dummy", CONTENT_MODEL_WIKITEXT
);
441 $content = $rev->getContent( Revision
::RAW
);
442 $content2 = $rev->getContent( Revision
::RAW
);
444 // for immutable content like wikitext, this should be the same object
445 $this->assertSame( $content, $content2 );