| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Api; |
| 4 |
|
| 5 |
use MediaWiki\MainConfigNames; |
| 6 |
use MediaWiki\Title\Title; |
| 7 |
use Wikimedia\Timestamp\ConvertibleTimestamp; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
class ApiWatchTest extends ApiTestCase { |
| 16 |
protected function setUp(): void { |
| 17 |
parent::setUp(); |
| 18 |
|
| 19 |
|
| 20 |
ConvertibleTimestamp::setFakeTime( 1559764242 ); |
| 21 |
|
| 22 |
$this->overrideConfigValues( [ |
| 23 |
MainConfigNames::WatchlistExpiry => true, |
| 24 |
MainConfigNames::WatchlistExpiryMaxDuration => '6 months', |
| 25 |
] ); |
| 26 |
} |
| 27 |
|
| 28 |
public function testWatch() { |
| 29 |
|
| 30 |
|
| 31 |
$data = $this->doApiRequestWithToken( [ |
| 32 |
'action' => 'watch', |
| 33 |
'titles' => 'Talk:Test page', |
| 34 |
'expiry' => '99990101000000', |
| 35 |
'formatversion' => 2 |
| 36 |
] ); |
| 37 |
|
| 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'] ); |
| 43 |
|
| 44 |
|
| 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'] ); |
| 53 |
} |
| 54 |
|
| 55 |
public function testWatchWithExpiry() { |
| 56 |
$store = $this->getServiceContainer()->getWatchedItemStore(); |
| 57 |
$user = $this->getTestUser()->getUser(); |
| 58 |
$pageTitle = 'TestWatchWithExpiry'; |
| 59 |
|
| 60 |
|
| 61 |
$this->doApiRequestWithToken( [ |
| 62 |
'action' => 'watch', |
| 63 |
'titles' => $pageTitle, |
| 64 |
], null, $user ); |
| 65 |
|
| 66 |
|
| 67 |
[ $item ] = $store->getWatchedItemsForUser( $user ); |
| 68 |
$this->assertSame( $pageTitle, $item->getTarget()->getDBkey() ); |
| 69 |
$this->assertNull( $item->getExpiry() ); |
| 70 |
|
| 71 |
|
| 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() ); |
| 80 |
|
| 81 |
|
| 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() ); |
| 89 |
} |
| 90 |
|
| 91 |
public function testWatchInvalidExpiry() { |
| 92 |
$this->expectApiErrorCode( 'badexpiry' ); |
| 93 |
|
| 94 |
$this->doApiRequestWithToken( [ |
| 95 |
'action' => 'watch', |
| 96 |
'titles' => 'Talk:Test page', |
| 97 |
'expiry' => 'invalid expiry', |
| 98 |
'formatversion' => 2 |
| 99 |
] ); |
| 100 |
} |
| 101 |
|
| 102 |
public function testWatchExpiryInPast() { |
| 103 |
$this->expectApiErrorCode( 'badexpiry-past' ); |
| 104 |
|
| 105 |
$this->doApiRequestWithToken( [ |
| 106 |
'action' => 'watch', |
| 107 |
'titles' => 'Talk:Test page', |
| 108 |
'expiry' => '20010101000000', |
| 109 |
'formatversion' => 2 |
| 110 |
] ); |
| 111 |
} |
| 112 |
|
| 113 |
public function testWatchEdit() { |
| 114 |
$data = $this->doApiRequestWithToken( [ |
| 115 |
'action' => 'edit', |
| 116 |
'title' => 'Help:TestWatchEdit', |
| 117 |
'text' => 'new text', |
| 118 |
'watchlist' => 'watch' |
| 119 |
] ); |
| 120 |
|
| 121 |
$this->assertArrayHasKey( 'edit', $data[0] ); |
| 122 |
$this->assertArrayHasKey( 'result', $data[0]['edit'] ); |
| 123 |
$this->assertEquals( 'Success', $data[0]['edit']['result'] ); |
| 124 |
|
| 125 |
return $data; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public function testWatchClear() { |
| 132 |
$data = $this->doApiRequest( [ |
| 133 |
'action' => 'query', |
| 134 |
'wllimit' => 'max', |
| 135 |
'list' => 'watchlist' ] ); |
| 136 |
|
| 137 |
if ( isset( $data[0]['query']['watchlist'] ) ) { |
| 138 |
$wl = $data[0]['query']['watchlist']; |
| 139 |
|
| 140 |
foreach ( $wl as $page ) { |
| 141 |
$data = $this->doApiRequestWithToken( [ |
| 142 |
'action' => 'watch', |
| 143 |
'title' => $page['title'], |
| 144 |
'unwatch' => true, |
| 145 |
] ); |
| 146 |
} |
| 147 |
} |
| 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 |
|
| 155 |
|
| 156 |
|
| 157 |
if ( str_starts_with( $item['title'], ':' ) ) { |
| 158 |
unset( $data[0]['query']['watchlist'][$index] ); |
| 159 |
} |
| 160 |
} |
| 161 |
$this->assertSame( [], $data[0]['query']['watchlist'] ); |
| 162 |
|
| 163 |
return $data; |
| 164 |
} |
| 165 |
|
| 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 |
] ); |
| 175 |
|
| 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] ); |
| 180 |
} |
| 181 |
|
| 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 ); |
| 189 |
|
| 190 |
$watchlistManager = $this->getServiceContainer()->getWatchlistManager(); |
| 191 |
|
| 192 |
|
| 193 |
$this->assertFalse( $watchlistManager->isWatched( $revertingUser, $title ) ); |
| 194 |
|
| 195 |
$data = $this->doApiRequestWithToken( [ |
| 196 |
'action' => 'rollback', |
| 197 |
'title' => $titleText, |
| 198 |
'user' => $revertedUser, |
| 199 |
'watchlist' => 'watch' |
| 200 |
] ); |
| 201 |
|
| 202 |
$this->assertArrayHasKey( 'rollback', $data[0] ); |
| 203 |
$this->assertArrayHasKey( 'title', $data[0]['rollback'] ); |
| 204 |
$this->assertTrue( $watchlistManager->isWatched( $revertingUser, $title ) ); |
| 205 |
} |
| 206 |
} |