| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Rest\Handler\Helper; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use MediaWiki\CommentStore\CommentStoreComment; |
| 7 |
use MediaWiki\Config\ServiceOptions; |
| 8 |
use MediaWiki\MainConfigNames; |
| 9 |
use MediaWiki\Page\ExistingPageRecord; |
| 10 |
use MediaWiki\Page\PageIdentityValue; |
| 11 |
use MediaWiki\Permissions\Authority; |
| 12 |
use MediaWiki\Rest\Handler\Helper\PageContentHelper; |
| 13 |
use MediaWiki\Rest\HttpException; |
| 14 |
use MediaWiki\Rest\LocalizedHttpException; |
| 15 |
use MediaWiki\Rest\Response; |
| 16 |
use MediaWiki\Revision\MutableRevisionRecord; |
| 17 |
use MediaWiki\Revision\RevisionRecord; |
| 18 |
use MediaWiki\Revision\SlotRecord; |
| 19 |
use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait; |
| 20 |
use MediaWiki\Title\Title; |
| 21 |
use MediaWiki\User\UserIdentityValue; |
| 22 |
use MediaWikiIntegrationTestCase; |
| 23 |
use Wikimedia\TestingAccessWrapper; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
class PageContentHelperTest extends MediaWikiIntegrationTestCase { |
| 30 |
use MockAuthorityTrait; |
| 31 |
|
| 32 |
private const NO_REVISION_ETAG = '"7afa43d0f642f1fda1b8e30f4f67243049f5fe77"'; |
| 33 |
|
| 34 |
protected function setUp(): void { |
| 35 |
parent::setUp(); |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
private function newHelper( |
| 45 |
array $params = [], |
| 46 |
?Authority $authority = null |
| 47 |
): PageContentHelper { |
| 48 |
$helper = new PageContentHelper( |
| 49 |
new ServiceOptions( |
| 50 |
PageContentHelper::CONSTRUCTOR_OPTIONS, |
| 51 |
[ |
| 52 |
MainConfigNames::RightsUrl => 'https://example.com/rights', |
| 53 |
MainConfigNames::RightsText => 'some rights', |
| 54 |
] |
| 55 |
), |
| 56 |
$this->getServiceContainer()->getRevisionLookup(), |
| 57 |
$this->getServiceContainer()->getTitleFormatter(), |
| 58 |
$this->getServiceContainer()->getPageStore(), |
| 59 |
$this->getServiceContainer()->getTitleFactory(), |
| 60 |
$this->getServiceContainer()->getConnectionProvider(), |
| 61 |
$this->getServiceContainer()->getChangeTagsStore() |
| 62 |
); |
| 63 |
|
| 64 |
$authority = $authority ?: $this->mockRegisteredUltimateAuthority(); |
| 65 |
$helper->init( $authority, $params ); |
| 66 |
return $helper; |
| 67 |
} |
| 68 |
|
| 69 |
public function testGetRole() { |
| 70 |
$helper = $this->newHelper(); |
| 71 |
$this->assertSame( SlotRecord::MAIN, $helper->getRole() ); |
| 72 |
} |
| 73 |
|
| 74 |
public function testGetTitle() { |
| 75 |
$this->getExistingTestPage( 'Foo' ); |
| 76 |
|
| 77 |
$helper = $this->newHelper( [ 'title' => 'Foo' ] ); |
| 78 |
$this->assertSame( 'Foo', $helper->getTitleText() ); |
| 79 |
|
| 80 |
$this->assertInstanceOf( ExistingPageRecord::class, $helper->getPage() ); |
| 81 |
$this->assertSame( |
| 82 |
'Foo', |
| 83 |
$this->getServiceContainer()->getTitleFormatter()->getPrefixedDBkey( $helper->getPage() ) |
| 84 |
); |
| 85 |
} |
| 86 |
|
| 87 |
public function testGetTargetRevisionAndContent() { |
| 88 |
$page = $this->getExistingTestPage( __METHOD__ ); |
| 89 |
$rev = $page->getRevisionRecord(); |
| 90 |
|
| 91 |
$helper = $this->newHelper( [ 'title' => $page->getTitle()->getPrefixedDBkey() ] ); |
| 92 |
|
| 93 |
$targetRev = $helper->getTargetRevision(); |
| 94 |
$this->assertInstanceOf( RevisionRecord::class, $targetRev ); |
| 95 |
$this->assertSame( $rev->getId(), $targetRev->getId() ); |
| 96 |
|
| 97 |
$pageContent = $helper->getContent(); |
| 98 |
$this->assertSame( |
| 99 |
$rev->getContent( SlotRecord::MAIN )->serialize(), |
| 100 |
$pageContent->serialize() |
| 101 |
); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public function testT353687() { |
| 108 |
$page = $this->getExistingTestPage( '0' ); |
| 109 |
$rev = $page->getRevisionRecord(); |
| 110 |
|
| 111 |
$helper = $this->newHelper( [ 'title' => $page->getTitle()->getPrefixedDBkey() ] ); |
| 112 |
|
| 113 |
|
| 114 |
$helper->checkAccess(); |
| 115 |
|
| 116 |
$targetRev = $helper->getTargetRevision(); |
| 117 |
$this->assertInstanceOf( RevisionRecord::class, $targetRev ); |
| 118 |
$this->assertSame( $rev->getId(), $targetRev->getId() ); |
| 119 |
} |
| 120 |
|
| 121 |
public function testNoTitle() { |
| 122 |
$helper = $this->newHelper(); |
| 123 |
|
| 124 |
$this->assertNull( $helper->getTitleText() ); |
| 125 |
$this->assertNull( $helper->getPage() ); |
| 126 |
|
| 127 |
$this->assertFalse( $helper->hasContent() ); |
| 128 |
|
| 129 |
$this->assertNull( $helper->getTargetRevision() ); |
| 130 |
|
| 131 |
try { |
| 132 |
$helper->getContent(); |
| 133 |
$this->fail( 'Expected HttpException' ); |
| 134 |
} catch ( HttpException $ex ) { |
| 135 |
$this->assertSame( 404, $ex->getCode() ); |
| 136 |
} |
| 137 |
|
| 138 |
try { |
| 139 |
$helper->checkAccess(); |
| 140 |
$this->fail( 'Expected HttpException' ); |
| 141 |
} catch ( HttpException $ex ) { |
| 142 |
$this->assertSame( 404, $ex->getCode() ); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
public static function provideBadTitle() { |
| 147 |
yield [ '_' ]; |
| 148 |
yield [ '::Hello' ]; |
| 149 |
yield [ 'Special:Blankpage' ]; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
public function testBadTitle( $badTitle ) { |
| 156 |
$helper = $this->newHelper( [ 'title' => $badTitle ] ); |
| 157 |
|
| 158 |
$this->assertNotNull( $helper->getTitleText() ); |
| 159 |
$this->assertNull( $helper->getPage() ); |
| 160 |
|
| 161 |
$this->assertFalse( $helper->hasContent() ); |
| 162 |
$this->assertNull( $helper->getTargetRevision() ); |
| 163 |
|
| 164 |
try { |
| 165 |
$helper->getContent(); |
| 166 |
$this->fail( 'Expected HttpException' ); |
| 167 |
} catch ( HttpException $ex ) { |
| 168 |
$this->assertSame( 404, $ex->getCode() ); |
| 169 |
} |
| 170 |
|
| 171 |
try { |
| 172 |
$helper->checkAccess(); |
| 173 |
$this->fail( 'Expected HttpException' ); |
| 174 |
} catch ( HttpException $ex ) { |
| 175 |
$this->assertSame( 404, $ex->getCode() ); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
public function testCheckHasContent() { |
| 180 |
$page = $this->getNonexistingTestPage( __METHOD__ )->getDBkey(); |
| 181 |
$helper = $this->newHelper( [ 'title' => $page ] ); |
| 182 |
|
| 183 |
$this->expectException( LocalizedHttpException::class ); |
| 184 |
$this->expectExceptionCode( 404 ); |
| 185 |
|
| 186 |
$helper->checkHasContent(); |
| 187 |
} |
| 188 |
|
| 189 |
public function testCheckAccessPermission() { |
| 190 |
$helper = $this->newHelper(); |
| 191 |
|
| 192 |
$this->expectException( LocalizedHttpException::class ); |
| 193 |
$this->expectExceptionCode( 403 ); |
| 194 |
|
| 195 |
$helper->checkAccessPermission(); |
| 196 |
} |
| 197 |
|
| 198 |
public function testNonExistingPage() { |
| 199 |
$page = $this->getNonexistingTestPage( __METHOD__ ); |
| 200 |
$title = $page->getTitle(); |
| 201 |
$helper = $this->newHelper( [ 'title' => $title->getPrefixedDBkey() ] ); |
| 202 |
|
| 203 |
$this->assertSame( $title->getPrefixedDBkey(), $helper->getTitleText() ); |
| 204 |
$this->assertNull( $helper->getPage() ); |
| 205 |
|
| 206 |
$this->assertFalse( $helper->hasContent() ); |
| 207 |
$this->assertTrue( $helper->isAccessible() ); |
| 208 |
|
| 209 |
$this->assertNull( $helper->getTargetRevision() ); |
| 210 |
|
| 211 |
$this->assertNull( $helper->getLastModified() ); |
| 212 |
$this->assertSame( self::NO_REVISION_ETAG, $helper->getETag() ); |
| 213 |
|
| 214 |
try { |
| 215 |
$helper->getContent(); |
| 216 |
$this->fail( 'Expected HttpException' ); |
| 217 |
} catch ( HttpException $ex ) { |
| 218 |
$this->assertSame( 404, $ex->getCode() ); |
| 219 |
} |
| 220 |
|
| 221 |
try { |
| 222 |
$helper->checkAccess(); |
| 223 |
$this->fail( 'Expected HttpException' ); |
| 224 |
} catch ( HttpException $ex ) { |
| 225 |
$this->assertSame( 404, $ex->getCode() ); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
public function testForbidenPage() { |
| 230 |
$page = $this->getExistingTestPage( __METHOD__ ); |
| 231 |
$title = $page->getTitle(); |
| 232 |
$helper = $this->newHelper( |
| 233 |
[ 'title' => $title->getPrefixedDBkey() ], |
| 234 |
$this->mockAnonNullAuthority() |
| 235 |
); |
| 236 |
|
| 237 |
$this->assertSame( $title->getPrefixedDBkey(), $helper->getTitleText() ); |
| 238 |
$this->assertTrue( $helper->getPage()->isSamePageAs( $title ) ); |
| 239 |
|
| 240 |
$this->assertTrue( $helper->hasContent() ); |
| 241 |
$this->assertFalse( $helper->useDefaultSystemMessage() ); |
| 242 |
$this->assertNull( $helper->getDefaultSystemMessage() ); |
| 243 |
$this->assertFalse( $helper->isAccessible() ); |
| 244 |
|
| 245 |
$this->assertNull( $helper->getLastModified() ); |
| 246 |
|
| 247 |
try { |
| 248 |
$helper->checkAccess(); |
| 249 |
$this->fail( 'Expected HttpException' ); |
| 250 |
} catch ( HttpException $ex ) { |
| 251 |
$this->assertSame( 403, $ex->getCode() ); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
public function testForbiddenMessagePage() { |
| 256 |
$page = $this->getNonexistingTestPage( |
| 257 |
Title::makeTitle( NS_MEDIAWIKI, 'Logouttext' ) |
| 258 |
); |
| 259 |
$title = $page->getTitle(); |
| 260 |
$helper = $this->newHelper( |
| 261 |
[ 'title' => $title->getPrefixedDBkey() ], |
| 262 |
$this->mockAnonNullAuthority() |
| 263 |
); |
| 264 |
|
| 265 |
$this->assertSame( $title->getPrefixedDBkey(), $helper->getTitleText() ); |
| 266 |
$this->assertNull( $helper->getPage() ); |
| 267 |
|
| 268 |
$this->assertTrue( $helper->hasContent() ); |
| 269 |
$this->assertTrue( $helper->useDefaultSystemMessage() ); |
| 270 |
$this->assertNotNull( $helper->getDefaultSystemMessage() ); |
| 271 |
$this->assertFalse( $helper->isAccessible() ); |
| 272 |
|
| 273 |
$this->assertNull( $helper->getLastModified() ); |
| 274 |
|
| 275 |
$this->expectException( HttpException::class ); |
| 276 |
$this->expectExceptionCode( 403 ); |
| 277 |
$helper->checkAccess(); |
| 278 |
} |
| 279 |
|
| 280 |
public function testMessagePage() { |
| 281 |
$page = $this->getNonexistingTestPage( |
| 282 |
Title::makeTitle( NS_MEDIAWIKI, 'Logouttext' ) |
| 283 |
); |
| 284 |
$title = $page->getTitle(); |
| 285 |
$helper = $this->newHelper( |
| 286 |
[ 'title' => $title->getPrefixedDBkey() ], |
| 287 |
$this->mockAnonUltimateAuthority() |
| 288 |
); |
| 289 |
|
| 290 |
$this->assertSame( $title->getPrefixedDBkey(), $helper->getTitleText() ); |
| 291 |
$this->assertNull( $helper->getPage() ); |
| 292 |
|
| 293 |
$this->assertTrue( $helper->hasContent() ); |
| 294 |
$this->assertTrue( $helper->useDefaultSystemMessage() ); |
| 295 |
$this->assertNotNull( $helper->getDefaultSystemMessage() ); |
| 296 |
$this->assertTrue( $helper->isAccessible() ); |
| 297 |
|
| 298 |
$this->assertNull( $helper->getLastModified() ); |
| 299 |
|
| 300 |
|
| 301 |
$helper->checkAccess(); |
| 302 |
} |
| 303 |
|
| 304 |
public function testParameterSettings() { |
| 305 |
$helper = $this->newHelper(); |
| 306 |
$settings = $helper->getParamSettings(); |
| 307 |
$this->assertArrayHasKey( 'title', $settings ); |
| 308 |
} |
| 309 |
|
| 310 |
public function testCacheControl() { |
| 311 |
$helper = $this->newHelper(); |
| 312 |
|
| 313 |
$response = new Response(); |
| 314 |
|
| 315 |
$helper->setCacheControl( $response ); |
| 316 |
$this->assertStringContainsString( 'max-age=5', $response->getHeaderLine( 'Cache-Control' ) ); |
| 317 |
|
| 318 |
$helper->setCacheControl( $response, 2 ); |
| 319 |
$this->assertStringContainsString( 'max-age=2', $response->getHeaderLine( 'Cache-Control' ) ); |
| 320 |
|
| 321 |
$helper->setCacheControl( $response, 1000 * 1000 ); |
| 322 |
$this->assertStringContainsString( 'max-age=5', $response->getHeaderLine( 'Cache-Control' ) ); |
| 323 |
} |
| 324 |
|
| 325 |
public function testConstructMetadata() { |
| 326 |
$page = $this->getExistingTestPage( __METHOD__ ); |
| 327 |
$title = $page->getTitle(); |
| 328 |
|
| 329 |
$revision = $page->getRevisionRecord(); |
| 330 |
$content = $revision->getContent( SlotRecord::MAIN ); |
| 331 |
$expected = [ |
| 332 |
'id' => $title->getArticleID(), |
| 333 |
'key' => $title->getPrefixedDBkey(), |
| 334 |
'title' => $title->getPrefixedText(), |
| 335 |
'latest' => [ |
| 336 |
'id' => $revision->getId(), |
| 337 |
'timestamp' => wfTimestampOrNull( TS_ISO_8601, $revision->getTimestamp() ) |
| 338 |
], |
| 339 |
'content_model' => $content->getModel(), |
| 340 |
'license' => [ |
| 341 |
'url' => 'https://example.com/rights', |
| 342 |
'title' => 'some rights', |
| 343 |
] |
| 344 |
]; |
| 345 |
|
| 346 |
$helper = $this->newHelper( [ 'title' => $title->getPrefixedDBkey() ] ); |
| 347 |
$data = $helper->constructMetadata(); |
| 348 |
|
| 349 |
$this->assertEquals( $expected, $data ); |
| 350 |
} |
| 351 |
|
| 352 |
public function provideConstructRestbaseCompatibleMetadata() { |
| 353 |
$pageName = 'User:Morg'; |
| 354 |
$page = PageIdentityValue::localIdentity( 7, NS_USER, 'Morg' ); |
| 355 |
$user = UserIdentityValue::newRegistered( 444, 'Morg' ); |
| 356 |
$comment = CommentStoreComment::newUnsavedComment( 'just an edit' ); |
| 357 |
$timestamp = '20220102112233'; |
| 358 |
|
| 359 |
$rev = new MutableRevisionRecord( $page ); |
| 360 |
$rev->setId( 123 ); |
| 361 |
$rev->setUser( $user ); |
| 362 |
$rev->setComment( $comment ); |
| 363 |
$rev->setTimestamp( $timestamp ); |
| 364 |
|
| 365 |
$expected = [ |
| 366 |
'title' => $pageName, |
| 367 |
'page_id' => 7, |
| 368 |
'rev' => 123, |
| 369 |
'tid' => 'DUMMY', |
| 370 |
'namespace' => NS_USER, |
| 371 |
'user_id' => 444, |
| 372 |
'user_text' => 'Morg', |
| 373 |
'comment' => $comment->text, |
| 374 |
'timestamp' => wfTimestampOrNull( TS_ISO_8601, $timestamp ), |
| 375 |
'tags' => [], |
| 376 |
'restrictions' => [], |
| 377 |
'page_language' => 'en', |
| 378 |
'redirect' => false |
| 379 |
]; |
| 380 |
|
| 381 |
yield [ |
| 382 |
$pageName, |
| 383 |
$rev, |
| 384 |
$expected |
| 385 |
]; |
| 386 |
|
| 387 |
|
| 388 |
$rev = new MutableRevisionRecord( $page ); |
| 389 |
$rev->setId( 123 ); |
| 390 |
$rev->setUser( $user ); |
| 391 |
$rev->setComment( $comment ); |
| 392 |
$rev->setTimestamp( $timestamp ); |
| 393 |
$rev->setVisibility( RevisionRecord::DELETED_COMMENT ); |
| 394 |
|
| 395 |
$expectedHiddenComment = [ |
| 396 |
'comment' => null, |
| 397 |
'restrictions' => [ 'commenthidden' ], |
| 398 |
] + $expected; |
| 399 |
|
| 400 |
yield [ |
| 401 |
$pageName, |
| 402 |
$rev, |
| 403 |
$expectedHiddenComment |
| 404 |
]; |
| 405 |
|
| 406 |
|
| 407 |
$rev = new MutableRevisionRecord( $page ); |
| 408 |
$rev->setId( 123 ); |
| 409 |
$rev->setUser( $user ); |
| 410 |
$rev->setComment( $comment ); |
| 411 |
$rev->setTimestamp( $timestamp ); |
| 412 |
$rev->setVisibility( RevisionRecord::DELETED_USER ); |
| 413 |
|
| 414 |
$expectedHiddenComment = [ |
| 415 |
'user_text' => null, |
| 416 |
'restrictions' => [ 'userhidden' ], |
| 417 |
] + $expected; |
| 418 |
|
| 419 |
yield [ |
| 420 |
$pageName, |
| 421 |
$rev, |
| 422 |
$expectedHiddenComment |
| 423 |
]; |
| 424 |
} |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
public function testConstructRestbaseCompatibleMetadata( |
| 430 |
string $pageName, |
| 431 |
RevisionRecord $revision, |
| 432 |
array $expected |
| 433 |
) { |
| 434 |
$helper = $this->newHelper( [ 'title' => $pageName ] ); |
| 435 |
|
| 436 |
$helperAccess = TestingAccessWrapper::newFromObject( $helper ); |
| 437 |
$helperAccess->pageIdentity = $revision->getPage(); |
| 438 |
$helperAccess->targetRevision = $revision; |
| 439 |
|
| 440 |
$data = $helper->constructRestbaseCompatibleMetadata(); |
| 441 |
|
| 442 |
$this->assertEquals( |
| 443 |
$expected, |
| 444 |
$data |
| 445 |
); |
| 446 |
} |
| 447 |
|
| 448 |
} |