Debian Codegraph @

tests/phpunit/integration/includes/Rest/Handler/PageSourceHandlerTest.php

raw symbols SCIP
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\PageSourceHandler;
9 use MediaWiki\Rest\LocalizedHttpException;
10 use MediaWiki\Rest\RequestData;
11 use MediaWiki\Revision\SlotRecord;
12 use MediaWikiIntegrationTestCase;
13 use Wikimedia\Message\MessageValue;
14 use WikiPage;
15
16 /**
17 * @covers \MediaWiki\Rest\Handler\PageSourceHandler
18 * @group Database
19 */
20 class PageSourceHandlerTest extends MediaWikiIntegrationTestCase {
21 use HandlerTestTrait;
22 use PageHandlerTestTrait;
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 * @return PageSourceHandler
39 * @throws Exception
40 */
41 private function newHandler(): PageSourceHandler {
42 return $this->newPageSourceHandler();
43 }
44
45 public function testExecuteBare() {
46 $page = $this->getExistingTestPage( 'Talk:SourceEndpointTestPage/with/slashes' );
47 $request = new RequestData(
48 [ 'pathParams' => [ 'title' => $page->getTitle()->getPrefixedText() ] ]
49 );
50
51 $htmlUrl = 'https://wiki.example.com/rest/mock/page/Talk%3ASourceEndpointTestPage%2Fwith%2Fslashes/html';
52
53 $handler = $this->newHandler();
54 $config = [ 'format' => 'bare' ];
55 $data = $this->executeHandlerAndGetBodyData( $handler, $request, $config );
56
57 $this->assertResponseData( $page, $data );
58 $this->assertSame( $htmlUrl, $data['html_url'] );
59 }
60
61 public function testExecuteSource() {
62 $page = $this->getExistingTestPage( 'Talk:SourceEndpointTestPage/with/slashes' );
63 $request = new RequestData(
64 [ 'pathParams' => [ 'title' => $page->getTitle()->getPrefixedText() ] ]
65 );
66
67 $handler = $this->newHandler();
68 $config = [ 'format' => 'source' ];
69 $data = $this->executeHandlerAndGetBodyData( $handler, $request, $config );
70
71 /** @var TextContent $content */
72 $content = $page->getRevisionRecord()->getContent( SlotRecord::MAIN );
73
74 $this->assertResponseData( $page, $data );
75 $this->assertSame( $content->getText(), $data['source'] );
76 }
77
78 public function testExecuteRestbaseCompat() {
79 $page = $this->getExistingTestPage( 'Talk:SourceEndpointTestPage/with/slashes' );
80 $request = new RequestData(
81 [
82 'pathParams' => [ 'title' => $page->getTitle()->getPrefixedText() ],
83 'headers' => [ 'x-restbase-compat' => 'true' ]
84 ]
85
86 );
87
88 $htmlUrl = 'https://wiki.example.com/rest/mock/page/Talk%3ASourceEndpointTestPage%2Fwith%2Fslashes/html';
89
90 $handler = $this->newHandler();
91 $config = [ 'format' => 'bare' ];
92 $data = $this->executeHandlerAndGetBodyData( $handler, $request, $config );
93
94 $this->assertRestbaseCompatibleResponseData( $page, $data );
95 }
96
97 public function testExecute_missingparam() {
98 $request = new RequestData();
99
100 $this->expectExceptionObject(
101 new LocalizedHttpException(
102 new MessageValue( "paramvalidator-missingparam", [ 'title' ] ),
103 400
104 )
105 );
106
107 $handler = $this->newHandler();
108 $this->executeHandler( $handler, $request );
109 }
110
111 public function testExecute_error() {
112 $request = new RequestData( [ 'pathParams' => [ 'title' => 'DoesNotExist8237456assda1234' ] ] );
113
114 $this->expectExceptionObject(
115 new LocalizedHttpException(
116 new MessageValue( "rest-nonexistent-title", [ 'testing' ] ),
117 404
118 )
119 );
120
121 $handler = $this->newHandler();
122 $config = [ 'format' => 'bare' ];
123 $this->executeHandler( $handler, $request, $config );
124 }
125
126 public function testExecute_message() {
127 $request = new RequestData( [ 'pathParams' => [ 'title' => 'MediaWiki:Ok' ] ] );
128
129 $this->expectExceptionObject(
130 new LocalizedHttpException(
131 new MessageValue( "rest-nonexistent-title", [ 'testing' ] ),
132 404
133 )
134 );
135
136 $handler = $this->newHandler();
137 $config = [ 'format' => 'bare' ];
138 $this->executeHandler( $handler, $request, $config );
139 }
140
141 /**
142 * @param WikiPage $page
143 * @param array $data
144 */
145 private function assertResponseData( WikiPage $page, array $data ): void {
146 $this->assertSame( $page->getId(), $data['id'] );
147 $this->assertSame( $page->getTitle()->getPrefixedDBkey(), $data['key'] );
148 $this->assertSame( $page->getTitle()->getPrefixedText(), $data['title'] );
149 $this->assertSame( $page->getLatest(), $data['latest']['id'] );
150 $this->assertSame(
151 wfTimestampOrNull( TS_ISO_8601, $page->getTimestamp() ),
152 $data['latest']['timestamp']
153 );
154 $this->assertSame( CONTENT_MODEL_WIKITEXT, $data['content_model'] );
155 $this->assertSame( 'https://example.com/rights', $data['license']['url'] );
156 $this->assertSame( 'some rights', $data['license']['title'] );
157 }
158
159 /**
160 * @param WikiPage $page
161 * @param array $data
162 */
163 private function assertRestbaseCompatibleResponseData( WikiPage $page, array $data ): void {
164 $this->assertArrayHasKey( 'items', $data );
165 $this->assertSame( $page->getTitle()->getPrefixedDBkey(), $data['items'][0]['title'] );
166 $this->assertSame( $page->getId(), $data['items'][0]['page_id'] );
167 $this->assertSame( $page->getLatest(), $data['items'][0]['rev'] );
168 $this->assertSame( $page->getNamespace(), $data['items'][0]['namespace'] );
169 $this->assertSame( $page->getUser(), $data['items'][0]['user_id'] );
170 $this->assertSame( $page->getUserText(), $data['items'][0]['user_text'] );
171 $this->assertSame(
172 wfTimestampOrNull( TS_ISO_8601, $page->getTimestamp() ),
173 $data['items'][0]['timestamp']
174 );
175 $this->assertSame( $page->getComment(), $data['items'][0]['comment'] );
176 $this->assertSame( [], $data['items'][0]['tags'] );
177 $this->assertSame( [], $data['items'][0]['restrictions'] );
178 $this->assertSame(
179 $page->getTitle()->getPageLanguage()->getCode(),
180 $data['items'][0]['page_language']
181 );
182 $this->assertSame( $page->isRedirect(), $data['items'][0]['redirect'] );
183 }
184
185 }