rdbms: Rename "memCache" to "memStash" in LBFactory
[mediawiki.git] / tests / phpunit / includes / api / ApiQueryWatchlistRawIntegrationTest.php
blob0f01664e720af897a8012a1984506feeac20fc72
1 <?php
3 use MediaWiki\MediaWikiServices;
5 /**
6 * @group API
7 * @group Database
8 * @group medium
10 * @covers ApiQueryWatchlistRaw
12 class ApiQueryWatchlistRawIntegrationTest extends ApiTestCase {
14 protected function setUp() {
15 parent::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' ],
38 $params
39 ) );
42 private function doGeneratorWatchlistRawRequest( array $params = [] ) {
43 return $this->doApiRequest( array_merge(
44 [ 'action' => 'query', 'generator' => 'watchlistraw' ],
45 $params
46 ) );
49 private function getItemsFromApiResponse( array $response ) {
50 return $response[0]['watchlistraw'];
53 public function testListWatchlistRaw_returnsWatchedItems() {
54 $store = $this->getWatchedItemStore();
55 $store->addWatch(
56 $this->getLoggedInTestUser(),
57 new TitleValue( 0, 'ApiQueryWatchlistRawIntegrationTestPage' )
60 $result = $this->doListWatchlistRawRequest();
62 $this->assertArrayHasKey( 'watchlistraw', $result[0] );
64 $this->assertEquals(
67 'ns' => 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(
83 $otherUser,
84 $target,
85 '20151212010101'
88 $result = $this->doListWatchlistRawRequest( [ 'wrprop' => 'changed' ] );
90 $this->assertEquals(
93 'ns' => 0,
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' ),
108 ] );
110 $result = $this->doListWatchlistRawRequest( [ 'wrnamespace' => '0' ] );
112 $this->assertEquals(
115 'ns' => 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(), [
131 $subjectTarget,
132 $talkTarget,
133 ] );
134 $store->updateNotificationTimestamp(
135 $otherUser,
136 $subjectTarget,
137 '20151212010101'
140 $resultChanged = $this->doListWatchlistRawRequest(
141 [ 'wrprop' => 'changed', 'wrshow' => WatchedItemQueryService::FILTER_CHANGED ]
143 $resultNotChanged = $this->doListWatchlistRawRequest(
144 [ 'wrprop' => 'changed', 'wrshow' => WatchedItemQueryService::FILTER_NOT_CHANGED ]
147 $this->assertEquals(
150 'ns' => 0,
151 'title' => 'ApiQueryWatchlistRawIntegrationTestPage',
152 'changed' => '2015-12-12T01:01:01Z',
155 $this->getItemsFromApiResponse( $resultChanged )
158 $this->assertEquals(
161 'ns' => 1,
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' ),
176 ] );
178 $resultWithoutLimit = $this->doListWatchlistRawRequest();
179 $resultWithLimit = $this->doListWatchlistRawRequest( [ 'wrlimit' => 2 ] );
181 $this->assertEquals(
184 'ns' => 0,
185 'title' => 'ApiQueryWatchlistRawIntegrationTestPage1',
188 'ns' => 0,
189 'title' => 'ApiQueryWatchlistRawIntegrationTestPage2',
192 'ns' => 1,
193 'title' => 'Talk:ApiQueryWatchlistRawIntegrationTestPage1',
196 $this->getItemsFromApiResponse( $resultWithoutLimit )
198 $this->assertEquals(
201 'ns' => 0,
202 'title' => 'ApiQueryWatchlistRawIntegrationTestPage1',
205 'ns' => 0,
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' ),
224 ] );
226 $resultDirAsc = $this->doListWatchlistRawRequest( [ 'wrdir' => 'ascending' ] );
227 $resultDirDesc = $this->doListWatchlistRawRequest( [ 'wrdir' => 'descending' ] );
229 $this->assertEquals(
232 'ns' => 0,
233 'title' => 'ApiQueryWatchlistRawIntegrationTestPage1',
236 'ns' => 0,
237 'title' => 'ApiQueryWatchlistRawIntegrationTestPage2',
240 'ns' => 1,
241 'title' => 'Talk:ApiQueryWatchlistRawIntegrationTestPage1',
244 $this->getItemsFromApiResponse( $resultDirAsc )
247 $this->assertEquals(
250 'ns' => 1,
251 'title' => 'Talk:ApiQueryWatchlistRawIntegrationTestPage1',
254 'ns' => 0,
255 'title' => 'ApiQueryWatchlistRawIntegrationTestPage2',
258 'ns' => 0,
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' ),
273 ] );
275 $resultNoDir = $this->doListWatchlistRawRequest();
276 $resultAscDir = $this->doListWatchlistRawRequest( [ 'wrdir' => 'ascending' ] );
278 $this->assertEquals(
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' ),
291 ] );
293 $result = $this->doListWatchlistRawRequest( [
294 'wrfromtitle' => 'ApiQueryWatchlistRawIntegrationTestPage2',
295 ] );
297 $this->assertEquals(
300 'ns' => 0,
301 'title' => 'ApiQueryWatchlistRawIntegrationTestPage2',
304 'ns' => 0,
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' ),
319 ] );
321 $result = $this->doListWatchlistRawRequest( [
322 'wrtotitle' => 'ApiQueryWatchlistRawIntegrationTestPage2',
323 ] );
325 $this->assertEquals(
328 'ns' => 0,
329 'title' => 'ApiQueryWatchlistRawIntegrationTestPage1',
332 'ns' => 0,
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' ),
347 ] );
349 $firstResult = $this->doListWatchlistRawRequest( [ 'wrlimit' => 2 ] );
350 $continuationParam = $firstResult[0]['continue']['wrcontinue'];
352 $this->assertEquals( '0|ApiQueryWatchlistRawIntegrationTestPage3', $continuationParam );
354 $continuedResult = $this->doListWatchlistRawRequest( [ 'wrcontinue' => $continuationParam ] );
356 $this->assertEquals(
359 'ns' => 0,
360 'title' => 'ApiQueryWatchlistRawIntegrationTestPage3',
363 $this->getItemsFromApiResponse( $continuedResult )
367 public function fromTitleToTitleContinueComboProvider() {
368 return [
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' ),
421 ] );
423 $result = $this->doListWatchlistRawRequest( $params );
425 $this->assertEquals( $expectedItems, $this->getItemsFromApiResponse( $result ) );
428 public function fromTitleToTitleContinueSelfContradictoryComboProvider() {
429 return [
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' ),
461 ] );
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' ),
478 ] );
480 ObjectCache::getMainWANInstance()->clearProcessCache();
481 $result = $this->doListWatchlistRawRequest( [
482 'wrowner' => $otherUser->getName(),
483 'wrtoken' => '1234567890',
484 ] );
486 $this->assertEquals(
489 'ns' => 0,
490 'title' => 'ApiQueryWatchlistRawIntegrationTestPage1',
493 'ns' => 1,
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',
511 ] );
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',
520 ] );
523 public function testGeneratorWatchlistRawPropInfo_returnsWatchedItems() {
524 $store = $this->getWatchedItemStore();
525 $store->addWatch(
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'] );