From 62961558a5b0bc61f752bd816ad633b7cacc02a9 Mon Sep 17 00:00:00 2001 From: Stephane Bisson Date: Wed, 10 Jun 2015 08:39:17 -0400 Subject: [PATCH] Encapsulate rc_params handling in RecentChange::parseParams * Make MachineReadableRCFeedFormatter use it * Some unit tests * Also fixed some line-too-long warnings in RecentChange.php Change-Id: I443d14f5d4cdac0945cb9c03608d55745bbb865b --- includes/changes/RecentChange.php | 17 ++++++ .../rcfeed/MachineReadableRCFeedFormatter.php | 4 +- .../includes/changes/RecentChangeTest.php | 55 ++++++++++++++++++- 3 files changed, 71 insertions(+), 5 deletions(-) diff --git a/includes/changes/RecentChange.php b/includes/changes/RecentChange.php index 13e94db329..691f9b27c2 100644 --- a/includes/changes/RecentChange.php +++ b/includes/changes/RecentChange.php @@ -848,4 +848,21 @@ class RecentChange { return wfTimestamp( TS_UNIX, $timestamp ) > time() - $tolerance - $wgRCMaxAge; } + + /** + * Parses and returns the rc_params attribute + * + * @since 1.26 + * + * @return array|null + */ + public function parseParams() { + $rcParams = $this->getAttribute( 'rc_params' ); + + wfSuppressWarnings(); + $unserializedParams = unserialize( $rcParams ); + wfRestoreWarnings(); + + return $unserializedParams; + } } diff --git a/includes/rcfeed/MachineReadableRCFeedFormatter.php b/includes/rcfeed/MachineReadableRCFeedFormatter.php index 519606ca3c..f524361ab1 100644 --- a/includes/rcfeed/MachineReadableRCFeedFormatter.php +++ b/includes/rcfeed/MachineReadableRCFeedFormatter.php @@ -90,9 +90,7 @@ abstract class MachineReadableRCFeedFormatter implements RCFeedFormatter { $packet['log_type'] = $rc->getAttribute( 'rc_log_type' ); $packet['log_action'] = $rc->getAttribute( 'rc_log_action' ); if ( $rc->getAttribute( 'rc_params' ) ) { - wfSuppressWarnings(); - $params = unserialize( $rc->getAttribute( 'rc_params' ) ); - wfRestoreWarnings(); + $params = $rc->parseParams(); if ( // If it's an actual serialised false... $rc->getAttribute( 'rc_params' ) == serialize( false ) || diff --git a/tests/phpunit/includes/changes/RecentChangeTest.php b/tests/phpunit/includes/changes/RecentChangeTest.php index e39c3823cd..ab35453dbc 100644 --- a/tests/phpunit/includes/changes/RecentChangeTest.php +++ b/tests/phpunit/includes/changes/RecentChangeTest.php @@ -297,16 +297,22 @@ class RecentChangeTest extends MediaWikiTestCase { $sep = $this->context->msg( 'colon-separator' )->text(); # import/upload + $msg = $this->context->msg( 'import-logentry-upload', 'SomeTitle' )->plain() . + $sep . + $this->user_comment; $this->assertIRCComment( - $this->context->msg( 'import-logentry-upload', 'SomeTitle' )->plain() . $sep . $this->user_comment, + $msg, 'import', 'upload', array(), $this->user_comment ); # import/interwiki + $msg = $this->context->msg( 'import-logentry-interwiki', 'SomeTitle' )->plain() . + $sep . + $this->user_comment; $this->assertIRCComment( - $this->context->msg( 'import-logentry-interwiki', 'SomeTitle' )->plain() . $sep . $this->user_comment, + $msg, 'import', 'interwiki', array(), $this->user_comment @@ -336,6 +342,51 @@ class RecentChangeTest extends MediaWikiTestCase { } */ + /** + * @covers RecentChange::parseParams + */ + public function testParseParams() { + $params = array( + 'root' => array( + 'A' => 1, + 'B' => 'two' + ) + ); + + $this->assertParseParams( + $params, + 'a:1:{s:4:"root";a:2:{s:1:"A";i:1;s:1:"B";s:3:"two";}}' + ); + + $this->assertParseParams( + null, + null + ); + + $this->assertParseParams( + null, + serialize( false ) + ); + + $this->assertParseParams( + null, + 'not-an-array' + ); + } + + /** + * @param array $expectedParseParams + * @param string|null $rawRcParams + */ + protected function assertParseParams( $expectedParseParams, $rawRcParams ) { + $rc = new RecentChange; + $rc->setAttribs( array( 'rc_params' => $rawRcParams ) ); + + $actualParseParams = $rc->parseParams(); + + $this->assertEquals( $expectedParseParams, $actualParseParams ); + } + /** * @param string $expected Expected IRC text without colors codes * @param string $type Log type (move, delete, suppress, patrol ...) -- 2.20.1