| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Content\ContentHandler; |
| 4 |
use MediaWiki\Content\TextContent; |
| 5 |
use MediaWiki\Context\RequestContext; |
| 6 |
use MediaWiki\EditPage\EditPage; |
| 7 |
use MediaWiki\EditPage\SpamChecker; |
| 8 |
use MediaWiki\MainConfigNames; |
| 9 |
use MediaWiki\Permissions\PermissionManager; |
| 10 |
use MediaWiki\Request\FauxRequest; |
| 11 |
use MediaWiki\Tests\User\TempUser\TempUserTestTrait; |
| 12 |
use MediaWiki\Title\Title; |
| 13 |
use MediaWiki\User\User; |
| 14 |
use Wikimedia\Rdbms\ReadOnlyMode; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
class EditPageConstraintsTest extends MediaWikiLangTestCase { |
| 28 |
|
| 29 |
use TempUserTestTrait; |
| 30 |
|
| 31 |
protected function setUp(): void { |
| 32 |
parent::setUp(); |
| 33 |
|
| 34 |
$contLang = $this->getServiceContainer()->getContentLanguage(); |
| 35 |
$this->overrideConfigValues( [ |
| 36 |
MainConfigNames::ExtraNamespaces => [ |
| 37 |
12312 => 'Dummy', |
| 38 |
12313 => 'Dummy_talk', |
| 39 |
], |
| 40 |
MainConfigNames::NamespaceContentModels => [ 12312 => 'testing' ], |
| 41 |
MainConfigNames::LanguageCode => $contLang->getCode(), |
| 42 |
] ); |
| 43 |
$this->mergeMwGlobalArrayValue( |
| 44 |
'wgContentHandlers', |
| 45 |
[ 'testing' => 'DummyContentHandlerForTesting' ] |
| 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 |
protected function assertEdit( |
| 74 |
$title, |
| 75 |
$baseText, |
| 76 |
?User $user, |
| 77 |
array $edit, |
| 78 |
$expectedCode, |
| 79 |
$message |
| 80 |
) { |
| 81 |
if ( is_string( $title ) ) { |
| 82 |
$ns = $this->getDefaultWikitextNS(); |
| 83 |
$title = Title::newFromText( $title, $ns ); |
| 84 |
} |
| 85 |
$this->assertNotNull( $title ); |
| 86 |
|
| 87 |
$wikiPageFactory = $this->getServiceContainer()->getWikiPageFactory(); |
| 88 |
$page = $wikiPageFactory->newFromTitle( $title ); |
| 89 |
|
| 90 |
$user ??= $this->getTestUser()->getUser(); |
| 91 |
|
| 92 |
if ( $baseText !== null ) { |
| 93 |
$content = ContentHandler::makeContent( $baseText, $title ); |
| 94 |
$page->doUserEditContent( $content, $user, "base text for test" ); |
| 95 |
|
| 96 |
|
| 97 |
$dbw = $this->getDb(); |
| 98 |
$dbw->newUpdateQueryBuilder() |
| 99 |
->update( 'revision' ) |
| 100 |
->set( [ 'rev_timestamp' => $dbw->timestamp( '20120101000000' ) ] ) |
| 101 |
->where( [ 'rev_id' => $page->getLatest() ] ) |
| 102 |
->execute(); |
| 103 |
$page->clear(); |
| 104 |
|
| 105 |
$content = $page->getContent(); |
| 106 |
$this->assertInstanceOf( TextContent::class, $content ); |
| 107 |
$currentText = $content->getText(); |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
$this->assertEquals( |
| 112 |
rtrim( $baseText ), |
| 113 |
rtrim( $currentText ), |
| 114 |
'page should have the text specified' |
| 115 |
); |
| 116 |
} |
| 117 |
|
| 118 |
if ( !isset( $edit['wpEditToken'] ) ) { |
| 119 |
$edit['wpEditToken'] = $user->getEditToken(); |
| 120 |
} |
| 121 |
|
| 122 |
if ( !isset( $edit['wpEdittime'] ) && !isset( $edit['editRevId'] ) ) { |
| 123 |
$edit['wpEdittime'] = $page->exists() ? $page->getTimestamp() : ''; |
| 124 |
} |
| 125 |
|
| 126 |
if ( !isset( $edit['wpStarttime'] ) ) { |
| 127 |
$edit['wpStarttime'] = wfTimestampNow(); |
| 128 |
} |
| 129 |
|
| 130 |
if ( !isset( $edit['wpUnicodeCheck'] ) ) { |
| 131 |
$edit['wpUnicodeCheck'] = EditPage::UNICODE_CHECK; |
| 132 |
} |
| 133 |
|
| 134 |
$req = new FauxRequest( $edit, true ); |
| 135 |
|
| 136 |
$context = new RequestContext(); |
| 137 |
$context->setRequest( $req ); |
| 138 |
$context->setTitle( $title ); |
| 139 |
$context->setUser( $user ); |
| 140 |
$article = new Article( $title ); |
| 141 |
$article->setContext( $context ); |
| 142 |
$ep = new EditPage( $article ); |
| 143 |
$ep->setContextTitle( $title ); |
| 144 |
$ep->importFormData( $req ); |
| 145 |
|
| 146 |
|
| 147 |
$status = $ep->attemptSave( $result ); |
| 148 |
|
| 149 |
|
| 150 |
$this->assertSame( |
| 151 |
$expectedCode, |
| 152 |
$status->value, |
| 153 |
"Expected result code mismatch. $message" |
| 154 |
); |
| 155 |
|
| 156 |
return $wikiPageFactory->newFromTitle( $title ); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
public function testAccidentalRecreationConstraint() { |
| 161 |
|
| 162 |
$this->getExistingTestPage( 'AccidentalRecreationConstraintPage' ); |
| 163 |
|
| 164 |
|
| 165 |
$page = $this->getNonexistingTestPage( 'AccidentalRecreationConstraintPage' ); |
| 166 |
$title = $page->getTitle(); |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
$dbw = $this->getDb(); |
| 172 |
$dbw->newUpdateQueryBuilder() |
| 173 |
->update( 'logging' ) |
| 174 |
->set( [ 'log_timestamp' => $dbw->timestamp( '20200101000000' ) ] ) |
| 175 |
->where( [ |
| 176 |
'log_namespace' => $title->getNamespace(), |
| 177 |
'log_title' => $title->getDBkey(), |
| 178 |
'log_type' => 'delete', |
| 179 |
'log_action' => 'delete', |
| 180 |
] ) |
| 181 |
->caller( __METHOD__ )->execute(); |
| 182 |
|
| 183 |
$user = $this->getTestUser()->getUser(); |
| 184 |
|
| 185 |
$permissionManager = $this->getServiceContainer()->getPermissionManager(); |
| 186 |
|
| 187 |
$permissionManager->overrideUserRightsForTesting( $user, [ 'edit' ] ); |
| 188 |
|
| 189 |
|
| 190 |
$edit = [ |
| 191 |
'wpTextbox1' => 'New content', |
| 192 |
'wpStarttime' => '20190101000000' |
| 193 |
]; |
| 194 |
$this->assertEdit( |
| 195 |
$title, |
| 196 |
null, |
| 197 |
$user, |
| 198 |
$edit, |
| 199 |
EditPage::AS_ARTICLE_WAS_DELETED, |
| 200 |
'expected AS_ARTICLE_WAS_DELETED update' |
| 201 |
); |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
public function testExistingSectionEditConstraint() { |
| 206 |
|
| 207 |
$this->mergeMwGlobalArrayValue( |
| 208 |
'wgDefaultUserOptions', |
| 209 |
[ 'forceeditsummary' => 1 ] |
| 210 |
); |
| 211 |
|
| 212 |
$page = $this->getExistingTestPage( 'ExistingSectionEditConstraint page does exist' ); |
| 213 |
$title = $page->getTitle(); |
| 214 |
|
| 215 |
$user = $this->getTestUser()->getUser(); |
| 216 |
|
| 217 |
$permissionManager = $this->getServiceContainer()->getPermissionManager(); |
| 218 |
|
| 219 |
$permissionManager->overrideUserRightsForTesting( $user, [ 'edit' ] ); |
| 220 |
|
| 221 |
$edit = [ |
| 222 |
'wpTextbox1' => 'New content, different from base content', |
| 223 |
'wpSummary' => 'SameAsAutoSummary', |
| 224 |
'wpAutoSummary' => md5( 'SameAsAutoSummary' ) |
| 225 |
]; |
| 226 |
$this->assertEdit( |
| 227 |
$title, |
| 228 |
'Base content, different from new content', |
| 229 |
$user, |
| 230 |
$edit, |
| 231 |
EditPage::AS_SUMMARY_NEEDED, |
| 232 |
'expected AS_SUMMARY_NEEDED update' |
| 233 |
); |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
public function testChangeTagsConstraint() { |
| 238 |
|
| 239 |
$this->mergeMwGlobalArrayValue( |
| 240 |
'wgRevokePermissions', |
| 241 |
[ 'user' => [ 'applychangetags' => true ] ] |
| 242 |
); |
| 243 |
$edit = [ |
| 244 |
'wpTextbox1' => 'Text', |
| 245 |
'wpChangeTags' => 'tag-name' |
| 246 |
]; |
| 247 |
$this->assertEdit( |
| 248 |
'EditPageTest_changeTagsConstraint', |
| 249 |
null, |
| 250 |
null, |
| 251 |
$edit, |
| 252 |
EditPage::AS_CHANGE_TAG_ERROR, |
| 253 |
'expected AS_CHANGE_TAG_ERROR update' |
| 254 |
); |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
public function testContentModelChangeConstraint() { |
| 259 |
$user = $this->getTestUser()->getUser(); |
| 260 |
$permissionManager = $this->getServiceContainer()->getPermissionManager(); |
| 261 |
|
| 262 |
$permissionManager->overrideUserRightsForTesting( $user, [ 'edit' ] ); |
| 263 |
|
| 264 |
$edit = [ |
| 265 |
'wpTextbox1' => 'New text goes here', |
| 266 |
'wpSummary' => 'Summary', |
| 267 |
'model' => CONTENT_MODEL_TEXT, |
| 268 |
'format' => CONTENT_FORMAT_TEXT, |
| 269 |
]; |
| 270 |
|
| 271 |
$title = Title::makeTitle( NS_MAIN, 'Example' ); |
| 272 |
$this->assertSame( |
| 273 |
CONTENT_MODEL_WIKITEXT, |
| 274 |
$title->getContentModel(), |
| 275 |
'title should start as wikitext content model' |
| 276 |
); |
| 277 |
|
| 278 |
$this->assertEdit( |
| 279 |
$title, |
| 280 |
'Base text', |
| 281 |
$user, |
| 282 |
$edit, |
| 283 |
EditPage::AS_NO_CHANGE_CONTENT_MODEL, |
| 284 |
'expected AS_NO_CHANGE_CONTENT_MODEL update' |
| 285 |
); |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
public function testCreationPermissionConstraint() { |
| 290 |
$page = $this->getNonexistingTestPage( 'CreationPermissionConstraint page does not exist' ); |
| 291 |
$title = $page->getTitle(); |
| 292 |
|
| 293 |
$user = $this->getTestUser()->getUser(); |
| 294 |
$permissionManager = $this->getServiceContainer()->getPermissionManager(); |
| 295 |
|
| 296 |
$permissionManager->overrideUserRightsForTesting( $user, [ 'edit' ] ); |
| 297 |
|
| 298 |
$edit = [ |
| 299 |
'wpTextbox1' => 'Page content', |
| 300 |
'wpSummary' => 'Summary' |
| 301 |
]; |
| 302 |
$this->assertEdit( |
| 303 |
$title, |
| 304 |
null, |
| 305 |
$user, |
| 306 |
$edit, |
| 307 |
EditPage::AS_NO_CREATE_PERMISSION, |
| 308 |
'expected AS_NO_CREATE_PERMISSION creation' |
| 309 |
); |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
public function testDefaultTextConstraint() { |
| 314 |
$page = $this->getNonexistingTestPage( 'DefaultTextConstraint page does not exist' ); |
| 315 |
$title = $page->getTitle(); |
| 316 |
|
| 317 |
$user = $this->getTestUser()->getUser(); |
| 318 |
$permissionManager = $this->getServiceContainer()->getPermissionManager(); |
| 319 |
|
| 320 |
$permissionManager->overrideUserRightsForTesting( $user, [ 'edit', 'createpage' ] ); |
| 321 |
|
| 322 |
$edit = [ |
| 323 |
'wpTextbox1' => '', |
| 324 |
'wpSummary' => 'Summary' |
| 325 |
]; |
| 326 |
$this->assertEdit( |
| 327 |
$title, |
| 328 |
null, |
| 329 |
$user, |
| 330 |
$edit, |
| 331 |
EditPage::AS_BLANK_ARTICLE, |
| 332 |
'expected AS_BLANK_ARTICLE creation' |
| 333 |
); |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
public function testEditFilterMergedContentHookConstraint( |
| 346 |
bool $hookReturn, |
| 347 |
$statusValue, |
| 348 |
bool $statusFatal, |
| 349 |
int $expectedFailure, |
| 350 |
string $expectedFailureStr |
| 351 |
) { |
| 352 |
$this->setTemporaryHook( |
| 353 |
'EditFilterMergedContent', |
| 354 |
static function ( $context, $content, $status, $summary, $user, $minorEdit ) |
| 355 |
use ( $hookReturn, $statusValue, $statusFatal ) |
| 356 |
{ |
| 357 |
if ( $statusValue !== null ) { |
| 358 |
$status->value = $statusValue; |
| 359 |
} |
| 360 |
if ( $statusFatal ) { |
| 361 |
$status->fatal( 'SomeErrorInTheHook' ); |
| 362 |
} |
| 363 |
return $hookReturn; |
| 364 |
} |
| 365 |
); |
| 366 |
|
| 367 |
$user = $this->getTestUser()->getUser(); |
| 368 |
$permissionManager = $this->getServiceContainer()->getPermissionManager(); |
| 369 |
|
| 370 |
$permissionManager->overrideUserRightsForTesting( $user, [ 'edit', 'createpage' ] ); |
| 371 |
|
| 372 |
$edit = [ |
| 373 |
'wpTextbox1' => 'Text', |
| 374 |
'wpSummary' => 'Summary' |
| 375 |
]; |
| 376 |
$this->assertEdit( |
| 377 |
'EditPageTest_testEditFilterMergedContentHookConstraint', |
| 378 |
null, |
| 379 |
$user, |
| 380 |
$edit, |
| 381 |
$expectedFailure, |
| 382 |
"expected $expectedFailureStr creation" |
| 383 |
); |
| 384 |
} |
| 385 |
|
| 386 |
public static function provideTestEditFilterMergedContentHookConstraint() { |
| 387 |
yield 'Hook returns false, status is good, no value set' => [ |
| 388 |
false, null, false, EditPage::AS_HOOK_ERROR_EXPECTED, 'AS_HOOK_ERROR_EXPECTED' |
| 389 |
]; |
| 390 |
yield 'Hook returns false, status is good, value set' => [ |
| 391 |
false, 1234567, false, 1234567, 'custom value 1234567' |
| 392 |
]; |
| 393 |
yield 'Hook returns false, status is not good' => [ |
| 394 |
false, null, true, EditPage::AS_HOOK_ERROR_EXPECTED, 'AS_HOOK_ERROR_EXPECTED' |
| 395 |
]; |
| 396 |
yield 'Hook returns true, status is not ok' => [ |
| 397 |
true, null, true, EditPage::AS_HOOK_ERROR_EXPECTED, 'AS_HOOK_ERROR_EXPECTED' |
| 398 |
]; |
| 399 |
} |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
public function testEditRightConstraint( $anon, $expectedErrorCode ) { |
| 408 |
if ( $anon ) { |
| 409 |
$this->disableAutoCreateTempUser(); |
| 410 |
$user = $this->getServiceContainer()->getUserFactory()->newAnonymous( '127.0.0.1' ); |
| 411 |
} else { |
| 412 |
$user = $this->getTestUser()->getUser(); |
| 413 |
} |
| 414 |
$permissionManager = $this->getServiceContainer()->getPermissionManager(); |
| 415 |
$permissionManager->overrideUserRightsForTesting( $user, [] ); |
| 416 |
|
| 417 |
$edit = [ |
| 418 |
'wpTextbox1' => 'Page content', |
| 419 |
'wpSummary' => 'Summary' |
| 420 |
]; |
| 421 |
$this->assertEdit( |
| 422 |
'EditPageTest_noEditRight', |
| 423 |
'base text', |
| 424 |
$user, |
| 425 |
$edit, |
| 426 |
$expectedErrorCode, |
| 427 |
'expected AS_READ_ONLY_PAGE_* update' |
| 428 |
); |
| 429 |
} |
| 430 |
|
| 431 |
public static function provideTestEditRightConstraint() { |
| 432 |
yield 'Anonymous user' => [ true, EditPage::AS_READ_ONLY_PAGE_ANON ]; |
| 433 |
yield 'Registered user' => [ false, EditPage::AS_READ_ONLY_PAGE_LOGGED ]; |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
public function testImageRedirectConstraint( $anon, $expectedErrorCode ) { |
| 443 |
if ( $anon ) { |
| 444 |
$this->disableAutoCreateTempUser(); |
| 445 |
$user = $this->getServiceContainer()->getUserFactory()->newAnonymous( '127.0.0.1' ); |
| 446 |
} else { |
| 447 |
$user = $this->getTestUser()->getUser(); |
| 448 |
} |
| 449 |
|
| 450 |
$permissionManager = $this->getServiceContainer()->getPermissionManager(); |
| 451 |
|
| 452 |
$permissionManager->overrideUserRightsForTesting( $user, [ 'edit' ] ); |
| 453 |
|
| 454 |
$edit = [ |
| 455 |
'wpTextbox1' => '#REDIRECT [[File:Example other file.jpg]]', |
| 456 |
'wpSummary' => 'Summary' |
| 457 |
]; |
| 458 |
|
| 459 |
$title = Title::makeTitle( NS_FILE, 'Example.jpg' ); |
| 460 |
$this->assertEdit( |
| 461 |
$title, |
| 462 |
null, |
| 463 |
$user, |
| 464 |
$edit, |
| 465 |
$expectedErrorCode, |
| 466 |
'expected AS_IMAGE_REDIRECT_* update' |
| 467 |
); |
| 468 |
} |
| 469 |
|
| 470 |
public static function provideTestImageRedirectConstraint() { |
| 471 |
yield 'Anonymous user' => [ true, EditPage::AS_IMAGE_REDIRECT_ANON ]; |
| 472 |
yield 'Registered user' => [ false, EditPage::AS_IMAGE_REDIRECT_LOGGED ]; |
| 473 |
} |
| 474 |
|
| 475 |
|
| 476 |
public function testMissingCommentConstraint() { |
| 477 |
$page = $this->getExistingTestPage( 'MissingCommentConstraint page does exist' ); |
| 478 |
$title = $page->getTitle(); |
| 479 |
|
| 480 |
$user = $this->getTestUser()->getUser(); |
| 481 |
|
| 482 |
$permissionManager = $this->getServiceContainer()->getPermissionManager(); |
| 483 |
|
| 484 |
$permissionManager->overrideUserRightsForTesting( $user, [ 'edit' ] ); |
| 485 |
|
| 486 |
$edit = [ |
| 487 |
'wpTextbox1' => '', |
| 488 |
'wpSection' => 'new', |
| 489 |
'wpSummary' => 'Summary' |
| 490 |
]; |
| 491 |
$this->assertEdit( |
| 492 |
$title, |
| 493 |
null, |
| 494 |
$user, |
| 495 |
$edit, |
| 496 |
EditPage::AS_TEXTBOX_EMPTY, |
| 497 |
'expected AS_TEXTBOX_EMPTY update' |
| 498 |
); |
| 499 |
} |
| 500 |
|
| 501 |
|
| 502 |
public function testNewSectionMissingSubjectConstraint() { |
| 503 |
|
| 504 |
$this->mergeMwGlobalArrayValue( |
| 505 |
'wgDefaultUserOptions', |
| 506 |
[ 'forceeditsummary' => 1 ] |
| 507 |
); |
| 508 |
|
| 509 |
$page = $this->getExistingTestPage( 'NewSectionMissingSubjectConstraint page does exist' ); |
| 510 |
$title = $page->getTitle(); |
| 511 |
|
| 512 |
$user = $this->getTestUser()->getUser(); |
| 513 |
|
| 514 |
$permissionManager = $this->getServiceContainer()->getPermissionManager(); |
| 515 |
|
| 516 |
$permissionManager->overrideUserRightsForTesting( $user, [ 'edit' ] ); |
| 517 |
|
| 518 |
$edit = [ |
| 519 |
'wpTextbox1' => 'Comment', |
| 520 |
'wpSection' => 'new', |
| 521 |
'wpSummary' => '' |
| 522 |
]; |
| 523 |
$this->assertEdit( |
| 524 |
$title, |
| 525 |
null, |
| 526 |
$user, |
| 527 |
$edit, |
| 528 |
EditPage::AS_SUMMARY_NEEDED, |
| 529 |
'expected AS_SUMMARY_NEEDED update' |
| 530 |
); |
| 531 |
} |
| 532 |
|
| 533 |
|
| 534 |
public function testPageSizeConstraintBeforeMerge() { |
| 535 |
|
| 536 |
$this->overrideConfigValue( MainConfigNames::MaxArticleSize, 1 ); |
| 537 |
|
| 538 |
$edit = [ |
| 539 |
'wpTextbox1' => str_repeat( 'text', 1000 ) |
| 540 |
]; |
| 541 |
$this->assertEdit( |
| 542 |
'EditPageTest_pageSizeConstraintBeforeMerge', |
| 543 |
null, |
| 544 |
null, |
| 545 |
$edit, |
| 546 |
EditPage::AS_CONTENT_TOO_BIG, |
| 547 |
'expected AS_CONTENT_TOO_BIG update' |
| 548 |
); |
| 549 |
} |
| 550 |
|
| 551 |
|
| 552 |
public function testPageSizeConstraintAfterMerge() { |
| 553 |
|
| 554 |
$this->overrideConfigValue( MainConfigNames::MaxArticleSize, 1 ); |
| 555 |
|
| 556 |
$edit = [ |
| 557 |
'wpSection' => 'new', |
| 558 |
'wpTextbox1' => str_repeat( 'b', 600 ) |
| 559 |
]; |
| 560 |
$this->assertEdit( |
| 561 |
'EditPageTest_pageSizeConstraintAfterMerge', |
| 562 |
str_repeat( 'a', 600 ), |
| 563 |
null, |
| 564 |
$edit, |
| 565 |
EditPage::AS_MAX_ARTICLE_SIZE_EXCEEDED, |
| 566 |
'expected AS_MAX_ARTICLE_SIZE_EXCEEDED update' |
| 567 |
); |
| 568 |
} |
| 569 |
|
| 570 |
|
| 571 |
public function testReadOnlyConstraint() { |
| 572 |
$readOnlyMode = $this->createMock( ReadOnlyMode::class ); |
| 573 |
$readOnlyMode->method( 'isReadOnly' )->willReturn( true ); |
| 574 |
$this->setService( 'ReadOnlyMode', $readOnlyMode ); |
| 575 |
|
| 576 |
$edit = [ |
| 577 |
'wpTextbox1' => 'Text goes here' |
| 578 |
]; |
| 579 |
$this->assertEdit( |
| 580 |
'EditPageTest_readOnlyConstraint', |
| 581 |
null, |
| 582 |
null, |
| 583 |
$edit, |
| 584 |
EditPage::AS_READ_ONLY_PAGE, |
| 585 |
'expected AS_READ_ONLY_PAGE update' |
| 586 |
); |
| 587 |
} |
| 588 |
|
| 589 |
|
| 590 |
public function testSelfRedirectConstraint() { |
| 591 |
|
| 592 |
$page = $this->getNonexistingTestPage( 'SelfRedirectConstraint page does not exist' ); |
| 593 |
$title = $page->getTitle(); |
| 594 |
|
| 595 |
$edit = [ |
| 596 |
'wpTextbox1' => '#REDIRECT [[SelfRedirectConstraint page does not exist]]', |
| 597 |
'wpSummary' => 'Redirect to self' |
| 598 |
]; |
| 599 |
$this->assertEdit( |
| 600 |
$title, |
| 601 |
'zero', |
| 602 |
null, |
| 603 |
$edit, |
| 604 |
EditPage::AS_SELF_REDIRECT, |
| 605 |
'expected AS_SELF_REDIRECT update' |
| 606 |
); |
| 607 |
} |
| 608 |
|
| 609 |
|
| 610 |
public function testSimpleAntiSpamConstraint() { |
| 611 |
$edit = [ |
| 612 |
'wpTextbox1' => 'one', |
| 613 |
'wpSummary' => 'first update', |
| 614 |
'wpAntispam' => 'tatata' |
| 615 |
]; |
| 616 |
$this->assertEdit( |
| 617 |
'EditPageTest_testUpdatePageSpamError', |
| 618 |
'zero', |
| 619 |
null, |
| 620 |
$edit, |
| 621 |
EditPage::AS_SPAM_ERROR, |
| 622 |
'expected AS_SPAM_ERROR update' |
| 623 |
); |
| 624 |
} |
| 625 |
|
| 626 |
|
| 627 |
public function testSpamRegexConstraint() { |
| 628 |
$spamChecker = $this->createMock( SpamChecker::class ); |
| 629 |
$spamChecker->method( 'checkContent' ) |
| 630 |
->willReturnArgument( 0 ); |
| 631 |
$spamChecker->method( 'checkSummary' ) |
| 632 |
->willReturnArgument( 0 ); |
| 633 |
$this->setService( 'SpamChecker', $spamChecker ); |
| 634 |
|
| 635 |
$edit = [ |
| 636 |
'wpTextbox1' => 'two', |
| 637 |
'wpSummary' => 'spam summary' |
| 638 |
]; |
| 639 |
$this->assertEdit( |
| 640 |
'EditPageTest_testUpdatePageSpamRegexError', |
| 641 |
'zero', |
| 642 |
null, |
| 643 |
$edit, |
| 644 |
EditPage::AS_SPAM_ERROR, |
| 645 |
'expected AS_SPAM_ERROR update' |
| 646 |
); |
| 647 |
} |
| 648 |
|
| 649 |
|
| 650 |
public function testUserBlockConstraint() { |
| 651 |
$user = $this->createMock( User::class ); |
| 652 |
$user->method( 'getName' )->willReturn( 'NameGoesHere' ); |
| 653 |
$user->method( 'getId' )->willReturn( 12345 ); |
| 654 |
|
| 655 |
$permissionManager = $this->createMock( PermissionManager::class ); |
| 656 |
|
| 657 |
$permissionManager->method( 'userHasRight' )->willReturn( true ); |
| 658 |
$permissionManager->method( 'userCan' )->willReturn( true ); |
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
$permissionManager->method( 'isBlockedFrom' )->willReturn( true ); |
| 663 |
|
| 664 |
$this->setService( 'PermissionManager', $permissionManager ); |
| 665 |
|
| 666 |
$edit = [ |
| 667 |
'wpTextbox1' => 'Page content', |
| 668 |
'wpSummary' => 'Summary' |
| 669 |
]; |
| 670 |
$this->assertEdit( |
| 671 |
'EditPageTest_userBlocked', |
| 672 |
'base text', |
| 673 |
null, |
| 674 |
$edit, |
| 675 |
EditPage::AS_BLOCKED_PAGE_FOR_USER, |
| 676 |
'expected AS_BLOCKED_PAGE_FOR_USER update' |
| 677 |
); |
| 678 |
} |
| 679 |
|
| 680 |
|
| 681 |
public function testUserRateLimitConstraint() { |
| 682 |
$this->setTemporaryHook( |
| 683 |
'PingLimiter', |
| 684 |
static function ( $user, $action, &$result, $incrBy ) { |
| 685 |
|
| 686 |
$result = true; |
| 687 |
return false; |
| 688 |
} |
| 689 |
); |
| 690 |
|
| 691 |
$edit = [ |
| 692 |
'wpTextbox1' => 'Text goes here' |
| 693 |
]; |
| 694 |
$this->assertEdit( |
| 695 |
'EditPageTest_userRateLimitConstraint', |
| 696 |
null, |
| 697 |
null, |
| 698 |
$edit, |
| 699 |
EditPage::AS_RATE_LIMITED, |
| 700 |
'expected AS_RATE_LIMITED update' |
| 701 |
); |
| 702 |
} |
| 703 |
|
| 704 |
} |