jenkins-bot has submitted this change and it was merged.
Change subject: Add WatchedItemStore::getWatchedItemsForUser
......................................................................
Add WatchedItemStore::getWatchedItemsForUser
This method is also no used in SpecialEditWatchlist
Unit and Integration tests have also been added.
Bug: T129481
Change-Id: Ica136b6fee63be8de4c2fe502288dd2318d99114
---
M includes/WatchedItemStore.php
M includes/specials/SpecialEditWatchlist.php
M tests/phpunit/includes/WatchedItemStoreIntegrationTest.php
M tests/phpunit/includes/WatchedItemStoreUnitTest.php
4 files changed, 169 insertions(+), 20 deletions(-)
Approvals:
Legoktm: Looks good to me, approved
jenkins-bot: Verified
diff --git a/includes/WatchedItemStore.php b/includes/WatchedItemStore.php
index b3a06e2..26bba50 100644
--- a/includes/WatchedItemStore.php
+++ b/includes/WatchedItemStore.php
@@ -397,6 +397,38 @@
}
/**
+ * @param User $user
+ * @param array $options Allowed keys:
+ * 'forWrite' => bool defaults to false
+ *
+ * @return WatchedItem[]
+ */
+ public function getWatchedItemsForUser( User $user, array $options = []
) {
+ $options += [ 'forWrite' => false ];
+
+ $db = $this->getConnection( $options['forWrite'] ? DB_MASTER :
DB_SLAVE );
+ $res = $db->select(
+ 'watchlist',
+ [ 'wl_namespace', 'wl_title',
'wl_notificationtimestamp' ],
+ [ 'wl_user' => $user->getId() ],
+ __METHOD__
+ );
+ $this->reuseConnection( $db );
+
+ $watchedItems = [];
+ foreach ( $res as $row ) {
+ // todo these could all be cached at some point?
+ $watchedItems[] = new WatchedItem(
+ $user,
+ new TitleValue( (int)$row->wl_namespace,
$row->wl_title ),
+ $row->wl_notificationtimestamp
+ );
+ }
+
+ return $watchedItems;
+ }
+
+ /**
* Must be called separately for Subject & Talk namespaces
*
* @param User $user
diff --git a/includes/specials/SpecialEditWatchlist.php
b/includes/specials/SpecialEditWatchlist.php
index e76988d..dd440b9 100644
--- a/includes/specials/SpecialEditWatchlist.php
+++ b/includes/specials/SpecialEditWatchlist.php
@@ -307,32 +307,25 @@
private function getWatchlist() {
$list = [];
- $index = $this->getRequest()->wasPosted() ? DB_MASTER :
DB_SLAVE;
- $dbr = wfGetDB( $index );
-
- $res = $dbr->select(
- 'watchlist',
- [
- 'wl_namespace', 'wl_title'
- ], [
- 'wl_user' => $this->getUser()->getId(),
- ],
- __METHOD__
+ $watchedItems =
WatchedItemStore::getDefaultInstance()->getWatchedItemsForUser(
+ $this->getUser(),
+ [ 'forWrite' => $this->getRequest()->wasPosted() ]
);
- if ( $res->numRows() > 0 ) {
+ if ( $watchedItems ) {
/** @var Title[] $titles */
$titles = [];
- foreach ( $res as $row ) {
- $title = Title::makeTitleSafe(
$row->wl_namespace, $row->wl_title );
+ foreach ( $watchedItems as $watchedItem ) {
+ $namespace =
$watchedItem->getLinkTarget()->getNamespace();
+ $dbKey =
$watchedItem->getLinkTarget()->getDBkey();
+ $title = Title::makeTitleSafe( $namespace,
$dbKey );
- if ( $this->checkTitle( $title,
$row->wl_namespace, $row->wl_title )
+ if ( $this->checkTitle( $title, $namespace,
$dbKey )
&& !$title->isTalkPage()
) {
$titles[] = $title;
}
}
- $res->free();
GenderCache::singleton()->doTitlesArray( $titles );
diff --git a/tests/phpunit/includes/WatchedItemStoreIntegrationTest.php
b/tests/phpunit/includes/WatchedItemStoreIntegrationTest.php
index 91dd1aa..9eaa35a 100644
--- a/tests/phpunit/includes/WatchedItemStoreIntegrationTest.php
+++ b/tests/phpunit/includes/WatchedItemStoreIntegrationTest.php
@@ -39,6 +39,21 @@
'Page should be watched'
);
$this->assertEquals( $initialUserWatchedItems + 1,
$store->countWatchedItems( $user ) );
+ $watchedItemsForUser = $store->getWatchedItemsForUser( $user );
+ $this->assertCount( $initialUserWatchedItems + 1,
$watchedItemsForUser );
+ $watchedItemsForUserHasExpectedItem = false;
+ foreach ( $watchedItemsForUser as $watchedItem ) {
+ if (
+ $watchedItem->getUser()->equals( $user ) &&
+ $watchedItem->getLinkTarget() ==
$title->getTitleValue()
+ ) {
+ $watchedItemsForUserHasExpectedItem = true;
+ }
+ }
+ $this->assertTrue(
+ $watchedItemsForUserHasExpectedItem,
+ 'getWatchedItemsForUser should contain the page'
+ );
$this->assertEquals( $initialWatchers + 1,
$store->countWatchers( $title ) );
$this->assertEquals(
$initialWatchers + 1,
@@ -59,6 +74,21 @@
'Page should be unwatched'
);
$this->assertEquals( $initialUserWatchedItems,
$store->countWatchedItems( $user ) );
+ $watchedItemsForUser = $store->getWatchedItemsForUser( $user );
+ $this->assertCount( $initialUserWatchedItems,
$watchedItemsForUser );
+ $watchedItemsForUserHasExpectedItem = false;
+ foreach ( $watchedItemsForUser as $watchedItem ) {
+ if (
+ $watchedItem->getUser()->equals( $user ) &&
+ $watchedItem->getLinkTarget() ==
$title->getTitleValue()
+ ) {
+ $watchedItemsForUserHasExpectedItem = true;
+ }
+ }
+ $this->assertFalse(
+ $watchedItemsForUserHasExpectedItem,
+ 'getWatchedItemsForUser should not contain the page'
+ );
$this->assertEquals( $initialWatchers, $store->countWatchers(
$title ) );
$this->assertEquals(
$initialWatchers,
diff --git a/tests/phpunit/includes/WatchedItemStoreUnitTest.php
b/tests/phpunit/includes/WatchedItemStoreUnitTest.php
index 983a5fe..2996abf 100644
--- a/tests/phpunit/includes/WatchedItemStoreUnitTest.php
+++ b/tests/phpunit/includes/WatchedItemStoreUnitTest.php
@@ -17,13 +17,20 @@
/**
* @return PHPUnit_Framework_MockObject_MockObject|LoadBalancer
*/
- private function getMockLoadBalancer( $mockDb ) {
+ private function getMockLoadBalancer( $mockDb, $expectedConnectionType
= null ) {
$mock = $this->getMockBuilder( LoadBalancer::class )
->disableOriginalConstructor()
->getMock();
- $mock->expects( $this->any() )
- ->method( 'getConnection' )
- ->will( $this->returnValue( $mockDb ) );
+ if ( $expectedConnectionType !== null ) {
+ $mock->expects( $this->any() )
+ ->method( 'getConnection' )
+ ->with( $expectedConnectionType )
+ ->will( $this->returnValue( $mockDb ) );
+ } else {
+ $mock->expects( $this->any() )
+ ->method( 'getConnection' )
+ ->will( $this->returnValue( $mockDb ) );
+ }
$mock->expects( $this->any() )
->method( 'getReadOnlyReason' )
->will( $this->returnValue( false ) );
@@ -1139,6 +1146,93 @@
);
}
+ public function testGetWatchedItemsForUser() {
+ $mockDb = $this->getMockDb();
+ $mockDb->expects( $this->once() )
+ ->method( 'select' )
+ ->with(
+ 'watchlist',
+ [ 'wl_namespace', 'wl_title',
'wl_notificationtimestamp' ],
+ [ 'wl_user' => 1 ]
+ )
+ ->will( $this->returnValue( [
+ $this->getFakeRow( [
+ 'wl_namespace' => 0,
+ 'wl_title' => 'Foo1',
+ 'wl_notificationtimestamp' =>
'20151212010101',
+ ] ),
+ $this->getFakeRow( [
+ 'wl_namespace' => 1,
+ 'wl_title' => 'Foo2',
+ 'wl_notificationtimestamp' => null,
+ ] ),
+ ] ) );
+
+ $mockCache = $this->getMockCache();
+ $mockCache->expects( $this->never() )->method( 'delete' );
+ $mockCache->expects( $this->never() )->method( 'get' );
+ $mockCache->expects( $this->never() )->method( 'set' );
+
+ $store = new WatchedItemStore(
+ $this->getMockLoadBalancer( $mockDb ),
+ $mockCache
+ );
+ $user = $this->getMockNonAnonUserWithId( 1 );
+
+ $watchedItems = $store->getWatchedItemsForUser( $user );
+
+ $this->assertInternalType( 'array', $watchedItems );
+ $this->assertCount( 2, $watchedItems );
+ foreach ( $watchedItems as $watchedItem ) {
+ $this->assertInstanceOf( 'WatchedItem', $watchedItem );
+ }
+ $this->assertEquals(
+ new WatchedItem( $user, new TitleValue( 0, 'Foo1' ),
'20151212010101' ),
+ $watchedItems[0]
+ );
+ $this->assertEquals(
+ new WatchedItem( $user, new TitleValue( 1, 'Foo2' ),
null ),
+ $watchedItems[1]
+ );
+ }
+
+ public function provideDbTypes() {
+ return [
+ [ false, DB_SLAVE ],
+ [ true, DB_MASTER ],
+ ];
+ }
+
+ /**
+ * @dataProvider provideDbTypes
+ */
+ public function testGetWatchedItemsForUser_optionsAndEmptyResult(
$forWrite, $dbType ) {
+ $mockDb = $this->getMockDb();
+ $mockCache = $this->getMockCache();
+ $mockLoadBalancer = $this->getMockLoadBalancer( $mockDb,
$dbType );
+ $user = $this->getMockNonAnonUserWithId( 1 );
+
+ $mockDb->expects( $this->once() )
+ ->method( 'select' )
+ ->with(
+ 'watchlist',
+ [ 'wl_namespace', 'wl_title',
'wl_notificationtimestamp' ],
+ [ 'wl_user' => 1 ]
+ )
+ ->will( $this->returnValue( [] ) );
+
+ $store = new WatchedItemStore(
+ $mockLoadBalancer,
+ $mockCache
+ );
+
+ $watchedItems = $store->getWatchedItemsForUser(
+ $user,
+ [ 'forWrite' => $forWrite ]
+ );
+ $this->assertEquals( [], $watchedItems );
+ }
+
public function testIsWatchedItem_existingItem() {
$mockDb = $this->getMockDb();
$mockDb->expects( $this->once() )
--
To view, visit https://gerrit.wikimedia.org/r/277675
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Ica136b6fee63be8de4c2fe502288dd2318d99114
Gerrit-PatchSet: 7
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Addshore <[email protected]>
Gerrit-Reviewer: Addshore <[email protected]>
Gerrit-Reviewer: Anomie <[email protected]>
Gerrit-Reviewer: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: Legoktm <[email protected]>
Gerrit-Reviewer: Tobias Gritschacher <[email protected]>
Gerrit-Reviewer: WMDE-Fisch <[email protected]>
Gerrit-Reviewer: WMDE-leszek <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits