3 class RevisionTest
extends MediaWikiTestCase
{
4 var $saveGlobals = array();
8 $wgContLang = Language
::factory( 'en' );
11 'wgLegacyEncoding' => false,
12 'wgCompressRevisions' => false,
14 'wgContentHandlerTextFallback' => $GLOBALS['wgContentHandlerTextFallback'],
15 'wgExtraNamespaces' => $GLOBALS['wgExtraNamespaces'],
16 'wgNamespaceContentModels' => $GLOBALS['wgNamespaceContentModels'],
17 'wgContentHandlers' => $GLOBALS['wgContentHandlers'],
20 foreach ( $globalSet as $var => $data ) {
21 $this->saveGlobals
[$var] = $GLOBALS[$var];
22 $GLOBALS[$var] = $data;
25 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
26 $wgExtraNamespaces[ 12312 ] = 'Dummy';
27 $wgExtraNamespaces[ 12313 ] = 'Dummy_talk';
29 $wgNamespaceContentModels[ 12312 ] = 'DUMMY';
30 $wgContentHandlers[ 'DUMMY' ] = 'DummyContentHandlerForTesting';
32 MWNamespace
::getCanonicalNamespaces( true ); # reset namespace cache
33 $wgContLang->resetNamespaces(); # reset namespace cache
35 global $wgContentHandlerTextFallback;
36 $wgContentHandlerTextFallback = 'ignore';
42 foreach ( $this->saveGlobals
as $var => $data ) {
43 $GLOBALS[$var] = $data;
46 MWNamespace
::getCanonicalNamespaces( true ); # reset namespace cache
47 $wgContLang->resetNamespaces(); # reset namespace cache
50 function testGetRevisionText() {
53 $row->old_text
= 'This is a bunch of revision text.';
55 'This is a bunch of revision text.',
56 Revision
::getRevisionText( $row ) );
59 function testGetRevisionTextGzip() {
60 if ( !function_exists( 'gzdeflate' ) ) {
61 $this->markTestSkipped( 'Gzip compression is not enabled (requires zlib).' );
64 $row->old_flags
= 'gzip';
65 $row->old_text
= gzdeflate( 'This is a bunch of revision text.' );
67 'This is a bunch of revision text.',
68 Revision
::getRevisionText( $row ) );
72 function testGetRevisionTextUtf8Native() {
74 $row->old_flags
= 'utf-8';
75 $row->old_text
= "Wiki est l'\xc3\xa9cole superieur !";
76 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
78 "Wiki est l'\xc3\xa9cole superieur !",
79 Revision
::getRevisionText( $row ) );
82 function testGetRevisionTextUtf8Legacy() {
85 $row->old_text
= "Wiki est l'\xe9cole superieur !";
86 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
88 "Wiki est l'\xc3\xa9cole superieur !",
89 Revision
::getRevisionText( $row ) );
92 function testGetRevisionTextUtf8NativeGzip() {
93 if ( !function_exists( 'gzdeflate' ) ) {
94 $this->markTestSkipped( 'Gzip compression is not enabled (requires zlib).' );
97 $row->old_flags
= 'gzip,utf-8';
98 $row->old_text
= gzdeflate( "Wiki est l'\xc3\xa9cole superieur !" );
99 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
101 "Wiki est l'\xc3\xa9cole superieur !",
102 Revision
::getRevisionText( $row ) );
106 function testGetRevisionTextUtf8LegacyGzip() {
107 if ( !function_exists( 'gzdeflate' ) ) {
108 $this->markTestSkipped( 'Gzip compression is not enabled (requires zlib).' );
111 $row->old_flags
= 'gzip';
112 $row->old_text
= gzdeflate( "Wiki est l'\xe9cole superieur !" );
113 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
115 "Wiki est l'\xc3\xa9cole superieur !",
116 Revision
::getRevisionText( $row ) );
120 function testCompressRevisionTextUtf8() {
122 $row->old_text
= "Wiki est l'\xc3\xa9cole superieur !";
123 $row->old_flags
= Revision
::compressRevisionText( $row->old_text
);
124 $this->assertTrue( false !== strpos( $row->old_flags
, 'utf-8' ),
125 "Flags should contain 'utf-8'" );
126 $this->assertFalse( false !== strpos( $row->old_flags
, 'gzip' ),
127 "Flags should not contain 'gzip'" );
128 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
129 $row->old_text
, "Direct check" );
130 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
131 Revision
::getRevisionText( $row ), "getRevisionText" );
134 function testCompressRevisionTextUtf8Gzip() {
135 $GLOBALS['wgCompressRevisions'] = true;
137 $row->old_text
= "Wiki est l'\xc3\xa9cole superieur !";
138 $row->old_flags
= Revision
::compressRevisionText( $row->old_text
);
139 $this->assertTrue( false !== strpos( $row->old_flags
, 'utf-8' ),
140 "Flags should contain 'utf-8'" );
141 $this->assertTrue( false !== strpos( $row->old_flags
, 'gzip' ),
142 "Flags should contain 'gzip'" );
143 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
144 gzinflate( $row->old_text
), "Direct check" );
145 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
146 Revision
::getRevisionText( $row ), "getRevisionText" );
149 # =================================================================================================================
152 * @param string $text
153 * @param string $title
154 * @param string $model
157 function newTestRevision( $text, $title = "Test", $model = CONTENT_MODEL_WIKITEXT
, $format = null ) {
158 if ( is_string( $title ) ) {
159 $title = Title
::newFromText( $title );
162 $content = ContentHandler
::makeContent( $text, $title, $model, $format );
170 'content' => $content,
171 'length' => $content->getSize(),
172 'comment' => "testing",
173 'minor_edit' => false,
175 'content_format' => $format,
182 function dataGetContentModel() {
184 array( 'hello world', 'Hello', null, null, CONTENT_MODEL_WIKITEXT
),
185 array( 'hello world', 'User:hello/there.css', null, null, CONTENT_MODEL_CSS
),
186 array( serialize('hello world'), 'Dummy:Hello', null, null, 'DUMMY' ),
191 * @dataProvider dataGetContentModel
193 function testGetContentModel( $text, $title, $model, $format, $expectedModel ) {
194 $rev = $this->newTestRevision( $text, $title, $model, $format );
196 $this->assertEquals( $expectedModel, $rev->getContentModelName() );
199 function dataGetContentFormat() {
201 array( 'hello world', 'Hello', null, null, 'application/x-wiki' ),
202 array( 'hello world', 'Hello', CONTENT_MODEL_CSS
, null, 'text/css' ),
203 array( 'hello world', 'User:hello/there.css', null, null, 'text/css' ),
204 array( serialize('hello world'), 'Dummy:Hello', null, null, 'dummy' ),
209 * @dataProvider dataGetContentFormat
211 function testGetContentFormat( $text, $title, $model, $format, $expectedFormat ) {
212 $rev = $this->newTestRevision( $text, $title, $model, $format );
214 $this->assertEquals( $expectedFormat, $rev->getContentFormat() );
217 function dataGetContentHandler() {
219 array( 'hello world', 'Hello', null, null, 'WikitextContentHandler' ),
220 array( 'hello world', 'User:hello/there.css', null, null, 'CssContentHandler' ),
221 array( serialize('hello world'), 'Dummy:Hello', null, null, 'DummyContentHandlerForTesting' ),
226 * @dataProvider dataGetContentHandler
228 function testGetContentHandler( $text, $title, $model, $format, $expectedClass ) {
229 $rev = $this->newTestRevision( $text, $title, $model, $format );
231 $this->assertEquals( $expectedClass, get_class( $rev->getContentHandler() ) );
234 function dataGetContent() {
236 array( 'hello world', 'Hello', null, null, Revision
::FOR_PUBLIC
, 'hello world' ),
237 array( serialize('hello world'), 'Hello', 'DUMMY', null, Revision
::FOR_PUBLIC
, serialize('hello world') ),
238 array( serialize('hello world'), 'Dummy:Hello', null, null, Revision
::FOR_PUBLIC
, serialize('hello world') ),
243 * @dataProvider dataGetContent
245 function testGetContent( $text, $title, $model, $format, $audience, $expectedSerialization ) {
246 $rev = $this->newTestRevision( $text, $title, $model, $format );
247 $content = $rev->getContent( $audience );
249 $this->assertEquals( $expectedSerialization, is_null( $content ) ?
null : $content->serialize( $format ) );
252 function dataGetText() {
254 array( 'hello world', 'Hello', null, null, Revision
::FOR_PUBLIC
, 'hello world' ),
255 array( serialize('hello world'), 'Hello', 'DUMMY', null, Revision
::FOR_PUBLIC
, null ),
256 array( serialize('hello world'), 'Dummy:Hello', null, null, Revision
::FOR_PUBLIC
, null ),
261 * @dataProvider dataGetText
263 function testGetText( $text, $title, $model, $format, $audience, $expectedText ) {
264 $rev = $this->newTestRevision( $text, $title, $model, $format );
266 $this->assertEquals( $expectedText, $rev->getText( $audience ) );
270 * @dataProvider dataGetText
272 function testGetRawText( $text, $title, $model, $format, $audience, $expectedText ) {
273 $rev = $this->newTestRevision( $text, $title, $model, $format );
275 $this->assertEquals( $expectedText, $rev->getRawText( $audience ) );
278 // @todo: set up testing environment with database to tgest loading and inserting revisions