WMDE-Fisch has uploaded a new change for review.
https://gerrit.wikimedia.org/r/283213
Change subject: Hide hiddencat catwatch changes in special changelists
......................................................................
Hide hiddencat catwatch changes in special changelists
If a hidden category has a member added or removed
this will only be shown to users that have the user
preference "display hidden categories".
Also added static method to WikiCategoryPage to check
for hidden status.
Added unit tests for both.
Bug: T127944
Change-Id: Ic53d51391fa29d5e18fef4c42b8a275816c7ba4a
---
M includes/PageProps.php
M includes/changes/RecentChange.php
M includes/page/WikiCategoryPage.php
M includes/page/WikiPage.php
M includes/specials/SpecialRecentchanges.php
M includes/specials/SpecialWatchlist.php
M tests/phpunit/includes/changes/RecentChangeTest.php
A tests/phpunit/includes/page/WikiCategoryPageTest.php
8 files changed, 203 insertions(+), 2 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core
refs/changes/13/283213/1
diff --git a/includes/PageProps.php b/includes/PageProps.php
index bc3e3f1..50ccb86 100644
--- a/includes/PageProps.php
+++ b/includes/PageProps.php
@@ -34,6 +34,31 @@
private static $instance;
/**
+ * Overrides the default instance of this class
+ * This is intended for use while testing and will fail if
MW_PHPUNIT_TEST is not defined.
+ *
+ * If this method is used it MUST also be called with null after a test
to ensure a new
+ * default instance is created next time getInstance is called.
+ *
+ * @param PageProps|null $store
+ *
+ * @return ScopedCallback to reset the overridden value
+ * @throws MWException
+ */
+ public static function overrideInstance( PageProps $store = null ) {
+ if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
+ throw new MWException(
+ 'Cannot override ' . __CLASS__ . 'default
instance in operation.'
+ );
+ }
+ $previousValue = self::$instance;
+ self::$instance = $store;
+ return new ScopedCallback( function() use ( $previousValue ) {
+ self::$instance = $previousValue;
+ } );
+ }
+
+ /**
* @return PageProps
*/
public static function getInstance() {
diff --git a/includes/changes/RecentChange.php
b/includes/changes/RecentChange.php
index 2508304..d0a599a 100644
--- a/includes/changes/RecentChange.php
+++ b/includes/changes/RecentChange.php
@@ -851,7 +851,9 @@
'rc_logid' => 0,
'rc_log_type' => null,
'rc_log_action' => '',
- 'rc_params' => ''
+ 'rc_params' => serialize( [
+ 'hidden-cat' => WikiCategoryPage::factory(
$categoryTitle )->isHidden()
+ ] )
];
$rc->mExtra = [
@@ -863,6 +865,29 @@
];
return $rc;
+ }
+
+ /**
+ * Get a parameter value
+ *
+ * @param string $name parameter name
+ * @return mixed
+ */
+ public function getParam( $name ) {
+ $params = $this->parseParams();
+ return isset( $params[$name] ) ? $params[$name] : null;
+ }
+
+ /**
+ * Set a parameter value
+ *
+ * @param string $name parameter name
+ * @param mixed $value parameter value
+ */
+ public function setParam( $name, $value ) {
+ $params = $this->parseParams();
+ $params = array_merge( $params ? $params : [], [ $name =>
$value ] );
+ $this->setAttribute( 'rc_params', serialize( $params ) );
}
/**
@@ -887,6 +912,16 @@
}
/**
+ * Set an attribute value
+ *
+ * @param string $name Attribute name
+ * @param mixed $value Attribute value
+ */
+ public function setAttribute( $name, $value ) {
+ $this->mAttribs[$name] = $value;
+ }
+
+ /**
* @return array
*/
public function getAttributes() {
diff --git a/includes/page/WikiCategoryPage.php
b/includes/page/WikiCategoryPage.php
index d382001..4f79df5 100644
--- a/includes/page/WikiCategoryPage.php
+++ b/includes/page/WikiCategoryPage.php
@@ -47,4 +47,15 @@
}
return false;
}
+
+ /**
+ * Checks if a category is hidden.
+ * @return bool
+ */
+ public function isHidden() {
+ $pageId = $this->getTitle()->getArticleID();
+ $pageProps = PageProps::getInstance()->getProperties(
$this->getTitle(), 'hiddencat' );
+
+ return isset( $pageProps[$pageId] ) ? $pageProps[$pageId] :
false;
+ }
}
diff --git a/includes/page/WikiPage.php b/includes/page/WikiPage.php
index 82e66aa2..06785b7 100644
--- a/includes/page/WikiPage.php
+++ b/includes/page/WikiPage.php
@@ -94,7 +94,7 @@
* @param Title $title
*
* @throws MWException
- * @return WikiPage Object of the appropriate type
+ * @return WikiPage|WikiCategoryPage|WikiFilePage
*/
public static function factory( Title $title ) {
$ns = $title->getNamespace();
diff --git a/includes/specials/SpecialRecentchanges.php
b/includes/specials/SpecialRecentchanges.php
index de77380..8e52f14 100644
--- a/includes/specials/SpecialRecentchanges.php
+++ b/includes/specials/SpecialRecentchanges.php
@@ -324,12 +324,23 @@
$list = ChangesList::newFromContext( $this->getContext() );
$list->initChangesListRows( $rows );
+ $userShowHiddenCats = $this->getUser()->getBoolOption(
'showhiddencats' );
$rclistOutput = $list->beginRecentChangesList();
foreach ( $rows as $obj ) {
if ( $limit == 0 ) {
break;
}
$rc = RecentChange::newFromRow( $obj );
+
+ # Skip CatWatch entries for hidden cats based on user
preference
+ if (
+ $rc->getAttribute( 'rc_type' ) == RC_CATEGORIZE
&&
+ !$userShowHiddenCats &&
+ $rc->getParam( 'hidden-cat' )
+ ) {
+ continue;
+ }
+
$rc->counter = $counter++;
# Check if the page has been updated since the last
visit
if ( $this->getConfig()->get( 'ShowUpdatedMarker' )
diff --git a/includes/specials/SpecialWatchlist.php
b/includes/specials/SpecialWatchlist.php
index 27e5829..15691f2 100644
--- a/includes/specials/SpecialWatchlist.php
+++ b/includes/specials/SpecialWatchlist.php
@@ -369,10 +369,21 @@
}
$s = $list->beginRecentChangesList();
+ $userShowHiddenCats = $this->getUser()->getBoolOption(
'showhiddencats' );
$counter = 1;
foreach ( $rows as $obj ) {
# Make RC entry
$rc = RecentChange::newFromRow( $obj );
+
+ # Skip CatWatch entries for hidden cats based on user
preference
+ if (
+ $rc->getAttribute( 'rc_type' ) == RC_CATEGORIZE
&&
+ !$userShowHiddenCats &&
+ $rc->getParam( 'hidden-cat' )
+ ) {
+ continue;
+ }
+
$rc->counter = $counter++;
if ( $this->getConfig()->get( 'ShowUpdatedMarker' ) ) {
diff --git a/tests/phpunit/includes/changes/RecentChangeTest.php
b/tests/phpunit/includes/changes/RecentChangeTest.php
index 4abe9ee..d8a7b8d 100644
--- a/tests/phpunit/includes/changes/RecentChangeTest.php
+++ b/tests/phpunit/includes/changes/RecentChangeTest.php
@@ -134,4 +134,51 @@
$this->assertEquals( $rcType, RecentChange::parseToRCType(
$type ) );
}
+ /**
+ * @return PHPUnit_Framework_MockObject_MockObject|PageProps
+ */
+ private function getMockPageProps() {
+ return $this->getMockBuilder( PageProps::class )
+ ->disableOriginalConstructor()
+ ->getMock();
+ }
+
+ public function provideCategoryContent() {
+ return [
+ [ true ],
+ [ false ],
+ ];
+ }
+
+ /**
+ * @dataProvider provideCategoryContent
+ * @covers RecentChange::newForCategorization
+ */
+ public function testHiddenCategoryChange( $isHidden ) {
+ $categoryTitle = Title::newFromText( 'CategoryPage',
NS_CATEGORY );
+
+ $pageProps = $this->getMockPageProps();
+ $pageProps->expects( $this->once() )
+ ->method( 'getProperties' )
+ ->with( $categoryTitle, 'hiddencat' )
+ ->will( $this->returnValue( [
$categoryTitle->getArticleID() => $isHidden ] ) );
+
+ $scopedOverride = PageProps::overrideInstance( $pageProps );
+
+ $rc = RecentChange::newForCategorization(
+ '0',
+ $categoryTitle,
+ $this->user,
+ $this->user_comment,
+ $this->title,
+ $categoryTitle->getLatestRevID(),
+ $categoryTitle->getLatestRevID(),
+ '0',
+ false
+ );
+
+ $this->assertEquals( $isHidden, $rc->getParam( 'hidden-cat' ) );
+
+ ScopedCallback::consume( $scopedOverride );
+ }
}
diff --git a/tests/phpunit/includes/page/WikiCategoryPageTest.php
b/tests/phpunit/includes/page/WikiCategoryPageTest.php
new file mode 100644
index 0000000..548cd10
--- /dev/null
+++ b/tests/phpunit/includes/page/WikiCategoryPageTest.php
@@ -0,0 +1,61 @@
+<?php
+
+class WikiCategoryPageTest extends MediaWikiLangTestCase {
+
+ /**
+ * @return PHPUnit_Framework_MockObject_MockObject|PageProps
+ */
+ private function getMockPageProps() {
+ return $this->getMockBuilder( PageProps::class )
+ ->disableOriginalConstructor()
+ ->getMock();
+ }
+
+ /**
+ * @covers WikiCategoryPage::isHidden
+ */
+ public function testHiddenCategory_PropertyNotSet() {
+ $title = Title::makeTitle( NS_CATEGORY, 'CategoryPage' );
+ $categoryPage = WikiCategoryPage::factory( $title );
+
+ $pageProps = $this->getMockPageProps();
+ $pageProps->expects( $this->once() )
+ ->method( 'getProperties' )
+ ->with( $title, 'hiddencat' )
+ ->will( $this->returnValue( [ ] ) );
+
+ $scopedOverride = PageProps::overrideInstance( $pageProps );
+
+ $this->assertFalse( $categoryPage->isHidden() );
+
+ ScopedCallback::consume( $scopedOverride );
+ }
+
+ public function provideCategoryContent() {
+ return [
+ [ true ],
+ [ false ],
+ ];
+ }
+
+ /**
+ * @dataProvider provideCategoryContent
+ * @covers WikiCategoryPage::isHidden
+ */
+ public function testHiddenCategory_PropertyIsSet( $isHidden ) {
+ $title = Title::makeTitle( NS_CATEGORY, 'CategoryPage' );
+ $categoryPage = WikiCategoryPage::factory( $title );
+
+ $pageProps = $this->getMockPageProps();
+ $pageProps->expects( $this->once() )
+ ->method( 'getProperties' )
+ ->with( $title, 'hiddencat' )
+ ->will( $this->returnValue( [ $title->getArticleID() =>
$isHidden ] ) );
+
+ $scopedOverride = PageProps::overrideInstance( $pageProps );
+
+ $this->assertEquals( $isHidden, $categoryPage->isHidden() );
+
+ ScopedCallback::consume( $scopedOverride );
+ }
+}
--
To view, visit https://gerrit.wikimedia.org/r/283213
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ic53d51391fa29d5e18fef4c42b8a275816c7ba4a
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: WMDE-Fisch <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits