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';
35 MWNamespace
::getCanonicalNamespaces( true ); # reset namespace cache
36 $wgContLang->resetNamespaces(); # reset namespace cache
38 global $wgContentHandlerTextFallback;
39 $wgContentHandlerTextFallback = 'ignore';
45 foreach ( $this->saveGlobals
as $var => $data ) {
46 $GLOBALS[$var] = $data;
49 MWNamespace
::getCanonicalNamespaces( true ); # reset namespace cache
50 $wgContLang->resetNamespaces(); # reset namespace cache
53 function testGetRevisionText() {
56 $row->old_text
= 'This is a bunch of revision text.';
58 'This is a bunch of revision text.',
59 Revision
::getRevisionText( $row ) );
62 function testGetRevisionTextGzip() {
63 if ( !function_exists( 'gzdeflate' ) ) {
64 $this->markTestSkipped( 'Gzip compression is not enabled (requires zlib).' );
67 $row->old_flags
= 'gzip';
68 $row->old_text
= gzdeflate( 'This is a bunch of revision text.' );
70 'This is a bunch of revision text.',
71 Revision
::getRevisionText( $row ) );
75 function testGetRevisionTextUtf8Native() {
77 $row->old_flags
= 'utf-8';
78 $row->old_text
= "Wiki est l'\xc3\xa9cole superieur !";
79 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
81 "Wiki est l'\xc3\xa9cole superieur !",
82 Revision
::getRevisionText( $row ) );
85 function testGetRevisionTextUtf8Legacy() {
88 $row->old_text
= "Wiki est l'\xe9cole superieur !";
89 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
91 "Wiki est l'\xc3\xa9cole superieur !",
92 Revision
::getRevisionText( $row ) );
95 function testGetRevisionTextUtf8NativeGzip() {
96 if ( !function_exists( 'gzdeflate' ) ) {
97 $this->markTestSkipped( 'Gzip compression is not enabled (requires zlib).' );
100 $row->old_flags
= 'gzip,utf-8';
101 $row->old_text
= gzdeflate( "Wiki est l'\xc3\xa9cole superieur !" );
102 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
104 "Wiki est l'\xc3\xa9cole superieur !",
105 Revision
::getRevisionText( $row ) );
109 function testGetRevisionTextUtf8LegacyGzip() {
110 if ( !function_exists( 'gzdeflate' ) ) {
111 $this->markTestSkipped( 'Gzip compression is not enabled (requires zlib).' );
114 $row->old_flags
= 'gzip';
115 $row->old_text
= gzdeflate( "Wiki est l'\xe9cole superieur !" );
116 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
118 "Wiki est l'\xc3\xa9cole superieur !",
119 Revision
::getRevisionText( $row ) );
123 function testCompressRevisionTextUtf8() {
125 $row->old_text
= "Wiki est l'\xc3\xa9cole superieur !";
126 $row->old_flags
= Revision
::compressRevisionText( $row->old_text
);
127 $this->assertTrue( false !== strpos( $row->old_flags
, 'utf-8' ),
128 "Flags should contain 'utf-8'" );
129 $this->assertFalse( false !== strpos( $row->old_flags
, 'gzip' ),
130 "Flags should not contain 'gzip'" );
131 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
132 $row->old_text
, "Direct check" );
133 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
134 Revision
::getRevisionText( $row ), "getRevisionText" );
137 function testCompressRevisionTextUtf8Gzip() {
138 $GLOBALS['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() {
187 array( 'hello world', 'Hello', null, null, CONTENT_MODEL_WIKITEXT
),
188 array( 'hello world', 'User:hello/there.css', null, null, CONTENT_MODEL_CSS
),
189 array( serialize('hello world'), 'Dummy:Hello', null, null, "testing" ),
195 * @dataProvider dataGetContentModel
197 function testGetContentModel( $text, $title, $model, $format, $expectedModel ) {
198 $rev = $this->newTestRevision( $text, $title, $model, $format );
200 $this->assertEquals( $expectedModel, $rev->getContentModel() );
203 function dataGetContentFormat() {
205 array( 'hello world', 'Hello', null, null, CONTENT_FORMAT_WIKITEXT
),
206 array( 'hello world', 'Hello', CONTENT_MODEL_CSS
, null, CONTENT_FORMAT_CSS
),
207 array( 'hello world', 'User:hello/there.css', null, null, CONTENT_FORMAT_CSS
),
208 array( serialize('hello world'), 'Dummy:Hello', null, null, "testing" ),
214 * @dataProvider dataGetContentFormat
216 function testGetContentFormat( $text, $title, $model, $format, $expectedFormat ) {
217 $rev = $this->newTestRevision( $text, $title, $model, $format );
219 $this->assertEquals( $expectedFormat, $rev->getContentFormat() );
222 function dataGetContentHandler() {
224 array( 'hello world', 'Hello', null, null, 'WikitextContentHandler' ),
225 array( 'hello world', 'User:hello/there.css', null, null, 'CssContentHandler' ),
226 array( serialize('hello world'), 'Dummy:Hello', null, null, 'DummyContentHandlerForTesting' ),
232 * @dataProvider dataGetContentHandler
234 function testGetContentHandler( $text, $title, $model, $format, $expectedClass ) {
235 $rev = $this->newTestRevision( $text, $title, $model, $format );
237 $this->assertEquals( $expectedClass, get_class( $rev->getContentHandler() ) );
240 function dataGetContent() {
242 array( 'hello world', 'Hello', null, null, Revision
::FOR_PUBLIC
, 'hello world' ),
243 array( serialize('hello world'), 'Hello', "testing", null, Revision
::FOR_PUBLIC
, serialize('hello world') ),
244 array( serialize('hello world'), 'Dummy:Hello', null, null, Revision
::FOR_PUBLIC
, serialize('hello world') ),
250 * @dataProvider dataGetContent
252 function testGetContent( $text, $title, $model, $format, $audience, $expectedSerialization ) {
253 $rev = $this->newTestRevision( $text, $title, $model, $format );
254 $content = $rev->getContent( $audience );
256 $this->assertEquals( $expectedSerialization, is_null( $content ) ?
null : $content->serialize( $format ) );
259 function dataGetText() {
261 array( 'hello world', 'Hello', null, null, Revision
::FOR_PUBLIC
, 'hello world' ),
262 array( serialize('hello world'), 'Hello', "testing", null, Revision
::FOR_PUBLIC
, null ),
263 array( serialize('hello world'), 'Dummy:Hello', null, null, Revision
::FOR_PUBLIC
, null ),
269 * @dataProvider dataGetText
271 function testGetText( $text, $title, $model, $format, $audience, $expectedText ) {
272 $rev = $this->newTestRevision( $text, $title, $model, $format );
274 $this->assertEquals( $expectedText, $rev->getText( $audience ) );
279 * @dataProvider dataGetText
281 function testGetRawText( $text, $title, $model, $format, $audience, $expectedText ) {
282 $rev = $this->newTestRevision( $text, $title, $model, $format );
284 $this->assertEquals( $expectedText, $rev->getRawText( $audience ) );
288 public function dataGetSize( ) {
290 array( "hello world.", null, 12 ),
291 array( serialize( "hello world." ), "testing", 12 ),
296 * @covers Revision::getSize
298 * @dataProvider dataGetSize
300 public function testGetSize( $text, $model, $expected_size )
302 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSize', $model );
303 $this->assertEquals( $expected_size, $rev->getSize() );
306 public function dataGetSha1( ) {
308 array( "hello world.", null, Revision
::base36Sha1( "hello world." ) ),
309 array( serialize( "hello world." ), "testing", Revision
::base36Sha1( serialize( "hello world." ) ) ),
314 * @covers Revision::getSha1
316 * @dataProvider dataGetSha1
318 public function testGetSha1( $text, $model, $expected_hash )
320 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSha1', $model );
321 $this->assertEquals( $expected_hash, $rev->getSha1() );
324 public function testConstructWithText() {
325 $rev = new Revision( array(
326 'text' => 'hello world.',
327 'content_model' => CONTENT_MODEL_JAVASCRIPT
330 $this->assertNotNull( $rev->getText(), 'no content text' );
331 $this->assertNotNull( $rev->getContent(), 'no content object available' );
332 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContent()->getModel() );
333 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContentModel() );
336 public function testConstructWithContent() {
337 $title = Title
::newFromText( 'RevisionTest_testConstructWithContent' );
339 $rev = new Revision( array(
340 'content' => ContentHandler
::makeContent( 'hello world.', $title, CONTENT_MODEL_JAVASCRIPT
),
343 $this->assertNotNull( $rev->getText(), 'no content text' );
344 $this->assertNotNull( $rev->getContent(), 'no content object available' );
345 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContent()->getModel() );
346 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContentModel() );