| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Content\ContentHandler; |
| 4 |
use MediaWiki\Content\ContentModelChange; |
| 5 |
use MediaWiki\Context\RequestContext; |
| 6 |
use MediaWiki\Page\PageIdentity; |
| 7 |
use MediaWiki\Page\WikiPageFactory; |
| 8 |
use MediaWiki\Permissions\Authority; |
| 9 |
use MediaWiki\Permissions\PermissionStatus; |
| 10 |
use MediaWiki\Permissions\RateLimiter; |
| 11 |
use MediaWiki\Status\Status; |
| 12 |
use MediaWiki\Tests\Unit\MockServiceDependenciesTrait; |
| 13 |
use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait; |
| 14 |
use MediaWiki\Title\Title; |
| 15 |
use Wikimedia\Rdbms\IDBAccessObject; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
class ContentModelChangeTest extends MediaWikiIntegrationTestCase { |
| 27 |
use MockAuthorityTrait; |
| 28 |
use MockServiceDependenciesTrait; |
| 29 |
|
| 30 |
protected function setUp(): void { |
| 31 |
parent::setUp(); |
| 32 |
|
| 33 |
$this->getExistingTestPage( 'ExistingPage' ); |
| 34 |
$this->mergeMwGlobalArrayValue( 'wgContentHandlers', [ |
| 35 |
'testing' => 'DummyContentHandlerForTesting', |
| 36 |
] ); |
| 37 |
} |
| 38 |
|
| 39 |
private function newContentModelChange( |
| 40 |
Authority $performer, |
| 41 |
WikiPage $page, |
| 42 |
string $newModel |
| 43 |
) { |
| 44 |
return $this->getServiceContainer() |
| 45 |
->getContentModelChangeFactory() |
| 46 |
->newContentModelChange( $performer, $page, $newModel ); |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public function testChangeNeeded() { |
| 53 |
$wikipage = $this->getExistingTestPage( 'ExistingPage' ); |
| 54 |
$this->assertSame( |
| 55 |
'wikitext', |
| 56 |
$wikipage->getTitle()->getContentModel(), |
| 57 |
'`ExistingPage` should be wikitext' |
| 58 |
); |
| 59 |
|
| 60 |
$change = $this->newContentModelChange( |
| 61 |
$this->mockRegisteredAuthorityWithPermissions( [ 'editcontentmodel' ] ), |
| 62 |
$wikipage, |
| 63 |
'wikitext' |
| 64 |
); |
| 65 |
$status = $change->doContentModelChange( |
| 66 |
RequestContext::getMain(), |
| 67 |
__METHOD__ . ' comment', |
| 68 |
false |
| 69 |
); |
| 70 |
$this->assertStatusError( 'apierror-nochanges', $status ); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public function testInvalidContent() { |
| 77 |
$invalidJSON = 'Foo\nBar\nEaster egg\nT22281'; |
| 78 |
$wikipage = $this->getExistingTestPage( 'PageWithTextThatIsNotValidJSON' ); |
| 79 |
$wikipage->doUserEditContent( |
| 80 |
ContentHandler::makeContent( $invalidJSON, $wikipage->getTitle() ), |
| 81 |
$this->getTestSysop()->getUser(), |
| 82 |
'EditSummaryForThisTest', |
| 83 |
EDIT_UPDATE | EDIT_SUPPRESS_RC |
| 84 |
); |
| 85 |
$this->assertSame( |
| 86 |
'wikitext', |
| 87 |
$wikipage->getTitle()->getContentModel(), |
| 88 |
'`PageWithTextThatIsNotValidJSON` should be wikitext at first' |
| 89 |
); |
| 90 |
|
| 91 |
$change = $this->newContentModelChange( |
| 92 |
$this->mockRegisteredAuthorityWithPermissions( [ 'editcontentmodel' ] ), |
| 93 |
$wikipage, |
| 94 |
'json' |
| 95 |
); |
| 96 |
$status = $change->doContentModelChange( |
| 97 |
RequestContext::getMain(), |
| 98 |
__METHOD__ . ' comment', |
| 99 |
false |
| 100 |
); |
| 101 |
$this->assertStatusError( 'invalid-json-data', $status ); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public function testEditFilterMergedContent( $customMessage, $expectedMessage ) { |
| 112 |
$wikipage = $this->getExistingTestPage( 'ExistingPage' ); |
| 113 |
$this->assertSame( |
| 114 |
'wikitext', |
| 115 |
$wikipage->getTitle()->getContentModel( IDBAccessObject::READ_LATEST ), |
| 116 |
'`ExistingPage` should be wikitext' |
| 117 |
); |
| 118 |
|
| 119 |
$this->setTemporaryHook( 'EditFilterMergedContent', |
| 120 |
static function ( $unused1, $unused2, Status $status ) use ( $customMessage ) { |
| 121 |
if ( $customMessage !== false ) { |
| 122 |
$status->fatal( $customMessage ); |
| 123 |
} |
| 124 |
return false; |
| 125 |
} |
| 126 |
); |
| 127 |
|
| 128 |
$change = $this->newContentModelChange( |
| 129 |
$this->mockRegisteredAuthorityWithPermissions( [ 'editcontentmodel' ] ), |
| 130 |
$wikipage, |
| 131 |
'text' |
| 132 |
); |
| 133 |
$status = $change->doContentModelChange( |
| 134 |
RequestContext::getMain(), |
| 135 |
__METHOD__ . ' comment', |
| 136 |
false |
| 137 |
); |
| 138 |
$this->assertStatusError( $expectedMessage, $status ); |
| 139 |
} |
| 140 |
|
| 141 |
public static function provideTestEditFilterMergedContent() { |
| 142 |
return [ |
| 143 |
[ 'DannyS712 objects to this change!', 'DannyS712 objects to this change!' ], |
| 144 |
[ false, 'hookaborted' ] |
| 145 |
]; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
public function testContentModelCanBeUsedOn() { |
| 152 |
$wikipage = $this->getExistingTestPage( 'ExistingPage' ); |
| 153 |
$wikipage->doUserEditContent( |
| 154 |
ContentHandler::makeContent( 'Text', $wikipage->getTitle() ), |
| 155 |
$this->getTestSysop()->getUser(), |
| 156 |
'Ensure a revision exists', |
| 157 |
EDIT_UPDATE | EDIT_SUPPRESS_RC |
| 158 |
); |
| 159 |
$this->assertSame( |
| 160 |
'wikitext', |
| 161 |
$wikipage->getTitle()->getContentModel( IDBAccessObject::READ_LATEST ), |
| 162 |
'`ExistingPage` should be wikitext' |
| 163 |
); |
| 164 |
|
| 165 |
$this->setTemporaryHook( 'ContentModelCanBeUsedOn', |
| 166 |
static function ( $unused1, $unused2, &$ok ) { |
| 167 |
$ok = false; |
| 168 |
return false; |
| 169 |
} |
| 170 |
); |
| 171 |
|
| 172 |
$change = $this->newContentModelChange( |
| 173 |
$this->mockRegisteredAuthorityWithPermissions( [ 'editcontentmodel' ] ), |
| 174 |
$wikipage, |
| 175 |
'text' |
| 176 |
); |
| 177 |
$status = $change->doContentModelChange( |
| 178 |
RequestContext::getMain(), |
| 179 |
__METHOD__ . ' comment', |
| 180 |
false |
| 181 |
); |
| 182 |
$this->assertStatusError( 'apierror-changecontentmodel-cannotbeused', $status ); |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
public function testNoDirectEditing() { |
| 189 |
$title = Title::newFromText( 'Dummy:NoDirectEditing' ); |
| 190 |
$wikipage = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title ); |
| 191 |
|
| 192 |
$dummyContent = $this->getServiceContainer() |
| 193 |
->getContentHandlerFactory() |
| 194 |
->getContentHandler( 'testing' ) |
| 195 |
->makeEmptyContent(); |
| 196 |
|
| 197 |
$wikipage->doUserEditContent( |
| 198 |
$dummyContent, |
| 199 |
$this->getTestSysop()->getUser(), |
| 200 |
'EditSummaryForThisTest', |
| 201 |
EDIT_NEW | EDIT_SUPPRESS_RC |
| 202 |
); |
| 203 |
$this->assertSame( |
| 204 |
'testing', |
| 205 |
$title->getContentModel( IDBAccessObject::READ_LATEST ), |
| 206 |
'Dummy:NoDirectEditing should start with the `testing` content model' |
| 207 |
); |
| 208 |
|
| 209 |
$change = $this->newContentModelChange( |
| 210 |
$this->mockRegisteredAuthorityWithPermissions( [ 'editcontentmodel' ] ), |
| 211 |
$wikipage, |
| 212 |
'text' |
| 213 |
); |
| 214 |
$status = $change->doContentModelChange( |
| 215 |
RequestContext::getMain(), |
| 216 |
__METHOD__ . ' comment', |
| 217 |
false |
| 218 |
); |
| 219 |
$this->assertStatusError( |
| 220 |
'apierror-changecontentmodel-nodirectediting', |
| 221 |
$status |
| 222 |
); |
| 223 |
} |
| 224 |
|
| 225 |
public function testCannotApplyTags() { |
| 226 |
$this->getServiceContainer()->getChangeTagsStore()->defineTag( 'edit content model tag' ); |
| 227 |
|
| 228 |
$change = $this->newContentModelChange( |
| 229 |
$this->mockRegisteredAuthorityWithoutPermissions( [ 'applychangetags' ] ), |
| 230 |
$this->getExistingTestPage( 'ExistingPage' ), |
| 231 |
'text' |
| 232 |
); |
| 233 |
$status = $change->setTags( [ 'edit content model tag' ] ); |
| 234 |
$this->assertStatusError( |
| 235 |
'tags-apply-no-permission', |
| 236 |
$status |
| 237 |
); |
| 238 |
} |
| 239 |
|
| 240 |
public function testCheckPermissions() { |
| 241 |
$wikipage = $this->getExistingTestPage( 'ExistingPage' ); |
| 242 |
$title = $wikipage->getTitle(); |
| 243 |
$currentContentModel = $title->getContentModel( IDBAccessObject::READ_LATEST ); |
| 244 |
$newContentModel = 'text'; |
| 245 |
|
| 246 |
$this->assertSame( |
| 247 |
'wikitext', |
| 248 |
$currentContentModel, |
| 249 |
'`ExistingPage` should be wikitext' |
| 250 |
); |
| 251 |
|
| 252 |
$performer = $this->mockRegisteredAuthority( static function ( |
| 253 |
string $permission, |
| 254 |
PageIdentity $page, |
| 255 |
PermissionStatus $status |
| 256 |
) use ( $currentContentModel, $newContentModel ) { |
| 257 |
$title = Title::newFromPageIdentity( $page ); |
| 258 |
if ( $permission === 'editcontentmodel' && $title->hasContentModel( $currentContentModel ) ) { |
| 259 |
$status->fatal( 'no edit old content model' ); |
| 260 |
return false; |
| 261 |
} |
| 262 |
if ( $permission === 'editcontentmodel' && $title->hasContentModel( $newContentModel ) ) { |
| 263 |
$status->fatal( 'no edit new content model' ); |
| 264 |
return false; |
| 265 |
} |
| 266 |
if ( $permission === 'edit' && $title->hasContentModel( $currentContentModel ) ) { |
| 267 |
$status->fatal( 'no edit at all old content model' ); |
| 268 |
return false; |
| 269 |
} |
| 270 |
if ( $permission === 'edit' && $title->hasContentModel( $newContentModel ) ) { |
| 271 |
$status->fatal( 'no edit at all new content model' ); |
| 272 |
return false; |
| 273 |
} |
| 274 |
return true; |
| 275 |
} ); |
| 276 |
|
| 277 |
$wpFactory = $this->createMock( WikiPageFactory::class ); |
| 278 |
$wpFactory->method( 'newFromTitle' )->willReturn( $wikipage ); |
| 279 |
$change = $this->newServiceInstance( |
| 280 |
ContentModelChange::class, |
| 281 |
[ |
| 282 |
'performer' => $performer, |
| 283 |
'page' => $wikipage, |
| 284 |
'newModel' => $newContentModel, |
| 285 |
'wikiPageFactory' => $wpFactory, |
| 286 |
] |
| 287 |
); |
| 288 |
|
| 289 |
foreach ( [ 'probablyCanChange', 'authorizeChange' ] as $method ) { |
| 290 |
$status = $change->$method(); |
| 291 |
$this->assertArrayEquals( |
| 292 |
[ |
| 293 |
[ 'no edit new content model' ], |
| 294 |
[ 'no edit old content model' ], |
| 295 |
[ 'no edit at all old content model' ], |
| 296 |
[ 'no edit at all new content model' ], |
| 297 |
], |
| 298 |
$status->toLegacyErrorArray() |
| 299 |
); |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
public function testCheckPermissionsThrottle() { |
| 304 |
$user = $this->getTestUser()->getUser(); |
| 305 |
|
| 306 |
$limiter = $this->createNoOpMock( RateLimiter::class, [ 'limit', 'isLimitable' ] ); |
| 307 |
$limiter->method( 'isLimitable' )->willReturn( true ); |
| 308 |
$limiter->method( 'limit' ) |
| 309 |
->willReturnCallback( function ( $user, $action, $incr ) { |
| 310 |
if ( $action === 'editcontentmodel' ) { |
| 311 |
$this->assertSame( 1, $incr ); |
| 312 |
return true; |
| 313 |
} |
| 314 |
return false; |
| 315 |
} ); |
| 316 |
|
| 317 |
$this->setService( 'RateLimiter', $limiter ); |
| 318 |
|
| 319 |
$change = $this->newContentModelChange( |
| 320 |
$user, |
| 321 |
$this->getNonexistingTestPage( 'NonExistingPage' ), |
| 322 |
'text' |
| 323 |
); |
| 324 |
|
| 325 |
$status = $change->authorizeChange(); |
| 326 |
$this->assertFalse( $status->isOK() ); |
| 327 |
$this->assertTrue( $status->isRateLimitExceeded() ); |
| 328 |
} |
| 329 |
|
| 330 |
} |