merge latest master.
[lhc/web/wiklou.git] / tests / phpunit / includes / RevisionTest.php
index ed7d919..3778914 100644 (file)
@@ -29,8 +29,9 @@ class RevisionTest extends MediaWikiTestCase {
                $wgExtraNamespaces[ 12312 ] = 'Dummy';
                $wgExtraNamespaces[ 12313 ] = 'Dummy_talk';
 
-               $wgNamespaceContentModels[ 12312 ] = 999999;
-               $wgContentHandlers[ 999999 ] = 'DummyContentHandlerForTesting';
+               $wgNamespaceContentModels[ 12312 ] = "testing";
+               $wgContentHandlers[ "testing" ] = 'DummyContentHandlerForTesting';
+               $wgContentHandlers[ "RevisionTestModifyableContent" ] = 'RevisionTestModifyableContentHandler';
 
                MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
                $wgContLang->resetNamespaces(); # reset namespace cache
@@ -186,11 +187,12 @@ class RevisionTest extends MediaWikiTestCase {
                return array(
                        array( 'hello world', 'Hello', null, null, CONTENT_MODEL_WIKITEXT ),
                        array( 'hello world', 'User:hello/there.css', null, null, CONTENT_MODEL_CSS ),
-                       array( serialize('hello world'), 'Dummy:Hello', null, null, 999999 ),
+                       array( serialize('hello world'), 'Dummy:Hello', null, null, "testing" ),
                );
        }
 
        /**
+        * @group Database
         * @dataProvider dataGetContentModel
         */
        function testGetContentModel( $text, $title, $model, $format, $expectedModel ) {
@@ -204,11 +206,12 @@ class RevisionTest extends MediaWikiTestCase {
                        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 ),
+                       array( serialize('hello world'), 'Dummy:Hello', null, null, "testing" ),
                );
        }
 
        /**
+        * @group Database
         * @dataProvider dataGetContentFormat
         */
        function testGetContentFormat( $text, $title, $model, $format, $expectedFormat ) {
@@ -226,6 +229,7 @@ class RevisionTest extends MediaWikiTestCase {
        }
 
        /**
+        * @group Database
         * @dataProvider dataGetContentHandler
         */
        function testGetContentHandler( $text, $title, $model, $format, $expectedClass ) {
@@ -237,12 +241,13 @@ class RevisionTest extends MediaWikiTestCase {
        function dataGetContent() {
                return array(
                        array( 'hello world', 'Hello', null, null, Revision::FOR_PUBLIC, 'hello world' ),
-                       array( serialize('hello world'), 'Hello', 999999, null, Revision::FOR_PUBLIC, serialize('hello world') ),
+                       array( serialize('hello world'), 'Hello', "testing", null, Revision::FOR_PUBLIC, serialize('hello world') ),
                        array( serialize('hello world'), 'Dummy:Hello', null, null, Revision::FOR_PUBLIC, serialize('hello world') ),
                );
        }
 
        /**
+        * @group Database
         * @dataProvider dataGetContent
         */
        function testGetContent( $text, $title, $model, $format, $audience, $expectedSerialization ) {
@@ -255,24 +260,30 @@ class RevisionTest extends MediaWikiTestCase {
        function dataGetText() {
                return array(
                        array( 'hello world', 'Hello', null, null, Revision::FOR_PUBLIC, 'hello world' ),
-                       array( serialize('hello world'), 'Hello', 999999, null, Revision::FOR_PUBLIC, null ),
+                       array( serialize('hello world'), 'Hello', "testing", null, Revision::FOR_PUBLIC, null ),
                        array( serialize('hello world'), 'Dummy:Hello', null, null, Revision::FOR_PUBLIC, null ),
                );
        }
 
        /**
+        * @group Database
         * @dataProvider dataGetText
         */
        function testGetText( $text, $title, $model, $format, $audience, $expectedText ) {
+               $this->hideDeprecated( 'Revision::getText' );
+
                $rev = $this->newTestRevision( $text, $title, $model, $format );
 
                $this->assertEquals( $expectedText, $rev->getText( $audience ) );
        }
 
        /**
+        * @group Database
         * @dataProvider dataGetText
         */
        function testGetRawText( $text, $title, $model, $format, $audience, $expectedText ) {
+               $this->hideDeprecated( 'Revision::getRawText' );
+
                $rev = $this->newTestRevision( $text, $title, $model, $format );
 
                $this->assertEquals( $expectedText, $rev->getRawText( $audience ) );
@@ -282,12 +293,13 @@ class RevisionTest extends MediaWikiTestCase {
        public function dataGetSize( ) {
                return array(
                        array( "hello world.", null, 12 ),
-                       array( serialize( "hello world." ), 999999, 12 ),
+                       array( serialize( "hello world." ), "testing", 12 ),
                );
        }
 
        /**
         * @covers Revision::getSize
+        * @group Database
         * @dataProvider dataGetSize
         */
        public function testGetSize( $text, $model, $expected_size )
@@ -299,12 +311,13 @@ class RevisionTest extends MediaWikiTestCase {
        public function dataGetSha1( ) {
                return array(
                        array( "hello world.", null, Revision::base36Sha1( "hello world." ) ),
-                       array( serialize( "hello world." ), 999999, Revision::base36Sha1( serialize( "hello world." ) ) ),
+                       array( serialize( "hello world." ), "testing", Revision::base36Sha1( serialize( "hello world." ) ) ),
                );
        }
 
        /**
         * @covers Revision::getSha1
+        * @group Database
         * @dataProvider dataGetSha1
         */
        public function testGetSha1( $text, $model, $expected_hash )
@@ -314,6 +327,8 @@ class RevisionTest extends MediaWikiTestCase {
        }
 
        public function testConstructWithText() {
+               $this->hideDeprecated( "Revision::getText" );
+
                $rev = new Revision( array(
                                          'text' => 'hello world.',
                                          'content_model' => CONTENT_MODEL_JAVASCRIPT
@@ -326,6 +341,8 @@ class RevisionTest extends MediaWikiTestCase {
        }
 
        public function testConstructWithContent() {
+               $this->hideDeprecated( "Revision::getText" );
+
                $title = Title::newFromText( 'RevisionTest_testConstructWithContent' );
 
                $rev = new Revision( array(
@@ -338,6 +355,87 @@ class RevisionTest extends MediaWikiTestCase {
                $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContentModel() );
        }
 
+       /**
+        * Tests whether $rev->getContent() returns a clone when needed.
+        *
+        * @group Database
+        */
+       function testGetContentClone( ) {
+               $content = new RevisionTestModifyableContent( "foo" );
+
+               $rev = new Revision(
+                       array(
+                               'id'         => 42,
+                               'page'       => 23,
+                               'title'      => Title::newFromText( "testGetContentClone_dummy" ),
+
+                               'content'    => $content,
+                               'length'     => $content->getSize(),
+                               'comment'    => "testing",
+                               'minor_edit' => false,
+                       )
+               );
+
+               $content = $rev->getContent( Revision::RAW );
+               $content->setText( "bar" );
+
+               $content2 = $rev->getContent( Revision::RAW );
+               $this->assertNotSame( $content, $content2, "expected a clone" ); // content is mutable, expect clone
+               $this->assertEquals( "foo", $content2->getText() ); // clone should contain the original text
+
+               $content2->setText( "bla bla" );
+               $this->assertEquals( "bar", $content->getText() ); // clones should be independent
+       }
+
+
+       /**
+        * Tests whether $rev->getContent() returns the same object repeatedly if appropriate.
+        *
+        * @group Database
+        */
+       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 );
+       }
+
 }
 
+class RevisionTestModifyableContent extends TextContent {
+       public function __construct( $text ) {
+               parent::__construct( $text, "RevisionTestModifyableContent" );
+       }
+
+       public function copy( ) {
+               return new RevisionTestModifyableContent( $this->mText );
+       }
 
+       public function getText() {
+               return $this->mText;
+       }
+
+       public function setText( $text ) {
+               $this->mText = $text;
+       }
+
+}
+
+class RevisionTestModifyableContentHandler extends TextContentHandler {
+
+       public function __construct( ) {
+               parent::__construct( "RevisionTestModifyableContent", array( CONTENT_FORMAT_TEXT ) );
+       }
+
+       public function unserializeContent( $text, $format = null ) {
+               $this->checkFormat( $format );
+
+               return new RevisionTestModifyableContent( $text );
+       }
+
+       public function makeEmptyContent() {
+               return new RevisionTestModifyableContent( '' );
+       }
+}