1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
<?php
namespace MediaWiki\Tests\Rest\Handler;
use Exception;
use MediaWiki\Content\TextContent;
use MediaWiki\MainConfigNames;
use MediaWiki\Rest\Handler\PageSourceHandler;
use MediaWiki\Rest\LocalizedHttpException;
use MediaWiki\Rest\RequestData;
use MediaWiki\Revision\SlotRecord;
use MediaWikiIntegrationTestCase;
use Wikimedia\Message\MessageValue;
use WikiPage;
/**
* @covers \MediaWiki\Rest\Handler\PageSourceHandler
* @group Database
*/
class PageSourceHandlerTest extends MediaWikiIntegrationTestCase {
use HandlerTestTrait;
use PageHandlerTestTrait;
private const WIKITEXT = 'Hello \'\'\'World\'\'\'';
private const HTML = '<p>Hello <b>World</b></p>';
protected function setUp(): void {
parent::setUp();
$this->overrideConfigValues( [
MainConfigNames::RightsUrl => 'https://example.com/rights',
MainConfigNames::RightsText => 'some rights',
] );
}
/**
* @return PageSourceHandler
* @throws Exception
*/
private function newHandler(): PageSourceHandler {
return $this->newPageSourceHandler();
}
public function testExecuteBare() {
$page = $this->getExistingTestPage( 'Talk:SourceEndpointTestPage/with/slashes' );
$request = new RequestData(
[ 'pathParams' => [ 'title' => $page->getTitle()->getPrefixedText() ] ]
);
$htmlUrl = 'https://wiki.example.com/rest/mock/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 testExecuteRestbaseCompat() {
$page = $this->getExistingTestPage( 'Talk:SourceEndpointTestPage/with/slashes' );
$request = new RequestData(
[
'pathParams' => [ 'title' => $page->getTitle()->getPrefixedText() ],
'headers' => [ 'x-restbase-compat' => 'true' ]
]
);
$htmlUrl = 'https://wiki.example.com/rest/mock/page/Talk%3ASourceEndpointTestPage%2Fwith%2Fslashes/html';
$handler = $this->newHandler();
$config = [ 'format' => 'bare' ];
$data = $this->executeHandlerAndGetBodyData( $handler, $request, $config );
$this->assertRestbaseCompatibleResponseData( $page, $data );
}
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();
$config = [ 'format' => 'bare' ];
$this->executeHandler( $handler, $request, $config );
}
public function testExecute_message() {
$request = new RequestData( [ 'pathParams' => [ 'title' => 'MediaWiki:Ok' ] ] );
$this->expectExceptionObject(
new LocalizedHttpException(
new MessageValue( "rest-nonexistent-title", [ 'testing' ] ),
404
)
);
$handler = $this->newHandler();
$config = [ 'format' => 'bare' ];
$this->executeHandler( $handler, $request, $config );
}
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'] );
}
private function assertRestbaseCompatibleResponseData( WikiPage $page, array $data ): void {
$this->assertArrayHasKey( 'items', $data );
$this->assertSame( $page->getTitle()->getPrefixedDBkey(), $data['items'][0]['title'] );
$this->assertSame( $page->getId(), $data['items'][0]['page_id'] );
$this->assertSame( $page->getLatest(), $data['items'][0]['rev'] );
$this->assertSame( $page->getNamespace(), $data['items'][0]['namespace'] );
$this->assertSame( $page->getUser(), $data['items'][0]['user_id'] );
$this->assertSame( $page->getUserText(), $data['items'][0]['user_text'] );
$this->assertSame(
wfTimestampOrNull( TS_ISO_8601, $page->getTimestamp() ),
$data['items'][0]['timestamp']
);
$this->assertSame( $page->getComment(), $data['items'][0]['comment'] );
$this->assertSame( [], $data['items'][0]['tags'] );
$this->assertSame( [], $data['items'][0]['restrictions'] );
$this->assertSame(
$page->getTitle()->getPageLanguage()->getCode(),
$data['items'][0]['page_language']
);
$this->assertSame( $page->isRedirect(), $data['items'][0]['redirect'] );
}
}
|