ba19f3b2ae71e0c17ad7bba65bafc5e8e03f3ec8
4 * @group ContentHandler
6 class RevisionTest
extends MediaWikiTestCase
{
8 protected function setUp() {
13 $this->setMwGlobals( [
14 'wgContLang' => Language
::factory( 'en' ),
15 'wgLanguageCode' => 'en',
16 'wgLegacyEncoding' => false,
17 'wgCompressRevisions' => false,
19 'wgContentHandlerTextFallback' => 'ignore',
22 $this->mergeMwGlobalArrayValue(
26 12313 => 'Dummy_talk',
30 $this->mergeMwGlobalArrayValue(
31 'wgNamespaceContentModels',
37 $this->mergeMwGlobalArrayValue(
40 'testing' => 'DummyContentHandlerForTesting',
41 'RevisionTestModifyableContent' => 'RevisionTestModifyableContentHandler',
45 MWNamespace
::clearCaches();
46 // Reset namespace cache
47 $wgContLang->resetNamespaces();
50 protected function tearDown() {
53 MWNamespace
::clearCaches();
54 // Reset namespace cache
55 $wgContLang->resetNamespaces();
61 * @covers Revision::getRevisionText
63 public function testGetRevisionText() {
66 $row->old_text
= 'This is a bunch of revision text.';
68 'This is a bunch of revision text.',
69 Revision
::getRevisionText( $row ) );
73 * @covers Revision::getRevisionText
75 public function testGetRevisionTextGzip() {
76 $this->checkPHPExtension( 'zlib' );
79 $row->old_flags
= 'gzip';
80 $row->old_text
= gzdeflate( 'This is a bunch of revision text.' );
82 'This is a bunch of revision text.',
83 Revision
::getRevisionText( $row ) );
87 * @covers Revision::getRevisionText
89 public function testGetRevisionTextUtf8Native() {
91 $row->old_flags
= 'utf-8';
92 $row->old_text
= "Wiki est l'\xc3\xa9cole superieur !";
93 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
95 "Wiki est l'\xc3\xa9cole superieur !",
96 Revision
::getRevisionText( $row ) );
100 * @covers Revision::getRevisionText
102 public function testGetRevisionTextUtf8Legacy() {
104 $row->old_flags
= '';
105 $row->old_text
= "Wiki est l'\xe9cole superieur !";
106 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
108 "Wiki est l'\xc3\xa9cole superieur !",
109 Revision
::getRevisionText( $row ) );
113 * @covers Revision::getRevisionText
115 public function testGetRevisionTextUtf8NativeGzip() {
116 $this->checkPHPExtension( 'zlib' );
119 $row->old_flags
= 'gzip,utf-8';
120 $row->old_text
= gzdeflate( "Wiki est l'\xc3\xa9cole superieur !" );
121 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
123 "Wiki est l'\xc3\xa9cole superieur !",
124 Revision
::getRevisionText( $row ) );
128 * @covers Revision::getRevisionText
130 public function testGetRevisionTextUtf8LegacyGzip() {
131 $this->checkPHPExtension( 'zlib' );
134 $row->old_flags
= 'gzip';
135 $row->old_text
= gzdeflate( "Wiki est l'\xe9cole superieur !" );
136 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
138 "Wiki est l'\xc3\xa9cole superieur !",
139 Revision
::getRevisionText( $row ) );
143 * @covers Revision::compressRevisionText
145 public function testCompressRevisionTextUtf8() {
147 $row->old_text
= "Wiki est l'\xc3\xa9cole superieur !";
148 $row->old_flags
= Revision
::compressRevisionText( $row->old_text
);
149 $this->assertTrue( false !== strpos( $row->old_flags
, 'utf-8' ),
150 "Flags should contain 'utf-8'" );
151 $this->assertFalse( false !== strpos( $row->old_flags
, 'gzip' ),
152 "Flags should not contain 'gzip'" );
153 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
154 $row->old_text
, "Direct check" );
155 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
156 Revision
::getRevisionText( $row ), "getRevisionText" );
160 * @covers Revision::compressRevisionText
162 public function testCompressRevisionTextUtf8Gzip() {
163 $this->checkPHPExtension( 'zlib' );
164 $this->setMwGlobals( 'wgCompressRevisions', true );
167 $row->old_text
= "Wiki est l'\xc3\xa9cole superieur !";
168 $row->old_flags
= Revision
::compressRevisionText( $row->old_text
);
169 $this->assertTrue( false !== strpos( $row->old_flags
, 'utf-8' ),
170 "Flags should contain 'utf-8'" );
171 $this->assertTrue( false !== strpos( $row->old_flags
, 'gzip' ),
172 "Flags should contain 'gzip'" );
173 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
174 gzinflate( $row->old_text
), "Direct check" );
175 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
176 Revision
::getRevisionText( $row ), "getRevisionText" );
179 # =========================================================================
182 * @param string $text
183 * @param string $title
184 * @param string $model
185 * @param string $format
189 private function newTestRevision( $text, $title = "Test",
190 $model = CONTENT_MODEL_WIKITEXT
, $format = null
192 if ( is_string( $title ) ) {
193 $title = Title
::newFromText( $title );
196 $content = ContentHandler
::makeContent( $text, $title, $model, $format );
204 'content' => $content,
205 'length' => $content->getSize(),
206 'comment' => "testing",
207 'minor_edit' => false,
209 'content_format' => $format,
216 public function dataGetContentModel() {
217 // NOTE: we expect the help namespace to always contain wikitext
219 [ 'hello world', 'Help:Hello', null, null, CONTENT_MODEL_WIKITEXT
],
220 [ 'hello world', 'User:hello/there.css', null, null, CONTENT_MODEL_CSS
],
221 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, "testing" ],
227 * @dataProvider dataGetContentModel
228 * @covers Revision::getContentModel
230 public function testGetContentModel( $text, $title, $model, $format, $expectedModel ) {
231 $rev = $this->newTestRevision( $text, $title, $model, $format );
233 $this->assertEquals( $expectedModel, $rev->getContentModel() );
236 public function dataGetContentFormat() {
237 // NOTE: we expect the help namespace to always contain wikitext
239 [ 'hello world', 'Help:Hello', null, null, CONTENT_FORMAT_WIKITEXT
],
240 [ 'hello world', 'Help:Hello', CONTENT_MODEL_CSS
, null, CONTENT_FORMAT_CSS
],
241 [ 'hello world', 'User:hello/there.css', null, null, CONTENT_FORMAT_CSS
],
242 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, "testing" ],
248 * @dataProvider dataGetContentFormat
249 * @covers Revision::getContentFormat
251 public function testGetContentFormat( $text, $title, $model, $format, $expectedFormat ) {
252 $rev = $this->newTestRevision( $text, $title, $model, $format );
254 $this->assertEquals( $expectedFormat, $rev->getContentFormat() );
257 public function dataGetContentHandler() {
258 // NOTE: we expect the help namespace to always contain wikitext
260 [ 'hello world', 'Help:Hello', null, null, 'WikitextContentHandler' ],
261 [ 'hello world', 'User:hello/there.css', null, null, 'CssContentHandler' ],
262 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, 'DummyContentHandlerForTesting' ],
268 * @dataProvider dataGetContentHandler
269 * @covers Revision::getContentHandler
271 public function testGetContentHandler( $text, $title, $model, $format, $expectedClass ) {
272 $rev = $this->newTestRevision( $text, $title, $model, $format );
274 $this->assertEquals( $expectedClass, get_class( $rev->getContentHandler() ) );
277 public function dataGetContent() {
278 // NOTE: we expect the help namespace to always contain wikitext
280 [ 'hello world', 'Help:Hello', null, null, Revision
::FOR_PUBLIC
, 'hello world' ],
282 serialize( 'hello world' ),
286 Revision
::FOR_PUBLIC
,
287 serialize( 'hello world' )
290 serialize( 'hello world' ),
294 Revision
::FOR_PUBLIC
,
295 serialize( 'hello world' )
302 * @dataProvider dataGetContent
303 * @covers Revision::getContent
305 public function testGetContent( $text, $title, $model, $format,
306 $audience, $expectedSerialization
308 $rev = $this->newTestRevision( $text, $title, $model, $format );
309 $content = $rev->getContent( $audience );
312 $expectedSerialization,
313 is_null( $content ) ?
null : $content->serialize( $format )
317 public function dataGetSize() {
319 [ "hello world.", CONTENT_MODEL_WIKITEXT
, 12 ],
320 [ serialize( "hello world." ), "testing", 12 ],
325 * @covers Revision::getSize
327 * @dataProvider dataGetSize
329 public function testGetSize( $text, $model, $expected_size ) {
330 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSize', $model );
331 $this->assertEquals( $expected_size, $rev->getSize() );
334 public function dataGetSha1() {
336 [ "hello world.", CONTENT_MODEL_WIKITEXT
, Revision
::base36Sha1( "hello world." ) ],
338 serialize( "hello world." ),
340 Revision
::base36Sha1( serialize( "hello world." ) )
346 * @covers Revision::getSha1
348 * @dataProvider dataGetSha1
350 public function testGetSha1( $text, $model, $expected_hash ) {
351 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSha1', $model );
352 $this->assertEquals( $expected_hash, $rev->getSha1() );
356 * @covers Revision::__construct
358 public function testConstructWithText() {
359 $rev = new Revision( [
360 'text' => 'hello world.',
361 'content_model' => CONTENT_MODEL_JAVASCRIPT
364 $this->assertNotNull( $rev->getContent(), 'no content object available' );
365 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContent()->getModel() );
366 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContentModel() );
370 * @covers Revision::__construct
372 public function testConstructWithContent() {
373 $title = Title
::newFromText( 'RevisionTest_testConstructWithContent' );
375 $rev = new Revision( [
376 'content' => ContentHandler
::makeContent( 'hello world.', $title, CONTENT_MODEL_JAVASCRIPT
),
379 $this->assertNotNull( $rev->getContent(), 'no content object available' );
380 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContent()->getModel() );
381 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContentModel() );
385 * Tests whether $rev->getContent() returns a clone when needed.
388 * @covers Revision::getContent
390 public function testGetContentClone() {
391 $content = new RevisionTestModifyableContent( "foo" );
397 'title' => Title
::newFromText( "testGetContentClone_dummy" ),
399 'content' => $content,
400 'length' => $content->getSize(),
401 'comment' => "testing",
402 'minor_edit' => false,
406 /** @var RevisionTestModifyableContent $content */
407 $content = $rev->getContent( Revision
::RAW
);
408 $content->setText( "bar" );
410 /** @var RevisionTestModifyableContent $content2 */
411 $content2 = $rev->getContent( Revision
::RAW
);
412 // content is mutable, expect clone
413 $this->assertNotSame( $content, $content2, "expected a clone" );
414 // clone should contain the original text
415 $this->assertEquals( "foo", $content2->getText() );
417 $content2->setText( "bla bla" );
418 // clones should be independent
419 $this->assertEquals( "bar", $content->getText() );
423 * Tests whether $rev->getContent() returns the same object repeatedly if appropriate.
426 * @covers Revision::getContent
428 public function testGetContentUncloned() {
429 $rev = $this->newTestRevision( "hello", "testGetContentUncloned_dummy", CONTENT_MODEL_WIKITEXT
);
430 $content = $rev->getContent( Revision
::RAW
);
431 $content2 = $rev->getContent( Revision
::RAW
);
433 // for immutable content like wikitext, this should be the same object
434 $this->assertSame( $content, $content2 );