From 59e7587ed2719cc336996eae5182807eff14c707 Mon Sep 17 00:00:00 2001 From: addshore Date: Thu, 10 Mar 2016 13:43:36 +0000 Subject: [PATCH] Improve cache assertions in WatchedItemStoreUnitTest Change-Id: If4b2086cc29fcfc73dee2e9f96591a9e4aa50da9 --- includes/WatchedItemStore.php | 4 + .../includes/WatchedItemStoreUnitTest.php | 151 +++++++++++++++--- 2 files changed, 131 insertions(+), 24 deletions(-) diff --git a/includes/WatchedItemStore.php b/includes/WatchedItemStore.php index 721901f2ce..fc3af5f9ac 100644 --- a/includes/WatchedItemStore.php +++ b/includes/WatchedItemStore.php @@ -186,6 +186,10 @@ class WatchedItemStore { * @return WatchedItem|false */ public function getWatchedItem( User $user, LinkTarget $target ) { + if ( $user->isAnon() ) { + return false; + } + $cached = $this->getCached( $user, $target ); if ( $cached ) { return $cached; diff --git a/tests/phpunit/includes/WatchedItemStoreUnitTest.php b/tests/phpunit/includes/WatchedItemStoreUnitTest.php index fdb25455ee..1b1a319438 100644 --- a/tests/phpunit/includes/WatchedItemStoreUnitTest.php +++ b/tests/phpunit/includes/WatchedItemStoreUnitTest.php @@ -102,7 +102,7 @@ class WatchedItemStoreUnitTest extends PHPUnit_Framework_TestCase { $store = new WatchedItemStore( $this->getMockLoadBalancer( $mockDb ), - new HashBagOStuff( [ 'maxKeys' => 100 ] ) + $this->getMockCache() ); $store->duplicateEntry( @@ -154,9 +154,13 @@ class WatchedItemStoreUnitTest extends PHPUnit_Framework_TestCase { $this->isType( 'string' ) ); + $mockCache = $this->getMockCache(); + $mockCache->expects( $this->never() )->method( 'get' ); + $mockCache->expects( $this->never() )->method( 'delete' ); + $store = new WatchedItemStore( $this->getMockLoadBalancer( $mockDb ), - new HashBagOStuff( [ 'maxKeys' => 100 ] ) + $mockCache ); $store->duplicateEntry( @@ -196,9 +200,13 @@ class WatchedItemStoreUnitTest extends PHPUnit_Framework_TestCase { ) ->will( $this->returnValue( new FakeResultWrapper( [] ) ) ); + $mockCache = $this->getMockCache(); + $mockCache->expects( $this->never() )->method( 'get' ); + $mockCache->expects( $this->never() )->method( 'delete' ); + $store = new WatchedItemStore( $this->getMockLoadBalancer( $mockDb ), - new HashBagOStuff( [ 'maxKeys' => 100 ] ) + $mockCache ); $store->duplicateAllAssociatedEntries( @@ -272,9 +280,13 @@ class WatchedItemStoreUnitTest extends PHPUnit_Framework_TestCase { $this->isType( 'string' ) ); + $mockCache = $this->getMockCache(); + $mockCache->expects( $this->never() )->method( 'get' ); + $mockCache->expects( $this->never() )->method( 'delete' ); + $store = new WatchedItemStore( $this->getMockLoadBalancer( $mockDb ), - new HashBagOStuff( [ 'maxKeys' => 100 ] ) + $mockCache ); $store->duplicateAllAssociatedEntries( @@ -519,8 +531,8 @@ class WatchedItemStoreUnitTest extends PHPUnit_Framework_TestCase { ->will( $this->returnValue( [] ) ); $mockCache = $this->getMockCache(); - $mockCache->expects( $this->never() ) - ->method( 'delete' ); + $mockCache->expects( $this->never() )->method( 'get' ); + $mockCache->expects( $this->never() )->method( 'delete' ); $store = new WatchedItemStore( $this->getMockLoadBalancer( $mockDb ), @@ -541,8 +553,8 @@ class WatchedItemStoreUnitTest extends PHPUnit_Framework_TestCase { ->method( 'selectRow' ); $mockCache = $this->getMockCache(); - $mockCache->expects( $this->never() ) - ->method( 'delete' ); + $mockCache->expects( $this->never() )->method( 'get' ); + $mockCache->expects( $this->never() )->method( 'delete' ); $store = new WatchedItemStore( $this->getMockLoadBalancer( $mockDb ), @@ -574,6 +586,7 @@ class WatchedItemStoreUnitTest extends PHPUnit_Framework_TestCase { ->will( $this->returnValue( 1 ) ); $mockCache = $this->getMockCache(); + $mockCache->expects( $this->never() )->method( 'get' ); $mockCache->expects( $this->once() ) ->method( 'delete' ) ->with( '0:SomeDbKey:1' ); @@ -608,6 +621,7 @@ class WatchedItemStoreUnitTest extends PHPUnit_Framework_TestCase { ->will( $this->returnValue( 0 ) ); $mockCache = $this->getMockCache(); + $mockCache->expects( $this->never() )->method( 'get' ); $mockCache->expects( $this->once() ) ->method( 'delete' ) ->with( '0:SomeDbKey:1' ); @@ -631,6 +645,7 @@ class WatchedItemStoreUnitTest extends PHPUnit_Framework_TestCase { ->method( 'delete' ); $mockCache = $this->getMockCache(); + $mockCache->expects( $this->never() )->method( 'get' ); $mockCache->expects( $this->never() ) ->method( 'delete' ); @@ -665,6 +680,7 @@ class WatchedItemStoreUnitTest extends PHPUnit_Framework_TestCase { ) ); $mockCache = $this->getMockCache(); + $mockCache->expects( $this->never() )->method( 'delete' ); $mockCache->expects( $this->once() ) ->method( 'get' ) ->with( @@ -702,14 +718,14 @@ class WatchedItemStoreUnitTest extends PHPUnit_Framework_TestCase { $cachedItem = new WatchedItem( $mockUser, $linkTarget, '20151212010101' ); $mockCache = $this->getMockCache(); + $mockCache->expects( $this->never() )->method( 'delete' ); + $mockCache->expects( $this->never() )->method( 'set' ); $mockCache->expects( $this->once() ) ->method( 'get' ) ->with( '0:SomeDbKey:1' ) ->will( $this->returnValue( $cachedItem ) ); - $mockCache->expects( $this->never() ) - ->method( 'set' ); $store = new WatchedItemStore( $this->getMockLoadBalancer( $mockDb ), @@ -741,8 +757,12 @@ class WatchedItemStoreUnitTest extends PHPUnit_Framework_TestCase { ->will( $this->returnValue( [] ) ); $mockCache = $this->getMockCache(); - $mockCache->expects( $this->never() ) - ->method( 'set' ); + $mockCache->expects( $this->never() )->method( 'set' ); + $mockCache->expects( $this->never() )->method( 'delete' ); + $mockCache->expects( $this->once() ) + ->method( 'get' ) + ->with( '0:SomeDbKey:1' ) + ->will( $this->returnValue( false ) ); $store = new WatchedItemStore( $this->getMockLoadBalancer( $mockDb ), @@ -763,8 +783,9 @@ class WatchedItemStoreUnitTest extends PHPUnit_Framework_TestCase { ->method( 'selectRow' ); $mockCache = $this->getMockCache(); - $mockCache->expects( $this->never() ) - ->method( 'set' ); + $mockCache->expects( $this->never() )->method( 'set' ); + $mockCache->expects( $this->never() )->method( 'get' ); + $mockCache->expects( $this->never() )->method( 'delete' ); $store = new WatchedItemStore( $this->getMockLoadBalancer( $mockDb ), @@ -797,6 +818,11 @@ class WatchedItemStoreUnitTest extends PHPUnit_Framework_TestCase { ) ); $mockCache = $this->getMockCache(); + $mockCache->expects( $this->never() )->method( 'delete' ); + $mockCache->expects( $this->once() ) + ->method( 'get' ) + ->with( '0:SomeDbKey:1' ) + ->will( $this->returnValue( false ) ); $mockCache->expects( $this->once() ) ->method( 'set' ) ->with( @@ -832,8 +858,12 @@ class WatchedItemStoreUnitTest extends PHPUnit_Framework_TestCase { ->will( $this->returnValue( [] ) ); $mockCache = $this->getMockCache(); - $mockCache->expects( $this->never() ) - ->method( 'set' ); + $mockCache->expects( $this->never() )->method( 'set' ); + $mockCache->expects( $this->never() )->method( 'delete' ); + $mockCache->expects( $this->once() ) + ->method( 'get' ) + ->with( '0:SomeDbKey:1' ) + ->will( $this->returnValue( false ) ); $store = new WatchedItemStore( $this->getMockLoadBalancer( $mockDb ), @@ -854,8 +884,9 @@ class WatchedItemStoreUnitTest extends PHPUnit_Framework_TestCase { ->method( 'selectRow' ); $mockCache = $this->getMockCache(); - $mockCache->expects( $this->never() ) - ->method( 'set' ); + $mockCache->expects( $this->never() )->method( 'set' ); + $mockCache->expects( $this->never() )->method( 'get' ); + $mockCache->expects( $this->never() )->method( 'delete' ); $store = new WatchedItemStore( $this->getMockLoadBalancer( $mockDb ), @@ -876,8 +907,9 @@ class WatchedItemStoreUnitTest extends PHPUnit_Framework_TestCase { ->method( 'selectRow' ); $mockCache = $this->getMockCache(); - $mockCache->expects( $this->never() ) - ->method( 'set' ); + $mockCache->expects( $this->never() )->method( 'get' ); + $mockCache->expects( $this->never() )->method( 'set' ); + $mockCache->expects( $this->never() )->method( 'delete' ); $store = new WatchedItemStore( $this->getMockLoadBalancer( $mockDb ), @@ -908,8 +940,9 @@ class WatchedItemStoreUnitTest extends PHPUnit_Framework_TestCase { ->will( $this->returnValue( [] ) ); $mockCache = $this->getMockCache(); - $mockCache->expects( $this->never() ) - ->method( 'set' ); + $mockCache->expects( $this->never() )->method( 'get' ); + $mockCache->expects( $this->never() )->method( 'set' ); + $mockCache->expects( $this->never() )->method( 'delete' ); $store = new WatchedItemStore( $this->getMockLoadBalancer( $mockDb ), @@ -945,6 +978,7 @@ class WatchedItemStoreUnitTest extends PHPUnit_Framework_TestCase { ) ); $mockCache = $this->getMockCache(); + $mockCache->expects( $this->never() )->method( 'get' ); $mockCache->expects( $this->once() ) ->method( 'set' ) ->with( @@ -986,6 +1020,8 @@ class WatchedItemStoreUnitTest extends PHPUnit_Framework_TestCase { ->method( 'selectRow' ); $mockCache = $this->getMockCache(); + $mockDb->expects( $this->never() ) + ->method( 'get' ); $mockDb->expects( $this->never() ) ->method( 'set' ); $mockDb->expects( $this->never() ) @@ -1048,6 +1084,8 @@ class WatchedItemStoreUnitTest extends PHPUnit_Framework_TestCase { ->method( 'selectRow' ); $mockCache = $this->getMockCache(); + $mockDb->expects( $this->never() ) + ->method( 'get' ); $mockDb->expects( $this->never() ) ->method( 'set' ); $mockDb->expects( $this->never() ) @@ -1104,6 +1142,8 @@ class WatchedItemStoreUnitTest extends PHPUnit_Framework_TestCase { ) ); $mockCache = $this->getMockCache(); + $mockDb->expects( $this->never() ) + ->method( 'get' ); $mockDb->expects( $this->never() ) ->method( 'set' ); $mockDb->expects( $this->never() ) @@ -1182,9 +1222,14 @@ class WatchedItemStoreUnitTest extends PHPUnit_Framework_TestCase { ] ); + $mockCache = $this->getMockCache(); + $mockCache->expects( $this->never() )->method( 'set' ); + $mockCache->expects( $this->never() )->method( 'get' ); + $mockCache->expects( $this->never() )->method( 'delete' ); + $store = new WatchedItemStore( $this->getMockLoadBalancer( $mockDb ), - new HashBagOStuff( [ 'maxKeys' => 100 ] ) + $mockCache ); $this->assertEquals( @@ -1219,9 +1264,14 @@ class WatchedItemStoreUnitTest extends PHPUnit_Framework_TestCase { $mockDb->expects( $this->never() ) ->method( 'update' ); + $mockCache = $this->getMockCache(); + $mockCache->expects( $this->never() )->method( 'set' ); + $mockCache->expects( $this->never() )->method( 'get' ); + $mockCache->expects( $this->never() )->method( 'delete' ); + $store = new WatchedItemStore( $this->getMockLoadBalancer( $mockDb ), - new HashBagOStuff( [ 'maxKeys' => 100 ] ) + $mockCache ); $watchers = $store->updateNotificationTimestamp( @@ -1233,4 +1283,57 @@ class WatchedItemStoreUnitTest extends PHPUnit_Framework_TestCase { $this->assertEmpty( $watchers ); } + public function testUpdateNotificationTimestamp_clearsCachedItems() { + $user = $this->getMockNonAnonUserWithId( 1 ); + $titleValue = new TitleValue( 0, 'SomeDbKey' ); + + $mockDb = $this->getMockDb(); + $mockDb->expects( $this->once() ) + ->method( 'selectRow' ) + ->will( $this->returnValue( + $this->getFakeRow( [ 'wl_notificationtimestamp' => '20151212010101' ] ) + ) ); + $mockDb->expects( $this->once() ) + ->method( 'select' ) + ->will( + $this->returnValue( [ + $this->getFakeRow( [ 'wl_user' => '2' ] ), + $this->getFakeRow( [ 'wl_user' => '3' ] ) + ] ) + ); + $mockDb->expects( $this->once() ) + ->method( 'onTransactionIdle' ) + ->with( $this->isType( 'callable' ) ) + ->will( $this->returnCallback( function( $callable ) { + $callable(); + } ) ); + $mockDb->expects( $this->once() ) + ->method( 'update' ); + + $mockCache = $this->getMockCache(); + $mockCache->expects( $this->once() ) + ->method( 'set' ) + ->with( '0:SomeDbKey:1', $this->isType( 'object' ) ); + $mockCache->expects( $this->once() ) + ->method( 'get' ) + ->with( '0:SomeDbKey:1' ); + $mockCache->expects( $this->once() ) + ->method( 'delete' ) + ->with( '0:SomeDbKey:1' ); + + $store = new WatchedItemStore( + $this->getMockLoadBalancer( $mockDb ), + $mockCache + ); + + // This will add the item to the cache + $store->getWatchedItem( $user, $titleValue ); + + $store->updateNotificationTimestamp( + $this->getMockNonAnonUserWithId( 1 ), + $titleValue, + '20151212010101' + ); + } + } -- 2.20.1