aboutsummaryrefslogtreecommitdiffstats
path: root/tests/phpunit
diff options
context:
space:
mode:
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>2020-12-09 22:04:21 +0000
committerGerrit Code Review <gerrit@wikimedia.org>2020-12-09 22:04:21 +0000
commit2b7dac53f3ac38e36a69854160ea981e43bdb7f2 (patch)
tree27c7f5747b3a0882c2edc557bc04c0784fda6039 /tests/phpunit
parent3b0fb6559d18d23a0716047019cec56a0a44627e (diff)
parent808d841447599948ce3e004e66776e6554918dcc (diff)
downloadmediawikicore-2b7dac53f3ac38e36a69854160ea981e43bdb7f2.tar.gz
mediawikicore-2b7dac53f3ac38e36a69854160ea981e43bdb7f2.zip
Merge "Moved page/{title}/bare to PageSourceHandler"
Diffstat (limited to 'tests/phpunit')
-rw-r--r--tests/phpunit/integration/includes/Rest/Handler/PageHTMLHandlerTest.php16
-rw-r--r--tests/phpunit/integration/includes/Rest/Handler/PageSourceHandlerTest.php140
2 files changed, 140 insertions, 16 deletions
diff --git a/tests/phpunit/integration/includes/Rest/Handler/PageHTMLHandlerTest.php b/tests/phpunit/integration/includes/Rest/Handler/PageHTMLHandlerTest.php
index d66221ac4b13..f06d6b1c2f58 100644
--- a/tests/phpunit/integration/includes/Rest/Handler/PageHTMLHandlerTest.php
+++ b/tests/phpunit/integration/includes/Rest/Handler/PageHTMLHandlerTest.php
@@ -96,22 +96,6 @@ class PageHTMLHandlerTest extends MediaWikiIntegrationTestCase {
return $handler;
}
- public function testExecuteBare() {
- $page = $this->getExistingTestPage( 'Talk:HtmlEndpointTestPage/with/slashes' );
- $request = new RequestData(
- [ 'pathParams' => [ 'title' => $page->getTitle()->getPrefixedText() ] ]
- );
-
- $htmlUrl = 'https://wiki.example.com/rest/v1/page/Talk%3AHtmlEndpointTestPage%2Fwith%2Fslashes/html';
-
- $handler = $this->newHandler();
- $config = [ 'format' => 'bare' ];
- $data = $this->executeHandlerAndGetBodyData( $handler, $request, $config );
-
- $this->assertResponseData( $page, $data );
- $this->assertSame( $htmlUrl, $data['html_url'] );
- }
-
public function testExecuteWithHtml() {
$this->checkParsoidInstalled();
$page = $this->getExistingTestPage( 'HtmlEndpointTestPage/with/slashes' );
diff --git a/tests/phpunit/integration/includes/Rest/Handler/PageSourceHandlerTest.php b/tests/phpunit/integration/includes/Rest/Handler/PageSourceHandlerTest.php
new file mode 100644
index 000000000000..0b113e12b599
--- /dev/null
+++ b/tests/phpunit/integration/includes/Rest/Handler/PageSourceHandlerTest.php
@@ -0,0 +1,140 @@
+<?php
+
+namespace MediaWiki\Tests\Rest\Handler;
+
+use BagOStuff;
+use Exception;
+use HashConfig;
+use MediaWiki\Rest\Handler\PageSourceHandler;
+use MediaWiki\Rest\LocalizedHttpException;
+use MediaWiki\Rest\RequestData;
+use MediaWiki\Revision\SlotRecord;
+use MediaWikiIntegrationTestCase;
+use TextContent;
+use Wikimedia\Message\MessageValue;
+use WikiPage;
+
+/**
+ * @covers \MediaWiki\Rest\Handler\PageSourceHandler
+ * @group Database
+ */
+class PageSourceHandlerTest extends MediaWikiIntegrationTestCase {
+ use HandlerTestTrait;
+
+ private const WIKITEXT = 'Hello \'\'\'World\'\'\'';
+
+ private const HTML = '<p>Hello <b>World</b></p>';
+
+ protected function setUp(): void {
+ parent::setUp();
+
+ // Clean up these tables after each test
+ $this->tablesUsed = [
+ 'page',
+ 'revision',
+ 'comment',
+ 'text',
+ 'content'
+ ];
+ }
+
+ /**
+ * @param BagOStuff|null $cache
+ * @return PageSourceHandler
+ * @throws Exception
+ */
+ private function newHandler( BagOStuff $cache = null ): PageSourceHandler {
+ $handler = new PageSourceHandler(
+ new HashConfig( [
+ 'RightsUrl' => 'https://example.com/rights',
+ 'RightsText' => 'some rights',
+ ] ),
+ $this->getServiceContainer()->getPermissionManager(),
+ $this->getServiceContainer()->getRevisionLookup(),
+ $this->getServiceContainer()->getTitleFormatter(),
+ $this->getServiceContainer()->getTitleFactory()
+ );
+
+ return $handler;
+ }
+
+ public function testExecuteBare() {
+ $page = $this->getExistingTestPage( 'Talk:SourceEndpointTestPage/with/slashes' );
+ $request = new RequestData(
+ [ 'pathParams' => [ 'title' => $page->getTitle()->getPrefixedText() ] ]
+ );
+
+ $htmlUrl = 'https://wiki.example.com/rest/v1/page/Talk%3ASourceEndpointTestPage%2Fwith%2Fslashes/html';
+
+ $handler = $this->newHandler();
+ $config = [ 'format' => 'bare' ];
+ $data = $this->executeHandlerAndGetBodyData( $handler, $request, $config );
+
+ $this->assertResponseData( $page, $data );
+ $this->assertSame( $htmlUrl, $data['html_url'] );
+ }
+
+ public function testExecuteSource() {
+ $page = $this->getExistingTestPage( 'Talk:SourceEndpointTestPage/with/slashes' );
+ $request = new RequestData(
+ [ 'pathParams' => [ 'title' => $page->getTitle()->getPrefixedText() ] ]
+ );
+
+ $handler = $this->newHandler();
+ $config = [ 'format' => 'source' ];
+ $data = $this->executeHandlerAndGetBodyData( $handler, $request, $config );
+
+ /** @var TextContent $content */
+ $content = $page->getRevisionRecord()->getContent( SlotRecord::MAIN );
+
+ $this->assertResponseData( $page, $data );
+ $this->assertSame( $content->getText(), $data['source'] );
+ }
+
+ public function testExecute_missingparam() {
+ $request = new RequestData();
+
+ $this->expectExceptionObject(
+ new LocalizedHttpException(
+ new MessageValue( "paramvalidator-missingparam", [ 'title' ] ),
+ 400
+ )
+ );
+
+ $handler = $this->newHandler();
+ $this->executeHandler( $handler, $request );
+ }
+
+ public function testExecute_error() {
+ $request = new RequestData( [ 'pathParams' => [ 'title' => 'DoesNotExist8237456assda1234' ] ] );
+
+ $this->expectExceptionObject(
+ new LocalizedHttpException(
+ new MessageValue( "rest-nonexistent-title", [ 'testing' ] ),
+ 404
+ )
+ );
+
+ $handler = $this->newHandler();
+ $this->executeHandler( $handler, $request );
+ }
+
+ /**
+ * @param WikiPage $page
+ * @param array $data
+ */
+ private function assertResponseData( WikiPage $page, array $data ): void {
+ $this->assertSame( $page->getId(), $data['id'] );
+ $this->assertSame( $page->getTitle()->getPrefixedDBkey(), $data['key'] );
+ $this->assertSame( $page->getTitle()->getPrefixedText(), $data['title'] );
+ $this->assertSame( $page->getLatest(), $data['latest']['id'] );
+ $this->assertSame(
+ wfTimestampOrNull( TS_ISO_8601, $page->getTimestamp() ),
+ $data['latest']['timestamp']
+ );
+ $this->assertSame( CONTENT_MODEL_WIKITEXT, $data['content_model'] );
+ $this->assertSame( 'https://example.com/rights', $data['license']['url'] );
+ $this->assertSame( 'some rights', $data['license']['title'] );
+ }
+
+}