| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Api; |
| 4 |
|
| 5 |
use MediaWiki\Api\ApiUsageException; |
| 6 |
use MediaWiki\Block\DatabaseBlock; |
| 7 |
use MediaWiki\CommentStore\CommentStoreComment; |
| 8 |
use MediaWiki\Content\JavaScriptContent; |
| 9 |
use MediaWiki\Content\WikitextContent; |
| 10 |
use MediaWiki\Context\RequestContext; |
| 11 |
use MediaWiki\MainConfigNames; |
| 12 |
use MediaWiki\Revision\RevisionRecord; |
| 13 |
use MediaWiki\Status\Status; |
| 14 |
use MediaWiki\Tests\User\TempUser\TempUserTestTrait; |
| 15 |
use MediaWiki\Title\Title; |
| 16 |
use MediaWiki\Title\TitleValue; |
| 17 |
use MediaWiki\User\User; |
| 18 |
use MediaWiki\Utils\MWTimestamp; |
| 19 |
use RevisionDeleter; |
| 20 |
use Wikimedia\Rdbms\IDBAccessObject; |
| 21 |
use WikiPage; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
class ApiEditPageTest extends ApiTestCase { |
| 35 |
|
| 36 |
use TempUserTestTrait; |
| 37 |
|
| 38 |
protected function setUp(): void { |
| 39 |
parent::setUp(); |
| 40 |
|
| 41 |
$this->overrideConfigValues( [ |
| 42 |
MainConfigNames::ExtraNamespaces => [ |
| 43 |
12312 => 'Dummy', |
| 44 |
12313 => 'Dummy_talk', |
| 45 |
12314 => 'DummyNonText', |
| 46 |
12315 => 'DummyNonText_talk', |
| 47 |
], |
| 48 |
MainConfigNames::NamespaceContentModels => [ |
| 49 |
12312 => 'testing', |
| 50 |
12314 => 'testing-nontext', |
| 51 |
], |
| 52 |
MainConfigNames::WatchlistExpiry => true, |
| 53 |
MainConfigNames::WatchlistExpiryMaxDuration => '6 months', |
| 54 |
] ); |
| 55 |
$this->mergeMwGlobalArrayValue( 'wgContentHandlers', [ |
| 56 |
'testing' => 'DummyContentHandlerForTesting', |
| 57 |
'testing-nontext' => 'DummyNonTextContentHandler', |
| 58 |
'testing-serialize-error' => 'DummySerializeErrorContentHandler', |
| 59 |
] ); |
| 60 |
} |
| 61 |
|
| 62 |
public function testEdit() { |
| 63 |
$name = 'Help:ApiEditPageTest_testEdit'; |
| 64 |
|
| 65 |
|
| 66 |
$apiResult = $this->doApiRequestWithToken( [ |
| 67 |
'action' => 'edit', |
| 68 |
'title' => $name, |
| 69 |
'text' => 'some text', |
| 70 |
] ); |
| 71 |
$apiResult = $apiResult[0]; |
| 72 |
|
| 73 |
|
| 74 |
$this->assertArrayHasKey( 'edit', $apiResult ); |
| 75 |
$this->assertArrayHasKey( 'result', $apiResult['edit'] ); |
| 76 |
$this->assertSame( 'Success', $apiResult['edit']['result'] ); |
| 77 |
|
| 78 |
$this->assertArrayHasKey( 'new', $apiResult['edit'] ); |
| 79 |
$this->assertArrayNotHasKey( 'nochange', $apiResult['edit'] ); |
| 80 |
|
| 81 |
$this->assertArrayHasKey( 'pageid', $apiResult['edit'] ); |
| 82 |
|
| 83 |
|
| 84 |
$data = $this->doApiRequestWithToken( [ |
| 85 |
'action' => 'edit', |
| 86 |
'title' => $name, |
| 87 |
'text' => 'some text', |
| 88 |
] ); |
| 89 |
|
| 90 |
$this->assertSame( 'Success', $data[0]['edit']['result'] ); |
| 91 |
|
| 92 |
$this->assertArrayNotHasKey( 'new', $data[0]['edit'] ); |
| 93 |
$this->assertArrayHasKey( 'nochange', $data[0]['edit'] ); |
| 94 |
|
| 95 |
|
| 96 |
$data = $this->doApiRequestWithToken( [ |
| 97 |
'action' => 'edit', |
| 98 |
'title' => $name, |
| 99 |
'text' => 'different text' |
| 100 |
] ); |
| 101 |
|
| 102 |
$this->assertSame( 'Success', $data[0]['edit']['result'] ); |
| 103 |
|
| 104 |
$this->assertArrayNotHasKey( 'new', $data[0]['edit'] ); |
| 105 |
$this->assertArrayNotHasKey( 'nochange', $data[0]['edit'] ); |
| 106 |
|
| 107 |
$this->assertArrayHasKey( 'oldrevid', $data[0]['edit'] ); |
| 108 |
$this->assertArrayHasKey( 'newrevid', $data[0]['edit'] ); |
| 109 |
$this->assertNotEquals( |
| 110 |
$data[0]['edit']['newrevid'], |
| 111 |
$data[0]['edit']['oldrevid'], |
| 112 |
"revision id should change after edit" |
| 113 |
); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public static function provideEditAppend() { |
| 120 |
return [ |
| 121 |
[ |
| 122 |
'foo', 'append', 'bar', "foobar" |
| 123 |
], |
| 124 |
[ |
| 125 |
'foo', 'prepend', 'bar', "barfoo" |
| 126 |
], |
| 127 |
[ |
| 128 |
'', 'append', 'foo', "foo" |
| 129 |
], |
| 130 |
[ |
| 131 |
'', 'prepend', 'foo', "foo" |
| 132 |
], |
| 133 |
[ |
| 134 |
null, 'append', 'foo', "foo" |
| 135 |
], |
| 136 |
[ |
| 137 |
null, 'prepend', 'foo', "foo" |
| 138 |
], |
| 139 |
]; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
public function testEditAppend( $text, $op, $append, $expected ) { |
| 146 |
static $count = 0; |
| 147 |
$count++; |
| 148 |
|
| 149 |
|
| 150 |
$title = Title::makeTitle( NS_HELP, "ApiEditPageTest_testEditAppend_$count" ); |
| 151 |
|
| 152 |
|
| 153 |
if ( $text !== null ) { |
| 154 |
[ $re ] = $this->doApiRequestWithToken( [ |
| 155 |
'action' => 'edit', |
| 156 |
'title' => $title->getPrefixedText(), |
| 157 |
'text' => $text, ] ); |
| 158 |
|
| 159 |
$this->assertSame( 'Success', $re['edit']['result'] ); |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
[ $re ] = $this->doApiRequestWithToken( [ |
| 164 |
'action' => 'edit', |
| 165 |
'title' => $title->getPrefixedText(), |
| 166 |
$op . 'text' => $append, ] ); |
| 167 |
|
| 168 |
$this->assertSame( 'Success', $re['edit']['result'] ); |
| 169 |
|
| 170 |
|
| 171 |
$page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title ); |
| 172 |
$content = $page->getContent(); |
| 173 |
$this->assertNotNull( $content, 'Page should have been created' ); |
| 174 |
|
| 175 |
$text = $content->getText(); |
| 176 |
|
| 177 |
$this->assertSame( $expected, $text ); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
public function testEditSection() { |
| 184 |
$title = Title::makeTitle( NS_HELP, 'ApiEditPageTest_testEditSection' ); |
| 185 |
$wikiPageFactory = $this->getServiceContainer()->getWikiPageFactory(); |
| 186 |
$page = $wikiPageFactory->newFromTitle( $title ); |
| 187 |
$text = "==section 1==\ncontent 1\n==section 2==\ncontent2"; |
| 188 |
|
| 189 |
$page->doUserEditContent( |
| 190 |
$page->getContentHandler()->unserializeContent( $text ), |
| 191 |
$this->getTestSysop()->getAuthority(), |
| 192 |
'summary' |
| 193 |
); |
| 194 |
|
| 195 |
[ $re ] = $this->doApiRequestWithToken( [ |
| 196 |
'action' => 'edit', |
| 197 |
'title' => $title->getPrefixedText(), |
| 198 |
'section' => '1', |
| 199 |
'text' => "==section 1==\nnew content 1", |
| 200 |
] ); |
| 201 |
$this->assertSame( 'Success', $re['edit']['result'] ); |
| 202 |
$newtext = $wikiPageFactory->newFromTitle( $title ) |
| 203 |
->getContent( RevisionRecord::RAW ) |
| 204 |
->getText(); |
| 205 |
$this->assertSame( "==section 1==\nnew content 1\n\n==section 2==\ncontent2", $newtext ); |
| 206 |
|
| 207 |
|
| 208 |
try { |
| 209 |
$this->doApiRequestWithToken( [ |
| 210 |
'action' => 'edit', |
| 211 |
'title' => $title->getPrefixedText(), |
| 212 |
'section' => '9999', |
| 213 |
'text' => 'text', |
| 214 |
] ); |
| 215 |
$this->fail( "Should have raised an ApiUsageException" ); |
| 216 |
} catch ( ApiUsageException $e ) { |
| 217 |
$this->assertApiErrorCode( 'nosuchsection', $e ); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
public function testEditNewSection() { |
| 228 |
$title = Title::makeTitle( NS_HELP, 'ApiEditPageTest_testEditNewSection' ); |
| 229 |
$wikiPageFactory = $this->getServiceContainer()->getWikiPageFactory(); |
| 230 |
|
| 231 |
|
| 232 |
$this->assertFalse( $title->exists() ); |
| 233 |
[ $re ] = $this->doApiRequestWithToken( [ |
| 234 |
'action' => 'edit', |
| 235 |
'title' => $title->getPrefixedText(), |
| 236 |
'section' => 'new', |
| 237 |
'text' => 'test', |
| 238 |
'summary' => 'header', |
| 239 |
] ); |
| 240 |
|
| 241 |
$this->assertSame( 'Success', $re['edit']['result'] ); |
| 242 |
|
| 243 |
$text = $wikiPageFactory->newFromTitle( $title ) |
| 244 |
->getContent( RevisionRecord::RAW ) |
| 245 |
->getText(); |
| 246 |
$this->assertSame( "== header ==\n\ntest", $text ); |
| 247 |
|
| 248 |
|
| 249 |
$this->assertTrue( $title->exists( IDBAccessObject::READ_LATEST ) ); |
| 250 |
[ $re2 ] = $this->doApiRequestWithToken( [ |
| 251 |
'action' => 'edit', |
| 252 |
'title' => $title->getPrefixedText(), |
| 253 |
'section' => 'new', |
| 254 |
'text' => 'test', |
| 255 |
'summary' => 'header', |
| 256 |
] ); |
| 257 |
|
| 258 |
$this->assertSame( 'Success', $re2['edit']['result'] ); |
| 259 |
$text = $wikiPageFactory->newFromTitle( $title ) |
| 260 |
->getContent( RevisionRecord::RAW ) |
| 261 |
->getText(); |
| 262 |
$this->assertSame( "== header ==\n\ntest\n\n== header ==\n\ntest", $text ); |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
public function testEditNewSectionSummarySectiontitle( |
| 271 |
$sectiontitle, |
| 272 |
$summary, |
| 273 |
$expectedText, |
| 274 |
$expectedSummary |
| 275 |
) { |
| 276 |
static $count = 0; |
| 277 |
$count++; |
| 278 |
$title = Title::makeTitle( NS_HELP, 'ApiEditPageTest_testEditNewSectionSummarySectiontitle' . $count ); |
| 279 |
|
| 280 |
|
| 281 |
$this->doApiRequestWithToken( [ |
| 282 |
'action' => 'edit', |
| 283 |
'title' => $title->getPrefixedText(), |
| 284 |
'section' => 'new', |
| 285 |
'text' => 'text', |
| 286 |
'sectiontitle' => $sectiontitle, |
| 287 |
'summary' => $summary, |
| 288 |
] ); |
| 289 |
|
| 290 |
$wikiPageFactory = $this->getServiceContainer()->getWikiPageFactory(); |
| 291 |
$wikiPage = $wikiPageFactory->newFromTitle( $title ); |
| 292 |
|
| 293 |
|
| 294 |
$savedText = $wikiPage->getContent( RevisionRecord::RAW )->getText(); |
| 295 |
$this->assertSame( $expectedText, $savedText, 'Correct text saved (new page)' ); |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
$savedSummary = $wikiPage->getRevisionRecord()->getComment( RevisionRecord::RAW )->text; |
| 300 |
$expectedSummaryNew = $expectedSummary ?: wfMessage( 'autosumm-new' )->rawParams( $expectedText ) |
| 301 |
->inContentLanguage()->text(); |
| 302 |
$this->assertSame( $expectedSummaryNew, $savedSummary, 'Correct summary saved (new page)' ); |
| 303 |
|
| 304 |
|
| 305 |
$this->editPage( $wikiPage, '' ); |
| 306 |
|
| 307 |
|
| 308 |
$this->doApiRequestWithToken( [ |
| 309 |
'action' => 'edit', |
| 310 |
'title' => $title->getPrefixedText(), |
| 311 |
'section' => 'new', |
| 312 |
'text' => 'text', |
| 313 |
'sectiontitle' => $sectiontitle, |
| 314 |
'summary' => $summary, |
| 315 |
] ); |
| 316 |
|
| 317 |
$wikiPage = $wikiPageFactory->newFromTitle( $title ); |
| 318 |
|
| 319 |
|
| 320 |
$savedText = $wikiPage->getContent( RevisionRecord::RAW )->getText(); |
| 321 |
$this->assertSame( $expectedText, $savedText, 'Correct text saved (existing page)' ); |
| 322 |
|
| 323 |
|
| 324 |
$savedSummary = $wikiPage->getRevisionRecord()->getComment( RevisionRecord::RAW )->text; |
| 325 |
$this->assertSame( $expectedSummary, $savedSummary, 'Correct summary saved (existing page)' ); |
| 326 |
} |
| 327 |
|
| 328 |
public static function provideEditNewSectionSummarySectiontitle() { |
| 329 |
$sectiontitleCases = [ |
| 330 |
'unset' => null, |
| 331 |
'empty' => '', |
| 332 |
'set' => 'sectiontitle', |
| 333 |
]; |
| 334 |
$summaryCases = [ |
| 335 |
'unset' => null, |
| 336 |
'empty' => '', |
| 337 |
'set' => 'summary', |
| 338 |
]; |
| 339 |
|
| 340 |
$expectedTexts = [ |
| 341 |
"text", |
| 342 |
"text", |
| 343 |
"== summary ==\n\ntext", |
| 344 |
"text", |
| 345 |
"text", |
| 346 |
"text", |
| 347 |
"== sectiontitle ==\n\ntext", |
| 348 |
"== sectiontitle ==\n\ntext", |
| 349 |
"== sectiontitle ==\n\ntext", |
| 350 |
]; |
| 351 |
|
| 352 |
$expectedSummaries = [ |
| 353 |
'', |
| 354 |
'', |
| 355 |
'/* summary */ new section', |
| 356 |
'', |
| 357 |
'', |
| 358 |
'summary', |
| 359 |
'/* sectiontitle */ new section', |
| 360 |
'/* sectiontitle */ new section', |
| 361 |
'summary', |
| 362 |
]; |
| 363 |
|
| 364 |
$i = 0; |
| 365 |
foreach ( $sectiontitleCases as $sectiontitleDesc => $sectiontitle ) { |
| 366 |
foreach ( $summaryCases as $summaryDesc => $summary ) { |
| 367 |
$message = "sectiontitle $sectiontitleDesc, summary $summaryDesc"; |
| 368 |
yield $message => [ |
| 369 |
$sectiontitle, |
| 370 |
$summary, |
| 371 |
$expectedTexts[$i], |
| 372 |
$expectedSummaries[$i], |
| 373 |
]; |
| 374 |
$i++; |
| 375 |
} |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
public function testEdit_redirect() { |
| 383 |
static $count = 0; |
| 384 |
$count++; |
| 385 |
|
| 386 |
|
| 387 |
$title = Title::makeTitle( NS_HELP, "ApiEditPageTest_testEdit_redirect_$count" ); |
| 388 |
$wikiPageFactory = $this->getServiceContainer()->getWikiPageFactory(); |
| 389 |
$page = $this->getExistingTestPage( $title ); |
| 390 |
$this->forceRevisionDate( $page, '20120101000000' ); |
| 391 |
|
| 392 |
$rtitle = Title::makeTitle( NS_HELP, "ApiEditPageTest_testEdit_redirect_r$count" ); |
| 393 |
$rpage = $wikiPageFactory->newFromTitle( $rtitle ); |
| 394 |
|
| 395 |
$baseTime = $page->getRevisionRecord()->getTimestamp(); |
| 396 |
|
| 397 |
|
| 398 |
$rpage->doUserEditContent( |
| 399 |
new WikitextContent( "#REDIRECT [[{$title->getPrefixedText()}]]" ), |
| 400 |
$this->getTestSysop()->getUser(), |
| 401 |
"testing 1", |
| 402 |
EDIT_NEW |
| 403 |
); |
| 404 |
$this->forceRevisionDate( $rpage, '20120101000000' ); |
| 405 |
|
| 406 |
|
| 407 |
$rpage->doUserEditContent( |
| 408 |
new WikitextContent( "#REDIRECT [[{$title->getPrefixedText()}]]\n\n[[Category:Test]]" ), |
| 409 |
$this->getTestUser()->getUser(), |
| 410 |
"testing 2", |
| 411 |
EDIT_UPDATE |
| 412 |
); |
| 413 |
$this->forceRevisionDate( $rpage, '20120101020202' ); |
| 414 |
|
| 415 |
|
| 416 |
[ $re, , ] = $this->doApiRequestWithToken( [ |
| 417 |
'action' => 'edit', |
| 418 |
'title' => $rtitle->getPrefixedText(), |
| 419 |
'text' => 'nix bar!', |
| 420 |
'basetimestamp' => $baseTime, |
| 421 |
'section' => 'new', |
| 422 |
'redirect' => true, |
| 423 |
] ); |
| 424 |
|
| 425 |
$this->assertSame( 'Success', $re['edit']['result'], |
| 426 |
"no problems expected when following redirect" ); |
| 427 |
} |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
public function testEdit_redirectText() { |
| 433 |
static $count = 0; |
| 434 |
$count++; |
| 435 |
|
| 436 |
|
| 437 |
$title = Title::makeTitle( NS_HELP, "ApiEditPageTest_testEdit_redirectText_$count" ); |
| 438 |
$wikiPageFactory = $this->getServiceContainer()->getWikiPageFactory(); |
| 439 |
$page = $this->getExistingTestPage( $title ); |
| 440 |
$this->forceRevisionDate( $page, '20120101000000' ); |
| 441 |
$baseTime = $page->getRevisionRecord()->getTimestamp(); |
| 442 |
|
| 443 |
$rtitle = Title::makeTitle( NS_HELP, "ApiEditPageTest_testEdit_redirectText_r$count" ); |
| 444 |
$rpage = $wikiPageFactory->newFromTitle( $rtitle ); |
| 445 |
|
| 446 |
|
| 447 |
$rpage->doUserEditContent( |
| 448 |
new WikitextContent( "#REDIRECT [[{$title->getPrefixedText()}]]" ), |
| 449 |
$this->getTestSysop()->getUser(), |
| 450 |
"testing 1", |
| 451 |
EDIT_NEW |
| 452 |
); |
| 453 |
$this->forceRevisionDate( $rpage, '20120101000000' ); |
| 454 |
|
| 455 |
|
| 456 |
$rpage->doUserEditContent( |
| 457 |
new WikitextContent( "#REDIRECT [[{$title->getPrefixedText()}]]\n\n[[Category:Test]]" ), |
| 458 |
$this->getTestUser()->getUser(), |
| 459 |
"testing 2", |
| 460 |
EDIT_UPDATE |
| 461 |
); |
| 462 |
$this->forceRevisionDate( $rpage, '20120101020202' ); |
| 463 |
|
| 464 |
|
| 465 |
try { |
| 466 |
$this->doApiRequestWithToken( [ |
| 467 |
'action' => 'edit', |
| 468 |
'title' => $rtitle->getPrefixedText(), |
| 469 |
'text' => 'nix bar!', |
| 470 |
'basetimestamp' => $baseTime, |
| 471 |
'redirect' => true, |
| 472 |
] ); |
| 473 |
|
| 474 |
$this->fail( 'redirect-appendonly error expected' ); |
| 475 |
} catch ( ApiUsageException $ex ) { |
| 476 |
$this->assertApiErrorCode( 'redirect-appendonly', $ex ); |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
public function testEditConflict_revid() { |
| 481 |
static $count = 0; |
| 482 |
$count++; |
| 483 |
|
| 484 |
|
| 485 |
$title = Title::makeTitle( NS_HELP, "ApiEditPageTest_testEditConflict_$count" ); |
| 486 |
|
| 487 |
$page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title ); |
| 488 |
|
| 489 |
|
| 490 |
$page->doUserEditContent( |
| 491 |
new WikitextContent( "Foo" ), |
| 492 |
$this->getTestSysop()->getUser(), |
| 493 |
"testing 1", |
| 494 |
EDIT_NEW |
| 495 |
); |
| 496 |
$this->forceRevisionDate( $page, '20120101000000' ); |
| 497 |
$baseId = $page->getRevisionRecord()->getId(); |
| 498 |
|
| 499 |
|
| 500 |
$page->doUserEditContent( |
| 501 |
new WikitextContent( "Foo bar" ), |
| 502 |
$this->getTestUser()->getUser(), |
| 503 |
"testing 2", |
| 504 |
EDIT_UPDATE |
| 505 |
); |
| 506 |
$this->forceRevisionDate( $page, '20120101020202' ); |
| 507 |
|
| 508 |
|
| 509 |
try { |
| 510 |
$this->doApiRequestWithToken( [ |
| 511 |
'action' => 'edit', |
| 512 |
'title' => $title->getPrefixedText(), |
| 513 |
'text' => 'nix bar!', |
| 514 |
'baserevid' => $baseId, |
| 515 |
], null, $this->getTestSysop()->getUser() ); |
| 516 |
|
| 517 |
$this->fail( 'edit conflict expected' ); |
| 518 |
} catch ( ApiUsageException $ex ) { |
| 519 |
$this->assertApiErrorCode( 'editconflict', $ex ); |
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
public function testEditConflict_timestamp() { |
| 524 |
static $count = 0; |
| 525 |
$count++; |
| 526 |
|
| 527 |
|
| 528 |
$title = Title::makeTitle( NS_HELP, "ApiEditPageTest_testEditConflict_$count" ); |
| 529 |
|
| 530 |
$page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title ); |
| 531 |
|
| 532 |
|
| 533 |
$page->doUserEditContent( |
| 534 |
new WikitextContent( "Foo" ), |
| 535 |
$this->getTestSysop()->getUser(), |
| 536 |
"testing 1", |
| 537 |
EDIT_NEW |
| 538 |
); |
| 539 |
$this->forceRevisionDate( $page, '20120101000000' ); |
| 540 |
$baseTime = $page->getRevisionRecord()->getTimestamp(); |
| 541 |
|
| 542 |
|
| 543 |
$page->doUserEditContent( |
| 544 |
new WikitextContent( "Foo bar" ), |
| 545 |
$this->getTestUser()->getUser(), |
| 546 |
"testing 2", |
| 547 |
EDIT_UPDATE |
| 548 |
); |
| 549 |
$this->forceRevisionDate( $page, '20120101020202' ); |
| 550 |
|
| 551 |
|
| 552 |
try { |
| 553 |
$this->doApiRequestWithToken( [ |
| 554 |
'action' => 'edit', |
| 555 |
'title' => $title->getPrefixedText(), |
| 556 |
'text' => 'nix bar!', |
| 557 |
'basetimestamp' => $baseTime, |
| 558 |
] ); |
| 559 |
|
| 560 |
$this->fail( 'edit conflict expected' ); |
| 561 |
} catch ( ApiUsageException $ex ) { |
| 562 |
$this->assertApiErrorCode( 'editconflict', $ex ); |
| 563 |
} |
| 564 |
} |
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
public function testEditConflict_newSection() { |
| 570 |
static $count = 0; |
| 571 |
$count++; |
| 572 |
|
| 573 |
|
| 574 |
$title = Title::makeTitle( NS_HELP, "ApiEditPageTest_testEditConflict_newSection_$count" ); |
| 575 |
|
| 576 |
$page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title ); |
| 577 |
|
| 578 |
|
| 579 |
$page->doUserEditContent( |
| 580 |
new WikitextContent( "Foo" ), |
| 581 |
$this->getTestSysop()->getUser(), |
| 582 |
"testing 1", |
| 583 |
EDIT_NEW |
| 584 |
); |
| 585 |
$this->forceRevisionDate( $page, '20120101000000' ); |
| 586 |
$baseTime = $page->getRevisionRecord()->getTimestamp(); |
| 587 |
|
| 588 |
|
| 589 |
$page->doUserEditContent( |
| 590 |
new WikitextContent( "Foo bar" ), |
| 591 |
$this->getTestUser()->getUser(), |
| 592 |
"testing 2", |
| 593 |
EDIT_UPDATE |
| 594 |
); |
| 595 |
$this->forceRevisionDate( $page, '20120101020202' ); |
| 596 |
|
| 597 |
|
| 598 |
[ $re, , ] = $this->doApiRequestWithToken( [ |
| 599 |
'action' => 'edit', |
| 600 |
'title' => $title->getPrefixedText(), |
| 601 |
'text' => 'nix bar!', |
| 602 |
'basetimestamp' => $baseTime, |
| 603 |
'section' => 'new', |
| 604 |
] ); |
| 605 |
|
| 606 |
$this->assertSame( 'Success', $re['edit']['result'], |
| 607 |
"no edit conflict expected here" ); |
| 608 |
} |
| 609 |
|
| 610 |
public function testEditConflict_T43990() { |
| 611 |
static $count = 0; |
| 612 |
$count++; |
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
$title = Title::makeTitle( NS_HELP, "ApiEditPageTest_testEditConflict_redirect_T43990_$count" ); |
| 622 |
$wikiPageFactory = $this->getServiceContainer()->getWikiPageFactory(); |
| 623 |
$page = $this->getExistingTestPage( $title ); |
| 624 |
$this->forceRevisionDate( $page, '20120101000000' ); |
| 625 |
|
| 626 |
$rtitle = Title::makeTitle( NS_HELP, "ApiEditPageTest_testEditConflict_redirect_T43990_r$count" ); |
| 627 |
$rpage = $wikiPageFactory->newFromTitle( $rtitle ); |
| 628 |
|
| 629 |
|
| 630 |
$rpage->doUserEditContent( |
| 631 |
new WikitextContent( "#REDIRECT [[{$title->getPrefixedText()}]]" ), |
| 632 |
$this->getTestSysop()->getUser(), |
| 633 |
"testing 1", |
| 634 |
EDIT_NEW |
| 635 |
); |
| 636 |
$this->forceRevisionDate( $rpage, '20120101000000' ); |
| 637 |
|
| 638 |
|
| 639 |
$page->doUserEditContent( |
| 640 |
new WikitextContent( "Foo bar" ), |
| 641 |
$this->getTestUser()->getUser(), |
| 642 |
"testing 2", |
| 643 |
EDIT_UPDATE |
| 644 |
); |
| 645 |
$this->forceRevisionDate( $rpage, '20120101020202' ); |
| 646 |
|
| 647 |
|
| 648 |
[ $re, , ] = $this->doApiRequestWithToken( [ |
| 649 |
'action' => 'edit', |
| 650 |
'title' => $rtitle->getPrefixedText(), |
| 651 |
'text' => 'nix bar!', |
| 652 |
'section' => 'new', |
| 653 |
'redirect' => true, |
| 654 |
] ); |
| 655 |
|
| 656 |
$this->assertSame( 'Success', $re['edit']['result'], |
| 657 |
"no edit conflict expected here" ); |
| 658 |
} |
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
protected function forceRevisionDate( WikiPage $page, $timestamp ) { |
| 665 |
$dbw = $this->getDb(); |
| 666 |
|
| 667 |
$dbw->newUpdateQueryBuilder() |
| 668 |
->update( 'revision' ) |
| 669 |
->set( [ 'rev_timestamp' => $dbw->timestamp( $timestamp ) ] ) |
| 670 |
->where( [ 'rev_id' => $page->getLatest() ] ) |
| 671 |
->caller( __METHOD__ )->execute(); |
| 672 |
|
| 673 |
$page->clear(); |
| 674 |
} |
| 675 |
|
| 676 |
public function testCheckDirectApiEditingDisallowed_forNonTextContent() { |
| 677 |
$this->expectApiErrorCode( 'no-direct-editing' ); |
| 678 |
|
| 679 |
$this->doApiRequestWithToken( [ |
| 680 |
'action' => 'edit', |
| 681 |
'title' => 'Dummy:ApiEditPageTest_nonTextPageEdit', |
| 682 |
'text' => '{"animals":["kittens!"]}' |
| 683 |
] ); |
| 684 |
} |
| 685 |
|
| 686 |
public function testSupportsDirectApiEditing_withContentHandlerOverride() { |
| 687 |
$name = 'DummyNonText:ApiEditPageTest_testNonTextEdit'; |
| 688 |
$data = 'some bla bla text'; |
| 689 |
|
| 690 |
$result = $this->doApiRequestWithToken( [ |
| 691 |
'action' => 'edit', |
| 692 |
'title' => $name, |
| 693 |
'text' => $data, |
| 694 |
] ); |
| 695 |
|
| 696 |
$apiResult = $result[0]; |
| 697 |
|
| 698 |
|
| 699 |
$this->assertArrayHasKey( 'edit', $apiResult ); |
| 700 |
$this->assertArrayHasKey( 'result', $apiResult['edit'] ); |
| 701 |
$this->assertSame( 'Success', $apiResult['edit']['result'] ); |
| 702 |
|
| 703 |
$this->assertArrayHasKey( 'new', $apiResult['edit'] ); |
| 704 |
$this->assertArrayNotHasKey( 'nochange', $apiResult['edit'] ); |
| 705 |
|
| 706 |
$this->assertArrayHasKey( 'pageid', $apiResult['edit'] ); |
| 707 |
|
| 708 |
|
| 709 |
$page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( Title::newFromText( $name ) ); |
| 710 |
$this->assertSame( "testing-nontext", $page->getContentModel() ); |
| 711 |
$this->assertSame( $data, $page->getContent()->serialize() ); |
| 712 |
} |
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
public function testUndoAfterContentModelChange() { |
| 720 |
$name = 'Help:' . __FUNCTION__; |
| 721 |
$sysop = $this->getTestSysop()->getUser(); |
| 722 |
$otherUser = $this->getTestUser()->getUser(); |
| 723 |
|
| 724 |
$apiResult = $this->doApiRequestWithToken( [ |
| 725 |
'action' => 'edit', |
| 726 |
'title' => $name, |
| 727 |
'text' => 'some text', |
| 728 |
], null, $sysop )[0]; |
| 729 |
|
| 730 |
|
| 731 |
$this->assertArrayHasKey( 'edit', $apiResult ); |
| 732 |
$this->assertArrayHasKey( 'result', $apiResult['edit'] ); |
| 733 |
$this->assertSame( 'Success', $apiResult['edit']['result'] ); |
| 734 |
$this->assertArrayHasKey( 'contentmodel', $apiResult['edit'] ); |
| 735 |
|
| 736 |
$this->assertSame( 'wikitext', $apiResult['edit']['contentmodel'] ); |
| 737 |
|
| 738 |
|
| 739 |
$apiResult = $this->doApiRequestWithToken( [ |
| 740 |
'action' => 'edit', |
| 741 |
'title' => $name, |
| 742 |
'text' => '{}', |
| 743 |
'contentmodel' => 'json', |
| 744 |
], null, $otherUser )[0]; |
| 745 |
|
| 746 |
|
| 747 |
$this->assertArrayHasKey( 'edit', $apiResult ); |
| 748 |
$this->assertArrayHasKey( 'result', $apiResult['edit'] ); |
| 749 |
$this->assertSame( 'Success', $apiResult['edit']['result'] ); |
| 750 |
$this->assertArrayHasKey( 'contentmodel', $apiResult['edit'] ); |
| 751 |
$this->assertSame( 'json', $apiResult['edit']['contentmodel'] ); |
| 752 |
|
| 753 |
$apiResult = $this->doApiRequestWithToken( [ |
| 754 |
'action' => 'edit', |
| 755 |
'title' => $name, |
| 756 |
'undo' => $apiResult['edit']['newrevid'] |
| 757 |
], null, $sysop )[0]; |
| 758 |
|
| 759 |
|
| 760 |
$this->assertArrayHasKey( 'edit', $apiResult ); |
| 761 |
$this->assertArrayHasKey( 'result', $apiResult['edit'] ); |
| 762 |
$this->assertSame( 'Success', $apiResult['edit']['result'] ); |
| 763 |
$this->assertArrayHasKey( 'contentmodel', $apiResult['edit'] ); |
| 764 |
|
| 765 |
$this->assertSame( 'wikitext', $apiResult['edit']['contentmodel'] ); |
| 766 |
} |
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
public function testCorrectContentFormat() { |
| 772 |
$title = Title::makeTitle( NS_HELP, 'TestCorrectContentFormat' ); |
| 773 |
|
| 774 |
$this->doApiRequestWithToken( [ |
| 775 |
'action' => 'edit', |
| 776 |
'title' => $title->getPrefixedText(), |
| 777 |
'text' => 'some text', |
| 778 |
'contentmodel' => 'wikitext', |
| 779 |
'contentformat' => 'text/x-wiki', |
| 780 |
] ); |
| 781 |
|
| 782 |
$this->assertTrue( $title->exists( IDBAccessObject::READ_LATEST ) ); |
| 783 |
} |
| 784 |
|
| 785 |
public function testUnsupportedContentFormat() { |
| 786 |
$title = Title::makeTitle( NS_HELP, 'TestUnsupportedContentFormat' ); |
| 787 |
|
| 788 |
$this->expectApiErrorCode( 'badvalue' ); |
| 789 |
|
| 790 |
try { |
| 791 |
$this->doApiRequestWithToken( [ |
| 792 |
'action' => 'edit', |
| 793 |
'title' => $title->getPrefixedText(), |
| 794 |
'text' => 'some text', |
| 795 |
'contentformat' => 'nonexistent format', |
| 796 |
] ); |
| 797 |
} finally { |
| 798 |
$this->assertFalse( $title->exists( IDBAccessObject::READ_LATEST ) ); |
| 799 |
} |
| 800 |
} |
| 801 |
|
| 802 |
public function testMismatchedContentFormat() { |
| 803 |
$title = Title::makeTitle( NS_HELP, 'TestMismatchedContentFormat' ); |
| 804 |
|
| 805 |
$this->expectApiErrorCode( 'badformat' ); |
| 806 |
|
| 807 |
try { |
| 808 |
$this->doApiRequestWithToken( [ |
| 809 |
'action' => 'edit', |
| 810 |
'title' => $title->getPrefixedText(), |
| 811 |
'text' => 'some text', |
| 812 |
'contentmodel' => 'wikitext', |
| 813 |
'contentformat' => 'text/plain', |
| 814 |
] ); |
| 815 |
} finally { |
| 816 |
$this->assertFalse( $title->exists( IDBAccessObject::READ_LATEST ) ); |
| 817 |
} |
| 818 |
} |
| 819 |
|
| 820 |
public function testUndoToInvalidRev() { |
| 821 |
$title = Title::makeTitle( NS_HELP, 'TestUndoToInvalidRev' ); |
| 822 |
|
| 823 |
$revId = $this->editPage( $title, 'Some text' )->getNewRevision() |
| 824 |
->getId(); |
| 825 |
$revId++; |
| 826 |
|
| 827 |
$this->expectApiErrorCode( 'nosuchrevid' ); |
| 828 |
|
| 829 |
$this->doApiRequestWithToken( [ |
| 830 |
'action' => 'edit', |
| 831 |
'title' => $title->getPrefixedText(), |
| 832 |
'undo' => $revId, |
| 833 |
] ); |
| 834 |
} |
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
public function testUndoAfterToInvalidRev() { |
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
$page = $this->getServiceContainer()->getWikiPageFactory() |
| 851 |
->newFromLinkTarget( new TitleValue( NS_HELP, 'TestUndoAfterToInvalidRev' ) ); |
| 852 |
|
| 853 |
$revId1 = $this->editPage( $page, '1' )->getNewRevision()->getId(); |
| 854 |
$revId2 = $this->editPage( $page, '2' )->getNewRevision()->getId(); |
| 855 |
$revId3 = $this->editPage( $page, '3' )->getNewRevision()->getId(); |
| 856 |
|
| 857 |
|
| 858 |
$dbw = $this->getDb(); |
| 859 |
$dbw->newDeleteQueryBuilder() |
| 860 |
->deleteFrom( 'revision' ) |
| 861 |
->where( [ 'rev_id' => $revId2 ] ) |
| 862 |
->caller( __METHOD__ )->execute(); |
| 863 |
$dbw->newUpdateQueryBuilder() |
| 864 |
->update( 'revision' ) |
| 865 |
->set( [ 'rev_parent_id' => $revId1 ] ) |
| 866 |
->where( [ 'rev_id' => $revId3 ] ) |
| 867 |
->caller( __METHOD__ )->execute(); |
| 868 |
|
| 869 |
$this->expectApiErrorCode( 'nosuchrevid' ); |
| 870 |
|
| 871 |
$this->doApiRequestWithToken( [ |
| 872 |
'action' => 'edit', |
| 873 |
'title' => $page->getTitle()->getPrefixedText(), |
| 874 |
'undo' => $revId3, |
| 875 |
'undoafter' => $revId2, |
| 876 |
] ); |
| 877 |
} |
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
public function testUndoAfterToHiddenRev() { |
| 884 |
$page = $this->getServiceContainer()->getWikiPageFactory() |
| 885 |
->newFromLinkTarget( new TitleValue( NS_HELP, 'TestUndoAfterToHiddenRev' ) ); |
| 886 |
$titleObj = $page->getTitle(); |
| 887 |
|
| 888 |
$this->editPage( $page, '0' ); |
| 889 |
|
| 890 |
$revId1 = $this->editPage( $page, '1' )->getNewRevision()->getId(); |
| 891 |
|
| 892 |
$revId2 = $this->editPage( $page, '2' )->getNewRevision()->getId(); |
| 893 |
|
| 894 |
|
| 895 |
$list = RevisionDeleter::createList( 'revision', |
| 896 |
RequestContext::getMain(), $titleObj, [ $revId1 ] ); |
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
RequestContext::getMain()->setUser( $this->getTestUser()->getUser() ); |
| 901 |
$list->setVisibility( [ |
| 902 |
'value' => [ RevisionRecord::DELETED_TEXT => 1 ], |
| 903 |
'comment' => 'Bye-bye', |
| 904 |
] ); |
| 905 |
|
| 906 |
$this->expectApiErrorCode( 'nosuchrevid' ); |
| 907 |
|
| 908 |
$this->doApiRequestWithToken( [ |
| 909 |
'action' => 'edit', |
| 910 |
'title' => $titleObj->getPrefixedText(), |
| 911 |
'undo' => $revId2, |
| 912 |
'undoafter' => $revId1, |
| 913 |
] ); |
| 914 |
} |
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
public function testUndoWithSwappedRevisions() { |
| 921 |
$this->markTestSkippedIfNoDiff3(); |
| 922 |
|
| 923 |
$page = $this->getServiceContainer()->getWikiPageFactory() |
| 924 |
->newFromLinkTarget( new TitleValue( NS_HELP, 'TestUndoWithSwappedRevisions' ) ); |
| 925 |
$this->editPage( $page, '0' ); |
| 926 |
|
| 927 |
$revId2 = $this->editPage( $page, '2' )->getNewRevision()->getId(); |
| 928 |
|
| 929 |
$revId1 = $this->editPage( $page, '1' )->getNewRevision()->getId(); |
| 930 |
|
| 931 |
|
| 932 |
$dbw = $this->getDb(); |
| 933 |
$dbw->newUpdateQueryBuilder() |
| 934 |
->update( 'revision' ) |
| 935 |
->set( [ 'rev_timestamp' => $dbw->timestamp( time() - 86400 ) ] ) |
| 936 |
->where( [ 'rev_id' => $revId1 ] ) |
| 937 |
->caller( __METHOD__ )->execute(); |
| 938 |
|
| 939 |
$this->doApiRequestWithToken( [ |
| 940 |
'action' => 'edit', |
| 941 |
'title' => $page->getTitle()->getPrefixedText(), |
| 942 |
'undo' => $revId2, |
| 943 |
'undoafter' => $revId1, |
| 944 |
] ); |
| 945 |
|
| 946 |
$page->loadPageData( IDBAccessObject::READ_LATEST ); |
| 947 |
$this->assertSame( '1', $page->getContent()->getText() ); |
| 948 |
} |
| 949 |
|
| 950 |
public function testUndoWithConflicts() { |
| 951 |
$this->expectApiErrorCode( 'undofailure' ); |
| 952 |
|
| 953 |
$page = $this->getServiceContainer()->getWikiPageFactory() |
| 954 |
->newFromLinkTarget( new TitleValue( NS_HELP, 'TestUndoWithConflicts' ) ); |
| 955 |
$this->editPage( $page, '1' ); |
| 956 |
|
| 957 |
$revId = $this->editPage( $page, '2' )->getNewRevision()->getId(); |
| 958 |
|
| 959 |
$this->editPage( $page, '3' ); |
| 960 |
|
| 961 |
$this->doApiRequestWithToken( [ |
| 962 |
'action' => 'edit', |
| 963 |
'title' => $page->getTitle()->getPrefixedText(), |
| 964 |
'undo' => $revId, |
| 965 |
] ); |
| 966 |
|
| 967 |
$page->loadPageData( IDBAccessObject::READ_LATEST ); |
| 968 |
$this->assertSame( '3', $page->getContent()->getText() ); |
| 969 |
} |
| 970 |
|
| 971 |
public function testReversedUndoAfter() { |
| 972 |
$this->markTestSkippedIfNoDiff3(); |
| 973 |
|
| 974 |
$page = $this->getServiceContainer()->getWikiPageFactory() |
| 975 |
->newFromLinkTarget( new TitleValue( NS_HELP, 'TestReversedUndoAfter' ) ); |
| 976 |
$this->editPage( $page, '0' ); |
| 977 |
$revId1 = $this->editPage( $page, '1' )->getNewRevision()->getId(); |
| 978 |
$revId2 = $this->editPage( $page, '2' )->getNewRevision()->getId(); |
| 979 |
|
| 980 |
$this->doApiRequestWithToken( [ |
| 981 |
'action' => 'edit', |
| 982 |
'title' => $page->getTitle()->getPrefixedText(), |
| 983 |
'undo' => $revId1, |
| 984 |
'undoafter' => $revId2, |
| 985 |
] ); |
| 986 |
|
| 987 |
$page->loadPageData( IDBAccessObject::READ_LATEST ); |
| 988 |
$this->assertSame( '2', $page->getContent()->getText() ); |
| 989 |
} |
| 990 |
|
| 991 |
public function testUndoToRevFromDifferentPage() { |
| 992 |
$title1 = Title::makeTitle( NS_HELP, 'TestUndoToRevFromDifferentPage-1' ); |
| 993 |
$this->editPage( $title1, 'Some text' ); |
| 994 |
$revId = $this->editPage( $title1, 'Some more text' ) |
| 995 |
->getNewRevision()->getId(); |
| 996 |
|
| 997 |
$title2 = Title::makeTitle( NS_HELP, 'TestUndoToRevFromDifferentPage-2' ); |
| 998 |
$this->editPage( $title2, 'Some text' ); |
| 999 |
|
| 1000 |
$this->expectApiErrorCode( 'revwrongpage' ); |
| 1001 |
|
| 1002 |
$this->doApiRequestWithToken( [ |
| 1003 |
'action' => 'edit', |
| 1004 |
'title' => $title2->getPrefixedText(), |
| 1005 |
'undo' => $revId, |
| 1006 |
] ); |
| 1007 |
} |
| 1008 |
|
| 1009 |
public function testUndoAfterToRevFromDifferentPage() { |
| 1010 |
$title1 = Title::makeTitle( NS_HELP, 'TestUndoAfterToRevFromDifferentPage-1' ); |
| 1011 |
$revId1 = $this->editPage( $title1, 'Some text' ) |
| 1012 |
->getNewRevision()->getId(); |
| 1013 |
|
| 1014 |
$title2 = Title::makeTitle( NS_HELP, 'TestUndoAfterToRevFromDifferentPage-2' ); |
| 1015 |
$revId2 = $this->editPage( $title2, 'Some text' ) |
| 1016 |
->getNewRevision()->getId(); |
| 1017 |
|
| 1018 |
$this->expectApiErrorCode( 'revwrongpage' ); |
| 1019 |
|
| 1020 |
$this->doApiRequestWithToken( [ |
| 1021 |
'action' => 'edit', |
| 1022 |
'title' => $title2->getPrefixedText(), |
| 1023 |
'undo' => $revId2, |
| 1024 |
'undoafter' => $revId1, |
| 1025 |
] ); |
| 1026 |
} |
| 1027 |
|
| 1028 |
public function testMd5Text() { |
| 1029 |
$title = Title::makeTitle( NS_HELP, 'TestMd5Text' ); |
| 1030 |
|
| 1031 |
$this->assertFalse( $title->exists( IDBAccessObject::READ_LATEST ) ); |
| 1032 |
|
| 1033 |
$this->doApiRequestWithToken( [ |
| 1034 |
'action' => 'edit', |
| 1035 |
'title' => $title->getPrefixedText(), |
| 1036 |
'text' => 'Some text', |
| 1037 |
'md5' => md5( 'Some text' ), |
| 1038 |
] ); |
| 1039 |
|
| 1040 |
$this->assertTrue( $title->exists( IDBAccessObject::READ_LATEST ) ); |
| 1041 |
} |
| 1042 |
|
| 1043 |
public function testMd5PrependText() { |
| 1044 |
$title = Title::makeTitle( NS_HELP, 'TestMd5PrependText' ); |
| 1045 |
|
| 1046 |
$this->editPage( $title, 'Some text' ); |
| 1047 |
|
| 1048 |
$this->doApiRequestWithToken( [ |
| 1049 |
'action' => 'edit', |
| 1050 |
'title' => $title->getPrefixedText(), |
| 1051 |
'prependtext' => 'Alert: ', |
| 1052 |
'md5' => md5( 'Alert: ' ), |
| 1053 |
] ); |
| 1054 |
|
| 1055 |
$text = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title ) |
| 1056 |
->getContent()->getText(); |
| 1057 |
$this->assertSame( 'Alert: Some text', $text ); |
| 1058 |
} |
| 1059 |
|
| 1060 |
public function testMd5AppendText() { |
| 1061 |
$title = Title::makeTitle( NS_HELP, 'TestMd5AppendText' ); |
| 1062 |
|
| 1063 |
$this->editPage( $title, 'Some text' ); |
| 1064 |
|
| 1065 |
$this->doApiRequestWithToken( [ |
| 1066 |
'action' => 'edit', |
| 1067 |
'title' => $title->getPrefixedText(), |
| 1068 |
'appendtext' => ' is nice', |
| 1069 |
'md5' => md5( ' is nice' ), |
| 1070 |
] ); |
| 1071 |
|
| 1072 |
$text = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title ) |
| 1073 |
->getContent()->getText(); |
| 1074 |
$this->assertSame( 'Some text is nice', $text ); |
| 1075 |
} |
| 1076 |
|
| 1077 |
public function testMd5PrependAndAppendText() { |
| 1078 |
$title = Title::makeTitle( NS_HELP, 'TestMd5PrependAndAppendText' ); |
| 1079 |
|
| 1080 |
$this->editPage( $title, 'Some text' ); |
| 1081 |
|
| 1082 |
$this->doApiRequestWithToken( [ |
| 1083 |
'action' => 'edit', |
| 1084 |
'title' => $title->getPrefixedText(), |
| 1085 |
'prependtext' => 'Alert: ', |
| 1086 |
'appendtext' => ' is nice', |
| 1087 |
'md5' => md5( 'Alert: is nice' ), |
| 1088 |
] ); |
| 1089 |
|
| 1090 |
$text = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title ) |
| 1091 |
->getContent()->getText(); |
| 1092 |
$this->assertSame( 'Alert: Some text is nice', $text ); |
| 1093 |
} |
| 1094 |
|
| 1095 |
public function testIncorrectMd5Text() { |
| 1096 |
$name = 'Help:' . ucfirst( __FUNCTION__ ); |
| 1097 |
|
| 1098 |
$this->expectApiErrorCode( 'badmd5' ); |
| 1099 |
|
| 1100 |
$this->doApiRequestWithToken( [ |
| 1101 |
'action' => 'edit', |
| 1102 |
'title' => $name, |
| 1103 |
'text' => 'Some text', |
| 1104 |
'md5' => md5( '' ), |
| 1105 |
] ); |
| 1106 |
} |
| 1107 |
|
| 1108 |
public function testIncorrectMd5PrependText() { |
| 1109 |
$name = 'Help:' . ucfirst( __FUNCTION__ ); |
| 1110 |
|
| 1111 |
$this->expectApiErrorCode( 'badmd5' ); |
| 1112 |
|
| 1113 |
$this->doApiRequestWithToken( [ |
| 1114 |
'action' => 'edit', |
| 1115 |
'title' => $name, |
| 1116 |
'prependtext' => 'Some ', |
| 1117 |
'appendtext' => 'text', |
| 1118 |
'md5' => md5( 'Some ' ), |
| 1119 |
] ); |
| 1120 |
} |
| 1121 |
|
| 1122 |
public function testIncorrectMd5AppendText() { |
| 1123 |
$name = 'Help:' . ucfirst( __FUNCTION__ ); |
| 1124 |
|
| 1125 |
$this->expectApiErrorCode( 'badmd5' ); |
| 1126 |
|
| 1127 |
$this->doApiRequestWithToken( [ |
| 1128 |
'action' => 'edit', |
| 1129 |
'title' => $name, |
| 1130 |
'prependtext' => 'Some ', |
| 1131 |
'appendtext' => 'text', |
| 1132 |
'md5' => md5( 'text' ), |
| 1133 |
] ); |
| 1134 |
} |
| 1135 |
|
| 1136 |
public function testCreateOnly() { |
| 1137 |
$title = Title::makeTitle( NS_HELP, 'TestCreateOnly' ); |
| 1138 |
|
| 1139 |
$this->expectApiErrorCode( 'articleexists' ); |
| 1140 |
|
| 1141 |
$this->editPage( $title, 'Some text' ); |
| 1142 |
$this->assertTrue( $title->exists( IDBAccessObject::READ_LATEST ) ); |
| 1143 |
|
| 1144 |
try { |
| 1145 |
$this->doApiRequestWithToken( [ |
| 1146 |
'action' => 'edit', |
| 1147 |
'title' => $title->getPrefixedText(), |
| 1148 |
'text' => 'Some more text', |
| 1149 |
'createonly' => '', |
| 1150 |
] ); |
| 1151 |
} finally { |
| 1152 |
|
| 1153 |
$text = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title ) |
| 1154 |
->getContent()->getText(); |
| 1155 |
|
| 1156 |
$this->assertSame( 'Some text', $text ); |
| 1157 |
} |
| 1158 |
} |
| 1159 |
|
| 1160 |
public function testNoCreate() { |
| 1161 |
$title = Title::makeTitle( NS_HELP, 'TestNoCreate' ); |
| 1162 |
|
| 1163 |
$this->expectApiErrorCode( 'missingtitle' ); |
| 1164 |
|
| 1165 |
$this->assertFalse( $title->exists( IDBAccessObject::READ_LATEST ) ); |
| 1166 |
|
| 1167 |
try { |
| 1168 |
$this->doApiRequestWithToken( [ |
| 1169 |
'action' => 'edit', |
| 1170 |
'title' => $title->getPrefixedText(), |
| 1171 |
'text' => 'Some text', |
| 1172 |
'nocreate' => '', |
| 1173 |
] ); |
| 1174 |
} finally { |
| 1175 |
$this->assertFalse( $title->exists( IDBAccessObject::READ_LATEST ) ); |
| 1176 |
} |
| 1177 |
} |
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
public function testAppendWithNonTextContentHandler() { |
| 1185 |
$name = 'MediaWiki:' . ucfirst( __FUNCTION__ ); |
| 1186 |
|
| 1187 |
$this->expectApiErrorCode( 'appendnotsupported' ); |
| 1188 |
|
| 1189 |
$this->setTemporaryHook( 'ContentHandlerDefaultModelFor', |
| 1190 |
static function ( Title $title, &$model ) use ( $name ) { |
| 1191 |
if ( $title->getPrefixedText() === $name ) { |
| 1192 |
$model = 'testing-nontext'; |
| 1193 |
} |
| 1194 |
return true; |
| 1195 |
} |
| 1196 |
); |
| 1197 |
|
| 1198 |
$this->doApiRequestWithToken( [ |
| 1199 |
'action' => 'edit', |
| 1200 |
'title' => $name, |
| 1201 |
'appendtext' => 'Some text', |
| 1202 |
] ); |
| 1203 |
} |
| 1204 |
|
| 1205 |
public function testAppendInMediaWikiNamespace() { |
| 1206 |
$title = Title::makeTitle( NS_MEDIAWIKI, 'TestAppendInMediaWikiNamespace' ); |
| 1207 |
|
| 1208 |
$this->assertFalse( $title->exists( IDBAccessObject::READ_LATEST ) ); |
| 1209 |
|
| 1210 |
$this->doApiRequestWithToken( [ |
| 1211 |
'action' => 'edit', |
| 1212 |
'title' => $title->getPrefixedText(), |
| 1213 |
'appendtext' => 'Some text', |
| 1214 |
] ); |
| 1215 |
|
| 1216 |
$this->assertTrue( $title->exists( IDBAccessObject::READ_LATEST ) ); |
| 1217 |
} |
| 1218 |
|
| 1219 |
public function testAppendInMediaWikiNamespaceWithSerializationError() { |
| 1220 |
$name = 'MediaWiki:' . ucfirst( __FUNCTION__ ); |
| 1221 |
|
| 1222 |
$this->expectApiErrorCode( 'parseerror' ); |
| 1223 |
|
| 1224 |
$this->setTemporaryHook( 'ContentHandlerDefaultModelFor', |
| 1225 |
static function ( Title $title, &$model ) use ( $name ) { |
| 1226 |
if ( $title->getPrefixedText() === $name ) { |
| 1227 |
$model = 'testing-serialize-error'; |
| 1228 |
} |
| 1229 |
return true; |
| 1230 |
} |
| 1231 |
); |
| 1232 |
|
| 1233 |
$this->doApiRequestWithToken( [ |
| 1234 |
'action' => 'edit', |
| 1235 |
'title' => $name, |
| 1236 |
'appendtext' => 'Some text', |
| 1237 |
] ); |
| 1238 |
} |
| 1239 |
|
| 1240 |
public function testAppendNewSection() { |
| 1241 |
$title = Title::makeTitle( NS_HELP, 'TestAppendNewSection' ); |
| 1242 |
|
| 1243 |
$this->editPage( $title, 'Initial content' ); |
| 1244 |
|
| 1245 |
$this->doApiRequestWithToken( [ |
| 1246 |
'action' => 'edit', |
| 1247 |
'title' => $title->getPrefixedText(), |
| 1248 |
'appendtext' => '== New section ==', |
| 1249 |
'section' => 'new', |
| 1250 |
] ); |
| 1251 |
|
| 1252 |
$text = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title ) |
| 1253 |
->getContent()->getText(); |
| 1254 |
|
| 1255 |
$this->assertSame( "Initial content\n\n== New section ==", $text ); |
| 1256 |
} |
| 1257 |
|
| 1258 |
public function testAppendNewSectionWithInvalidContentModel() { |
| 1259 |
$title = Title::makeTitle( NS_HELP, 'TestAppendNewSectionWithInvalidContentModel' ); |
| 1260 |
|
| 1261 |
$this->expectApiErrorCode( 'sectionsnotsupported' ); |
| 1262 |
|
| 1263 |
$this->editPage( $title, 'Initial content' ); |
| 1264 |
|
| 1265 |
$this->doApiRequestWithToken( [ |
| 1266 |
'action' => 'edit', |
| 1267 |
'title' => $title->getPrefixedText(), |
| 1268 |
'appendtext' => '== New section ==', |
| 1269 |
'section' => 'new', |
| 1270 |
'contentmodel' => 'text', |
| 1271 |
] ); |
| 1272 |
} |
| 1273 |
|
| 1274 |
public function testAppendNewSectionWithTitle() { |
| 1275 |
$title = Title::makeTitle( NS_HELP, 'TestAppendNewSectionWithTitle' ); |
| 1276 |
|
| 1277 |
$this->editPage( $title, 'Initial content' ); |
| 1278 |
|
| 1279 |
$this->doApiRequestWithToken( [ |
| 1280 |
'action' => 'edit', |
| 1281 |
'title' => $title->getPrefixedText(), |
| 1282 |
'sectiontitle' => 'My section', |
| 1283 |
'appendtext' => 'More content', |
| 1284 |
'section' => 'new', |
| 1285 |
] ); |
| 1286 |
|
| 1287 |
$page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title ); |
| 1288 |
|
| 1289 |
$this->assertSame( "Initial content\n\n== My section ==\n\nMore content", |
| 1290 |
$page->getContent()->getText() ); |
| 1291 |
$comment = $page->getRevisionRecord()->getComment(); |
| 1292 |
$this->assertInstanceOf( CommentStoreComment::class, $comment ); |
| 1293 |
$this->assertSame( '/* My section */ new section', $comment->text ); |
| 1294 |
} |
| 1295 |
|
| 1296 |
public function testAppendNewSectionWithSummary() { |
| 1297 |
$title = Title::makeTitle( NS_HELP, 'TestAppendNewSectionWithSummary' ); |
| 1298 |
|
| 1299 |
$this->editPage( $title, 'Initial content' ); |
| 1300 |
|
| 1301 |
$this->doApiRequestWithToken( [ |
| 1302 |
'action' => 'edit', |
| 1303 |
'title' => $title->getPrefixedText(), |
| 1304 |
'appendtext' => 'More content', |
| 1305 |
'section' => 'new', |
| 1306 |
'summary' => 'Add new section', |
| 1307 |
] ); |
| 1308 |
|
| 1309 |
$page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title ); |
| 1310 |
|
| 1311 |
$this->assertSame( "Initial content\n\n== Add new section ==\n\nMore content", |
| 1312 |
$page->getContent()->getText() ); |
| 1313 |
|
| 1314 |
$comment = $page->getRevisionRecord()->getComment(); |
| 1315 |
$this->assertInstanceOf( CommentStoreComment::class, $comment ); |
| 1316 |
$this->assertSame( '/* Add new section */ new section', $comment->text ); |
| 1317 |
} |
| 1318 |
|
| 1319 |
public function testAppendNewSectionWithTitleAndSummary() { |
| 1320 |
$title = Title::makeTitle( NS_HELP, 'TestAppendNewSectionWithTitleAndSummary' ); |
| 1321 |
|
| 1322 |
$this->editPage( $title, 'Initial content' ); |
| 1323 |
|
| 1324 |
$this->doApiRequestWithToken( [ |
| 1325 |
'action' => 'edit', |
| 1326 |
'title' => $title->getPrefixedText(), |
| 1327 |
'sectiontitle' => 'My section', |
| 1328 |
'appendtext' => 'More content', |
| 1329 |
'section' => 'new', |
| 1330 |
'summary' => 'Add new section', |
| 1331 |
] ); |
| 1332 |
|
| 1333 |
$page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title ); |
| 1334 |
|
| 1335 |
$this->assertSame( "Initial content\n\n== My section ==\n\nMore content", |
| 1336 |
$page->getContent()->getText() ); |
| 1337 |
$comment = $page->getRevisionRecord()->getComment(); |
| 1338 |
$this->assertInstanceOf( CommentStoreComment::class, $comment ); |
| 1339 |
$this->assertSame( 'Add new section', $comment->text ); |
| 1340 |
} |
| 1341 |
|
| 1342 |
public function testAppendToSection() { |
| 1343 |
$title = Title::makeTitle( NS_HELP, 'TestAppendToSection' ); |
| 1344 |
|
| 1345 |
$this->editPage( $title, "== Section 1 ==\n\nContent\n\n" . |
| 1346 |
"== Section 2 ==\n\nFascinating!" ); |
| 1347 |
|
| 1348 |
$this->doApiRequestWithToken( [ |
| 1349 |
'action' => 'edit', |
| 1350 |
'title' => $title->getPrefixedText(), |
| 1351 |
'appendtext' => ' and more content', |
| 1352 |
'section' => '1', |
| 1353 |
] ); |
| 1354 |
|
| 1355 |
$text = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title ) |
| 1356 |
->getContent()->getText(); |
| 1357 |
|
| 1358 |
$this->assertSame( "== Section 1 ==\n\nContent and more content\n\n" . |
| 1359 |
"== Section 2 ==\n\nFascinating!", $text ); |
| 1360 |
} |
| 1361 |
|
| 1362 |
public function testAppendToFirstSection() { |
| 1363 |
$title = Title::makeTitle( NS_HELP, 'TestAppendToFirstSection' ); |
| 1364 |
|
| 1365 |
$this->editPage( $title, "Content\n\n== Section 1 ==\n\nFascinating!" ); |
| 1366 |
|
| 1367 |
$this->doApiRequestWithToken( [ |
| 1368 |
'action' => 'edit', |
| 1369 |
'title' => $title->getPrefixedText(), |
| 1370 |
'appendtext' => ' and more content', |
| 1371 |
'section' => '0', |
| 1372 |
] ); |
| 1373 |
|
| 1374 |
$text = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title ) |
| 1375 |
->getContent()->getText(); |
| 1376 |
|
| 1377 |
$this->assertSame( "Content and more content\n\n== Section 1 ==\n\n" . |
| 1378 |
"Fascinating!", $text ); |
| 1379 |
} |
| 1380 |
|
| 1381 |
public function testAppendToNonexistentSection() { |
| 1382 |
$title = Title::makeTitle( NS_HELP, 'TestAppendToNonexistentSection' ); |
| 1383 |
|
| 1384 |
$this->expectApiErrorCode( 'nosuchsection' ); |
| 1385 |
|
| 1386 |
$this->editPage( $title, 'Content' ); |
| 1387 |
|
| 1388 |
try { |
| 1389 |
$this->doApiRequestWithToken( [ |
| 1390 |
'action' => 'edit', |
| 1391 |
'title' => $title->getPrefixedText(), |
| 1392 |
'appendtext' => ' and more content', |
| 1393 |
'section' => '1', |
| 1394 |
] ); |
| 1395 |
} finally { |
| 1396 |
$text = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title ) |
| 1397 |
->getContent()->getText(); |
| 1398 |
|
| 1399 |
$this->assertSame( 'Content', $text ); |
| 1400 |
} |
| 1401 |
} |
| 1402 |
|
| 1403 |
public function testEditMalformedSection() { |
| 1404 |
$title = Title::makeTitle( NS_HELP, 'TestEditMalformedSection' ); |
| 1405 |
|
| 1406 |
$this->expectApiErrorCode( 'invalidsection' ); |
| 1407 |
$this->editPage( $title, 'Content' ); |
| 1408 |
|
| 1409 |
try { |
| 1410 |
$this->doApiRequestWithToken( [ |
| 1411 |
'action' => 'edit', |
| 1412 |
'title' => $title->getPrefixedText(), |
| 1413 |
'text' => 'Different content', |
| 1414 |
'section' => 'It is unlikely that this is valid', |
| 1415 |
] ); |
| 1416 |
} finally { |
| 1417 |
$text = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title ) |
| 1418 |
->getContent()->getText(); |
| 1419 |
|
| 1420 |
$this->assertSame( 'Content', $text ); |
| 1421 |
} |
| 1422 |
} |
| 1423 |
|
| 1424 |
public function testEditWithStartTimestamp() { |
| 1425 |
$title = Title::makeTitle( NS_HELP, 'TestEditWithStartTimestamp' ); |
| 1426 |
$this->expectApiErrorCode( 'pagedeleted' ); |
| 1427 |
|
| 1428 |
$startTime = MWTimestamp::convert( TS_MW, time() - 1 ); |
| 1429 |
|
| 1430 |
$this->editPage( $title, 'Some text' ); |
| 1431 |
|
| 1432 |
$pageObj = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title ); |
| 1433 |
$this->deletePage( $pageObj ); |
| 1434 |
|
| 1435 |
$this->assertFalse( $pageObj->exists() ); |
| 1436 |
|
| 1437 |
try { |
| 1438 |
$this->doApiRequestWithToken( [ |
| 1439 |
'action' => 'edit', |
| 1440 |
'title' => $title->getPrefixedText(), |
| 1441 |
'text' => 'Different text', |
| 1442 |
'starttimestamp' => $startTime, |
| 1443 |
] ); |
| 1444 |
} finally { |
| 1445 |
$this->assertFalse( $pageObj->exists() ); |
| 1446 |
} |
| 1447 |
} |
| 1448 |
|
| 1449 |
public function testEditMinor() { |
| 1450 |
$title = Title::makeTitle( NS_HELP, 'TestEditMinor' ); |
| 1451 |
|
| 1452 |
$this->editPage( $title, 'Some text' ); |
| 1453 |
|
| 1454 |
$this->doApiRequestWithToken( [ |
| 1455 |
'action' => 'edit', |
| 1456 |
'title' => $title->getPrefixedText(), |
| 1457 |
'text' => 'Different text', |
| 1458 |
'minor' => '', |
| 1459 |
] ); |
| 1460 |
|
| 1461 |
$revisionStore = $this->getServiceContainer()->getRevisionStore(); |
| 1462 |
$revision = $revisionStore->getRevisionByTitle( $title ); |
| 1463 |
$this->assertTrue( $revision->isMinor() ); |
| 1464 |
} |
| 1465 |
|
| 1466 |
public function testEditRecreate() { |
| 1467 |
$title = Title::makeTitle( NS_HELP, 'TestEditRecreate' ); |
| 1468 |
|
| 1469 |
$startTime = MWTimestamp::convert( TS_MW, time() - 1 ); |
| 1470 |
|
| 1471 |
$this->editPage( $title, 'Some text' ); |
| 1472 |
|
| 1473 |
$pageObj = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title ); |
| 1474 |
$this->deletePage( $pageObj ); |
| 1475 |
|
| 1476 |
$this->assertFalse( $pageObj->exists() ); |
| 1477 |
|
| 1478 |
$this->doApiRequestWithToken( [ |
| 1479 |
'action' => 'edit', |
| 1480 |
'title' => $title->getPrefixedText(), |
| 1481 |
'text' => 'Different text', |
| 1482 |
'starttimestamp' => $startTime, |
| 1483 |
'recreate' => '', |
| 1484 |
] ); |
| 1485 |
|
| 1486 |
$this->assertTrue( $title->exists( IDBAccessObject::READ_LATEST ) ); |
| 1487 |
} |
| 1488 |
|
| 1489 |
public function testEditWatch() { |
| 1490 |
$title = Title::makeTitle( NS_HELP, 'TestEditWatch' ); |
| 1491 |
$user = $this->getTestSysop()->getUser(); |
| 1492 |
$watchlistManager = $this->getServiceContainer()->getWatchlistManager(); |
| 1493 |
|
| 1494 |
$this->doApiRequestWithToken( [ |
| 1495 |
'action' => 'edit', |
| 1496 |
'title' => $title->getPrefixedText(), |
| 1497 |
'text' => 'Some text', |
| 1498 |
'watch' => '', |
| 1499 |
'watchlistexpiry' => '99990123000000', |
| 1500 |
] ); |
| 1501 |
|
| 1502 |
$this->assertTrue( $title->exists( IDBAccessObject::READ_LATEST ) ); |
| 1503 |
$this->assertTrue( $watchlistManager->isWatched( $user, $title ) ); |
| 1504 |
$this->assertTrue( $watchlistManager->isTempWatched( $user, $title ) ); |
| 1505 |
} |
| 1506 |
|
| 1507 |
public function testEditUnwatch() { |
| 1508 |
$title = Title::makeTitle( NS_HELP, 'TestEditUnwatch' ); |
| 1509 |
$user = $this->getTestSysop()->getUser(); |
| 1510 |
|
| 1511 |
$watchlistManager = $this->getServiceContainer()->getWatchlistManager(); |
| 1512 |
$watchlistManager->addWatch( $user, $title ); |
| 1513 |
|
| 1514 |
$this->assertFalse( $title->exists() ); |
| 1515 |
$this->assertTrue( $watchlistManager->isWatched( $user, $title ) ); |
| 1516 |
|
| 1517 |
$this->doApiRequestWithToken( [ |
| 1518 |
'action' => 'edit', |
| 1519 |
'title' => $title->getPrefixedText(), |
| 1520 |
'text' => 'Some text', |
| 1521 |
'unwatch' => '', |
| 1522 |
] ); |
| 1523 |
|
| 1524 |
$this->assertTrue( $title->exists( IDBAccessObject::READ_LATEST ) ); |
| 1525 |
$this->assertFalse( $watchlistManager->isWatched( $user, $title ) ); |
| 1526 |
} |
| 1527 |
|
| 1528 |
public function testEditWithTag() { |
| 1529 |
$name = 'Help:' . ucfirst( __FUNCTION__ ); |
| 1530 |
|
| 1531 |
$this->getServiceContainer()->getChangeTagsStore()->defineTag( 'custom tag' ); |
| 1532 |
|
| 1533 |
$revId = $this->doApiRequestWithToken( [ |
| 1534 |
'action' => 'edit', |
| 1535 |
'title' => $name, |
| 1536 |
'text' => 'Some text', |
| 1537 |
'tags' => 'custom tag', |
| 1538 |
] )[0]['edit']['newrevid']; |
| 1539 |
|
| 1540 |
$this->assertSame( 'custom tag', $this->getDb()->newSelectQueryBuilder() |
| 1541 |
->select( 'ctd_name' ) |
| 1542 |
->from( 'change_tag' ) |
| 1543 |
->join( 'change_tag_def', null, 'ctd_id = ct_tag_id' ) |
| 1544 |
->where( [ 'ct_rev_id' => $revId ] ) |
| 1545 |
->caller( __METHOD__ )->fetchField() ); |
| 1546 |
} |
| 1547 |
|
| 1548 |
public function testEditWithoutTagPermission() { |
| 1549 |
$title = Title::makeTitle( NS_HELP, 'TestEditWithoutTagPermission' ); |
| 1550 |
|
| 1551 |
$this->expectApiErrorCode( 'tags-apply-no-permission' ); |
| 1552 |
|
| 1553 |
$this->assertFalse( $title->exists( IDBAccessObject::READ_LATEST ) ); |
| 1554 |
|
| 1555 |
$this->getServiceContainer()->getChangeTagsStore()->defineTag( 'custom tag' ); |
| 1556 |
$this->overrideConfigValue( |
| 1557 |
MainConfigNames::RevokePermissions, |
| 1558 |
[ 'user' => [ 'applychangetags' => true ] ] |
| 1559 |
); |
| 1560 |
|
| 1561 |
try { |
| 1562 |
$this->doApiRequestWithToken( [ |
| 1563 |
'action' => 'edit', |
| 1564 |
'title' => $title->getPrefixedText(), |
| 1565 |
'text' => 'Some text', |
| 1566 |
'tags' => 'custom tag', |
| 1567 |
] ); |
| 1568 |
} finally { |
| 1569 |
$this->assertFalse( $title->exists( IDBAccessObject::READ_LATEST ) ); |
| 1570 |
} |
| 1571 |
} |
| 1572 |
|
| 1573 |
public function testEditAbortedByEditPageHookWithResult() { |
| 1574 |
$title = Title::makeTitle( NS_HELP, 'TestEditAbortedByEditPageHookWithResult' ); |
| 1575 |
|
| 1576 |
$this->setTemporaryHook( 'EditFilterMergedContent', |
| 1577 |
static function ( $unused1, $unused2, Status $status ) { |
| 1578 |
$status->statusData = [ 'msg' => 'A message for you!' ]; |
| 1579 |
return false; |
| 1580 |
} ); |
| 1581 |
|
| 1582 |
$res = $this->doApiRequestWithToken( [ |
| 1583 |
'action' => 'edit', |
| 1584 |
'title' => $title->getPrefixedText(), |
| 1585 |
'text' => 'Some text', |
| 1586 |
] ); |
| 1587 |
|
| 1588 |
$this->assertFalse( $title->exists( IDBAccessObject::READ_LATEST ) ); |
| 1589 |
$this->assertSame( [ 'edit' => [ 'msg' => 'A message for you!', |
| 1590 |
'result' => 'Failure' ] ], $res[0] ); |
| 1591 |
} |
| 1592 |
|
| 1593 |
public function testEditAbortedByEditPageHookWithNoResult() { |
| 1594 |
$title = Title::makeTitle( NS_HELP, 'TestEditAbortedByEditPageHookWithNoResult' ); |
| 1595 |
|
| 1596 |
$this->expectApiErrorCode( 'hookaborted' ); |
| 1597 |
|
| 1598 |
$this->setTemporaryHook( 'EditFilterMergedContent', |
| 1599 |
static function () { |
| 1600 |
return false; |
| 1601 |
} |
| 1602 |
); |
| 1603 |
|
| 1604 |
try { |
| 1605 |
$this->doApiRequestWithToken( [ |
| 1606 |
'action' => 'edit', |
| 1607 |
'title' => $title->getPrefixedText(), |
| 1608 |
'text' => 'Some text', |
| 1609 |
] ); |
| 1610 |
} finally { |
| 1611 |
$this->assertFalse( $title->exists( IDBAccessObject::READ_LATEST ) ); |
| 1612 |
} |
| 1613 |
} |
| 1614 |
|
| 1615 |
public function testEditWhileBlocked() { |
| 1616 |
$name = 'Help:' . ucfirst( __FUNCTION__ ); |
| 1617 |
|
| 1618 |
$blockStore = $this->getServiceContainer()->getDatabaseBlockStore(); |
| 1619 |
$this->assertNull( $blockStore->newFromTarget( '127.0.0.1' ) ); |
| 1620 |
|
| 1621 |
$user = $this->getTestSysop()->getUser(); |
| 1622 |
$block = new DatabaseBlock( [ |
| 1623 |
'address' => $user->getName(), |
| 1624 |
'by' => $user, |
| 1625 |
'reason' => 'Capriciousness', |
| 1626 |
'timestamp' => '19370101000000', |
| 1627 |
'expiry' => 'infinity', |
| 1628 |
'enableAutoblock' => true, |
| 1629 |
] ); |
| 1630 |
$blockStore->insertBlock( $block ); |
| 1631 |
|
| 1632 |
try { |
| 1633 |
$this->doApiRequestWithToken( [ |
| 1634 |
'action' => 'edit', |
| 1635 |
'title' => $name, |
| 1636 |
'text' => 'Some text', |
| 1637 |
] ); |
| 1638 |
$this->fail( 'Expected exception not thrown' ); |
| 1639 |
} catch ( ApiUsageException $ex ) { |
| 1640 |
$this->assertApiErrorCode( 'blocked', $ex ); |
| 1641 |
$this->assertNotNull( $blockStore->newFromTarget( '127.0.0.1' ), 'Autoblock spread' ); |
| 1642 |
} |
| 1643 |
} |
| 1644 |
|
| 1645 |
public function testEditWhileReadOnly() { |
| 1646 |
$name = 'Help:' . ucfirst( __FUNCTION__ ); |
| 1647 |
|
| 1648 |
|
| 1649 |
$user = $this->getTestSysop()->getUser(); |
| 1650 |
$this->expectApiErrorCode( 'readonly' ); |
| 1651 |
|
| 1652 |
$svc = $this->getServiceContainer()->getReadOnlyMode(); |
| 1653 |
$svc->setReason( "Read-only for testing" ); |
| 1654 |
|
| 1655 |
try { |
| 1656 |
$this->doApiRequestWithToken( [ |
| 1657 |
'action' => 'edit', |
| 1658 |
'title' => $name, |
| 1659 |
'text' => 'Some text', |
| 1660 |
], null, $user ); |
| 1661 |
} finally { |
| 1662 |
$svc->setReason( false ); |
| 1663 |
} |
| 1664 |
} |
| 1665 |
|
| 1666 |
public function testCreateImageRedirectAnon() { |
| 1667 |
$this->disableAutoCreateTempUser(); |
| 1668 |
$name = 'File:' . ucfirst( __FUNCTION__ ); |
| 1669 |
|
| 1670 |
$this->expectApiErrorCode( 'noimageredirect-anon' ); |
| 1671 |
|
| 1672 |
$this->doApiRequestWithToken( [ |
| 1673 |
'action' => 'edit', |
| 1674 |
'title' => $name, |
| 1675 |
'text' => '#REDIRECT [[File:Other file.png]]', |
| 1676 |
], null, new User() ); |
| 1677 |
} |
| 1678 |
|
| 1679 |
public function testCreateImageRedirectLoggedIn() { |
| 1680 |
$name = 'File:' . ucfirst( __FUNCTION__ ); |
| 1681 |
|
| 1682 |
$this->expectApiErrorCode( 'noimageredirect' ); |
| 1683 |
|
| 1684 |
$this->overrideConfigValue( |
| 1685 |
MainConfigNames::RevokePermissions, |
| 1686 |
[ 'user' => [ 'upload' => true ] ] |
| 1687 |
); |
| 1688 |
|
| 1689 |
$this->doApiRequestWithToken( [ |
| 1690 |
'action' => 'edit', |
| 1691 |
'title' => $name, |
| 1692 |
'text' => '#REDIRECT [[File:Other file.png]]', |
| 1693 |
] ); |
| 1694 |
} |
| 1695 |
|
| 1696 |
public function testTooBigEdit() { |
| 1697 |
$name = 'Help:' . ucfirst( __FUNCTION__ ); |
| 1698 |
|
| 1699 |
$this->expectApiErrorCode( 'contenttoobig' ); |
| 1700 |
|
| 1701 |
$this->overrideConfigValue( MainConfigNames::MaxArticleSize, 1 ); |
| 1702 |
|
| 1703 |
$text = str_repeat( '!', 1025 ); |
| 1704 |
|
| 1705 |
$this->doApiRequestWithToken( [ |
| 1706 |
'action' => 'edit', |
| 1707 |
'title' => $name, |
| 1708 |
'text' => $text, |
| 1709 |
] ); |
| 1710 |
} |
| 1711 |
|
| 1712 |
public function testProhibitedAnonymousEdit() { |
| 1713 |
$name = 'Help:' . ucfirst( __FUNCTION__ ); |
| 1714 |
|
| 1715 |
$this->expectApiErrorCode( 'permissiondenied' ); |
| 1716 |
|
| 1717 |
$this->overrideConfigValue( |
| 1718 |
MainConfigNames::RevokePermissions, |
| 1719 |
[ '*' => [ 'edit' => true ] ] |
| 1720 |
); |
| 1721 |
|
| 1722 |
$this->doApiRequestWithToken( [ |
| 1723 |
'action' => 'edit', |
| 1724 |
'title' => $name, |
| 1725 |
'text' => 'Some text', |
| 1726 |
], null, new User() ); |
| 1727 |
} |
| 1728 |
|
| 1729 |
public function testProhibitedChangeContentModel() { |
| 1730 |
$name = 'Help:' . ucfirst( __FUNCTION__ ); |
| 1731 |
|
| 1732 |
$this->expectApiErrorCode( 'cantchangecontentmodel' ); |
| 1733 |
|
| 1734 |
$this->overrideConfigValue( |
| 1735 |
MainConfigNames::RevokePermissions, |
| 1736 |
[ 'user' => [ 'editcontentmodel' => true ] ] |
| 1737 |
); |
| 1738 |
|
| 1739 |
$this->doApiRequestWithToken( [ |
| 1740 |
'action' => 'edit', |
| 1741 |
'title' => $name, |
| 1742 |
'text' => 'Some text', |
| 1743 |
'contentmodel' => 'json', |
| 1744 |
] ); |
| 1745 |
} |
| 1746 |
|
| 1747 |
public function testMidEditContentModelMismatch() { |
| 1748 |
$title = Title::makeTitle( NS_HELP, 'TestMidEditContentModelMismatch' ); |
| 1749 |
|
| 1750 |
$page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title ); |
| 1751 |
|
| 1752 |
|
| 1753 |
$page->doUserEditContent( |
| 1754 |
new WikitextContent( "Foo" ), |
| 1755 |
$this->getTestSysop()->getUser(), |
| 1756 |
"testing 1", |
| 1757 |
EDIT_NEW |
| 1758 |
); |
| 1759 |
$this->forceRevisionDate( $page, '20120101000000' ); |
| 1760 |
$baseId = $page->getRevisionRecord()->getId(); |
| 1761 |
|
| 1762 |
|
| 1763 |
|
| 1764 |
|
| 1765 |
$page->doUserEditContent( |
| 1766 |
new JavaScriptContent( "Bar" ), |
| 1767 |
$this->getTestUser()->getUser(), |
| 1768 |
"testing 2", |
| 1769 |
EDIT_UPDATE |
| 1770 |
); |
| 1771 |
$this->forceRevisionDate( $page, '20120101020202' ); |
| 1772 |
|
| 1773 |
|
| 1774 |
|
| 1775 |
try { |
| 1776 |
$this->doApiRequestWithToken( [ |
| 1777 |
'action' => 'edit', |
| 1778 |
'title' => $title->getPrefixedText(), |
| 1779 |
'text' => 'different content models!', |
| 1780 |
'baserevid' => $baseId, |
| 1781 |
] ); |
| 1782 |
$this->fail( "Should have raised an ApiUsageException" ); |
| 1783 |
} catch ( ApiUsageException $e ) { |
| 1784 |
$this->assertApiErrorCode( 'contentmodel-mismatch', $e ); |
| 1785 |
} |
| 1786 |
} |
| 1787 |
} |