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();
20 $this->doLogin( 'ApiQueryWatchlistRawIntegrationTestUser' );
23 private function getLoggedInTestUser() {
24 return self
::$users['ApiQueryWatchlistRawIntegrationTestUser']->getUser();
27 private function getNotLoggedInTestUser() {
28 return self
::$users['ApiQueryWatchlistRawIntegrationTestUser2']->getUser();
31 private function getWatchedItemStore() {
32 return MediaWikiServices
::getInstance()->getWatchedItemStore();
35 private function doListWatchlistRawRequest( array $params = [] ) {
36 return $this->doApiRequest( array_merge(
37 [ 'action' => 'query', 'list' => 'watchlistraw' ],
42 private function doGeneratorWatchlistRawRequest( array $params = [] ) {
43 return $this->doApiRequest( array_merge(
44 [ 'action' => 'query', 'generator' => 'watchlistraw' ],
49 private function getItemsFromApiResponse( array $response ) {
50 return $response[0]['watchlistraw'];
53 public function testListWatchlistRaw_returnsWatchedItems() {
54 $store = $this->getWatchedItemStore();
56 $this->getLoggedInTestUser(),
57 new TitleValue( 0, 'ApiQueryWatchlistRawIntegrationTestPage' )
60 $result = $this->doListWatchlistRawRequest();
62 $this->assertArrayHasKey( 'watchlistraw', $result[0] );
68 'title' => 'ApiQueryWatchlistRawIntegrationTestPage',
71 $this->getItemsFromApiResponse( $result )
75 public function testPropChanged_addsNotificationTimestamp() {
76 $target = new TitleValue( 0, 'ApiQueryWatchlistRawIntegrationTestPage' );
77 $otherUser = $this->getNotLoggedInTestUser();
79 $store = $this->getWatchedItemStore();
81 $store->addWatch( $this->getLoggedInTestUser(), $target );
82 $store->updateNotificationTimestamp(
88 $result = $this->doListWatchlistRawRequest( [ 'wrprop' => 'changed' ] );
94 'title' => 'ApiQueryWatchlistRawIntegrationTestPage',
95 'changed' => '2015-12-12T01:01:01Z',
98 $this->getItemsFromApiResponse( $result )
102 public function testNamespaceParam() {
103 $store = $this->getWatchedItemStore();
105 $store->addWatchBatchForUser( $this->getLoggedInTestUser(), [
106 new TitleValue( 0, 'ApiQueryWatchlistRawIntegrationTestPage' ),
107 new TitleValue( 1, 'ApiQueryWatchlistRawIntegrationTestPage' ),
110 $result = $this->doListWatchlistRawRequest( [ 'wrnamespace' => '0' ] );
116 'title' => 'ApiQueryWatchlistRawIntegrationTestPage',
119 $this->getItemsFromApiResponse( $result )
123 public function testShowChangedParams() {
124 $subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistRawIntegrationTestPage' );
125 $talkTarget = new TitleValue( 1, 'ApiQueryWatchlistRawIntegrationTestPage' );
126 $otherUser = $this->getNotLoggedInTestUser();
128 $store = $this->getWatchedItemStore();
130 $store->addWatchBatchForUser( $this->getLoggedInTestUser(), [
134 $store->updateNotificationTimestamp(
140 $resultChanged = $this->doListWatchlistRawRequest(
141 [ 'wrprop' => 'changed', 'wrshow' => WatchedItemQueryService
::FILTER_CHANGED
]
143 $resultNotChanged = $this->doListWatchlistRawRequest(
144 [ 'wrprop' => 'changed', 'wrshow' => WatchedItemQueryService
::FILTER_NOT_CHANGED
]
151 'title' => 'ApiQueryWatchlistRawIntegrationTestPage',
152 'changed' => '2015-12-12T01:01:01Z',
155 $this->getItemsFromApiResponse( $resultChanged )
162 'title' => 'Talk:ApiQueryWatchlistRawIntegrationTestPage',
165 $this->getItemsFromApiResponse( $resultNotChanged )
169 public function testLimitParam() {
170 $store = $this->getWatchedItemStore();
172 $store->addWatchBatchForUser( $this->getLoggedInTestUser(), [
173 new TitleValue( 0, 'ApiQueryWatchlistRawIntegrationTestPage1' ),
174 new TitleValue( 1, 'ApiQueryWatchlistRawIntegrationTestPage1' ),
175 new TitleValue( 0, 'ApiQueryWatchlistRawIntegrationTestPage2' ),
178 $resultWithoutLimit = $this->doListWatchlistRawRequest();
179 $resultWithLimit = $this->doListWatchlistRawRequest( [ 'wrlimit' => 2 ] );
185 'title' => 'ApiQueryWatchlistRawIntegrationTestPage1',
189 'title' => 'ApiQueryWatchlistRawIntegrationTestPage2',
193 'title' => 'Talk:ApiQueryWatchlistRawIntegrationTestPage1',
196 $this->getItemsFromApiResponse( $resultWithoutLimit )
202 'title' => 'ApiQueryWatchlistRawIntegrationTestPage1',
206 'title' => 'ApiQueryWatchlistRawIntegrationTestPage2',
209 $this->getItemsFromApiResponse( $resultWithLimit )
212 $this->assertArrayNotHasKey( 'continue', $resultWithoutLimit[0] );
213 $this->assertArrayHasKey( 'continue', $resultWithLimit[0] );
214 $this->assertArrayHasKey( 'wrcontinue', $resultWithLimit[0]['continue'] );
217 public function testDirParams() {
218 $store = $this->getWatchedItemStore();
220 $store->addWatchBatchForUser( $this->getLoggedInTestUser(), [
221 new TitleValue( 0, 'ApiQueryWatchlistRawIntegrationTestPage1' ),
222 new TitleValue( 1, 'ApiQueryWatchlistRawIntegrationTestPage1' ),
223 new TitleValue( 0, 'ApiQueryWatchlistRawIntegrationTestPage2' ),
226 $resultDirAsc = $this->doListWatchlistRawRequest( [ 'wrdir' => 'ascending' ] );
227 $resultDirDesc = $this->doListWatchlistRawRequest( [ 'wrdir' => 'descending' ] );
233 'title' => 'ApiQueryWatchlistRawIntegrationTestPage1',
237 'title' => 'ApiQueryWatchlistRawIntegrationTestPage2',
241 'title' => 'Talk:ApiQueryWatchlistRawIntegrationTestPage1',
244 $this->getItemsFromApiResponse( $resultDirAsc )
251 'title' => 'Talk:ApiQueryWatchlistRawIntegrationTestPage1',
255 'title' => 'ApiQueryWatchlistRawIntegrationTestPage2',
259 'title' => 'ApiQueryWatchlistRawIntegrationTestPage1',
262 $this->getItemsFromApiResponse( $resultDirDesc )
266 public function testAscendingIsDefaultOrder() {
267 $store = $this->getWatchedItemStore();
269 $store->addWatchBatchForUser( $this->getLoggedInTestUser(), [
270 new TitleValue( 0, 'ApiQueryWatchlistRawIntegrationTestPage1' ),
271 new TitleValue( 1, 'ApiQueryWatchlistRawIntegrationTestPage1' ),
272 new TitleValue( 0, 'ApiQueryWatchlistRawIntegrationTestPage2' ),
275 $resultNoDir = $this->doListWatchlistRawRequest();
276 $resultAscDir = $this->doListWatchlistRawRequest( [ 'wrdir' => 'ascending' ] );
279 $this->getItemsFromApiResponse( $resultNoDir ),
280 $this->getItemsFromApiResponse( $resultAscDir )
284 public function testFromTitleParam() {
285 $store = $this->getWatchedItemStore();
287 $store->addWatchBatchForUser( $this->getLoggedInTestUser(), [
288 new TitleValue( 0, 'ApiQueryWatchlistRawIntegrationTestPage1' ),
289 new TitleValue( 0, 'ApiQueryWatchlistRawIntegrationTestPage2' ),
290 new TitleValue( 0, 'ApiQueryWatchlistRawIntegrationTestPage3' ),
293 $result = $this->doListWatchlistRawRequest( [
294 'wrfromtitle' => 'ApiQueryWatchlistRawIntegrationTestPage2',
301 'title' => 'ApiQueryWatchlistRawIntegrationTestPage2',
305 'title' => 'ApiQueryWatchlistRawIntegrationTestPage3',
308 $this->getItemsFromApiResponse( $result )
312 public function testToTitleParam() {
313 $store = $this->getWatchedItemStore();
315 $store->addWatchBatchForUser( $this->getLoggedInTestUser(), [
316 new TitleValue( 0, 'ApiQueryWatchlistRawIntegrationTestPage1' ),
317 new TitleValue( 0, 'ApiQueryWatchlistRawIntegrationTestPage2' ),
318 new TitleValue( 0, 'ApiQueryWatchlistRawIntegrationTestPage3' ),
321 $result = $this->doListWatchlistRawRequest( [
322 'wrtotitle' => 'ApiQueryWatchlistRawIntegrationTestPage2',
329 'title' => 'ApiQueryWatchlistRawIntegrationTestPage1',
333 'title' => 'ApiQueryWatchlistRawIntegrationTestPage2',
336 $this->getItemsFromApiResponse( $result )
340 public function testContinueParam() {
341 $store = $this->getWatchedItemStore();
343 $store->addWatchBatchForUser( $this->getLoggedInTestUser(), [
344 new TitleValue( 0, 'ApiQueryWatchlistRawIntegrationTestPage1' ),
345 new TitleValue( 0, 'ApiQueryWatchlistRawIntegrationTestPage2' ),
346 new TitleValue( 0, 'ApiQueryWatchlistRawIntegrationTestPage3' ),
349 $firstResult = $this->doListWatchlistRawRequest( [ 'wrlimit' => 2 ] );
350 $continuationParam = $firstResult[0]['continue']['wrcontinue'];
352 $this->assertEquals( '0|ApiQueryWatchlistRawIntegrationTestPage3', $continuationParam );
354 $continuedResult = $this->doListWatchlistRawRequest( [ 'wrcontinue' => $continuationParam ] );
360 'title' => 'ApiQueryWatchlistRawIntegrationTestPage3',
363 $this->getItemsFromApiResponse( $continuedResult )
367 public function fromTitleToTitleContinueComboProvider() {
371 'wrfromtitle' => 'ApiQueryWatchlistRawIntegrationTestPage1',
372 'wrtotitle' => 'ApiQueryWatchlistRawIntegrationTestPage2',
375 [ 'ns' => 0, 'title' => 'ApiQueryWatchlistRawIntegrationTestPage1' ],
376 [ 'ns' => 0, 'title' => 'ApiQueryWatchlistRawIntegrationTestPage2' ],
381 'wrfromtitle' => 'ApiQueryWatchlistRawIntegrationTestPage1',
382 'wrcontinue' => '0|ApiQueryWatchlistRawIntegrationTestPage3',
385 [ 'ns' => 0, 'title' => 'ApiQueryWatchlistRawIntegrationTestPage3' ],
390 'wrtotitle' => 'ApiQueryWatchlistRawIntegrationTestPage3',
391 'wrcontinue' => '0|ApiQueryWatchlistRawIntegrationTestPage2',
394 [ 'ns' => 0, 'title' => 'ApiQueryWatchlistRawIntegrationTestPage2' ],
395 [ 'ns' => 0, 'title' => 'ApiQueryWatchlistRawIntegrationTestPage3' ],
400 'wrfromtitle' => 'ApiQueryWatchlistRawIntegrationTestPage1',
401 'wrtotitle' => 'ApiQueryWatchlistRawIntegrationTestPage3',
402 'wrcontinue' => '0|ApiQueryWatchlistRawIntegrationTestPage3',
405 [ 'ns' => 0, 'title' => 'ApiQueryWatchlistRawIntegrationTestPage3' ],
412 * @dataProvider fromTitleToTitleContinueComboProvider
414 public function testFromTitleToTitleContinueCombo( array $params, array $expectedItems ) {
415 $store = $this->getWatchedItemStore();
417 $store->addWatchBatchForUser( $this->getLoggedInTestUser(), [
418 new TitleValue( 0, 'ApiQueryWatchlistRawIntegrationTestPage1' ),
419 new TitleValue( 0, 'ApiQueryWatchlistRawIntegrationTestPage2' ),
420 new TitleValue( 0, 'ApiQueryWatchlistRawIntegrationTestPage3' ),
423 $result = $this->doListWatchlistRawRequest( $params );
425 $this->assertEquals( $expectedItems, $this->getItemsFromApiResponse( $result ) );
428 public function fromTitleToTitleContinueSelfContradictoryComboProvider() {
432 'wrfromtitle' => 'ApiQueryWatchlistRawIntegrationTestPage2',
433 'wrtotitle' => 'ApiQueryWatchlistRawIntegrationTestPage1',
438 'wrfromtitle' => 'ApiQueryWatchlistRawIntegrationTestPage1',
439 'wrtotitle' => 'ApiQueryWatchlistRawIntegrationTestPage2',
440 'wrdir' => 'descending',
445 'wrtotitle' => 'ApiQueryWatchlistRawIntegrationTestPage1',
446 'wrcontinue' => '0|ApiQueryWatchlistRawIntegrationTestPage2',
453 * @dataProvider fromTitleToTitleContinueSelfContradictoryComboProvider
455 public function testFromTitleToTitleContinueSelfContradictoryCombo( array $params ) {
456 $store = $this->getWatchedItemStore();
458 $store->addWatchBatchForUser( $this->getLoggedInTestUser(), [
459 new TitleValue( 0, 'ApiQueryWatchlistRawIntegrationTestPage1' ),
460 new TitleValue( 0, 'ApiQueryWatchlistRawIntegrationTestPage2' ),
463 $result = $this->doListWatchlistRawRequest( $params );
465 $this->assertEmpty( $this->getItemsFromApiResponse( $result ) );
466 $this->assertArrayNotHasKey( 'continue', $result[0] );
469 public function testOwnerAndTokenParams() {
470 $otherUser = $this->getNotLoggedInTestUser();
471 $otherUser->setOption( 'watchlisttoken', '1234567890' );
472 $otherUser->saveSettings();
474 $store = $this->getWatchedItemStore();
475 $store->addWatchBatchForUser( $otherUser, [
476 new TitleValue( 0, 'ApiQueryWatchlistRawIntegrationTestPage1' ),
477 new TitleValue( 1, 'ApiQueryWatchlistRawIntegrationTestPage1' ),
480 ObjectCache
::getMainWANInstance()->clearProcessCache();
481 $result = $this->doListWatchlistRawRequest( [
482 'wrowner' => $otherUser->getName(),
483 'wrtoken' => '1234567890',
490 'title' => 'ApiQueryWatchlistRawIntegrationTestPage1',
494 'title' => 'Talk:ApiQueryWatchlistRawIntegrationTestPage1',
497 $this->getItemsFromApiResponse( $result )
501 public function testOwnerAndTokenParams_wrongToken() {
502 $otherUser = $this->getNotLoggedInTestUser();
503 $otherUser->setOption( 'watchlisttoken', '1234567890' );
504 $otherUser->saveSettings();
506 $this->setExpectedException( ApiUsageException
::class, 'Incorrect watchlist token provided' );
508 $this->doListWatchlistRawRequest( [
509 'wrowner' => $otherUser->getName(),
510 'wrtoken' => 'wrong-token',
514 public function testOwnerAndTokenParams_userHasNoWatchlistToken() {
515 $this->setExpectedException( ApiUsageException
::class, 'Incorrect watchlist token provided' );
517 $this->doListWatchlistRawRequest( [
518 'wrowner' => $this->getNotLoggedInTestUser()->getName(),
519 'wrtoken' => 'some-watchlist-token',
523 public function testGeneratorWatchlistRawPropInfo_returnsWatchedItems() {
524 $store = $this->getWatchedItemStore();
526 $this->getLoggedInTestUser(),
527 new TitleValue( 0, 'ApiQueryWatchlistRawIntegrationTestPage' )
530 $result = $this->doGeneratorWatchlistRawRequest( [ 'prop' => 'info' ] );
532 $this->assertArrayHasKey( 'query', $result[0] );
533 $this->assertArrayHasKey( 'pages', $result[0]['query'] );
534 $this->assertCount( 1, $result[0]['query']['pages'] );
536 // $result[0]['query']['pages'] uses page ids as keys
537 $item = array_values( $result[0]['query']['pages'] )[0];
539 $this->assertEquals( 0, $item['ns'] );
540 $this->assertEquals( 'ApiQueryWatchlistRawIntegrationTestPage', $item['title'] );