wiki.techinc.nl/tests/phpunit/includes/RevisionTest.php

437 lines
12 KiB
PHP
Raw Normal View History

<?php
/**
* @group ContentHandler
*/
class RevisionTest extends MediaWikiTestCase {
Clean and repair many phpunit tests (+ fix implied configuration) This commit depends on the introduction of MediaWikiTestCase::setMwGlobals in change Iccf6ea81f4. Various tests already set their globals, but forgot to restore them afterwards, or forgot to call the parent setUp, tearDown... Either way they won't have to anymore with setMwGlobals. Consistent use of function characteristics: * protected function setUp * protected function tearDown * public static function (provide..) (Matching the function signature with PHPUnit/Framework/TestCase.php) Replaces: * public function (setUp|tearDown)\( * protected function $1( * \tfunction (setUp|tearDown)\( * \tprotected function $1( * \tfunction (data|provide)\( * \tpublic static function $1\( Also renamed a few "data#", "provider#" and "provides#" functions to "provide#" for consistency. This also removes confusion where the /media tests had a few private methods called dataFile(), which were sometimes expected to be data providers. Fixes: TimestampTest often failed due to a previous test setting a different language (it tests "1 hour ago" so need to make sure it is set to English). MWNamespaceTest became a lot cleaner now that it executes with a known context. Though the now-redundant code that was removed didn't work anyway because wgContentNamespaces isn't keyed by namespace id, it had them was values... FileBackendTest: * Fixed: "PHP Fatal: Using $this when not in object context" HttpTest * Added comment about: "PHP Fatal: Call to protected MWHttpRequest::__construct()" (too much unrelated code to fix in this commit) ExternalStoreTest * Add an assertTrue as well, without it the test is useless because regardless of whether wgExternalStores is true or false it only uses it if it is an array. Change-Id: I9d2b148e57bada64afeb7d5a99bec0e58f8e1561
2012-10-08 10:56:20 +00:00
protected function setUp() {
global $wgContLang;
Clean and repair many phpunit tests (+ fix implied configuration) This commit depends on the introduction of MediaWikiTestCase::setMwGlobals in change Iccf6ea81f4. Various tests already set their globals, but forgot to restore them afterwards, or forgot to call the parent setUp, tearDown... Either way they won't have to anymore with setMwGlobals. Consistent use of function characteristics: * protected function setUp * protected function tearDown * public static function (provide..) (Matching the function signature with PHPUnit/Framework/TestCase.php) Replaces: * public function (setUp|tearDown)\( * protected function $1( * \tfunction (setUp|tearDown)\( * \tprotected function $1( * \tfunction (data|provide)\( * \tpublic static function $1\( Also renamed a few "data#", "provider#" and "provides#" functions to "provide#" for consistency. This also removes confusion where the /media tests had a few private methods called dataFile(), which were sometimes expected to be data providers. Fixes: TimestampTest often failed due to a previous test setting a different language (it tests "1 hour ago" so need to make sure it is set to English). MWNamespaceTest became a lot cleaner now that it executes with a known context. Though the now-redundant code that was removed didn't work anyway because wgContentNamespaces isn't keyed by namespace id, it had them was values... FileBackendTest: * Fixed: "PHP Fatal: Using $this when not in object context" HttpTest * Added comment about: "PHP Fatal: Call to protected MWHttpRequest::__construct()" (too much unrelated code to fix in this commit) ExternalStoreTest * Add an assertTrue as well, without it the test is useless because regardless of whether wgExternalStores is true or false it only uses it if it is an array. Change-Id: I9d2b148e57bada64afeb7d5a99bec0e58f8e1561
2012-10-08 10:56:20 +00:00
parent::setUp();
$this->setMwGlobals( [
Clean and repair many phpunit tests (+ fix implied configuration) This commit depends on the introduction of MediaWikiTestCase::setMwGlobals in change Iccf6ea81f4. Various tests already set their globals, but forgot to restore them afterwards, or forgot to call the parent setUp, tearDown... Either way they won't have to anymore with setMwGlobals. Consistent use of function characteristics: * protected function setUp * protected function tearDown * public static function (provide..) (Matching the function signature with PHPUnit/Framework/TestCase.php) Replaces: * public function (setUp|tearDown)\( * protected function $1( * \tfunction (setUp|tearDown)\( * \tprotected function $1( * \tfunction (data|provide)\( * \tpublic static function $1\( Also renamed a few "data#", "provider#" and "provides#" functions to "provide#" for consistency. This also removes confusion where the /media tests had a few private methods called dataFile(), which were sometimes expected to be data providers. Fixes: TimestampTest often failed due to a previous test setting a different language (it tests "1 hour ago" so need to make sure it is set to English). MWNamespaceTest became a lot cleaner now that it executes with a known context. Though the now-redundant code that was removed didn't work anyway because wgContentNamespaces isn't keyed by namespace id, it had them was values... FileBackendTest: * Fixed: "PHP Fatal: Using $this when not in object context" HttpTest * Added comment about: "PHP Fatal: Call to protected MWHttpRequest::__construct()" (too much unrelated code to fix in this commit) ExternalStoreTest * Add an assertTrue as well, without it the test is useless because regardless of whether wgExternalStores is true or false it only uses it if it is an array. Change-Id: I9d2b148e57bada64afeb7d5a99bec0e58f8e1561
2012-10-08 10:56:20 +00:00
'wgContLang' => Language::factory( 'en' ),
'wgLanguageCode' => 'en',
'wgLegacyEncoding' => false,
'wgCompressRevisions' => false,
'wgContentHandlerTextFallback' => 'ignore',
] );
$this->mergeMwGlobalArrayValue(
'wgExtraNamespaces',
[
12312 => 'Dummy',
12313 => 'Dummy_talk',
]
);
$this->mergeMwGlobalArrayValue(
'wgNamespaceContentModels',
[
12312 => 'testing',
]
);
$this->mergeMwGlobalArrayValue(
'wgContentHandlers',
[
'testing' => 'DummyContentHandlerForTesting',
'RevisionTestModifyableContent' => 'RevisionTestModifyableContentHandler',
]
);
MWNamespace::clearCaches();
// Reset namespace cache
$wgContLang->resetNamespaces();
}
protected function tearDown() {
global $wgContLang;
MWNamespace::clearCaches();
// Reset namespace cache
$wgContLang->resetNamespaces();
parent::tearDown();
}
/**
* @covers Revision::getRevisionText
*/
public function testGetRevisionText() {
$row = new stdClass;
$row->old_flags = '';
$row->old_text = 'This is a bunch of revision text.';
$this->assertEquals(
'This is a bunch of revision text.',
Revision::getRevisionText( $row ) );
}
/**
* @covers Revision::getRevisionText
*/
public function testGetRevisionTextGzip() {
$this->checkPHPExtension( 'zlib' );
$row = new stdClass;
$row->old_flags = 'gzip';
$row->old_text = gzdeflate( 'This is a bunch of revision text.' );
$this->assertEquals(
'This is a bunch of revision text.',
Revision::getRevisionText( $row ) );
}
/**
* @covers Revision::getRevisionText
*/
public function testGetRevisionTextUtf8Native() {
$row = new stdClass;
$row->old_flags = 'utf-8';
$row->old_text = "Wiki est l'\xc3\xa9cole superieur !";
$GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
$this->assertEquals(
"Wiki est l'\xc3\xa9cole superieur !",
Revision::getRevisionText( $row ) );
}
/**
* @covers Revision::getRevisionText
*/
public function testGetRevisionTextUtf8Legacy() {
$row = new stdClass;
$row->old_flags = '';
$row->old_text = "Wiki est l'\xe9cole superieur !";
$GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
$this->assertEquals(
"Wiki est l'\xc3\xa9cole superieur !",
Revision::getRevisionText( $row ) );
}
/**
* @covers Revision::getRevisionText
*/
public function testGetRevisionTextUtf8NativeGzip() {
$this->checkPHPExtension( 'zlib' );
$row = new stdClass;
$row->old_flags = 'gzip,utf-8';
$row->old_text = gzdeflate( "Wiki est l'\xc3\xa9cole superieur !" );
$GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
$this->assertEquals(
"Wiki est l'\xc3\xa9cole superieur !",
Revision::getRevisionText( $row ) );
}
/**
* @covers Revision::getRevisionText
*/
public function testGetRevisionTextUtf8LegacyGzip() {
$this->checkPHPExtension( 'zlib' );
$row = new stdClass;
$row->old_flags = 'gzip';
$row->old_text = gzdeflate( "Wiki est l'\xe9cole superieur !" );
$GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
$this->assertEquals(
"Wiki est l'\xc3\xa9cole superieur !",
Revision::getRevisionText( $row ) );
}
/**
* @covers Revision::compressRevisionText
*/
public function testCompressRevisionTextUtf8() {
$row = new stdClass;
$row->old_text = "Wiki est l'\xc3\xa9cole superieur !";
$row->old_flags = Revision::compressRevisionText( $row->old_text );
$this->assertTrue( false !== strpos( $row->old_flags, 'utf-8' ),
"Flags should contain 'utf-8'" );
$this->assertFalse( false !== strpos( $row->old_flags, 'gzip' ),
"Flags should not contain 'gzip'" );
$this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
$row->old_text, "Direct check" );
$this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
Revision::getRevisionText( $row ), "getRevisionText" );
}
/**
* @covers Revision::compressRevisionText
*/
public function testCompressRevisionTextUtf8Gzip() {
$this->checkPHPExtension( 'zlib' );
$this->setMwGlobals( 'wgCompressRevisions', true );
Clean and repair many phpunit tests (+ fix implied configuration) This commit depends on the introduction of MediaWikiTestCase::setMwGlobals in change Iccf6ea81f4. Various tests already set their globals, but forgot to restore them afterwards, or forgot to call the parent setUp, tearDown... Either way they won't have to anymore with setMwGlobals. Consistent use of function characteristics: * protected function setUp * protected function tearDown * public static function (provide..) (Matching the function signature with PHPUnit/Framework/TestCase.php) Replaces: * public function (setUp|tearDown)\( * protected function $1( * \tfunction (setUp|tearDown)\( * \tprotected function $1( * \tfunction (data|provide)\( * \tpublic static function $1\( Also renamed a few "data#", "provider#" and "provides#" functions to "provide#" for consistency. This also removes confusion where the /media tests had a few private methods called dataFile(), which were sometimes expected to be data providers. Fixes: TimestampTest often failed due to a previous test setting a different language (it tests "1 hour ago" so need to make sure it is set to English). MWNamespaceTest became a lot cleaner now that it executes with a known context. Though the now-redundant code that was removed didn't work anyway because wgContentNamespaces isn't keyed by namespace id, it had them was values... FileBackendTest: * Fixed: "PHP Fatal: Using $this when not in object context" HttpTest * Added comment about: "PHP Fatal: Call to protected MWHttpRequest::__construct()" (too much unrelated code to fix in this commit) ExternalStoreTest * Add an assertTrue as well, without it the test is useless because regardless of whether wgExternalStores is true or false it only uses it if it is an array. Change-Id: I9d2b148e57bada64afeb7d5a99bec0e58f8e1561
2012-10-08 10:56:20 +00:00
$row = new stdClass;
$row->old_text = "Wiki est l'\xc3\xa9cole superieur !";
$row->old_flags = Revision::compressRevisionText( $row->old_text );
$this->assertTrue( false !== strpos( $row->old_flags, 'utf-8' ),
"Flags should contain 'utf-8'" );
$this->assertTrue( false !== strpos( $row->old_flags, 'gzip' ),
"Flags should contain 'gzip'" );
$this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
gzinflate( $row->old_text ), "Direct check" );
$this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
Revision::getRevisionText( $row ), "getRevisionText" );
}
# =========================================================================
/**
* @param string $text
* @param string $title
* @param string $model
* @param string $format
*
* @return Revision
*/
private function newTestRevision( $text, $title = "Test",
$model = CONTENT_MODEL_WIKITEXT, $format = null
) {
if ( is_string( $title ) ) {
$title = Title::newFromText( $title );
}
$content = ContentHandler::makeContent( $text, $title, $model, $format );
$rev = new Revision(
[
'id' => 42,
'page' => 23,
'title' => $title,
'content' => $content,
'length' => $content->getSize(),
'comment' => "testing",
'minor_edit' => false,
'content_format' => $format,
]
);
return $rev;
}
public function dataGetContentModel() {
// NOTE: we expect the help namespace to always contain wikitext
return [
[ 'hello world', 'Help:Hello', null, null, CONTENT_MODEL_WIKITEXT ],
[ 'hello world', 'User:hello/there.css', null, null, CONTENT_MODEL_CSS ],
[ serialize( 'hello world' ), 'Dummy:Hello', null, null, "testing" ],
];
}
/**
* @group Database
* @dataProvider dataGetContentModel
* @covers Revision::getContentModel
*/
public function testGetContentModel( $text, $title, $model, $format, $expectedModel ) {
$rev = $this->newTestRevision( $text, $title, $model, $format );
$this->assertEquals( $expectedModel, $rev->getContentModel() );
}
public function dataGetContentFormat() {
// NOTE: we expect the help namespace to always contain wikitext
return [
[ 'hello world', 'Help:Hello', null, null, CONTENT_FORMAT_WIKITEXT ],
[ 'hello world', 'Help:Hello', CONTENT_MODEL_CSS, null, CONTENT_FORMAT_CSS ],
[ 'hello world', 'User:hello/there.css', null, null, CONTENT_FORMAT_CSS ],
[ serialize( 'hello world' ), 'Dummy:Hello', null, null, "testing" ],
];
}
/**
* @group Database
* @dataProvider dataGetContentFormat
* @covers Revision::getContentFormat
*/
public function testGetContentFormat( $text, $title, $model, $format, $expectedFormat ) {
$rev = $this->newTestRevision( $text, $title, $model, $format );
$this->assertEquals( $expectedFormat, $rev->getContentFormat() );
}
public function dataGetContentHandler() {
// NOTE: we expect the help namespace to always contain wikitext
return [
[ 'hello world', 'Help:Hello', null, null, 'WikitextContentHandler' ],
[ 'hello world', 'User:hello/there.css', null, null, 'CssContentHandler' ],
[ serialize( 'hello world' ), 'Dummy:Hello', null, null, 'DummyContentHandlerForTesting' ],
];
}
/**
* @group Database
* @dataProvider dataGetContentHandler
* @covers Revision::getContentHandler
*/
public function testGetContentHandler( $text, $title, $model, $format, $expectedClass ) {
$rev = $this->newTestRevision( $text, $title, $model, $format );
$this->assertEquals( $expectedClass, get_class( $rev->getContentHandler() ) );
}
public function dataGetContent() {
// NOTE: we expect the help namespace to always contain wikitext
return [
[ 'hello world', 'Help:Hello', null, null, Revision::FOR_PUBLIC, 'hello world' ],
[
serialize( 'hello world' ),
'Hello',
"testing",
null,
Revision::FOR_PUBLIC,
serialize( 'hello world' )
],
[
serialize( 'hello world' ),
'Dummy:Hello',
null,
null,
Revision::FOR_PUBLIC,
serialize( 'hello world' )
],
];
}
/**
* @group Database
* @dataProvider dataGetContent
* @covers Revision::getContent
*/
public function testGetContent( $text, $title, $model, $format,
$audience, $expectedSerialization
) {
$rev = $this->newTestRevision( $text, $title, $model, $format );
$content = $rev->getContent( $audience );
$this->assertEquals(
$expectedSerialization,
is_null( $content ) ? null : $content->serialize( $format )
);
}
public function dataGetSize() {
return [
[ "hello world.", CONTENT_MODEL_WIKITEXT, 12 ],
[ serialize( "hello world." ), "testing", 12 ],
];
}
/**
* @covers Revision::getSize
* @group Database
* @dataProvider dataGetSize
*/
public function testGetSize( $text, $model, $expected_size ) {
$rev = $this->newTestRevision( $text, 'RevisionTest_testGetSize', $model );
$this->assertEquals( $expected_size, $rev->getSize() );
}
public function dataGetSha1() {
return [
[ "hello world.", CONTENT_MODEL_WIKITEXT, Revision::base36Sha1( "hello world." ) ],
[
serialize( "hello world." ),
"testing",
Revision::base36Sha1( serialize( "hello world." ) )
],
];
}
/**
* @covers Revision::getSha1
* @group Database
* @dataProvider dataGetSha1
*/
public function testGetSha1( $text, $model, $expected_hash ) {
$rev = $this->newTestRevision( $text, 'RevisionTest_testGetSha1', $model );
$this->assertEquals( $expected_hash, $rev->getSha1() );
}
/**
* @covers Revision::__construct
*/
public function testConstructWithText() {
$rev = new Revision( [
'text' => 'hello world.',
'content_model' => CONTENT_MODEL_JAVASCRIPT
] );
$this->assertNotNull( $rev->getContent(), 'no content object available' );
$this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContent()->getModel() );
$this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContentModel() );
}
/**
* @covers Revision::__construct
*/
public function testConstructWithContent() {
$title = Title::newFromText( 'RevisionTest_testConstructWithContent' );
$rev = new Revision( [
'content' => ContentHandler::makeContent( 'hello world.', $title, CONTENT_MODEL_JAVASCRIPT ),
] );
$this->assertNotNull( $rev->getContent(), 'no content object available' );
$this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContent()->getModel() );
$this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContentModel() );
}
/**
* Tests whether $rev->getContent() returns a clone when needed.
*
* @group Database
* @covers Revision::getContent
*/
public function testGetContentClone() {
$content = new RevisionTestModifyableContent( "foo" );
$rev = new Revision(
[
'id' => 42,
'page' => 23,
'title' => Title::newFromText( "testGetContentClone_dummy" ),
'content' => $content,
'length' => $content->getSize(),
'comment' => "testing",
'minor_edit' => false,
]
);
/** @var RevisionTestModifyableContent $content */
$content = $rev->getContent( Revision::RAW );
$content->setText( "bar" );
/** @var RevisionTestModifyableContent $content2 */
$content2 = $rev->getContent( Revision::RAW );
// content is mutable, expect clone
$this->assertNotSame( $content, $content2, "expected a clone" );
// clone should contain the original text
$this->assertEquals( "foo", $content2->getText() );
$content2->setText( "bla bla" );
// clones should be independent
$this->assertEquals( "bar", $content->getText() );
}
/**
* Tests whether $rev->getContent() returns the same object repeatedly if appropriate.
*
* @group Database
* @covers Revision::getContent
*/
public function testGetContentUncloned() {
$rev = $this->newTestRevision( "hello", "testGetContentUncloned_dummy", CONTENT_MODEL_WIKITEXT );
$content = $rev->getContent( Revision::RAW );
$content2 = $rev->getContent( Revision::RAW );
// for immutable content like wikitext, this should be the same object
$this->assertSame( $content, $content2 );
}
}