| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Rest\Handler\Helper; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use MediaWiki\Config\ServiceOptions; |
| 7 |
use MediaWiki\MainConfigNames; |
| 8 |
use MediaWiki\Page\ExistingPageRecord; |
| 9 |
use MediaWiki\Permissions\Authority; |
| 10 |
use MediaWiki\Rest\Handler\Helper\PageContentHelper; |
| 11 |
use MediaWiki\Rest\Handler\Helper\RevisionContentHelper; |
| 12 |
use MediaWiki\Rest\HttpException; |
| 13 |
use MediaWiki\Rest\Response; |
| 14 |
use MediaWiki\Revision\RevisionRecord; |
| 15 |
use MediaWiki\Revision\SlotRecord; |
| 16 |
use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait; |
| 17 |
use MediaWikiIntegrationTestCase; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
class RevisionContentHelperTest extends MediaWikiIntegrationTestCase { |
| 24 |
use MockAuthorityTrait; |
| 25 |
|
| 26 |
private const NO_REVISION_ETAG = '"b620cd7841f9ea8f545f11cc44ce794f848fa2d3"'; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
private function newHelper( |
| 35 |
array $params = [], |
| 36 |
?Authority $authority = null |
| 37 |
): RevisionContentHelper { |
| 38 |
$helper = new RevisionContentHelper( |
| 39 |
new ServiceOptions( |
| 40 |
PageContentHelper::CONSTRUCTOR_OPTIONS, |
| 41 |
[ |
| 42 |
MainConfigNames::RightsUrl => 'https://example.com/rights', |
| 43 |
MainConfigNames::RightsText => 'some rights', |
| 44 |
] |
| 45 |
), |
| 46 |
$this->getServiceContainer()->getRevisionLookup(), |
| 47 |
$this->getServiceContainer()->getTitleFormatter(), |
| 48 |
$this->getServiceContainer()->getPageStore(), |
| 49 |
$this->getServiceContainer()->getTitleFactory(), |
| 50 |
$this->getServiceContainer()->getConnectionProvider(), |
| 51 |
$this->getServiceContainer()->getChangeTagsStore() |
| 52 |
); |
| 53 |
|
| 54 |
$authority = $authority ?: $this->mockRegisteredUltimateAuthority(); |
| 55 |
$helper->init( $authority, $params ); |
| 56 |
return $helper; |
| 57 |
} |
| 58 |
|
| 59 |
private function getExistingPageWithRevisions( $name ) { |
| 60 |
$page = $this->getNonexistingTestPage( $name ); |
| 61 |
|
| 62 |
$this->editPage( $page, 'First revision of ' . $name ); |
| 63 |
$revisions['first'] = $page->getRevisionRecord(); |
| 64 |
|
| 65 |
$this->editPage( $page, 'DEAD BEEF' ); |
| 66 |
$revisions['latest'] = $page->getRevisionRecord(); |
| 67 |
|
| 68 |
return [ $page, $revisions ]; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public function testGetRole() { |
| 75 |
$helper = $this->newHelper(); |
| 76 |
$this->assertSame( SlotRecord::MAIN, $helper->getRole() ); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public function testGetPage() { |
| 84 |
[ $page, $revisions ] = $this->getExistingPageWithRevisions( __METHOD__ ); |
| 85 |
|
| 86 |
$helper = $this->newHelper( [ 'id' => $revisions['first']->getId() ] ); |
| 87 |
$this->assertSame( $page->getTitle()->getPrefixedDBKey(), $helper->getTitleText() ); |
| 88 |
|
| 89 |
$this->assertInstanceOf( ExistingPageRecord::class, $helper->getPage() ); |
| 90 |
$this->assertTrue( $helper->getPage()->isSamePageAs( $page->getTitle() ) ); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public function testGetTargetRevisionAndContent() { |
| 98 |
[ $page, $revisions ] = $this->getExistingPageWithRevisions( __METHOD__ ); |
| 99 |
|
| 100 |
$helper = $this->newHelper( [ 'id' => $revisions['first']->getId() ] ); |
| 101 |
|
| 102 |
$targetRev = $helper->getTargetRevision(); |
| 103 |
$this->assertInstanceOf( RevisionRecord::class, $targetRev ); |
| 104 |
$this->assertSame( $revisions['first']->getId(), $targetRev->getId() ); |
| 105 |
|
| 106 |
$pageContent = $helper->getContent(); |
| 107 |
$this->assertSame( |
| 108 |
$revisions['first']->getContent( SlotRecord::MAIN )->serialize(), |
| 109 |
$pageContent->serialize() |
| 110 |
); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
public function testNoTitle() { |
| 125 |
$helper = $this->newHelper(); |
| 126 |
|
| 127 |
$this->assertNull( $helper->getTitleText() ); |
| 128 |
$this->assertNull( $helper->getPage() ); |
| 129 |
|
| 130 |
$this->assertFalse( $helper->hasContent() ); |
| 131 |
$this->assertFalse( $helper->isAccessible() ); |
| 132 |
|
| 133 |
$this->assertNull( $helper->getTargetRevision() ); |
| 134 |
|
| 135 |
$this->assertNull( $helper->getLastModified() ); |
| 136 |
$this->assertSame( self::NO_REVISION_ETAG, $helper->getETag() ); |
| 137 |
|
| 138 |
try { |
| 139 |
$helper->getContent(); |
| 140 |
$this->fail( 'Expected HttpException' ); |
| 141 |
} catch ( HttpException $ex ) { |
| 142 |
$this->assertSame( 404, $ex->getCode() ); |
| 143 |
} |
| 144 |
|
| 145 |
try { |
| 146 |
$helper->checkAccess(); |
| 147 |
$this->fail( 'Expected HttpException' ); |
| 148 |
} catch ( HttpException $ex ) { |
| 149 |
$this->assertSame( 404, $ex->getCode() ); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
public function testNonExistingRevision() { |
| 165 |
$helper = $this->newHelper( [ 'id' => 287436534 ] ); |
| 166 |
|
| 167 |
$this->assertSame( 287436534, $helper->getRevisionId() ); |
| 168 |
$this->assertNull( $helper->getTitleText() ); |
| 169 |
$this->assertNull( $helper->getPage() ); |
| 170 |
|
| 171 |
$this->assertFalse( $helper->hasContent() ); |
| 172 |
$this->assertFalse( $helper->isAccessible() ); |
| 173 |
|
| 174 |
$this->assertNull( $helper->getTargetRevision() ); |
| 175 |
|
| 176 |
$this->assertNull( $helper->getLastModified() ); |
| 177 |
$this->assertSame( self::NO_REVISION_ETAG, $helper->getETag() ); |
| 178 |
|
| 179 |
try { |
| 180 |
$helper->getContent(); |
| 181 |
$this->fail( 'Expected HttpException' ); |
| 182 |
} catch ( HttpException $ex ) { |
| 183 |
$this->assertSame( 404, $ex->getCode() ); |
| 184 |
} |
| 185 |
|
| 186 |
try { |
| 187 |
$helper->checkAccess(); |
| 188 |
$this->fail( 'Expected HttpException' ); |
| 189 |
} catch ( HttpException $ex ) { |
| 190 |
$this->assertSame( 404, $ex->getCode() ); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
public function testForbidenPage() { |
| 206 |
[ $page, $revisions ] = $this->getExistingPageWithRevisions( __METHOD__ ); |
| 207 |
$title = $page->getTitle(); |
| 208 |
$helper = $this->newHelper( |
| 209 |
[ 'id' => $revisions['first']->getId() ], |
| 210 |
$this->mockAnonNullAuthority() |
| 211 |
); |
| 212 |
|
| 213 |
$this->assertSame( $title->getPrefixedDBkey(), $helper->getTitleText() ); |
| 214 |
$this->assertTrue( $helper->getPage()->isSamePageAs( $title ) ); |
| 215 |
|
| 216 |
$this->assertTrue( $helper->hasContent() ); |
| 217 |
$this->assertFalse( $helper->isAccessible() ); |
| 218 |
|
| 219 |
$this->assertNull( $helper->getLastModified() ); |
| 220 |
|
| 221 |
try { |
| 222 |
$helper->checkAccess(); |
| 223 |
$this->fail( 'Expected HttpException' ); |
| 224 |
} catch ( HttpException $ex ) { |
| 225 |
$this->assertSame( 403, $ex->getCode() ); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
public function testParameterSettings() { |
| 233 |
$helper = $this->newHelper(); |
| 234 |
$settings = $helper->getParamSettings(); |
| 235 |
$this->assertArrayHasKey( 'id', $settings ); |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
public function testCacheControl() { |
| 242 |
$helper = $this->newHelper(); |
| 243 |
|
| 244 |
$response = new Response(); |
| 245 |
|
| 246 |
$helper->setCacheControl( $response ); |
| 247 |
$this->assertStringContainsString( 'max-age=5', $response->getHeaderLine( 'Cache-Control' ) ); |
| 248 |
|
| 249 |
$helper->setCacheControl( $response, 2 ); |
| 250 |
$this->assertStringContainsString( 'max-age=2', $response->getHeaderLine( 'Cache-Control' ) ); |
| 251 |
|
| 252 |
$helper->setCacheControl( $response, 1000 * 1000 ); |
| 253 |
$this->assertStringContainsString( 'max-age=5', $response->getHeaderLine( 'Cache-Control' ) ); |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
public function testConstructMetadata() { |
| 260 |
[ $page, $revisions ] = $this->getExistingPageWithRevisions( __METHOD__ ); |
| 261 |
$title = $page->getTitle(); |
| 262 |
|
| 263 |
$revision = $revisions['first']; |
| 264 |
$content = $revision->getContent( SlotRecord::MAIN ); |
| 265 |
$expected = [ |
| 266 |
'id' => $revision->getId(), |
| 267 |
'size' => $revision->getSize(), |
| 268 |
'minor' => $revision->isMinor(), |
| 269 |
'timestamp' => wfTimestampOrNull( TS_ISO_8601, $revision->getTimestamp() ), |
| 270 |
'content_model' => $content->getModel(), |
| 271 |
'page' => [ |
| 272 |
'id' => $title->getArticleID(), |
| 273 |
'key' => $title->getPrefixedDBkey(), |
| 274 |
'title' => $title->getPrefixedText(), |
| 275 |
], |
| 276 |
'license' => [ |
| 277 |
'url' => 'https://example.com/rights', |
| 278 |
'title' => 'some rights', |
| 279 |
], |
| 280 |
'user' => [ |
| 281 |
'id' => $revision->getUser()->getId(), |
| 282 |
'name' => $revision->getUser()->getName(), |
| 283 |
], |
| 284 |
'comment' => '', |
| 285 |
'delta' => null, |
| 286 |
]; |
| 287 |
|
| 288 |
$helper = $this->newHelper( [ 'id' => $revision->getId() ] ); |
| 289 |
$data = $helper->constructMetadata(); |
| 290 |
$this->assertEquals( $expected, $data ); |
| 291 |
} |
| 292 |
|
| 293 |
} |