2010-12-14 16:26:35 +00:00
|
|
|
<?php
|
|
|
|
|
|
2012-05-13 22:02:29 +00:00
|
|
|
/**
|
|
|
|
|
* @group ContentHandler
|
|
|
|
|
*/
|
2010-12-28 18:17:16 +00:00
|
|
|
class RevisionTest extends MediaWikiTestCase {
|
2010-12-14 16:26:35 +00:00
|
|
|
var $saveGlobals = array();
|
|
|
|
|
|
|
|
|
|
function setUp() {
|
|
|
|
|
global $wgContLang;
|
|
|
|
|
$wgContLang = Language::factory( 'en' );
|
2012-04-23 19:22:06 +00:00
|
|
|
|
2010-12-14 16:26:35 +00:00
|
|
|
$globalSet = array(
|
|
|
|
|
'wgLegacyEncoding' => false,
|
|
|
|
|
'wgCompressRevisions' => false,
|
2012-04-23 19:22:06 +00:00
|
|
|
|
|
|
|
|
'wgContentHandlerTextFallback' => $GLOBALS['wgContentHandlerTextFallback'],
|
|
|
|
|
'wgExtraNamespaces' => $GLOBALS['wgExtraNamespaces'],
|
|
|
|
|
'wgNamespaceContentModels' => $GLOBALS['wgNamespaceContentModels'],
|
|
|
|
|
'wgContentHandlers' => $GLOBALS['wgContentHandlers'],
|
2011-05-15 12:09:59 +00:00
|
|
|
);
|
2012-04-23 19:22:06 +00:00
|
|
|
|
2010-12-14 16:26:35 +00:00
|
|
|
foreach ( $globalSet as $var => $data ) {
|
|
|
|
|
$this->saveGlobals[$var] = $GLOBALS[$var];
|
|
|
|
|
$GLOBALS[$var] = $data;
|
|
|
|
|
}
|
2012-04-23 19:22:06 +00:00
|
|
|
|
2012-04-26 10:11:01 +00:00
|
|
|
global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
|
2012-04-23 19:22:06 +00:00
|
|
|
$wgExtraNamespaces[ 12312 ] = 'Dummy';
|
|
|
|
|
$wgExtraNamespaces[ 12313 ] = 'Dummy_talk';
|
|
|
|
|
|
2012-05-13 22:02:29 +00:00
|
|
|
$wgNamespaceContentModels[ 12312 ] = 999999;
|
|
|
|
|
$wgContentHandlers[ 999999 ] = 'DummyContentHandlerForTesting';
|
2012-04-23 19:22:06 +00:00
|
|
|
|
2012-04-26 10:11:01 +00:00
|
|
|
MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
|
|
|
|
|
$wgContLang->resetNamespaces(); # reset namespace cache
|
|
|
|
|
|
2012-04-23 19:22:06 +00:00
|
|
|
global $wgContentHandlerTextFallback;
|
|
|
|
|
$wgContentHandlerTextFallback = 'ignore';
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function tearDown() {
|
2012-04-26 10:11:01 +00:00
|
|
|
global $wgContLang;
|
|
|
|
|
|
2010-12-14 16:26:35 +00:00
|
|
|
foreach ( $this->saveGlobals as $var => $data ) {
|
|
|
|
|
$GLOBALS[$var] = $data;
|
|
|
|
|
}
|
2012-04-26 10:11:01 +00:00
|
|
|
|
|
|
|
|
MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
|
|
|
|
|
$wgContLang->resetNamespaces(); # reset namespace cache
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function testGetRevisionTextGzip() {
|
2011-06-17 21:15:24 +00:00
|
|
|
if ( !function_exists( 'gzdeflate' ) ) {
|
|
|
|
|
$this->markTestSkipped( 'Gzip compression is not enabled (requires zlib).' );
|
|
|
|
|
} else {
|
|
|
|
|
$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 ) );
|
|
|
|
|
}
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function testGetRevisionTextUtf8NativeGzip() {
|
2011-06-17 21:15:24 +00:00
|
|
|
if ( !function_exists( 'gzdeflate' ) ) {
|
|
|
|
|
$this->markTestSkipped( 'Gzip compression is not enabled (requires zlib).' );
|
|
|
|
|
} else {
|
|
|
|
|
$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 ) );
|
|
|
|
|
}
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function testGetRevisionTextUtf8LegacyGzip() {
|
2011-06-17 21:15:24 +00:00
|
|
|
if ( !function_exists( 'gzdeflate' ) ) {
|
|
|
|
|
$this->markTestSkipped( 'Gzip compression is not enabled (requires zlib).' );
|
|
|
|
|
} else {
|
|
|
|
|
$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 ) );
|
|
|
|
|
}
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function testCompressRevisionTextUtf8Gzip() {
|
|
|
|
|
$GLOBALS['wgCompressRevisions'] = true;
|
|
|
|
|
$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" );
|
|
|
|
|
}
|
2012-04-23 19:22:06 +00:00
|
|
|
|
|
|
|
|
# =================================================================================================================
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $text
|
|
|
|
|
* @param string $title
|
|
|
|
|
* @param string $model
|
|
|
|
|
* @return Revision
|
|
|
|
|
*/
|
|
|
|
|
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(
|
|
|
|
|
array(
|
|
|
|
|
'id' => 42,
|
|
|
|
|
'page' => 23,
|
|
|
|
|
'title' => $title,
|
|
|
|
|
|
|
|
|
|
'content' => $content,
|
|
|
|
|
'length' => $content->getSize(),
|
|
|
|
|
'comment' => "testing",
|
|
|
|
|
'minor_edit' => false,
|
|
|
|
|
|
|
|
|
|
'content_format' => $format,
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return $rev;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function dataGetContentModel() {
|
|
|
|
|
return array(
|
|
|
|
|
array( 'hello world', 'Hello', null, null, CONTENT_MODEL_WIKITEXT ),
|
|
|
|
|
array( 'hello world', 'User:hello/there.css', null, null, CONTENT_MODEL_CSS ),
|
2012-05-13 22:02:29 +00:00
|
|
|
array( serialize('hello world'), 'Dummy:Hello', null, null, 999999 ),
|
2012-04-23 19:22:06 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider dataGetContentModel
|
|
|
|
|
*/
|
|
|
|
|
function testGetContentModel( $text, $title, $model, $format, $expectedModel ) {
|
|
|
|
|
$rev = $this->newTestRevision( $text, $title, $model, $format );
|
|
|
|
|
|
2012-05-13 22:02:29 +00:00
|
|
|
$this->assertEquals( $expectedModel, $rev->getContentModel() );
|
2012-04-23 19:22:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function dataGetContentFormat() {
|
|
|
|
|
return array(
|
2012-05-13 22:02:29 +00:00
|
|
|
array( 'hello world', 'Hello', null, null, CONTENT_FORMAT_WIKITEXT ),
|
|
|
|
|
array( 'hello world', 'Hello', CONTENT_MODEL_CSS, null, CONTENT_FORMAT_CSS ),
|
|
|
|
|
array( 'hello world', 'User:hello/there.css', null, null, CONTENT_FORMAT_CSS ),
|
|
|
|
|
array( serialize('hello world'), 'Dummy:Hello', null, null, 999999 ),
|
2012-04-23 19:22:06 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider dataGetContentFormat
|
|
|
|
|
*/
|
|
|
|
|
function testGetContentFormat( $text, $title, $model, $format, $expectedFormat ) {
|
|
|
|
|
$rev = $this->newTestRevision( $text, $title, $model, $format );
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( $expectedFormat, $rev->getContentFormat() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function dataGetContentHandler() {
|
|
|
|
|
return array(
|
|
|
|
|
array( 'hello world', 'Hello', null, null, 'WikitextContentHandler' ),
|
|
|
|
|
array( 'hello world', 'User:hello/there.css', null, null, 'CssContentHandler' ),
|
|
|
|
|
array( serialize('hello world'), 'Dummy:Hello', null, null, 'DummyContentHandlerForTesting' ),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider dataGetContentHandler
|
|
|
|
|
*/
|
|
|
|
|
function testGetContentHandler( $text, $title, $model, $format, $expectedClass ) {
|
|
|
|
|
$rev = $this->newTestRevision( $text, $title, $model, $format );
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( $expectedClass, get_class( $rev->getContentHandler() ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function dataGetContent() {
|
|
|
|
|
return array(
|
|
|
|
|
array( 'hello world', 'Hello', null, null, Revision::FOR_PUBLIC, 'hello world' ),
|
2012-05-13 22:02:29 +00:00
|
|
|
array( serialize('hello world'), 'Hello', 999999, null, Revision::FOR_PUBLIC, serialize('hello world') ),
|
2012-04-23 19:22:06 +00:00
|
|
|
array( serialize('hello world'), 'Dummy:Hello', null, null, Revision::FOR_PUBLIC, serialize('hello world') ),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider dataGetContent
|
|
|
|
|
*/
|
|
|
|
|
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 ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function dataGetText() {
|
|
|
|
|
return array(
|
|
|
|
|
array( 'hello world', 'Hello', null, null, Revision::FOR_PUBLIC, 'hello world' ),
|
2012-05-13 22:02:29 +00:00
|
|
|
array( serialize('hello world'), 'Hello', 999999, null, Revision::FOR_PUBLIC, null ),
|
2012-04-23 19:22:06 +00:00
|
|
|
array( serialize('hello world'), 'Dummy:Hello', null, null, Revision::FOR_PUBLIC, null ),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider dataGetText
|
|
|
|
|
*/
|
|
|
|
|
function testGetText( $text, $title, $model, $format, $audience, $expectedText ) {
|
|
|
|
|
$rev = $this->newTestRevision( $text, $title, $model, $format );
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( $expectedText, $rev->getText( $audience ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider dataGetText
|
|
|
|
|
*/
|
|
|
|
|
function testGetRawText( $text, $title, $model, $format, $audience, $expectedText ) {
|
|
|
|
|
$rev = $this->newTestRevision( $text, $title, $model, $format );
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( $expectedText, $rev->getRawText( $audience ) );
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-02 10:54:27 +00:00
|
|
|
|
|
|
|
|
public function dataGetSize( ) {
|
|
|
|
|
return array(
|
|
|
|
|
array( "hello world.", null, 12 ),
|
2012-05-13 22:02:29 +00:00
|
|
|
array( serialize( "hello world." ), 999999, 12 ),
|
2012-05-02 10:54:27 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers Revision::getSize
|
|
|
|
|
* @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 array(
|
|
|
|
|
array( "hello world.", null, Revision::base36Sha1( "hello world." ) ),
|
2012-05-13 22:02:29 +00:00
|
|
|
array( serialize( "hello world." ), 999999, Revision::base36Sha1( serialize( "hello world." ) ) ),
|
2012-05-02 10:54:27 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers Revision::getSha1
|
|
|
|
|
* @dataProvider dataGetSha1
|
|
|
|
|
*/
|
|
|
|
|
public function testGetSha1( $text, $model, $expected_hash )
|
|
|
|
|
{
|
|
|
|
|
$rev = $this->newTestRevision( $text, 'RevisionTest_testGetSha1', $model );
|
|
|
|
|
$this->assertEquals( $expected_hash, $rev->getSha1() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testConstructWithText() {
|
|
|
|
|
$rev = new Revision( array(
|
|
|
|
|
'text' => 'hello world.',
|
|
|
|
|
'content_model' => CONTENT_MODEL_JAVASCRIPT
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
$this->assertNotNull( $rev->getText(), 'no content text' );
|
|
|
|
|
$this->assertNotNull( $rev->getContent(), 'no content object available' );
|
2012-05-13 22:02:29 +00:00
|
|
|
$this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContent()->getModel() );
|
|
|
|
|
$this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContentModel() );
|
2012-05-02 10:54:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testConstructWithContent() {
|
|
|
|
|
$title = Title::newFromText( 'RevisionTest_testConstructWithContent' );
|
|
|
|
|
|
|
|
|
|
$rev = new Revision( array(
|
|
|
|
|
'content' => ContentHandler::makeContent( 'hello world.', $title, CONTENT_MODEL_JAVASCRIPT ),
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
$this->assertNotNull( $rev->getText(), 'no content text' );
|
|
|
|
|
$this->assertNotNull( $rev->getContent(), 'no content object available' );
|
2012-05-13 22:02:29 +00:00
|
|
|
$this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContent()->getModel() );
|
|
|
|
|
$this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContentModel() );
|
2012-05-02 10:54:27 +00:00
|
|
|
}
|
2012-04-23 19:22:06 +00:00
|
|
|
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|