reset namespace caches when testing extra namespace handling
[lhc/web/wiklou.git] / tests / phpunit / includes / RevisionTest.php
1 <?php
2
3 class RevisionTest extends MediaWikiTestCase {
4 var $saveGlobals = array();
5
6 function setUp() {
7 global $wgContLang;
8 $wgContLang = Language::factory( 'en' );
9
10 $globalSet = array(
11 'wgLegacyEncoding' => false,
12 'wgCompressRevisions' => false,
13
14 'wgContentHandlerTextFallback' => $GLOBALS['wgContentHandlerTextFallback'],
15 'wgExtraNamespaces' => $GLOBALS['wgExtraNamespaces'],
16 'wgNamespaceContentModels' => $GLOBALS['wgNamespaceContentModels'],
17 'wgContentHandlers' => $GLOBALS['wgContentHandlers'],
18 );
19
20 foreach ( $globalSet as $var => $data ) {
21 $this->saveGlobals[$var] = $GLOBALS[$var];
22 $GLOBALS[$var] = $data;
23 }
24
25 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
26 $wgExtraNamespaces[ 12312 ] = 'Dummy';
27 $wgExtraNamespaces[ 12313 ] = 'Dummy_talk';
28
29 $wgNamespaceContentModels[ 12312 ] = 'DUMMY';
30 $wgContentHandlers[ 'DUMMY' ] = 'DummyContentHandlerForTesting';
31
32 MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
33 $wgContLang->resetNamespaces(); # reset namespace cache
34
35 global $wgContentHandlerTextFallback;
36 $wgContentHandlerTextFallback = 'ignore';
37 }
38
39 function tearDown() {
40 global $wgContLang;
41
42 foreach ( $this->saveGlobals as $var => $data ) {
43 $GLOBALS[$var] = $data;
44 }
45
46 MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
47 $wgContLang->resetNamespaces(); # reset namespace cache
48 }
49
50 function testGetRevisionText() {
51 $row = new stdClass;
52 $row->old_flags = '';
53 $row->old_text = 'This is a bunch of revision text.';
54 $this->assertEquals(
55 'This is a bunch of revision text.',
56 Revision::getRevisionText( $row ) );
57 }
58
59 function testGetRevisionTextGzip() {
60 if ( !function_exists( 'gzdeflate' ) ) {
61 $this->markTestSkipped( 'Gzip compression is not enabled (requires zlib).' );
62 } else {
63 $row = new stdClass;
64 $row->old_flags = 'gzip';
65 $row->old_text = gzdeflate( 'This is a bunch of revision text.' );
66 $this->assertEquals(
67 'This is a bunch of revision text.',
68 Revision::getRevisionText( $row ) );
69 }
70 }
71
72 function testGetRevisionTextUtf8Native() {
73 $row = new stdClass;
74 $row->old_flags = 'utf-8';
75 $row->old_text = "Wiki est l'\xc3\xa9cole superieur !";
76 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
77 $this->assertEquals(
78 "Wiki est l'\xc3\xa9cole superieur !",
79 Revision::getRevisionText( $row ) );
80 }
81
82 function testGetRevisionTextUtf8Legacy() {
83 $row = new stdClass;
84 $row->old_flags = '';
85 $row->old_text = "Wiki est l'\xe9cole superieur !";
86 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
87 $this->assertEquals(
88 "Wiki est l'\xc3\xa9cole superieur !",
89 Revision::getRevisionText( $row ) );
90 }
91
92 function testGetRevisionTextUtf8NativeGzip() {
93 if ( !function_exists( 'gzdeflate' ) ) {
94 $this->markTestSkipped( 'Gzip compression is not enabled (requires zlib).' );
95 } else {
96 $row = new stdClass;
97 $row->old_flags = 'gzip,utf-8';
98 $row->old_text = gzdeflate( "Wiki est l'\xc3\xa9cole superieur !" );
99 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
100 $this->assertEquals(
101 "Wiki est l'\xc3\xa9cole superieur !",
102 Revision::getRevisionText( $row ) );
103 }
104 }
105
106 function testGetRevisionTextUtf8LegacyGzip() {
107 if ( !function_exists( 'gzdeflate' ) ) {
108 $this->markTestSkipped( 'Gzip compression is not enabled (requires zlib).' );
109 } else {
110 $row = new stdClass;
111 $row->old_flags = 'gzip';
112 $row->old_text = gzdeflate( "Wiki est l'\xe9cole superieur !" );
113 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
114 $this->assertEquals(
115 "Wiki est l'\xc3\xa9cole superieur !",
116 Revision::getRevisionText( $row ) );
117 }
118 }
119
120 function testCompressRevisionTextUtf8() {
121 $row = new stdClass;
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" );
132 }
133
134 function testCompressRevisionTextUtf8Gzip() {
135 $GLOBALS['wgCompressRevisions'] = true;
136 $row = new stdClass;
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" );
147 }
148
149 # =================================================================================================================
150
151 /**
152 * @param string $text
153 * @param string $title
154 * @param string $model
155 * @return Revision
156 */
157 function newTestRevision( $text, $title = "Test", $model = CONTENT_MODEL_WIKITEXT, $format = null ) {
158 if ( is_string( $title ) ) {
159 $title = Title::newFromText( $title );
160 }
161
162 $content = ContentHandler::makeContent( $text, $title, $model, $format );
163
164 $rev = new Revision(
165 array(
166 'id' => 42,
167 'page' => 23,
168 'title' => $title,
169
170 'content' => $content,
171 'length' => $content->getSize(),
172 'comment' => "testing",
173 'minor_edit' => false,
174
175 'content_format' => $format,
176 )
177 );
178
179 return $rev;
180 }
181
182 function dataGetContentModel() {
183 return array(
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' ),
187 );
188 }
189
190 /**
191 * @dataProvider dataGetContentModel
192 */
193 function testGetContentModel( $text, $title, $model, $format, $expectedModel ) {
194 $rev = $this->newTestRevision( $text, $title, $model, $format );
195
196 $this->assertEquals( $expectedModel, $rev->getContentModelName() );
197 }
198
199 function dataGetContentFormat() {
200 return array(
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' ),
205 );
206 }
207
208 /**
209 * @dataProvider dataGetContentFormat
210 */
211 function testGetContentFormat( $text, $title, $model, $format, $expectedFormat ) {
212 $rev = $this->newTestRevision( $text, $title, $model, $format );
213
214 $this->assertEquals( $expectedFormat, $rev->getContentFormat() );
215 }
216
217 function dataGetContentHandler() {
218 return array(
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' ),
222 );
223 }
224
225 /**
226 * @dataProvider dataGetContentHandler
227 */
228 function testGetContentHandler( $text, $title, $model, $format, $expectedClass ) {
229 $rev = $this->newTestRevision( $text, $title, $model, $format );
230
231 $this->assertEquals( $expectedClass, get_class( $rev->getContentHandler() ) );
232 }
233
234 function dataGetContent() {
235 return array(
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') ),
239 );
240 }
241
242 /**
243 * @dataProvider dataGetContent
244 */
245 function testGetContent( $text, $title, $model, $format, $audience, $expectedSerialization ) {
246 $rev = $this->newTestRevision( $text, $title, $model, $format );
247 $content = $rev->getContent( $audience );
248
249 $this->assertEquals( $expectedSerialization, is_null( $content ) ? null : $content->serialize( $format ) );
250 }
251
252 function dataGetText() {
253 return array(
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 ),
257 );
258 }
259
260 /**
261 * @dataProvider dataGetText
262 */
263 function testGetText( $text, $title, $model, $format, $audience, $expectedText ) {
264 $rev = $this->newTestRevision( $text, $title, $model, $format );
265
266 $this->assertEquals( $expectedText, $rev->getText( $audience ) );
267 }
268
269 /**
270 * @dataProvider dataGetText
271 */
272 function testGetRawText( $text, $title, $model, $format, $audience, $expectedText ) {
273 $rev = $this->newTestRevision( $text, $title, $model, $format );
274
275 $this->assertEquals( $expectedText, $rev->getRawText( $audience ) );
276 }
277
278 // @todo: set up testing environment with database to tgest loading and inserting revisions
279
280 }
281
282