4 * @group ContentHandler
6 class RevisionTest
extends MediaWikiTestCase
{
7 var $saveGlobals = array();
11 $wgContLang = Language
::factory( 'en' );
14 'wgLegacyEncoding' => false,
15 'wgCompressRevisions' => false,
17 'wgContentHandlerTextFallback' => $GLOBALS['wgContentHandlerTextFallback'],
18 'wgExtraNamespaces' => $GLOBALS['wgExtraNamespaces'],
19 'wgNamespaceContentModels' => $GLOBALS['wgNamespaceContentModels'],
20 'wgContentHandlers' => $GLOBALS['wgContentHandlers'],
23 foreach ( $globalSet as $var => $data ) {
24 $this->saveGlobals
[$var] = $GLOBALS[$var];
25 $GLOBALS[$var] = $data;
28 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
29 $wgExtraNamespaces[ 12312 ] = 'Dummy';
30 $wgExtraNamespaces[ 12313 ] = 'Dummy_talk';
32 $wgNamespaceContentModels[ 12312 ] = "testing";
33 $wgContentHandlers[ "testing" ] = 'DummyContentHandlerForTesting';
34 $wgContentHandlers[ "RevisionTestModifyableContent" ] = 'RevisionTestModifyableContentHandler';
36 MWNamespace
::getCanonicalNamespaces( true ); # reset namespace cache
37 $wgContLang->resetNamespaces(); # reset namespace cache
39 global $wgContentHandlerTextFallback;
40 $wgContentHandlerTextFallback = 'ignore';
46 foreach ( $this->saveGlobals
as $var => $data ) {
47 $GLOBALS[$var] = $data;
50 MWNamespace
::getCanonicalNamespaces( true ); # reset namespace cache
51 $wgContLang->resetNamespaces(); # reset namespace cache
54 function testGetRevisionText() {
57 $row->old_text
= 'This is a bunch of revision text.';
59 'This is a bunch of revision text.',
60 Revision
::getRevisionText( $row ) );
63 function testGetRevisionTextGzip() {
64 if ( !function_exists( 'gzdeflate' ) ) {
65 $this->markTestSkipped( 'Gzip compression is not enabled (requires zlib).' );
68 $row->old_flags
= 'gzip';
69 $row->old_text
= gzdeflate( 'This is a bunch of revision text.' );
71 'This is a bunch of revision text.',
72 Revision
::getRevisionText( $row ) );
76 function testGetRevisionTextUtf8Native() {
78 $row->old_flags
= 'utf-8';
79 $row->old_text
= "Wiki est l'\xc3\xa9cole superieur !";
80 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
82 "Wiki est l'\xc3\xa9cole superieur !",
83 Revision
::getRevisionText( $row ) );
86 function testGetRevisionTextUtf8Legacy() {
89 $row->old_text
= "Wiki est l'\xe9cole superieur !";
90 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
92 "Wiki est l'\xc3\xa9cole superieur !",
93 Revision
::getRevisionText( $row ) );
96 function testGetRevisionTextUtf8NativeGzip() {
97 if ( !function_exists( 'gzdeflate' ) ) {
98 $this->markTestSkipped( 'Gzip compression is not enabled (requires 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 ) );
110 function testGetRevisionTextUtf8LegacyGzip() {
111 if ( !function_exists( 'gzdeflate' ) ) {
112 $this->markTestSkipped( 'Gzip compression is not enabled (requires zlib).' );
115 $row->old_flags
= 'gzip';
116 $row->old_text
= gzdeflate( "Wiki est l'\xe9cole superieur !" );
117 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
119 "Wiki est l'\xc3\xa9cole superieur !",
120 Revision
::getRevisionText( $row ) );
124 function testCompressRevisionTextUtf8() {
126 $row->old_text
= "Wiki est l'\xc3\xa9cole superieur !";
127 $row->old_flags
= Revision
::compressRevisionText( $row->old_text
);
128 $this->assertTrue( false !== strpos( $row->old_flags
, 'utf-8' ),
129 "Flags should contain 'utf-8'" );
130 $this->assertFalse( false !== strpos( $row->old_flags
, 'gzip' ),
131 "Flags should not contain 'gzip'" );
132 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
133 $row->old_text
, "Direct check" );
134 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
135 Revision
::getRevisionText( $row ), "getRevisionText" );
138 function testCompressRevisionTextUtf8Gzip() {
139 $GLOBALS['wgCompressRevisions'] = true;
141 $row->old_text
= "Wiki est l'\xc3\xa9cole superieur !";
142 $row->old_flags
= Revision
::compressRevisionText( $row->old_text
);
143 $this->assertTrue( false !== strpos( $row->old_flags
, 'utf-8' ),
144 "Flags should contain 'utf-8'" );
145 $this->assertTrue( false !== strpos( $row->old_flags
, 'gzip' ),
146 "Flags should contain 'gzip'" );
147 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
148 gzinflate( $row->old_text
), "Direct check" );
149 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
150 Revision
::getRevisionText( $row ), "getRevisionText" );
153 # =================================================================================================================
156 * @param string $text
157 * @param string $title
158 * @param string $model
161 function newTestRevision( $text, $title = "Test", $model = CONTENT_MODEL_WIKITEXT
, $format = null ) {
162 if ( is_string( $title ) ) {
163 $title = Title
::newFromText( $title );
166 $content = ContentHandler
::makeContent( $text, $title, $model, $format );
174 'content' => $content,
175 'length' => $content->getSize(),
176 'comment' => "testing",
177 'minor_edit' => false,
179 'content_format' => $format,
186 function dataGetContentModel() {
188 array( 'hello world', '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() {
206 array( 'hello world', 'Hello', null, null, CONTENT_FORMAT_WIKITEXT
),
207 array( 'hello world', 'Hello', CONTENT_MODEL_CSS
, null, CONTENT_FORMAT_CSS
),
208 array( 'hello world', 'User:hello/there.css', null, null, CONTENT_FORMAT_CSS
),
209 array( serialize('hello world'), 'Dummy:Hello', null, null, "testing" ),
215 * @dataProvider dataGetContentFormat
217 function testGetContentFormat( $text, $title, $model, $format, $expectedFormat ) {
218 $rev = $this->newTestRevision( $text, $title, $model, $format );
220 $this->assertEquals( $expectedFormat, $rev->getContentFormat() );
223 function dataGetContentHandler() {
225 array( 'hello world', 'Hello', null, null, 'WikitextContentHandler' ),
226 array( 'hello world', 'User:hello/there.css', null, null, 'CssContentHandler' ),
227 array( serialize('hello world'), 'Dummy:Hello', null, null, 'DummyContentHandlerForTesting' ),
233 * @dataProvider dataGetContentHandler
235 function testGetContentHandler( $text, $title, $model, $format, $expectedClass ) {
236 $rev = $this->newTestRevision( $text, $title, $model, $format );
238 $this->assertEquals( $expectedClass, get_class( $rev->getContentHandler() ) );
241 function dataGetContent() {
243 array( 'hello world', 'Hello', null, null, Revision
::FOR_PUBLIC
, 'hello world' ),
244 array( serialize('hello world'), 'Hello', "testing", null, Revision
::FOR_PUBLIC
, serialize('hello world') ),
245 array( serialize('hello world'), 'Dummy:Hello', null, null, Revision
::FOR_PUBLIC
, serialize('hello world') ),
251 * @dataProvider dataGetContent
253 function testGetContent( $text, $title, $model, $format, $audience, $expectedSerialization ) {
254 $rev = $this->newTestRevision( $text, $title, $model, $format );
255 $content = $rev->getContent( $audience );
257 $this->assertEquals( $expectedSerialization, is_null( $content ) ?
null : $content->serialize( $format ) );
260 function dataGetText() {
262 array( 'hello world', 'Hello', null, null, Revision
::FOR_PUBLIC
, 'hello world' ),
263 array( serialize('hello world'), 'Hello', "testing", null, Revision
::FOR_PUBLIC
, null ),
264 array( serialize('hello world'), 'Dummy:Hello', null, null, Revision
::FOR_PUBLIC
, null ),
270 * @dataProvider dataGetText
272 function testGetText( $text, $title, $model, $format, $audience, $expectedText ) {
273 $rev = $this->newTestRevision( $text, $title, $model, $format );
275 $this->assertEquals( $expectedText, $rev->getText( $audience ) );
280 * @dataProvider dataGetText
282 function testGetRawText( $text, $title, $model, $format, $audience, $expectedText ) {
283 $rev = $this->newTestRevision( $text, $title, $model, $format );
285 $this->assertEquals( $expectedText, $rev->getRawText( $audience ) );
289 public function dataGetSize( ) {
291 array( "hello world.", null, 12 ),
292 array( serialize( "hello world." ), "testing", 12 ),
297 * @covers Revision::getSize
299 * @dataProvider dataGetSize
301 public function testGetSize( $text, $model, $expected_size )
303 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSize', $model );
304 $this->assertEquals( $expected_size, $rev->getSize() );
307 public function dataGetSha1( ) {
309 array( "hello world.", null, Revision
::base36Sha1( "hello world." ) ),
310 array( serialize( "hello world." ), "testing", Revision
::base36Sha1( serialize( "hello world." ) ) ),
315 * @covers Revision::getSha1
317 * @dataProvider dataGetSha1
319 public function testGetSha1( $text, $model, $expected_hash )
321 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSha1', $model );
322 $this->assertEquals( $expected_hash, $rev->getSha1() );
325 public function testConstructWithText() {
326 $rev = new Revision( array(
327 'text' => 'hello world.',
328 'content_model' => CONTENT_MODEL_JAVASCRIPT
331 $this->assertNotNull( $rev->getText(), 'no content text' );
332 $this->assertNotNull( $rev->getContent(), 'no content object available' );
333 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContent()->getModel() );
334 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContentModel() );
337 public function testConstructWithContent() {
338 $title = Title
::newFromText( 'RevisionTest_testConstructWithContent' );
340 $rev = new Revision( array(
341 'content' => ContentHandler
::makeContent( 'hello world.', $title, CONTENT_MODEL_JAVASCRIPT
),
344 $this->assertNotNull( $rev->getText(), 'no content text' );
345 $this->assertNotNull( $rev->getContent(), 'no content object available' );
346 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContent()->getModel() );
347 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContentModel() );
351 * Tests whether $rev->getContent() returns a clone when needed.
355 function testGetContentClone( ) {
356 $content = new RevisionTestModifyableContent( "foo" );
362 'title' => Title
::newFromText( "testGetContentClone_dummy" ),
364 'content' => $content,
365 'length' => $content->getSize(),
366 'comment' => "testing",
367 'minor_edit' => false,
371 $content = $rev->getContent( Revision
::RAW
);
372 $content->setText( "bar" );
374 $content2 = $rev->getContent( Revision
::RAW
);
375 $this->assertNotSame( $content, $content2, "expected a clone" ); // content is mutable, expect clone
376 $this->assertEquals( "foo", $content2->getText() ); // clone should contain the original text
378 $content2->setText( "bla bla" );
379 $this->assertEquals( "bar", $content->getText() ); // clones should be independent
384 * Tests whether $rev->getContent() returns the same object repeatedly if appropriate.
388 function testGetContentUncloned() {
389 $rev = $this->newTestRevision( "hello", "testGetContentUncloned_dummy", CONTENT_MODEL_WIKITEXT
);
390 $content = $rev->getContent( Revision
::RAW
);
391 $content2 = $rev->getContent( Revision
::RAW
);
393 // for immutable content like wikitext, this should be the same object
394 $this->assertSame( $content, $content2 );
399 class RevisionTestModifyableContent
extends TextContent
{
400 public function __construct( $text ) {
401 parent
::__construct( $text, "RevisionTestModifyableContent" );
404 public function copy( ) {
405 return new RevisionTestModifyableContent( $this->mText
);
408 public function getText() {
412 public function setText( $text ) {
413 $this->mText
= $text;
418 class RevisionTestModifyableContentHandler
extends TextContentHandler
{
420 public function __construct( ) {
421 parent
::__construct( "RevisionTestModifyableContent", array( CONTENT_FORMAT_TEXT
) );
424 public function unserializeContent( $text, $format = null ) {
425 $this->checkFormat( $format );
427 return new RevisionTestModifyableContent( $text );
430 public function makeEmptyContent() {
431 return new RevisionTestModifyableContent( '' );