[bug 37746] string ids for content model and format.
[lhc/web/wiklou.git] / tests / phpunit / includes / RevisionTest.php
1 <?php
2
3 /**
4 * @group ContentHandler
5 */
6 class RevisionTest extends MediaWikiTestCase {
7 var $saveGlobals = array();
8
9 function setUp() {
10 global $wgContLang;
11 $wgContLang = Language::factory( 'en' );
12
13 $globalSet = array(
14 'wgLegacyEncoding' => false,
15 'wgCompressRevisions' => false,
16
17 'wgContentHandlerTextFallback' => $GLOBALS['wgContentHandlerTextFallback'],
18 'wgExtraNamespaces' => $GLOBALS['wgExtraNamespaces'],
19 'wgNamespaceContentModels' => $GLOBALS['wgNamespaceContentModels'],
20 'wgContentHandlers' => $GLOBALS['wgContentHandlers'],
21 );
22
23 foreach ( $globalSet as $var => $data ) {
24 $this->saveGlobals[$var] = $GLOBALS[$var];
25 $GLOBALS[$var] = $data;
26 }
27
28 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
29 $wgExtraNamespaces[ 12312 ] = 'Dummy';
30 $wgExtraNamespaces[ 12313 ] = 'Dummy_talk';
31
32 $wgNamespaceContentModels[ 12312 ] = "testing";
33 $wgContentHandlers[ "testing" ] = 'DummyContentHandlerForTesting';
34
35 MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
36 $wgContLang->resetNamespaces(); # reset namespace cache
37
38 global $wgContentHandlerTextFallback;
39 $wgContentHandlerTextFallback = 'ignore';
40 }
41
42 function tearDown() {
43 global $wgContLang;
44
45 foreach ( $this->saveGlobals as $var => $data ) {
46 $GLOBALS[$var] = $data;
47 }
48
49 MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
50 $wgContLang->resetNamespaces(); # reset namespace cache
51 }
52
53 function testGetRevisionText() {
54 $row = new stdClass;
55 $row->old_flags = '';
56 $row->old_text = 'This is a bunch of revision text.';
57 $this->assertEquals(
58 'This is a bunch of revision text.',
59 Revision::getRevisionText( $row ) );
60 }
61
62 function testGetRevisionTextGzip() {
63 if ( !function_exists( 'gzdeflate' ) ) {
64 $this->markTestSkipped( 'Gzip compression is not enabled (requires zlib).' );
65 } else {
66 $row = new stdClass;
67 $row->old_flags = 'gzip';
68 $row->old_text = gzdeflate( 'This is a bunch of revision text.' );
69 $this->assertEquals(
70 'This is a bunch of revision text.',
71 Revision::getRevisionText( $row ) );
72 }
73 }
74
75 function testGetRevisionTextUtf8Native() {
76 $row = new stdClass;
77 $row->old_flags = 'utf-8';
78 $row->old_text = "Wiki est l'\xc3\xa9cole superieur !";
79 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
80 $this->assertEquals(
81 "Wiki est l'\xc3\xa9cole superieur !",
82 Revision::getRevisionText( $row ) );
83 }
84
85 function testGetRevisionTextUtf8Legacy() {
86 $row = new stdClass;
87 $row->old_flags = '';
88 $row->old_text = "Wiki est l'\xe9cole superieur !";
89 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
90 $this->assertEquals(
91 "Wiki est l'\xc3\xa9cole superieur !",
92 Revision::getRevisionText( $row ) );
93 }
94
95 function testGetRevisionTextUtf8NativeGzip() {
96 if ( !function_exists( 'gzdeflate' ) ) {
97 $this->markTestSkipped( 'Gzip compression is not enabled (requires zlib).' );
98 } else {
99 $row = new stdClass;
100 $row->old_flags = 'gzip,utf-8';
101 $row->old_text = gzdeflate( "Wiki est l'\xc3\xa9cole superieur !" );
102 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
103 $this->assertEquals(
104 "Wiki est l'\xc3\xa9cole superieur !",
105 Revision::getRevisionText( $row ) );
106 }
107 }
108
109 function testGetRevisionTextUtf8LegacyGzip() {
110 if ( !function_exists( 'gzdeflate' ) ) {
111 $this->markTestSkipped( 'Gzip compression is not enabled (requires zlib).' );
112 } else {
113 $row = new stdClass;
114 $row->old_flags = 'gzip';
115 $row->old_text = gzdeflate( "Wiki est l'\xe9cole superieur !" );
116 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
117 $this->assertEquals(
118 "Wiki est l'\xc3\xa9cole superieur !",
119 Revision::getRevisionText( $row ) );
120 }
121 }
122
123 function testCompressRevisionTextUtf8() {
124 $row = new stdClass;
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" );
135 }
136
137 function testCompressRevisionTextUtf8Gzip() {
138 $GLOBALS['wgCompressRevisions'] = true;
139 $row = new stdClass;
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" );
150 }
151
152 # =================================================================================================================
153
154 /**
155 * @param string $text
156 * @param string $title
157 * @param string $model
158 * @return Revision
159 */
160 function newTestRevision( $text, $title = "Test", $model = CONTENT_MODEL_WIKITEXT, $format = null ) {
161 if ( is_string( $title ) ) {
162 $title = Title::newFromText( $title );
163 }
164
165 $content = ContentHandler::makeContent( $text, $title, $model, $format );
166
167 $rev = new Revision(
168 array(
169 'id' => 42,
170 'page' => 23,
171 'title' => $title,
172
173 'content' => $content,
174 'length' => $content->getSize(),
175 'comment' => "testing",
176 'minor_edit' => false,
177
178 'content_format' => $format,
179 )
180 );
181
182 return $rev;
183 }
184
185 function dataGetContentModel() {
186 return array(
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" ),
190 );
191 }
192
193 /**
194 * @dataProvider dataGetContentModel
195 */
196 function testGetContentModel( $text, $title, $model, $format, $expectedModel ) {
197 $rev = $this->newTestRevision( $text, $title, $model, $format );
198
199 $this->assertEquals( $expectedModel, $rev->getContentModel() );
200 }
201
202 function dataGetContentFormat() {
203 return array(
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, "testing" ),
208 );
209 }
210
211 /**
212 * @dataProvider dataGetContentFormat
213 */
214 function testGetContentFormat( $text, $title, $model, $format, $expectedFormat ) {
215 $rev = $this->newTestRevision( $text, $title, $model, $format );
216
217 $this->assertEquals( $expectedFormat, $rev->getContentFormat() );
218 }
219
220 function dataGetContentHandler() {
221 return array(
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' ),
225 );
226 }
227
228 /**
229 * @dataProvider dataGetContentHandler
230 */
231 function testGetContentHandler( $text, $title, $model, $format, $expectedClass ) {
232 $rev = $this->newTestRevision( $text, $title, $model, $format );
233
234 $this->assertEquals( $expectedClass, get_class( $rev->getContentHandler() ) );
235 }
236
237 function dataGetContent() {
238 return array(
239 array( 'hello world', 'Hello', null, null, Revision::FOR_PUBLIC, 'hello world' ),
240 array( serialize('hello world'), 'Hello', "testing", null, Revision::FOR_PUBLIC, serialize('hello world') ),
241 array( serialize('hello world'), 'Dummy:Hello', null, null, Revision::FOR_PUBLIC, serialize('hello world') ),
242 );
243 }
244
245 /**
246 * @dataProvider dataGetContent
247 */
248 function testGetContent( $text, $title, $model, $format, $audience, $expectedSerialization ) {
249 $rev = $this->newTestRevision( $text, $title, $model, $format );
250 $content = $rev->getContent( $audience );
251
252 $this->assertEquals( $expectedSerialization, is_null( $content ) ? null : $content->serialize( $format ) );
253 }
254
255 function dataGetText() {
256 return array(
257 array( 'hello world', 'Hello', null, null, Revision::FOR_PUBLIC, 'hello world' ),
258 array( serialize('hello world'), 'Hello', "testing", null, Revision::FOR_PUBLIC, null ),
259 array( serialize('hello world'), 'Dummy:Hello', null, null, Revision::FOR_PUBLIC, null ),
260 );
261 }
262
263 /**
264 * @dataProvider dataGetText
265 */
266 function testGetText( $text, $title, $model, $format, $audience, $expectedText ) {
267 $rev = $this->newTestRevision( $text, $title, $model, $format );
268
269 $this->assertEquals( $expectedText, $rev->getText( $audience ) );
270 }
271
272 /**
273 * @dataProvider dataGetText
274 */
275 function testGetRawText( $text, $title, $model, $format, $audience, $expectedText ) {
276 $rev = $this->newTestRevision( $text, $title, $model, $format );
277
278 $this->assertEquals( $expectedText, $rev->getRawText( $audience ) );
279 }
280
281
282 public function dataGetSize( ) {
283 return array(
284 array( "hello world.", null, 12 ),
285 array( serialize( "hello world." ), "testing", 12 ),
286 );
287 }
288
289 /**
290 * @covers Revision::getSize
291 * @dataProvider dataGetSize
292 */
293 public function testGetSize( $text, $model, $expected_size )
294 {
295 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSize', $model );
296 $this->assertEquals( $expected_size, $rev->getSize() );
297 }
298
299 public function dataGetSha1( ) {
300 return array(
301 array( "hello world.", null, Revision::base36Sha1( "hello world." ) ),
302 array( serialize( "hello world." ), "testing", Revision::base36Sha1( serialize( "hello world." ) ) ),
303 );
304 }
305
306 /**
307 * @covers Revision::getSha1
308 * @dataProvider dataGetSha1
309 */
310 public function testGetSha1( $text, $model, $expected_hash )
311 {
312 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSha1', $model );
313 $this->assertEquals( $expected_hash, $rev->getSha1() );
314 }
315
316 public function testConstructWithText() {
317 $rev = new Revision( array(
318 'text' => 'hello world.',
319 'content_model' => CONTENT_MODEL_JAVASCRIPT
320 ));
321
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() );
326 }
327
328 public function testConstructWithContent() {
329 $title = Title::newFromText( 'RevisionTest_testConstructWithContent' );
330
331 $rev = new Revision( array(
332 'content' => ContentHandler::makeContent( 'hello world.', $title, CONTENT_MODEL_JAVASCRIPT ),
333 ));
334
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() );
339 }
340
341 }
342
343