Revision: test and fix __construct exceptions
[lhc/web/wiklou.git] / tests / phpunit / includes / RevisionTest.php
index eb17588..39f7e5c 100644 (file)
  * @group ContentHandler
  */
 class RevisionTest extends MediaWikiTestCase {
+
        protected function setUp() {
                global $wgContLang;
 
                parent::setUp();
 
-               $this->setMwGlobals( array(
+               $this->setMwGlobals( [
                        'wgContLang' => Language::factory( 'en' ),
                        'wgLanguageCode' => 'en',
                        'wgLegacyEncoding' => false,
                        'wgCompressRevisions' => false,
 
                        'wgContentHandlerTextFallback' => 'ignore',
-               ) );
+               ] );
 
                $this->mergeMwGlobalArrayValue(
                        'wgExtraNamespaces',
-                       array(
+                       [
                                12312 => 'Dummy',
                                12313 => 'Dummy_talk',
-                       )
+                       ]
                );
 
                $this->mergeMwGlobalArrayValue(
                        'wgNamespaceContentModels',
-                       array(
+                       [
                                12312 => 'testing',
-                       )
+                       ]
                );
 
                $this->mergeMwGlobalArrayValue(
                        'wgContentHandlers',
-                       array(
+                       [
                                'testing' => 'DummyContentHandlerForTesting',
                                'RevisionTestModifyableContent' => 'RevisionTestModifyableContentHandler',
-                       )
+                       ]
                );
 
-               MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
-               $wgContLang->resetNamespaces(); # reset namespace cache
+               MWNamespace::clearCaches();
+               // Reset namespace cache
+               $wgContLang->resetNamespaces();
        }
 
-       function tearDown() {
+       protected function tearDown() {
                global $wgContLang;
 
-               MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
-               $wgContLang->resetNamespaces(); # reset namespace cache
+               MWNamespace::clearCaches();
+               // Reset namespace cache
+               $wgContLang->resetNamespaces();
 
                parent::tearDown();
        }
 
+       public function provideConstruct() {
+               yield 'with text' => [
+                       [
+                               'text' => 'hello world.',
+                               'content_model' => CONTENT_MODEL_JAVASCRIPT
+                       ],
+               ];
+               yield 'with content' => [
+                       [
+                               'content' => new JavaScriptContent( 'hellow world.' )
+                       ],
+               ];
+       }
+
        /**
-        * @covers Revision::getRevisionText
+        * @dataProvider provideConstruct
         */
-       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 ) );
+       public function testConstruct( $rowArray ) {
+               $rev = new Revision( $rowArray );
+               $this->assertNotNull( $rev->getContent(), 'no content object available' );
+               $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContent()->getModel() );
+               $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContentModel() );
+       }
+
+       public function provideConstructThrowsExceptions() {
+               yield 'content and text_id both not empty' => [
+                       [
+                               'content' => new WikitextContent( 'GOAT' ),
+                               'text_id' => 'someid',
+                               ],
+                       new MWException( "Text already stored in external store (id someid), " .
+                               "can't serialize content object" )
+               ];
+               yield 'with bad content object (class)' => [
+                       [ 'content' => new stdClass() ],
+                       new MWException( '`content` field must contain a Content object.' )
+               ];
+               yield 'with bad content object (string)' => [
+                       [ 'content' => 'ImAGoat' ],
+                       new MWException( '`content` field must contain a Content object.' )
+               ];
+               yield 'bad row format' => [
+                       'imastring, not a row',
+                       new MWException( 'Revision constructor passed invalid row format.' )
+               ];
        }
 
        /**
-        * @covers Revision::getRevisionText
+        * @dataProvider provideConstructThrowsExceptions
         */
-       public function testGetRevisionTextGzip() {
-               $this->checkPHPExtension( 'zlib' );
+       public function testConstructThrowsExceptions( $rowArray, Exception $expectedException ) {
+               $this->setExpectedException(
+                       get_class( $expectedException ),
+                       $expectedException->getMessage(),
+                       $expectedException->getCode()
+               );
+               new Revision( $rowArray );
+       }
 
-               $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 ) );
+       public function provideGetRevisionText() {
+               yield 'Generic test' => [
+                       'This is a goat of revision text.',
+                       [
+                               'old_flags' => '',
+                               'old_text' => 'This is a goat of revision text.',
+                       ],
+               ];
        }
 
        /**
         * @covers Revision::getRevisionText
+        * @dataProvider provideGetRevisionText
         */
-       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';
+       public function testGetRevisionText( $expected, $rowData, $prefix = 'old_', $wiki = false ) {
                $this->assertEquals(
-                       "Wiki est l'\xc3\xa9cole superieur !",
-                       Revision::getRevisionText( $row ) );
+                       $expected,
+                       Revision::getRevisionText( (object)$rowData, $prefix, $wiki ) );
+       }
+
+       public function provideGetRevisionTextWithZlibExtension() {
+               yield 'Generic gzip test' => [
+                       'This is a small goat of revision text.',
+                       [
+                               'old_flags' => 'gzip',
+                               'old_text' => gzdeflate( 'This is a small goat of revision text.' ),
+                       ],
+               ];
        }
 
        /**
         * @covers Revision::getRevisionText
+        * @dataProvider provideGetRevisionTextWithZlibExtension
         */
-       public function testGetRevisionTextUtf8Legacy() {
-               $row = new stdClass;
-               $row->old_flags = '';
-               $row->old_text = "Wiki est l'\xe9cole superieur !";
-               $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1';
-               $this->assertEquals(
+       public function testGetRevisionWithZlibExtension( $expected, $rowData ) {
+               $this->checkPHPExtension( 'zlib' );
+               $this->testGetRevisionText( $expected, $rowData );
+       }
+
+       public function provideGetRevisionTextWithLegacyEncoding() {
+               yield 'Utf8Native' => [
+                       "Wiki est l'\xc3\xa9cole superieur !",
+                       'iso-8859-1',
+                       [
+                               'old_flags' => 'utf-8',
+                               'old_text' => "Wiki est l'\xc3\xa9cole superieur !",
+                       ]
+               ];
+               yield 'Utf8Legacy' => [
                        "Wiki est l'\xc3\xa9cole superieur !",
-                       Revision::getRevisionText( $row ) );
+                       'iso-8859-1',
+                       [
+                               'old_flags' => '',
+                               'old_text' => "Wiki est l'\xe9cole superieur !",
+                       ]
+               ];
        }
 
        /**
         * @covers Revision::getRevisionText
+        * @dataProvider provideGetRevisionTextWithLegacyEncoding
         */
-       public function testGetRevisionTextUtf8NativeGzip() {
-               $this->checkPHPExtension( 'zlib' );
+       public function testGetRevisionWithLegacyEncoding( $expected, $encoding, $rowData ) {
+               $GLOBALS['wgLegacyEncoding'] = $encoding;
+               $this->testGetRevisionText( $expected, $rowData );
+       }
 
-               $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(
+       public function provideGetRevisionTextWithGzipAndLegacyEncoding() {
+               yield 'Utf8NativeGzip' => [
+                       "Wiki est l'\xc3\xa9cole superieur !",
+                       'iso-8859-1',
+                       [
+                               'old_flags' => 'gzip,utf-8',
+                               'old_text' => gzdeflate( "Wiki est l'\xc3\xa9cole superieur !" ),
+                       ]
+               ];
+               yield 'Utf8LegacyGzip' => [
                        "Wiki est l'\xc3\xa9cole superieur !",
-                       Revision::getRevisionText( $row ) );
+                       'iso-8859-1',
+                       [
+                               'old_flags' => 'gzip',
+                               'old_text' => gzdeflate( "Wiki est l'\xe9cole superieur !" ),
+                       ]
+               ];
        }
 
        /**
         * @covers Revision::getRevisionText
+        * @dataProvider provideGetRevisionTextWithGzipAndLegacyEncoding
         */
-       public function testGetRevisionTextUtf8LegacyGzip() {
+       public function testGetRevisionWithGzipAndLegacyEncoding( $expected, $encoding, $rowData ) {
                $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 ) );
+               $GLOBALS['wgLegacyEncoding'] = $encoding;
+               $this->testGetRevisionText( $expected, $rowData );
        }
 
        /**
@@ -173,8 +249,6 @@ class RevisionTest extends MediaWikiTestCase {
                        Revision::getRevisionText( $row ), "getRevisionText" );
        }
 
-       # =========================================================================
-
        /**
         * @param string $text
         * @param string $title
@@ -183,7 +257,7 @@ class RevisionTest extends MediaWikiTestCase {
         *
         * @return Revision
         */
-       function newTestRevision( $text, $title = "Test",
+       private function newTestRevision( $text, $title = "Test",
                $model = CONTENT_MODEL_WIKITEXT, $format = null
        ) {
                if ( is_string( $title ) ) {
@@ -193,7 +267,7 @@ class RevisionTest extends MediaWikiTestCase {
                $content = ContentHandler::makeContent( $text, $title, $model, $format );
 
                $rev = new Revision(
-                       array(
+                       [
                                'id' => 42,
                                'page' => 23,
                                'title' => $title,
@@ -204,24 +278,24 @@ class RevisionTest extends MediaWikiTestCase {
                                'minor_edit' => false,
 
                                'content_format' => $format,
-                       )
+                       ]
                );
 
                return $rev;
        }
 
-       function dataGetContentModel() {
+       public function provideGetContentModel() {
                // NOTE: we expect the help namespace to always contain wikitext
-               return array(
-                       array( 'hello world', 'Help: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, "testing" ),
-               );
+               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
+        * @dataProvider provideGetContentModel
         * @covers Revision::getContentModel
         */
        public function testGetContentModel( $text, $title, $model, $format, $expectedModel ) {
@@ -230,19 +304,19 @@ class RevisionTest extends MediaWikiTestCase {
                $this->assertEquals( $expectedModel, $rev->getContentModel() );
        }
 
-       function dataGetContentFormat() {
+       public function provideGetContentFormat() {
                // NOTE: we expect the help namespace to always contain wikitext
-               return array(
-                       array( 'hello world', 'Help:Hello', null, null, CONTENT_FORMAT_WIKITEXT ),
-                       array( 'hello world', 'Help: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, "testing" ),
-               );
+               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
+        * @dataProvider provideGetContentFormat
         * @covers Revision::getContentFormat
         */
        public function testGetContentFormat( $text, $title, $model, $format, $expectedFormat ) {
@@ -251,18 +325,18 @@ class RevisionTest extends MediaWikiTestCase {
                $this->assertEquals( $expectedFormat, $rev->getContentFormat() );
        }
 
-       function dataGetContentHandler() {
+       public function provideGetContentHandler() {
                // NOTE: we expect the help namespace to always contain wikitext
-               return array(
-                       array( 'hello world', 'Help:Hello', null, null, 'WikitextContentHandler' ),
-                       array( 'hello world', 'User:hello/there.css', null, null, 'CssContentHandler' ),
-                       array( serialize( 'hello world' ), 'Dummy:Hello', null, null, 'DummyContentHandlerForTesting' ),
-               );
+               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
+        * @dataProvider provideGetContentHandler
         * @covers Revision::getContentHandler
         */
        public function testGetContentHandler( $text, $title, $model, $format, $expectedClass ) {
@@ -271,32 +345,32 @@ class RevisionTest extends MediaWikiTestCase {
                $this->assertEquals( $expectedClass, get_class( $rev->getContentHandler() ) );
        }
 
-       function dataGetContent() {
+       public function provideGetContent() {
                // NOTE: we expect the help namespace to always contain wikitext
-               return array(
-                       array( 'hello world', 'Help:Hello', null, null, Revision::FOR_PUBLIC, 'hello world' ),
-                       array(
+               return [
+                       [ 'hello world', 'Help:Hello', null, null, Revision::FOR_PUBLIC, 'hello world' ],
+                       [
                                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
+        * @dataProvider provideGetContent
         * @covers Revision::getContent
         */
        public function testGetContent( $text, $title, $model, $format,
@@ -311,114 +385,44 @@ class RevisionTest extends MediaWikiTestCase {
                );
        }
 
-       function dataGetText() {
-               // NOTE: we expect the help namespace to always contain wikitext
-               return array(
-                       array( 'hello world', 'Help:Hello', null, null, Revision::FOR_PUBLIC, 'hello world' ),
-                       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
-        * @covers Revision::getText
-        */
-       public 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
-        * @covers Revision::getRawText
-        */
-       public 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 ) );
-       }
-
-       public function dataGetSize() {
-               return array(
-                       array( "hello world.", CONTENT_MODEL_WIKITEXT, 12 ),
-                       array( serialize( "hello world." ), "testing", 12 ),
-               );
+       public function provideGetSize() {
+               return [
+                       [ "hello world.", CONTENT_MODEL_WIKITEXT, 12 ],
+                       [ serialize( "hello world." ), "testing", 12 ],
+               ];
        }
 
        /**
         * @covers Revision::getSize
         * @group Database
-        * @dataProvider dataGetSize
+        * @dataProvider provideGetSize
         */
        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.", CONTENT_MODEL_WIKITEXT, Revision::base36Sha1( "hello world." ) ),
-                       array(
+       public function provideGetSha1() {
+               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
+        * @dataProvider provideGetSha1
         */
        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() {
-               $this->hideDeprecated( "Revision::getText" );
-
-               $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' );
-               $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContent()->getModel() );
-               $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContentModel() );
-       }
-
-       /**
-        * @covers Revision::__construct
-        */
-       public function testConstructWithContent() {
-               $this->hideDeprecated( "Revision::getText" );
-
-               $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' );
-               $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContent()->getModel() );
-               $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContentModel() );
-       }
-
        /**
         * Tests whether $rev->getContent() returns a clone when needed.
         *
@@ -429,7 +433,7 @@ class RevisionTest extends MediaWikiTestCase {
                $content = new RevisionTestModifyableContent( "foo" );
 
                $rev = new Revision(
-                       array(
+                       [
                                'id' => 42,
                                'page' => 23,
                                'title' => Title::newFromText( "testGetContentClone_dummy" ),
@@ -438,12 +442,14 @@ class RevisionTest extends MediaWikiTestCase {
                                '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" );
@@ -451,7 +457,8 @@ class RevisionTest extends MediaWikiTestCase {
                $this->assertEquals( "foo", $content2->getText() );
 
                $content2->setText( "bla bla" );
-               $this->assertEquals( "bar", $content->getText() ); // clones should be independent
+               // clones should be independent
+               $this->assertEquals( "bar", $content->getText() );
        }
 
        /**
@@ -469,38 +476,3 @@ class RevisionTest extends MediaWikiTestCase {
                $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( '' );
-       }
-}