From 54c70c3551ea2b19a8967135fce5913c65e4bfbd Mon Sep 17 00:00:00 2001 From: daniel Date: Thu, 8 Nov 2018 16:19:23 +0100 Subject: [PATCH] Deprecate Content::getNativeData, add TextContent::getText 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 --- RELEASE-NOTES-1.33 | 4 ++ includes/content/AbstractContent.php | 40 ++++++++++++- includes/content/Content.php | 11 +++- includes/content/ContentHandler.php | 2 +- includes/content/CssContent.php | 6 +- includes/content/JavaScriptContent.php | 6 +- includes/content/JsonContent.php | 4 +- includes/content/MessageContent.php | 16 ++++- includes/content/TextContent.php | 35 +++++++---- includes/content/TextContentHandler.php | 2 +- includes/content/WikitextContent.php | 24 ++++---- includes/content/WikitextContentHandler.php | 20 +++++++ .../includes/content/ContentHandlerTest.php | 26 ++++---- .../content/JavaScriptContentTest.php | 2 +- .../includes/content/MessageContentTest.php | 60 +++++++++++++++++++ .../includes/content/TextContentTest.php | 30 ++++++++-- .../content/WikitextContentHandlerTest.php | 8 +-- .../includes/content/WikitextContentTest.php | 6 +- 18 files changed, 236 insertions(+), 66 deletions(-) create mode 100644 tests/phpunit/includes/content/MessageContentTest.php diff --git a/RELEASE-NOTES-1.33 b/RELEASE-NOTES-1.33 index 51756672e9..c9bd093b36 100644 --- a/RELEASE-NOTES-1.33 +++ b/RELEASE-NOTES-1.33 @@ -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 === diff --git a/includes/content/AbstractContent.php b/includes/content/AbstractContent.php index 733d85a065..c82b4734ca 100644 --- a/includes/content/AbstractContent.php +++ b/includes/content/AbstractContent.php @@ -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(); } /** diff --git a/includes/content/Content.php b/includes/content/Content.php index 1bb43f83b6..2637aa6929 100644 --- a/includes/content/Content.php +++ b/includes/content/Content.php @@ -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: * diff --git a/includes/content/ContentHandler.php b/includes/content/ContentHandler.php index 5c18a330cb..ae47b86056 100644 --- a/includes/content/ContentHandler.php +++ b/includes/content/ContentHandler.php @@ -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!' ); diff --git a/includes/content/CssContent.php b/includes/content/CssContent.php index a09cd151a1..d32fa88e59 100644 --- a/includes/content/CssContent.php +++ b/includes/content/CssContent.php @@ -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 .= "
\n";
-		$html .= htmlspecialchars( $this->getNativeData() );
+		$html .= htmlspecialchars( $this->getText() );
 		$html .= "\n
\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 ); diff --git a/includes/content/JavaScriptContent.php b/includes/content/JavaScriptContent.php index 69ed8d47a3..e6377980f9 100644 --- a/includes/content/JavaScriptContent.php +++ b/includes/content/JavaScriptContent.php @@ -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 .= "
\n";
-		$html .= htmlspecialchars( $this->getNativeData() );
+		$html .= htmlspecialchars( $this->getText() );
 		$html .= "\n
\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 ); diff --git a/includes/content/JsonContent.php b/includes/content/JsonContent.php index 7d8f67ce9d..0f8a9a945a 100644 --- a/includes/content/JsonContent.php +++ b/includes/content/JsonContent.php @@ -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; } diff --git a/includes/content/MessageContent.php b/includes/content/MessageContent.php index b21c6f4bb3..5626b549ea 100644 --- a/includes/content/MessageContent.php +++ b/includes/content/MessageContent.php @@ -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; } diff --git a/includes/content/TextContent.php b/includes/content/TextContent.php index 0198a0de11..750b958eab 100644 --- a/includes/content/TextContent.php +++ b/includes/content/TextContent.php @@ -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 ); } diff --git a/includes/content/TextContentHandler.php b/includes/content/TextContentHandler.php index 0978ffc288..e3dc187ffe 100644 --- a/includes/content/TextContentHandler.php +++ b/includes/content/TextContentHandler.php @@ -45,7 +45,7 @@ class TextContentHandler extends ContentHandler { public function serializeContent( Content $content, $format = null ) { $this->checkFormat( $format ); - return $content->getNativeData(); + return $content->getText(); } /** diff --git a/includes/content/WikitextContent.php b/includes/content/WikitextContent.php index 517d807867..3e2313c4b6 100644 --- a/includes/content/WikitextContent.php +++ b/includes/content/WikitextContent.php @@ -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() ); } } diff --git a/includes/content/WikitextContentHandler.php b/includes/content/WikitextContentHandler.php index ab157f55b3..191c718ca8 100644 --- a/includes/content/WikitextContentHandler.php +++ b/includes/content/WikitextContentHandler.php @@ -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 ); + } + } diff --git a/tests/phpunit/includes/content/ContentHandlerTest.php b/tests/phpunit/includes/content/ContentHandlerTest.php index 43edf60fea..a8ea3f0af3 100644 --- a/tests/phpunit/includes/content/ContentHandlerTest.php +++ b/tests/phpunit/includes/content/ContentHandlerTest.php @@ -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() ); diff --git a/tests/phpunit/includes/content/JavaScriptContentTest.php b/tests/phpunit/includes/content/JavaScriptContentTest.php index 2c61b7db2f..a4dd1fceb0 100644 --- a/tests/phpunit/includes/content/JavaScriptContentTest.php +++ b/tests/phpunit/includes/content/JavaScriptContentTest.php @@ -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 index 0000000000..60f68e76cc --- /dev/null +++ b/tests/phpunit/includes/content/MessageContentTest.php @@ -0,0 +1,60 @@ +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 ) ); + } +} diff --git a/tests/phpunit/includes/content/TextContentTest.php b/tests/phpunit/includes/content/TextContentTest.php index 4f04e64fff..8e537d684a 100644 --- a/tests/phpunit/includes/content/TextContentTest.php +++ b/tests/phpunit/includes/content/TextContentTest.php @@ -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() ); + } + } diff --git a/tests/phpunit/includes/content/WikitextContentHandlerTest.php b/tests/phpunit/includes/content/WikitextContentHandlerTest.php index 31d90cb568..5f78a5c00c 100644 --- a/tests/phpunit/includes/content/WikitextContentHandlerTest.php +++ b/tests/phpunit/includes/content/WikitextContentHandlerTest.php @@ -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() { diff --git a/tests/phpunit/includes/content/WikitextContentTest.php b/tests/phpunit/includes/content/WikitextContentTest.php index be93563168..f689cae73b 100644 --- a/tests/phpunit/includes/content/WikitextContentTest.php +++ b/tests/phpunit/includes/content/WikitextContentTest.php @@ -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() { -- 2.20.1