MediaWiki  1.30.0
RevisionTest.php
Go to the documentation of this file.
1 <?php
2 
7  protected function setUp() {
9 
10  parent::setUp();
11 
12  $this->setMwGlobals( [
13  'wgContLang' => Language::factory( 'en' ),
14  'wgLanguageCode' => 'en',
15  'wgLegacyEncoding' => false,
16  'wgCompressRevisions' => false,
17 
18  'wgContentHandlerTextFallback' => 'ignore',
19  ] );
20 
22  'wgExtraNamespaces',
23  [
24  12312 => 'Dummy',
25  12313 => 'Dummy_talk',
26  ]
27  );
28 
30  'wgNamespaceContentModels',
31  [
32  12312 => 'testing',
33  ]
34  );
35 
37  'wgContentHandlers',
38  [
39  'testing' => 'DummyContentHandlerForTesting',
40  'RevisionTestModifyableContent' => 'RevisionTestModifyableContentHandler',
41  ]
42  );
43 
44  MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
45  $wgContLang->resetNamespaces(); # reset namespace cache
46  }
47 
50 
51  MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
52  $wgContLang->resetNamespaces(); # reset namespace cache
53 
54  parent::tearDown();
55  }
56 
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 
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 
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 
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 
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 
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 
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 
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 
186  function newTestRevision( $text, $title = "Test",
187  $model = CONTENT_MODEL_WIKITEXT, $format = null
188  ) {
189  if ( is_string( $title ) ) {
191  }
192 
193  $content = ContentHandler::makeContent( $text, $title, $model, $format );
194 
195  $rev = new Revision(
196  [
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 [
216  [ 'hello world', 'Help:Hello', null, null, CONTENT_MODEL_WIKITEXT ],
217  [ 'hello world', 'User:hello/there.css', null, null, CONTENT_MODEL_CSS ],
218  [ serialize( 'hello world' ), 'Dummy:Hello', null, null, "testing" ],
219  ];
220  }
221 
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 [
236  [ 'hello world', 'Help:Hello', null, null, CONTENT_FORMAT_WIKITEXT ],
237  [ 'hello world', 'Help:Hello', CONTENT_MODEL_CSS, null, CONTENT_FORMAT_CSS ],
238  [ 'hello world', 'User:hello/there.css', null, null, CONTENT_FORMAT_CSS ],
239  [ serialize( 'hello world' ), 'Dummy:Hello', null, null, "testing" ],
240  ];
241  }
242 
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 
255  // NOTE: we expect the help namespace to always contain wikitext
256  return [
257  [ 'hello world', 'Help:Hello', null, null, 'WikitextContentHandler' ],
258  [ 'hello world', 'User:hello/there.css', null, null, 'CssContentHandler' ],
259  [ serialize( 'hello world' ), 'Dummy:Hello', null, null, 'DummyContentHandlerForTesting' ],
260  ];
261  }
262 
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 [
277  [ 'hello world', 'Help:Hello', null, null, Revision::FOR_PUBLIC, 'hello world' ],
278  [
279  serialize( 'hello world' ),
280  'Hello',
281  "testing",
282  null,
284  serialize( 'hello world' )
285  ],
286  [
287  serialize( 'hello world' ),
288  'Dummy:Hello',
289  null,
290  null,
292  serialize( 'hello world' )
293  ],
294  ];
295  }
296 
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  public function dataGetSize() {
315  return [
316  [ "hello world.", CONTENT_MODEL_WIKITEXT, 12 ],
317  [ serialize( "hello world." ), "testing", 12 ],
318  ];
319  }
320 
326  public function testGetSize( $text, $model, $expected_size ) {
327  $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSize', $model );
328  $this->assertEquals( $expected_size, $rev->getSize() );
329  }
330 
331  public function dataGetSha1() {
332  return [
333  [ "hello world.", CONTENT_MODEL_WIKITEXT, Revision::base36Sha1( "hello world." ) ],
334  [
335  serialize( "hello world." ),
336  "testing",
337  Revision::base36Sha1( serialize( "hello world." ) )
338  ],
339  ];
340  }
341 
347  public function testGetSha1( $text, $model, $expected_hash ) {
348  $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSha1', $model );
349  $this->assertEquals( $expected_hash, $rev->getSha1() );
350  }
351 
355  public function testConstructWithText() {
356  $rev = new Revision( [
357  'text' => 'hello world.',
358  'content_model' => CONTENT_MODEL_JAVASCRIPT
359  ] );
360 
361  $this->assertNotNull( $rev->getContent(), 'no content object available' );
362  $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContent()->getModel() );
363  $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContentModel() );
364  }
365 
369  public function testConstructWithContent() {
370  $title = Title::newFromText( 'RevisionTest_testConstructWithContent' );
371 
372  $rev = new Revision( [
373  'content' => ContentHandler::makeContent( 'hello world.', $title, CONTENT_MODEL_JAVASCRIPT ),
374  ] );
375 
376  $this->assertNotNull( $rev->getContent(), 'no content object available' );
377  $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContent()->getModel() );
378  $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContentModel() );
379  }
380 
387  public function testGetContentClone() {
388  $content = new RevisionTestModifyableContent( "foo" );
389 
390  $rev = new Revision(
391  [
392  'id' => 42,
393  'page' => 23,
394  'title' => Title::newFromText( "testGetContentClone_dummy" ),
395 
396  'content' => $content,
397  'length' => $content->getSize(),
398  'comment' => "testing",
399  'minor_edit' => false,
400  ]
401  );
402 
403  $content = $rev->getContent( Revision::RAW );
404  $content->setText( "bar" );
405 
406  $content2 = $rev->getContent( Revision::RAW );
407  // content is mutable, expect clone
408  $this->assertNotSame( $content, $content2, "expected a clone" );
409  // clone should contain the original text
410  $this->assertEquals( "foo", $content2->getText() );
411 
412  $content2->setText( "bla bla" );
413  $this->assertEquals( "bar", $content->getText() ); // clones should be independent
414  }
415 
422  public function testGetContentUncloned() {
423  $rev = $this->newTestRevision( "hello", "testGetContentUncloned_dummy", CONTENT_MODEL_WIKITEXT );
424  $content = $rev->getContent( Revision::RAW );
425  $content2 = $rev->getContent( Revision::RAW );
426 
427  // for immutable content like wikitext, this should be the same object
428  $this->assertSame( $content, $content2 );
429  }
430 }
431 
433  public function __construct( $text ) {
434  parent::__construct( $text, "RevisionTestModifyableContent" );
435  }
436 
437  public function copy() {
438  return new RevisionTestModifyableContent( $this->mText );
439  }
440 
441  public function getText() {
442  return $this->mText;
443  }
444 
445  public function setText( $text ) {
446  $this->mText = $text;
447  }
448 }
449 
451 
452  public function __construct() {
453  parent::__construct( "RevisionTestModifyableContent", [ CONTENT_FORMAT_TEXT ] );
454  }
455 
456  public function unserializeContent( $text, $format = null ) {
457  $this->checkFormat( $format );
458 
459  return new RevisionTestModifyableContent( $text );
460  }
461 
462  public function makeEmptyContent() {
463  return new RevisionTestModifyableContent( '' );
464  }
465 }
Revision\FOR_PUBLIC
const FOR_PUBLIC
Definition: Revision.php:98
function
when a variable name is used in a function
Definition: design.txt:93
RevisionTestModifyableContent\getText
getText()
Definition: RevisionTest.php:441
RevisionTest\tearDown
tearDown()
Definition: RevisionTest.php:48
RevisionTest\testConstructWithContent
testConstructWithContent()
Revision::__construct.
Definition: RevisionTest.php:369
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:268
RevisionTest\testGetSize
testGetSize( $text, $model, $expected_size)
Revision::getSize Database dataGetSize.
Definition: RevisionTest.php:326
MediaWikiTestCase\mergeMwGlobalArrayValue
mergeMwGlobalArrayValue( $name, $values)
Merges the given values into a MW global array variable.
Definition: MediaWikiTestCase.php:807
CONTENT_MODEL_CSS
const CONTENT_MODEL_CSS
Definition: Defines.php:238
RevisionTestModifyableContentHandler\__construct
__construct()
Definition: RevisionTest.php:452
RevisionTest\testGetRevisionTextUtf8Native
testGetRevisionTextUtf8Native()
Revision::getRevisionText.
Definition: RevisionTest.php:86
serialize
serialize()
Definition: ApiMessage.php:177
Revision\getRevisionText
static getRevisionText( $row, $prefix='old_', $wiki=false)
Get revision text associated with an old or archive row.
Definition: Revision.php:1266
CONTENT_FORMAT_CSS
const CONTENT_FORMAT_CSS
Definition: Defines.php:255
RevisionTest\dataGetContent
dataGetContent()
Definition: RevisionTest.php:274
CONTENT_MODEL_WIKITEXT
const CONTENT_MODEL_WIKITEXT
Definition: Defines.php:236
RevisionTestModifyableContent\setText
setText( $text)
Definition: RevisionTest.php:445
RevisionTest\testGetRevisionTextUtf8NativeGzip
testGetRevisionTextUtf8NativeGzip()
Revision::getRevisionText.
Definition: RevisionTest.php:112
cache
you have access to all of the normal MediaWiki so you can get a DB use the cache
Definition: maintenance.txt:52
Revision\base36Sha1
static base36Sha1( $text)
Get the base 36 SHA-1 value for a string of text.
Definition: Revision.php:1600
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
RevisionTest\testGetRevisionTextUtf8LegacyGzip
testGetRevisionTextUtf8LegacyGzip()
Revision::getRevisionText.
Definition: RevisionTest.php:127
Revision
Definition: Revision.php:33
RevisionTest\testGetRevisionText
testGetRevisionText()
Revision::getRevisionText.
Definition: RevisionTest.php:60
RevisionTest\testConstructWithText
testConstructWithText()
Revision::__construct.
Definition: RevisionTest.php:355
RevisionTest\testCompressRevisionTextUtf8Gzip
testCompressRevisionTextUtf8Gzip()
Revision::compressRevisionText.
Definition: RevisionTest.php:159
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:932
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
Definition: MediaWikiTestCase.php:672
MediaWikiTestCase
Definition: MediaWikiTestCase.php:15
Revision\compressRevisionText
static compressRevisionText(&$text)
If $wgCompressRevisions is enabled, we will compress data.
Definition: Revision.php:1323
RevisionTest\dataGetContentModel
dataGetContentModel()
Definition: RevisionTest.php:213
RevisionTest\setUp
setUp()
Definition: RevisionTest.php:7
CONTENT_FORMAT_TEXT
const CONTENT_FORMAT_TEXT
Definition: Defines.php:257
RevisionTest\dataGetContentHandler
dataGetContentHandler()
Definition: RevisionTest.php:254
RevisionTestModifyableContent\__construct
__construct( $text)
Definition: RevisionTest.php:433
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
$GLOBALS
$GLOBALS['wgAutoloadClasses']['LocalisationUpdate']
Definition: Autoload.php:10
CONTENT_FORMAT_WIKITEXT
const CONTENT_FORMAT_WIKITEXT
Definition: Defines.php:251
RevisionTest
ContentHandler.
Definition: RevisionTest.php:6
RevisionTest\newTestRevision
newTestRevision( $text, $title="Test", $model=CONTENT_MODEL_WIKITEXT, $format=null)
Definition: RevisionTest.php:186
ContentHandler\makeContent
static makeContent( $text, Title $title=null, $modelId=null, $format=null)
Convenience function for creating a Content object from a given textual representation.
Definition: ContentHandler.php:129
RevisionTest\dataGetSha1
dataGetSha1()
Definition: RevisionTest.php:331
TextContentHandler
Base content handler implementation for flat text contents.
Definition: TextContentHandler.php:31
Revision\RAW
const RAW
Definition: Revision.php:100
RevisionTestModifyableContentHandler
Definition: RevisionTest.php:450
RevisionTest\testGetContentModel
testGetContentModel( $text, $title, $model, $format, $expectedModel)
Database dataGetContentModel Revision::getContentModel.
Definition: RevisionTest.php:227
TextContent
Content object implementation for representing flat text.
Definition: TextContent.php:35
RevisionTest\testGetRevisionTextGzip
testGetRevisionTextGzip()
Revision::getRevisionText.
Definition: RevisionTest.php:72
RevisionTestModifyableContent\copy
copy()
Definition: RevisionTest.php:437
RevisionTest\testGetContentHandler
testGetContentHandler( $text, $title, $model, $format, $expectedClass)
Database dataGetContentHandler Revision::getContentHandler.
Definition: RevisionTest.php:268
RevisionTestModifyableContentHandler\makeEmptyContent
makeEmptyContent()
Creates an empty TextContent object.
Definition: RevisionTest.php:462
RevisionTest\testGetRevisionTextUtf8Legacy
testGetRevisionTextUtf8Legacy()
Revision::getRevisionText.
Definition: RevisionTest.php:99
RevisionTest\testGetContentFormat
testGetContentFormat( $text, $title, $model, $format, $expectedFormat)
Database dataGetContentFormat Revision::getContentFormat.
Definition: RevisionTest.php:248
RevisionTest\testGetContentUncloned
testGetContentUncloned()
Tests whether $rev->getContent() returns the same object repeatedly if appropriate.
Definition: RevisionTest.php:422
$rev
presenting them properly to the user as errors is done by the caller return true use this to change the list i e etc $rev
Definition: hooks.txt:1750
MWNamespace\getCanonicalNamespaces
static getCanonicalNamespaces( $rebuild=false)
Returns array of all defined namespaces with their canonical (English) names.
Definition: MWNamespace.php:207
MediaWikiTestCase\checkPHPExtension
checkPHPExtension( $extName)
Check if $extName is a loaded PHP extension, will skip the test whenever it is not loaded.
Definition: MediaWikiTestCase.php:1770
RevisionTestModifyableContent
Definition: RevisionTest.php:432
Language\factory
static factory( $code)
Get a cached or new language object for a given language code.
Definition: Language.php:183
CONTENT_MODEL_JAVASCRIPT
const CONTENT_MODEL_JAVASCRIPT
Definition: Defines.php:237
RevisionTest\testGetContent
testGetContent( $text, $title, $model, $format, $audience, $expectedSerialization)
Database dataGetContent Revision::getContent.
Definition: RevisionTest.php:302
RevisionTest\dataGetContentFormat
dataGetContentFormat()
Definition: RevisionTest.php:233
RevisionTest\testGetContentClone
testGetContentClone()
Tests whether $rev->getContent() returns a clone when needed.
Definition: RevisionTest.php:387
RevisionTestModifyableContentHandler\unserializeContent
unserializeContent( $text, $format=null)
Unserializes a Content object of the type supported by this ContentHandler.
Definition: RevisionTest.php:456
RevisionTest\testCompressRevisionTextUtf8
testCompressRevisionTextUtf8()
Revision::compressRevisionText.
Definition: RevisionTest.php:142
RevisionTest\dataGetSize
dataGetSize()
Definition: RevisionTest.php:314
RevisionTest\testGetSha1
testGetSha1( $text, $model, $expected_hash)
Revision::getSha1 Database dataGetSha1.
Definition: RevisionTest.php:347
$wgContLang
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as and the content language as $wgContLang
Definition: design.txt:56