4 * @group ContentHandler
6 class RevisionTest
extends MediaWikiTestCase
{
7 protected function setUp() {
12 $this->setMwGlobals( array(
13 'wgContLang' => Language
::factory( 'en' ),
14 'wgLanguageCode' => 'en',
15 'wgLegacyEncoding' => false,
16 'wgCompressRevisions' => false,
18 'wgContentHandlerTextFallback' => 'ignore',
21 $this->mergeMwGlobalArrayValue(
25 12313 => 'Dummy_talk',
29 $this->mergeMwGlobalArrayValue(
30 'wgNamespaceContentModels',
36 $this->mergeMwGlobalArrayValue(
39 'testing' => 'DummyContentHandlerForTesting',
40 'RevisionTestModifyableContent' => 'RevisionTestModifyableContentHandler',
44 MWNamespace
::getCanonicalNamespaces( true ); # reset namespace cache
45 $wgContLang->resetNamespaces(); # reset namespace cache
51 MWNamespace
::getCanonicalNamespaces( true ); # reset namespace cache
52 $wgContLang->resetNamespaces(); # reset namespace cache
57 function testGetRevisionText() {
60 $row->old_text
= 'This is a bunch of revision text.';
62 'This is a bunch of revision text.',
63 Revision
::getRevisionText( $row ) );
66 function testGetRevisionTextGzip() {
67 if ( !function_exists( 'gzdeflate' ) ) {
68 $this->markTestSkipped( 'Gzip compression is not enabled (requires zlib).' );
71 $row->old_flags
= 'gzip';
72 $row->old_text
= gzdeflate( 'This is a bunch of revision text.' );
74 'This is a bunch of revision text.',
75 Revision
::getRevisionText( $row ) );
79 function testGetRevisionTextUtf8Native() {
81 $row->old_flags
= 'utf-8';
82 $row->old_text
= "Wiki est l'\xc3\xa9cole superieur !";
83 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
85 "Wiki est l'\xc3\xa9cole superieur !",
86 Revision
::getRevisionText( $row ) );
89 function testGetRevisionTextUtf8Legacy() {
92 $row->old_text
= "Wiki est l'\xe9cole superieur !";
93 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
95 "Wiki est l'\xc3\xa9cole superieur !",
96 Revision
::getRevisionText( $row ) );
99 function testGetRevisionTextUtf8NativeGzip() {
100 if ( !function_exists( 'gzdeflate' ) ) {
101 $this->markTestSkipped( 'Gzip compression is not enabled (requires zlib).' );
104 $row->old_flags
= 'gzip,utf-8';
105 $row->old_text
= gzdeflate( "Wiki est l'\xc3\xa9cole superieur !" );
106 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
108 "Wiki est l'\xc3\xa9cole superieur !",
109 Revision
::getRevisionText( $row ) );
113 function testGetRevisionTextUtf8LegacyGzip() {
114 if ( !function_exists( 'gzdeflate' ) ) {
115 $this->markTestSkipped( 'Gzip compression is not enabled (requires zlib).' );
118 $row->old_flags
= 'gzip';
119 $row->old_text
= gzdeflate( "Wiki est l'\xe9cole superieur !" );
120 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
122 "Wiki est l'\xc3\xa9cole superieur !",
123 Revision
::getRevisionText( $row ) );
127 function testCompressRevisionTextUtf8() {
129 $row->old_text
= "Wiki est l'\xc3\xa9cole superieur !";
130 $row->old_flags
= Revision
::compressRevisionText( $row->old_text
);
131 $this->assertTrue( false !== strpos( $row->old_flags
, 'utf-8' ),
132 "Flags should contain 'utf-8'" );
133 $this->assertFalse( false !== strpos( $row->old_flags
, 'gzip' ),
134 "Flags should not contain 'gzip'" );
135 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
136 $row->old_text
, "Direct check" );
137 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
138 Revision
::getRevisionText( $row ), "getRevisionText" );
141 function testCompressRevisionTextUtf8Gzip() {
142 global $wgCompressRevisions;
144 $wgCompressRevisions = true;
147 $row->old_text
= "Wiki est l'\xc3\xa9cole superieur !";
148 $row->old_flags
= Revision
::compressRevisionText( $row->old_text
);
149 $this->assertTrue( false !== strpos( $row->old_flags
, 'utf-8' ),
150 "Flags should contain 'utf-8'" );
151 $this->assertTrue( false !== strpos( $row->old_flags
, 'gzip' ),
152 "Flags should contain 'gzip'" );
153 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
154 gzinflate( $row->old_text
), "Direct check" );
155 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
156 Revision
::getRevisionText( $row ), "getRevisionText" );
159 # =================================================================================================================
162 * @param string $text
163 * @param string $title
164 * @param string $model
167 function newTestRevision( $text, $title = "Test", $model = CONTENT_MODEL_WIKITEXT
, $format = null ) {
168 if ( is_string( $title ) ) {
169 $title = Title
::newFromText( $title );
172 $content = ContentHandler
::makeContent( $text, $title, $model, $format );
180 'content' => $content,
181 'length' => $content->getSize(),
182 'comment' => "testing",
183 'minor_edit' => false,
185 'content_format' => $format,
192 function dataGetContentModel() {
193 //NOTE: we expect the help namespace to always contain wikitext
195 array( 'hello world', 'Help:Hello', null, null, CONTENT_MODEL_WIKITEXT
),
196 array( 'hello world', 'User:hello/there.css', null, null, CONTENT_MODEL_CSS
),
197 array( serialize('hello world'), 'Dummy:Hello', null, null, "testing" ),
203 * @dataProvider dataGetContentModel
205 function testGetContentModel( $text, $title, $model, $format, $expectedModel ) {
206 $rev = $this->newTestRevision( $text, $title, $model, $format );
208 $this->assertEquals( $expectedModel, $rev->getContentModel() );
211 function dataGetContentFormat() {
212 //NOTE: we expect the help namespace to always contain wikitext
214 array( 'hello world', 'Help:Hello', null, null, CONTENT_FORMAT_WIKITEXT
),
215 array( 'hello world', 'Help:Hello', CONTENT_MODEL_CSS
, null, CONTENT_FORMAT_CSS
),
216 array( 'hello world', 'User:hello/there.css', null, null, CONTENT_FORMAT_CSS
),
217 array( serialize('hello world'), 'Dummy:Hello', null, null, "testing" ),
223 * @dataProvider dataGetContentFormat
225 function testGetContentFormat( $text, $title, $model, $format, $expectedFormat ) {
226 $rev = $this->newTestRevision( $text, $title, $model, $format );
228 $this->assertEquals( $expectedFormat, $rev->getContentFormat() );
231 function dataGetContentHandler() {
232 //NOTE: we expect the help namespace to always contain wikitext
234 array( 'hello world', 'Help:Hello', null, null, 'WikitextContentHandler' ),
235 array( 'hello world', 'User:hello/there.css', null, null, 'CssContentHandler' ),
236 array( serialize('hello world'), 'Dummy:Hello', null, null, 'DummyContentHandlerForTesting' ),
242 * @dataProvider dataGetContentHandler
244 function testGetContentHandler( $text, $title, $model, $format, $expectedClass ) {
245 $rev = $this->newTestRevision( $text, $title, $model, $format );
247 $this->assertEquals( $expectedClass, get_class( $rev->getContentHandler() ) );
250 function dataGetContent() {
251 //NOTE: we expect the help namespace to always contain wikitext
253 array( 'hello world', 'Help:Hello', null, null, Revision
::FOR_PUBLIC
, 'hello world' ),
254 array( serialize('hello world'), 'Hello', "testing", null, Revision
::FOR_PUBLIC
, serialize('hello world') ),
255 array( serialize('hello world'), 'Dummy:Hello', null, null, Revision
::FOR_PUBLIC
, serialize('hello world') ),
261 * @dataProvider dataGetContent
263 function testGetContent( $text, $title, $model, $format, $audience, $expectedSerialization ) {
264 $rev = $this->newTestRevision( $text, $title, $model, $format );
265 $content = $rev->getContent( $audience );
267 $this->assertEquals( $expectedSerialization, is_null( $content ) ?
null : $content->serialize( $format ) );
270 function dataGetText() {
271 //NOTE: we expect the help namespace to always contain wikitext
273 array( 'hello world', 'Help:Hello', null, null, Revision
::FOR_PUBLIC
, 'hello world' ),
274 array( serialize('hello world'), 'Hello', "testing", null, Revision
::FOR_PUBLIC
, null ),
275 array( serialize('hello world'), 'Dummy:Hello', null, null, Revision
::FOR_PUBLIC
, null ),
281 * @dataProvider dataGetText
283 function testGetText( $text, $title, $model, $format, $audience, $expectedText ) {
284 $this->hideDeprecated( 'Revision::getText' );
286 $rev = $this->newTestRevision( $text, $title, $model, $format );
288 $this->assertEquals( $expectedText, $rev->getText( $audience ) );
293 * @dataProvider dataGetText
295 function testGetRawText( $text, $title, $model, $format, $audience, $expectedText ) {
296 $this->hideDeprecated( 'Revision::getRawText' );
298 $rev = $this->newTestRevision( $text, $title, $model, $format );
300 $this->assertEquals( $expectedText, $rev->getRawText( $audience ) );
304 public function dataGetSize( ) {
306 array( "hello world.", CONTENT_MODEL_WIKITEXT
, 12 ),
307 array( serialize( "hello world." ), "testing", 12 ),
312 * @covers Revision::getSize
314 * @dataProvider dataGetSize
316 public function testGetSize( $text, $model, $expected_size )
318 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSize', $model );
319 $this->assertEquals( $expected_size, $rev->getSize() );
322 public function dataGetSha1( ) {
324 array( "hello world.", CONTENT_MODEL_WIKITEXT
, Revision
::base36Sha1( "hello world." ) ),
325 array( serialize( "hello world." ), "testing", Revision
::base36Sha1( serialize( "hello world." ) ) ),
330 * @covers Revision::getSha1
332 * @dataProvider dataGetSha1
334 public function testGetSha1( $text, $model, $expected_hash )
336 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSha1', $model );
337 $this->assertEquals( $expected_hash, $rev->getSha1() );
340 public function testConstructWithText() {
341 $this->hideDeprecated( "Revision::getText" );
343 $rev = new Revision( array(
344 'text' => 'hello world.',
345 'content_model' => CONTENT_MODEL_JAVASCRIPT
348 $this->assertNotNull( $rev->getText(), 'no content text' );
349 $this->assertNotNull( $rev->getContent(), 'no content object available' );
350 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContent()->getModel() );
351 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContentModel() );
354 public function testConstructWithContent() {
355 $this->hideDeprecated( "Revision::getText" );
357 $title = Title
::newFromText( 'RevisionTest_testConstructWithContent' );
359 $rev = new Revision( array(
360 'content' => ContentHandler
::makeContent( 'hello world.', $title, CONTENT_MODEL_JAVASCRIPT
),
363 $this->assertNotNull( $rev->getText(), 'no content text' );
364 $this->assertNotNull( $rev->getContent(), 'no content object available' );
365 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContent()->getModel() );
366 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContentModel() );
370 * Tests whether $rev->getContent() returns a clone when needed.
374 function testGetContentClone( ) {
375 $content = new RevisionTestModifyableContent( "foo" );
381 'title' => Title
::newFromText( "testGetContentClone_dummy" ),
383 'content' => $content,
384 'length' => $content->getSize(),
385 'comment' => "testing",
386 'minor_edit' => false,
390 $content = $rev->getContent( Revision
::RAW
);
391 $content->setText( "bar" );
393 $content2 = $rev->getContent( Revision
::RAW
);
394 $this->assertNotSame( $content, $content2, "expected a clone" ); // content is mutable, expect clone
395 $this->assertEquals( "foo", $content2->getText() ); // clone should contain the original text
397 $content2->setText( "bla bla" );
398 $this->assertEquals( "bar", $content->getText() ); // clones should be independent
403 * Tests whether $rev->getContent() returns the same object repeatedly if appropriate.
407 function testGetContentUncloned() {
408 $rev = $this->newTestRevision( "hello", "testGetContentUncloned_dummy", CONTENT_MODEL_WIKITEXT
);
409 $content = $rev->getContent( Revision
::RAW
);
410 $content2 = $rev->getContent( Revision
::RAW
);
412 // for immutable content like wikitext, this should be the same object
413 $this->assertSame( $content, $content2 );
418 class RevisionTestModifyableContent
extends TextContent
{
419 public function __construct( $text ) {
420 parent
::__construct( $text, "RevisionTestModifyableContent" );
423 public function copy( ) {
424 return new RevisionTestModifyableContent( $this->mText
);
427 public function getText() {
431 public function setText( $text ) {
432 $this->mText
= $text;
437 class RevisionTestModifyableContentHandler
extends TextContentHandler
{
439 public function __construct( ) {
440 parent
::__construct( "RevisionTestModifyableContent", array( CONTENT_FORMAT_TEXT
) );
443 public function unserializeContent( $text, $format = null ) {
444 $this->checkFormat( $format );
446 return new RevisionTestModifyableContent( $text );
449 public function makeEmptyContent() {
450 return new RevisionTestModifyableContent( '' );