2013-05-24 22:51:47 +00:00
|
|
|
<?php
|
|
|
|
|
2014-07-25 20:19:15 +00:00
|
|
|
/**
|
|
|
|
* @group Echo
|
|
|
|
*/
|
2013-05-24 22:51:47 +00:00
|
|
|
class MWEchoNotifUserTest extends MediaWikiTestCase {
|
|
|
|
|
2015-11-01 09:59:16 +00:00
|
|
|
/**
|
|
|
|
* @var BagOStuff
|
|
|
|
*/
|
|
|
|
private $cache;
|
|
|
|
|
2013-05-24 22:51:47 +00:00
|
|
|
protected function setUp() {
|
|
|
|
parent::setUp();
|
2015-11-01 09:59:16 +00:00
|
|
|
$this->cache = ObjectCache::getMainStashInstance();
|
2013-05-24 22:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testNewFromUser() {
|
|
|
|
$exception = false;
|
|
|
|
try {
|
|
|
|
MWEchoNotifUser::newFromUser( User::newFromId( 0 ) );
|
|
|
|
} catch ( Exception $e ) {
|
|
|
|
$exception = true;
|
|
|
|
$this->assertEquals( "User must be logged in to view notification!",
|
|
|
|
$e->getMessage() );
|
|
|
|
}
|
|
|
|
$this->assertTrue( $exception, "Got exception" );
|
|
|
|
|
|
|
|
$notifUser = MWEchoNotifUser::newFromUser( User::newFromId( 2 ) );
|
|
|
|
$this->assertInstanceOf( 'MWEchoNotifUser', $notifUser );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFlagCacheWithNewTalkNotification() {
|
|
|
|
$notifUser = MWEchoNotifUser::newFromUser( User::newFromId( 2 ) );
|
|
|
|
|
|
|
|
$notifUser->flagCacheWithNewTalkNotification();
|
2015-11-01 09:59:16 +00:00
|
|
|
$this->assertEquals( '1', $this->cache->get( $notifUser->getTalkNotificationCacheKey() ) );
|
2013-05-24 22:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testFlagCacheWithNoTalkNotification() {
|
|
|
|
$notifUser = MWEchoNotifUser::newFromUser( User::newFromId( 2 ) );
|
|
|
|
|
|
|
|
$notifUser->flagCacheWithNoTalkNotification();
|
2015-11-01 09:59:16 +00:00
|
|
|
$this->assertEquals( '0', $this->cache->get( $notifUser->getTalkNotificationCacheKey() ) );
|
2013-05-24 22:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testNotifCountHasReachedMax() {
|
|
|
|
$notifUser = MWEchoNotifUser::newFromUser( User::newFromId( 2 ) );
|
|
|
|
|
NotifUser: Refactor getNotificationCount() and friends, add caching for global counts
Previously, getNotificationCount() only looked at local notifications,
and foreign notifications were added in separately by getMessageCount()
and getAlertCount(). This didn't make any sense and resulted in
counter-intuitive things like I4d49b543.
Instead, add a $global flag to getNotificationCount(). If $global=false,
the local count is returned as before, but if $global=true, the
global count (=local+foreign) is returned. If $global is omitted,
the user's cross-wiki notification preference determines which is returned.
Update getLastUnreadNotificationCount() in the same way, since it had
the same issues.
Also add caching for global counts and timestamps, using a global
memc key.
Bug: T133623
Change-Id: If78bfc710acd91a075771b565cc99f4c302a104d
2016-04-27 07:12:32 +00:00
|
|
|
if ( $notifUser->getLocalNotificationCount() > MWEchoNotifUser::MAX_BADGE_COUNT ) {
|
2013-05-24 22:51:47 +00:00
|
|
|
$this->assertTrue( $notifUser->notifCountHasReachedMax() );
|
|
|
|
} else {
|
|
|
|
$this->assertFalse( $notifUser->notifCountHasReachedMax() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testClearTalkNotification() {
|
|
|
|
$notifUser = MWEchoNotifUser::newFromUser( User::newFromId( 2 ) );
|
|
|
|
|
|
|
|
$notifUser->flagCacheWithNewTalkNotification();
|
|
|
|
|
|
|
|
$hasMax = $notifUser->notifCountHasReachedMax();
|
|
|
|
|
|
|
|
$notifUser->clearTalkNotification();
|
|
|
|
if ( $hasMax ) {
|
2015-11-01 09:59:16 +00:00
|
|
|
$this->assertEquals( '1', $this->cache->get( $notifUser->getTalkNotificationCacheKey() ) );
|
2013-05-24 22:51:47 +00:00
|
|
|
} else {
|
2015-11-01 09:59:16 +00:00
|
|
|
$this->assertEquals( '0', $this->cache->get( $notifUser->getTalkNotificationCacheKey() ) );
|
2013-05-24 22:51:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-24 00:22:08 +00:00
|
|
|
public function testGetEmailFormat() {
|
|
|
|
$user = User::newFromId( 2 );
|
|
|
|
$notifUser = MWEchoNotifUser::newFromUser( $user );
|
|
|
|
|
|
|
|
$this->setMwGlobals( 'wgAllowHTMLEmail', true );
|
|
|
|
$this->assertEquals( $notifUser->getEmailFormat(), $user->getOption( 'echo-email-format' ) );
|
|
|
|
$this->setMwGlobals( 'wgAllowHTMLEmail', false );
|
|
|
|
$this->assertEquals( $notifUser->getEmailFormat(), EchoHooks::EMAIL_FORMAT_PLAIN_TEXT );
|
|
|
|
}
|
|
|
|
|
2014-08-13 22:00:25 +00:00
|
|
|
public function testMarkRead() {
|
|
|
|
$notifUser = new MWEchoNotifUser(
|
|
|
|
User::newFromId( 2 ),
|
2015-11-01 09:59:16 +00:00
|
|
|
$this->cache,
|
2014-08-13 22:00:25 +00:00
|
|
|
$this->mockEchoUserNotificationGateway( array( 'markRead' => true ) ),
|
2014-08-07 00:07:34 +00:00
|
|
|
$this->mockEchoNotificationMapper(),
|
|
|
|
$this->mockEchoTargetPageMapper()
|
2014-08-13 22:00:25 +00:00
|
|
|
);
|
|
|
|
$this->assertFalse( $notifUser->markRead( array() ) );
|
|
|
|
$this->assertTrue( $notifUser->markRead( array( 1 ) ) );
|
|
|
|
|
|
|
|
$notifUser = new MWEchoNotifUser(
|
|
|
|
User::newFromId( 2 ),
|
2015-11-01 09:59:16 +00:00
|
|
|
$this->cache,
|
2014-08-13 22:00:25 +00:00
|
|
|
$this->mockEchoUserNotificationGateway( array( 'markRead' => false ) ),
|
2014-08-07 00:07:34 +00:00
|
|
|
$this->mockEchoNotificationMapper(),
|
|
|
|
$this->mockEchoTargetPageMapper()
|
2014-08-13 22:00:25 +00:00
|
|
|
);
|
|
|
|
$this->assertFalse( $notifUser->markRead( array() ) );
|
|
|
|
$this->assertFalse( $notifUser->markRead( array( 1 ) ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMarkAllRead() {
|
|
|
|
// Successful mark as read & non empty fetch
|
|
|
|
$notifUser = new MWEchoNotifUser(
|
|
|
|
User::newFromId( 2 ),
|
2015-11-01 09:59:16 +00:00
|
|
|
$this->cache,
|
2014-08-13 22:00:25 +00:00
|
|
|
$this->mockEchoUserNotificationGateway( array( 'markRead' => true ) ),
|
2014-08-07 00:07:34 +00:00
|
|
|
$this->mockEchoNotificationMapper( array( $this->mockEchoNotification() ) ),
|
|
|
|
$this->mockEchoTargetPageMapper()
|
2014-08-13 22:00:25 +00:00
|
|
|
);
|
|
|
|
$this->assertTrue( $notifUser->markAllRead() );
|
|
|
|
|
|
|
|
// Unsuccessful mark as read & non empty fetch
|
|
|
|
$notifUser = new MWEchoNotifUser(
|
|
|
|
User::newFromId( 2 ),
|
2015-11-01 09:59:16 +00:00
|
|
|
$this->cache,
|
2014-08-13 22:00:25 +00:00
|
|
|
$this->mockEchoUserNotificationGateway( array( 'markRead' => false ) ),
|
2014-08-07 00:07:34 +00:00
|
|
|
$this->mockEchoNotificationMapper( array( $this->mockEchoNotification() ) ),
|
|
|
|
$this->mockEchoTargetPageMapper()
|
2014-08-13 22:00:25 +00:00
|
|
|
);
|
|
|
|
$this->assertFalse( $notifUser->markAllRead() );
|
|
|
|
|
|
|
|
// Successful mark as read & empty fetch
|
|
|
|
$notifUser = new MWEchoNotifUser(
|
|
|
|
User::newFromId( 2 ),
|
2015-11-01 09:59:16 +00:00
|
|
|
$this->cache,
|
2014-08-13 22:00:25 +00:00
|
|
|
$this->mockEchoUserNotificationGateway( array( 'markRead' => true ) ),
|
2014-08-07 00:07:34 +00:00
|
|
|
$this->mockEchoNotificationMapper(),
|
|
|
|
$this->mockEchoTargetPageMapper()
|
2014-08-13 22:00:25 +00:00
|
|
|
);
|
|
|
|
$this->assertFalse( $notifUser->markAllRead() );
|
|
|
|
|
|
|
|
// Unsuccessful mark as read & empty fetch
|
|
|
|
$notifUser = new MWEchoNotifUser(
|
|
|
|
User::newFromId( 2 ),
|
2015-11-01 09:59:16 +00:00
|
|
|
$this->cache,
|
2014-08-13 22:00:25 +00:00
|
|
|
$this->mockEchoUserNotificationGateway( array( 'markRead' => false ) ),
|
2014-08-07 00:07:34 +00:00
|
|
|
$this->mockEchoNotificationMapper(),
|
|
|
|
$this->mockEchoTargetPageMapper()
|
2014-08-13 22:00:25 +00:00
|
|
|
);
|
|
|
|
$this->assertFalse( $notifUser->markAllRead() );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function mockEchoUserNotificationGateway( array $dbResult = array() ) {
|
|
|
|
$dbResult += array(
|
|
|
|
'markRead' => true
|
|
|
|
);
|
|
|
|
$gateway = $this->getMockBuilder( 'EchoUserNotificationGateway' )
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$gateway->expects( $this->any() )
|
|
|
|
->method( 'markRead' )
|
|
|
|
->will( $this->returnValue( $dbResult['markRead'] ) );
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2014-08-13 22:00:25 +00:00
|
|
|
return $gateway;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function mockEchoNotificationMapper( array $result = array() ) {
|
|
|
|
$mapper = $this->getMockBuilder( 'EchoNotificationMapper' )
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$mapper->expects( $this->any() )
|
|
|
|
->method( 'fetchUnreadByUser' )
|
|
|
|
->will( $this->returnValue( $result ) );
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2014-08-13 22:00:25 +00:00
|
|
|
return $mapper;
|
|
|
|
}
|
|
|
|
|
2014-08-07 00:07:34 +00:00
|
|
|
public function mockEchoTargetPageMapper( array $result = array() ) {
|
|
|
|
$mapper = $this->getMockBuilder( 'EchoTargetPageMapper' )
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$mapper->expects( $this->any() )
|
|
|
|
->method( 'deleteByUserEvents' )
|
|
|
|
->will( $this->returnValue( $result ) );
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2014-08-07 00:07:34 +00:00
|
|
|
return $mapper;
|
|
|
|
}
|
|
|
|
|
2014-08-13 22:00:25 +00:00
|
|
|
protected function mockEchoNotification() {
|
|
|
|
$notification = $this->getMockBuilder( 'EchoNotification' )
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$notification->expects( $this->any() )
|
|
|
|
->method( 'getEvent' )
|
|
|
|
->will( $this->returnValue( $this->mockEchoEvent() ) );
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2014-08-13 22:00:25 +00:00
|
|
|
return $notification;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function mockEchoEvent() {
|
|
|
|
$event = $this->getMockBuilder( 'EchoEvent' )
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$event->expects( $this->any() )
|
|
|
|
->method( 'getId' )
|
|
|
|
->will( $this->returnValue( 1 ) );
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2014-08-13 22:00:25 +00:00
|
|
|
return $event;
|
|
|
|
}
|
2013-05-24 22:51:47 +00:00
|
|
|
}
|