4 * @group ContentHandler
6 class RevisionTest
extends MediaWikiTestCase
{
7 protected function setUp() {
12 $this->setMwGlobals( array(
13 'wgContLang' => Language
::factory( 'en' ),
14 'wgLegacyEncoding' => false,
15 'wgCompressRevisions' => false,
17 'wgContentHandlerTextFallback' => 'ignore',
20 $this->mergeMwGlobalArrayValue(
24 12313 => 'Dummy_talk',
28 $this->mergeMwGlobalArrayValue(
29 'wgNamespaceContentModels',
35 $this->mergeMwGlobalArrayValue(
38 'testing' => 'DummyContentHandlerForTesting',
39 'RevisionTestModifyableContent' => 'RevisionTestModifyableContentHandler',
43 MWNamespace
::getCanonicalNamespaces( true ); # reset namespace cache
44 $wgContLang->resetNamespaces(); # reset namespace cache
50 MWNamespace
::getCanonicalNamespaces( true ); # reset namespace cache
51 $wgContLang->resetNamespaces(); # reset namespace cache
56 function testGetRevisionText() {
59 $row->old_text
= 'This is a bunch of revision text.';
61 'This is a bunch of revision text.',
62 Revision
::getRevisionText( $row ) );
65 function testGetRevisionTextGzip() {
66 if ( !function_exists( 'gzdeflate' ) ) {
67 $this->markTestSkipped( 'Gzip compression is not enabled (requires zlib).' );
70 $row->old_flags
= 'gzip';
71 $row->old_text
= gzdeflate( 'This is a bunch of revision text.' );
73 'This is a bunch of revision text.',
74 Revision
::getRevisionText( $row ) );
78 function testGetRevisionTextUtf8Native() {
80 $row->old_flags
= 'utf-8';
81 $row->old_text
= "Wiki est l'\xc3\xa9cole superieur !";
82 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
84 "Wiki est l'\xc3\xa9cole superieur !",
85 Revision
::getRevisionText( $row ) );
88 function testGetRevisionTextUtf8Legacy() {
91 $row->old_text
= "Wiki est l'\xe9cole superieur !";
92 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
94 "Wiki est l'\xc3\xa9cole superieur !",
95 Revision
::getRevisionText( $row ) );
98 function testGetRevisionTextUtf8NativeGzip() {
99 if ( !function_exists( 'gzdeflate' ) ) {
100 $this->markTestSkipped( 'Gzip compression is not enabled (requires zlib).' );
103 $row->old_flags
= 'gzip,utf-8';
104 $row->old_text
= gzdeflate( "Wiki est l'\xc3\xa9cole superieur !" );
105 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
107 "Wiki est l'\xc3\xa9cole superieur !",
108 Revision
::getRevisionText( $row ) );
112 function testGetRevisionTextUtf8LegacyGzip() {
113 if ( !function_exists( 'gzdeflate' ) ) {
114 $this->markTestSkipped( 'Gzip compression is not enabled (requires zlib).' );
117 $row->old_flags
= 'gzip';
118 $row->old_text
= gzdeflate( "Wiki est l'\xe9cole superieur !" );
119 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
121 "Wiki est l'\xc3\xa9cole superieur !",
122 Revision
::getRevisionText( $row ) );
126 function testCompressRevisionTextUtf8() {
128 $row->old_text
= "Wiki est l'\xc3\xa9cole superieur !";
129 $row->old_flags
= Revision
::compressRevisionText( $row->old_text
);
130 $this->assertTrue( false !== strpos( $row->old_flags
, 'utf-8' ),
131 "Flags should contain 'utf-8'" );
132 $this->assertFalse( false !== strpos( $row->old_flags
, 'gzip' ),
133 "Flags should not contain 'gzip'" );
134 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
135 $row->old_text
, "Direct check" );
136 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
137 Revision
::getRevisionText( $row ), "getRevisionText" );
140 function testCompressRevisionTextUtf8Gzip() {
141 global $wgCompressRevisions;
143 $wgCompressRevisions = true;
146 $row->old_text
= "Wiki est l'\xc3\xa9cole superieur !";
147 $row->old_flags
= Revision
::compressRevisionText( $row->old_text
);
148 $this->assertTrue( false !== strpos( $row->old_flags
, 'utf-8' ),
149 "Flags should contain 'utf-8'" );
150 $this->assertTrue( false !== strpos( $row->old_flags
, 'gzip' ),
151 "Flags should contain 'gzip'" );
152 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
153 gzinflate( $row->old_text
), "Direct check" );
154 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
155 Revision
::getRevisionText( $row ), "getRevisionText" );
158 # =================================================================================================================
161 * @param string $text
162 * @param string $title
163 * @param string $model
166 function newTestRevision( $text, $title = "Test", $model = CONTENT_MODEL_WIKITEXT
, $format = null ) {
167 if ( is_string( $title ) ) {
168 $title = Title
::newFromText( $title );
171 $content = ContentHandler
::makeContent( $text, $title, $model, $format );
179 'content' => $content,
180 'length' => $content->getSize(),
181 'comment' => "testing",
182 'minor_edit' => false,
184 'content_format' => $format,
191 function dataGetContentModel() {
192 //NOTE: we expect the help namespace to always contain wikitext
194 array( 'hello world', 'Help:Hello', null, null, CONTENT_MODEL_WIKITEXT
),
195 array( 'hello world', 'User:hello/there.css', null, null, CONTENT_MODEL_CSS
),
196 array( serialize('hello world'), 'Dummy:Hello', null, null, "testing" ),
202 * @dataProvider dataGetContentModel
204 function testGetContentModel( $text, $title, $model, $format, $expectedModel ) {
205 $rev = $this->newTestRevision( $text, $title, $model, $format );
207 $this->assertEquals( $expectedModel, $rev->getContentModel() );
210 function dataGetContentFormat() {
211 //NOTE: we expect the help namespace to always contain wikitext
213 array( 'hello world', 'Help:Hello', null, null, CONTENT_FORMAT_WIKITEXT
),
214 array( 'hello world', 'Help:Hello', CONTENT_MODEL_CSS
, null, CONTENT_FORMAT_CSS
),
215 array( 'hello world', 'User:hello/there.css', null, null, CONTENT_FORMAT_CSS
),
216 array( serialize('hello world'), 'Dummy:Hello', null, null, "testing" ),
222 * @dataProvider dataGetContentFormat
224 function testGetContentFormat( $text, $title, $model, $format, $expectedFormat ) {
225 $rev = $this->newTestRevision( $text, $title, $model, $format );
227 $this->assertEquals( $expectedFormat, $rev->getContentFormat() );
230 function dataGetContentHandler() {
231 //NOTE: we expect the help namespace to always contain wikitext
233 array( 'hello world', 'Help:Hello', null, null, 'WikitextContentHandler' ),
234 array( 'hello world', 'User:hello/there.css', null, null, 'CssContentHandler' ),
235 array( serialize('hello world'), 'Dummy:Hello', null, null, 'DummyContentHandlerForTesting' ),
241 * @dataProvider dataGetContentHandler
243 function testGetContentHandler( $text, $title, $model, $format, $expectedClass ) {
244 $rev = $this->newTestRevision( $text, $title, $model, $format );
246 $this->assertEquals( $expectedClass, get_class( $rev->getContentHandler() ) );
249 function dataGetContent() {
250 //NOTE: we expect the help namespace to always contain wikitext
252 array( 'hello world', 'Help:Hello', null, null, Revision
::FOR_PUBLIC
, 'hello world' ),
253 array( serialize('hello world'), 'Hello', "testing", null, Revision
::FOR_PUBLIC
, serialize('hello world') ),
254 array( serialize('hello world'), 'Dummy:Hello', null, null, Revision
::FOR_PUBLIC
, serialize('hello world') ),
260 * @dataProvider dataGetContent
262 function testGetContent( $text, $title, $model, $format, $audience, $expectedSerialization ) {
263 $rev = $this->newTestRevision( $text, $title, $model, $format );
264 $content = $rev->getContent( $audience );
266 $this->assertEquals( $expectedSerialization, is_null( $content ) ?
null : $content->serialize( $format ) );
269 function dataGetText() {
270 //NOTE: we expect the help namespace to always contain wikitext
272 array( 'hello world', 'Help:Hello', null, null, Revision
::FOR_PUBLIC
, 'hello world' ),
273 array( serialize('hello world'), 'Hello', "testing", null, Revision
::FOR_PUBLIC
, null ),
274 array( serialize('hello world'), 'Dummy:Hello', null, null, Revision
::FOR_PUBLIC
, null ),
280 * @dataProvider dataGetText
282 function testGetText( $text, $title, $model, $format, $audience, $expectedText ) {
283 $this->hideDeprecated( 'Revision::getText' );
285 $rev = $this->newTestRevision( $text, $title, $model, $format );
287 $this->assertEquals( $expectedText, $rev->getText( $audience ) );
292 * @dataProvider dataGetText
294 function testGetRawText( $text, $title, $model, $format, $audience, $expectedText ) {
295 $this->hideDeprecated( 'Revision::getRawText' );
297 $rev = $this->newTestRevision( $text, $title, $model, $format );
299 $this->assertEquals( $expectedText, $rev->getRawText( $audience ) );
303 public function dataGetSize( ) {
305 array( "hello world.", CONTENT_MODEL_WIKITEXT
, 12 ),
306 array( serialize( "hello world." ), "testing", 12 ),
311 * @covers Revision::getSize
313 * @dataProvider dataGetSize
315 public function testGetSize( $text, $model, $expected_size )
317 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSize', $model );
318 $this->assertEquals( $expected_size, $rev->getSize() );
321 public function dataGetSha1( ) {
323 array( "hello world.", CONTENT_MODEL_WIKITEXT
, Revision
::base36Sha1( "hello world." ) ),
324 array( serialize( "hello world." ), "testing", Revision
::base36Sha1( serialize( "hello world." ) ) ),
329 * @covers Revision::getSha1
331 * @dataProvider dataGetSha1
333 public function testGetSha1( $text, $model, $expected_hash )
335 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSha1', $model );
336 $this->assertEquals( $expected_hash, $rev->getSha1() );
339 public function testConstructWithText() {
340 $this->hideDeprecated( "Revision::getText" );
342 $rev = new Revision( array(
343 'text' => 'hello world.',
344 'content_model' => CONTENT_MODEL_JAVASCRIPT
347 $this->assertNotNull( $rev->getText(), 'no content text' );
348 $this->assertNotNull( $rev->getContent(), 'no content object available' );
349 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContent()->getModel() );
350 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContentModel() );
353 public function testConstructWithContent() {
354 $this->hideDeprecated( "Revision::getText" );
356 $title = Title
::newFromText( 'RevisionTest_testConstructWithContent' );
358 $rev = new Revision( array(
359 'content' => ContentHandler
::makeContent( 'hello world.', $title, CONTENT_MODEL_JAVASCRIPT
),
362 $this->assertNotNull( $rev->getText(), 'no content text' );
363 $this->assertNotNull( $rev->getContent(), 'no content object available' );
364 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContent()->getModel() );
365 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContentModel() );
369 * Tests whether $rev->getContent() returns a clone when needed.
373 function testGetContentClone( ) {
374 $content = new RevisionTestModifyableContent( "foo" );
380 'title' => Title
::newFromText( "testGetContentClone_dummy" ),
382 'content' => $content,
383 'length' => $content->getSize(),
384 'comment' => "testing",
385 'minor_edit' => false,
389 $content = $rev->getContent( Revision
::RAW
);
390 $content->setText( "bar" );
392 $content2 = $rev->getContent( Revision
::RAW
);
393 $this->assertNotSame( $content, $content2, "expected a clone" ); // content is mutable, expect clone
394 $this->assertEquals( "foo", $content2->getText() ); // clone should contain the original text
396 $content2->setText( "bla bla" );
397 $this->assertEquals( "bar", $content->getText() ); // clones should be independent
402 * Tests whether $rev->getContent() returns the same object repeatedly if appropriate.
406 function testGetContentUncloned() {
407 $rev = $this->newTestRevision( "hello", "testGetContentUncloned_dummy", CONTENT_MODEL_WIKITEXT
);
408 $content = $rev->getContent( Revision
::RAW
);
409 $content2 = $rev->getContent( Revision
::RAW
);
411 // for immutable content like wikitext, this should be the same object
412 $this->assertSame( $content, $content2 );
417 class RevisionTestModifyableContent
extends TextContent
{
418 public function __construct( $text ) {
419 parent
::__construct( $text, "RevisionTestModifyableContent" );
422 public function copy( ) {
423 return new RevisionTestModifyableContent( $this->mText
);
426 public function getText() {
430 public function setText( $text ) {
431 $this->mText
= $text;
436 class RevisionTestModifyableContentHandler
extends TextContentHandler
{
438 public function __construct( ) {
439 parent
::__construct( "RevisionTestModifyableContent", array( CONTENT_FORMAT_TEXT
) );
442 public function unserializeContent( $text, $format = null ) {
443 $this->checkFormat( $format );
445 return new RevisionTestModifyableContent( $text );
448 public function makeEmptyContent() {
449 return new RevisionTestModifyableContent( '' );