Debian Codegraph @

tests/phpunit/includes/actions/WatchActionTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\Context\DerivativeContext;
4 use MediaWiki\Context\IContextSource;
5 use MediaWiki\Context\RequestContext;
6 use MediaWiki\Language\Language;
7 use MediaWiki\Language\RawMessage;
8 use MediaWiki\MainConfigNames;
9 use MediaWiki\Message\Message;
10 use MediaWiki\Output\OutputPage;
11 use MediaWiki\Permissions\PermissionManager;
12 use MediaWiki\Request\WebRequest;
13 use MediaWiki\Status\Status;
14 use MediaWiki\Tests\Unit\DummyServicesTrait;
15 use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait;
16 use MediaWiki\Title\Title;
17 use MediaWiki\Title\TitleValue;
18 use MediaWiki\User\User;
19 use MediaWiki\User\UserFactory;
20 use MediaWiki\User\UserIdentityValue;
21 use MediaWiki\Watchlist\WatchedItem;
22 use PHPUnit\Framework\MockObject\MockObject;
23 use Wikimedia\TestingAccessWrapper;
24 use Wikimedia\Timestamp\ConvertibleTimestamp;
25
26 /**
27 * @covers \WatchAction
28 *
29 * @group Action
30 */
31 class WatchActionTest extends MediaWikiIntegrationTestCase {
32 use DummyServicesTrait;
33 use MockAuthorityTrait;
34
35 /**
36 * @var WatchAction
37 */
38 private $watchAction;
39
40 /**
41 * @var WikiPage
42 */
43 private $testWikiPage;
44
45 /**
46 * @var IContextSource
47 */
48 private $context;
49
50 protected function setUp(): void {
51 parent::setUp();
52 $this->overrideConfigValues( [
53 MainConfigNames::LanguageCode => 'en',
54 ] );
55
56 $this->setService( 'ReadOnlyMode', $this->getDummyReadOnlyMode( false ) );
57 $testTitle = Title::makeTitle( NS_MAIN, 'UTTest' );
58 $this->testWikiPage = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $testTitle );
59 $testContext = new DerivativeContext( RequestContext::getMain() );
60 $testContext->setTitle( $testTitle );
61 $this->context = $testContext;
62 $this->watchAction = $this->getWatchAction(
63 Article::newFromWikiPage( $this->testWikiPage, $testContext ),
64 $testContext
65 );
66 }
67
68 private function getWatchAction( Article $article, IContextSource $context ) {
69 $mwServices = $this->getServiceContainer();
70 return new WatchAction(
71 $article,
72 $context,
73 $mwServices->getWatchlistManager(),
74 $mwServices->getWatchedItemStore()
75 );
76 }
77
78 /**
79 * @covers \WatchAction::getName()
80 */
81 public function testGetName() {
82 $this->assertEquals( 'watch', $this->watchAction->getName() );
83 }
84
85 /**
86 * @covers \WatchAction::requiresUnblock()
87 */
88 public function testRequiresUnlock() {
89 $this->assertFalse( $this->watchAction->requiresUnblock() );
90 }
91
92 /**
93 * @covers \WatchAction::doesWrites()
94 */
95 public function testDoesWrites() {
96 $this->assertTrue( $this->watchAction->doesWrites() );
97 }
98
99 /**
100 * @covers \WatchAction::onSubmit()
101 */
102 public function testOnSubmit() {
103 /** @var Status $actual */
104 $actual = $this->watchAction->onSubmit( [] );
105
106 $this->assertStatusGood( $actual );
107 }
108
109 /**
110 * @covers \WatchAction::onSubmit()
111 */
112 public function testOnSubmitHookAborted() {
113 // WatchlistExpiry feature flag.
114 $this->overrideConfigValue( MainConfigNames::WatchlistExpiry, true );
115
116 $testContext = $this->getMockBuilder( DerivativeContext::class )
117 ->onlyMethods( [ 'getRequest' ] )
118 ->setConstructorArgs( [ $this->watchAction->getContext() ] )
119 ->getMock();
120
121 // Change the context to have a registered user with correct permission.
122 $user = new UserIdentityValue( 100, 'User Name' );
123 $performer = $this->mockUserAuthorityWithPermissions( $user, [ 'editmywatchlist' ] );
124 $testContext->setAuthority( $performer );
125 $userFactory = $this->createMock( UserFactory::class );
126 $userFactory->method( 'newFromUserIdentity' )->willReturn( $this->createMock( User::class ) );
127 $this->setService( 'UserFactory', $userFactory );
128
129 /** @var MockObject|WebRequest $testRequest */
130 $testRequest = $this->createMock( WebRequest::class );
131 $testRequest->expects( $this->once() )
132 ->method( 'getVal' )
133 ->willReturn( '6 months' );
134 $testContext->method( 'getRequest' )->willReturn( $testRequest );
135
136 $this->setService( 'WatchedItemStore', $this->getDummyWatchedItemStore() );
137
138 $this->watchAction = $this->getWatchAction(
139 Article::newFromWikiPage( $this->testWikiPage, $testContext ),
140 $testContext
141 );
142
143 $this->setTemporaryHook( 'WatchArticle', static function () {
144 return false;
145 } );
146
147 /** @var Status $actual */
148 $actual = $this->watchAction->onSubmit( [] );
149
150 $this->assertInstanceOf( Status::class, $actual );
151 $this->assertStatusError( 'hookaborted', $actual );
152 }
153
154 /**
155 * @covers \WatchAction::checkCanExecute()
156 */
157 public function testShowUserNotLoggedIn() {
158 $notLoggedInUser = new User();
159 $testContext = new DerivativeContext( $this->watchAction->getContext() );
160 $testContext->setUser( $notLoggedInUser );
161 $watchAction = $this->getWatchAction(
162 Article::newFromWikiPage( $this->testWikiPage, $testContext ),
163 $testContext
164 );
165 $this->expectException( UserNotLoggedIn::class );
166
167 $watchAction->show();
168 }
169
170 /**
171 * @covers \WatchAction::checkCanExecute()
172 */
173 public function testShowUserLoggedInNoException() {
174 $this->setService( 'PermissionManager', $this->createMock( PermissionManager::class ) );
175 $registeredUser = $this->createMock( User::class );
176 $registeredUser->method( 'isRegistered' )->willReturn( true );
177 $registeredUser->method( 'isNamed' )->willReturn( true );
178 $testContext = new DerivativeContext( $this->watchAction->getContext() );
179 $testContext->setUser( $registeredUser );
180 $watchAction = $this->getWatchAction(
181 Article::newFromWikiPage( $this->testWikiPage, $testContext ),
182 $testContext
183 );
184
185 $exception = null;
186 try {
187 $watchAction->show();
188 } catch ( UserNotLoggedIn $e ) {
189 $exception = $e;
190 }
191 $this->assertNull( $exception,
192 'UserNotLoggedIn exception should not be thrown if user is a registered one.' );
193 }
194
195 /**
196 * @covers \WatchAction::onSuccess()
197 */
198 public function testOnSuccessMainNamespaceTitle() {
199 /** @var MockObject|IContextSource $testContext */
200 $testContext = $this->getMockBuilder( DerivativeContext::class )
201 ->onlyMethods( [ 'msg' ] )
202 ->setConstructorArgs( [ $this->watchAction->getContext() ] )
203 ->getMock();
204 $testOutput = new OutputPage( $testContext );
205 $testContext->setOutput( $testOutput );
206 $testContext->method( 'msg' )->willReturnCallback( static function ( $msgKey ) {
207 return new RawMessage( $msgKey );
208 } );
209 $watchAction = $this->getWatchAction(
210 Article::newFromWikiPage( $this->testWikiPage, $testContext ),
211 $testContext
212 );
213
214 $watchAction->onSuccess();
215
216 $this->assertEquals( '<p>addedwatchtext
217 </p>', $testOutput->getHTML() );
218 }
219
220 /**
221 * @covers \WatchAction::onSuccess()
222 */
223 public function testOnSuccessTalkPage() {
224 /** @var MockObject|IContextSource $testContext */
225 $testContext = $this->getMockBuilder( DerivativeContext::class )
226 ->onlyMethods( [ 'getOutput', 'msg' ] )
227 ->setConstructorArgs( [ $this->watchAction->getContext() ] )
228 ->getMock();
229 $testOutput = new OutputPage( $testContext );
230 $testContext->method( 'getOutput' )->willReturn( $testOutput );
231 $testContext->method( 'msg' )->willReturnCallback( static function ( $msgKey ) {
232 return new RawMessage( $msgKey );
233 } );
234 $talkPageTitle = Title::makeTitle( NS_TALK, 'UTTest' );
235 $testContext->setTitle( $talkPageTitle );
236 $watchAction = $this->getWatchAction(
237 Article::newFromTitle( $talkPageTitle, $testContext ),
238 $testContext
239 );
240
241 $watchAction->onSuccess();
242
243 $this->assertEquals( '<p>addedwatchtext-talk
244 </p>', $testOutput->getHTML() );
245 }
246
247 /**
248 * @dataProvider provideOnSuccessDifferentMessages
249 */
250 public function testOnSuccessDifferentMessages(
251 $watchlistExpiry, $msg, $prefixedTitle, $submittedExpiry, $expiryLabel
252 ) {
253 // Fake current time to be 2020-09-17 12:00:00 UTC.
254 ConvertibleTimestamp::setFakeTime( '20200917120000' );
255
256 // WatchlistExpiry feature flag.
257 $this->overrideConfigValue( MainConfigNames::WatchlistExpiry, $watchlistExpiry );
258
259 // Set up context, request, and output.
260 /** @var MockObject|IContextSource $testContext */
261 $testContext = $this->getMockBuilder( DerivativeContext::class )
262 ->onlyMethods( [ 'getOutput', 'getRequest', 'getLanguage' ] )
263 ->setConstructorArgs( [ $this->watchAction->getContext() ] )
264 ->getMock();
265 /** @var MockObject|OutputPage $testOutput */
266 $testOutput = $this->createMock( OutputPage::class );
267 $testOutput->expects( $this->once() )
268 ->method( 'addWikiMsg' )
269 ->with( $msg, $prefixedTitle, $expiryLabel );
270 $testContext->method( 'getOutput' )->willReturn( $testOutput );
271 // Set language to anything non-English/default, to catch assumptions.
272 $langDe = $this->getServiceContainer()->getLanguageFactory()->getLanguage( 'de' );
273 $testContext->method( 'getLanguage' )->willReturn( $langDe );
274 /** @var MockObject|WebRequest $testRequest */
275 $testRequest = $this->createMock( WebRequest::class );
276 $testRequest->expects( $this->once() )
277 ->method( 'getText' )
278 ->willReturn( $submittedExpiry );
279 $testContext->method( 'getRequest' )->willReturn( $testRequest );
280
281 // Call the onSuccess method, and the above mocks will confirm it's correct.
282 /** @var WatchAction $watchAction */
283 $watchAction = TestingAccessWrapper::newFromObject(
284 $this->getWatchAction(
285 Article::newFromTitle( Title::newFromText( $prefixedTitle ), $testContext ),
286 $testContext
287 )
288 );
289 $watchAction->onSuccess();
290 }
291
292 public static function provideOnSuccessDifferentMessages() {
293 return [
294 [
295 'wgWatchlistExpiry' => false,
296 'msg' => 'addedwatchtext',
297 'prefixedTitle' => 'Foo',
298 'submittedExpiry' => null,
299 'expiryLabel' => null,
300 ],
301 [
302 'wgWatchlistExpiry' => false,
303 'msg' => 'addedwatchtext-talk',
304 'prefixedTitle' => 'Talk:Foo',
305 'submittedExpiry' => null,
306 'expiryLabel' => null,
307 ],
308 [
309 'wgWatchlistExpiry' => true,
310 'msg' => 'addedwatchindefinitelytext',
311 'prefixedTitle' => 'Foo',
312 'submittedExpiry' => 'infinite',
313 'expiryLabel' => 'Dauerhaft',
314 ],
315 [
316 'wgWatchlistExpiry' => true,
317 'msg' => 'addedwatchindefinitelytext-talk',
318 'prefixedTitle' => 'Talk:Foo',
319 'submittedExpiry' => 'infinite',
320 'expiryLabel' => 'Dauerhaft',
321 ],
322 [
323 'wgWatchlistExpiry' => true,
324 'msg' => 'addedwatchexpirytext',
325 'prefixedTitle' => 'Foo',
326 'submittedExpiry' => '1 week',
327 'expiryLabel' => '1 Woche',
328 ],
329 [
330 'wgWatchlistExpiry' => true,
331 'msg' => 'addedwatchexpirytext-talk',
332 'prefixedTitle' => 'Talk:Foo',
333 'submittedExpiry' => '1 week',
334 'expiryLabel' => '1 Woche',
335 ],
336 [
337 'wgWatchlistExpiry' => true,
338 'msg' => 'addedwatchexpiryhours',
339 'prefixedTitle' => 'Foo',
340 'submittedExpiry' => '2020-09-17T14:00:00Z',
341 'expiryLabel' => null,
342 ],
343 [
344 'wgWatchlistExpiry' => true,
345 'msg' => 'addedwatchexpiryhours-talk',
346 'prefixedTitle' => 'Talk:Foo',
347 'submittedExpiry' => '2020-09-17T14:00:00Z',
348 'expiryLabel' => null,
349 ],
350 ];
351 }
352
353 /**
354 * @covers \WatchAction::getExpiryOptions()
355 */
356 public function testGetExpiryOptions() {
357 // Fake current time to be 2020-06-10T00:00:00Z
358 ConvertibleTimestamp::setFakeTime( '20200610000000' );
359 $userIdentity = new UserIdentityValue( 100, 'User Name' );
360 $target = new TitleValue( 0, 'SomeDbKey' );
361
362 $optionsNoExpiry = WatchAction::getExpiryOptions( $this->context, false );
363 $expectedNoExpiry = [
364 'options' => [
365 'Permanent' => 'infinite',
366 '1 week' => '1 week',
367 '1 month' => '1 month',
368 '3 months' => '3 months',
369 '6 months' => '6 months',
370 '1 year' => '1 year',
371 ],
372 'default' => 'infinite'
373 ];
374
375 $this->assertSame( $expectedNoExpiry, $optionsNoExpiry );
376
377 // Adding a watched item with an expiry a month from the frozen time
378 $watchedItemMonth = new WatchedItem( $userIdentity, $target, null, '20200710000000' );
379 $optionsExpiryOneMonth = WatchAction::getExpiryOptions( $this->context, $watchedItemMonth );
380 $expectedExpiryOneMonth = [
381 'options' => [
382 '30 days left' => '2020-07-10T00:00:00Z',
383 'Permanent' => 'infinite',
384 '1 week' => '1 week',
385 '1 month' => '1 month',
386 '3 months' => '3 months',
387 '6 months' => '6 months',
388 '1 year' => '1 year',
389 ],
390 'default' => '2020-07-10T00:00:00Z'
391 ];
392
393 $this->assertSame( $expectedExpiryOneMonth, $optionsExpiryOneMonth );
394
395 // Adding a watched item with an expiry 7 days from the frozen time
396 $watchedItemWeek = new WatchedItem( $userIdentity, $target, null, '20200617000000' );
397 $optionsExpiryOneWeek = WatchAction::getExpiryOptions( $this->context, $watchedItemWeek );
398 $expectedOneWeek = [
399 'options' => [
400 '7 days left' => '2020-06-17T00:00:00Z',
401 'Permanent' => 'infinite',
402 '1 week' => '1 week',
403 '1 month' => '1 month',
404 '3 months' => '3 months',
405 '6 months' => '6 months',
406 '1 year' => '1 year',
407 ],
408 'default' => '2020-06-17T00:00:00Z'
409 ];
410
411 $this->assertSame( $expectedOneWeek, $optionsExpiryOneWeek );
412
413 // Case for when WatchedItem is true
414 $optionsNoExpiryWIFalse = WatchAction::getExpiryOptions( $this->context, true );
415 $expectedNoExpiryWIFalse = [
416 'options' => [
417 'Permanent' => 'infinite',
418 '1 week' => '1 week',
419 '1 month' => '1 month',
420 '3 months' => '3 months',
421 '6 months' => '6 months',
422 '1 year' => '1 year',
423 ],
424 'default' => 'infinite'
425 ];
426
427 $this->assertSame( $expectedNoExpiryWIFalse, $optionsNoExpiryWIFalse );
428 }
429
430 /**
431 * @covers \WatchAction::getExpiryOptions()
432 */
433 public function testGetExpiryOptionsWithInvalidTranslations() {
434 $mockMessageLocalizer = $this->createMock( MockMessageLocalizer::class );
435 $mockLanguage = $this->createMock( Language::class );
436 $mockLanguage->method( 'getCode' )->willReturn( 'not-english' );
437 $mockMessage = $this->getMockMessage( 'invalid:invalid, foo:bar, thing' );
438 $mockMessage->method( 'getLanguage' )->willReturn( $mockLanguage );
439
440 $mockMessageLocalizer->expects( $this->exactly( 2 ) )
441 ->method( 'msg' )
442 ->willReturnOnConsecutiveCalls(
443 $mockMessage,
444 new Message( 'watchlist-expiry-options' )
445 );
446
447 $expected = WatchAction::getExpiryOptions( new MockMessageLocalizer( 'en' ), false );
448 $expiryOptions = WatchAction::getExpiryOptions( $mockMessageLocalizer, false );
449 $this->assertSame( $expected, $expiryOptions );
450 }
451
452 /**
453 * @covers \WatchAction::getExpiryOptions()
454 */
455 public function testGetExpiryOptionsWithPartialInvalidTranslations() {
456 $mockMessageLocalizer = $this->createMock( MockMessageLocalizer::class );
457 $mockMessageLocalizer->expects( $this->once() )
458 ->method( 'msg' )
459 ->with( 'watchlist-expiry-options' )
460 ->willReturn( $this->getMockMessage( 'invalid:invalid, thing, 1 week: 1 week,3 days:3 days' ) );
461
462 $expected = [
463 'options' => [
464 '1 week' => '1 week',
465 '3 days' => '3 days',
466 ],
467 'default' => '1 week'
468 ];
469 $expiryOptions = WatchAction::getExpiryOptions( $mockMessageLocalizer, false );
470 $this->assertSame( $expected, $expiryOptions );
471 }
472 }