WMDE-leszek has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/284461

Change subject: Assert jobs called from WIS::resetNotificationTimestamp are 
correct
......................................................................

Assert jobs called from WIS::resetNotificationTimestamp are correct

This also adds tests for some previously untested paths.

Change-Id: Id95144c0bad90cfc123a4444a03aad91531d2332
---
M tests/phpunit/includes/WatchedItemStoreUnitTest.php
1 file changed, 276 insertions(+), 6 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/61/284461/1

diff --git a/tests/phpunit/includes/WatchedItemStoreUnitTest.php 
b/tests/phpunit/includes/WatchedItemStoreUnitTest.php
index 17b2fa4..8e9d4d7 100644
--- a/tests/phpunit/includes/WatchedItemStoreUnitTest.php
+++ b/tests/phpunit/includes/WatchedItemStoreUnitTest.php
@@ -2006,6 +2006,33 @@
                return $title;
        }
 
+       private function verifyCallbackJob(
+               $callback,
+               LinkTarget $expectedTitle,
+               $expectedUserId,
+               callable $notificationTimestampCondition
+       ) {
+               $this->assertInternalType( 'callable', $callback );
+
+               $callbackReflector = new ReflectionFunction( $callback );
+               $vars = $callbackReflector->getStaticVariables();
+               $this->assertArrayHasKey( 'job', $vars );
+               $this->assertInstanceOf( ActivityUpdateJob::class, $vars['job'] 
);
+
+               /** @var ActivityUpdateJob $job */
+               $job = $vars['job'];
+               $this->assertEquals( $expectedTitle->getDBkey(), 
$job->getTitle()->getDBkey() );
+               $this->assertEquals( $expectedTitle->getNamespace(), 
$job->getTitle()->getNamespace() );
+
+               $jobParams = $job->getParams();
+               $this->assertArrayHasKey( 'type', $jobParams );
+               $this->assertEquals( 'updateWatchlistNotification', 
$jobParams['type'] );
+               $this->assertArrayHasKey( 'userid', $jobParams );
+               $this->assertEquals( $expectedUserId, $jobParams['userid'] );
+               $this->assertArrayHasKey( 'notifTime', $jobParams );
+               $this->assertTrue( $notificationTimestampCondition( 
$jobParams['notifTime'] ) );
+       }
+
        public function 
testResetNotificationTimestamp_oldidSpecifiedLatestRevisionForced() {
                $user = $this->getMockNonAnonUserWithId( 1 );
                $oldid = 22;
@@ -2032,12 +2059,12 @@
                        $mockCache
                );
 
-               // Note: This does not actually assert the job is correct
                $callableCallCounter = 0;
+               $callback = null;
                $scopedOverride = 
$store->overrideDeferredUpdatesAddCallableUpdateCallback(
-                       function( $callable ) use ( &$callableCallCounter ) {
+                       function( $callable ) use ( &$callableCallCounter, 
&$callback ) {
                                $callableCallCounter++;
-                               $this->assertInternalType( 'callable', 
$callable );
+                               $callback = $callable;
                        }
                );
 
@@ -2050,6 +2077,14 @@
                        )
                );
                $this->assertEquals( 1, $callableCallCounter );
+               $this->verifyCallbackJob(
+                       $callback,
+                       $title,
+                       $user->getId(),
+                       function( $time ) {
+                               return $time === null;
+                       }
+               );
 
                ScopedCallback::consume( $scopedOverride );
        }
@@ -2092,12 +2127,12 @@
                        $mockCache
                );
 
-               // Note: This does not actually assert the job is correct
                $addUpdateCallCounter = 0;
+               $callback = null;
                $scopedOverrideDeferred = 
$store->overrideDeferredUpdatesAddCallableUpdateCallback(
-                       function( $callable ) use ( &$addUpdateCallCounter ) {
+                       function( $callable ) use ( &$addUpdateCallCounter, 
&$callback ) {
                                $addUpdateCallCounter++;
-                               $this->assertInternalType( 'callable', 
$callable );
+                               $callback = $callable;
                        }
                );
 
@@ -2119,6 +2154,241 @@
                        )
                );
                $this->assertEquals( 1, $addUpdateCallCounter );
+               $this->verifyCallbackJob(
+                       $callback,
+                       $title,
+                       $user->getId(),
+                       function( $time ) {
+                               return $time !== null && $time > 
'20151212010101';
+                       }
+               );
+
+               $this->assertEquals( 1, $getTimestampCallCounter );
+
+               ScopedCallback::consume( $scopedOverrideDeferred );
+               ScopedCallback::consume( $scopedOverrideRevision );
+       }
+
+       public function testResetNotificationTimestamp_notWatchedPageForced() {
+               $user = $this->getMockNonAnonUserWithId( 1 );
+               $oldid = 22;
+               $title = $this->getMockTitle( 'SomeDbKey' );
+               $title->expects( $this->once() )
+                       ->method( 'getNextRevisionID' )
+                       ->with( $oldid )
+                       ->will( $this->returnValue( 33 ) );
+
+               $mockDb = $this->getMockDb();
+               $mockDb->expects( $this->once() )
+                       ->method( 'selectRow' )
+                       ->with(
+                               'watchlist',
+                               'wl_notificationtimestamp',
+                               [
+                                       'wl_user' => 1,
+                                       'wl_namespace' => 0,
+                                       'wl_title' => 'SomeDbKey',
+                               ]
+                       )
+                       ->will( $this->returnValue( false ) );
+
+               $mockCache = $this->getMockCache();
+               $mockDb->expects( $this->never() )
+                       ->method( 'get' );
+               $mockDb->expects( $this->never() )
+                       ->method( 'set' );
+               $mockDb->expects( $this->never() )
+                       ->method( 'delete' );
+
+               $store = $this->newWatchedItemStore(
+                       $this->getMockLoadBalancer( $mockDb ),
+                       $mockCache
+               );
+
+               $callableCallCounter = 0;
+               $callback = null;
+               $scopedOverride = 
$store->overrideDeferredUpdatesAddCallableUpdateCallback(
+                       function( $callable ) use ( &$callableCallCounter, 
&$callback ) {
+                               $callableCallCounter++;
+                               $callback = $callable;
+                       }
+               );
+
+               $this->assertTrue(
+                       $store->resetNotificationTimestamp(
+                               $user,
+                               $title,
+                               'force',
+                               $oldid
+                       )
+               );
+               $this->assertEquals( 1, $callableCallCounter );
+               $this->verifyCallbackJob(
+                       $callback,
+                       $title,
+                       $user->getId(),
+                       function( $time ) {
+                               return $time === null;
+                       }
+               );
+
+               ScopedCallback::consume( $scopedOverride );
+       }
+
+       public function 
testResetNotificationTimestamp_futureNotificationTimestampForced() {
+               $user = $this->getMockNonAnonUserWithId( 1 );
+               $oldid = 22;
+               $title = $this->getMockTitle( 'SomeDbKey' );
+               $title->expects( $this->once() )
+                       ->method( 'getNextRevisionID' )
+                       ->with( $oldid )
+                       ->will( $this->returnValue( 33 ) );
+
+               $mockDb = $this->getMockDb();
+               $mockDb->expects( $this->once() )
+                       ->method( 'selectRow' )
+                       ->with(
+                               'watchlist',
+                               'wl_notificationtimestamp',
+                               [
+                                       'wl_user' => 1,
+                                       'wl_namespace' => 0,
+                                       'wl_title' => 'SomeDbKey',
+                               ]
+                       )
+                       ->will( $this->returnValue(
+                               $this->getFakeRow( [ 'wl_notificationtimestamp' 
=> '30151212010101' ] )
+                       ) );
+
+               $mockCache = $this->getMockCache();
+               $mockDb->expects( $this->never() )
+                       ->method( 'get' );
+               $mockDb->expects( $this->never() )
+                       ->method( 'set' );
+               $mockDb->expects( $this->never() )
+                       ->method( 'delete' );
+
+               $store = $this->newWatchedItemStore(
+                       $this->getMockLoadBalancer( $mockDb ),
+                       $mockCache
+               );
+
+               $addUpdateCallCounter = 0;
+               $callback = null;
+               $scopedOverrideDeferred = 
$store->overrideDeferredUpdatesAddCallableUpdateCallback(
+                       function( $callable ) use ( &$addUpdateCallCounter, 
&$callback ) {
+                               $addUpdateCallCounter++;
+                               $callback = $callable;
+                       }
+               );
+
+               $getTimestampCallCounter = 0;
+               $scopedOverrideRevision = 
$store->overrideRevisionGetTimestampFromIdCallback(
+                       function( $titleParam, $oldidParam ) use ( 
&$getTimestampCallCounter, $title, $oldid ) {
+                               $getTimestampCallCounter++;
+                               $this->assertEquals( $title, $titleParam );
+                               $this->assertEquals( $oldid, $oldidParam );
+                       }
+               );
+
+               $this->assertTrue(
+                       $store->resetNotificationTimestamp(
+                               $user,
+                               $title,
+                               'force',
+                               $oldid
+                       )
+               );
+               $this->assertEquals( 1, $addUpdateCallCounter );
+               $this->verifyCallbackJob(
+                       $callback,
+                       $title,
+                       $user->getId(),
+                       function( $time ) {
+                               return $time === '30151212010101';
+                       }
+               );
+
+               $this->assertEquals( 1, $getTimestampCallCounter );
+
+               ScopedCallback::consume( $scopedOverrideDeferred );
+               ScopedCallback::consume( $scopedOverrideRevision );
+       }
+
+       public function 
testResetNotificationTimestamp_futureNotificationTimestampNotForced() {
+               $user = $this->getMockNonAnonUserWithId( 1 );
+               $oldid = 22;
+               $title = $this->getMockTitle( 'SomeDbKey' );
+               $title->expects( $this->once() )
+                       ->method( 'getNextRevisionID' )
+                       ->with( $oldid )
+                       ->will( $this->returnValue( 33 ) );
+
+               $mockDb = $this->getMockDb();
+               $mockDb->expects( $this->once() )
+                       ->method( 'selectRow' )
+                       ->with(
+                               'watchlist',
+                               'wl_notificationtimestamp',
+                               [
+                                       'wl_user' => 1,
+                                       'wl_namespace' => 0,
+                                       'wl_title' => 'SomeDbKey',
+                               ]
+                       )
+                       ->will( $this->returnValue(
+                               $this->getFakeRow( [ 'wl_notificationtimestamp' 
=> '30151212010101' ] )
+                       ) );
+
+               $mockCache = $this->getMockCache();
+               $mockDb->expects( $this->never() )
+                       ->method( 'get' );
+               $mockDb->expects( $this->never() )
+                       ->method( 'set' );
+               $mockDb->expects( $this->never() )
+                       ->method( 'delete' );
+
+               $store = $this->newWatchedItemStore(
+                       $this->getMockLoadBalancer( $mockDb ),
+                       $mockCache
+               );
+
+               $addUpdateCallCounter = 0;
+               $callback = null;
+               $scopedOverrideDeferred = 
$store->overrideDeferredUpdatesAddCallableUpdateCallback(
+                       function( $callable ) use ( &$addUpdateCallCounter, 
&$callback ) {
+                               $addUpdateCallCounter++;
+                               $callback = $callable;
+                       }
+               );
+
+               $getTimestampCallCounter = 0;
+               $scopedOverrideRevision = 
$store->overrideRevisionGetTimestampFromIdCallback(
+                       function( $titleParam, $oldidParam ) use ( 
&$getTimestampCallCounter, $title, $oldid ) {
+                               $getTimestampCallCounter++;
+                               $this->assertEquals( $title, $titleParam );
+                               $this->assertEquals( $oldid, $oldidParam );
+                       }
+               );
+
+               $this->assertTrue(
+                       $store->resetNotificationTimestamp(
+                               $user,
+                               $title,
+                               '',
+                               $oldid
+                       )
+               );
+               $this->assertEquals( 1, $addUpdateCallCounter );
+               $this->verifyCallbackJob(
+                       $callback,
+                       $title,
+                       $user->getId(),
+                       function( $time ) {
+                               return $time === false;
+                       }
+               );
+
                $this->assertEquals( 1, $getTimestampCallCounter );
 
                ScopedCallback::consume( $scopedOverrideDeferred );

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Id95144c0bad90cfc123a4444a03aad91531d2332
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: WMDE-leszek <leszek.mani...@wikimedia.de>

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

Reply via email to