3 use MediaWiki\MediaWikiServices
;
10 * @covers ApiQueryWatchlistRaw
12 class ApiQueryWatchlistRawIntegrationTest
extends ApiTestCase
{
14 protected function setUp() {
16 self
::$users['ApiQueryWatchlistRawIntegrationTestUser']
17 = $this->getMutableTestUser();
18 self
::$users['ApiQueryWatchlistRawIntegrationTestUser2']
19 = $this->getMutableTestUser();
22 private function getLoggedInTestUser() {
23 return self
::$users['ApiQueryWatchlistRawIntegrationTestUser']->getUser();
26 private function getNotLoggedInTestUser() {
27 return self
::$users['ApiQueryWatchlistRawIntegrationTestUser2']->getUser();
30 private function getWatchedItemStore() {
31 return MediaWikiServices
::getInstance()->getWatchedItemStore();
34 private function doListWatchlistRawRequest( array $params = [] ) {
35 return $this->doApiRequest( array_merge(
36 [ 'action' => 'query', 'list' => 'watchlistraw' ],
38 ), null, false, $this->getLoggedInTestUser() );
41 private function doGeneratorWatchlistRawRequest( array $params = [] ) {
42 return $this->doApiRequest( array_merge(
43 [ 'action' => 'query', 'generator' => 'watchlistraw' ],
45 ), null, false, $this->getLoggedInTestUser() );
48 private function getItemsFromApiResponse( array $response ) {
49 return $response[0]['watchlistraw'];
52 public function testListWatchlistRaw_returnsWatchedItems() {
53 $store = $this->getWatchedItemStore();
55 $this->getLoggedInTestUser(),
56 new TitleValue( 0, 'ApiQueryWatchlistRawIntegrationTestPage' )
59 $result = $this->doListWatchlistRawRequest();
61 $this->assertArrayHasKey( 'watchlistraw', $result[0] );
67 'title' => 'ApiQueryWatchlistRawIntegrationTestPage',
70 $this->getItemsFromApiResponse( $result )
74 public function testPropChanged_addsNotificationTimestamp() {
75 $target = new TitleValue( 0, 'ApiQueryWatchlistRawIntegrationTestPage' );
76 $otherUser = $this->getNotLoggedInTestUser();
78 $store = $this->getWatchedItemStore();
80 $store->addWatch( $this->getLoggedInTestUser(), $target );
81 $store->updateNotificationTimestamp(
87 $result = $this->doListWatchlistRawRequest( [ 'wrprop' => 'changed' ] );
93 'title' => 'ApiQueryWatchlistRawIntegrationTestPage',
94 'changed' => '2015-12-12T01:01:01Z',
97 $this->getItemsFromApiResponse( $result )
101 public function testNamespaceParam() {
102 $store = $this->getWatchedItemStore();
104 $store->addWatchBatchForUser( $this->getLoggedInTestUser(), [
105 new TitleValue( 0, 'ApiQueryWatchlistRawIntegrationTestPage' ),
106 new TitleValue( 1, 'ApiQueryWatchlistRawIntegrationTestPage' ),
109 $result = $this->doListWatchlistRawRequest( [ 'wrnamespace' => '0' ] );
115 'title' => 'ApiQueryWatchlistRawIntegrationTestPage',
118 $this->getItemsFromApiResponse( $result )
122 public function testShowChangedParams() {
123 $subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistRawIntegrationTestPage' );
124 $talkTarget = new TitleValue( 1, 'ApiQueryWatchlistRawIntegrationTestPage' );
125 $otherUser = $this->getNotLoggedInTestUser();
127 $store = $this->getWatchedItemStore();
129 $store->addWatchBatchForUser( $this->getLoggedInTestUser(), [
133 $store->updateNotificationTimestamp(
139 $resultChanged = $this->doListWatchlistRawRequest(
140 [ 'wrprop' => 'changed', 'wrshow' => WatchedItemQueryService
::FILTER_CHANGED
]
142 $resultNotChanged = $this->doListWatchlistRawRequest(
143 [ 'wrprop' => 'changed', 'wrshow' => WatchedItemQueryService
::FILTER_NOT_CHANGED
]
150 'title' => 'ApiQueryWatchlistRawIntegrationTestPage',
151 'changed' => '2015-12-12T01:01:01Z',
154 $this->getItemsFromApiResponse( $resultChanged )
161 'title' => 'Talk:ApiQueryWatchlistRawIntegrationTestPage',
164 $this->getItemsFromApiResponse( $resultNotChanged )
168 public function testLimitParam() {
169 $store = $this->getWatchedItemStore();
171 $store->addWatchBatchForUser( $this->getLoggedInTestUser(), [
172 new TitleValue( 0, 'ApiQueryWatchlistRawIntegrationTestPage1' ),
173 new TitleValue( 1, 'ApiQueryWatchlistRawIntegrationTestPage1' ),
174 new TitleValue( 0, 'ApiQueryWatchlistRawIntegrationTestPage2' ),
177 $resultWithoutLimit = $this->doListWatchlistRawRequest();
178 $resultWithLimit = $this->doListWatchlistRawRequest( [ 'wrlimit' => 2 ] );
184 'title' => 'ApiQueryWatchlistRawIntegrationTestPage1',
188 'title' => 'ApiQueryWatchlistRawIntegrationTestPage2',
192 'title' => 'Talk:ApiQueryWatchlistRawIntegrationTestPage1',
195 $this->getItemsFromApiResponse( $resultWithoutLimit )
201 'title' => 'ApiQueryWatchlistRawIntegrationTestPage1',
205 'title' => 'ApiQueryWatchlistRawIntegrationTestPage2',
208 $this->getItemsFromApiResponse( $resultWithLimit )
211 $this->assertArrayNotHasKey( 'continue', $resultWithoutLimit[0] );
212 $this->assertArrayHasKey( 'continue', $resultWithLimit[0] );
213 $this->assertArrayHasKey( 'wrcontinue', $resultWithLimit[0]['continue'] );
216 public function testDirParams() {
217 $store = $this->getWatchedItemStore();
219 $store->addWatchBatchForUser( $this->getLoggedInTestUser(), [
220 new TitleValue( 0, 'ApiQueryWatchlistRawIntegrationTestPage1' ),
221 new TitleValue( 1, 'ApiQueryWatchlistRawIntegrationTestPage1' ),
222 new TitleValue( 0, 'ApiQueryWatchlistRawIntegrationTestPage2' ),
225 $resultDirAsc = $this->doListWatchlistRawRequest( [ 'wrdir' => 'ascending' ] );
226 $resultDirDesc = $this->doListWatchlistRawRequest( [ 'wrdir' => 'descending' ] );
232 'title' => 'ApiQueryWatchlistRawIntegrationTestPage1',
236 'title' => 'ApiQueryWatchlistRawIntegrationTestPage2',
240 'title' => 'Talk:ApiQueryWatchlistRawIntegrationTestPage1',
243 $this->getItemsFromApiResponse( $resultDirAsc )
250 'title' => 'Talk:ApiQueryWatchlistRawIntegrationTestPage1',
254 'title' => 'ApiQueryWatchlistRawIntegrationTestPage2',
258 'title' => 'ApiQueryWatchlistRawIntegrationTestPage1',
261 $this->getItemsFromApiResponse( $resultDirDesc )
265 public function testAscendingIsDefaultOrder() {
266 $store = $this->getWatchedItemStore();
268 $store->addWatchBatchForUser( $this->getLoggedInTestUser(), [
269 new TitleValue( 0, 'ApiQueryWatchlistRawIntegrationTestPage1' ),
270 new TitleValue( 1, 'ApiQueryWatchlistRawIntegrationTestPage1' ),
271 new TitleValue( 0, 'ApiQueryWatchlistRawIntegrationTestPage2' ),
274 $resultNoDir = $this->doListWatchlistRawRequest();
275 $resultAscDir = $this->doListWatchlistRawRequest( [ 'wrdir' => 'ascending' ] );
278 $this->getItemsFromApiResponse( $resultNoDir ),
279 $this->getItemsFromApiResponse( $resultAscDir )
283 public function testFromTitleParam() {
284 $store = $this->getWatchedItemStore();
286 $store->addWatchBatchForUser( $this->getLoggedInTestUser(), [
287 new TitleValue( 0, 'ApiQueryWatchlistRawIntegrationTestPage1' ),
288 new TitleValue( 0, 'ApiQueryWatchlistRawIntegrationTestPage2' ),
289 new TitleValue( 0, 'ApiQueryWatchlistRawIntegrationTestPage3' ),
292 $result = $this->doListWatchlistRawRequest( [
293 'wrfromtitle' => 'ApiQueryWatchlistRawIntegrationTestPage2',
300 'title' => 'ApiQueryWatchlistRawIntegrationTestPage2',
304 'title' => 'ApiQueryWatchlistRawIntegrationTestPage3',
307 $this->getItemsFromApiResponse( $result )
311 public function testToTitleParam() {
312 $store = $this->getWatchedItemStore();
314 $store->addWatchBatchForUser( $this->getLoggedInTestUser(), [
315 new TitleValue( 0, 'ApiQueryWatchlistRawIntegrationTestPage1' ),
316 new TitleValue( 0, 'ApiQueryWatchlistRawIntegrationTestPage2' ),
317 new TitleValue( 0, 'ApiQueryWatchlistRawIntegrationTestPage3' ),
320 $result = $this->doListWatchlistRawRequest( [
321 'wrtotitle' => 'ApiQueryWatchlistRawIntegrationTestPage2',
328 'title' => 'ApiQueryWatchlistRawIntegrationTestPage1',
332 'title' => 'ApiQueryWatchlistRawIntegrationTestPage2',
335 $this->getItemsFromApiResponse( $result )
339 public function testContinueParam() {
340 $store = $this->getWatchedItemStore();
342 $store->addWatchBatchForUser( $this->getLoggedInTestUser(), [
343 new TitleValue( 0, 'ApiQueryWatchlistRawIntegrationTestPage1' ),
344 new TitleValue( 0, 'ApiQueryWatchlistRawIntegrationTestPage2' ),
345 new TitleValue( 0, 'ApiQueryWatchlistRawIntegrationTestPage3' ),
348 $firstResult = $this->doListWatchlistRawRequest( [ 'wrlimit' => 2 ] );
349 $continuationParam = $firstResult[0]['continue']['wrcontinue'];
351 $this->assertEquals( '0|ApiQueryWatchlistRawIntegrationTestPage3', $continuationParam );
353 $continuedResult = $this->doListWatchlistRawRequest( [ 'wrcontinue' => $continuationParam ] );
359 'title' => 'ApiQueryWatchlistRawIntegrationTestPage3',
362 $this->getItemsFromApiResponse( $continuedResult )
366 public function fromTitleToTitleContinueComboProvider() {
370 'wrfromtitle' => 'ApiQueryWatchlistRawIntegrationTestPage1',
371 'wrtotitle' => 'ApiQueryWatchlistRawIntegrationTestPage2',
374 [ 'ns' => 0, 'title' => 'ApiQueryWatchlistRawIntegrationTestPage1' ],
375 [ 'ns' => 0, 'title' => 'ApiQueryWatchlistRawIntegrationTestPage2' ],
380 'wrfromtitle' => 'ApiQueryWatchlistRawIntegrationTestPage1',
381 'wrcontinue' => '0|ApiQueryWatchlistRawIntegrationTestPage3',
384 [ 'ns' => 0, 'title' => 'ApiQueryWatchlistRawIntegrationTestPage3' ],
389 'wrtotitle' => 'ApiQueryWatchlistRawIntegrationTestPage3',
390 'wrcontinue' => '0|ApiQueryWatchlistRawIntegrationTestPage2',
393 [ 'ns' => 0, 'title' => 'ApiQueryWatchlistRawIntegrationTestPage2' ],
394 [ 'ns' => 0, 'title' => 'ApiQueryWatchlistRawIntegrationTestPage3' ],
399 'wrfromtitle' => 'ApiQueryWatchlistRawIntegrationTestPage1',
400 'wrtotitle' => 'ApiQueryWatchlistRawIntegrationTestPage3',
401 'wrcontinue' => '0|ApiQueryWatchlistRawIntegrationTestPage3',
404 [ 'ns' => 0, 'title' => 'ApiQueryWatchlistRawIntegrationTestPage3' ],
411 * @dataProvider fromTitleToTitleContinueComboProvider
413 public function testFromTitleToTitleContinueCombo( array $params, array $expectedItems ) {
414 $store = $this->getWatchedItemStore();
416 $store->addWatchBatchForUser( $this->getLoggedInTestUser(), [
417 new TitleValue( 0, 'ApiQueryWatchlistRawIntegrationTestPage1' ),
418 new TitleValue( 0, 'ApiQueryWatchlistRawIntegrationTestPage2' ),
419 new TitleValue( 0, 'ApiQueryWatchlistRawIntegrationTestPage3' ),
422 $result = $this->doListWatchlistRawRequest( $params );
424 $this->assertEquals( $expectedItems, $this->getItemsFromApiResponse( $result ) );
427 public function fromTitleToTitleContinueSelfContradictoryComboProvider() {
431 'wrfromtitle' => 'ApiQueryWatchlistRawIntegrationTestPage2',
432 'wrtotitle' => 'ApiQueryWatchlistRawIntegrationTestPage1',
437 'wrfromtitle' => 'ApiQueryWatchlistRawIntegrationTestPage1',
438 'wrtotitle' => 'ApiQueryWatchlistRawIntegrationTestPage2',
439 'wrdir' => 'descending',
444 'wrtotitle' => 'ApiQueryWatchlistRawIntegrationTestPage1',
445 'wrcontinue' => '0|ApiQueryWatchlistRawIntegrationTestPage2',
452 * @dataProvider fromTitleToTitleContinueSelfContradictoryComboProvider
454 public function testFromTitleToTitleContinueSelfContradictoryCombo( array $params ) {
455 $store = $this->getWatchedItemStore();
457 $store->addWatchBatchForUser( $this->getLoggedInTestUser(), [
458 new TitleValue( 0, 'ApiQueryWatchlistRawIntegrationTestPage1' ),
459 new TitleValue( 0, 'ApiQueryWatchlistRawIntegrationTestPage2' ),
462 $result = $this->doListWatchlistRawRequest( $params );
464 $this->assertEmpty( $this->getItemsFromApiResponse( $result ) );
465 $this->assertArrayNotHasKey( 'continue', $result[0] );
468 public function testOwnerAndTokenParams() {
469 $otherUser = $this->getNotLoggedInTestUser();
470 $otherUser->setOption( 'watchlisttoken', '1234567890' );
471 $otherUser->saveSettings();
473 $store = $this->getWatchedItemStore();
474 $store->addWatchBatchForUser( $otherUser, [
475 new TitleValue( 0, 'ApiQueryWatchlistRawIntegrationTestPage1' ),
476 new TitleValue( 1, 'ApiQueryWatchlistRawIntegrationTestPage1' ),
479 ObjectCache
::getMainWANInstance()->clearProcessCache();
480 $result = $this->doListWatchlistRawRequest( [
481 'wrowner' => $otherUser->getName(),
482 'wrtoken' => '1234567890',
489 'title' => 'ApiQueryWatchlistRawIntegrationTestPage1',
493 'title' => 'Talk:ApiQueryWatchlistRawIntegrationTestPage1',
496 $this->getItemsFromApiResponse( $result )
500 public function testOwnerAndTokenParams_wrongToken() {
501 $otherUser = $this->getNotLoggedInTestUser();
502 $otherUser->setOption( 'watchlisttoken', '1234567890' );
503 $otherUser->saveSettings();
505 $this->setExpectedException( ApiUsageException
::class, 'Incorrect watchlist token provided' );
507 $this->doListWatchlistRawRequest( [
508 'wrowner' => $otherUser->getName(),
509 'wrtoken' => 'wrong-token',
513 public function testOwnerAndTokenParams_userHasNoWatchlistToken() {
514 $this->setExpectedException( ApiUsageException
::class, 'Incorrect watchlist token provided' );
516 $this->doListWatchlistRawRequest( [
517 'wrowner' => $this->getNotLoggedInTestUser()->getName(),
518 'wrtoken' => 'some-watchlist-token',
522 public function testGeneratorWatchlistRawPropInfo_returnsWatchedItems() {
523 $store = $this->getWatchedItemStore();
525 $this->getLoggedInTestUser(),
526 new TitleValue( 0, 'ApiQueryWatchlistRawIntegrationTestPage' )
529 $result = $this->doGeneratorWatchlistRawRequest( [ 'prop' => 'info' ] );
531 $this->assertArrayHasKey( 'query', $result[0] );
532 $this->assertArrayHasKey( 'pages', $result[0]['query'] );
533 $this->assertCount( 1, $result[0]['query']['pages'] );
535 // $result[0]['query']['pages'] uses page ids as keys
536 $item = array_values( $result[0]['query']['pages'] )[0];
538 $this->assertEquals( 0, $item['ns'] );
539 $this->assertEquals( 'ApiQueryWatchlistRawIntegrationTestPage', $item['title'] );