mediawiki-extensions-Echo/tests/phpunit/NotifUserTest.php
Matthew Flaschen cac85b635f Make plural support for large values (100 or more) explicit in l10n
This involves:

* Making this value no longer admin-configurable.
* Changing getNotificationCountForOutput to return only a single value
  Since there is no + in the formatted value anymore, we can actually
  use the same value for both.

  This is a B/C break, but hopefully worth it to simplify the method
  call.

  For now, the excess parameter is just marked unused.  It could be
  removed at some point if the translations are updated.

This must be merged at the same time as:
* Flow - Ibfa56b1af9e8c56b4c5f900e0d487bc09688b2a2
* MobileFrontend - Ibf784b279d56773a227ff261b75f2b26125bbb63 (well, MF
  can be merged first)
* translatewiki - I2a4b6938aed49e4101deb6b9351c43656a863490

Also, change 1 to One/one, per Siebrand on the task.  This can easily
be dropped/undone if we don't want it.

Also, remove reference to no-longer-existent notification-page-linked-bundle

Bug: T127288
Change-Id: Iabeaae747f99980c0610d552f6b38f89d940b890
2016-03-16 18:43:16 -04:00

208 lines
6.1 KiB
PHP

<?php
/**
* @group Echo
*/
class MWEchoNotifUserTest extends MediaWikiTestCase {
protected function setUp() {
parent::setUp();
$this->setMwGlobals( 'wgMemc', new HashBagOStuff() );
}
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() {
global $wgMemc;
$notifUser = MWEchoNotifUser::newFromUser( User::newFromId( 2 ) );
$notifUser->flagCacheWithNewTalkNotification();
$this->assertEquals( '1', $wgMemc->get( $notifUser->getTalkNotificationCacheKey() ) );
}
public function testFlagCacheWithNoTalkNotification() {
global $wgMemc;
$notifUser = MWEchoNotifUser::newFromUser( User::newFromId( 2 ) );
$notifUser->flagCacheWithNoTalkNotification();
$this->assertEquals( '0', $wgMemc->get( $notifUser->getTalkNotificationCacheKey() ) );
}
public function testNotifCountHasReachedMax() {
$notifUser = MWEchoNotifUser::newFromUser( User::newFromId( 2 ) );
if ( $notifUser->getNotificationCount() > MWEchoNotifUser::MAX_BADGE_COUNT ) {
$this->assertTrue( $notifUser->notifCountHasReachedMax() );
} else {
$this->assertFalse( $notifUser->notifCountHasReachedMax() );
}
}
public function testClearTalkNotification() {
global $wgMemc;
$notifUser = MWEchoNotifUser::newFromUser( User::newFromId( 2 ) );
$notifUser->flagCacheWithNewTalkNotification();
$hasMax = $notifUser->notifCountHasReachedMax();
$notifUser->clearTalkNotification();
if ( $hasMax ) {
$this->assertEquals( '1', $wgMemc->get( $notifUser->getTalkNotificationCacheKey() ) );
} else {
$this->assertEquals( '0', $wgMemc->get( $notifUser->getTalkNotificationCacheKey() ) );
}
}
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 );
}
public function testMarkRead() {
global $wgMemc;
$notifUser = new MWEchoNotifUser(
User::newFromId( 2 ),
$wgMemc,
$this->mockEchoUserNotificationGateway( array( 'markRead' => true ) ),
$this->mockEchoNotificationMapper(),
$this->mockEchoTargetPageMapper()
);
$this->assertFalse( $notifUser->markRead( array() ) );
$this->assertTrue( $notifUser->markRead( array( 1 ) ) );
$notifUser = new MWEchoNotifUser(
User::newFromId( 2 ),
$wgMemc,
$this->mockEchoUserNotificationGateway( array( 'markRead' => false ) ),
$this->mockEchoNotificationMapper(),
$this->mockEchoTargetPageMapper()
);
$this->assertFalse( $notifUser->markRead( array() ) );
$this->assertFalse( $notifUser->markRead( array( 1 ) ) );
}
public function testMarkAllRead() {
global $wgMemc;
// Successful mark as read & non empty fetch
$notifUser = new MWEchoNotifUser(
User::newFromId( 2 ),
$wgMemc,
$this->mockEchoUserNotificationGateway( array( 'markRead' => true ) ),
$this->mockEchoNotificationMapper( array( $this->mockEchoNotification() ) ),
$this->mockEchoTargetPageMapper()
);
$this->assertTrue( $notifUser->markAllRead() );
// Unsuccessful mark as read & non empty fetch
$notifUser = new MWEchoNotifUser(
User::newFromId( 2 ),
$wgMemc,
$this->mockEchoUserNotificationGateway( array( 'markRead' => false ) ),
$this->mockEchoNotificationMapper( array( $this->mockEchoNotification() ) ),
$this->mockEchoTargetPageMapper()
);
$this->assertFalse( $notifUser->markAllRead() );
// Successful mark as read & empty fetch
$notifUser = new MWEchoNotifUser(
User::newFromId( 2 ),
$wgMemc,
$this->mockEchoUserNotificationGateway( array( 'markRead' => true ) ),
$this->mockEchoNotificationMapper(),
$this->mockEchoTargetPageMapper()
);
$this->assertFalse( $notifUser->markAllRead() );
// Unsuccessful mark as read & empty fetch
$notifUser = new MWEchoNotifUser(
User::newFromId( 2 ),
$wgMemc,
$this->mockEchoUserNotificationGateway( array( 'markRead' => false ) ),
$this->mockEchoNotificationMapper(),
$this->mockEchoTargetPageMapper()
);
$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'] ) );
return $gateway;
}
public function mockEchoNotificationMapper( array $result = array() ) {
$mapper = $this->getMockBuilder( 'EchoNotificationMapper' )
->disableOriginalConstructor()
->getMock();
$mapper->expects( $this->any() )
->method( 'fetchUnreadByUser' )
->will( $this->returnValue( $result ) );
return $mapper;
}
public function mockEchoTargetPageMapper( array $result = array() ) {
$mapper = $this->getMockBuilder( 'EchoTargetPageMapper' )
->disableOriginalConstructor()
->getMock();
$mapper->expects( $this->any() )
->method( 'deleteByUserEvents' )
->will( $this->returnValue( $result ) );
return $mapper;
}
protected function mockEchoNotification() {
$notification = $this->getMockBuilder( 'EchoNotification' )
->disableOriginalConstructor()
->getMock();
$notification->expects( $this->any() )
->method( 'getEvent' )
->will( $this->returnValue( $this->mockEchoEvent() ) );
return $notification;
}
protected function mockEchoEvent() {
$event = $this->getMockBuilder( 'EchoEvent' )
->disableOriginalConstructor()
->getMock();
$event->expects( $this->any() )
->method( 'getId' )
->will( $this->returnValue( 1 ) );
return $event;
}
}