jenkins-bot has submitted this change and it was merged.

Change subject: Pass edit summary through ExternalChangeFactory.
......................................................................


Pass edit summary through ExternalChangeFactory.

This is part of the refactoring of the edit summary handling code that will
allow us to re-use the repo side summary generation code on the client.

This change implements points A, B, and C of part 4 of T101836#1414639.
In particular, it allows an edit summary to be passed via the
ExternalChange without being overwritten.

NOTE: this requires core patch I757f2b91f3b

Bug: T111039
Change-Id: I0fed0a5280450af6caeff9328e07f9f3409ee2b7
---
M client/WikibaseClient.hooks.php
M client/WikibaseClient.php
M client/includes/recentchanges/ChangeLineFormatter.php
M client/includes/recentchanges/ExternalChange.php
M client/includes/recentchanges/ExternalChangeFactory.php
M client/includes/recentchanges/ExternalRecentChange.php
M client/tests/phpunit/includes/recentchanges/ChangeLineFormatterTest.php
M client/tests/phpunit/includes/recentchanges/ExternalChangeFactoryTest.php
M client/tests/phpunit/includes/recentchanges/RevisionDataTest.php
9 files changed, 326 insertions(+), 209 deletions(-)

Approvals:
  Hoo man: Looks good to me, approved
  Daniel Kinzler: Looks good to me, but someone else must approve
  jenkins-bot: Verified



diff --git a/client/WikibaseClient.hooks.php b/client/WikibaseClient.hooks.php
index 1cdeb15..9716fee 100644
--- a/client/WikibaseClient.hooks.php
+++ b/client/WikibaseClient.hooks.php
@@ -15,6 +15,7 @@
 use Skin;
 use SpecialRecentChanges;
 use SpecialWatchlist;
+use StubObject;
 use Title;
 use UnexpectedValueException;
 use User;
@@ -31,6 +32,7 @@
 use Wikibase\Client\WikibaseClient;
 use Wikibase\DataModel\Entity\EntityId;
 use Wikibase\DataModel\SiteLink;
+use Wikibase\Lib\AutoCommentFormatter;
 
 /**
  * File defining the hook handlers for the Wikibase Client extension.
@@ -143,7 +145,8 @@
                if ( $type == RC_EXTERNAL ) {
                        $wikibaseClient = WikibaseClient::getDefaultInstance();
                        $changeFactory = new ExternalChangeFactory(
-                               $wikibaseClient->getSettings()->getSetting( 
'repoSiteId' )
+                               $wikibaseClient->getSettings()->getSetting( 
'repoSiteId' ),
+                               $wikibaseClient->getContentLanguage()
                        );
 
                        try {
@@ -177,6 +180,44 @@
        }
 
        /**
+        * Handler for the FormatAutocomments hook, implementing localized 
formatting
+        * for machine readable autocomments generated by SummaryFormatter.
+        *
+        * @param string &$comment reference to the autocomment text
+        * @param bool $pre true if there is content before the autocomment
+        * @param string $auto the autocomment unformatted
+        * @param bool $post true if there is content after the autocomment
+        * @param Title|null $title use for further information
+        * @param bool $local shall links be generated locally or globally
+        * @param string|null $wikiId The ID of the wiki the comment applies 
to, if not the local wiki.
+        *
+        * @return bool
+        */
+       public static function onFormat( &$comment, $pre, $auto, $post, $title, 
$local, $wikiId = null ) {
+               global $wgContLang;
+
+               $wikibaseClient = WikibaseClient::getDefaultInstance();
+               $repoId = $wikibaseClient->getSettings()->getSetting( 
'repoSiteId' );
+
+               // Only do special formatting for comments from a wikibase repo.
+               // XXX: what to do if the local wiki is the repo? For entity 
pages, RepoHooks has a handler.
+               // But what to do for other pages? Note that if the local wiki 
is the repo, $repoId will be
+               // false, and $wikiId will be null.
+               if ( $wikiId !== $repoId ) {
+                       return;
+               }
+
+               StubObject::unstub( $wgContLang );
+
+               $formatter = new AutoCommentFormatter( $wgContLang, array( 
'wikibase-entity' ) );
+               $formattedComment = $formatter->formatAutoComment( $auto );
+
+               if ( is_string( $formattedComment ) ) {
+                       $comment = $formatter->wrapAutoComment( $pre, 
$formattedComment, $post );
+               }
+       }
+
+       /**
         * Modifies watchlist query to include external changes
         * @see 
https://www.mediawiki.org/wiki/Manual:Hooks/SpecialWatchlistQuery
         *
diff --git a/client/WikibaseClient.php b/client/WikibaseClient.php
index ecf4e88..35d5702 100644
--- a/client/WikibaseClient.php
+++ b/client/WikibaseClient.php
@@ -126,6 +126,7 @@
        $wgHooks['ArticleDeleteAfterSuccess'][] = 
'\Wikibase\ClientHooks::onArticleDeleteAfterSuccess';
        $wgHooks['ParserLimitReportFormat'][] = 
'\Wikibase\Client\Hooks\ParserLimitHookHandlers::onParserLimitReportFormat';
        $wgHooks['ParserLimitReportPrepare'][] = 
'\Wikibase\Client\Hooks\ParserLimitHookHandlers::onParserLimitReportPrepare';
+       $wgHooks['FormatAutocomments'][] = '\Wikibase\ClientHooks::onFormat';
 
        // update hooks
        $wgHooks['LoadExtensionSchemaUpdates'][] = 
'\Wikibase\Client\Usage\Sql\SqlUsageTrackerSchemaUpdater::onSchemaUpdate';
diff --git a/client/includes/recentchanges/ChangeLineFormatter.php 
b/client/includes/recentchanges/ChangeLineFormatter.php
index 41a5310..6c5efaa 100644
--- a/client/includes/recentchanges/ChangeLineFormatter.php
+++ b/client/includes/recentchanges/ChangeLineFormatter.php
@@ -17,6 +17,7 @@
  *
  * @licence GNU GPL v2+
  * @author Katie Filbert < aude.w...@gmail.com >
+ * @author Daniel Kinzler
  */
 class ChangeLineFormatter {
 
@@ -52,7 +53,7 @@
         * @param int $count
         * @param string $flag - flag formatted by 
ChangesList::recentChangesFlags()
         *
-        * @return string
+        * @return string HTML
         */
        public function format( ExternalChange $externalChange, Title $title, 
$count, $flag ) {
                $changeType = $externalChange->getChangeType();
@@ -73,13 +74,13 @@
 
                $line .= $this->formatTimestamp( $rev->getTimestamp() );
                $line .= $this->formatUserLinks( $rev->getUserName() );
-               $line .= $this->formatComment( $rev->getComment() );
+               $line .= Linker::commentBlock( $rev->getComment(), $title, 
false, $externalChange->getSiteId() );
 
                return $line;
        }
 
        /**
-        * @return string
+        * @return string HTML
         */
        private function changeSeparator() {
                return ' <span class="mw-changeslist-separator">. .</span> ';
@@ -88,7 +89,7 @@
        /**
         * @param string $timestamp
         *
-        * @return string
+        * @return string HTML
         */
        private function formatTimestamp( $timestamp ) {
                return wfMessage( 'semicolon-separator' )->text()
@@ -100,7 +101,7 @@
        /**
         * @param string $userName
         *
-        * @return string
+        * @return string HTML
         */
        private function formatUserLinks( $userName ) {
                $links = $this->buildUserLinks( $userName );
@@ -117,7 +118,7 @@
         *
         * @param string[] $links
         *
-        * @return string
+        * @return string HTML
         */
        private function formatIpUserLinks( array $links ) {
                $userlinks = $links['contribs'];
@@ -135,7 +136,7 @@
         *
         * @param string[] $links
         *
-        * @return string
+        * @return string HTML
         */
        private function formatRegisteredUserLinks( array $links ) {
                $userlinks = $links['user'];
@@ -158,7 +159,7 @@
        /**
         * @param EntityId $entityId
         *
-        * @return string
+        * @return string HTML
         */
        private function formatEntityLink( EntityId $entityId ) {
                $entityLink = $this->repoLinker->buildEntityLink( $entityId );
@@ -168,7 +169,7 @@
        }
 
        /**
-        * @return string
+        * @return string HTML
         */
        private function formatDeletionLogLink() {
                $logLink = $this->repoLinker->formatLink(
@@ -184,7 +185,7 @@
         * @param RevisionData $rev
         * @param int $count
         *
-        * @return string
+        * @return string HTML
         */
        private function formatDiffHist( EntityId $entityId, RevisionData $rev, 
$count ) {
                $diffLink = $this->buildDiffLink( $entityId, $rev, $count );
@@ -200,7 +201,7 @@
         * @param RevisionData $rev
         * @param int $count
         *
-        * @return string
+        * @return string HTML
         */
        private function buildDiffLink( EntityId $entityId, RevisionData $rev, 
$count ) {
                $params = array(
@@ -226,7 +227,7 @@
         * @param EntityId $entityId
         * @param RevisionData $rev
         *
-        * @return string
+        * @return string HTML
         */
        private function buildHistoryLink( EntityId $entityId, RevisionData 
$rev ) {
                $titleText = $this->repoLinker->getEntityTitle( $entityId );
@@ -251,7 +252,7 @@
        /**
         * @param string $userName
         *
-        * @return string
+        * @return string HTML
         */
        private function buildUserLink( $userName ) {
                return $this->repoLinker->formatLink(
@@ -268,7 +269,7 @@
         * @param string $userName
         * @param string $text
         *
-        * @return string
+        * @return string HTML
         */
        private function buildUserContribsLink( $userName, $text = null ) {
                // @todo: know how the repo is localised. it's english now
@@ -285,7 +286,7 @@
        /**
         * @param string $userName
         *
-        * @return string
+        * @return string HTML
         */
        private function buildUserTalkLink( $userName ) {
                // @todo: localize this once we can localize namespaces on the 
repo
@@ -298,7 +299,7 @@
        /**
         * @param string $userName
         *
-        * @return string[]
+        * @return string[] List of HTML links
         */
        private function buildUserLinks( $userName ) {
                $links = array();
@@ -313,24 +314,6 @@
                }
 
                return $links;
-       }
-
-       /**
-        * @param array $comment
-        *
-        * @return string
-        */
-       private function formatComment( array $comment ) {
-               $commentMsg = wfMessage( $comment['key'] );
-
-               if ( isset( $comment['numparams'] ) ) {
-                       $commentMsg->numParams( $comment['numparams'] );
-               }
-
-               // fixme: find a way to inject or not use Linker
-               return Linker::commentBlock(
-                       $commentMsg->inLanguage( $this->lang )->text()
-               );
        }
 
 }
diff --git a/client/includes/recentchanges/ExternalChange.php 
b/client/includes/recentchanges/ExternalChange.php
index 721c4b5..ec826e5 100644
--- a/client/includes/recentchanges/ExternalChange.php
+++ b/client/includes/recentchanges/ExternalChange.php
@@ -61,4 +61,11 @@
                return $this->changeType;
        }
 
+       /**
+        * @return string
+        */
+       public function getSiteId() {
+               return $this->rev->getSiteId();
+       }
+
 }
diff --git a/client/includes/recentchanges/ExternalChangeFactory.php 
b/client/includes/recentchanges/ExternalChangeFactory.php
index 43f1d9d..355c284 100644
--- a/client/includes/recentchanges/ExternalChangeFactory.php
+++ b/client/includes/recentchanges/ExternalChangeFactory.php
@@ -3,6 +3,7 @@
 namespace Wikibase\Client\RecentChanges;
 
 use InvalidArgumentException;
+use Language;
 use RecentChange;
 use UnexpectedValueException;
 use Wikibase\DataModel\Entity\ItemId;
@@ -12,6 +13,7 @@
  *
  * @licence GNU GPL v2+
  * @author Katie Filbert < aude.w...@gmail.com >
+ * @author Daniel Kinzler
  */
 class ExternalChangeFactory {
 
@@ -21,10 +23,17 @@
        private $repoSiteId;
 
        /**
-        * @param string $repoSiteId
+        * @var Language
         */
-       public function __construct( $repoSiteId ) {
+       private $summaryLanguage;
+
+       /**
+        * @param string $repoSiteId
+        * @param Language $summaryLanguage Language to use when generating 
edit summaries
+        */
+       public function __construct( $repoSiteId, Language $summaryLanguage ) {
                $this->repoSiteId = $repoSiteId;
+               $this->summaryLanguage = $summaryLanguage;
        }
 
        /**
@@ -55,13 +64,19 @@
                $repoId = isset( $changeParams['site_id'] )
                        ? $changeParams['site_id'] : $this->repoSiteId;
 
+               $comment = $recentChange->getAttribute( 'rc_comment' );
+
+               if ( $comment === '' || $comment === null ) {
+                       $comment = $this->generateComment( $changeParams );
+               }
+
                return new RevisionData(
                        $recentChange->getAttribute( 'rc_user_text' ),
                        $changeParams['page_id'],
                        $changeParams['rev_id'],
                        $changeParams['parent_id'],
                        $recentChange->getAttribute( 'rc_timestamp' ),
-                       $this->extractComment( $changeParams ),
+                       $comment,
                        $repoId
                );
        }
@@ -168,7 +183,7 @@
         *
         * @return string
         */
-       private function parseComment( $comment, $type ) {
+       private function parseAutoComment( $comment, $type ) {
                $newComment = array(
                        'key' => 'wikibase-comment-update'
                );
@@ -185,39 +200,49 @@
                        }
                }
 
-               // @todo handle $comment values that are strings or whatever 
format
-               // that we use to transfer autocomments from repo to client.
-
                return $newComment;
        }
 
        /**
-        * @fixme refactor comments handling!
+        * @param array $comment
         *
+        * @return string
+        */
+       private function formatComment( array $comment ) {
+               $commentMsg = wfMessage( $comment['key'] )->inLanguage( 
$this->summaryLanguage );
+
+               if ( isset( $comment['numparams'] ) ) {
+                       $commentMsg->numParams( $comment['numparams'] );
+               }
+
+               return $commentMsg->text();
+       }
+
+       /**
         * @param array $changeParams
         *
         * @return string
         */
-       private function extractComment( array $changeParams ) {
-               $comment = array(
-                       'key' => 'wikibase-comment-update'
-               );
-
-               //TODO: If $changeParams['changes'] is set, this is a coalesced 
change.
-               //        Combine all the comments! Up to some max length?
+       private function generateComment( array $changeParams ) {
+               // NOTE: We want to get rid of the comment and 
composite-comment fields in $changeParams
+               // in the future, see 
https://phabricator.wikimedia.org/T101836#1414639 part 3.
                if ( array_key_exists( 'composite-comment', $changeParams ) ) {
                        $comment['key'] = 'wikibase-comment-multi';
                        $comment['numparams'] = $this->countCompositeComments( 
$changeParams['composite-comment'] );
-               } elseif ( array_key_exists( 'comment', $changeParams ) ) {
-                       $comment = $this->parseComment( 
$changeParams['comment'], $changeParams['type'] );
-               }
 
-               return $comment;
+                       return $this->formatComment( $comment );
+               } elseif ( array_key_exists( 'comment', $changeParams ) ) {
+                       $comment = $this->parseAutoComment( 
$changeParams['comment'], $changeParams['type'] );
+
+                       return $this->formatComment( $comment );
+               } else {
+                       // no override
+                       return false;
+               }
        }
 
        /**
         * normalizes for extra empty comment in rc_params (see bug T47812)
-        * @fixme: can remove at some point in the future
         *
         * @param array $comments
         *
diff --git a/client/includes/recentchanges/ExternalRecentChange.php 
b/client/includes/recentchanges/ExternalRecentChange.php
index e98fea2..b8e2274 100644
--- a/client/includes/recentchanges/ExternalRecentChange.php
+++ b/client/includes/recentchanges/ExternalRecentChange.php
@@ -33,6 +33,13 @@
        }
 
        /**
+        * @return array
+        */
+       public function getAttributes() {
+               return $this->mAttribs;
+       }
+
+       /**
         * Builds the attribute array for saving into recentchanges table
         *
         * @param array $attribs
diff --git 
a/client/tests/phpunit/includes/recentchanges/ChangeLineFormatterTest.php 
b/client/tests/phpunit/includes/recentchanges/ChangeLineFormatterTest.php
index 084abe3..f69e176 100644
--- a/client/tests/phpunit/includes/recentchanges/ChangeLineFormatterTest.php
+++ b/client/tests/phpunit/includes/recentchanges/ChangeLineFormatterTest.php
@@ -7,10 +7,13 @@
 use Language;
 use RecentChange;
 use RequestContext;
+use Title;
 use User;
 use Wikibase\Client\RecentChanges\ChangeLineFormatter;
 use Wikibase\Client\RecentChanges\ExternalChangeFactory;
+use Wikibase\Client\RecentChanges\ExternalRecentChange;
 use Wikibase\Client\RepoLinker;
+use Wikibase\Client\WikibaseClient;
 
 /**
  * @covers Wikibase\Client\RecentChanges\ChangeLineFormatter
@@ -23,7 +26,7 @@
  * @licence GNU GPL v2+
  * @author Katie Filbert < aude.w...@gmail.com >
  */
-class ChangeLineFormatterTest extends \MediaWikiTestCase {
+class ChangeLineFormatterTest extends \MediaWikiLangTestCase {
 
        protected $repoLinker;
 
@@ -56,8 +59,12 @@
        public function testFormat( array $expectedTags, array $patterns, 
RecentChange $recentChange ) {
                $context = $this->getTestContext();
 
+               // Use the actual setting, because out handler for the 
FormatAutocomment hook will check
+               // the wiki id against this setting.
+               $repoWikiId = 
WikibaseClient::getDefaultInstance()->getSettings()->getSetting( 'repoSiteId' );
+
                $changesList = ChangesList::newFromContext( $context );
-               $changeFactory = new ExternalChangeFactory( 'testrepo' );
+               $changeFactory = new ExternalChangeFactory( $repoWikiId, 
Language::factory( 'en' ) );
                $externalChange = $changeFactory->newFromRecentChange( 
$recentChange );
 
                $formatter = new ChangeLineFormatter(
@@ -74,7 +81,7 @@
                );
 
                foreach ( $expectedTags as $key => $tag ) {
-                       $this->assertTag( $tag, $formattedLine, $key );
+                       $this->assertTag( $tag, $formattedLine, $key . "\n\t" . 
$formattedLine );
                }
 
                foreach ( $patterns as $pattern ) {
@@ -92,27 +99,46 @@
 
        public function formatProvider() {
                return array(
-                       array(
-                               $this->getEditChangeTagMatchers(),
-                               $this->getEditPatterns(),
-                               $this->getEditRecentChange()
+                       'edit-change' => array(
+                               $this->getEditSiteLinkChangeTagMatchers(),
+                               $this->getEditSiteLinkPatterns(),
+                               $this->getEditSiteLinkRecentChange()
                        ),
-                       array(
+                       'log-change' => array(
                                $this->getLogChangeTagMatchers(),
-                               array(),
+                               array(
+                                       '/Log Change Comment/',
+                               ),
                                $this->getLogRecentChange()
-                       )
+                       ),
+                       'comment-fallback' => array(
+                               array(),
+                               array(
+                                       '/\(Associated .*? item deleted\. 
Language links removed\.\)/'
+                               ),
+                               $this->getEditSiteLinkRecentChange(
+                                       '',
+                                       array(
+                                               'message' => 
'wikibase-comment-remove',
+                                       ),
+                                       null
+                               )
+                       ),
                );
        }
 
-       public function getEditPatterns() {
+       public function getEditSiteLinkPatterns() {
                return array(
                        '/title=Q4&amp;curid=5&amp;action=history/',
-                       '/title=Q4&amp;curid=5&amp;diff=92&amp;oldid=90/'
+                       '/title=Q4&amp;curid=5&amp;diff=92&amp;oldid=90/',
+                       '/<span class="comment">\('
+                               . '‎<span dir="auto"><span 
class="autocomment">Changed claim: <\/span><\/span> '
+                               . '<a .*?>Property:P213<\/a>: <a .*?>Q850<\/a>'
+                               . '\)<\/span>/',
                );
        }
 
-       public function getEditChangeTagMatchers() {
+       public function getEditSiteLinkChangeTagMatchers() {
                return array(
                        'edit-difflink' => array(
                                'tag' => 'a',
@@ -190,10 +216,7 @@
                );
        }
 
-       protected function getEditRecentChange() {
-               $recentChange = new RecentChange();
-               $recentChange->counter = 2;
-
+       protected function getEditSiteLinkRecentChange( $comment = null, 
$legacyComment = null, $compositeLegacyComment = null ) {
                $params = array(
                        'wikibase-repo-change' => array(
                                'id' => 4,
@@ -208,44 +231,23 @@
                                'page_id' => 5,
                                'rev_id' => 92,
                                'parent_id' => 90,
-                               'comment' => array(
-                                       'message' => 
'wikibase-comment-sitelink-add',
-                                       'sitelink' => array(
-                                               'newlink' => array( 'site' => 
'dewiki', 'page' => 'Kanada' )
-                                       )
-                               )
                        )
                );
 
-               $attribs = array(
-                       'rc_id' => 315,
-                       'rc_timestamp' => '20130819111741',
-                       'rc_user' => 0,
-                       'rc_user_text' => 'Cat',
-                       'rc_namespace' => 0,
-                       'rc_title' => 'Canada',
-                       'rc_comment' => '',
-                       'rc_minor' => 1,
-                       'rc_bot' => 0,
-                       'rc_new' => 0,
-                       'rc_cur_id' => 52,
-                       'rc_this_oldid' => 114,
-                       'rc_last_oldid' => 114,
-                       'rc_type' => 5,
-                       'rc_patrolled' => 1,
-                       'rc_ip' => '',
-                       'rc_old_len' => 2,
-                       'rc_new_len' => 2,
-                       'rc_deleted' => 0,
-                       'rc_logid' => 0,
-                       'rc_log_type' => null,
-                       'rc_log_action' => '',
-                       'rc_params' => serialize( $params )
-               );
+               if ( !is_string( $comment ) ) {
+                       $comment = '/* wbsetclaim-update:2||1 */ 
[[Property:P213]]: [[Q850]]';
+               }
 
-               $recentChange->setAttribs( $attribs );
+               if ( $legacyComment ) {
+                       $params['wikibase-repo-change']['comment'] = 
$legacyComment;
+               }
 
-               return $recentChange;
+               if ( $compositeLegacyComment ) {
+                       $params['wikibase-repo-change']['composite-comment'] = 
$compositeLegacyComment;
+               }
+
+               $title = $this->makeTitle( NS_MAIN, 'Canada', 52, 114 );
+               return $this->makeRecentChange( $params, $title, $comment );
        }
 
        protected function getLogChangeTagMatchers() {
@@ -323,9 +325,6 @@
        }
 
        protected function getLogRecentChange() {
-               $recentChange = new RecentChange();
-               $recentChange->counter = 1;
-
                $params = array(
                        'wikibase-repo-change' => array(
                                'id' => 20,
@@ -339,38 +338,59 @@
                                'page_id' => 0,
                                'rev_id' => 0,
                                'parent_id' => 0,
-                               'comment' => array(
-                                       'message' => 'wikibase-comment-remove'
-                               )
                        )
                );
 
-               $attribs = array(
-                       'rc_id' => 316,
-                       'rc_timestamp' => '20130820151835',
-                       'rc_user' => 0,
-                       'rc_user_text' => 'Cat',
-                       'rc_namespace' => 0,
-                       'rc_title' => 'Canada',
-                       'rc_comment' => '',
-                       'rc_minor' => 1,
-                       'rc_bot' => 0,
-                       'rc_new' => 0,
-                       'rc_cur_id' => 12,
-                       'rc_this_oldid' => 53,
-                       'rc_last_oldid' => 53,
-                       'rc_type' => 5,
-                       'rc_patrolled' => 1,
-                       'rc_ip' => '',
-                       'rc_old_len' => 5,
-                       'rc_new_len' => 5,
-                       'rc_deleted' => 0,
-                       'rc_logid' => 0,
-                       'rc_log_type' => null,
-                       'rc_log_action' => '',
-                       'rc_params' => serialize( $params )
-               );
+               $title = $this->makeTitle( NS_MAIN, 'Canada', 12, 53 );
+               return $this->makeRecentChange( $params, $title, 'Log Change 
Comment' );
+       }
 
+       /**
+        * @param int $ns
+        * @param string $text
+        * @param int $pageId
+        * @param int $currentRevision
+        *
+        * @return Title
+        */
+       private function makeTitle( $ns, $text, $pageId, $currentRevision ) {
+               $title = $this->getMock( 'Title' );
+
+               $title->expects( $this->any() )
+                       ->method( 'getNamespace' )
+                       ->will( $this->returnValue( $ns ) );
+
+               $title->expects( $this->any() )
+                       ->method( 'getText' )
+                       ->will( $this->returnValue( $text ) );
+
+               $title->expects( $this->any() )
+                       ->method( 'getDBKey' )
+                       ->will( $this->returnValue( str_replace( ' ', '_', 
$text ) ) );
+
+               $title->expects( $this->any() )
+                       ->method( 'getArticleID' )
+                       ->will( $this->returnValue( $pageId ) );
+
+               $title->expects( $this->any() )
+                       ->method( 'getLatestRevID' )
+                       ->will( $this->returnValue( $currentRevision ) );
+
+               $title->expects( $this->any() )
+                       ->method( 'getLength' )
+                       ->will( $this->returnValue( 1234 ) );
+
+               return $title;
+       }
+
+       private function makeRecentChange( array $params, Title $title, 
$comment ) {
+               $recentChange = new RecentChange();
+               $recentChange->counter = 1;
+
+               $externalChange = ExternalRecentChange::newFromAttribs( 
$params, $title );
+               $attribs = $externalChange->getAttributes();
+
+               $attribs['rc_comment'] = $comment;
                $recentChange->setAttribs( $attribs );
 
                return $recentChange;
diff --git 
a/client/tests/phpunit/includes/recentchanges/ExternalChangeFactoryTest.php 
b/client/tests/phpunit/includes/recentchanges/ExternalChangeFactoryTest.php
index 3b4957d..172c996 100644
--- a/client/tests/phpunit/includes/recentchanges/ExternalChangeFactoryTest.php
+++ b/client/tests/phpunit/includes/recentchanges/ExternalChangeFactoryTest.php
@@ -2,6 +2,7 @@
 
 namespace Wikibase\Client\Tests\RecentChanges;
 
+use Language;
 use RecentChange;
 use Wikibase\Client\RecentChanges\ExternalChange;
 use Wikibase\Client\RecentChanges\ExternalChangeFactory;
@@ -18,18 +19,27 @@
  *
  * @licence GNU GPL v2+
  * @author Katie Filbert < aude.w...@gmail.com >
+ * @author Daniel Kinzler
  */
 class ExternalChangeFactoryTest extends \MediaWikiTestCase {
 
+       private function getExternalChangeFactory() {
+               return new ExternalChangeFactory( 'testrepo', 
Language::factory( 'qqx' ) );
+       }
+
        public function testNewFromRecentChange_itemUpdated() {
-               $commentData = 'wikibase-comment-update';
+               $recentChange = $this->makeRecentChange(
+                       '',
+                       'wikibase-comment-update',
+                       null,
+                       'wikibase-item~update',
+                       false
+               );
 
-               $recentChange = $this->makeRecentChange( $commentData, 
'wikibase-item~update', false );
-
-               $externalChangeFactory = new ExternalChangeFactory( 'testrepo' 
);
+               $externalChangeFactory = $this->getExternalChangeFactory();
 
                $this->assertEquals(
-                       $this->makeExpectedExternalChange( 
'wikibase-comment-update', 'update' ),
+                       $this->makeExpectedExternalChange( 
'(wikibase-comment-update)', 'update' ),
                        $externalChangeFactory->newFromRecentChange( 
$recentChange )
                );
        }
@@ -37,76 +47,86 @@
        public function testNewFromRecentChange_siteLinkChange() {
                // at the moment, we don't do anything with this info :( and 
just say
                // 'wikibase-comment-update' for these changes.
-               $commentData = array(
-                       'message' => 'wikibase-comment-sitelink-add',
-                       'sitelink' => array(
-                               'newlink' => array( 'site' => 'dewiki', 'page' 
=> 'Kanada' )
-                       )
+               $recentChange = $this->makeRecentChange(
+                       '',
+                       array(
+                               'message' => 'wikibase-comment-sitelink-add',
+                               'sitelink' => array(
+                                       'newlink' => array( 'site' => 'dewiki', 
'page' => 'Kanada' )
+                               )
+                       ),
+                       null,
+                       'wikibase-item~update',
+                       false
                );
 
-               $recentChange = $this->makeRecentChange( $commentData, 
'wikibase-item~update', false );
-
-               $externalChangeFactory = new ExternalChangeFactory( 'testrepo' 
);
+               $externalChangeFactory = $this->getExternalChangeFactory();
 
                $this->assertEquals(
-                       $this->makeExpectedExternalChange( 
'wikibase-comment-update', 'update' ),
+                       $this->makeExpectedExternalChange( 
'(wikibase-comment-update)', 'update' ),
                        $externalChangeFactory->newFromRecentChange( 
$recentChange )
                );
        }
 
        public function testNewFromRecentChange_pageLinkedOnRepo() {
-               $commentData = array(
-                       'message' => 'wikibase-comment-linked'
+               $recentChange = $this->makeRecentChange(
+                       '',
+                       array(
+                               'message' => 'wikibase-comment-linked'
+                       ),
+                       null,
+                       'wikibase-item~add',
+                       false
                );
 
-               $recentChange = $this->makeRecentChange( $commentData, 
'wikibase-item~add', false );
-
-               $externalChangeFactory = new ExternalChangeFactory( 'testrepo' 
);
+               $externalChangeFactory = $this->getExternalChangeFactory();
 
                $this->assertEquals(
-                       $this->makeExpectedExternalChange( 
'wikibase-comment-linked', 'add' ),
+                       $this->makeExpectedExternalChange( 
'(wikibase-comment-linked)', 'add' ),
                        $externalChangeFactory->newFromRecentChange( 
$recentChange )
                );
        }
 
        public function testNewFromRecentChange_withRepoComment() {
-               $commentData = '/* wbsetclaim-update:2||1 */ [[Property:P213]]: 
[[Q850]]';
+               $comment = '/* wbsetclaim-update:2||1 */ [[Property:P213]]: 
[[Q850]]';
 
-               $recentChange = $this->makeRecentChange( $commentData, 
'wikibase-item~update', false );
+               $recentChange = $this->makeRecentChange(
+                       $comment,
+                       array(
+                               'message' => 'this-shall-be-ignored'
+                       ),
+                       null,
+                       'wikibase-item~update',
+                       false
+               );
 
-               $externalChangeFactory = new ExternalChangeFactory( 'testrepo' 
);
+               $externalChangeFactory = $this->getExternalChangeFactory();
 
                $this->assertEquals(
-                       $this->makeExpectedExternalChange( 
'wikibase-comment-update', 'update' ),
+                       $this->makeExpectedExternalChange( $comment, 'update' ),
                        $externalChangeFactory->newFromRecentChange( 
$recentChange )
                );
        }
 
        public function testNewFromRecentChange_compositeComment() {
-               $commentData = 'wikibase-comment-update';
-
-               $recentChange = new RecentChange();
-               $recentChange->counter = 2;
-
-               $rcParams = $this->makeRCParams( $commentData, 
'wikibase-item~update', false );
-
-               $rcParams['wikibase-repo-change']['composite-comment'] = array(
-                       'wikibase-comment-update',
-                       'wikibase-comment-update'
+               $recentChange = $this->makeRecentChange(
+                       '',
+                       null,
+                       array(
+                               'wikibase-comment-update',
+                               'wikibase-comment-update'
+                       ),
+                       'wikibase-item~update',
+                       false
                );
-
-               $recentChange->setAttribs( $this->makeAttribs( $rcParams, false 
) );
 
                $expected = new ExternalChange(
                        new ItemId( 'Q4' ),
-                       $this->makeRevisionData( array(
-                               'key' => 'wikibase-comment-multi',
-                               'numparams' => 2
-                       ) ),
+                       $this->makeRevisionData( '(wikibase-comment-multi: 2)' 
),
                        'update'
                );
 
-               $externalChangeFactory = new ExternalChangeFactory( 'testrepo' 
);
+               $externalChangeFactory = $this->getExternalChangeFactory();
 
                $this->assertEquals(
                        $expected,
@@ -115,35 +135,35 @@
        }
 
        public function testNewFromRecentChange_botEdit() {
-               $commentData = 'wikibase-comment-update';
-
-               $recentChange = $this->makeRecentChange( $commentData, 
'wikibase-item~update', true );
-
-               $expected = new ExternalChange(
-                       new ItemId( 'Q4' ),
-                       $this->makeRevisionData( array(
-                               'key' => 'wikibase-comment-update'
-                       ) ),
-                       'update'
+               $recentChange = $this->makeRecentChange(
+                       '',
+                       'wikibase-comment-update',
+                       null,
+                       'wikibase-item~update',
+                       true
                );
 
-               $externalChangeFactory = new ExternalChangeFactory( 'testrepo' 
);
+               $externalChangeFactory = $this->getExternalChangeFactory();
 
                $this->assertEquals(
-                       $this->makeExpectedExternalChange( 
'wikibase-comment-update', 'update' ),
+                       $this->makeExpectedExternalChange( 
'(wikibase-comment-update)', 'update' ),
                        $externalChangeFactory->newFromRecentChange( 
$recentChange )
                );
        }
 
        public function testNewFromRecentChange_nonBotEdit() {
-               $commentData = 'wikibase-comment-update';
+               $recentChange = $this->makeRecentChange(
+                       '',
+                       'wikibase-comment-update',
+                       null,
+                       'wikibase-item~update',
+                       false
+               );
 
-               $recentChange = $this->makeRecentChange( $commentData, 
'wikibase-item~update', false );
-
-               $externalChangeFactory = new ExternalChangeFactory( 'testrepo' 
);
+               $externalChangeFactory = $this->getExternalChangeFactory();
 
                $this->assertEquals(
-                       $this->makeExpectedExternalChange( 
'wikibase-comment-update', 'update' ),
+                       $this->makeExpectedExternalChange( 
'(wikibase-comment-update)', 'update' ),
                        $externalChangeFactory->newFromRecentChange( 
$recentChange )
                );
        }
@@ -157,55 +177,58 @@
        private function makeExpectedExternalChange( $expectedComment, 
$expectedType ) {
                return new ExternalChange(
                        new ItemId( 'Q4' ),
-                       $this->makeRevisionData( array(
-                               'key' => $expectedComment
-                       ) ),
+                       $this->makeRevisionData( $expectedComment ),
                        $expectedType
                );
        }
 
-       private function makeRevisionData( array $comment ) {
+       private function makeRevisionData( $comment ) {
                return new RevisionData(
                        'Cat',
                        5,
                        92,
                        90,
                        '20130819111741',
-                       $comment,
+                       strval( $comment ),
                        'testrepo'
                );
        }
 
        /**
-        * @param array|string $commentData
+        * @param string $comment
+        * @param null|string|array $commentOverride
+        * @param null|string|array $compositeCommentOverride
         * @param string $changeType
         * @param bool $isBot
         *
         * @return RecentChange
         */
-       private function makeRecentChange( $commentData, $changeType, $isBot ) {
+       private function makeRecentChange( $comment, $commentOverride, 
$compositeCommentOverride, $changeType, $isBot ) {
                $recentChange = new RecentChange();
                $recentChange->counter = 2;
 
                $attribs = $this->makeAttribs(
-                       $this->makeRCParams( $commentData, $changeType, $isBot 
),
+                       $this->makeRCParams( $comment, $commentOverride, 
$compositeCommentOverride, $changeType, $isBot ),
                        $isBot
                );
 
+               $attribs['rc_comment'] = $comment;
                $recentChange->setAttribs( $attribs );
 
                return $recentChange;
        }
 
        /**
-        * @param array|string $commentData
+        * @param string $comment
+        * @param null|string|array $commentOverride
+        * @param null|string|array $compositeCommentOverride
         * @param string $changeType
         * @param boolean $bot
         *
         * @return array
         */
-       private function makeRCParams( $commentData, $changeType, $bot ) {
-               return array(
+       private function makeRCParams( $comment, $commentOverride, 
$compositeCommentOverride, $changeType, $bot ) {
+               $params = array(
                        'wikibase-repo-change' => array(
                                'id' => 4,
                                'type' => $changeType,
@@ -219,9 +242,19 @@
                                'page_id' => 5,
                                'rev_id' => 92,
                                'parent_id' => 90,
-                               'comment' => $commentData
-                       )
+                       ),
+                       'comment' => $comment
                );
+
+               if ( $commentOverride ) {
+                       $params['wikibase-repo-change']['comment'] = 
$commentOverride;
+               }
+
+               if ( $compositeCommentOverride ) {
+                       $params['wikibase-repo-change']['composite-comment'] = 
$compositeCommentOverride;
+               }
+
+               return $params;
        }
 
        private function makeAttribs( array $rcParams, $bot ) {
diff --git a/client/tests/phpunit/includes/recentchanges/RevisionDataTest.php 
b/client/tests/phpunit/includes/recentchanges/RevisionDataTest.php
index a7bb8ce..7aa8be9 100644
--- a/client/tests/phpunit/includes/recentchanges/RevisionDataTest.php
+++ b/client/tests/phpunit/includes/recentchanges/RevisionDataTest.php
@@ -42,7 +42,7 @@
 
        public function testGetComment() {
                $revisionData = $this->newRevisionData();
-               $this->assertEquals( array( 'key' => 'wikibase-comment-update' 
), $revisionData->getComment() );
+               $this->assertEquals( 'Kitten Comment', 
$revisionData->getComment() );
        }
 
        public function testGetSiteId() {
@@ -51,7 +51,7 @@
        }
 
        private function newRevisionData() {
-               $comment = array( 'key' => 'wikibase-comment-update' );
+               $comment = 'Kitten Comment';
                return new RevisionData( 'Cat', 5, 92, 90, '20130819111741', 
$comment, 'testrepo' );
        }
 

-- 
To view, visit https://gerrit.wikimedia.org/r/235786
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I0fed0a5280450af6caeff9328e07f9f3409ee2b7
Gerrit-PatchSet: 25
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Daniel Kinzler <daniel.kinz...@wikimedia.de>
Gerrit-Reviewer: Addshore <addshorew...@gmail.com>
Gerrit-Reviewer: Adrian Lang <adrian.he...@wikimedia.de>
Gerrit-Reviewer: Aude <aude.w...@gmail.com>
Gerrit-Reviewer: Daniel Kinzler <daniel.kinz...@wikimedia.de>
Gerrit-Reviewer: Hoo man <h...@online.de>
Gerrit-Reviewer: Jonas Kress (WMDE) <jonas.kr...@wikimedia.de>
Gerrit-Reviewer: Thiemo Mättig (WMDE) <thiemo.maet...@wikimedia.de>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to