4623b3834127e91c2d65217cc91d4d4d1e9d0bcf
[lhc/web/wiklou.git] / tests / phpunit / includes / RevisionTest.php
1 <?php
2
3 /**
4 * @group ContentHandler
5 */
6 class RevisionTest extends MediaWikiTestCase {
7 protected function setUp() {
8 global $wgContLang;
9
10 parent::setUp();
11
12 $this->setMwGlobals( array(
13 'wgContLang' => Language::factory( 'en' ),
14 'wgLanguageCode' => 'en',
15 'wgLegacyEncoding' => false,
16 'wgCompressRevisions' => false,
17
18 'wgContentHandlerTextFallback' => 'ignore',
19 ) );
20
21 $this->mergeMwGlobalArrayValue(
22 'wgExtraNamespaces',
23 array(
24 12312 => 'Dummy',
25 12313 => 'Dummy_talk',
26 )
27 );
28
29 $this->mergeMwGlobalArrayValue(
30 'wgNamespaceContentModels',
31 array(
32 12312 => 'testing',
33 )
34 );
35
36 $this->mergeMwGlobalArrayValue(
37 'wgContentHandlers',
38 array(
39 'testing' => 'DummyContentHandlerForTesting',
40 'RevisionTestModifyableContent' => 'RevisionTestModifyableContentHandler',
41 )
42 );
43
44 MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
45 $wgContLang->resetNamespaces(); # reset namespace cache
46 }
47
48 function tearDown() {
49 global $wgContLang;
50
51 MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
52 $wgContLang->resetNamespaces(); # reset namespace cache
53
54 parent::tearDown();
55 }
56
57 /**
58 * @covers Revision::getRevisionText
59 */
60 public function testGetRevisionText() {
61 $row = new stdClass;
62 $row->old_flags = '';
63 $row->old_text = 'This is a bunch of revision text.';
64 $this->assertEquals(
65 'This is a bunch of revision text.',
66 Revision::getRevisionText( $row ) );
67 }
68
69 /**
70 * @covers Revision::getRevisionText
71 */
72 public function testGetRevisionTextGzip() {
73 $this->checkPHPExtension( 'zlib' );
74
75 $row = new stdClass;
76 $row->old_flags = 'gzip';
77 $row->old_text = gzdeflate( 'This is a bunch of revision text.' );
78 $this->assertEquals(
79 'This is a bunch of revision text.',
80 Revision::getRevisionText( $row ) );
81 }
82
83 /**
84 * @covers Revision::getRevisionText
85 */
86 public function testGetRevisionTextUtf8Native() {
87 $row = new stdClass;
88 $row->old_flags = 'utf-8';
89 $row->old_text = "Wiki est l'\xc3\xa9cole superieur !";
90 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
91 $this->assertEquals(
92 "Wiki est l'\xc3\xa9cole superieur !",
93 Revision::getRevisionText( $row ) );
94 }
95
96 /**
97 * @covers Revision::getRevisionText
98 */
99 public function testGetRevisionTextUtf8Legacy() {
100 $row = new stdClass;
101 $row->old_flags = '';
102 $row->old_text = "Wiki est l'\xe9cole superieur !";
103 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
104 $this->assertEquals(
105 "Wiki est l'\xc3\xa9cole superieur !",
106 Revision::getRevisionText( $row ) );
107 }
108
109 /**
110 * @covers Revision::getRevisionText
111 */
112 public function testGetRevisionTextUtf8NativeGzip() {
113 $this->checkPHPExtension( 'zlib' );
114
115 $row = new stdClass;
116 $row->old_flags = 'gzip,utf-8';
117 $row->old_text = gzdeflate( "Wiki est l'\xc3\xa9cole superieur !" );
118 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
119 $this->assertEquals(
120 "Wiki est l'\xc3\xa9cole superieur !",
121 Revision::getRevisionText( $row ) );
122 }
123
124 /**
125 * @covers Revision::getRevisionText
126 */
127 public function testGetRevisionTextUtf8LegacyGzip() {
128 $this->checkPHPExtension( 'zlib' );
129
130 $row = new stdClass;
131 $row->old_flags = 'gzip';
132 $row->old_text = gzdeflate( "Wiki est l'\xe9cole superieur !" );
133 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
134 $this->assertEquals(
135 "Wiki est l'\xc3\xa9cole superieur !",
136 Revision::getRevisionText( $row ) );
137 }
138
139 /**
140 * @covers Revision::compressRevisionText
141 */
142 public function testCompressRevisionTextUtf8() {
143 $row = new stdClass;
144 $row->old_text = "Wiki est l'\xc3\xa9cole superieur !";
145 $row->old_flags = Revision::compressRevisionText( $row->old_text );
146 $this->assertTrue( false !== strpos( $row->old_flags, 'utf-8' ),
147 "Flags should contain 'utf-8'" );
148 $this->assertFalse( false !== strpos( $row->old_flags, 'gzip' ),
149 "Flags should not contain 'gzip'" );
150 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
151 $row->old_text, "Direct check" );
152 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
153 Revision::getRevisionText( $row ), "getRevisionText" );
154 }
155
156 /**
157 * @covers Revision::compressRevisionText
158 */
159 public function testCompressRevisionTextUtf8Gzip() {
160 $this->checkPHPExtension( 'zlib' );
161 $this->setMwGlobals( 'wgCompressRevisions', true );
162
163 $row = new stdClass;
164 $row->old_text = "Wiki est l'\xc3\xa9cole superieur !";
165 $row->old_flags = Revision::compressRevisionText( $row->old_text );
166 $this->assertTrue( false !== strpos( $row->old_flags, 'utf-8' ),
167 "Flags should contain 'utf-8'" );
168 $this->assertTrue( false !== strpos( $row->old_flags, 'gzip' ),
169 "Flags should contain 'gzip'" );
170 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
171 gzinflate( $row->old_text ), "Direct check" );
172 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
173 Revision::getRevisionText( $row ), "getRevisionText" );
174 }
175
176 # =========================================================================
177
178 /**
179 * @param string $text
180 * @param string $title
181 * @param string $model
182 * @param string $format
183 *
184 * @return Revision
185 */
186 function newTestRevision( $text, $title = "Test",
187 $model = CONTENT_MODEL_WIKITEXT, $format = null
188 ) {
189 if ( is_string( $title ) ) {
190 $title = Title::newFromText( $title );
191 }
192
193 $content = ContentHandler::makeContent( $text, $title, $model, $format );
194
195 $rev = new Revision(
196 array(
197 'id' => 42,
198 'page' => 23,
199 'title' => $title,
200
201 'content' => $content,
202 'length' => $content->getSize(),
203 'comment' => "testing",
204 'minor_edit' => false,
205
206 'content_format' => $format,
207 )
208 );
209
210 return $rev;
211 }
212
213 function dataGetContentModel() {
214 //NOTE: we expect the help namespace to always contain wikitext
215 return array(
216 array( 'hello world', 'Help:Hello', null, null, CONTENT_MODEL_WIKITEXT ),
217 array( 'hello world', 'User:hello/there.css', null, null, CONTENT_MODEL_CSS ),
218 array( serialize( 'hello world' ), 'Dummy:Hello', null, null, "testing" ),
219 );
220 }
221
222 /**
223 * @group Database
224 * @dataProvider dataGetContentModel
225 * @covers Revision::getContentModel
226 */
227 public function testGetContentModel( $text, $title, $model, $format, $expectedModel ) {
228 $rev = $this->newTestRevision( $text, $title, $model, $format );
229
230 $this->assertEquals( $expectedModel, $rev->getContentModel() );
231 }
232
233 function dataGetContentFormat() {
234 //NOTE: we expect the help namespace to always contain wikitext
235 return array(
236 array( 'hello world', 'Help:Hello', null, null, CONTENT_FORMAT_WIKITEXT ),
237 array( 'hello world', 'Help:Hello', CONTENT_MODEL_CSS, null, CONTENT_FORMAT_CSS ),
238 array( 'hello world', 'User:hello/there.css', null, null, CONTENT_FORMAT_CSS ),
239 array( serialize( 'hello world' ), 'Dummy:Hello', null, null, "testing" ),
240 );
241 }
242
243 /**
244 * @group Database
245 * @dataProvider dataGetContentFormat
246 * @covers Revision::getContentFormat
247 */
248 public function testGetContentFormat( $text, $title, $model, $format, $expectedFormat ) {
249 $rev = $this->newTestRevision( $text, $title, $model, $format );
250
251 $this->assertEquals( $expectedFormat, $rev->getContentFormat() );
252 }
253
254 function dataGetContentHandler() {
255 //NOTE: we expect the help namespace to always contain wikitext
256 return array(
257 array( 'hello world', 'Help:Hello', null, null, 'WikitextContentHandler' ),
258 array( 'hello world', 'User:hello/there.css', null, null, 'CssContentHandler' ),
259 array( serialize( 'hello world' ), 'Dummy:Hello', null, null, 'DummyContentHandlerForTesting' ),
260 );
261 }
262
263 /**
264 * @group Database
265 * @dataProvider dataGetContentHandler
266 * @covers Revision::getContentHandler
267 */
268 public function testGetContentHandler( $text, $title, $model, $format, $expectedClass ) {
269 $rev = $this->newTestRevision( $text, $title, $model, $format );
270
271 $this->assertEquals( $expectedClass, get_class( $rev->getContentHandler() ) );
272 }
273
274 function dataGetContent() {
275 //NOTE: we expect the help namespace to always contain wikitext
276 return array(
277 array( 'hello world', 'Help:Hello', null, null, Revision::FOR_PUBLIC, 'hello world' ),
278 array(
279 serialize( 'hello world' ),
280 'Hello',
281 "testing",
282 null,
283 Revision::FOR_PUBLIC,
284 serialize( 'hello world' )
285 ),
286 array(
287 serialize( 'hello world' ),
288 'Dummy:Hello',
289 null,
290 null,
291 Revision::FOR_PUBLIC,
292 serialize( 'hello world' )
293 ),
294 );
295 }
296
297 /**
298 * @group Database
299 * @dataProvider dataGetContent
300 * @covers Revision::getContent
301 */
302 public function testGetContent( $text, $title, $model, $format,
303 $audience, $expectedSerialization
304 ) {
305 $rev = $this->newTestRevision( $text, $title, $model, $format );
306 $content = $rev->getContent( $audience );
307
308 $this->assertEquals(
309 $expectedSerialization,
310 is_null( $content ) ? null : $content->serialize( $format )
311 );
312 }
313
314 function dataGetText() {
315 //NOTE: we expect the help namespace to always contain wikitext
316 return array(
317 array( 'hello world', 'Help:Hello', null, null, Revision::FOR_PUBLIC, 'hello world' ),
318 array( serialize( 'hello world' ), 'Hello', "testing", null, Revision::FOR_PUBLIC, null ),
319 array( serialize( 'hello world' ), 'Dummy:Hello', null, null, Revision::FOR_PUBLIC, null ),
320 );
321 }
322
323 /**
324 * @group Database
325 * @dataProvider dataGetText
326 * @covers Revision::getText
327 */
328 public function testGetText( $text, $title, $model, $format, $audience, $expectedText ) {
329 $this->hideDeprecated( 'Revision::getText' );
330
331 $rev = $this->newTestRevision( $text, $title, $model, $format );
332
333 $this->assertEquals( $expectedText, $rev->getText( $audience ) );
334 }
335
336 /**
337 * @group Database
338 * @dataProvider dataGetText
339 * @covers Revision::getRawText
340 */
341 public function testGetRawText( $text, $title, $model, $format, $audience, $expectedText ) {
342 $this->hideDeprecated( 'Revision::getRawText' );
343
344 $rev = $this->newTestRevision( $text, $title, $model, $format );
345
346 $this->assertEquals( $expectedText, $rev->getRawText( $audience ) );
347 }
348
349 public function dataGetSize() {
350 return array(
351 array( "hello world.", CONTENT_MODEL_WIKITEXT, 12 ),
352 array( serialize( "hello world." ), "testing", 12 ),
353 );
354 }
355
356 /**
357 * @covers Revision::getSize
358 * @group Database
359 * @dataProvider dataGetSize
360 */
361 public function testGetSize( $text, $model, $expected_size ) {
362 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSize', $model );
363 $this->assertEquals( $expected_size, $rev->getSize() );
364 }
365
366 public function dataGetSha1() {
367 return array(
368 array( "hello world.", CONTENT_MODEL_WIKITEXT, Revision::base36Sha1( "hello world." ) ),
369 array(
370 serialize( "hello world." ),
371 "testing",
372 Revision::base36Sha1( serialize( "hello world." ) )
373 ),
374 );
375 }
376
377 /**
378 * @covers Revision::getSha1
379 * @group Database
380 * @dataProvider dataGetSha1
381 */
382 public function testGetSha1( $text, $model, $expected_hash ) {
383 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSha1', $model );
384 $this->assertEquals( $expected_hash, $rev->getSha1() );
385 }
386
387 /**
388 * @covers Revision::__construct
389 */
390 public function testConstructWithText() {
391 $this->hideDeprecated( "Revision::getText" );
392
393 $rev = new Revision( array(
394 'text' => 'hello world.',
395 'content_model' => CONTENT_MODEL_JAVASCRIPT
396 ) );
397
398 $this->assertNotNull( $rev->getText(), 'no content text' );
399 $this->assertNotNull( $rev->getContent(), 'no content object available' );
400 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContent()->getModel() );
401 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContentModel() );
402 }
403
404 /**
405 * @covers Revision::__construct
406 */
407 public function testConstructWithContent() {
408 $this->hideDeprecated( "Revision::getText" );
409
410 $title = Title::newFromText( 'RevisionTest_testConstructWithContent' );
411
412 $rev = new Revision( array(
413 'content' => ContentHandler::makeContent( 'hello world.', $title, CONTENT_MODEL_JAVASCRIPT ),
414 ) );
415
416 $this->assertNotNull( $rev->getText(), 'no content text' );
417 $this->assertNotNull( $rev->getContent(), 'no content object available' );
418 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContent()->getModel() );
419 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContentModel() );
420 }
421
422 /**
423 * Tests whether $rev->getContent() returns a clone when needed.
424 *
425 * @group Database
426 * @covers Revision::getContent
427 */
428 public function testGetContentClone() {
429 $content = new RevisionTestModifyableContent( "foo" );
430
431 $rev = new Revision(
432 array(
433 'id' => 42,
434 'page' => 23,
435 'title' => Title::newFromText( "testGetContentClone_dummy" ),
436
437 'content' => $content,
438 'length' => $content->getSize(),
439 'comment' => "testing",
440 'minor_edit' => false,
441 )
442 );
443
444 $content = $rev->getContent( Revision::RAW );
445 $content->setText( "bar" );
446
447 $content2 = $rev->getContent( Revision::RAW );
448 // content is mutable, expect clone
449 $this->assertNotSame( $content, $content2, "expected a clone" );
450 // clone should contain the original text
451 $this->assertEquals( "foo", $content2->getText() );
452
453 $content2->setText( "bla bla" );
454 $this->assertEquals( "bar", $content->getText() ); // clones should be independent
455 }
456
457 /**
458 * Tests whether $rev->getContent() returns the same object repeatedly if appropriate.
459 *
460 * @group Database
461 * @covers Revision::getContent
462 */
463 public function testGetContentUncloned() {
464 $rev = $this->newTestRevision( "hello", "testGetContentUncloned_dummy", CONTENT_MODEL_WIKITEXT );
465 $content = $rev->getContent( Revision::RAW );
466 $content2 = $rev->getContent( Revision::RAW );
467
468 // for immutable content like wikitext, this should be the same object
469 $this->assertSame( $content, $content2 );
470 }
471 }
472
473 class RevisionTestModifyableContent extends TextContent {
474 public function __construct( $text ) {
475 parent::__construct( $text, "RevisionTestModifyableContent" );
476 }
477
478 public function copy() {
479 return new RevisionTestModifyableContent( $this->mText );
480 }
481
482 public function getText() {
483 return $this->mText;
484 }
485
486 public function setText( $text ) {
487 $this->mText = $text;
488 }
489 }
490
491 class RevisionTestModifyableContentHandler extends TextContentHandler {
492
493 public function __construct() {
494 parent::__construct( "RevisionTestModifyableContent", array( CONTENT_FORMAT_TEXT ) );
495 }
496
497 public function unserializeContent( $text, $format = null ) {
498 $this->checkFormat( $format );
499
500 return new RevisionTestModifyableContent( $text );
501 }
502
503 public function makeEmptyContent() {
504 return new RevisionTestModifyableContent( '' );
505 }
506 }