1<?php
2
3use MediaWiki\User\UserIdentityValue;
4
5/**
6 * @author Addshore
7 *
8 * @covers NoWriteWatchedItemStore
9 */
10class NoWriteWatchedItemStoreUnitTest extends \MediaWikiUnitTestCase {
11
12	/**
13	 * @return NoWriteWatchedItemStore
14	 */
15	private function getNoWriteStoreForErrors(): NoWriteWatchedItemStore {
16		// NoWriteWatchedItemStore where the inner actual store should never be called,
17		// because we are testing the methods that throw exceptions instead
18		// We could do a fancy constrant for never having a method that matches the
19		// specific list, but since we don't use this for the cases that we have the
20		// inner actual store do anything, it should never be used
21		$innerService = $this->createNoOpAbstractMock( WatchedItemStoreInterface::class );
22		return new NoWriteWatchedItemStore( $innerService );
23	}
24
25	/**
26	 * @param string $method
27	 * @param mixed $result
28	 * @return NoWriteWatchedItemStore
29	 */
30	private function getNoWriteStoreForProxyCall( string $method, $result ): NoWriteWatchedItemStore {
31		// NoWriteWatchedItemStore where the inner actual store is used a single time
32		// for a method call
33		$innerService = $this->createNoOpAbstractMock(
34			WatchedItemStoreInterface::class,
35			[ $method ]
36		);
37		$innerService->expects( $this->once() )->method( $method )->willReturn( $result );
38		return new NoWriteWatchedItemStore( $innerService );
39	}
40
41	public function testAddWatch() {
42		$noWriteService = $this->getNoWriteStoreForErrors();
43
44		$this->expectException( DBReadOnlyError::class );
45		$noWriteService->addWatch(
46			new UserIdentityValue( 1, 'MockUser' ), new TitleValue( 0, 'Foo' ) );
47	}
48
49	public function testAddWatchBatchForUser() {
50		$noWriteService = $this->getNoWriteStoreForErrors();
51
52		$this->expectException( DBReadOnlyError::class );
53		$noWriteService->addWatchBatchForUser( new UserIdentityValue( 1, 'MockUser' ), [] );
54	}
55
56	public function testRemoveWatch() {
57		$noWriteService = $this->getNoWriteStoreForErrors();
58
59		$this->expectException( DBReadOnlyError::class );
60		$noWriteService->removeWatch(
61			new UserIdentityValue( 1, 'MockUser' ), new TitleValue( 0, 'Foo' ) );
62	}
63
64	public function testSetNotificationTimestampsForUser() {
65		$noWriteService = $this->getNoWriteStoreForErrors();
66
67		$this->expectException( DBReadOnlyError::class );
68		$noWriteService->setNotificationTimestampsForUser(
69			new UserIdentityValue( 1, 'MockUser' ),
70			'timestamp',
71			[]
72		);
73	}
74
75	public function testUpdateNotificationTimestamp() {
76		$noWriteService = $this->getNoWriteStoreForErrors();
77
78		$this->expectException( DBReadOnlyError::class );
79		$noWriteService->updateNotificationTimestamp(
80			new UserIdentityValue( 1, 'MockUser' ),
81			new TitleValue( 0, 'Foo' ),
82			'timestamp'
83		);
84	}
85
86	public function testResetNotificationTimestamp() {
87		$noWriteService = $this->getNoWriteStoreForErrors();
88
89		$this->expectException( DBReadOnlyError::class );
90		$noWriteService->resetNotificationTimestamp(
91			new UserIdentityValue( 1, 'MockUser' ),
92			new TitleValue( 0, 'Foo' )
93		);
94	}
95
96	public function testCountWatchedItems() {
97		$noWriteService = $this->getNoWriteStoreForProxyCall( 'countWatchedItems', __METHOD__ );
98
99		$return = $noWriteService->countWatchedItems(
100			new UserIdentityValue( 1, 'MockUser' )
101		);
102		$this->assertEquals( __METHOD__, $return );
103	}
104
105	public function testCountWatchers() {
106		$noWriteService = $this->getNoWriteStoreForProxyCall( 'countWatchers', __METHOD__ );
107
108		$return = $noWriteService->countWatchers(
109			new TitleValue( 0, 'Foo' )
110		);
111		$this->assertEquals( __METHOD__, $return );
112	}
113
114	public function testCountVisitingWatchers() {
115		$noWriteService = $this->getNoWriteStoreForProxyCall( 'countVisitingWatchers', __METHOD__ );
116
117		$return = $noWriteService->countVisitingWatchers(
118			new TitleValue( 0, 'Foo' ),
119			9
120		);
121		$this->assertEquals( __METHOD__, $return );
122	}
123
124	public function testCountWatchersMultiple() {
125		$noWriteService = $this->getNoWriteStoreForProxyCall( 'countWatchersMultiple', __METHOD__ );
126
127		$return = $noWriteService->countWatchersMultiple(
128			[ new TitleValue( 0, 'Foo' ) ],
129			[]
130		);
131		$this->assertEquals( __METHOD__, $return );
132	}
133
134	public function testCountVisitingWatchersMultiple() {
135		$noWriteService = $this->getNoWriteStoreForProxyCall( 'countVisitingWatchersMultiple', __METHOD__ );
136
137		$return = $noWriteService->countVisitingWatchersMultiple(
138			[ [ new TitleValue( 0, 'Foo' ), 99 ] ],
139			11
140		);
141		$this->assertEquals( __METHOD__, $return );
142	}
143
144	public function testGetWatchedItem() {
145		$noWriteService = $this->getNoWriteStoreForProxyCall( 'getWatchedItem', __METHOD__ );
146
147		$return = $noWriteService->getWatchedItem(
148			new UserIdentityValue( 1, 'MockUser' ),
149			new TitleValue( 0, 'Foo' )
150		);
151		$this->assertEquals( __METHOD__, $return );
152	}
153
154	public function testLoadWatchedItem() {
155		$noWriteService = $this->getNoWriteStoreForProxyCall( 'loadWatchedItem', __METHOD__ );
156
157		$return = $noWriteService->loadWatchedItem(
158			new UserIdentityValue( 1, 'MockUser' ),
159			new TitleValue( 0, 'Foo' )
160		);
161		$this->assertEquals( __METHOD__, $return );
162	}
163
164	public function testGetWatchedItemsForUser() {
165		$noWriteService = $this->getNoWriteStoreForProxyCall( 'getWatchedItemsForUser', __METHOD__ );
166
167		$return = $noWriteService->getWatchedItemsForUser(
168			new UserIdentityValue( 1, 'MockUser' ),
169			[]
170		);
171		$this->assertEquals( __METHOD__, $return );
172	}
173
174	public function testIsWatched() {
175		$noWriteService = $this->getNoWriteStoreForProxyCall( 'isWatched', __METHOD__ );
176
177		$return = $noWriteService->isWatched(
178			new UserIdentityValue( 1, 'MockUser' ),
179			new TitleValue( 0, 'Foo' )
180		);
181		$this->assertEquals( __METHOD__, $return );
182	}
183
184	public function testGetNotificationTimestampsBatch() {
185		$noWriteService = $this->getNoWriteStoreForProxyCall( 'getNotificationTimestampsBatch', __METHOD__ );
186
187		$return = $noWriteService->getNotificationTimestampsBatch(
188			new UserIdentityValue( 1, 'MockUser' ),
189			[ new TitleValue( 0, 'Foo' ) ]
190		);
191		$this->assertEquals( __METHOD__, $return );
192	}
193
194	public function testCountUnreadNotifications() {
195		$noWriteService = $this->getNoWriteStoreForProxyCall( 'countUnreadNotifications', __METHOD__ );
196
197		$return = $noWriteService->countUnreadNotifications(
198			new UserIdentityValue( 1, 'MockUser' ),
199			88
200		);
201		$this->assertEquals( __METHOD__, $return );
202	}
203
204	public function testDuplicateAllAssociatedEntries() {
205		$noWriteService = $this->getNoWriteStoreForErrors();
206
207		$this->expectException( DBReadOnlyError::class );
208		$noWriteService->duplicateAllAssociatedEntries(
209			new TitleValue( 0, 'Foo' ),
210			new TitleValue( 0, 'Bar' )
211		);
212	}
213
214}
215