Merge "mw.hook: Use hasOwnProperty"
[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", $model = CONTENT_MODEL_WIKITEXT, $format = null ) {
187 if ( is_string( $title ) ) {
188 $title = Title::newFromText( $title );
189 }
190
191 $content = ContentHandler::makeContent( $text, $title, $model, $format );
192
193 $rev = new Revision(
194 array(
195 'id' => 42,
196 'page' => 23,
197 'title' => $title,
198
199 'content' => $content,
200 'length' => $content->getSize(),
201 'comment' => "testing",
202 'minor_edit' => false,
203
204 'content_format' => $format,
205 )
206 );
207
208 return $rev;
209 }
210
211 function dataGetContentModel() {
212 //NOTE: we expect the help namespace to always contain wikitext
213 return array(
214 array( 'hello world', 'Help:Hello', null, null, CONTENT_MODEL_WIKITEXT ),
215 array( 'hello world', 'User:hello/there.css', null, null, CONTENT_MODEL_CSS ),
216 array( serialize( 'hello world' ), 'Dummy:Hello', null, null, "testing" ),
217 );
218 }
219
220 /**
221 * @group Database
222 * @dataProvider dataGetContentModel
223 * @covers Revision::getContentModel
224 */
225 public function testGetContentModel( $text, $title, $model, $format, $expectedModel ) {
226 $rev = $this->newTestRevision( $text, $title, $model, $format );
227
228 $this->assertEquals( $expectedModel, $rev->getContentModel() );
229 }
230
231 function dataGetContentFormat() {
232 //NOTE: we expect the help namespace to always contain wikitext
233 return array(
234 array( 'hello world', 'Help:Hello', null, null, CONTENT_FORMAT_WIKITEXT ),
235 array( 'hello world', 'Help:Hello', CONTENT_MODEL_CSS, null, CONTENT_FORMAT_CSS ),
236 array( 'hello world', 'User:hello/there.css', null, null, CONTENT_FORMAT_CSS ),
237 array( serialize( 'hello world' ), 'Dummy:Hello', null, null, "testing" ),
238 );
239 }
240
241 /**
242 * @group Database
243 * @dataProvider dataGetContentFormat
244 * @covers Revision::getContentFormat
245 */
246 public function testGetContentFormat( $text, $title, $model, $format, $expectedFormat ) {
247 $rev = $this->newTestRevision( $text, $title, $model, $format );
248
249 $this->assertEquals( $expectedFormat, $rev->getContentFormat() );
250 }
251
252 function dataGetContentHandler() {
253 //NOTE: we expect the help namespace to always contain wikitext
254 return array(
255 array( 'hello world', 'Help:Hello', null, null, 'WikitextContentHandler' ),
256 array( 'hello world', 'User:hello/there.css', null, null, 'CssContentHandler' ),
257 array( serialize( 'hello world' ), 'Dummy:Hello', null, null, 'DummyContentHandlerForTesting' ),
258 );
259 }
260
261 /**
262 * @group Database
263 * @dataProvider dataGetContentHandler
264 * @covers Revision::getContentHandler
265 */
266 public function testGetContentHandler( $text, $title, $model, $format, $expectedClass ) {
267 $rev = $this->newTestRevision( $text, $title, $model, $format );
268
269 $this->assertEquals( $expectedClass, get_class( $rev->getContentHandler() ) );
270 }
271
272 function dataGetContent() {
273 //NOTE: we expect the help namespace to always contain wikitext
274 return array(
275 array( 'hello world', 'Help:Hello', null, null, Revision::FOR_PUBLIC, 'hello world' ),
276 array( serialize( 'hello world' ), 'Hello', "testing", null, Revision::FOR_PUBLIC, serialize( 'hello world' ) ),
277 array( serialize( 'hello world' ), 'Dummy:Hello', null, null, Revision::FOR_PUBLIC, serialize( 'hello world' ) ),
278 );
279 }
280
281 /**
282 * @group Database
283 * @dataProvider dataGetContent
284 * @covers Revision::getContent
285 */
286 public function testGetContent( $text, $title, $model, $format, $audience, $expectedSerialization ) {
287 $rev = $this->newTestRevision( $text, $title, $model, $format );
288 $content = $rev->getContent( $audience );
289
290 $this->assertEquals( $expectedSerialization, is_null( $content ) ? null : $content->serialize( $format ) );
291 }
292
293 function dataGetText() {
294 //NOTE: we expect the help namespace to always contain wikitext
295 return array(
296 array( 'hello world', 'Help:Hello', null, null, Revision::FOR_PUBLIC, 'hello world' ),
297 array( serialize( 'hello world' ), 'Hello', "testing", null, Revision::FOR_PUBLIC, null ),
298 array( serialize( 'hello world' ), 'Dummy:Hello', null, null, Revision::FOR_PUBLIC, null ),
299 );
300 }
301
302 /**
303 * @group Database
304 * @dataProvider dataGetText
305 * @covers Revision::getText
306 */
307 public function testGetText( $text, $title, $model, $format, $audience, $expectedText ) {
308 $this->hideDeprecated( 'Revision::getText' );
309
310 $rev = $this->newTestRevision( $text, $title, $model, $format );
311
312 $this->assertEquals( $expectedText, $rev->getText( $audience ) );
313 }
314
315 /**
316 * @group Database
317 * @dataProvider dataGetText
318 * @covers Revision::getRawText
319 */
320 public function testGetRawText( $text, $title, $model, $format, $audience, $expectedText ) {
321 $this->hideDeprecated( 'Revision::getRawText' );
322
323 $rev = $this->newTestRevision( $text, $title, $model, $format );
324
325 $this->assertEquals( $expectedText, $rev->getRawText( $audience ) );
326 }
327
328 public function dataGetSize() {
329 return array(
330 array( "hello world.", CONTENT_MODEL_WIKITEXT, 12 ),
331 array( serialize( "hello world." ), "testing", 12 ),
332 );
333 }
334
335 /**
336 * @covers Revision::getSize
337 * @group Database
338 * @dataProvider dataGetSize
339 */
340 public function testGetSize( $text, $model, $expected_size ) {
341 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSize', $model );
342 $this->assertEquals( $expected_size, $rev->getSize() );
343 }
344
345 public function dataGetSha1() {
346 return array(
347 array( "hello world.", CONTENT_MODEL_WIKITEXT, Revision::base36Sha1( "hello world." ) ),
348 array( serialize( "hello world." ), "testing", Revision::base36Sha1( serialize( "hello world." ) ) ),
349 );
350 }
351
352 /**
353 * @covers Revision::getSha1
354 * @group Database
355 * @dataProvider dataGetSha1
356 */
357 public function testGetSha1( $text, $model, $expected_hash ) {
358 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSha1', $model );
359 $this->assertEquals( $expected_hash, $rev->getSha1() );
360 }
361
362 /**
363 * @covers Revision::__construct
364 */
365 public function testConstructWithText() {
366 $this->hideDeprecated( "Revision::getText" );
367
368 $rev = new Revision( array(
369 'text' => 'hello world.',
370 'content_model' => CONTENT_MODEL_JAVASCRIPT
371 ) );
372
373 $this->assertNotNull( $rev->getText(), 'no content text' );
374 $this->assertNotNull( $rev->getContent(), 'no content object available' );
375 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContent()->getModel() );
376 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContentModel() );
377 }
378
379 /**
380 * @covers Revision::__construct
381 */
382 public function testConstructWithContent() {
383 $this->hideDeprecated( "Revision::getText" );
384
385 $title = Title::newFromText( 'RevisionTest_testConstructWithContent' );
386
387 $rev = new Revision( array(
388 'content' => ContentHandler::makeContent( 'hello world.', $title, CONTENT_MODEL_JAVASCRIPT ),
389 ) );
390
391 $this->assertNotNull( $rev->getText(), 'no content text' );
392 $this->assertNotNull( $rev->getContent(), 'no content object available' );
393 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContent()->getModel() );
394 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContentModel() );
395 }
396
397 /**
398 * Tests whether $rev->getContent() returns a clone when needed.
399 *
400 * @group Database
401 * @covers Revision::getContent
402 */
403 public function testGetContentClone() {
404 $content = new RevisionTestModifyableContent( "foo" );
405
406 $rev = new Revision(
407 array(
408 'id' => 42,
409 'page' => 23,
410 'title' => Title::newFromText( "testGetContentClone_dummy" ),
411
412 'content' => $content,
413 'length' => $content->getSize(),
414 'comment' => "testing",
415 'minor_edit' => false,
416 )
417 );
418
419 $content = $rev->getContent( Revision::RAW );
420 $content->setText( "bar" );
421
422 $content2 = $rev->getContent( Revision::RAW );
423 $this->assertNotSame( $content, $content2, "expected a clone" ); // content is mutable, expect clone
424 $this->assertEquals( "foo", $content2->getText() ); // clone should contain the original text
425
426 $content2->setText( "bla bla" );
427 $this->assertEquals( "bar", $content->getText() ); // clones should be independent
428 }
429
430 /**
431 * Tests whether $rev->getContent() returns the same object repeatedly if appropriate.
432 *
433 * @group Database
434 * @covers Revision::getContent
435 */
436 public function testGetContentUncloned() {
437 $rev = $this->newTestRevision( "hello", "testGetContentUncloned_dummy", CONTENT_MODEL_WIKITEXT );
438 $content = $rev->getContent( Revision::RAW );
439 $content2 = $rev->getContent( Revision::RAW );
440
441 // for immutable content like wikitext, this should be the same object
442 $this->assertSame( $content, $content2 );
443 }
444 }
445
446 class RevisionTestModifyableContent extends TextContent {
447 public function __construct( $text ) {
448 parent::__construct( $text, "RevisionTestModifyableContent" );
449 }
450
451 public function copy() {
452 return new RevisionTestModifyableContent( $this->mText );
453 }
454
455 public function getText() {
456 return $this->mText;
457 }
458
459 public function setText( $text ) {
460 $this->mText = $text;
461 }
462 }
463
464 class RevisionTestModifyableContentHandler extends TextContentHandler {
465
466 public function __construct() {
467 parent::__construct( "RevisionTestModifyableContent", array( CONTENT_FORMAT_TEXT ) );
468 }
469
470 public function unserializeContent( $text, $format = null ) {
471 $this->checkFormat( $format );
472
473 return new RevisionTestModifyableContent( $text );
474 }
475
476 public function makeEmptyContent() {
477 return new RevisionTestModifyableContent( '' );
478 }
479 }