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 $this->checkPHPExtension( '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 ) );
77 function testGetRevisionTextUtf8Native() {
79 $row->old_flags
= 'utf-8';
80 $row->old_text
= "Wiki est l'\xc3\xa9cole superieur !";
81 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
83 "Wiki est l'\xc3\xa9cole superieur !",
84 Revision
::getRevisionText( $row ) );
87 function testGetRevisionTextUtf8Legacy() {
90 $row->old_text
= "Wiki est l'\xe9cole superieur !";
91 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
93 "Wiki est l'\xc3\xa9cole superieur !",
94 Revision
::getRevisionText( $row ) );
97 function testGetRevisionTextUtf8NativeGzip() {
98 $this->checkPHPExtension( 'zlib' );
101 $row->old_flags
= 'gzip,utf-8';
102 $row->old_text
= gzdeflate( "Wiki est l'\xc3\xa9cole superieur !" );
103 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
105 "Wiki est l'\xc3\xa9cole superieur !",
106 Revision
::getRevisionText( $row ) );
109 function testGetRevisionTextUtf8LegacyGzip() {
110 $this->checkPHPExtension( 'zlib' );
113 $row->old_flags
= 'gzip';
114 $row->old_text
= gzdeflate( "Wiki est l'\xe9cole superieur !" );
115 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
117 "Wiki est l'\xc3\xa9cole superieur !",
118 Revision
::getRevisionText( $row ) );
121 function testCompressRevisionTextUtf8() {
123 $row->old_text
= "Wiki est l'\xc3\xa9cole superieur !";
124 $row->old_flags
= Revision
::compressRevisionText( $row->old_text
);
125 $this->assertTrue( false !== strpos( $row->old_flags
, 'utf-8' ),
126 "Flags should contain 'utf-8'" );
127 $this->assertFalse( false !== strpos( $row->old_flags
, 'gzip' ),
128 "Flags should not contain 'gzip'" );
129 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
130 $row->old_text
, "Direct check" );
131 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
132 Revision
::getRevisionText( $row ), "getRevisionText" );
135 function testCompressRevisionTextUtf8Gzip() {
136 $this->checkPHPExtension( 'zlib' );
138 global $wgCompressRevisions;
139 $wgCompressRevisions = true;
142 $row->old_text
= "Wiki est l'\xc3\xa9cole superieur !";
143 $row->old_flags
= Revision
::compressRevisionText( $row->old_text
);
144 $this->assertTrue( false !== strpos( $row->old_flags
, 'utf-8' ),
145 "Flags should contain 'utf-8'" );
146 $this->assertTrue( false !== strpos( $row->old_flags
, 'gzip' ),
147 "Flags should contain 'gzip'" );
148 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
149 gzinflate( $row->old_text
), "Direct check" );
150 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
151 Revision
::getRevisionText( $row ), "getRevisionText" );
154 # =================================================================================================================
157 * @param string $text
158 * @param string $title
159 * @param string $model
162 function newTestRevision( $text, $title = "Test", $model = CONTENT_MODEL_WIKITEXT
, $format = null ) {
163 if ( is_string( $title ) ) {
164 $title = Title
::newFromText( $title );
167 $content = ContentHandler
::makeContent( $text, $title, $model, $format );
175 'content' => $content,
176 'length' => $content->getSize(),
177 'comment' => "testing",
178 'minor_edit' => false,
180 'content_format' => $format,
187 function dataGetContentModel() {
188 //NOTE: we expect the help namespace to always contain wikitext
190 array( 'hello world', 'Help:Hello', null, null, CONTENT_MODEL_WIKITEXT
),
191 array( 'hello world', 'User:hello/there.css', null, null, CONTENT_MODEL_CSS
),
192 array( serialize( 'hello world' ), 'Dummy:Hello', null, null, "testing" ),
198 * @dataProvider dataGetContentModel
200 function testGetContentModel( $text, $title, $model, $format, $expectedModel ) {
201 $rev = $this->newTestRevision( $text, $title, $model, $format );
203 $this->assertEquals( $expectedModel, $rev->getContentModel() );
206 function dataGetContentFormat() {
207 //NOTE: we expect the help namespace to always contain wikitext
209 array( 'hello world', 'Help:Hello', null, null, CONTENT_FORMAT_WIKITEXT
),
210 array( 'hello world', 'Help:Hello', CONTENT_MODEL_CSS
, null, CONTENT_FORMAT_CSS
),
211 array( 'hello world', 'User:hello/there.css', null, null, CONTENT_FORMAT_CSS
),
212 array( serialize( 'hello world' ), 'Dummy:Hello', null, null, "testing" ),
218 * @dataProvider dataGetContentFormat
220 function testGetContentFormat( $text, $title, $model, $format, $expectedFormat ) {
221 $rev = $this->newTestRevision( $text, $title, $model, $format );
223 $this->assertEquals( $expectedFormat, $rev->getContentFormat() );
226 function dataGetContentHandler() {
227 //NOTE: we expect the help namespace to always contain wikitext
229 array( 'hello world', 'Help:Hello', null, null, 'WikitextContentHandler' ),
230 array( 'hello world', 'User:hello/there.css', null, null, 'CssContentHandler' ),
231 array( serialize( 'hello world' ), 'Dummy:Hello', null, null, 'DummyContentHandlerForTesting' ),
237 * @dataProvider dataGetContentHandler
239 function testGetContentHandler( $text, $title, $model, $format, $expectedClass ) {
240 $rev = $this->newTestRevision( $text, $title, $model, $format );
242 $this->assertEquals( $expectedClass, get_class( $rev->getContentHandler() ) );
245 function dataGetContent() {
246 //NOTE: we expect the help namespace to always contain wikitext
248 array( 'hello world', 'Help:Hello', null, null, Revision
::FOR_PUBLIC
, 'hello world' ),
249 array( serialize( 'hello world' ), 'Hello', "testing", null, Revision
::FOR_PUBLIC
, serialize( 'hello world' ) ),
250 array( serialize( 'hello world' ), 'Dummy:Hello', null, null, Revision
::FOR_PUBLIC
, serialize( 'hello world' ) ),
256 * @dataProvider dataGetContent
258 function testGetContent( $text, $title, $model, $format, $audience, $expectedSerialization ) {
259 $rev = $this->newTestRevision( $text, $title, $model, $format );
260 $content = $rev->getContent( $audience );
262 $this->assertEquals( $expectedSerialization, is_null( $content ) ?
null : $content->serialize( $format ) );
265 function dataGetText() {
266 //NOTE: we expect the help namespace to always contain wikitext
268 array( 'hello world', 'Help:Hello', null, null, Revision
::FOR_PUBLIC
, 'hello world' ),
269 array( serialize( 'hello world' ), 'Hello', "testing", null, Revision
::FOR_PUBLIC
, null ),
270 array( serialize( 'hello world' ), 'Dummy:Hello', null, null, Revision
::FOR_PUBLIC
, null ),
276 * @dataProvider dataGetText
278 function testGetText( $text, $title, $model, $format, $audience, $expectedText ) {
279 $this->hideDeprecated( 'Revision::getText' );
281 $rev = $this->newTestRevision( $text, $title, $model, $format );
283 $this->assertEquals( $expectedText, $rev->getText( $audience ) );
288 * @dataProvider dataGetText
290 function testGetRawText( $text, $title, $model, $format, $audience, $expectedText ) {
291 $this->hideDeprecated( 'Revision::getRawText' );
293 $rev = $this->newTestRevision( $text, $title, $model, $format );
295 $this->assertEquals( $expectedText, $rev->getRawText( $audience ) );
299 public function dataGetSize() {
301 array( "hello world.", CONTENT_MODEL_WIKITEXT
, 12 ),
302 array( serialize( "hello world." ), "testing", 12 ),
307 * @covers Revision::getSize
309 * @dataProvider dataGetSize
311 public function testGetSize( $text, $model, $expected_size ) {
312 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSize', $model );
313 $this->assertEquals( $expected_size, $rev->getSize() );
316 public function dataGetSha1() {
318 array( "hello world.", CONTENT_MODEL_WIKITEXT
, Revision
::base36Sha1( "hello world." ) ),
319 array( serialize( "hello world." ), "testing", Revision
::base36Sha1( serialize( "hello world." ) ) ),
324 * @covers Revision::getSha1
326 * @dataProvider dataGetSha1
328 public function testGetSha1( $text, $model, $expected_hash ) {
329 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSha1', $model );
330 $this->assertEquals( $expected_hash, $rev->getSha1() );
333 public function testConstructWithText() {
334 $this->hideDeprecated( "Revision::getText" );
336 $rev = new Revision( array(
337 'text' => 'hello world.',
338 'content_model' => CONTENT_MODEL_JAVASCRIPT
341 $this->assertNotNull( $rev->getText(), 'no content text' );
342 $this->assertNotNull( $rev->getContent(), 'no content object available' );
343 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContent()->getModel() );
344 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContentModel() );
347 public function testConstructWithContent() {
348 $this->hideDeprecated( "Revision::getText" );
350 $title = Title
::newFromText( 'RevisionTest_testConstructWithContent' );
352 $rev = new Revision( array(
353 'content' => ContentHandler
::makeContent( 'hello world.', $title, CONTENT_MODEL_JAVASCRIPT
),
356 $this->assertNotNull( $rev->getText(), 'no content text' );
357 $this->assertNotNull( $rev->getContent(), 'no content object available' );
358 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContent()->getModel() );
359 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContentModel() );
363 * Tests whether $rev->getContent() returns a clone when needed.
367 function testGetContentClone() {
368 $content = new RevisionTestModifyableContent( "foo" );
374 'title' => Title
::newFromText( "testGetContentClone_dummy" ),
376 'content' => $content,
377 'length' => $content->getSize(),
378 'comment' => "testing",
379 'minor_edit' => false,
383 $content = $rev->getContent( Revision
::RAW
);
384 $content->setText( "bar" );
386 $content2 = $rev->getContent( Revision
::RAW
);
387 $this->assertNotSame( $content, $content2, "expected a clone" ); // content is mutable, expect clone
388 $this->assertEquals( "foo", $content2->getText() ); // clone should contain the original text
390 $content2->setText( "bla bla" );
391 $this->assertEquals( "bar", $content->getText() ); // clones should be independent
396 * Tests whether $rev->getContent() returns the same object repeatedly if appropriate.
400 function testGetContentUncloned() {
401 $rev = $this->newTestRevision( "hello", "testGetContentUncloned_dummy", CONTENT_MODEL_WIKITEXT
);
402 $content = $rev->getContent( Revision
::RAW
);
403 $content2 = $rev->getContent( Revision
::RAW
);
405 // for immutable content like wikitext, this should be the same object
406 $this->assertSame( $content, $content2 );
411 class RevisionTestModifyableContent
extends TextContent
{
412 public function __construct( $text ) {
413 parent
::__construct( $text, "RevisionTestModifyableContent" );
416 public function copy() {
417 return new RevisionTestModifyableContent( $this->mText
);
420 public function getText() {
424 public function setText( $text ) {
425 $this->mText
= $text;
430 class RevisionTestModifyableContentHandler
extends TextContentHandler
{
432 public function __construct() {
433 parent
::__construct( "RevisionTestModifyableContent", array( CONTENT_FORMAT_TEXT
) );
436 public function unserializeContent( $text, $format = null ) {
437 $this->checkFormat( $format );
439 return new RevisionTestModifyableContent( $text );
442 public function makeEmptyContent() {
443 return new RevisionTestModifyableContent( '' );