Merge "(bug 43560) Initial input focus on Special:ListUsers isn't set"
[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 function testGetRevisionText() {
58 $row = new stdClass;
59 $row->old_flags = '';
60 $row->old_text = 'This is a bunch of revision text.';
61 $this->assertEquals(
62 'This is a bunch of revision text.',
63 Revision::getRevisionText( $row ) );
64 }
65
66 function testGetRevisionTextGzip() {
67 $this->checkPHPExtension( 'zlib' );
68
69 $row = new stdClass;
70 $row->old_flags = 'gzip';
71 $row->old_text = gzdeflate( 'This is a bunch of revision text.' );
72 $this->assertEquals(
73 'This is a bunch of revision text.',
74 Revision::getRevisionText( $row ) );
75 }
76
77 function testGetRevisionTextUtf8Native() {
78 $row = new stdClass;
79 $row->old_flags = 'utf-8';
80 $row->old_text = "Wiki est l'\xc3\xa9cole superieur !";
81 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
82 $this->assertEquals(
83 "Wiki est l'\xc3\xa9cole superieur !",
84 Revision::getRevisionText( $row ) );
85 }
86
87 function testGetRevisionTextUtf8Legacy() {
88 $row = new stdClass;
89 $row->old_flags = '';
90 $row->old_text = "Wiki est l'\xe9cole superieur !";
91 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
92 $this->assertEquals(
93 "Wiki est l'\xc3\xa9cole superieur !",
94 Revision::getRevisionText( $row ) );
95 }
96
97 function testGetRevisionTextUtf8NativeGzip() {
98 $this->checkPHPExtension( 'zlib' );
99
100 $row = new stdClass;
101 $row->old_flags = 'gzip,utf-8';
102 $row->old_text = gzdeflate( "Wiki est l'\xc3\xa9cole superieur !" );
103 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
104 $this->assertEquals(
105 "Wiki est l'\xc3\xa9cole superieur !",
106 Revision::getRevisionText( $row ) );
107 }
108
109 function testGetRevisionTextUtf8LegacyGzip() {
110 $this->checkPHPExtension( 'zlib' );
111
112 $row = new stdClass;
113 $row->old_flags = 'gzip';
114 $row->old_text = gzdeflate( "Wiki est l'\xe9cole superieur !" );
115 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
116 $this->assertEquals(
117 "Wiki est l'\xc3\xa9cole superieur !",
118 Revision::getRevisionText( $row ) );
119 }
120
121 function testCompressRevisionTextUtf8() {
122 $row = new stdClass;
123 $row->old_text = "Wiki est l'\xc3\xa9cole superieur !";
124 $row->old_flags = Revision::compressRevisionText( $row->old_text );
125 $this->assertTrue( false !== strpos( $row->old_flags, 'utf-8' ),
126 "Flags should contain 'utf-8'" );
127 $this->assertFalse( false !== strpos( $row->old_flags, 'gzip' ),
128 "Flags should not contain 'gzip'" );
129 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
130 $row->old_text, "Direct check" );
131 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
132 Revision::getRevisionText( $row ), "getRevisionText" );
133 }
134
135 function testCompressRevisionTextUtf8Gzip() {
136 $this->checkPHPExtension( 'zlib' );
137
138 global $wgCompressRevisions;
139 $wgCompressRevisions = true;
140
141 $row = new stdClass;
142 $row->old_text = "Wiki est l'\xc3\xa9cole superieur !";
143 $row->old_flags = Revision::compressRevisionText( $row->old_text );
144 $this->assertTrue( false !== strpos( $row->old_flags, 'utf-8' ),
145 "Flags should contain 'utf-8'" );
146 $this->assertTrue( false !== strpos( $row->old_flags, 'gzip' ),
147 "Flags should contain 'gzip'" );
148 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
149 gzinflate( $row->old_text ), "Direct check" );
150 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
151 Revision::getRevisionText( $row ), "getRevisionText" );
152 }
153
154 # =================================================================================================================
155
156 /**
157 * @param string $text
158 * @param string $title
159 * @param string $model
160 * @return Revision
161 */
162 function newTestRevision( $text, $title = "Test", $model = CONTENT_MODEL_WIKITEXT, $format = null ) {
163 if ( is_string( $title ) ) {
164 $title = Title::newFromText( $title );
165 }
166
167 $content = ContentHandler::makeContent( $text, $title, $model, $format );
168
169 $rev = new Revision(
170 array(
171 'id' => 42,
172 'page' => 23,
173 'title' => $title,
174
175 'content' => $content,
176 'length' => $content->getSize(),
177 'comment' => "testing",
178 'minor_edit' => false,
179
180 'content_format' => $format,
181 )
182 );
183
184 return $rev;
185 }
186
187 function dataGetContentModel() {
188 //NOTE: we expect the help namespace to always contain wikitext
189 return array(
190 array( 'hello world', 'Help:Hello', null, null, CONTENT_MODEL_WIKITEXT ),
191 array( 'hello world', 'User:hello/there.css', null, null, CONTENT_MODEL_CSS ),
192 array( serialize('hello world'), 'Dummy:Hello', null, null, "testing" ),
193 );
194 }
195
196 /**
197 * @group Database
198 * @dataProvider dataGetContentModel
199 */
200 function testGetContentModel( $text, $title, $model, $format, $expectedModel ) {
201 $rev = $this->newTestRevision( $text, $title, $model, $format );
202
203 $this->assertEquals( $expectedModel, $rev->getContentModel() );
204 }
205
206 function dataGetContentFormat() {
207 //NOTE: we expect the help namespace to always contain wikitext
208 return array(
209 array( 'hello world', 'Help:Hello', null, null, CONTENT_FORMAT_WIKITEXT ),
210 array( 'hello world', 'Help:Hello', CONTENT_MODEL_CSS, null, CONTENT_FORMAT_CSS ),
211 array( 'hello world', 'User:hello/there.css', null, null, CONTENT_FORMAT_CSS ),
212 array( serialize('hello world'), 'Dummy:Hello', null, null, "testing" ),
213 );
214 }
215
216 /**
217 * @group Database
218 * @dataProvider dataGetContentFormat
219 */
220 function testGetContentFormat( $text, $title, $model, $format, $expectedFormat ) {
221 $rev = $this->newTestRevision( $text, $title, $model, $format );
222
223 $this->assertEquals( $expectedFormat, $rev->getContentFormat() );
224 }
225
226 function dataGetContentHandler() {
227 //NOTE: we expect the help namespace to always contain wikitext
228 return array(
229 array( 'hello world', 'Help:Hello', null, null, 'WikitextContentHandler' ),
230 array( 'hello world', 'User:hello/there.css', null, null, 'CssContentHandler' ),
231 array( serialize('hello world'), 'Dummy:Hello', null, null, 'DummyContentHandlerForTesting' ),
232 );
233 }
234
235 /**
236 * @group Database
237 * @dataProvider dataGetContentHandler
238 */
239 function testGetContentHandler( $text, $title, $model, $format, $expectedClass ) {
240 $rev = $this->newTestRevision( $text, $title, $model, $format );
241
242 $this->assertEquals( $expectedClass, get_class( $rev->getContentHandler() ) );
243 }
244
245 function dataGetContent() {
246 //NOTE: we expect the help namespace to always contain wikitext
247 return array(
248 array( 'hello world', 'Help:Hello', null, null, Revision::FOR_PUBLIC, 'hello world' ),
249 array( serialize('hello world'), 'Hello', "testing", null, Revision::FOR_PUBLIC, serialize('hello world') ),
250 array( serialize('hello world'), 'Dummy:Hello', null, null, Revision::FOR_PUBLIC, serialize('hello world') ),
251 );
252 }
253
254 /**
255 * @group Database
256 * @dataProvider dataGetContent
257 */
258 function testGetContent( $text, $title, $model, $format, $audience, $expectedSerialization ) {
259 $rev = $this->newTestRevision( $text, $title, $model, $format );
260 $content = $rev->getContent( $audience );
261
262 $this->assertEquals( $expectedSerialization, is_null( $content ) ? null : $content->serialize( $format ) );
263 }
264
265 function dataGetText() {
266 //NOTE: we expect the help namespace to always contain wikitext
267 return array(
268 array( 'hello world', 'Help:Hello', null, null, Revision::FOR_PUBLIC, 'hello world' ),
269 array( serialize('hello world'), 'Hello', "testing", null, Revision::FOR_PUBLIC, null ),
270 array( serialize('hello world'), 'Dummy:Hello', null, null, Revision::FOR_PUBLIC, null ),
271 );
272 }
273
274 /**
275 * @group Database
276 * @dataProvider dataGetText
277 */
278 function testGetText( $text, $title, $model, $format, $audience, $expectedText ) {
279 $this->hideDeprecated( 'Revision::getText' );
280
281 $rev = $this->newTestRevision( $text, $title, $model, $format );
282
283 $this->assertEquals( $expectedText, $rev->getText( $audience ) );
284 }
285
286 /**
287 * @group Database
288 * @dataProvider dataGetText
289 */
290 function testGetRawText( $text, $title, $model, $format, $audience, $expectedText ) {
291 $this->hideDeprecated( 'Revision::getRawText' );
292
293 $rev = $this->newTestRevision( $text, $title, $model, $format );
294
295 $this->assertEquals( $expectedText, $rev->getRawText( $audience ) );
296 }
297
298
299 public function dataGetSize( ) {
300 return array(
301 array( "hello world.", CONTENT_MODEL_WIKITEXT, 12 ),
302 array( serialize( "hello world." ), "testing", 12 ),
303 );
304 }
305
306 /**
307 * @covers Revision::getSize
308 * @group Database
309 * @dataProvider dataGetSize
310 */
311 public function testGetSize( $text, $model, $expected_size ) {
312 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSize', $model );
313 $this->assertEquals( $expected_size, $rev->getSize() );
314 }
315
316 public function dataGetSha1( ) {
317 return array(
318 array( "hello world.", CONTENT_MODEL_WIKITEXT, Revision::base36Sha1( "hello world." ) ),
319 array( serialize( "hello world." ), "testing", Revision::base36Sha1( serialize( "hello world." ) ) ),
320 );
321 }
322
323 /**
324 * @covers Revision::getSha1
325 * @group Database
326 * @dataProvider dataGetSha1
327 */
328 public function testGetSha1( $text, $model, $expected_hash ) {
329 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSha1', $model );
330 $this->assertEquals( $expected_hash, $rev->getSha1() );
331 }
332
333 public function testConstructWithText() {
334 $this->hideDeprecated( "Revision::getText" );
335
336 $rev = new Revision( array(
337 'text' => 'hello world.',
338 'content_model' => CONTENT_MODEL_JAVASCRIPT
339 ));
340
341 $this->assertNotNull( $rev->getText(), 'no content text' );
342 $this->assertNotNull( $rev->getContent(), 'no content object available' );
343 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContent()->getModel() );
344 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContentModel() );
345 }
346
347 public function testConstructWithContent() {
348 $this->hideDeprecated( "Revision::getText" );
349
350 $title = Title::newFromText( 'RevisionTest_testConstructWithContent' );
351
352 $rev = new Revision( array(
353 'content' => ContentHandler::makeContent( 'hello world.', $title, CONTENT_MODEL_JAVASCRIPT ),
354 ));
355
356 $this->assertNotNull( $rev->getText(), 'no content text' );
357 $this->assertNotNull( $rev->getContent(), 'no content object available' );
358 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContent()->getModel() );
359 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContentModel() );
360 }
361
362 /**
363 * Tests whether $rev->getContent() returns a clone when needed.
364 *
365 * @group Database
366 */
367 function testGetContentClone( ) {
368 $content = new RevisionTestModifyableContent( "foo" );
369
370 $rev = new Revision(
371 array(
372 'id' => 42,
373 'page' => 23,
374 'title' => Title::newFromText( "testGetContentClone_dummy" ),
375
376 'content' => $content,
377 'length' => $content->getSize(),
378 'comment' => "testing",
379 'minor_edit' => false,
380 )
381 );
382
383 $content = $rev->getContent( Revision::RAW );
384 $content->setText( "bar" );
385
386 $content2 = $rev->getContent( Revision::RAW );
387 $this->assertNotSame( $content, $content2, "expected a clone" ); // content is mutable, expect clone
388 $this->assertEquals( "foo", $content2->getText() ); // clone should contain the original text
389
390 $content2->setText( "bla bla" );
391 $this->assertEquals( "bar", $content->getText() ); // clones should be independent
392 }
393
394
395 /**
396 * Tests whether $rev->getContent() returns the same object repeatedly if appropriate.
397 *
398 * @group Database
399 */
400 function testGetContentUncloned() {
401 $rev = $this->newTestRevision( "hello", "testGetContentUncloned_dummy", CONTENT_MODEL_WIKITEXT );
402 $content = $rev->getContent( Revision::RAW );
403 $content2 = $rev->getContent( Revision::RAW );
404
405 // for immutable content like wikitext, this should be the same object
406 $this->assertSame( $content, $content2 );
407 }
408
409 }
410
411 class RevisionTestModifyableContent extends TextContent {
412 public function __construct( $text ) {
413 parent::__construct( $text, "RevisionTestModifyableContent" );
414 }
415
416 public function copy( ) {
417 return new RevisionTestModifyableContent( $this->mText );
418 }
419
420 public function getText() {
421 return $this->mText;
422 }
423
424 public function setText( $text ) {
425 $this->mText = $text;
426 }
427
428 }
429
430 class RevisionTestModifyableContentHandler extends TextContentHandler {
431
432 public function __construct( ) {
433 parent::__construct( "RevisionTestModifyableContent", array( CONTENT_FORMAT_TEXT ) );
434 }
435
436 public function unserializeContent( $text, $format = null ) {
437 $this->checkFormat( $format );
438
439 return new RevisionTestModifyableContent( $text );
440 }
441
442 public function makeEmptyContent() {
443 return new RevisionTestModifyableContent( '' );
444 }
445 }