Debian Codegraph @

tests/phpunit/integration/includes/watchlist/WatchlistManagerTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\Page\PageIdentityValue;
4 use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait;
5 use MediaWiki\User\UserIdentityValue;
6
7 /**
8 * @covers \MediaWiki\Watchlist\WatchlistManager
9 *
10 * @author DannyS712
11 * @group Database
12 */
13 class WatchlistManagerTest extends MediaWikiIntegrationTestCase {
14 use MockAuthorityTrait;
15
16 /**
17 * While this *could* be moved to a unit test, it is specifically kept
18 * here as an integration test to double check that the actual integration
19 * between this service and others, and getting this service from the
20 * service container, works as expected. Please don't move it to
21 * the unit tests.
22 *
23 * @covers \MediaWiki\Watchlist\WatchlistManager::isWatched
24 * @covers \MediaWiki\Watchlist\WatchlistManager::isTempWatched
25 * @covers \MediaWiki\Watchlist\WatchlistManager::addWatch
26 * @covers \MediaWiki\Watchlist\WatchlistManager::addWatchIgnoringRights()
27 * @covers \MediaWiki\Watchlist\WatchlistManager::removeWatch()
28 */
29 public function testWatchlist() {
30 $userIdentity = new UserIdentityValue( 100, 'User Name' );
31 $authority = $this->mockUserAuthorityWithPermissions(
32 $userIdentity,
33 [ 'editmywatchlist', 'viewmywatchlist' ]
34 );
35 $title = new PageIdentityValue( 100, NS_MAIN, 'Page_db_Key_goesHere', PageIdentityValue::LOCAL );
36
37 $services = $this->getServiceContainer();
38 $watchedItemStore = $services->getWatchedItemStore();
39 $watchlistManager = $services->getWatchlistManager();
40
41 $watchedItemStore->clearUserWatchedItems( $userIdentity );
42
43 $this->assertFalse(
44 $watchlistManager->isWatched( $authority, $title ),
45 'The article has not been watched yet'
46 );
47 $this->assertFalse(
48 $watchlistManager->isTempWatched( $authority, $title ),
49 "The article hasn't been temporarily watched"
50 );
51
52 $watchlistManager->addWatch( $authority, $title );
53 $this->assertTrue( $watchlistManager->isWatched( $authority, $title ), 'The article has been watched' );
54 $this->assertFalse(
55 $watchlistManager->isTempWatched( $authority, $title ),
56 "The article hasn't been temporarily watched"
57 );
58
59 $watchlistManager->removeWatch( $authority, $title );
60 $this->assertFalse( $watchlistManager->isWatched( $authority, $title ), 'The article has been unwatched' );
61 $this->assertFalse(
62 $watchlistManager->isTempWatched( $authority, $title ),
63 "The article hasn't been temporarily watched"
64 );
65
66 $watchlistManager->addWatch( $authority, $title, '2 weeks' );
67 $this->assertTrue( $watchlistManager->isWatched( $authority, $title ), 'The article has been watched' );
68 $this->assertTrue(
69 $watchlistManager->isTempWatched( $authority, $title ), 'The article has been tempoarily watched'
70 );
71
72 $watchlistManager->removeWatch( $authority, $title );
73 $this->assertFalse( $watchlistManager->isWatched( $authority, $title ), 'The article has been unwatched' );
74 $this->assertFalse(
75 $watchlistManager->isTempWatched( $authority, $title ),
76 "The article hasn't been temporarily watched"
77 );
78
79 $watchlistManager->addWatchIgnoringRights( $userIdentity, $title );
80 $this->assertTrue( $watchlistManager->isWatched( $authority, $title ), 'The article has been watched' );
81 $this->assertFalse(
82 $watchlistManager->isTempWatched( $authority, $title ),
83 "The article hasn't been temporarily watched"
84 );
85 }
86
87 /**
88 * Can't move to unit tests because if the user is missing rights
89 * the status is from User::newFatalPermissionDeniedStatus which uses
90 * MediaWikiServices
91 *
92 * @covers \MediaWiki\Watchlist\WatchlistManager::isWatched
93 * @covers \MediaWiki\Watchlist\WatchlistManager::isWatchedIgnoringRights
94 * @covers \MediaWiki\Watchlist\WatchlistManager::isTempWatched
95 * @covers \MediaWiki\Watchlist\WatchlistManager::isTempWatchedIgnoringRights
96 * @covers \MediaWiki\Watchlist\WatchlistManager::addWatch
97 * @covers \MediaWiki\Watchlist\WatchlistManager::addWatchIgnoringRights()
98 * @covers \MediaWiki\Watchlist\WatchlistManager::removeWatch()
99 */
100 public function testWatchlistNoRights() {
101 $userIdentity = new UserIdentityValue( 100, 'User Name' );
102 $authority = $this->mockUserAuthorityWithPermissions( $userIdentity, [] );
103 $title = new PageIdentityValue( 100, NS_MAIN, 'Page_db_Key_goesHere', PageIdentityValue::LOCAL );
104
105 $services = $this->getServiceContainer();
106 $watchedItemStore = $services->getWatchedItemStore();
107 $watchlistManager = $services->getWatchlistManager();
108
109 $watchedItemStore->clearUserWatchedItems( $userIdentity );
110
111 $this->assertFalse( $watchlistManager->isWatched( $authority, $title ) );
112 $this->assertFalse( $watchlistManager->isTempWatched( $authority, $title ) );
113 $this->assertFalse( $watchlistManager->isWatchedIgnoringRights( $userIdentity, $title ) );
114 $this->assertFalse( $watchlistManager->isTempWatchedIgnoringRights( $userIdentity, $title ) );
115
116 $watchlistManager->addWatch( $authority, $title );
117 $this->assertFalse( $watchlistManager->isWatched( $authority, $title ) );
118 $this->assertFalse( $watchlistManager->isTempWatched( $authority, $title ) );
119 $this->assertFalse( $watchlistManager->isWatchedIgnoringRights( $userIdentity, $title ) );
120 $this->assertFalse( $watchlistManager->isTempWatchedIgnoringRights( $userIdentity, $title ) );
121
122 $watchlistManager->addWatchIgnoringRights( $userIdentity, $title );
123 $this->assertFalse( $watchlistManager->isWatched( $authority, $title ) );
124 $this->assertFalse( $watchlistManager->isTempWatched( $authority, $title ) );
125 $this->assertTrue( $watchlistManager->isWatchedIgnoringRights( $userIdentity, $title ) );
126 $this->assertFalse( $watchlistManager->isTempWatchedIgnoringRights( $userIdentity, $title ) );
127
128 $watchlistManager->addWatchIgnoringRights( $userIdentity, $title, '1 week' );
129 $this->assertFalse( $watchlistManager->isWatched( $authority, $title ) );
130 $this->assertFalse( $watchlistManager->isTempWatched( $authority, $title ) );
131 $this->assertTrue( $watchlistManager->isWatchedIgnoringRights( $userIdentity, $title ) );
132 $this->assertTrue( $watchlistManager->isTempWatchedIgnoringRights( $userIdentity, $title ) );
133
134 $watchlistManager->removeWatch( $authority, $title );
135 $this->assertFalse( $watchlistManager->isWatched( $authority, $title ) );
136 $this->assertFalse( $watchlistManager->isTempWatched( $authority, $title ) );
137 $this->assertTrue( $watchlistManager->isWatchedIgnoringRights( $userIdentity, $title ) );
138 $this->assertTrue( $watchlistManager->isTempWatchedIgnoringRights( $userIdentity, $title ) );
139
140 $watchlistManager->removeWatchIgnoringRights( $userIdentity, $title );
141 $this->assertFalse( $watchlistManager->isWatched( $authority, $title ) );
142 $this->assertFalse( $watchlistManager->isTempWatched( $authority, $title ) );
143 $this->assertFalse( $watchlistManager->isWatchedIgnoringRights( $userIdentity, $title ) );
144 $this->assertFalse( $watchlistManager->isTempWatchedIgnoringRights( $userIdentity, $title ) );
145 }
146
147 /**
148 * Can't move to unit tests because if the user is missing rights
149 * the status is from User::newFatalPermissionDeniedStatus which uses
150 * MediaWikiServices
151 *
152 * @covers \MediaWiki\Watchlist\WatchlistManager::addWatch()
153 */
154 public function testAddWatchUserNotPermittedStatusNotGood() {
155 $userIdentity = new UserIdentityValue( 100, 'User Name' );
156 $performer = $this->mockUserAuthorityWithPermissions( $userIdentity, [] );
157 $title = new PageIdentityValue( 100, NS_MAIN, 'Page_db_Key_goesHere', PageIdentityValue::LOCAL );
158
159 $services = $this->getServiceContainer();
160 $watchedItemStore = $services->getWatchedItemStore();
161 $watchlistManager = $services->getWatchlistManager();
162
163 $watchedItemStore->clearUserWatchedItems( $userIdentity );
164
165 $actual = $watchlistManager->addWatch( $performer, $title );
166
167 $this->assertStatusNotGood( $actual );
168 $this->assertFalse( $watchlistManager->isWatchedIgnoringRights( $userIdentity, $title ) );
169 }
170
171 /**
172 * Can't move to unit tests because if the user is missing rights
173 * the status is from User::newFatalPermissionDeniedStatus which uses
174 * MediaWikiServices
175 *
176 * @covers \MediaWiki\Watchlist\WatchlistManager::removeWatch()
177 */
178 public function testRemoveWatchWithoutRights() {
179 $userIdentity = new UserIdentityValue( 100, 'User Name' );
180 $performer = $this->mockUserAuthorityWithPermissions( $userIdentity, [] );
181 $title = new PageIdentityValue( 100, NS_MAIN, 'Page_db_Key_goesHere', PageIdentityValue::LOCAL );
182
183 $services = $this->getServiceContainer();
184 $watchlistManager = $services->getWatchlistManager();
185
186 $watchlistManager->addWatchIgnoringRights( $userIdentity, $title );
187
188 $actual = $watchlistManager->removeWatch( $performer, $title );
189
190 $this->assertStatusNotGood( $actual );
191 $this->assertTrue( $watchlistManager->isWatchedIgnoringRights( $userIdentity, $title ) );
192 }
193
194 }