Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / includes / api / ApiWatchTest.php
blobe0efb3db4cbbb21ba709d08666ef15eb850b3c5b
1 <?php
3 namespace MediaWiki\Tests\Api;
5 use MediaWiki\MainConfigNames;
6 use MediaWiki\Title\Title;
7 use Wikimedia\Timestamp\ConvertibleTimestamp;
9 /**
10 * @group API
11 * @group Database
12 * @group medium
13 * @covers \MediaWiki\Api\ApiWatch
15 class ApiWatchTest extends ApiTestCase {
16 protected function setUp(): void {
17 parent::setUp();
19 // Fake current time to be 2019-06-05T19:50:42Z
20 ConvertibleTimestamp::setFakeTime( 1559764242 );
22 $this->overrideConfigValues( [
23 MainConfigNames::WatchlistExpiry => true,
24 MainConfigNames::WatchlistExpiryMaxDuration => '6 months',
25 ] );
28 public function testWatch() {
29 // Watch for a duration greater than the max ($wgWatchlistExpiryMaxDuration),
30 // which should get changed to the max
31 $data = $this->doApiRequestWithToken( [
32 'action' => 'watch',
33 'titles' => 'Talk:Test page',
34 'expiry' => '99990101000000',
35 'formatversion' => 2
36 ] );
38 $res = $data[0]['watch'][0];
39 $this->assertSame( 'Talk:Test page', $res['title'] );
40 $this->assertSame( 1, $res['ns'] );
41 $this->assertTrue( $res['watched'] );
42 $this->assertSame( '2019-12-05T19:50:42Z', $res['expiry'] );
44 // Re-watch, changing the expiry to indefinite.
45 $data = $this->doApiRequestWithToken( [
46 'action' => 'watch',
47 'titles' => 'Talk:Test page',
48 'expiry' => 'indefinite',
49 'formatversion' => 2
50 ] );
51 $res = $data[0]['watch'][0];
52 $this->assertSame( 'infinity', $res['expiry'] );
55 public function testWatchWithExpiry() {
56 $store = $this->getServiceContainer()->getWatchedItemStore();
57 $user = $this->getTestUser()->getUser();
58 $pageTitle = 'TestWatchWithExpiry';
60 // First watch without expiry (indefinite).
61 $this->doApiRequestWithToken( [
62 'action' => 'watch',
63 'titles' => $pageTitle,
64 ], null, $user );
66 // Ensure page was added to the user's watchlist, and expiry is null (not set).
67 [ $item ] = $store->getWatchedItemsForUser( $user );
68 $this->assertSame( $pageTitle, $item->getTarget()->getDBkey() );
69 $this->assertNull( $item->getExpiry() );
71 // Re-watch, setting an expiry.
72 $expiry = '2 weeks';
73 $this->doApiRequestWithToken( [
74 'action' => 'watch',
75 'titles' => $pageTitle,
76 'expiry' => $expiry,
77 ], null, $user );
78 [ $item ] = $store->getWatchedItemsForUser( $user );
79 $this->assertSame( '20190619195042', $item->getExpiry() );
81 // Re-watch again, providing no expiry parameter, so expiry should remain unchanged.
82 $oldExpiry = $item->getExpiry();
83 $this->doApiRequestWithToken( [
84 'action' => 'watch',
85 'titles' => $pageTitle,
86 ], null, $user );
87 [ $item ] = $store->getWatchedItemsForUser( $user );
88 $this->assertSame( $oldExpiry, $item->getExpiry() );
91 public function testWatchInvalidExpiry() {
92 $this->expectApiErrorCode( 'badexpiry' );
94 $this->doApiRequestWithToken( [
95 'action' => 'watch',
96 'titles' => 'Talk:Test page',
97 'expiry' => 'invalid expiry',
98 'formatversion' => 2
99 ] );
102 public function testWatchExpiryInPast() {
103 $this->expectApiErrorCode( 'badexpiry-past' );
105 $this->doApiRequestWithToken( [
106 'action' => 'watch',
107 'titles' => 'Talk:Test page',
108 'expiry' => '20010101000000',
109 'formatversion' => 2
110 ] );
113 public function testWatchEdit() {
114 $data = $this->doApiRequestWithToken( [
115 'action' => 'edit',
116 'title' => 'Help:TestWatchEdit', // Help namespace is hopefully wikitext
117 'text' => 'new text',
118 'watchlist' => 'watch'
119 ] );
121 $this->assertArrayHasKey( 'edit', $data[0] );
122 $this->assertArrayHasKey( 'result', $data[0]['edit'] );
123 $this->assertEquals( 'Success', $data[0]['edit']['result'] );
125 return $data;
129 * @depends testWatchEdit
131 public function testWatchClear() {
132 $data = $this->doApiRequest( [
133 'action' => 'query',
134 'wllimit' => 'max',
135 'list' => 'watchlist' ] );
137 if ( isset( $data[0]['query']['watchlist'] ) ) {
138 $wl = $data[0]['query']['watchlist'];
140 foreach ( $wl as $page ) {
141 $data = $this->doApiRequestWithToken( [
142 'action' => 'watch',
143 'title' => $page['title'],
144 'unwatch' => true,
145 ] );
148 $data = $this->doApiRequest( [
149 'action' => 'query',
150 'list' => 'watchlist' ], $data );
151 $this->assertArrayHasKey( 'query', $data[0] );
152 $this->assertArrayHasKey( 'watchlist', $data[0]['query'] );
153 foreach ( $data[0]['query']['watchlist'] as $index => $item ) {
154 // Previous tests may insert an invalid title
155 // like ":ApiEditPageTest testNonTextEdit", which
156 // can't be cleared.
157 if ( str_starts_with( $item['title'], ':' ) ) {
158 unset( $data[0]['query']['watchlist'][$index] );
161 $this->assertSame( [], $data[0]['query']['watchlist'] );
163 return $data;
166 public function testWatchProtect() {
167 $pageTitle = 'Help:TestWatchProtect';
168 $this->getExistingTestPage( $pageTitle );
169 $data = $this->doApiRequestWithToken( [
170 'action' => 'protect',
171 'title' => $pageTitle,
172 'protections' => 'edit=sysop',
173 'watchlist' => 'unwatch'
174 ] );
176 $this->assertArrayHasKey( 'protect', $data[0] );
177 $this->assertArrayHasKey( 'protections', $data[0]['protect'] );
178 $this->assertCount( 1, $data[0]['protect']['protections'] );
179 $this->assertArrayHasKey( 'edit', $data[0]['protect']['protections'][0] );
182 public function testWatchRollback() {
183 $titleText = 'Help:TestWatchRollback';
184 $title = Title::makeTitle( NS_HELP, 'TestWatchRollback' );
185 $revertingUser = $this->getTestSysop()->getUser();
186 $revertedUser = $this->getTestUser()->getUser();
187 $this->editPage( $title, 'Edit 1', '', NS_MAIN, $revertingUser );
188 $this->editPage( $title, 'Edit 2', '', NS_MAIN, $revertedUser );
190 $watchlistManager = $this->getServiceContainer()->getWatchlistManager();
192 // This (and assertTrue below) are mostly for completeness.
193 $this->assertFalse( $watchlistManager->isWatched( $revertingUser, $title ) );
195 $data = $this->doApiRequestWithToken( [
196 'action' => 'rollback',
197 'title' => $titleText,
198 'user' => $revertedUser,
199 'watchlist' => 'watch'
200 ] );
202 $this->assertArrayHasKey( 'rollback', $data[0] );
203 $this->assertArrayHasKey( 'title', $data[0]['rollback'] );
204 $this->assertTrue( $watchlistManager->isWatched( $revertingUser, $title ) );