3 namespace MediaWiki\Tests\Api
;
5 use MediaWiki\MainConfigNames
;
6 use MediaWiki\Title\Title
;
7 use Wikimedia\Timestamp\ConvertibleTimestamp
;
13 * @covers \MediaWiki\Api\ApiWatch
15 class ApiWatchTest
extends ApiTestCase
{
16 protected function setUp(): void
{
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',
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( [
33 'titles' => 'Talk:Test page',
34 'expiry' => '99990101000000',
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( [
47 'titles' => 'Talk:Test page',
48 'expiry' => 'indefinite',
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( [
63 'titles' => $pageTitle,
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.
73 $this->doApiRequestWithToken( [
75 'titles' => $pageTitle,
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( [
85 'titles' => $pageTitle,
87 [ $item ] = $store->getWatchedItemsForUser( $user );
88 $this->assertSame( $oldExpiry, $item->getExpiry() );
91 public function testWatchInvalidExpiry() {
92 $this->expectApiErrorCode( 'badexpiry' );
94 $this->doApiRequestWithToken( [
96 'titles' => 'Talk:Test page',
97 'expiry' => 'invalid expiry',
102 public function testWatchExpiryInPast() {
103 $this->expectApiErrorCode( 'badexpiry-past' );
105 $this->doApiRequestWithToken( [
107 'titles' => 'Talk:Test page',
108 'expiry' => '20010101000000',
113 public function testWatchEdit() {
114 $data = $this->doApiRequestWithToken( [
116 'title' => 'Help:TestWatchEdit', // Help namespace is hopefully wikitext
117 'text' => 'new text',
118 'watchlist' => 'watch'
121 $this->assertArrayHasKey( 'edit', $data[0] );
122 $this->assertArrayHasKey( 'result', $data[0]['edit'] );
123 $this->assertEquals( 'Success', $data[0]['edit']['result'] );
129 * @depends testWatchEdit
131 public function testWatchClear() {
132 $data = $this->doApiRequest( [
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( [
143 'title' => $page['title'],
148 $data = $this->doApiRequest( [
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
157 if ( str_starts_with( $item['title'], ':' ) ) {
158 unset( $data[0]['query']['watchlist'][$index] );
161 $this->assertSame( [], $data[0]['query']['watchlist'] );
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'
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'
202 $this->assertArrayHasKey( 'rollback', $data[0] );
203 $this->assertArrayHasKey( 'title', $data[0]['rollback'] );
204 $this->assertTrue( $watchlistManager->isWatched( $revertingUser, $title ) );