Debian Codegraph @

tests/phpunit/includes/api/ApiChangeContentModelTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Api;
4
5 use MediaWiki\Api\ApiUsageException;
6 use MediaWiki\MainConfigNames;
7 use MediaWiki\Permissions\RateLimiter;
8 use MediaWiki\Status\Status;
9 use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait;
10 use MediaWiki\Tests\User\TempUser\TempUserTestTrait;
11 use MediaWiki\Title\Title;
12 use Wikimedia\Rdbms\IDBAccessObject;
13
14 /**
15 * Tests for editing page content model via api
16 *
17 * @group API
18 * @group Database
19 * @group medium
20 *
21 * @covers \MediaWiki\Api\ApiChangeContentModel
22 * @author DannyS712
23 */
24 class ApiChangeContentModelTest extends ApiTestCase {
25 use MockAuthorityTrait;
26 use TempUserTestTrait;
27
28 protected function setUp(): void {
29 parent::setUp();
30
31 $this->getExistingTestPage( 'ExistingPage' );
32
33 $this->overrideConfigValues( [
34 MainConfigNames::ExtraNamespaces => [
35 12312 => 'Dummy',
36 12313 => 'Dummy_talk',
37 ],
38 MainConfigNames::NamespaceContentModels => [
39 12312 => 'testing',
40 ],
41 ] );
42 $this->mergeMwGlobalArrayValue( 'wgContentHandlers', [
43 'testing' => 'DummyContentHandlerForTesting',
44 ] );
45 }
46
47 public function testTitleMustExist() {
48 $title = Title::makeTitle( NS_MAIN, 'ApiChangeContentModelTest::TestTitleMustExist' );
49
50 $this->assertFalse(
51 $title->exists(),
52 'Check that title does not exist already'
53 );
54
55 $this->expectApiErrorCode( 'changecontentmodel-missingtitle' );
56
57 $this->doApiRequestWithToken( [
58 'action' => 'changecontentmodel',
59 'title' => $title->getPrefixedText(),
60 'model' => 'text'
61 ] );
62 }
63
64 /**
65 * Test user needs `editcontentmodel` rights
66 */
67 public function testRightsNeeded() {
68 $this->expectApiErrorCode( 'permissiondenied' );
69
70 $this->doApiRequestWithToken( [
71 'action' => 'changecontentmodel',
72 'summary' => __METHOD__,
73 'title' => 'ExistingPage',
74 'model' => 'text'
75 ],
76 null,
77 $this->mockAnonAuthorityWithoutPermissions( [ 'editcontentmodel' ] ) );
78 }
79
80 /**
81 * Test that the `editcontentmodel` rate limit is enforced
82 */
83 public function testRateLimitApplies() {
84 $limiter = $this->createNoOpMock(
85 RateLimiter::class,
86 [ 'limit', 'isLimitable', ]
87 );
88 $limiter->method( 'limit' )
89 ->willReturnCallback( function ( $user, $action, $incr ) {
90 if ( $action === 'editcontentmodel' ) {
91 $this->assertSame( 1, $incr );
92 return true;
93 }
94 return false;
95 } );
96 $limiter->method( 'isLimitable' )
97 ->willReturn( true );
98
99 $this->setService( 'RateLimiter', $limiter );
100
101 $this->setExpectedApiException( [
102 'apierror-ratelimited',
103 wfMessage( 'action-ratelimited' )
104 ] );
105
106 $this->doApiRequestWithToken( [
107 'action' => 'changecontentmodel',
108 'title' => 'ExistingPage',
109 'summary' => 'test',
110 'model' => 'text'
111 ] );
112 }
113
114 /**
115 * Test that the content model needs to change
116 */
117 public function testChangeNeeded() {
118 $this->assertSame(
119 'wikitext',
120 Title::makeTitle( NS_MAIN, 'ExistingPage' )->getContentModel(),
121 '`ExistingPage` should be wikitext'
122 );
123
124 $this->expectApiErrorCode( 'nochanges' );
125
126 $this->doApiRequestWithToken( [
127 'action' => 'changecontentmodel',
128 'summary' => __METHOD__,
129 'title' => 'ExistingPage',
130 'model' => 'wikitext'
131 ] );
132 }
133
134 /**
135 * Test that the content needs to be valid for the requested model
136 */
137 public function testInvalidContent() {
138 $wikipage = $this->getExistingTestPage( 'PageWithTextThatIsNotValidJSON' );
139 $invalidJSON = 'Foo\nBar\nEaster egg\nT22281';
140 $wikipage->doUserEditContent(
141 $wikipage->getContentHandler()->unserializeContent( $invalidJSON ),
142 $this->getTestSysop()->getAuthority(),
143 'EditSummaryForThisTest',
144 EDIT_UPDATE | EDIT_SUPPRESS_RC
145 );
146 $this->assertSame(
147 'wikitext',
148 $wikipage->getTitle()->getContentModel(),
149 '`PageWithTextThatIsNotValidJSON` should be wikitext at first'
150 );
151
152 $this->expectApiErrorCode( 'invalid-json-data' );
153 $this->doApiRequestWithToken( [
154 'action' => 'changecontentmodel',
155 'summary' => __METHOD__,
156 'title' => 'PageWithTextThatIsNotValidJSON',
157 'model' => 'json'
158 ],
159 null,
160 $this->mockAnonAuthorityWithPermissions( [ 'edit', 'editcontentmodel' ] )
161 );
162 }
163
164 /**
165 * Test the EditFilterMergedContent hook can be intercepted
166 *
167 * @dataProvider provideTestEditFilterMergedContent
168 * @param string|bool $customMessage Hook message, or false
169 * @param string $expectedMessage expected fatal
170 */
171 public function testEditFilterMergedContent( $customMessage, $expectedMessage ) {
172 $title = Title::makeTitle( NS_MAIN, 'ExistingPage' );
173
174 $this->assertSame(
175 'wikitext',
176 $title->getContentModel( IDBAccessObject::READ_LATEST ),
177 '`ExistingPage` should be wikitext'
178 );
179
180 $this->setTemporaryHook( 'EditFilterMergedContent',
181 static function ( $unused1, $unused2, Status $status ) use ( $customMessage ) {
182 if ( $customMessage !== false ) {
183 $status->fatal( $customMessage );
184 }
185 return false;
186 }
187 );
188
189 $exception = new ApiUsageException(
190 null,
191 Status::newFatal( $expectedMessage )
192 );
193 $this->expectException( ApiUsageException::class );
194 $this->expectExceptionMessage( $exception->getMessage() );
195
196 $this->doApiRequestWithToken( [
197 'action' => 'changecontentmodel',
198 'summary' => __METHOD__,
199 'title' => 'ExistingPage',
200 'model' => 'text'
201 ],
202 null,
203 $this->mockAnonAuthorityWithPermissions( [ 'edit', 'editcontentmodel' ] )
204 );
205 }
206
207 public static function provideTestEditFilterMergedContent() {
208 return [
209 [ 'DannyS712 objects to this change!', 'DannyS712 objects to this change!' ],
210 [ false, 'hookaborted' ]
211 ];
212 }
213
214 /**
215 * Test the ContentModelCanBeUsedOn hook can be intercepted
216 */
217 public function testContentModelCanBeUsedOn() {
218 $title = Title::makeTitle( NS_MAIN, 'ExistingPage' );
219
220 $this->assertSame(
221 'wikitext',
222 $title->getContentModel( IDBAccessObject::READ_LATEST ),
223 '`ExistingPage` should be wikitext'
224 );
225
226 $this->setTemporaryHook( 'ContentModelCanBeUsedOn',
227 static function ( $unused1, $unused2, &$ok ) {
228 $ok = false;
229 return false;
230 }
231 );
232
233 $this->expectApiErrorCode( 'changecontentmodel-cannotbeused' );
234
235 $this->doApiRequestWithToken( [
236 'action' => 'changecontentmodel',
237 'summary' => __METHOD__,
238 'title' => 'ExistingPage',
239 'model' => 'text'
240 ],
241 null,
242 $this->mockAnonAuthorityWithPermissions( [ 'edit', 'editcontentmodel' ] )
243 );
244 }
245
246 /**
247 * Test that content handler must support direct editing
248 */
249 public function testNoDirectEditing() {
250 $title = Title::newFromText( 'Dummy:NoDirectEditing' );
251
252 $dummyContent = $this->getServiceContainer()
253 ->getContentHandlerFactory()
254 ->getContentHandler( 'testing' )->makeEmptyContent();
255 $this->editPage(
256 $title,
257 $dummyContent,
258 'EditSummaryForThisTest',
259 NS_MAIN,
260 $this->getTestSysop()->getAuthority()
261 );
262 $this->assertSame(
263 'testing',
264 $title->getContentModel( IDBAccessObject::READ_LATEST ),
265 'Dummy:NoDirectEditing should start with the `testing` content model'
266 );
267
268 $this->expectApiErrorCode( 'changecontentmodel-nodirectediting' );
269
270 $this->doApiRequestWithToken( [
271 'action' => 'changecontentmodel',
272 'summary' => __METHOD__,
273 'title' => 'Dummy:NoDirectEditing',
274 'model' => 'wikitext'
275 ],
276 null,
277 $this->mockAnonAuthorityWithPermissions( [ 'edit', 'editcontentmodel' ] )
278 );
279 }
280
281 public function testCannotApplyTags() {
282 $this->getServiceContainer()->getChangeTagsStore()->defineTag( 'api edit content model tag' );
283 $this->expectApiErrorCode( 'tags-apply-no-permission' );
284
285 $this->doApiRequestWithToken( [
286 'action' => 'changecontentmodel',
287 'summary' => __METHOD__,
288 'title' => 'ExistingPage',
289 'model' => 'text',
290 'tags' => 'api edit content model tag',
291 ],
292 null,
293 $this->mockAnonAuthorityWithoutPermissions( [ 'applychangetags' ] ) );
294 }
295
296 /**
297 * Test that it works
298 */
299 public function testEverythingWorks() {
300 $this->disableAutoCreateTempUser();
301 $title = Title::makeTitle( NS_MAIN, 'ExistingPage' );
302 $performer = $this->mockAnonAuthorityWithPermissions(
303 [ 'edit', 'editcontentmodel', 'applychangetags' ]
304 );
305 $this->assertSame(
306 'wikitext',
307 $title->getContentModel( IDBAccessObject::READ_LATEST ),
308 '`ExistingPage` should be wikitext'
309 );
310
311 $this->getServiceContainer()->getChangeTagsStore()->defineTag( 'api edit content model tag' );
312
313 $data = $this->doApiRequestWithToken( [
314 'action' => 'changecontentmodel',
315 'summary' => __METHOD__,
316 'title' => 'ExistingPage',
317 'model' => 'text',
318 'tags' => 'api edit content model tag',
319 ], null, $performer );
320
321 $this->assertSame(
322 'text',
323 $title->getContentModel( IDBAccessObject::READ_LATEST ),
324 'API can successfully change the content model'
325 );
326
327 $data = $data[0]['changecontentmodel'];
328 $this->assertSame( 'Success', $data['result'], 'API reports successful change' );
329 $firstLogId = (int)$data['logid'];
330 $firstRevId = (int)$data['revid'];
331 $this->assertGreaterThan( 0, $firstLogId, 'Plausible log id generated' );
332 $this->assertGreaterThan( 0, $firstRevId, 'Plausible rev id generated' );
333
334 $data = $this->doApiRequestWithToken( [
335 'action' => 'changecontentmodel',
336 // no 'summary', should be optional
337 'title' => 'ExistingPage',
338 'model' => 'wikitext',
339 'tags' => 'api edit content model tag',
340 ], null, $performer );
341
342 $this->assertSame(
343 'wikitext',
344 $title->getContentModel( IDBAccessObject::READ_LATEST ),
345 'API can also change the content model back'
346 );
347
348 $data = $data[0]['changecontentmodel'];
349 $this->assertSame( 'Success', $data['result'], 'API reports successful change back' );
350 $this->assertGreaterThan(
351 $firstLogId,
352 (int)$data['logid'],
353 'Second log entry should come after the first'
354 );
355 $this->assertGreaterThan(
356 $firstRevId,
357 (int)$data['revid'],
358 'Second revision should come after the first'
359 );
360
361 $this->assertSame(
362 '4',
363 $this->getDb()->newSelectQueryBuilder()
364 ->select( 'ctd_count' )
365 ->from( 'change_tag_def' )
366 ->where( [ 'ctd_name' => 'api edit content model tag' ] )
367 ->caller( __METHOD__ )->fetchField(),
368 'There should be four uses of the `api edit content model tag` tag, '
369 . 'two for the two revisions and two for the two log entries'
370 );
371 }
372 }