| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Notifications\Test; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use MediaWiki\Extension\Notifications\EmailFormat; |
| 7 |
use MediaWiki\Extension\Notifications\Gateway\UserNotificationGateway; |
| 8 |
use MediaWiki\Extension\Notifications\Mapper\NotificationMapper; |
| 9 |
use MediaWiki\Extension\Notifications\Mapper\TargetPageMapper; |
| 10 |
use MediaWiki\Extension\Notifications\Model\Event; |
| 11 |
use MediaWiki\Extension\Notifications\Model\Notification; |
| 12 |
use MediaWiki\Extension\Notifications\NotifUser; |
| 13 |
use MediaWiki\MainConfigNames; |
| 14 |
use MediaWiki\User\Options\UserOptionsLookup; |
| 15 |
use MediaWiki\User\TalkPageNotificationManager; |
| 16 |
use MediaWiki\User\User; |
| 17 |
use MediaWiki\User\UserFactory; |
| 18 |
use MediaWiki\User\UserGroupManager; |
| 19 |
use MediaWikiIntegrationTestCase; |
| 20 |
use Wikimedia\ObjectCache\WANObjectCache; |
| 21 |
use Wikimedia\Rdbms\IDatabase; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
class NotifUserTest extends MediaWikiIntegrationTestCase { |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
private $cache; |
| 33 |
|
| 34 |
protected function setUp(): void { |
| 35 |
parent::setUp(); |
| 36 |
$this->cache = new WANObjectCache( [ |
| 37 |
'cache' => $this->getServiceContainer()->getMainObjectStash(), |
| 38 |
] ); |
| 39 |
} |
| 40 |
|
| 41 |
public function testNewFromUser() { |
| 42 |
$exception = false; |
| 43 |
try { |
| 44 |
NotifUser::newFromUser( User::newFromId( 0 ) ); |
| 45 |
} catch ( Exception $e ) { |
| 46 |
$exception = true; |
| 47 |
$this->assertEquals( "User must be logged in to view notification!", |
| 48 |
$e->getMessage() ); |
| 49 |
} |
| 50 |
$this->assertTrue( $exception, "Got exception" ); |
| 51 |
|
| 52 |
$notifUser = NotifUser::newFromUser( User::newFromId( 2 ) ); |
| 53 |
$this->assertInstanceOf( NotifUser::class, $notifUser ); |
| 54 |
} |
| 55 |
|
| 56 |
public function testGetEmailFormat() { |
| 57 |
$user = User::newFromId( 2 ); |
| 58 |
$pref = 'foo'; |
| 59 |
$userOptionsLookup = $this->createMock( UserOptionsLookup::class ); |
| 60 |
$userOptionsLookup->expects( $this->atLeastOnce() ) |
| 61 |
->method( 'getOption' ) |
| 62 |
->with( $user, 'echo-email-format' ) |
| 63 |
->willReturn( $pref ); |
| 64 |
$this->setService( 'UserOptionsLookup', $userOptionsLookup ); |
| 65 |
$notifUser = NotifUser::newFromUser( $user ); |
| 66 |
|
| 67 |
$this->overrideConfigValue( MainConfigNames::AllowHTMLEmail, true ); |
| 68 |
$this->assertEquals( $notifUser->getEmailFormat(), $pref ); |
| 69 |
$this->overrideConfigValue( MainConfigNames::AllowHTMLEmail, false ); |
| 70 |
$this->assertEquals( EmailFormat::PLAIN_TEXT, $notifUser->getEmailFormat() ); |
| 71 |
} |
| 72 |
|
| 73 |
public function testMarkRead() { |
| 74 |
$this->setService( 'UserFactory', $this->createMock( UserFactory::class ) ); |
| 75 |
$this->setService( 'TalkPageNotificationManager', $this->createMock( TalkPageNotificationManager::class ) ); |
| 76 |
$notifUser = new NotifUser( |
| 77 |
User::newFromId( 2 ), |
| 78 |
$this->cache, |
| 79 |
$this->mockUserNotificationGateway( [ 'markRead' => true ] ), |
| 80 |
$this->mockNotificationMapper(), |
| 81 |
$this->createMock( TargetPageMapper::class ), |
| 82 |
$this->createNoOpMock( UserOptionsLookup::class ), |
| 83 |
$this->getServiceContainer()->getUserFactory(), |
| 84 |
$this->getServiceContainer()->getReadOnlyMode() |
| 85 |
); |
| 86 |
$this->assertFalse( $notifUser->markRead( [] ) ); |
| 87 |
$this->assertTrue( $notifUser->markRead( [ 1 ] ) ); |
| 88 |
|
| 89 |
$notifUser = new NotifUser( |
| 90 |
User::newFromId( 2 ), |
| 91 |
$this->cache, |
| 92 |
$this->mockUserNotificationGateway( [ 'markRead' => false ] ), |
| 93 |
$this->mockNotificationMapper(), |
| 94 |
$this->createMock( TargetPageMapper::class ), |
| 95 |
$this->createNoOpMock( UserOptionsLookup::class ), |
| 96 |
$this->getServiceContainer()->getUserFactory(), |
| 97 |
$this->getServiceContainer()->getReadOnlyMode() |
| 98 |
); |
| 99 |
$this->assertFalse( $notifUser->markRead( [] ) ); |
| 100 |
$this->assertFalse( $notifUser->markRead( [ 1 ] ) ); |
| 101 |
} |
| 102 |
|
| 103 |
public function testMarkAllRead() { |
| 104 |
$this->setService( 'UserFactory', $this->createMock( UserFactory::class ) ); |
| 105 |
$this->setService( 'TalkPageNotificationManager', $this->createMock( TalkPageNotificationManager::class ) ); |
| 106 |
$this->setService( 'UserGroupManager', $this->createMock( UserGroupManager::class ) ); |
| 107 |
$this->setService( 'UserOptionsLookup', $this->createMock( UserOptionsLookup::class ) ); |
| 108 |
|
| 109 |
$notifUser = new NotifUser( |
| 110 |
User::newFromId( 2 ), |
| 111 |
$this->cache, |
| 112 |
$this->mockUserNotificationGateway( [ 'markRead' => true ] ), |
| 113 |
$this->mockNotificationMapper( [ $this->mockNotification() ] ), |
| 114 |
$this->createMock( TargetPageMapper::class ), |
| 115 |
$this->createNoOpMock( UserOptionsLookup::class ), |
| 116 |
$this->getServiceContainer()->getUserFactory(), |
| 117 |
$this->getServiceContainer()->getReadOnlyMode() |
| 118 |
); |
| 119 |
$this->assertTrue( $notifUser->markAllRead() ); |
| 120 |
|
| 121 |
|
| 122 |
$notifUser = new NotifUser( |
| 123 |
User::newFromId( 2 ), |
| 124 |
$this->cache, |
| 125 |
$this->mockUserNotificationGateway( [ 'markRead' => false ] ), |
| 126 |
$this->mockNotificationMapper( [ $this->mockNotification() ] ), |
| 127 |
$this->createMock( TargetPageMapper::class ), |
| 128 |
$this->createNoOpMock( UserOptionsLookup::class ), |
| 129 |
$this->getServiceContainer()->getUserFactory(), |
| 130 |
$this->getServiceContainer()->getReadOnlyMode() |
| 131 |
); |
| 132 |
$this->assertFalse( $notifUser->markAllRead() ); |
| 133 |
|
| 134 |
|
| 135 |
$notifUser = new NotifUser( |
| 136 |
User::newFromId( 2 ), |
| 137 |
$this->cache, |
| 138 |
$this->mockUserNotificationGateway( [ 'markRead' => true ] ), |
| 139 |
$this->mockNotificationMapper(), |
| 140 |
$this->createMock( TargetPageMapper::class ), |
| 141 |
$this->createNoOpMock( UserOptionsLookup::class ), |
| 142 |
$this->getServiceContainer()->getUserFactory(), |
| 143 |
$this->getServiceContainer()->getReadOnlyMode() |
| 144 |
); |
| 145 |
$this->assertFalse( $notifUser->markAllRead() ); |
| 146 |
|
| 147 |
|
| 148 |
$notifUser = new NotifUser( |
| 149 |
User::newFromId( 2 ), |
| 150 |
$this->cache, |
| 151 |
$this->mockUserNotificationGateway( [ 'markRead' => false ] ), |
| 152 |
$this->mockNotificationMapper(), |
| 153 |
$this->createMock( TargetPageMapper::class ), |
| 154 |
$this->createNoOpMock( UserOptionsLookup::class ), |
| 155 |
$this->getServiceContainer()->getUserFactory(), |
| 156 |
$this->getServiceContainer()->getReadOnlyMode() |
| 157 |
); |
| 158 |
$this->assertFalse( $notifUser->markAllRead() ); |
| 159 |
} |
| 160 |
|
| 161 |
public function mockUserNotificationGateway( array $dbResult = [] ) { |
| 162 |
$dbResult += [ |
| 163 |
'markRead' => true |
| 164 |
]; |
| 165 |
$gateway = $this->createMock( UserNotificationGateway::class ); |
| 166 |
$gateway->method( 'markRead' ) |
| 167 |
->willReturn( $dbResult['markRead'] ); |
| 168 |
$gateway->method( 'getDB' ) |
| 169 |
->willReturn( $this->createMock( IDatabase::class ) ); |
| 170 |
|
| 171 |
return $gateway; |
| 172 |
} |
| 173 |
|
| 174 |
public function mockNotificationMapper( array $result = [] ) { |
| 175 |
$mapper = $this->createMock( NotificationMapper::class ); |
| 176 |
$mapper->method( 'fetchUnreadByUser' ) |
| 177 |
->willReturn( $result ); |
| 178 |
|
| 179 |
return $mapper; |
| 180 |
} |
| 181 |
|
| 182 |
protected function mockNotification() { |
| 183 |
$notification = $this->createMock( Notification::class ); |
| 184 |
$notification->method( 'getEvent' ) |
| 185 |
->willReturn( $this->mockEvent() ); |
| 186 |
|
| 187 |
return $notification; |
| 188 |
} |
| 189 |
|
| 190 |
protected function mockEvent() { |
| 191 |
$event = $this->createMock( Event::class ); |
| 192 |
$event->method( 'getId' ) |
| 193 |
->willReturn( 1 ); |
| 194 |
|
| 195 |
return $event; |
| 196 |
} |
| 197 |
|
| 198 |
protected function newNotifUser() { |
| 199 |
return new NotifUser( |
| 200 |
User::newFromId( 2 ), |
| 201 |
$this->cache, |
| 202 |
$this->mockUserNotificationGateway(), |
| 203 |
$this->mockNotificationMapper(), |
| 204 |
$this->createMock( TargetPageMapper::class ), |
| 205 |
$this->createNoOpMock( UserOptionsLookup::class ), |
| 206 |
$this->getServiceContainer()->getUserFactory(), |
| 207 |
$this->getServiceContainer()->getReadOnlyMode() |
| 208 |
); |
| 209 |
} |
| 210 |
} |