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' );
137 $this->setMwGlobals( 'wgCompressRevisions', true );
140 $row->old_text
= "Wiki est l'\xc3\xa9cole superieur !";
141 $row->old_flags
= Revision
::compressRevisionText( $row->old_text
);
142 $this->assertTrue( false !== strpos( $row->old_flags
, 'utf-8' ),
143 "Flags should contain 'utf-8'" );
144 $this->assertTrue( false !== strpos( $row->old_flags
, 'gzip' ),
145 "Flags should contain 'gzip'" );
146 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
147 gzinflate( $row->old_text
), "Direct check" );
148 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
149 Revision
::getRevisionText( $row ), "getRevisionText" );
152 # =================================================================================================================
155 * @param string $text
156 * @param string $title
157 * @param string $model
160 function newTestRevision( $text, $title = "Test", $model = CONTENT_MODEL_WIKITEXT
, $format = null ) {
161 if ( is_string( $title ) ) {
162 $title = Title
::newFromText( $title );
165 $content = ContentHandler
::makeContent( $text, $title, $model, $format );
173 'content' => $content,
174 'length' => $content->getSize(),
175 'comment' => "testing",
176 'minor_edit' => false,
178 'content_format' => $format,
185 function dataGetContentModel() {
186 //NOTE: we expect the help namespace to always contain wikitext
188 array( 'hello world', 'Help:Hello', null, null, CONTENT_MODEL_WIKITEXT
),
189 array( 'hello world', 'User:hello/there.css', null, null, CONTENT_MODEL_CSS
),
190 array( serialize( 'hello world' ), 'Dummy:Hello', null, null, "testing" ),
196 * @dataProvider dataGetContentModel
198 function testGetContentModel( $text, $title, $model, $format, $expectedModel ) {
199 $rev = $this->newTestRevision( $text, $title, $model, $format );
201 $this->assertEquals( $expectedModel, $rev->getContentModel() );
204 function dataGetContentFormat() {
205 //NOTE: we expect the help namespace to always contain wikitext
207 array( 'hello world', 'Help:Hello', null, null, CONTENT_FORMAT_WIKITEXT
),
208 array( 'hello world', 'Help:Hello', CONTENT_MODEL_CSS
, null, CONTENT_FORMAT_CSS
),
209 array( 'hello world', 'User:hello/there.css', null, null, CONTENT_FORMAT_CSS
),
210 array( serialize( 'hello world' ), 'Dummy:Hello', null, null, "testing" ),
216 * @dataProvider dataGetContentFormat
218 function testGetContentFormat( $text, $title, $model, $format, $expectedFormat ) {
219 $rev = $this->newTestRevision( $text, $title, $model, $format );
221 $this->assertEquals( $expectedFormat, $rev->getContentFormat() );
224 function dataGetContentHandler() {
225 //NOTE: we expect the help namespace to always contain wikitext
227 array( 'hello world', 'Help:Hello', null, null, 'WikitextContentHandler' ),
228 array( 'hello world', 'User:hello/there.css', null, null, 'CssContentHandler' ),
229 array( serialize( 'hello world' ), 'Dummy:Hello', null, null, 'DummyContentHandlerForTesting' ),
235 * @dataProvider dataGetContentHandler
237 function testGetContentHandler( $text, $title, $model, $format, $expectedClass ) {
238 $rev = $this->newTestRevision( $text, $title, $model, $format );
240 $this->assertEquals( $expectedClass, get_class( $rev->getContentHandler() ) );
243 function dataGetContent() {
244 //NOTE: we expect the help namespace to always contain wikitext
246 array( 'hello world', 'Help:Hello', null, null, Revision
::FOR_PUBLIC
, 'hello world' ),
247 array( serialize( 'hello world' ), 'Hello', "testing", null, Revision
::FOR_PUBLIC
, serialize( 'hello world' ) ),
248 array( serialize( 'hello world' ), 'Dummy:Hello', null, null, Revision
::FOR_PUBLIC
, serialize( 'hello world' ) ),
254 * @dataProvider dataGetContent
256 function testGetContent( $text, $title, $model, $format, $audience, $expectedSerialization ) {
257 $rev = $this->newTestRevision( $text, $title, $model, $format );
258 $content = $rev->getContent( $audience );
260 $this->assertEquals( $expectedSerialization, is_null( $content ) ?
null : $content->serialize( $format ) );
263 function dataGetText() {
264 //NOTE: we expect the help namespace to always contain wikitext
266 array( 'hello world', 'Help:Hello', null, null, Revision
::FOR_PUBLIC
, 'hello world' ),
267 array( serialize( 'hello world' ), 'Hello', "testing", null, Revision
::FOR_PUBLIC
, null ),
268 array( serialize( 'hello world' ), 'Dummy:Hello', null, null, Revision
::FOR_PUBLIC
, null ),
274 * @dataProvider dataGetText
276 function testGetText( $text, $title, $model, $format, $audience, $expectedText ) {
277 $this->hideDeprecated( 'Revision::getText' );
279 $rev = $this->newTestRevision( $text, $title, $model, $format );
281 $this->assertEquals( $expectedText, $rev->getText( $audience ) );
286 * @dataProvider dataGetText
288 function testGetRawText( $text, $title, $model, $format, $audience, $expectedText ) {
289 $this->hideDeprecated( 'Revision::getRawText' );
291 $rev = $this->newTestRevision( $text, $title, $model, $format );
293 $this->assertEquals( $expectedText, $rev->getRawText( $audience ) );
297 public function dataGetSize() {
299 array( "hello world.", CONTENT_MODEL_WIKITEXT
, 12 ),
300 array( serialize( "hello world." ), "testing", 12 ),
305 * @covers Revision::getSize
307 * @dataProvider dataGetSize
309 public function testGetSize( $text, $model, $expected_size ) {
310 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSize', $model );
311 $this->assertEquals( $expected_size, $rev->getSize() );
314 public function dataGetSha1() {
316 array( "hello world.", CONTENT_MODEL_WIKITEXT
, Revision
::base36Sha1( "hello world." ) ),
317 array( serialize( "hello world." ), "testing", Revision
::base36Sha1( serialize( "hello world." ) ) ),
322 * @covers Revision::getSha1
324 * @dataProvider dataGetSha1
326 public function testGetSha1( $text, $model, $expected_hash ) {
327 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSha1', $model );
328 $this->assertEquals( $expected_hash, $rev->getSha1() );
331 public function testConstructWithText() {
332 $this->hideDeprecated( "Revision::getText" );
334 $rev = new Revision( array(
335 'text' => 'hello world.',
336 'content_model' => CONTENT_MODEL_JAVASCRIPT
339 $this->assertNotNull( $rev->getText(), 'no content text' );
340 $this->assertNotNull( $rev->getContent(), 'no content object available' );
341 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContent()->getModel() );
342 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContentModel() );
345 public function testConstructWithContent() {
346 $this->hideDeprecated( "Revision::getText" );
348 $title = Title
::newFromText( 'RevisionTest_testConstructWithContent' );
350 $rev = new Revision( array(
351 'content' => ContentHandler
::makeContent( 'hello world.', $title, CONTENT_MODEL_JAVASCRIPT
),
354 $this->assertNotNull( $rev->getText(), 'no content text' );
355 $this->assertNotNull( $rev->getContent(), 'no content object available' );
356 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContent()->getModel() );
357 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContentModel() );
361 * Tests whether $rev->getContent() returns a clone when needed.
365 function testGetContentClone() {
366 $content = new RevisionTestModifyableContent( "foo" );
372 'title' => Title
::newFromText( "testGetContentClone_dummy" ),
374 'content' => $content,
375 'length' => $content->getSize(),
376 'comment' => "testing",
377 'minor_edit' => false,
381 $content = $rev->getContent( Revision
::RAW
);
382 $content->setText( "bar" );
384 $content2 = $rev->getContent( Revision
::RAW
);
385 $this->assertNotSame( $content, $content2, "expected a clone" ); // content is mutable, expect clone
386 $this->assertEquals( "foo", $content2->getText() ); // clone should contain the original text
388 $content2->setText( "bla bla" );
389 $this->assertEquals( "bar", $content->getText() ); // clones should be independent
394 * Tests whether $rev->getContent() returns the same object repeatedly if appropriate.
398 function testGetContentUncloned() {
399 $rev = $this->newTestRevision( "hello", "testGetContentUncloned_dummy", CONTENT_MODEL_WIKITEXT
);
400 $content = $rev->getContent( Revision
::RAW
);
401 $content2 = $rev->getContent( Revision
::RAW
);
403 // for immutable content like wikitext, this should be the same object
404 $this->assertSame( $content, $content2 );
408 class RevisionTestModifyableContent
extends TextContent
{
409 public function __construct( $text ) {
410 parent
::__construct( $text, "RevisionTestModifyableContent" );
413 public function copy() {
414 return new RevisionTestModifyableContent( $this->mText
);
417 public function getText() {
421 public function setText( $text ) {
422 $this->mText
= $text;
426 class RevisionTestModifyableContentHandler
extends TextContentHandler
{
428 public function __construct() {
429 parent
::__construct( "RevisionTestModifyableContent", array( CONTENT_FORMAT_TEXT
) );
432 public function unserializeContent( $text, $format = null ) {
433 $this->checkFormat( $format );
435 return new RevisionTestModifyableContent( $text );
438 public function makeEmptyContent() {
439 return new RevisionTestModifyableContent( '' );