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
|
<?php
use MediaWiki\Content\ContentModelChange;
use MediaWiki\Context\RequestContext;
use MediaWiki\Page\PageIdentity;
use MediaWiki\Page\WikiPageFactory;
use MediaWiki\Permissions\Authority;
use MediaWiki\Permissions\PermissionStatus;
use MediaWiki\Permissions\RateLimiter;
use MediaWiki\Status\Status;
use MediaWiki\Tests\Unit\MockServiceDependenciesTrait;
use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait;
use MediaWiki\Title\Title;
/**
* TODO convert to a pure unit test
*
* @group Database
* @covers \MediaWiki\Content\ContentModelChange
*
* @author DannyS712
* @method ContentModelChange newServiceInstance(string $serviceClass, array $parameterOverrides)
*/
class ContentModelChangeTest extends MediaWikiIntegrationTestCase {
use MockAuthorityTrait;
use MockServiceDependenciesTrait;
protected function setUp(): void {
parent::setUp();
$this->getExistingTestPage( 'ExistingPage' );
$this->mergeMwGlobalArrayValue( 'wgContentHandlers', [
'testing' => 'DummyContentHandlerForTesting',
] );
}
private function newContentModelChange(
Authority $performer,
WikiPage $page,
string $newModel
) {
return $this->getServiceContainer()
->getContentModelChangeFactory()
->newContentModelChange( $performer, $page, $newModel );
}
/**
* Test that the content model needs to change
*/
public function testChangeNeeded() {
$wikipage = $this->getExistingTestPage( 'ExistingPage' );
$this->assertSame(
'wikitext',
$wikipage->getTitle()->getContentModel(),
'`ExistingPage` should be wikitext'
);
$change = $this->newContentModelChange(
$this->mockRegisteredAuthorityWithPermissions( [ 'editcontentmodel' ] ),
$wikipage,
'wikitext'
);
$status = $change->doContentModelChange(
RequestContext::getMain(),
__METHOD__ . ' comment',
false
);
$this->assertStatusError( 'apierror-nochanges', $status );
}
/**
* Test that the content needs to be valid for the requested model
*/
public function testInvalidContent() {
$invalidJSON = 'Foo\nBar\nEaster egg\nT22281';
$wikipage = $this->getExistingTestPage( 'PageWithTextThatIsNotValidJSON' );
$wikipage->doUserEditContent(
ContentHandler::makeContent( $invalidJSON, $wikipage->getTitle() ),
$this->getTestSysop()->getUser(),
'EditSummaryForThisTest',
EDIT_UPDATE | EDIT_SUPPRESS_RC
);
$this->assertSame(
'wikitext',
$wikipage->getTitle()->getContentModel(),
'`PageWithTextThatIsNotValidJSON` should be wikitext at first'
);
$change = $this->newContentModelChange(
$this->mockRegisteredAuthorityWithPermissions( [ 'editcontentmodel' ] ),
$wikipage,
'json'
);
$status = $change->doContentModelChange(
RequestContext::getMain(),
__METHOD__ . ' comment',
false
);
$this->assertStatusError( 'invalid-json-data', $status );
}
/**
* 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 ) {
$wikipage = $this->getExistingTestPage( 'ExistingPage' );
$this->assertSame(
'wikitext',
$wikipage->getTitle()->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;
}
);
$change = $this->newContentModelChange(
$this->mockRegisteredAuthorityWithPermissions( [ 'editcontentmodel' ] ),
$wikipage,
'text'
);
$status = $change->doContentModelChange(
RequestContext::getMain(),
__METHOD__ . ' comment',
false
);
$this->assertStatusError( $expectedMessage, $status );
}
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() {
$wikipage = $this->getExistingTestPage( 'ExistingPage' );
$wikipage->doUserEditContent(
ContentHandler::makeContent( 'Text', $wikipage->getTitle() ),
$this->getTestSysop()->getUser(),
'Ensure a revision exists',
EDIT_UPDATE | EDIT_SUPPRESS_RC
);
$this->assertSame(
'wikitext',
$wikipage->getTitle()->getContentModel( IDBAccessObject::READ_LATEST ),
'`ExistingPage` should be wikitext'
);
$this->setTemporaryHook( 'ContentModelCanBeUsedOn',
static function ( $unused1, $unused2, &$ok ) {
$ok = false;
return false;
}
);
$change = $this->newContentModelChange(
$this->mockRegisteredAuthorityWithPermissions( [ 'editcontentmodel' ] ),
$wikipage,
'text'
);
$status = $change->doContentModelChange(
RequestContext::getMain(),
__METHOD__ . ' comment',
false
);
$this->assertStatusError( 'apierror-changecontentmodel-cannotbeused', $status );
}
/**
* Test that content handler must support direct editing
*/
public function testNoDirectEditing() {
$title = Title::newFromText( 'Dummy:NoDirectEditing' );
$wikipage = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title );
$dummyContent = $this->getServiceContainer()
->getContentHandlerFactory()
->getContentHandler( 'testing' )
->makeEmptyContent();
$wikipage->doUserEditContent(
$dummyContent,
$this->getTestSysop()->getUser(),
'EditSummaryForThisTest',
EDIT_NEW | EDIT_SUPPRESS_RC
);
$this->assertSame(
'testing',
$title->getContentModel( IDBAccessObject::READ_LATEST ),
'Dummy:NoDirectEditing should start with the `testing` content model'
);
$change = $this->newContentModelChange(
$this->mockRegisteredAuthorityWithPermissions( [ 'editcontentmodel' ] ),
$wikipage,
'text'
);
$status = $change->doContentModelChange(
RequestContext::getMain(),
__METHOD__ . ' comment',
false
);
$this->assertStatusError(
'apierror-changecontentmodel-nodirectediting',
$status
);
}
public function testCannotApplyTags() {
ChangeTags::defineTag( 'edit content model tag' );
$change = $this->newContentModelChange(
$this->mockRegisteredAuthorityWithoutPermissions( [ 'applychangetags' ] ),
$this->getExistingTestPage( 'ExistingPage' ),
'text'
);
$status = $change->setTags( [ 'edit content model tag' ] );
$this->assertStatusError(
'tags-apply-no-permission',
$status
);
}
public function testCheckPermissions() {
$wikipage = $this->getExistingTestPage( 'ExistingPage' );
$title = $wikipage->getTitle();
$currentContentModel = $title->getContentModel( IDBAccessObject::READ_LATEST );
$newContentModel = 'text';
$this->assertSame(
'wikitext',
$currentContentModel,
'`ExistingPage` should be wikitext'
);
$performer = $this->mockRegisteredAuthority( static function (
string $permission,
PageIdentity $page,
PermissionStatus $status
) use ( $currentContentModel, $newContentModel ) {
$title = Title::newFromPageIdentity( $page );
if ( $permission === 'editcontentmodel' && $title->hasContentModel( $currentContentModel ) ) {
$status->fatal( 'no edit old content model' );
return false;
}
if ( $permission === 'editcontentmodel' && $title->hasContentModel( $newContentModel ) ) {
$status->fatal( 'no edit new content model' );
return false;
}
if ( $permission === 'edit' && $title->hasContentModel( $currentContentModel ) ) {
$status->fatal( 'no edit at all old content model' );
return false;
}
if ( $permission === 'edit' && $title->hasContentModel( $newContentModel ) ) {
$status->fatal( 'no edit at all new content model' );
return false;
}
return true;
} );
$wpFactory = $this->createMock( WikiPageFactory::class );
$wpFactory->method( 'newFromTitle' )->willReturn( $wikipage );
$change = $this->newServiceInstance(
ContentModelChange::class,
[
'performer' => $performer,
'page' => $wikipage,
'newModel' => $newContentModel,
'wikiPageFactory' => $wpFactory,
]
);
foreach ( [ 'probablyCanChange', 'authorizeChange' ] as $method ) {
$status = $change->$method();
$this->assertArrayEquals(
[
[ 'no edit new content model' ],
[ 'no edit old content model' ],
[ 'no edit at all old content model' ],
[ 'no edit at all new content model' ],
],
$status->toLegacyErrorArray()
);
}
}
public function testCheckPermissionsThrottle() {
$user = $this->getTestUser()->getUser();
$limiter = $this->createNoOpMock( RateLimiter::class, [ 'limit', 'isLimitable' ] );
$limiter->method( 'isLimitable' )->willReturn( true );
$limiter->method( 'limit' )
->willReturnCallback( function ( $user, $action, $incr ) {
if ( $action === 'editcontentmodel' ) {
$this->assertSame( 1, $incr );
return true;
}
return false;
} );
$this->setService( 'RateLimiter', $limiter );
$change = $this->newContentModelChange(
$user,
$this->getNonexistingTestPage( 'NonExistingPage' ),
'text'
);
$status = $change->authorizeChange();
$this->assertFalse( $status->isOK() );
$this->assertTrue( $status->isRateLimitExceeded() );
}
}
|