File: NoWriteWatchedItemStoreUnitTest.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (218 lines) | stat: -rw-r--r-- 6,771 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php

use MediaWiki\Title\TitleValue;
use MediaWiki\User\UserIdentityValue;
use MediaWiki\Watchlist\NoWriteWatchedItemStore;
use MediaWiki\Watchlist\WatchedItemStoreInterface;
use Wikimedia\Rdbms\DBReadOnlyError;

/**
 * @author Addshore
 *
 * @covers \MediaWiki\Watchlist\NoWriteWatchedItemStore
 */
class NoWriteWatchedItemStoreUnitTest extends \MediaWikiUnitTestCase {

	/**
	 * @return NoWriteWatchedItemStore
	 */
	private function getNoWriteStoreForErrors(): NoWriteWatchedItemStore {
		// NoWriteWatchedItemStore where the inner actual store should never be called,
		// because we are testing the methods that throw exceptions instead
		// We could do a fancy constrant for never having a method that matches the
		// specific list, but since we don't use this for the cases that we have the
		// inner actual store do anything, it should never be used
		$innerService = $this->createNoOpAbstractMock( WatchedItemStoreInterface::class );
		return new NoWriteWatchedItemStore( $innerService );
	}

	/**
	 * @param string $method
	 * @param mixed $result
	 * @return NoWriteWatchedItemStore
	 */
	private function getNoWriteStoreForProxyCall( string $method, $result ): NoWriteWatchedItemStore {
		// NoWriteWatchedItemStore where the inner actual store is used a single time
		// for a method call
		$innerService = $this->createNoOpAbstractMock(
			WatchedItemStoreInterface::class,
			[ $method ]
		);
		$innerService->expects( $this->once() )->method( $method )->willReturn( $result );
		return new NoWriteWatchedItemStore( $innerService );
	}

	public function testAddWatch() {
		$noWriteService = $this->getNoWriteStoreForErrors();

		$this->expectException( DBReadOnlyError::class );
		$noWriteService->addWatch(
			new UserIdentityValue( 1, 'MockUser' ), new TitleValue( 0, 'Foo' ) );
	}

	public function testAddWatchBatchForUser() {
		$noWriteService = $this->getNoWriteStoreForErrors();

		$this->expectException( DBReadOnlyError::class );
		$noWriteService->addWatchBatchForUser( new UserIdentityValue( 1, 'MockUser' ), [] );
	}

	public function testRemoveWatch() {
		$noWriteService = $this->getNoWriteStoreForErrors();

		$this->expectException( DBReadOnlyError::class );
		$noWriteService->removeWatch(
			new UserIdentityValue( 1, 'MockUser' ), new TitleValue( 0, 'Foo' ) );
	}

	public function testSetNotificationTimestampsForUser() {
		$noWriteService = $this->getNoWriteStoreForErrors();

		$this->expectException( DBReadOnlyError::class );
		$noWriteService->setNotificationTimestampsForUser(
			new UserIdentityValue( 1, 'MockUser' ),
			'timestamp',
			[]
		);
	}

	public function testUpdateNotificationTimestamp() {
		$noWriteService = $this->getNoWriteStoreForErrors();

		$this->expectException( DBReadOnlyError::class );
		$noWriteService->updateNotificationTimestamp(
			new UserIdentityValue( 1, 'MockUser' ),
			new TitleValue( 0, 'Foo' ),
			'timestamp'
		);
	}

	public function testResetNotificationTimestamp() {
		$noWriteService = $this->getNoWriteStoreForErrors();

		$this->expectException( DBReadOnlyError::class );
		$noWriteService->resetNotificationTimestamp(
			new UserIdentityValue( 1, 'MockUser' ),
			new TitleValue( 0, 'Foo' )
		);
	}

	public function testCountWatchedItems() {
		$noWriteService = $this->getNoWriteStoreForProxyCall( 'countWatchedItems', __METHOD__ );

		$return = $noWriteService->countWatchedItems(
			new UserIdentityValue( 1, 'MockUser' )
		);
		$this->assertEquals( __METHOD__, $return );
	}

	public function testCountWatchers() {
		$noWriteService = $this->getNoWriteStoreForProxyCall( 'countWatchers', __METHOD__ );

		$return = $noWriteService->countWatchers(
			new TitleValue( 0, 'Foo' )
		);
		$this->assertEquals( __METHOD__, $return );
	}

	public function testCountVisitingWatchers() {
		$noWriteService = $this->getNoWriteStoreForProxyCall( 'countVisitingWatchers', __METHOD__ );

		$return = $noWriteService->countVisitingWatchers(
			new TitleValue( 0, 'Foo' ),
			9
		);
		$this->assertEquals( __METHOD__, $return );
	}

	public function testCountWatchersMultiple() {
		$noWriteService = $this->getNoWriteStoreForProxyCall( 'countWatchersMultiple', __METHOD__ );

		$return = $noWriteService->countWatchersMultiple(
			[ new TitleValue( 0, 'Foo' ) ],
			[]
		);
		$this->assertEquals( __METHOD__, $return );
	}

	public function testCountVisitingWatchersMultiple() {
		$noWriteService = $this->getNoWriteStoreForProxyCall( 'countVisitingWatchersMultiple', __METHOD__ );

		$return = $noWriteService->countVisitingWatchersMultiple(
			[ [ new TitleValue( 0, 'Foo' ), 99 ] ],
			11
		);
		$this->assertEquals( __METHOD__, $return );
	}

	public function testGetWatchedItem() {
		$noWriteService = $this->getNoWriteStoreForProxyCall( 'getWatchedItem', __METHOD__ );

		$return = $noWriteService->getWatchedItem(
			new UserIdentityValue( 1, 'MockUser' ),
			new TitleValue( 0, 'Foo' )
		);
		$this->assertEquals( __METHOD__, $return );
	}

	public function testLoadWatchedItem() {
		$noWriteService = $this->getNoWriteStoreForProxyCall( 'loadWatchedItem', __METHOD__ );

		$return = $noWriteService->loadWatchedItem(
			new UserIdentityValue( 1, 'MockUser' ),
			new TitleValue( 0, 'Foo' )
		);
		$this->assertEquals( __METHOD__, $return );
	}

	public function testGetWatchedItemsForUser() {
		$noWriteService = $this->getNoWriteStoreForProxyCall( 'getWatchedItemsForUser', __METHOD__ );

		$return = $noWriteService->getWatchedItemsForUser(
			new UserIdentityValue( 1, 'MockUser' ),
			[]
		);
		$this->assertEquals( __METHOD__, $return );
	}

	public function testIsWatched() {
		$noWriteService = $this->getNoWriteStoreForProxyCall( 'isWatched', __METHOD__ );

		$return = $noWriteService->isWatched(
			new UserIdentityValue( 1, 'MockUser' ),
			new TitleValue( 0, 'Foo' )
		);
		$this->assertEquals( __METHOD__, $return );
	}

	public function testGetNotificationTimestampsBatch() {
		$noWriteService = $this->getNoWriteStoreForProxyCall( 'getNotificationTimestampsBatch', __METHOD__ );

		$return = $noWriteService->getNotificationTimestampsBatch(
			new UserIdentityValue( 1, 'MockUser' ),
			[ new TitleValue( 0, 'Foo' ) ]
		);
		$this->assertEquals( __METHOD__, $return );
	}

	public function testCountUnreadNotifications() {
		$noWriteService = $this->getNoWriteStoreForProxyCall( 'countUnreadNotifications', __METHOD__ );

		$return = $noWriteService->countUnreadNotifications(
			new UserIdentityValue( 1, 'MockUser' ),
			88
		);
		$this->assertEquals( __METHOD__, $return );
	}

	public function testDuplicateAllAssociatedEntries() {
		$noWriteService = $this->getNoWriteStoreForErrors();

		$this->expectException( DBReadOnlyError::class );
		$noWriteService->duplicateAllAssociatedEntries(
			new TitleValue( 0, 'Foo' ),
			new TitleValue( 0, 'Bar' )
		);
	}

}