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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
|
<?php
namespace MediaWiki\Tests\Api;
use MediaWiki\Api\ApiUsageException;
use MediaWiki\MainConfigNames;
use MediaWiki\Permissions\RateLimiter;
use MediaWiki\Status\Status;
use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait;
use MediaWiki\Tests\User\TempUser\TempUserTestTrait;
use MediaWiki\Title\Title;
use Wikimedia\Rdbms\IDBAccessObject;
/**
* Tests for editing page content model via api
*
* @group API
* @group Database
* @group medium
*
* @covers \MediaWiki\Api\ApiChangeContentModel
* @author DannyS712
*/
class ApiChangeContentModelTest extends ApiTestCase {
use MockAuthorityTrait;
use TempUserTestTrait;
protected function setUp(): void {
parent::setUp();
$this->getExistingTestPage( 'ExistingPage' );
$this->overrideConfigValues( [
MainConfigNames::ExtraNamespaces => [
12312 => 'Dummy',
12313 => 'Dummy_talk',
],
MainConfigNames::NamespaceContentModels => [
12312 => 'testing',
],
] );
$this->mergeMwGlobalArrayValue( 'wgContentHandlers', [
'testing' => 'DummyContentHandlerForTesting',
] );
}
public function testTitleMustExist() {
$title = Title::makeTitle( NS_MAIN, 'ApiChangeContentModelTest::TestTitleMustExist' );
$this->assertFalse(
$title->exists(),
'Check that title does not exist already'
);
$this->expectApiErrorCode( 'changecontentmodel-missingtitle' );
$this->doApiRequestWithToken( [
'action' => 'changecontentmodel',
'title' => $title->getPrefixedText(),
'model' => 'text'
] );
}
/**
* Test user needs `editcontentmodel` rights
*/
public function testRightsNeeded() {
$this->expectApiErrorCode( 'permissiondenied' );
$this->doApiRequestWithToken( [
'action' => 'changecontentmodel',
'summary' => __METHOD__,
'title' => 'ExistingPage',
'model' => 'text'
],
null,
$this->mockAnonAuthorityWithoutPermissions( [ 'editcontentmodel' ] ) );
}
/**
* Test that the `editcontentmodel` rate limit is enforced
*/
public function testRateLimitApplies() {
$limiter = $this->createNoOpMock(
RateLimiter::class,
[ 'limit', 'isLimitable', ]
);
$limiter->method( 'limit' )
->willReturnCallback( function ( $user, $action, $incr ) {
if ( $action === 'editcontentmodel' ) {
$this->assertSame( 1, $incr );
return true;
}
return false;
} );
$limiter->method( 'isLimitable' )
->willReturn( true );
$this->setService( 'RateLimiter', $limiter );
$this->setExpectedApiException( [
'apierror-ratelimited',
wfMessage( 'action-ratelimited' )
] );
$this->doApiRequestWithToken( [
'action' => 'changecontentmodel',
'title' => 'ExistingPage',
'summary' => 'test',
'model' => 'text'
] );
}
/**
* Test that the content model needs to change
*/
public function testChangeNeeded() {
$this->assertSame(
'wikitext',
Title::makeTitle( NS_MAIN, 'ExistingPage' )->getContentModel(),
'`ExistingPage` should be wikitext'
);
$this->expectApiErrorCode( 'nochanges' );
$this->doApiRequestWithToken( [
'action' => 'changecontentmodel',
'summary' => __METHOD__,
'title' => 'ExistingPage',
'model' => 'wikitext'
] );
}
/**
* Test that the content needs to be valid for the requested model
*/
public function testInvalidContent() {
$wikipage = $this->getExistingTestPage( 'PageWithTextThatIsNotValidJSON' );
$invalidJSON = 'Foo\nBar\nEaster egg\nT22281';
$wikipage->doUserEditContent(
$wikipage->getContentHandler()->unserializeContent( $invalidJSON ),
$this->getTestSysop()->getAuthority(),
'EditSummaryForThisTest',
EDIT_UPDATE | EDIT_SUPPRESS_RC
);
$this->assertSame(
'wikitext',
$wikipage->getTitle()->getContentModel(),
'`PageWithTextThatIsNotValidJSON` should be wikitext at first'
);
$this->expectApiErrorCode( 'invalid-json-data' );
$this->doApiRequestWithToken( [
'action' => 'changecontentmodel',
'summary' => __METHOD__,
'title' => 'PageWithTextThatIsNotValidJSON',
'model' => 'json'
],
null,
$this->mockAnonAuthorityWithPermissions( [ 'edit', 'editcontentmodel' ] )
);
}
/**
* Test the EditFilterMergedContent hook can be intercepted
*
* @dataProvider provideTestEditFilterMergedContent
* @param string|bool $customMessage Hook message, or false
* @param string $expectedMessage expected fatal
*/
public function testEditFilterMergedContent( $customMessage, $expectedMessage ) {
$title = Title::makeTitle( NS_MAIN, 'ExistingPage' );
$this->assertSame(
'wikitext',
$title->getContentModel( IDBAccessObject::READ_LATEST ),
'`ExistingPage` should be wikitext'
);
$this->setTemporaryHook( 'EditFilterMergedContent',
static function ( $unused1, $unused2, Status $status ) use ( $customMessage ) {
if ( $customMessage !== false ) {
$status->fatal( $customMessage );
}
return false;
}
);
$exception = new ApiUsageException(
null,
Status::newFatal( $expectedMessage )
);
$this->expectException( ApiUsageException::class );
$this->expectExceptionMessage( $exception->getMessage() );
$this->doApiRequestWithToken( [
'action' => 'changecontentmodel',
'summary' => __METHOD__,
'title' => 'ExistingPage',
'model' => 'text'
],
null,
$this->mockAnonAuthorityWithPermissions( [ 'edit', 'editcontentmodel' ] )
);
}
public static function provideTestEditFilterMergedContent() {
return [
[ 'DannyS712 objects to this change!', 'DannyS712 objects to this change!' ],
[ false, 'hookaborted' ]
];
}
/**
* Test the ContentModelCanBeUsedOn hook can be intercepted
*/
public function testContentModelCanBeUsedOn() {
$title = Title::makeTitle( NS_MAIN, 'ExistingPage' );
$this->assertSame(
'wikitext',
$title->getContentModel( IDBAccessObject::READ_LATEST ),
'`ExistingPage` should be wikitext'
);
$this->setTemporaryHook( 'ContentModelCanBeUsedOn',
static function ( $unused1, $unused2, &$ok ) {
$ok = false;
return false;
}
);
$this->expectApiErrorCode( 'changecontentmodel-cannotbeused' );
$this->doApiRequestWithToken( [
'action' => 'changecontentmodel',
'summary' => __METHOD__,
'title' => 'ExistingPage',
'model' => 'text'
],
null,
$this->mockAnonAuthorityWithPermissions( [ 'edit', 'editcontentmodel' ] )
);
}
/**
* Test that content handler must support direct editing
*/
public function testNoDirectEditing() {
$title = Title::newFromText( 'Dummy:NoDirectEditing' );
$dummyContent = $this->getServiceContainer()
->getContentHandlerFactory()
->getContentHandler( 'testing' )->makeEmptyContent();
$this->editPage(
$title,
$dummyContent,
'EditSummaryForThisTest',
NS_MAIN,
$this->getTestSysop()->getAuthority()
);
$this->assertSame(
'testing',
$title->getContentModel( IDBAccessObject::READ_LATEST ),
'Dummy:NoDirectEditing should start with the `testing` content model'
);
$this->expectApiErrorCode( 'changecontentmodel-nodirectediting' );
$this->doApiRequestWithToken( [
'action' => 'changecontentmodel',
'summary' => __METHOD__,
'title' => 'Dummy:NoDirectEditing',
'model' => 'wikitext'
],
null,
$this->mockAnonAuthorityWithPermissions( [ 'edit', 'editcontentmodel' ] )
);
}
public function testCannotApplyTags() {
$this->getServiceContainer()->getChangeTagsStore()->defineTag( 'api edit content model tag' );
$this->expectApiErrorCode( 'tags-apply-no-permission' );
$this->doApiRequestWithToken( [
'action' => 'changecontentmodel',
'summary' => __METHOD__,
'title' => 'ExistingPage',
'model' => 'text',
'tags' => 'api edit content model tag',
],
null,
$this->mockAnonAuthorityWithoutPermissions( [ 'applychangetags' ] ) );
}
/**
* Test that it works
*/
public function testEverythingWorks() {
$this->disableAutoCreateTempUser();
$title = Title::makeTitle( NS_MAIN, 'ExistingPage' );
$performer = $this->mockAnonAuthorityWithPermissions(
[ 'edit', 'editcontentmodel', 'applychangetags' ]
);
$this->assertSame(
'wikitext',
$title->getContentModel( IDBAccessObject::READ_LATEST ),
'`ExistingPage` should be wikitext'
);
$this->getServiceContainer()->getChangeTagsStore()->defineTag( 'api edit content model tag' );
$data = $this->doApiRequestWithToken( [
'action' => 'changecontentmodel',
'summary' => __METHOD__,
'title' => 'ExistingPage',
'model' => 'text',
'tags' => 'api edit content model tag',
], null, $performer );
$this->assertSame(
'text',
$title->getContentModel( IDBAccessObject::READ_LATEST ),
'API can successfully change the content model'
);
$data = $data[0]['changecontentmodel'];
$this->assertSame( 'Success', $data['result'], 'API reports successful change' );
$firstLogId = (int)$data['logid'];
$firstRevId = (int)$data['revid'];
$this->assertGreaterThan( 0, $firstLogId, 'Plausible log id generated' );
$this->assertGreaterThan( 0, $firstRevId, 'Plausible rev id generated' );
$data = $this->doApiRequestWithToken( [
'action' => 'changecontentmodel',
// no 'summary', should be optional
'title' => 'ExistingPage',
'model' => 'wikitext',
'tags' => 'api edit content model tag',
], null, $performer );
$this->assertSame(
'wikitext',
$title->getContentModel( IDBAccessObject::READ_LATEST ),
'API can also change the content model back'
);
$data = $data[0]['changecontentmodel'];
$this->assertSame( 'Success', $data['result'], 'API reports successful change back' );
$this->assertGreaterThan(
$firstLogId,
(int)$data['logid'],
'Second log entry should come after the first'
);
$this->assertGreaterThan(
$firstRevId,
(int)$data['revid'],
'Second revision should come after the first'
);
$this->assertSame(
'4',
$this->getDb()->newSelectQueryBuilder()
->select( 'ctd_count' )
->from( 'change_tag_def' )
->where( [ 'ctd_name' => 'api edit content model tag' ] )
->caller( __METHOD__ )->fetchField(),
'There should be four uses of the `api edit content model tag` tag, '
. 'two for the two revisions and two for the two log entries'
);
}
}
|