Deprecate Content::getNativeData, add TextContent::getText
authordaniel <dkinzler@wikimedia.org>
Thu, 8 Nov 2018 15:19:23 +0000 (16:19 +0100)
committerJames D. Forrester <jforrester@wikimedia.org>
Wed, 16 Jan 2019 19:57:50 +0000 (11:57 -0800)
getNativeData() is under-specified - callers can do nothing with the
value returned by getNativeData without knowing the concrete Content
class. And if they know the concrete class, they can and should use
a specialized getter instead, anyway.

Basically, getNativeData is overly generic, an example of polymorphism
done poorly. Let's fix it now.

Bug: T155582
Change-Id: Id2c61dcd38ab30416a25746e3680edb8791ae8e8

18 files changed:
RELEASE-NOTES-1.33
includes/content/AbstractContent.php
includes/content/Content.php
includes/content/ContentHandler.php
includes/content/CssContent.php
includes/content/JavaScriptContent.php
includes/content/JsonContent.php
includes/content/MessageContent.php
includes/content/TextContent.php
includes/content/TextContentHandler.php
includes/content/WikitextContent.php
includes/content/WikitextContentHandler.php
tests/phpunit/includes/content/ContentHandlerTest.php
tests/phpunit/includes/content/JavaScriptContentTest.php
tests/phpunit/includes/content/MessageContentTest.php [new file with mode: 0644]
tests/phpunit/includes/content/TextContentTest.php
tests/phpunit/includes/content/WikitextContentHandlerTest.php
tests/phpunit/includes/content/WikitextContentTest.php

index 5175667..c9bd093 100644 (file)
@@ -39,6 +39,8 @@ production.
 * Add PasswordPolicy to check the password isn't in the large blacklist.
 * The AuthManagerLoginAuthenticateAudit hook has a new parameter for
   additional information about the authentication event.
+* TextContent::getText() was introduced as a replacement for
+  Content::getNativeData() for text-based content models.
 * …
 
 === External library changes in 1.33 ===
@@ -205,6 +207,8 @@ because of Phabricator reports.
   requiresUnblock() returns the proper result (the default is `true`).
 * (T211608) The MediaWiki\Services namespace has been renamed to
   Wikimedia\Services. The old name is still supported, but deprecated.
+* (T155582) Content::getNativeData has been deprecated. Please use model-
+  specific getters, such as TextContent::getText().
 * …
 
 === Other changes in 1.33 ===
index 733d85a..c82b473 100644 (file)
@@ -180,6 +180,17 @@ abstract class AbstractContent implements Content {
        }
 
        /**
+        * Decides whether two Content objects are equal.
+        * Two Content objects MUST not be considered equal if they do not share the same content model.
+        * Two Content objects that are equal SHOULD have the same serialization.
+        *
+        * This default implementation relies on equalsInternal() to determin whether the
+        * Content objects are logically equivalent. Subclasses that need to implement a custom
+        * equality check should consider overriding equalsInternal(). Subclasses that override
+        * equals() itself MUST make sure that the implementation returns false for $that === null,
+        * and true for $that === this. It MUST also return false if $that does not have the same
+        * content model.
+        *
         * @since 1.21
         *
         * @param Content|null $that
@@ -201,7 +212,34 @@ abstract class AbstractContent implements Content {
                        return false;
                }
 
-               return $this->getNativeData() === $that->getNativeData();
+               // For type safety. Needed for odd cases like MessageContent using CONTENT_MODEL_WIKITEXT
+               if ( get_class( $that ) !== get_class( $this ) ) {
+                       return false;
+               }
+
+               return $this->equalsInternal( $that );
+       }
+
+       /**
+        * Checks whether $that is logically equal to this Content object.
+        *
+        * This method can be overwritten by subclasses that need to implement custom
+        * equality checks.
+        *
+        * This default implementation checks whether the serializations
+        * of $this and $that are the same: $this->serialize() === $that->serialize()
+        *
+        * Implementors can assume that $that is an instance of the same class
+        * as the present Content object, as long as equalsInternal() is only called
+        * by the standard implementation of equals().
+        *
+        * @note Do not call this method directly, call equals() instead.
+        *
+        * @param Content $that
+        * @return bool
+        */
+       protected function equalsInternal( Content $that ) {
+               return $this->serialize() === $that->serialize();
        }
 
        /**
index 1bb43f8..2637aa6 100644 (file)
@@ -77,6 +77,9 @@ interface Content {
         *
         * @since 1.21
         *
+        * @deprecated since 1.33 use getText() for TextContent instances.
+        *             For other content models, use specialized getters.
+        *
         * @return mixed The native representation of the content. Could be a
         *    string, a nested array structure, an object, a binary blob...
         *    anything, really.
@@ -199,9 +202,11 @@ interface Content {
         *
         * - Will return false if $that is null.
         * - Will return true if $that === $this.
-        * - Will return false if $that->getModel() != $this->getModel().
-        * - Will return false if $that->getNativeData() is not equal to $this->getNativeData(),
-        *   where the meaning of "equal" depends on the actual data model.
+        * - Will return false if $that->getModel() !== $this->getModel().
+        * - Will return false if get_class( $that ) !== get_class( $this )
+        * - Should return false if $that->getModel() == $this->getModel() and
+        *     $that is not semantically equivalent to $this, according to
+        *     the data model defined by $this->getModel().
         *
         * Implementations should be careful to make equals() transitive and reflexive:
         *
index 5c18a33..ae47b86 100644 (file)
@@ -88,7 +88,7 @@ abstract class ContentHandler {
                }
 
                if ( $content instanceof TextContent ) {
-                       return $content->getNativeData();
+                       return $content->getText();
                }
 
                wfDebugLog( 'ContentHandler', 'Accessing ' . $content->getModel() . ' content as text!' );
index a09cd15..d32fa88 100644 (file)
@@ -61,7 +61,7 @@ class CssContent extends TextContent {
                global $wgParser;
                // @todo Make pre-save transformation optional for script pages
 
-               $text = $this->getNativeData();
+               $text = $this->getText();
                $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts );
 
                return new static( $pst );
@@ -73,7 +73,7 @@ class CssContent extends TextContent {
        protected function getHtml() {
                $html = "";
                $html .= "<pre class=\"mw-code mw-css\" dir=\"ltr\">\n";
-               $html .= htmlspecialchars( $this->getNativeData() );
+               $html .= htmlspecialchars( $this->getText() );
                $html .= "\n</pre>\n";
 
                return $html;
@@ -99,7 +99,7 @@ class CssContent extends TextContent {
                        return $this->redirectTarget;
                }
                $this->redirectTarget = null;
-               $text = $this->getNativeData();
+               $text = $this->getText();
                if ( strpos( $text, '/* #REDIRECT */' ) === 0 ) {
                        // Extract the title from the url
                        preg_match( '/title=(.*?)&action=raw/', $text, $matches );
index 69ed8d4..e637798 100644 (file)
@@ -60,7 +60,7 @@ class JavaScriptContent extends TextContent {
                // @todo Make pre-save transformation optional for script pages
                // See T34858
 
-               $text = $this->getNativeData();
+               $text = $this->getText();
                $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts );
 
                return new static( $pst );
@@ -72,7 +72,7 @@ class JavaScriptContent extends TextContent {
        protected function getHtml() {
                $html = "";
                $html .= "<pre class=\"mw-code mw-js\" dir=\"ltr\">\n";
-               $html .= htmlspecialchars( $this->getNativeData() );
+               $html .= htmlspecialchars( $this->getText() );
                $html .= "\n</pre>\n";
 
                return $html;
@@ -101,7 +101,7 @@ class JavaScriptContent extends TextContent {
                        return $this->redirectTarget;
                }
                $this->redirectTarget = null;
-               $text = $this->getNativeData();
+               $text = $this->getText();
                if ( strpos( $text, '/* #REDIRECT */' ) === 0 ) {
                        // Extract the title from the url
                        preg_match( '/title=(.*?)\\\\u0026action=raw/', $text, $matches );
index 7d8f67c..0f8a9a9 100644 (file)
@@ -36,7 +36,7 @@ class JsonContent extends TextContent {
         */
        public function getJsonData() {
                wfDeprecated( __METHOD__, '1.25' );
-               return FormatJson::decode( $this->getNativeData(), true );
+               return FormatJson::decode( $this->getText(), true );
        }
 
        /**
@@ -49,7 +49,7 @@ class JsonContent extends TextContent {
         */
        public function getData() {
                if ( $this->jsonParse === null ) {
-                       $this->jsonParse = FormatJson::parse( $this->getNativeData() );
+                       $this->jsonParse = FormatJson::parse( $this->getText() );
                }
                return $this->jsonParse;
        }
index b21c6f4..5626b54 100644 (file)
@@ -80,9 +80,22 @@ class MessageContent extends AbstractContent {
        /**
         * Returns the message object, with any parameters already substituted.
         *
+        * @deprecated since 1.33 use getMessage() instead.
+        *
         * @return Message The message object.
         */
        public function getNativeData() {
+               return $this->getMessage();
+       }
+
+       /**
+        * Returns the message object, with any parameters already substituted.
+        *
+        * @since 1.33
+        *
+        * @return Message The message object.
+        */
+       public function getMessage() {
                // NOTE: Message objects are mutable. Cloning here makes MessageContent immutable.
                return clone $this->mMessage;
        }
@@ -131,7 +144,8 @@ class MessageContent extends AbstractContent {
         * @see Content::copy
         */
        public function copy() {
-               // MessageContent is immutable (because getNativeData() returns a clone of the Message object)
+               // MessageContent is immutable (because getNativeData() and getMessage()
+               //   returns a clone of the Message object)
                return $this;
        }
 
index 0198a0d..750b958 100644 (file)
@@ -73,7 +73,7 @@ class TextContent extends AbstractContent {
        }
 
        public function getTextForSummary( $maxlength = 250 ) {
-               $text = $this->getNativeData();
+               $text = $this->getText();
 
                $truncatedtext = MediaWikiServices::getInstance()->getContentLanguage()->
                        truncateForDatabase( preg_replace( "/[\n\r]/", ' ', $text ), max( 0, $maxlength ) );
@@ -87,7 +87,7 @@ class TextContent extends AbstractContent {
         * @return int
         */
        public function getSize() {
-               $text = $this->getNativeData();
+               $text = $this->getText();
 
                return strlen( $text );
        }
@@ -118,9 +118,22 @@ class TextContent extends AbstractContent {
        /**
         * Returns the text represented by this Content object, as a string.
         *
-        * @return string The raw text.
+        * @deprecated since 1.33 use getText() instead.
+        *
+        * @return string The raw text. Subclasses may guarantee a specific syntax here.
         */
        public function getNativeData() {
+               return $this->getText();
+       }
+
+       /**
+        * Returns the text represented by this Content object, as a string.
+        *
+        * @since 1.33
+        *
+        * @return string The raw text.
+        */
+       public function getText() {
                return $this->mText;
        }
 
@@ -130,7 +143,7 @@ class TextContent extends AbstractContent {
         * @return string The raw text.
         */
        public function getTextForSearchIndex() {
-               return $this->getNativeData();
+               return $this->getText();
        }
 
        /**
@@ -145,7 +158,7 @@ class TextContent extends AbstractContent {
                $wikitext = $this->convert( CONTENT_MODEL_WIKITEXT, 'lossy' );
 
                if ( $wikitext ) {
-                       return $wikitext->getNativeData();
+                       return $wikitext->getText();
                } else {
                        return false;
                }
@@ -181,7 +194,7 @@ class TextContent extends AbstractContent {
         * @return Content
         */
        public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
-               $text = $this->getNativeData();
+               $text = $this->getText();
                $pst = self::normalizeLineEndings( $text );
 
                return ( $text === $pst ) ? $this : new static( $pst, $this->getModel() );
@@ -208,8 +221,8 @@ class TextContent extends AbstractContent {
                        $lang = MediaWikiServices::getInstance()->getContentLanguage();
                }
 
-               $otext = $this->getNativeData();
-               $ntext = $that->getNativeData();
+               $otext = $this->getText();
+               $ntext = $that->getText();
 
                # Note: Use native PHP diff, external engines don't give us abstract output
                $ota = explode( "\n", $lang->segmentForDiff( $otext ) );
@@ -244,7 +257,7 @@ class TextContent extends AbstractContent {
 
                if ( in_array( $this->getModel(), $wgTextModelsToParse ) ) {
                        // parse just to get links etc into the database, HTML is replaced below.
-                       $output = $wgParser->parse( $this->getNativeData(), $title, $options, true, true, $revId );
+                       $output = $wgParser->parse( $this->getText(), $title, $options, true, true, $revId );
                }
 
                if ( $generateHtml ) {
@@ -291,7 +304,7 @@ class TextContent extends AbstractContent {
         * @return string An HTML representation of the content
         */
        protected function getHighlightHtml() {
-               return htmlspecialchars( $this->getNativeData() );
+               return htmlspecialchars( $this->getText() );
        }
 
        /**
@@ -318,7 +331,7 @@ class TextContent extends AbstractContent {
 
                if ( $toHandler instanceof TextContentHandler ) {
                        // NOTE: ignore content serialization format - it's just text anyway.
-                       $text = $this->getNativeData();
+                       $text = $this->getText();
                        $converted = $toHandler->unserializeContent( $text );
                }
 
index 0978ffc..e3dc187 100644 (file)
@@ -45,7 +45,7 @@ class TextContentHandler extends ContentHandler {
        public function serializeContent( Content $content, $format = null ) {
                $this->checkFormat( $format );
 
-               return $content->getNativeData();
+               return $content->getText();
        }
 
        /**
index 517d807..3e2313c 100644 (file)
@@ -61,7 +61,7 @@ class WikitextContent extends TextContent {
        public function getSection( $sectionId ) {
                global $wgParser;
 
-               $text = $this->getNativeData();
+               $text = $this->getText();
                $sect = $wgParser->getSection( $text, $sectionId, false );
 
                if ( $sect === false ) {
@@ -91,8 +91,8 @@ class WikitextContent extends TextContent {
                                "section uses $sectionModelId." );
                }
 
-               $oldtext = $this->getNativeData();
-               $text = $with->getNativeData();
+               $oldtext = $this->getText();
+               $text = $with->getText();
 
                if ( strval( $sectionId ) === '' ) {
                        return $with; # XXX: copy first?
@@ -131,7 +131,7 @@ class WikitextContent extends TextContent {
                $text = wfMessage( 'newsectionheaderdefaultlevel' )
                        ->rawParams( $header )->inContentLanguage()->text();
                $text .= "\n\n";
-               $text .= $this->getNativeData();
+               $text .= $this->getText();
 
                return new static( $text );
        }
@@ -149,7 +149,7 @@ class WikitextContent extends TextContent {
        public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
                global $wgParser;
 
-               $text = $this->getNativeData();
+               $text = $this->getText();
                $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts );
 
                if ( $text === $pst ) {
@@ -178,7 +178,7 @@ class WikitextContent extends TextContent {
        public function preloadTransform( Title $title, ParserOptions $popts, $params = [] ) {
                global $wgParser;
 
-               $text = $this->getNativeData();
+               $text = $this->getText();
                $plt = $wgParser->getPreloadText( $text, $title, $popts, $params );
 
                return new static( $plt );
@@ -202,12 +202,12 @@ class WikitextContent extends TextContent {
 
                if ( $wgMaxRedirects < 1 ) {
                        // redirects are disabled, so quit early
-                       $this->redirectTargetAndText = [ null, $this->getNativeData() ];
+                       $this->redirectTargetAndText = [ null, $this->getText() ];
                        return $this->redirectTargetAndText;
                }
 
                $redir = MediaWikiServices::getInstance()->getMagicWordFactory()->get( 'redirect' );
-               $text = ltrim( $this->getNativeData() );
+               $text = ltrim( $this->getText() );
                if ( $redir->matchStartAndRemove( $text ) ) {
                        // Extract the first link and see if it's usable
                        // Ensure that it really does come directly after #REDIRECT
@@ -223,7 +223,7 @@ class WikitextContent extends TextContent {
                                $title = Title::newFromText( $m[1] );
                                // If the title is a redirect to bad special pages or is invalid, return null
                                if ( !$title instanceof Title || !$title->isValidRedirectTarget() ) {
-                                       $this->redirectTargetAndText = [ null, $this->getNativeData() ];
+                                       $this->redirectTargetAndText = [ null, $this->getText() ];
                                        return $this->redirectTargetAndText;
                                }
 
@@ -232,7 +232,7 @@ class WikitextContent extends TextContent {
                        }
                }
 
-               $this->redirectTargetAndText = [ null, $this->getNativeData() ];
+               $this->redirectTargetAndText = [ null, $this->getText() ];
                return $this->redirectTargetAndText;
        }
 
@@ -271,7 +271,7 @@ class WikitextContent extends TextContent {
                # so the regex has to be fairly general
                $newText = preg_replace( '/ \[ \[  [^\]]*  \] \] /x',
                        '[[' . $target->getFullText() . ']]',
-                       $this->getNativeData(), 1 );
+                       $this->getText(), 1 );
 
                return new static( $newText );
        }
@@ -408,7 +408,7 @@ class WikitextContent extends TextContent {
         * @see Content::matchMagicWord()
         */
        public function matchMagicWord( MagicWord $word ) {
-               return $word->match( $this->getNativeData() );
+               return $word->match( $this->getText() );
        }
 
 }
index ab157f5..191c718 100644 (file)
@@ -162,4 +162,24 @@ class WikitextContentHandler extends TextContentHandler {
                return $fields;
        }
 
+       /**
+        * Returns the content's text as-is.
+        *
+        * @param Content $content
+        * @param string|null $format The serialization format to check
+        *
+        * @return mixed
+        */
+       public function serializeContent( Content $content, $format = null ) {
+               $this->checkFormat( $format );
+
+               // NOTE: MessageContent also uses CONTENT_MODEL_WIKITEXT, but it's not a TextContent!
+               // Perhaps MessageContent should use a separate ContentHandler instead.
+               if ( $content instanceof MessageContent ) {
+                       return $content->getMessage()->plain();
+               }
+
+               return parent::serializeContent( $content, $format );
+       }
+
 }
index 43edf60..a8ea3f0 100644 (file)
@@ -200,7 +200,7 @@ class ContentHandlerTest extends MediaWikiTestCase {
                $content = new WikitextContent( "hello world" );
 
                $text = ContentHandler::getContentText( $content );
-               $this->assertEquals( $content->getNativeData(), $text );
+               $this->assertEquals( $content->getText(), $text );
        }
 
        /**
@@ -242,9 +242,9 @@ class ContentHandlerTest extends MediaWikiTestCase {
 
        public static function dataMakeContent() {
                return [
-                       [ 'hallo', 'Help:Test', null, null, CONTENT_MODEL_WIKITEXT, 'hallo', false ],
-                       [ 'hallo', 'MediaWiki:Test.js', null, null, CONTENT_MODEL_JAVASCRIPT, 'hallo', false ],
-                       [ serialize( 'hallo' ), 'Dummy:Test', null, null, "testing", 'hallo', false ],
+                       [ 'hallo', 'Help:Test', null, null, CONTENT_MODEL_WIKITEXT, false ],
+                       [ 'hallo', 'MediaWiki:Test.js', null, null, CONTENT_MODEL_JAVASCRIPT, false ],
+                       [ serialize( 'hallo' ), 'Dummy:Test', null, null, "testing", false ],
 
                        [
                                'hallo',
@@ -252,7 +252,6 @@ class ContentHandlerTest extends MediaWikiTestCase {
                                null,
                                CONTENT_FORMAT_WIKITEXT,
                                CONTENT_MODEL_WIKITEXT,
-                               'hallo',
                                false
                        ],
                        [
@@ -261,19 +260,17 @@ class ContentHandlerTest extends MediaWikiTestCase {
                                null,
                                CONTENT_FORMAT_JAVASCRIPT,
                                CONTENT_MODEL_JAVASCRIPT,
-                               'hallo',
                                false
                        ],
-                       [ serialize( 'hallo' ), 'Dummy:Test', null, "testing", "testing", 'hallo', false ],
+                       [ serialize( 'hallo' ), 'Dummy:Test', null, "testing", "testing", false ],
 
-                       [ 'hallo', 'Help:Test', CONTENT_MODEL_CSS, null, CONTENT_MODEL_CSS, 'hallo', false ],
+                       [ 'hallo', 'Help:Test', CONTENT_MODEL_CSS, null, CONTENT_MODEL_CSS, false ],
                        [
                                'hallo',
                                'MediaWiki:Test.js',
                                CONTENT_MODEL_CSS,
                                null,
                                CONTENT_MODEL_CSS,
-                               'hallo',
                                false
                        ],
                        [
@@ -282,13 +279,12 @@ class ContentHandlerTest extends MediaWikiTestCase {
                                CONTENT_MODEL_CSS,
                                null,
                                CONTENT_MODEL_CSS,
-                               serialize( 'hallo' ),
                                false
                        ],
 
-                       [ 'hallo', 'Help:Test', CONTENT_MODEL_WIKITEXT, "testing", null, null, true ],
-                       [ 'hallo', 'MediaWiki:Test.js', CONTENT_MODEL_CSS, "testing", null, null, true ],
-                       [ 'hallo', 'Dummy:Test', CONTENT_MODEL_JAVASCRIPT, "testing", null, null, true ],
+                       [ 'hallo', 'Help:Test', CONTENT_MODEL_WIKITEXT, "testing", null, true ],
+                       [ 'hallo', 'MediaWiki:Test.js', CONTENT_MODEL_CSS, "testing", null, true ],
+                       [ 'hallo', 'Dummy:Test', CONTENT_MODEL_JAVASCRIPT, "testing", null, true ],
                ];
        }
 
@@ -297,7 +293,7 @@ class ContentHandlerTest extends MediaWikiTestCase {
         * @covers ContentHandler::makeContent
         */
        public function testMakeContent( $data, $title, $modelId, $format,
-               $expectedModelId, $expectedNativeData, $shouldFail
+               $expectedModelId, $shouldFail
        ) {
                $title = Title::newFromText( $title );
                MediaWikiServices::getInstance()->getLinkCache()->addBadLinkObj( $title );
@@ -309,7 +305,7 @@ class ContentHandlerTest extends MediaWikiTestCase {
                        }
 
                        $this->assertEquals( $expectedModelId, $content->getModel(), 'bad model id' );
-                       $this->assertEquals( $expectedNativeData, $content->getNativeData(), 'bads native data' );
+                       $this->assertEquals( $data, $content->serialize(), 'bad serialized data' );
                } catch ( MWException $ex ) {
                        if ( !$shouldFail ) {
                                $this->fail( "ContentHandler::makeContent failed unexpectedly: " . $ex->getMessage() );
index 2c61b7d..a4dd1fc 100644 (file)
@@ -234,7 +234,7 @@ class JavaScriptContentTest extends TextContentTest {
                $content = new JavaScriptContent( $oldText );
                $newContent = $content->updateRedirect( $target );
 
-               $this->assertEquals( $expectedText, $newContent->getNativeData() );
+               $this->assertEquals( $expectedText, $newContent->getText() );
        }
 
        public static function provideUpdateRedirect() {
diff --git a/tests/phpunit/includes/content/MessageContentTest.php b/tests/phpunit/includes/content/MessageContentTest.php
new file mode 100644 (file)
index 0000000..60f68e7
--- /dev/null
@@ -0,0 +1,60 @@
+<?php
+
+/**
+ * @group ContentHandler
+ */
+class MessageContentTest extends MediaWikiLangTestCase {
+
+       public function testGetHtml() {
+               $msg = new Message( 'about' );
+               $cnt = new MessageContent( $msg );
+
+               $this->assertSame( $msg->parse(), $cnt->getHtml() );
+       }
+
+       public function testGetWikitext() {
+               $msg = new Message( 'about' );
+               $cnt = new MessageContent( $msg );
+
+               $this->assertSame( $msg->text(), $cnt->getWikitext() );
+       }
+
+       public function testGetMessage() {
+               $msg = new Message( 'about' );
+               $cnt = new MessageContent( $msg );
+
+               $this->assertEquals( $msg, $cnt->getMessage() );
+       }
+
+       public function testGetParserOutput() {
+               $msg = new Message( 'about' );
+               $cnt = new MessageContent( $msg );
+
+               $title = Title::makeTitle( NS_MEDIAWIKI, 'about' );
+
+               $this->assertSame( $msg->parse(), $cnt->getParserOutput( $title )->getText() );
+       }
+
+       public function testSerialize() {
+               $msg = new Message( 'about' );
+               $cnt = new MessageContent( $msg );
+
+               $this->assertSame( $msg->plain(), $cnt->serialize() );
+       }
+
+       public function testEquals() {
+               $msg1 = new Message( 'about' );
+               $cnt1 = new MessageContent( $msg1 );
+
+               $msg2 = new Message( 'about' );
+               $cnt2 = new MessageContent( $msg2 );
+
+               $msg3 = new Message( 'faq' );
+               $cnt3 = new MessageContent( $msg3 );
+               $cnt4 = new WikitextContent( $msg3->plain() );
+
+               $this->assertTrue( $cnt1->equals( $cnt2 ) );
+               $this->assertFalse( $cnt1->equals( $cnt3 ) );
+               $this->assertFalse( $cnt1->equals( $cnt4 ) );
+       }
+}
index 4f04e64..8e537d6 100644 (file)
@@ -45,6 +45,10 @@ class TextContentTest extends MediaWikiLangTestCase {
                parent::tearDown();
        }
 
+       /**
+        * @param string $text
+        * @return TextContent
+        */
        public function newContent( $text ) {
                return new TextContent( $text );
        }
@@ -131,7 +135,7 @@ class TextContentTest extends MediaWikiLangTestCase {
                        $options
                );
 
-               $this->assertEquals( $expected, $content->getNativeData() );
+               $this->assertEquals( $expected, $content->getText() );
        }
 
        public static function dataPreloadTransform() {
@@ -154,7 +158,7 @@ class TextContentTest extends MediaWikiLangTestCase {
                $content = $this->newContent( $text );
                $content = $content->preloadTransform( $this->context->getTitle(), $options );
 
-               $this->assertEquals( $expected, $content->getNativeData() );
+               $this->assertEquals( $expected, $content->getText() );
        }
 
        public static function dataGetRedirectTarget() {
@@ -269,7 +273,7 @@ class TextContentTest extends MediaWikiLangTestCase {
                $copy = $content->copy();
 
                $this->assertTrue( $content->equals( $copy ), 'copy must be equal to original' );
-               $this->assertEquals( 'hello world.', $copy->getNativeData() );
+               $this->assertEquals( 'hello world.', $copy->getText() );
        }
 
        /**
@@ -281,13 +285,22 @@ class TextContentTest extends MediaWikiLangTestCase {
                $this->assertEquals( 12, $content->getSize() );
        }
 
+       /**
+        * @covers TextContent::getText
+        */
+       public function testGetText() {
+               $content = $this->newContent( 'hello world.' );
+
+               $this->assertEquals( 'hello world.', $content->getText() );
+       }
+
        /**
         * @covers TextContent::getNativeData
         */
        public function testGetNativeData() {
                $content = $this->newContent( 'hello world.' );
 
-               $this->assertEquals( 'hello world.', $content->getNativeData() );
+               $this->assertEquals( 'hello world.', $content->getText() );
        }
 
        /**
@@ -438,13 +451,14 @@ class TextContentTest extends MediaWikiLangTestCase {
        public function testConvert( $text, $model, $lossy, $expectedNative ) {
                $content = $this->newContent( $text );
 
+               /** @var TextContent $converted */
                $converted = $content->convert( $model, $lossy );
 
                if ( $expectedNative === false ) {
                        $this->assertFalse( $converted, "conversion to $model was expected to fail!" );
                } else {
                        $this->assertInstanceOf( Content::class, $converted );
-                       $this->assertEquals( $expectedNative, $converted->getNativeData() );
+                       $this->assertEquals( $expectedNative, $converted->getText() );
                }
        }
 
@@ -473,4 +487,10 @@ class TextContentTest extends MediaWikiLangTestCase {
                ];
        }
 
+       public function testSerialize() {
+               $cnt = $this->newContent( 'testing text' );
+
+               $this->assertSame( 'testing text', $cnt->serialize() );
+       }
+
 }
index 31d90cb..5f78a5c 100644 (file)
@@ -44,10 +44,10 @@ class WikitextContentHandlerTest extends MediaWikiLangTestCase {
         */
        public function testUnserializeContent() {
                $content = $this->handler->unserializeContent( 'hello world' );
-               $this->assertEquals( 'hello world', $content->getNativeData() );
+               $this->assertEquals( 'hello world', $content->getText() );
 
                $content = $this->handler->unserializeContent( 'hello world', CONTENT_FORMAT_WIKITEXT );
-               $this->assertEquals( 'hello world', $content->getNativeData() );
+               $this->assertEquals( 'hello world', $content->getText() );
 
                try {
                        $this->handler->unserializeContent( 'hello world', 'dummy/foo' );
@@ -64,7 +64,7 @@ class WikitextContentHandlerTest extends MediaWikiLangTestCase {
                $content = $this->handler->makeEmptyContent();
 
                $this->assertTrue( $content->isEmpty() );
-               $this->assertEquals( '', $content->getNativeData() );
+               $this->assertEquals( '', $content->getText() );
        }
 
        public static function dataIsSupportedFormat() {
@@ -172,7 +172,7 @@ class WikitextContentHandlerTest extends MediaWikiLangTestCase {
 
                $merged = $this->handler->merge3( $oldContent, $myContent, $yourContent );
 
-               $this->assertEquals( $expected, $merged ? $merged->getNativeData() : $merged );
+               $this->assertEquals( $expected, $merged ? $merged->getText() : $merged );
        }
 
        public static function dataGetAutosummary() {
index be93563..f689cae 100644 (file)
@@ -130,7 +130,7 @@ just a test"
 
                $sectionContent = $content->getSection( $sectionId );
                if ( is_object( $sectionContent ) ) {
-                       $sectionText = $sectionContent->getNativeData();
+                       $sectionText = $sectionContent->getText();
                } else {
                        $sectionText = $sectionContent;
                }
@@ -184,7 +184,7 @@ just a test"
                $content = $this->newContent( $text );
                $c = $content->replaceSection( $section, $this->newContent( $with ), $sectionTitle );
 
-               $this->assertEquals( $expected, is_null( $c ) ? null : $c->getNativeData() );
+               $this->assertEquals( $expected, is_null( $c ) ? null : $c->getText() );
        }
 
        /**
@@ -194,7 +194,7 @@ just a test"
                $content = $this->newContent( 'hello world' );
                $content = $content->addSectionHeader( 'test' );
 
-               $this->assertEquals( "== test ==\n\nhello world", $content->getNativeData() );
+               $this->assertEquals( "== test ==\n\nhello world", $content->getText() );
        }
 
        public static function dataPreSaveTransform() {