| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Rest\Handler; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use MediaWiki\Content\TextContent; |
| 7 |
use MediaWiki\MainConfigNames; |
| 8 |
use MediaWiki\Rest\Handler\RevisionSourceHandler; |
| 9 |
use MediaWiki\Rest\LocalizedHttpException; |
| 10 |
use MediaWiki\Rest\RequestData; |
| 11 |
use MediaWiki\Revision\RevisionRecord; |
| 12 |
use MediaWiki\Revision\SlotRecord; |
| 13 |
use MediaWikiIntegrationTestCase; |
| 14 |
use Wikimedia\Message\MessageValue; |
| 15 |
use Wikimedia\ObjectCache\BagOStuff; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
class RevisionSourceHandlerTest extends MediaWikiIntegrationTestCase { |
| 22 |
use HandlerTestTrait; |
| 23 |
|
| 24 |
private const WIKITEXT = 'Hello \'\'\'World\'\'\''; |
| 25 |
|
| 26 |
private const HTML = '<p>Hello <b>World</b></p>'; |
| 27 |
|
| 28 |
protected function setUp(): void { |
| 29 |
parent::setUp(); |
| 30 |
|
| 31 |
$this->overrideConfigValues( [ |
| 32 |
MainConfigNames::RightsUrl => 'https://example.com/rights', |
| 33 |
MainConfigNames::RightsText => 'some rights', |
| 34 |
] ); |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
private function newHandler( ?BagOStuff $cache = null ): RevisionSourceHandler { |
| 43 |
$handler = new RevisionSourceHandler( |
| 44 |
$this->getServiceContainer()->getPageRestHelperFactory() |
| 45 |
); |
| 46 |
|
| 47 |
return $handler; |
| 48 |
} |
| 49 |
|
| 50 |
private function getExistingPageWithRevisions( $name ) { |
| 51 |
$page = $this->getNonexistingTestPage( $name ); |
| 52 |
|
| 53 |
$this->editPage( $page, self::WIKITEXT ); |
| 54 |
$revisions['first'] = $page->getRevisionRecord(); |
| 55 |
|
| 56 |
$this->editPage( $page, 'DEAD BEEF' ); |
| 57 |
$revisions['latest'] = $page->getRevisionRecord(); |
| 58 |
|
| 59 |
return [ $page, $revisions ]; |
| 60 |
} |
| 61 |
|
| 62 |
public function testExecuteBare() { |
| 63 |
[ $page, $revisions ] = $this->getExistingPageWithRevisions( __METHOD__ ); |
| 64 |
|
| 65 |
$firstRev = $revisions['first']; |
| 66 |
$request = new RequestData( |
| 67 |
[ 'pathParams' => [ 'id' => $firstRev->getId() ] ] |
| 68 |
); |
| 69 |
|
| 70 |
$htmlUrl = "https://wiki.example.com/rest/mock/revision/{$firstRev->getId()}/html"; |
| 71 |
|
| 72 |
$handler = $this->newHandler(); |
| 73 |
$config = [ 'format' => 'bare' ]; |
| 74 |
$data = $this->executeHandlerAndGetBodyData( $handler, $request, $config ); |
| 75 |
|
| 76 |
$this->assertResponseData( $firstRev, $data ); |
| 77 |
$this->assertSame( $htmlUrl, $data['html_url'] ); |
| 78 |
} |
| 79 |
|
| 80 |
public function testExecuteSource() { |
| 81 |
[ $page, $revisions ] = $this->getExistingPageWithRevisions( __METHOD__ ); |
| 82 |
|
| 83 |
$firstRev = $revisions['first']; |
| 84 |
$request = new RequestData( |
| 85 |
[ 'pathParams' => [ 'id' => $firstRev->getId() ] ] |
| 86 |
); |
| 87 |
|
| 88 |
$handler = $this->newHandler(); |
| 89 |
$config = [ 'format' => 'source' ]; |
| 90 |
$data = $this->executeHandlerAndGetBodyData( $handler, $request, $config ); |
| 91 |
|
| 92 |
|
| 93 |
$content = $firstRev->getContent( SlotRecord::MAIN ); |
| 94 |
|
| 95 |
$this->assertResponseData( $firstRev, $data ); |
| 96 |
$this->assertSame( $content->getText(), $data['source'] ); |
| 97 |
} |
| 98 |
|
| 99 |
public function testExecute_missingparam() { |
| 100 |
$request = new RequestData(); |
| 101 |
|
| 102 |
$this->expectExceptionObject( |
| 103 |
new LocalizedHttpException( |
| 104 |
new MessageValue( "paramvalidator-missingparam", [ 'title' ] ), |
| 105 |
400 |
| 106 |
) |
| 107 |
); |
| 108 |
|
| 109 |
$handler = $this->newHandler(); |
| 110 |
$this->executeHandler( $handler, $request ); |
| 111 |
} |
| 112 |
|
| 113 |
public function testExecute_error() { |
| 114 |
$request = new RequestData( [ 'pathParams' => [ 'id' => '2074398742' ] ] ); |
| 115 |
|
| 116 |
$this->expectExceptionObject( |
| 117 |
new LocalizedHttpException( |
| 118 |
new MessageValue( "rest-nonexistent-revision", [ 'testing' ] ), |
| 119 |
404 |
| 120 |
) |
| 121 |
); |
| 122 |
|
| 123 |
$handler = $this->newHandler(); |
| 124 |
$this->executeHandler( $handler, $request ); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
private function assertResponseData( RevisionRecord $rev, array $data ): void { |
| 132 |
$this->assertSame( $rev->getId(), $data['id'] ); |
| 133 |
$this->assertSame( $rev->getSize(), $data['size'] ); |
| 134 |
$this->assertSame( $rev->isMinor(), $data['minor'] ); |
| 135 |
$this->assertSame( |
| 136 |
wfTimestampOrNull( TS_ISO_8601, $rev->getTimestamp() ), |
| 137 |
$data['timestamp'] |
| 138 |
); |
| 139 |
$this->assertSame( $rev->getPage()->getId(), $data['page']['id'] ); |
| 140 |
$this->assertSame( $rev->getPage()->getDBkey(), $data['page']['key'] ); |
| 141 |
$this->assertSame( |
| 142 |
$rev->getPageAsLinkTarget()->getText(), |
| 143 |
$data['page']['title'] |
| 144 |
); |
| 145 |
$this->assertSame( CONTENT_MODEL_WIKITEXT, $data['content_model'] ); |
| 146 |
$this->assertSame( 'https://example.com/rights', $data['license']['url'] ); |
| 147 |
$this->assertSame( 'some rights', $data['license']['title'] ); |
| 148 |
$this->assertSame( $rev->getComment()->text, $data['comment'] ); |
| 149 |
$this->assertSame( $rev->getUser()->getId(), $data['user']['id'] ); |
| 150 |
$this->assertSame( $rev->getUser()->getName(), $data['user']['name'] ); |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
private function assertRestbaseCompatibleResponseData( RevisionRecord $rev, array $data ): void { |
| 158 |
$page = $this->getServiceContainer()->getWikiPageFactory() |
| 159 |
->newFromTitle( $rev->getPage() ); |
| 160 |
|
| 161 |
$this->assertSame( $page->getTitle()->getPrefixedDBkey(), $data['title'] ); |
| 162 |
$this->assertSame( $rev->getPage()->getId(), $data['page_id'] ); |
| 163 |
$this->assertSame( $rev->getId(), $data['rev'] ); |
| 164 |
$this->assertSame( $rev->getPage()->getNamespace(), $data['namespace'] ); |
| 165 |
$this->assertSame( $page->getUser(), $data['user_id'] ); |
| 166 |
$this->assertSame( $page->getUserText(), $data['user_text'] ); |
| 167 |
$this->assertSame( |
| 168 |
wfTimestampOrNull( TS_ISO_8601, $page->getTimestamp() ), |
| 169 |
$data['timestamp'] |
| 170 |
); |
| 171 |
$this->assertSame( $rev->getComment()->text, $data['comment'] ); |
| 172 |
$this->assertSame( [], $data['tags'] ); |
| 173 |
$this->assertSame( [], $data['restrictions'] ); |
| 174 |
$this->assertSame( |
| 175 |
$page->getTitle()->getPageLanguage()->getCode(), |
| 176 |
$data['page_language'] |
| 177 |
); |
| 178 |
$this->assertSame( $page->isRedirect(), $data['redirect'] ); |
| 179 |
} |
| 180 |
|
| 181 |
} |