| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Api\Query; |
| 4 |
|
| 5 |
use MediaWiki\Block\DatabaseBlock; |
| 6 |
use MediaWiki\Context\RequestContext; |
| 7 |
use MediaWiki\MainConfigNames; |
| 8 |
use MediaWiki\Tests\Api\ApiTestCase; |
| 9 |
use MediaWiki\Tests\User\TempUser\TempUserTestTrait; |
| 10 |
use MediaWiki\Title\Title; |
| 11 |
use MediaWiki\User\User; |
| 12 |
use Wikimedia\Timestamp\ConvertibleTimestamp; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
class ApiQueryInfoTest extends ApiTestCase { |
| 22 |
use TempUserTestTrait; |
| 23 |
|
| 24 |
protected function setUp(): void { |
| 25 |
parent::setUp(); |
| 26 |
|
| 27 |
$this->overrideConfigValues( [ |
| 28 |
MainConfigNames::WatchlistExpiry => true, |
| 29 |
MainConfigNames::WatchlistExpiryMaxDuration => '6 months', |
| 30 |
] ); |
| 31 |
} |
| 32 |
|
| 33 |
public function testExecute() { |
| 34 |
|
| 35 |
ConvertibleTimestamp::setFakeTime( '2011-01-01T00:00:00Z' ); |
| 36 |
|
| 37 |
$page = $this->getExistingTestPage( 'Pluto' ); |
| 38 |
$title = $page->getTitle(); |
| 39 |
$user = $this->getTestUser()->getUser(); |
| 40 |
RequestContext::getMain()->setUser( $user ); |
| 41 |
$this->getServiceContainer()->getWatchlistManager()->addWatch( |
| 42 |
$user, |
| 43 |
$title, |
| 44 |
|
| 45 |
'2011-04-01T00:00:00Z' |
| 46 |
); |
| 47 |
|
| 48 |
[ $data ] = $this->doApiRequest( [ |
| 49 |
'action' => 'query', |
| 50 |
'prop' => 'info', |
| 51 |
'inprop' => 'watched|notificationtimestamp', |
| 52 |
'titles' => $title->getText() . '|' . 'NonExistingPage_lkasdoiewlmasdoiwem7483', |
| 53 |
], null, false, $user ); |
| 54 |
|
| 55 |
$this->assertArrayHasKey( 'query', $data ); |
| 56 |
$this->assertArrayHasKey( 'pages', $data['query'] ); |
| 57 |
$this->assertArrayHasKey( $page->getId(), $data['query']['pages'] ); |
| 58 |
|
| 59 |
$info = $data['query']['pages'][$page->getId()]; |
| 60 |
$this->assertSame( $page->getId(), $info['pageid'] ); |
| 61 |
$this->assertSame( NS_MAIN, $info['ns'] ); |
| 62 |
$this->assertSame( 'Pluto', $info['title'] ); |
| 63 |
$this->assertSame( 'wikitext', $info['contentmodel'] ); |
| 64 |
$this->assertSame( 'en', $info['pagelanguage'] ); |
| 65 |
$this->assertSame( 'en', $info['pagelanguagehtmlcode'] ); |
| 66 |
$this->assertSame( 'ltr', $info['pagelanguagedir'] ); |
| 67 |
$this->assertSame( '2011-01-01T00:00:00Z', $info['touched'] ); |
| 68 |
$this->assertSame( $title->getLatestRevID(), $info['lastrevid'] ); |
| 69 |
$this->assertSame( $title->getLength(), $info['length'] ); |
| 70 |
$this->assertSame( true, $info['new'] ); |
| 71 |
$this->assertSame( true, $info['watched'] ); |
| 72 |
$this->assertSame( '2011-04-01T00:00:00Z', $info['watchlistexpiry'] ); |
| 73 |
$this->assertArrayNotHasKey( 'actions', $info ); |
| 74 |
$this->assertArrayNotHasKey( 'linkclasses', $info ); |
| 75 |
} |
| 76 |
|
| 77 |
public function testExecuteLinkClasses() { |
| 78 |
$page = $this->getExistingTestPage( 'Pluto' ); |
| 79 |
$title = $page->getTitle(); |
| 80 |
|
| 81 |
[ $data ] = $this->doApiRequest( [ |
| 82 |
'action' => 'query', |
| 83 |
'prop' => 'info', |
| 84 |
'titles' => $title->getText(), |
| 85 |
'inprop' => 'linkclasses', |
| 86 |
'inlinkcontext' => $title->getText(), |
| 87 |
] ); |
| 88 |
|
| 89 |
$this->assertArrayHasKey( 'query', $data ); |
| 90 |
$this->assertArrayHasKey( 'pages', $data['query'] ); |
| 91 |
$this->assertArrayHasKey( $page->getId(), $data['query']['pages'] ); |
| 92 |
|
| 93 |
$info = $data['query']['pages'][$page->getId()]; |
| 94 |
$this->assertArrayHasKey( 'linkclasses', $info ); |
| 95 |
$this->assertEquals( [], $info['linkclasses'] ); |
| 96 |
} |
| 97 |
|
| 98 |
public function testExecuteEditActions() { |
| 99 |
$page = $this->getExistingTestPage( 'Pluto' ); |
| 100 |
$title = $page->getTitle(); |
| 101 |
|
| 102 |
[ $data ] = $this->doApiRequest( [ |
| 103 |
'action' => 'query', |
| 104 |
'prop' => 'info', |
| 105 |
'titles' => $title->getText(), |
| 106 |
'intestactions' => 'edit' |
| 107 |
] ); |
| 108 |
|
| 109 |
$this->assertArrayHasKey( 'query', $data ); |
| 110 |
$this->assertArrayHasKey( 'pages', $data['query'] ); |
| 111 |
$this->assertArrayHasKey( $page->getId(), $data['query']['pages'] ); |
| 112 |
|
| 113 |
$info = $data['query']['pages'][$page->getId()]; |
| 114 |
$this->assertArrayHasKey( 'actions', $info ); |
| 115 |
$this->assertArrayHasKey( 'edit', $info['actions'] ); |
| 116 |
$this->assertTrue( $info['actions']['edit'] ); |
| 117 |
} |
| 118 |
|
| 119 |
public function testExecuteEditActionsAutoCreate() { |
| 120 |
$page = $this->getExistingTestPage( 'Pluto' ); |
| 121 |
$title = $page->getTitle(); |
| 122 |
|
| 123 |
|
| 124 |
$this->disableAutoCreateTempUser(); |
| 125 |
[ $data ] = $this->doApiRequest( [ |
| 126 |
'action' => 'query', |
| 127 |
'prop' => 'info', |
| 128 |
'titles' => $title->getText(), |
| 129 |
'intestactions' => 'edit', |
| 130 |
'intestactionsautocreate' => true, |
| 131 |
], null, false, new User() ); |
| 132 |
$result = $data['query']['pages'][$page->getId()]['wouldautocreate']['edit']; |
| 133 |
$this->assertFalse( $result ); |
| 134 |
|
| 135 |
|
| 136 |
$this->setGroupPermissions( '*', 'createaccount', true ); |
| 137 |
$this->enableAutoCreateTempUser(); |
| 138 |
[ $data ] = $this->doApiRequest( [ |
| 139 |
'action' => 'query', |
| 140 |
'prop' => 'info', |
| 141 |
'titles' => $title->getText(), |
| 142 |
'intestactions' => 'edit', |
| 143 |
'intestactionsautocreate' => true, |
| 144 |
], null, false, new User() ); |
| 145 |
$result = $data['query']['pages'][$page->getId()]['wouldautocreate']['edit']; |
| 146 |
$this->assertTrue( $result ); |
| 147 |
|
| 148 |
[ $data ] = $this->doApiRequest( [ |
| 149 |
'action' => 'query', |
| 150 |
'prop' => 'info', |
| 151 |
'titles' => $title->getText(), |
| 152 |
'intestactions' => 'create', |
| 153 |
'intestactionsautocreate' => true, |
| 154 |
], null, false, new User() ); |
| 155 |
$result = $data['query']['pages'][$page->getId()]['wouldautocreate']['create']; |
| 156 |
$this->assertTrue( $result ); |
| 157 |
|
| 158 |
|
| 159 |
[ $data ] = $this->doApiRequest( [ |
| 160 |
'action' => 'query', |
| 161 |
'prop' => 'info', |
| 162 |
'titles' => $title->getText(), |
| 163 |
'intestactions' => 'read', |
| 164 |
'intestactionsautocreate' => true, |
| 165 |
], null, false, new User() ); |
| 166 |
$result = $data['query']['pages'][$page->getId()]['wouldautocreate']['read']; |
| 167 |
$this->assertFalse( $result ); |
| 168 |
|
| 169 |
|
| 170 |
[ $data ] = $this->doApiRequest( [ |
| 171 |
'action' => 'query', |
| 172 |
'prop' => 'info', |
| 173 |
'titles' => $title->getText(), |
| 174 |
'intestactions' => 'edit', |
| 175 |
'intestactionsautocreate' => true, |
| 176 |
], null, false, static::getTestSysop()->getAuthority() ); |
| 177 |
$result = $data['query']['pages'][$page->getId()]['wouldautocreate']['edit']; |
| 178 |
$this->assertFalse( $result ); |
| 179 |
|
| 180 |
|
| 181 |
$this->setGroupPermissions( '*', 'createaccount', false ); |
| 182 |
[ $data ] = $this->doApiRequest( [ |
| 183 |
'action' => 'query', |
| 184 |
'prop' => 'info', |
| 185 |
'titles' => $title->getText(), |
| 186 |
'intestactions' => 'edit', |
| 187 |
'intestactionsautocreate' => true, |
| 188 |
], null, false, new User() ); |
| 189 |
$result = $data['query']['pages'][$page->getId()]['wouldautocreate']['edit']; |
| 190 |
$this->assertFalse( $result ); |
| 191 |
} |
| 192 |
|
| 193 |
public function testExecuteEditActionsFull() { |
| 194 |
$page = $this->getExistingTestPage( 'Pluto' ); |
| 195 |
$title = $page->getTitle(); |
| 196 |
|
| 197 |
[ $data ] = $this->doApiRequest( [ |
| 198 |
'action' => 'query', |
| 199 |
'prop' => 'info', |
| 200 |
'titles' => $title->getText(), |
| 201 |
'intestactions' => 'edit', |
| 202 |
'intestactionsdetail' => 'full', |
| 203 |
] ); |
| 204 |
|
| 205 |
$this->assertArrayHasKey( 'query', $data ); |
| 206 |
$this->assertArrayHasKey( 'pages', $data['query'] ); |
| 207 |
$this->assertArrayHasKey( $page->getId(), $data['query']['pages'] ); |
| 208 |
|
| 209 |
$info = $data['query']['pages'][$page->getId()]; |
| 210 |
$this->assertArrayHasKey( 'actions', $info ); |
| 211 |
$this->assertArrayHasKey( 'edit', $info['actions'] ); |
| 212 |
$this->assertIsArray( $info['actions']['edit'] ); |
| 213 |
$this->assertSame( [], $info['actions']['edit'] ); |
| 214 |
} |
| 215 |
|
| 216 |
public function testExecuteEditActionsFullBlock() { |
| 217 |
$badActor = $this->getTestUser()->getUser(); |
| 218 |
$sysop = $this->getTestSysop()->getUser(); |
| 219 |
|
| 220 |
$block = new DatabaseBlock( [ |
| 221 |
'address' => $badActor, |
| 222 |
'by' => $sysop, |
| 223 |
'expiry' => 'infinity', |
| 224 |
'sitewide' => 1, |
| 225 |
'enableAutoblock' => true, |
| 226 |
] ); |
| 227 |
|
| 228 |
$blockStore = $this->getServiceContainer()->getDatabaseBlockStore(); |
| 229 |
$blockStore->insertBlock( $block ); |
| 230 |
|
| 231 |
$page = $this->getExistingTestPage( 'Pluto' ); |
| 232 |
$title = $page->getTitle(); |
| 233 |
|
| 234 |
[ $data ] = $this->doApiRequest( [ |
| 235 |
'action' => 'query', |
| 236 |
'prop' => 'info', |
| 237 |
'titles' => $title->getText(), |
| 238 |
'intestactions' => 'edit', |
| 239 |
'intestactionsdetail' => 'full', |
| 240 |
], null, false, $badActor ); |
| 241 |
|
| 242 |
$this->assertArrayHasKey( 'query', $data ); |
| 243 |
$this->assertArrayHasKey( 'pages', $data['query'] ); |
| 244 |
$this->assertArrayHasKey( $page->getId(), $data['query']['pages'] ); |
| 245 |
|
| 246 |
$info = $data['query']['pages'][$page->getId()]; |
| 247 |
$this->assertArrayHasKey( 'actions', $info ); |
| 248 |
$this->assertArrayHasKey( 'edit', $info['actions'] ); |
| 249 |
$this->assertIsArray( $info['actions']['edit'] ); |
| 250 |
$this->assertArrayHasKey( 0, $info['actions']['edit'] ); |
| 251 |
$this->assertArrayHasKey( 'code', $info['actions']['edit'][0] ); |
| 252 |
$this->assertSame( 'blocked', $info['actions']['edit'][0]['code'] ); |
| 253 |
$this->assertArrayHasKey( 'data', $info['actions']['edit'][0] ); |
| 254 |
$this->assertArrayHasKey( 'blockinfo', $info['actions']['edit'][0]['data'] ); |
| 255 |
$this->assertArrayHasKey( 'blockid', $info['actions']['edit'][0]['data']['blockinfo'] ); |
| 256 |
$this->assertSame( $block->getId(), $info['actions']['edit'][0]['data']['blockinfo']['blockid'] ); |
| 257 |
} |
| 258 |
|
| 259 |
public function testAssociatedPage() { |
| 260 |
$page = $this->getExistingTestPage( 'Demo' ); |
| 261 |
$title = $page->getTitle(); |
| 262 |
|
| 263 |
$title2 = Title::makeTitle( NS_TALK, 'Page does not exist' ); |
| 264 |
|
| 265 |
$this->getNonexistingTestPage( $title2 ); |
| 266 |
|
| 267 |
[ $data ] = $this->doApiRequest( [ |
| 268 |
'action' => 'query', |
| 269 |
'prop' => 'info', |
| 270 |
'titles' => $title->getPrefixedText() . '|' . $title2->getPrefixedText(), |
| 271 |
'inprop' => 'associatedpage', |
| 272 |
] ); |
| 273 |
|
| 274 |
$this->assertArrayHasKey( 'query', $data ); |
| 275 |
$this->assertArrayHasKey( 'pages', $data['query'] ); |
| 276 |
$this->assertArrayHasKey( $page->getId(), $data['query']['pages'] ); |
| 277 |
|
| 278 |
$info = $data['query']['pages'][$page->getId()]; |
| 279 |
$this->assertArrayHasKey( 'associatedpage', $info ); |
| 280 |
$this->assertEquals( |
| 281 |
'Talk:Demo', |
| 282 |
$info['associatedpage'] |
| 283 |
); |
| 284 |
|
| 285 |
|
| 286 |
$this->assertArrayHasKey( -1, $data['query']['pages'] ); |
| 287 |
|
| 288 |
$info = $data['query']['pages'][ -1 ]; |
| 289 |
$this->assertArrayHasKey( 'associatedpage', $info ); |
| 290 |
$this->assertEquals( |
| 291 |
'Page does not exist', |
| 292 |
$info['associatedpage'] |
| 293 |
); |
| 294 |
} |
| 295 |
|
| 296 |
public function testDisplayTitle() { |
| 297 |
[ $data ] = $this->doApiRequest( [ |
| 298 |
'action' => 'query', |
| 299 |
'prop' => 'info', |
| 300 |
'inprop' => 'displaytitle', |
| 301 |
'titles' => 'Art©', |
| 302 |
] ); |
| 303 |
|
| 304 |
$this->assertArrayHasKey( 'query', $data ); |
| 305 |
$this->assertArrayHasKey( 'pages', $data['query'] ); |
| 306 |
|
| 307 |
|
| 308 |
$this->assertArrayHasKey( -1, $data['query']['pages'] ); |
| 309 |
|
| 310 |
$info = $data['query']['pages'][ -1 ]; |
| 311 |
$this->assertArrayHasKey( 'displaytitle', $info ); |
| 312 |
$this->assertEquals( |
| 313 |
'Art&copy', |
| 314 |
$info['displaytitle'] |
| 315 |
); |
| 316 |
} |
| 317 |
|
| 318 |
} |