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 ] = 999999;
33 $wgContentHandlers[ 999999 ] = '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, 999999 ),
194 * @dataProvider dataGetContentModel
196 function testGetContentModel( $text, $title, $model, $format, $expectedModel ) {
197 $rev = $this->newTestRevision( $text, $title, $model, $format );
199 $this->assertEquals( $expectedModel, $rev->getContentModel() );
202 function dataGetContentFormat() {
204 array( 'hello world', 'Hello', null, null, CONTENT_FORMAT_WIKITEXT
),
205 array( 'hello world', 'Hello', CONTENT_MODEL_CSS
, null, CONTENT_FORMAT_CSS
),
206 array( 'hello world', 'User:hello/there.css', null, null, CONTENT_FORMAT_CSS
),
207 array( serialize('hello world'), 'Dummy:Hello', null, null, 999999 ),
212 * @dataProvider dataGetContentFormat
214 function testGetContentFormat( $text, $title, $model, $format, $expectedFormat ) {
215 $rev = $this->newTestRevision( $text, $title, $model, $format );
217 $this->assertEquals( $expectedFormat, $rev->getContentFormat() );
220 function dataGetContentHandler() {
222 array( 'hello world', 'Hello', null, null, 'WikitextContentHandler' ),
223 array( 'hello world', 'User:hello/there.css', null, null, 'CssContentHandler' ),
224 array( serialize('hello world'), 'Dummy:Hello', null, null, 'DummyContentHandlerForTesting' ),
229 * @dataProvider dataGetContentHandler
231 function testGetContentHandler( $text, $title, $model, $format, $expectedClass ) {
232 $rev = $this->newTestRevision( $text, $title, $model, $format );
234 $this->assertEquals( $expectedClass, get_class( $rev->getContentHandler() ) );
237 function dataGetContent() {
239 array( 'hello world', 'Hello', null, null, Revision
::FOR_PUBLIC
, 'hello world' ),
240 array( serialize('hello world'), 'Hello', 999999, null, Revision
::FOR_PUBLIC
, serialize('hello world') ),
241 array( serialize('hello world'), 'Dummy:Hello', null, null, Revision
::FOR_PUBLIC
, serialize('hello world') ),
246 * @dataProvider dataGetContent
248 function testGetContent( $text, $title, $model, $format, $audience, $expectedSerialization ) {
249 $rev = $this->newTestRevision( $text, $title, $model, $format );
250 $content = $rev->getContent( $audience );
252 $this->assertEquals( $expectedSerialization, is_null( $content ) ?
null : $content->serialize( $format ) );
255 function dataGetText() {
257 array( 'hello world', 'Hello', null, null, Revision
::FOR_PUBLIC
, 'hello world' ),
258 array( serialize('hello world'), 'Hello', 999999, null, Revision
::FOR_PUBLIC
, null ),
259 array( serialize('hello world'), 'Dummy:Hello', null, null, Revision
::FOR_PUBLIC
, null ),
264 * @dataProvider dataGetText
266 function testGetText( $text, $title, $model, $format, $audience, $expectedText ) {
267 $rev = $this->newTestRevision( $text, $title, $model, $format );
269 $this->assertEquals( $expectedText, $rev->getText( $audience ) );
273 * @dataProvider dataGetText
275 function testGetRawText( $text, $title, $model, $format, $audience, $expectedText ) {
276 $rev = $this->newTestRevision( $text, $title, $model, $format );
278 $this->assertEquals( $expectedText, $rev->getRawText( $audience ) );
282 public function dataGetSize( ) {
284 array( "hello world.", null, 12 ),
285 array( serialize( "hello world." ), 999999, 12 ),
290 * @covers Revision::getSize
291 * @dataProvider dataGetSize
293 public function testGetSize( $text, $model, $expected_size )
295 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSize', $model );
296 $this->assertEquals( $expected_size, $rev->getSize() );
299 public function dataGetSha1( ) {
301 array( "hello world.", null, Revision
::base36Sha1( "hello world." ) ),
302 array( serialize( "hello world." ), 999999, Revision
::base36Sha1( serialize( "hello world." ) ) ),
307 * @covers Revision::getSha1
308 * @dataProvider dataGetSha1
310 public function testGetSha1( $text, $model, $expected_hash )
312 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSha1', $model );
313 $this->assertEquals( $expected_hash, $rev->getSha1() );
316 public function testConstructWithText() {
317 $rev = new Revision( array(
318 'text' => 'hello world.',
319 'content_model' => CONTENT_MODEL_JAVASCRIPT
322 $this->assertNotNull( $rev->getText(), 'no content text' );
323 $this->assertNotNull( $rev->getContent(), 'no content object available' );
324 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContent()->getModel() );
325 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContentModel() );
328 public function testConstructWithContent() {
329 $title = Title
::newFromText( 'RevisionTest_testConstructWithContent' );
331 $rev = new Revision( array(
332 'content' => ContentHandler
::makeContent( 'hello world.', $title, CONTENT_MODEL_JAVASCRIPT
),
335 $this->assertNotNull( $rev->getText(), 'no content text' );
336 $this->assertNotNull( $rev->getContent(), 'no content object available' );
337 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContent()->getModel() );
338 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContentModel() );