Debian Codegraph @

extensions/Echo/tests/phpunit/unit/Gateway/UserNotificationGatewayTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\Notifications\Test\Unit;
4
5 use MediaWiki\Config\HashConfig;
6 use MediaWiki\Extension\Notifications\DbFactory;
7 use MediaWiki\Extension\Notifications\Gateway\UserNotificationGateway;
8 use MediaWiki\User\User;
9 use MediaWikiUnitTestCase;
10 use Wikimedia\Rdbms\FakeResultWrapper;
11 use Wikimedia\Rdbms\IDatabase;
12 use Wikimedia\Rdbms\SelectQueryBuilder;
13 use Wikimedia\Rdbms\UpdateQueryBuilder;
14
15 /**
16 * @covers \MediaWiki\Extension\Notifications\Gateway\UserNotificationGateway
17 */
18 class UserNotificationGatewayTest extends MediaWikiUnitTestCase {
19
20 public function testMarkRead() {
21 // no event ids to mark
22 $gateway = new UserNotificationGateway(
23 $this->mockUser(),
24 $this->mockDbFactory(),
25 $this->mockConfig()
26 );
27 $this->assertFalse( $gateway->markRead( [] ) );
28
29 // successful update
30 $gateway = new UserNotificationGateway(
31 $this->mockUser(),
32 $this->mockDbFactory( [ 'update' => true ] ),
33 $this->mockConfig()
34 );
35 $this->assertTrue( $gateway->markRead( [ 2 ] ) );
36
37 // unsuccessful update
38 $gateway = new UserNotificationGateway(
39 $this->mockUser(),
40 $this->mockDbFactory( [ 'update' => false ] ),
41 $this->mockConfig()
42 );
43 $this->assertFalse( $gateway->markRead( [ 2 ] ) );
44 }
45
46 public function testMarkAllRead() {
47 // successful update
48 $gateway = new UserNotificationGateway(
49 $this->mockUser(),
50 $this->mockDbFactory( [ 'update' => true ] ),
51 $this->mockConfig()
52 );
53 $this->assertTrue( $gateway->markAllRead( [ 2 ] ) );
54
55 // null update
56 $gateway = new UserNotificationGateway(
57 $this->mockUser(),
58 $this->mockDbFactory( [ 'update' => false ] ),
59 $this->mockConfig()
60 );
61 $this->assertTrue( $gateway->markAllRead( [ 2 ] ) );
62 }
63
64 public function testGetNotificationCount() {
65 // unsuccessful select
66 $gateway = new UserNotificationGateway(
67 $this->mockUser(),
68 $this->mockDbFactory( [ 'selectRowCount' => 0 ] ),
69 $this->mockConfig()
70 );
71 $this->assertSame( 0, $gateway->getCappedNotificationCount( DB_REPLICA, [ 'event_one' ] ) );
72
73 // successful select of alert
74 $gateway = new UserNotificationGateway(
75 $this->mockUser(),
76 $this->mockDbFactory( [ 'selectRowCount' => 2 ] ),
77 $this->mockConfig()
78 );
79 $this->assertSame( 2, $gateway->getCappedNotificationCount( DB_REPLICA, [ 'event_one', 'event_two' ] ) );
80
81 // there is event, should return 0
82 $gateway = new UserNotificationGateway(
83 $this->mockUser(),
84 $this->mockDbFactory( [ 'selectRowCount' => 2 ] ),
85 $this->mockConfig()
86 );
87 $this->assertSame( 0, $gateway->getCappedNotificationCount( DB_REPLICA, [] ) );
88
89 // successful select
90 $gateway = new UserNotificationGateway(
91 $this->mockUser(),
92 $this->mockDbFactory( [ 'selectRowCount' => 3 ] ),
93 $this->mockConfig()
94 );
95 $this->assertSame( 3, $gateway->getCappedNotificationCount( DB_REPLICA, [ 'event_one' ] ) );
96 }
97
98 public function testGetUnreadNotifications() {
99 $dbResult = [
100 (object)[ 'notification_event' => 1 ],
101 (object)[ 'notification_event' => 2 ],
102 (object)[ 'notification_event' => 3 ],
103 ];
104 $gateway = new UserNotificationGateway(
105 $this->mockUser(),
106 $this->mockDbFactory( [ 'select' => $dbResult ] ),
107 $this->mockConfig()
108 );
109 $res = $gateway->getUnreadNotifications( 'user_talk' );
110 $this->assertEquals( [ 1 => 1, 2 => 2, 3 => 3 ], $res );
111 }
112
113 /**
114 * Mock object of User
115 * @return User
116 */
117 protected function mockUser() {
118 $user = $this->createMock( User::class );
119 $user->method( 'getID' )
120 ->willReturn( 1 );
121
122 return $user;
123 }
124
125 /**
126 * Mock object of DbFactory
127 * @param array $dbResult
128 * @return DbFactory
129 */
130 protected function mockDbFactory( array $dbResult = [] ) {
131 $dbFactory = $this->createMock( DbFactory::class );
132 $dbFactory->method( 'getEchoDb' )
133 ->willReturn( $this->mockDb( $dbResult ) );
134
135 return $dbFactory;
136 }
137
138 protected function mockConfig() {
139 return new HashConfig( [
140 'UpdateRowsPerQuery' => 500,
141 ] );
142 }
143
144 /**
145 * Returns a mock database object
146 * @param array $dbResult
147 * @return \Wikimedia\Rdbms\IDatabase
148 */
149 protected function mockDb( array $dbResult = [] ) {
150 $dbResult += [
151 'update' => '',
152 'select' => [],
153 'selectRow' => '',
154 'selectRowCount' => 0,
155 ];
156 $db = $this->createMock( IDatabase::class );
157 $db->method( 'affectedRows' )
158 ->willReturn( $dbResult['update'] ? 1 : 0 );
159 $db->method( 'update' )
160 ->willReturn( $dbResult['update'] );
161 $db->method( 'select' )
162 ->willReturn( new FakeResultWrapper( $dbResult['select'] ) );
163 $db->method( 'selectRow' )
164 ->willReturn( $dbResult['selectRow'] );
165 $db->method( 'selectRowCount' )
166 ->willReturn( $dbResult['selectRowCount'] );
167 $db->method( 'newUpdateQueryBuilder' )
168 ->willReturnCallback( static function () use ( $db ) {
169 return new UpdateQueryBuilder( $db );
170 } );
171 $db->method( 'newSelectQueryBuilder' )
172 ->willReturnCallback( static function () use ( $db ) {
173 return new SelectQueryBuilder( $db );
174 } );
175
176 return $db;
177 }
178
179 }