2020-03-02 12:28:10 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace MediaWiki\Tests\Rest\Handler;
|
|
|
|
|
|
|
|
|
|
use ApiUsageException;
|
2020-03-13 13:47:08 +00:00
|
|
|
use FormatJson;
|
2023-09-20 07:54:42 +00:00
|
|
|
use MediaWiki\Config\HashConfig;
|
2022-08-24 12:55:03 +00:00
|
|
|
use MediaWiki\Languages\LanguageNameUtils;
|
2023-06-19 19:21:47 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
2022-12-09 12:28:41 +00:00
|
|
|
use MediaWiki\Parser\MagicWordFactory;
|
2022-05-27 16:38:32 +00:00
|
|
|
use MediaWiki\Parser\Parsoid\ParsoidParserFactory;
|
2020-03-02 12:28:10 +00:00
|
|
|
use MediaWiki\Rest\Handler\UpdateHandler;
|
|
|
|
|
use MediaWiki\Rest\LocalizedHttpException;
|
|
|
|
|
use MediaWiki\Rest\RequestData;
|
2022-04-14 20:54:04 +00:00
|
|
|
use MediaWiki\Revision\MutableRevisionRecord;
|
2020-03-02 12:28:10 +00:00
|
|
|
use MediaWiki\Revision\RevisionLookup;
|
2022-04-14 20:54:04 +00:00
|
|
|
use MediaWiki\Revision\SlotRecord;
|
2023-08-25 12:29:41 +00:00
|
|
|
use MediaWiki\Status\Status;
|
2021-05-03 20:38:48 +00:00
|
|
|
use MediaWiki\Tests\Unit\DummyServicesTrait;
|
2023-03-01 20:33:26 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2022-08-24 12:55:03 +00:00
|
|
|
use MediaWiki\Title\TitleFactory;
|
2021-01-13 17:34:01 +00:00
|
|
|
use MockTitleTrait;
|
2022-08-24 12:55:03 +00:00
|
|
|
use ParserFactory;
|
2020-03-02 12:28:10 +00:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
|
|
|
use Wikimedia\Message\MessageValue;
|
|
|
|
|
use Wikimedia\Message\ParamType;
|
|
|
|
|
use Wikimedia\Message\ScalarParam;
|
2022-08-24 12:55:03 +00:00
|
|
|
use Wikimedia\UUID\GlobalIdGenerator;
|
2020-03-02 12:28:10 +00:00
|
|
|
use WikitextContent;
|
2020-03-13 13:47:08 +00:00
|
|
|
use WikitextContentHandler;
|
2020-03-02 12:28:10 +00:00
|
|
|
|
|
|
|
|
/**
|
2023-08-01 00:57:54 +00:00
|
|
|
* @group Database
|
2020-03-02 12:28:10 +00:00
|
|
|
* @covers \MediaWiki\Rest\Handler\UpdateHandler
|
|
|
|
|
*/
|
2020-03-26 19:53:08 +00:00
|
|
|
class UpdateHandlerTest extends \MediaWikiLangTestCase {
|
2020-03-02 12:28:10 +00:00
|
|
|
use ActionModuleBasedHandlerTestTrait;
|
2021-05-03 20:38:48 +00:00
|
|
|
use DummyServicesTrait;
|
2021-01-13 17:34:01 +00:00
|
|
|
use MockTitleTrait;
|
2020-03-02 12:28:10 +00:00
|
|
|
|
2020-03-12 12:54:51 +00:00
|
|
|
private function newHandler( $resultData, $throwException = null, $csrfSafe = false ) {
|
2020-03-02 12:28:10 +00:00
|
|
|
$config = new HashConfig( [
|
2023-06-19 19:21:47 +00:00
|
|
|
MainConfigNames::RightsUrl => 'https://creativecommons.org/licenses/by-sa/4.0/',
|
|
|
|
|
MainConfigNames::RightsText => 'CC-BY-SA 4.0'
|
2020-03-02 12:28:10 +00:00
|
|
|
] );
|
|
|
|
|
|
2022-12-14 23:40:12 +00:00
|
|
|
$wikitextContentHandler = new WikitextContentHandler(
|
|
|
|
|
CONTENT_MODEL_WIKITEXT,
|
|
|
|
|
$this->createMock( TitleFactory::class ),
|
|
|
|
|
$this->createMock( ParserFactory::class ),
|
|
|
|
|
$this->createMock( GlobalIdGenerator::class ),
|
|
|
|
|
$this->createMock( LanguageNameUtils::class ),
|
2022-05-27 16:38:32 +00:00
|
|
|
$this->createMock( MagicWordFactory::class ),
|
|
|
|
|
$this->createMock( ParsoidParserFactory::class )
|
2022-12-14 23:40:12 +00:00
|
|
|
);
|
2020-03-02 12:28:10 +00:00
|
|
|
|
2022-12-14 23:40:12 +00:00
|
|
|
// Only wikitext is defined, returns specific handler instance
|
|
|
|
|
$contentHandlerFactory = $this->getDummyContentHandlerFactory(
|
|
|
|
|
[ CONTENT_MODEL_WIKITEXT => $wikitextContentHandler ]
|
|
|
|
|
);
|
2020-03-13 13:47:08 +00:00
|
|
|
|
2021-05-03 20:38:48 +00:00
|
|
|
// DummyServicesTrait::getDummyMediaWikiTitleCodec
|
|
|
|
|
$titleCodec = $this->getDummyMediaWikiTitleCodec();
|
2020-03-02 12:28:10 +00:00
|
|
|
|
|
|
|
|
/** @var RevisionLookup|MockObject $revisionLookup */
|
2020-03-13 13:47:08 +00:00
|
|
|
$revisionLookup = $this->createNoOpMock(
|
|
|
|
|
RevisionLookup::class,
|
|
|
|
|
[ 'getRevisionById', 'getRevisionByTitle' ]
|
|
|
|
|
);
|
2020-03-02 12:28:10 +00:00
|
|
|
$revisionLookup->method( 'getRevisionById' )
|
|
|
|
|
->willReturnCallback( function ( $id ) {
|
|
|
|
|
$title = $this->makeMockTitle( __CLASS__ );
|
|
|
|
|
$rev = new MutableRevisionRecord( $title );
|
|
|
|
|
$rev->setId( $id );
|
|
|
|
|
$rev->setContent( SlotRecord::MAIN, new WikitextContent( "Content of revision $id" ) );
|
2021-03-19 13:10:25 +00:00
|
|
|
$rev->setTimestamp( '2020-01-01T01:02:03Z' );
|
2020-03-02 12:28:10 +00:00
|
|
|
return $rev;
|
|
|
|
|
} );
|
2020-03-13 13:47:08 +00:00
|
|
|
$revisionLookup->method( 'getRevisionByTitle' )
|
2021-02-07 13:10:36 +00:00
|
|
|
->willReturnCallback( static function ( $title ) {
|
2020-03-13 13:47:08 +00:00
|
|
|
$rev = new MutableRevisionRecord( Title::castFromLinkTarget( $title ) );
|
|
|
|
|
$rev->setId( 1234 );
|
|
|
|
|
$rev->setContent( SlotRecord::MAIN, new WikitextContent( "Current content of $title" ) );
|
2021-03-19 13:10:25 +00:00
|
|
|
$rev->setTimestamp( '2020-01-01T01:02:03Z' );
|
2020-03-13 13:47:08 +00:00
|
|
|
return $rev;
|
|
|
|
|
} );
|
2020-03-02 12:28:10 +00:00
|
|
|
|
|
|
|
|
$handler = new UpdateHandler(
|
|
|
|
|
$config,
|
|
|
|
|
$contentHandlerFactory,
|
|
|
|
|
$titleCodec,
|
|
|
|
|
$titleCodec,
|
|
|
|
|
$revisionLookup
|
|
|
|
|
);
|
2020-03-12 12:54:51 +00:00
|
|
|
|
|
|
|
|
$apiMain = $this->getApiMain( $csrfSafe );
|
|
|
|
|
$dummyModule = $this->getDummyApiModule( $apiMain, 'edit', $resultData, $throwException );
|
|
|
|
|
|
|
|
|
|
$handler->setApiMain( $apiMain );
|
2020-03-02 12:28:10 +00:00
|
|
|
$handler->overrideActionModule(
|
|
|
|
|
'edit',
|
|
|
|
|
'action',
|
2020-03-12 12:54:51 +00:00
|
|
|
$dummyModule
|
2020-03-02 12:28:10 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return $handler;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-23 11:36:19 +00:00
|
|
|
public static function provideExecute() {
|
2020-03-12 12:54:51 +00:00
|
|
|
yield "create with token" => [
|
2020-03-02 12:28:10 +00:00
|
|
|
[ // Request data received by UpdateHandler
|
2020-03-12 12:54:51 +00:00
|
|
|
'method' => 'PUT',
|
2020-03-02 12:28:10 +00:00
|
|
|
'pathParams' => [ 'title' => 'Foo' ],
|
|
|
|
|
'headers' => [
|
|
|
|
|
'Content-Type' => 'application/json',
|
|
|
|
|
],
|
|
|
|
|
'bodyContents' => json_encode( [
|
|
|
|
|
'token' => 'TOKEN',
|
|
|
|
|
'source' => 'Lorem Ipsum',
|
|
|
|
|
'comment' => 'Testing'
|
|
|
|
|
] ),
|
|
|
|
|
],
|
|
|
|
|
[ // Fake request expected to be passed into ApiEditPage
|
|
|
|
|
'title' => 'Foo',
|
|
|
|
|
'text' => 'Lorem Ipsum',
|
|
|
|
|
'summary' => 'Testing',
|
|
|
|
|
'createonly' => '1',
|
|
|
|
|
'token' => 'TOKEN',
|
|
|
|
|
],
|
|
|
|
|
[ // Mock response returned by ApiEditPage
|
|
|
|
|
"edit" => [
|
2020-03-06 15:53:01 +00:00
|
|
|
"new" => true,
|
2020-03-02 12:28:10 +00:00
|
|
|
"result" => "Success",
|
|
|
|
|
"pageid" => 94542,
|
2020-07-22 19:12:00 +00:00
|
|
|
"title" => "Foo",
|
2020-03-02 12:28:10 +00:00
|
|
|
"contentmodel" => "wikitext",
|
|
|
|
|
"oldrevid" => 0,
|
|
|
|
|
"newrevid" => 371707,
|
|
|
|
|
"newtimestamp" => "2018-12-18T16:59:42Z",
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
[ // Response expected to be generated by UpdateHandler
|
|
|
|
|
'id' => 94542,
|
2021-05-03 20:38:48 +00:00
|
|
|
'title' => 'Foo',
|
|
|
|
|
'key' => 'Foo',
|
2020-03-02 12:28:10 +00:00
|
|
|
'content_model' => 'wikitext',
|
|
|
|
|
'latest' => [
|
|
|
|
|
'id' => 371707,
|
|
|
|
|
'timestamp' => "2018-12-18T16:59:42Z"
|
|
|
|
|
],
|
|
|
|
|
'license' => [
|
|
|
|
|
'url' => 'https://creativecommons.org/licenses/by-sa/4.0/',
|
|
|
|
|
'title' => 'CC-BY-SA 4.0'
|
|
|
|
|
],
|
|
|
|
|
'source' => 'Content of revision 371707'
|
2020-03-12 12:54:51 +00:00
|
|
|
],
|
|
|
|
|
false
|
2020-03-02 12:28:10 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
yield "create with model" => [
|
|
|
|
|
[ // Request data received by UpdateHandler
|
|
|
|
|
'method' => 'POST',
|
|
|
|
|
'pathParams' => [ 'title' => 'Foo' ],
|
|
|
|
|
'headers' => [
|
|
|
|
|
'Content-Type' => 'application/json',
|
|
|
|
|
],
|
|
|
|
|
'bodyContents' => json_encode( [
|
|
|
|
|
'token' => 'TOKEN',
|
|
|
|
|
'source' => 'Lorem Ipsum',
|
|
|
|
|
'comment' => 'Testing',
|
|
|
|
|
'content_model' => CONTENT_MODEL_WIKITEXT,
|
|
|
|
|
] ),
|
|
|
|
|
],
|
|
|
|
|
[ // Fake request expected to be passed into ApiEditPage
|
|
|
|
|
'title' => 'Foo',
|
|
|
|
|
'text' => 'Lorem Ipsum',
|
|
|
|
|
'summary' => 'Testing',
|
|
|
|
|
'contentmodel' => 'wikitext',
|
|
|
|
|
'createonly' => '1',
|
|
|
|
|
'token' => 'TOKEN',
|
|
|
|
|
],
|
|
|
|
|
[ // Mock response returned by ApiEditPage
|
|
|
|
|
"edit" => [
|
2020-03-06 15:53:01 +00:00
|
|
|
"new" => true,
|
2020-03-02 12:28:10 +00:00
|
|
|
"result" => "Success",
|
|
|
|
|
"pageid" => 94542,
|
2020-07-22 19:12:00 +00:00
|
|
|
"title" => "Foo",
|
2020-03-02 12:28:10 +00:00
|
|
|
"contentmodel" => "wikitext",
|
|
|
|
|
"oldrevid" => 0,
|
|
|
|
|
"newrevid" => 371707,
|
|
|
|
|
"newtimestamp" => "2018-12-18T16:59:42Z",
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
[ // Response expected to be generated by UpdateHandler
|
|
|
|
|
'id' => 94542,
|
2021-05-03 20:38:48 +00:00
|
|
|
'title' => 'Foo',
|
|
|
|
|
'key' => 'Foo',
|
2020-03-02 12:28:10 +00:00
|
|
|
'content_model' => 'wikitext',
|
|
|
|
|
'latest' => [
|
|
|
|
|
'id' => 371707,
|
|
|
|
|
'timestamp' => "2018-12-18T16:59:42Z"
|
|
|
|
|
],
|
|
|
|
|
'license' => [
|
|
|
|
|
'url' => 'https://creativecommons.org/licenses/by-sa/4.0/',
|
|
|
|
|
'title' => 'CC-BY-SA 4.0'
|
|
|
|
|
],
|
|
|
|
|
'source' => 'Content of revision 371707'
|
2020-03-12 12:54:51 +00:00
|
|
|
],
|
|
|
|
|
false
|
2020-03-02 12:28:10 +00:00
|
|
|
];
|
|
|
|
|
|
2020-03-12 12:54:51 +00:00
|
|
|
yield "update with token" => [
|
2020-03-02 12:28:10 +00:00
|
|
|
[ // Request data received by UpdateHandler
|
2020-03-12 12:54:51 +00:00
|
|
|
'method' => 'PUT',
|
2020-03-02 12:28:10 +00:00
|
|
|
'pathParams' => [ 'title' => 'foo bar' ],
|
|
|
|
|
'headers' => [
|
|
|
|
|
'Content-Type' => 'application/json',
|
|
|
|
|
],
|
|
|
|
|
'bodyContents' => json_encode( [
|
|
|
|
|
'token' => 'TOKEN',
|
|
|
|
|
'source' => 'Lorem Ipsum',
|
|
|
|
|
'comment' => 'Testing',
|
|
|
|
|
'latest' => [ 'id' => 789123 ],
|
|
|
|
|
] ),
|
|
|
|
|
],
|
|
|
|
|
[ // Fake request expected to be passed into ApiEditPage
|
|
|
|
|
'title' => 'foo bar',
|
|
|
|
|
'text' => 'Lorem Ipsum',
|
|
|
|
|
'summary' => 'Testing',
|
|
|
|
|
'nocreate' => '1',
|
|
|
|
|
'baserevid' => '789123',
|
|
|
|
|
'token' => 'TOKEN',
|
|
|
|
|
],
|
|
|
|
|
[ // Mock response returned by ApiEditPage
|
|
|
|
|
"edit" => [
|
|
|
|
|
"result" => "Success",
|
|
|
|
|
"pageid" => 94542,
|
2020-07-22 19:12:00 +00:00
|
|
|
"title" => "Foo_bar",
|
2020-03-02 12:28:10 +00:00
|
|
|
"contentmodel" => "wikitext",
|
|
|
|
|
"oldrevid" => 371705,
|
|
|
|
|
"newrevid" => 371707,
|
|
|
|
|
"newtimestamp" => "2018-12-18T16:59:42Z",
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
[ // Response expected to be generated by UpdateHandler
|
|
|
|
|
'id' => 94542,
|
|
|
|
|
'content_model' => 'wikitext',
|
2021-05-03 20:38:48 +00:00
|
|
|
'title' => 'Foo bar',
|
|
|
|
|
'key' => 'Foo_bar',
|
2020-03-02 12:28:10 +00:00
|
|
|
'latest' => [
|
|
|
|
|
'id' => 371707,
|
|
|
|
|
'timestamp' => "2018-12-18T16:59:42Z"
|
|
|
|
|
],
|
|
|
|
|
'license' => [
|
|
|
|
|
'url' => 'https://creativecommons.org/licenses/by-sa/4.0/',
|
|
|
|
|
'title' => 'CC-BY-SA 4.0'
|
|
|
|
|
],
|
|
|
|
|
'source' => 'Content of revision 371707'
|
2020-03-12 12:54:51 +00:00
|
|
|
],
|
|
|
|
|
false
|
2020-03-02 12:28:10 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
yield "update with model" => [
|
|
|
|
|
[ // Request data received by UpdateHandler
|
|
|
|
|
'method' => 'POST',
|
|
|
|
|
'pathParams' => [ 'title' => 'foo bar' ],
|
|
|
|
|
'headers' => [
|
|
|
|
|
'Content-Type' => 'application/json',
|
|
|
|
|
],
|
|
|
|
|
'bodyContents' => json_encode( [
|
|
|
|
|
'source' => 'Lorem Ipsum',
|
|
|
|
|
'comment' => 'Testing',
|
|
|
|
|
'content_model' => CONTENT_MODEL_WIKITEXT,
|
|
|
|
|
'latest' => [ 'id' => 789123 ],
|
|
|
|
|
] ),
|
|
|
|
|
],
|
|
|
|
|
[ // Fake request expected to be passed into ApiEditPage
|
|
|
|
|
'title' => 'foo bar',
|
|
|
|
|
'text' => 'Lorem Ipsum',
|
|
|
|
|
'summary' => 'Testing',
|
|
|
|
|
'contentmodel' => 'wikitext',
|
|
|
|
|
'nocreate' => '1',
|
|
|
|
|
'baserevid' => '789123',
|
2020-03-12 12:54:51 +00:00
|
|
|
'token' => '+\\',
|
2020-03-02 12:28:10 +00:00
|
|
|
],
|
|
|
|
|
[ // Mock response returned by ApiEditPage
|
|
|
|
|
"edit" => [
|
|
|
|
|
"result" => "Success",
|
|
|
|
|
"pageid" => 94542,
|
2020-07-22 19:12:00 +00:00
|
|
|
"title" => "Foo_bar",
|
2020-03-02 12:28:10 +00:00
|
|
|
"contentmodel" => "wikitext",
|
|
|
|
|
"oldrevid" => 371705,
|
|
|
|
|
"newrevid" => 371707,
|
|
|
|
|
"newtimestamp" => "2018-12-18T16:59:42Z",
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
[ // Response expected to be generated by UpdateHandler
|
|
|
|
|
'id' => 94542,
|
|
|
|
|
'content_model' => 'wikitext',
|
2021-05-03 20:38:48 +00:00
|
|
|
'title' => 'Foo bar',
|
|
|
|
|
'key' => 'Foo_bar',
|
2020-03-02 12:28:10 +00:00
|
|
|
'latest' => [
|
|
|
|
|
'id' => 371707,
|
|
|
|
|
'timestamp' => "2018-12-18T16:59:42Z"
|
|
|
|
|
],
|
|
|
|
|
'license' => [
|
|
|
|
|
'url' => 'https://creativecommons.org/licenses/by-sa/4.0/',
|
|
|
|
|
'title' => 'CC-BY-SA 4.0'
|
|
|
|
|
],
|
|
|
|
|
'source' => 'Content of revision 371707'
|
2020-03-12 12:54:51 +00:00
|
|
|
],
|
|
|
|
|
true
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
yield "update without token" => [
|
|
|
|
|
[ // Request data received by UpdateHandler
|
|
|
|
|
'method' => 'PUT',
|
|
|
|
|
'pathParams' => [ 'title' => 'Foo' ],
|
|
|
|
|
'headers' => [
|
|
|
|
|
'Content-Type' => 'application/json',
|
|
|
|
|
],
|
|
|
|
|
'bodyContents' => json_encode( [
|
|
|
|
|
'source' => 'Lorem Ipsum',
|
|
|
|
|
'comment' => 'Testing',
|
|
|
|
|
'content_model' => CONTENT_MODEL_WIKITEXT,
|
|
|
|
|
'latest' => [ 'id' => 789123 ],
|
|
|
|
|
] ),
|
|
|
|
|
],
|
|
|
|
|
[ // Fake request expected to be passed into ApiEditPage
|
|
|
|
|
'title' => 'Foo',
|
|
|
|
|
'text' => 'Lorem Ipsum',
|
|
|
|
|
'summary' => 'Testing',
|
|
|
|
|
'contentmodel' => 'wikitext',
|
|
|
|
|
'nocreate' => '1',
|
|
|
|
|
'baserevid' => '789123',
|
|
|
|
|
'token' => '+\\', // use known-good token for current user (anon)
|
|
|
|
|
],
|
|
|
|
|
[ // Mock response returned by ApiEditPage
|
|
|
|
|
"edit" => [
|
|
|
|
|
"result" => "Success",
|
|
|
|
|
"pageid" => 94542,
|
2020-07-22 19:12:00 +00:00
|
|
|
"title" => "Foo",
|
2020-03-12 12:54:51 +00:00
|
|
|
"contentmodel" => "wikitext",
|
|
|
|
|
"oldrevid" => 371705,
|
|
|
|
|
"newrevid" => 371707,
|
|
|
|
|
"newtimestamp" => "2018-12-18T16:59:42Z",
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
[ // Response expected to be generated by UpdateHandler
|
|
|
|
|
'id' => 94542,
|
|
|
|
|
'content_model' => 'wikitext',
|
|
|
|
|
'latest' => [
|
|
|
|
|
'id' => 371707,
|
|
|
|
|
'timestamp' => "2018-12-18T16:59:42Z"
|
|
|
|
|
],
|
|
|
|
|
'license' => [
|
|
|
|
|
'url' => 'https://creativecommons.org/licenses/by-sa/4.0/',
|
|
|
|
|
'title' => 'CC-BY-SA 4.0'
|
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
true
|
2020-03-02 12:28:10 +00:00
|
|
|
];
|
2021-03-19 13:10:25 +00:00
|
|
|
|
|
|
|
|
yield "null-edit (unchanged)" => [
|
|
|
|
|
[ // Request data received by UpdateHandler
|
|
|
|
|
'method' => 'PUT',
|
|
|
|
|
'pathParams' => [ 'title' => 'Foo' ],
|
|
|
|
|
'headers' => [
|
|
|
|
|
'Content-Type' => 'application/json',
|
|
|
|
|
],
|
|
|
|
|
'bodyContents' => json_encode( [
|
|
|
|
|
'source' => 'Lorem Ipsum',
|
|
|
|
|
'comment' => 'Testing',
|
|
|
|
|
'content_model' => CONTENT_MODEL_WIKITEXT,
|
|
|
|
|
'latest' => [ 'id' => 789123 ],
|
|
|
|
|
] ),
|
|
|
|
|
],
|
|
|
|
|
[ // Fake request expected to be passed into ApiEditPage
|
|
|
|
|
'title' => 'Foo',
|
|
|
|
|
'text' => 'Lorem Ipsum',
|
|
|
|
|
'summary' => 'Testing',
|
|
|
|
|
'contentmodel' => 'wikitext',
|
|
|
|
|
'nocreate' => '1',
|
|
|
|
|
'baserevid' => '789123',
|
|
|
|
|
'token' => '+\\', // use known-good token for current user (anon)
|
|
|
|
|
],
|
|
|
|
|
[ // Mock response returned by ApiEditPage
|
|
|
|
|
"edit" => [
|
|
|
|
|
"result" => "Success",
|
|
|
|
|
"pageid" => 94542,
|
|
|
|
|
"title" => "Foo",
|
|
|
|
|
"contentmodel" => "wikitext",
|
|
|
|
|
"nochange" => "", // null-edit!
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
[ // Response expected to be generated by UpdateHandler
|
|
|
|
|
'id' => 94542,
|
|
|
|
|
'content_model' => 'wikitext',
|
|
|
|
|
'latest' => [
|
2021-03-24 15:05:53 +00:00
|
|
|
'id' => 1234, // ID of current rev, as defined in newHandler()
|
2021-03-19 13:10:25 +00:00
|
|
|
'timestamp' => '2020-01-01T01:02:03Z' // see fake RevisionStore in newHandler()
|
|
|
|
|
],
|
|
|
|
|
'license' => [
|
|
|
|
|
'url' => 'https://creativecommons.org/licenses/by-sa/4.0/',
|
|
|
|
|
'title' => 'CC-BY-SA 4.0'
|
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
true
|
|
|
|
|
];
|
2020-03-02 12:28:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideExecute
|
|
|
|
|
*/
|
|
|
|
|
public function testExecute(
|
|
|
|
|
$requestData,
|
|
|
|
|
$expectedActionParams,
|
|
|
|
|
$actionResult,
|
2020-03-12 12:54:51 +00:00
|
|
|
$expectedResponse,
|
|
|
|
|
$csrfSafe
|
2020-03-02 12:28:10 +00:00
|
|
|
) {
|
|
|
|
|
$request = new RequestData( $requestData );
|
|
|
|
|
|
2020-03-12 12:54:51 +00:00
|
|
|
$handler = $this->newHandler( $actionResult, null, $csrfSafe );
|
2020-03-02 12:28:10 +00:00
|
|
|
|
2022-05-04 16:29:51 +00:00
|
|
|
$responseData = $this->executeHandlerAndGetBodyData(
|
Make it possible to override the session in REST API tests
The current signature of the various execute methods only takes a
boolean parameter to determine if the session should be safe against
CSRF, but that does not give callers fine-grained control over the
Session object, including setting a specific token.
Also, do not use createNoOpMock in getSession(), since it implies
strong assertions on what methods are called. This way, getSession
can also be used to get a simple mock session that tests may further
manipulate.
Make $csrfSafe parameter of SessionHelperTestTrait::getSession
mandatory. This way, callers are forced to think what makes sense in
each use case. The various methods in HandlerTestTrait now default to
a session that is safe against CSRF. This assumes that most REST
handlers don't care about the session, and that any handler that does
care about the session and where someone needs to test the behaviour
in case of bad/missing token will explicitly provide a Session that
is NOT safe against CSRF.
Typehint the return value of Session(Backend)::getUser so that PHPUnit
will automatically make it return a mock User object even if the method
is not explicitly mocked. Remove a useless PHPUnit assertion -- setting
the return value to be X and then veryfing that is equal to X is a
tautology, and can only fail if the test itself is flawed (as was the
case, since it was using stdClass as the return type for all
methods). Remove the getUser test case altogether, there's no way to
make it work given the DummySessionBackend, and the test isn't that
helpful anyway. More and more methods will have the same issue as soon
as their return value is typehinted.
Follow-up: I2a9215bf909b83564247ded95ecdb4ead0615150
Change-Id: Ic51dc3e7bf47c81f2ac4705308bb9ecd8275bbaf
2023-02-06 15:18:00 +00:00
|
|
|
$handler, $request, [], [], [], [], null, $this->getSession( $csrfSafe )
|
2022-05-04 16:29:51 +00:00
|
|
|
);
|
2020-03-02 12:28:10 +00:00
|
|
|
|
|
|
|
|
// Check parameters passed to ApiEditPage by UpdateHandler based on $requestData
|
|
|
|
|
foreach ( $expectedActionParams as $key => $value ) {
|
|
|
|
|
$this->assertSame(
|
|
|
|
|
$value,
|
2020-07-22 19:12:00 +00:00
|
|
|
$handler->getApiMain()->getVal( $key ),
|
2020-03-02 12:28:10 +00:00
|
|
|
"ApiEditPage param: $key"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check response that UpdateHandler created after receiving $actionResult from ApiEditPage
|
|
|
|
|
foreach ( $expectedResponse as $key => $value ) {
|
|
|
|
|
$this->assertArrayHasKey( $key, $responseData );
|
|
|
|
|
$this->assertSame(
|
|
|
|
|
$value,
|
|
|
|
|
$responseData[ $key ],
|
|
|
|
|
"UpdateHandler response field: $key"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-23 11:36:19 +00:00
|
|
|
public static function provideBodyValidation() {
|
2020-03-02 12:28:10 +00:00
|
|
|
yield "missing source field" => [
|
|
|
|
|
[ // Request data received by UpdateHandler
|
2020-03-12 12:54:51 +00:00
|
|
|
'method' => 'PUT',
|
2020-03-02 12:28:10 +00:00
|
|
|
'pathParams' => [ 'title' => 'Foo' ],
|
|
|
|
|
'headers' => [
|
|
|
|
|
'Content-Type' => 'application/json',
|
|
|
|
|
],
|
|
|
|
|
'bodyContents' => json_encode( [
|
|
|
|
|
'token' => 'TOKEN',
|
|
|
|
|
'comment' => 'Testing',
|
|
|
|
|
'content_model' => CONTENT_MODEL_WIKITEXT,
|
|
|
|
|
] ),
|
|
|
|
|
],
|
|
|
|
|
new MessageValue( 'rest-missing-body-field', [ 'source' ] ),
|
|
|
|
|
];
|
|
|
|
|
yield "missing comment field" => [
|
|
|
|
|
[ // Request data received by UpdateHandler
|
2020-03-12 12:54:51 +00:00
|
|
|
'method' => 'PUT',
|
2020-03-02 12:28:10 +00:00
|
|
|
'pathParams' => [ 'title' => 'Foo' ],
|
|
|
|
|
'headers' => [
|
|
|
|
|
'Content-Type' => 'application/json',
|
|
|
|
|
],
|
|
|
|
|
'bodyContents' => json_encode( [
|
|
|
|
|
'token' => 'TOKEN',
|
|
|
|
|
'source' => 'Lorem Ipsum',
|
|
|
|
|
'content_model' => CONTENT_MODEL_WIKITEXT,
|
|
|
|
|
] ),
|
|
|
|
|
],
|
|
|
|
|
new MessageValue( 'rest-missing-body-field', [ 'comment' ] ),
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideBodyValidation
|
|
|
|
|
*/
|
|
|
|
|
public function testBodyValidation( array $requestData, MessageValue $expectedMessage ) {
|
|
|
|
|
$request = new RequestData( $requestData );
|
|
|
|
|
|
|
|
|
|
$handler = $this->newHandler( [] );
|
|
|
|
|
|
|
|
|
|
$exception = $this->executeHandlerAndGetHttpException( $handler, $request );
|
|
|
|
|
|
|
|
|
|
$this->assertSame( 400, $exception->getCode(), 'HTTP status' );
|
|
|
|
|
$this->assertInstanceOf( LocalizedHttpException::class, $exception );
|
|
|
|
|
|
|
|
|
|
/** @var LocalizedHttpException $exception */
|
|
|
|
|
$this->assertEquals( $expectedMessage, $exception->getMessageValue() );
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-23 11:36:19 +00:00
|
|
|
public static function provideHeaderValidation() {
|
2020-03-02 12:28:10 +00:00
|
|
|
yield "bad content type" => [
|
|
|
|
|
[ // Request data received by UpdateHandler
|
2020-03-12 12:54:51 +00:00
|
|
|
'method' => 'PUT',
|
2020-03-02 12:28:10 +00:00
|
|
|
'pathParams' => [ 'title' => 'Foo' ],
|
|
|
|
|
'headers' => [
|
|
|
|
|
'Content-Type' => 'text/plain',
|
|
|
|
|
],
|
|
|
|
|
'bodyContents' => json_encode( [
|
|
|
|
|
'token' => 'TOKEN',
|
|
|
|
|
'source' => 'Lorem Ipsum',
|
|
|
|
|
'comment' => 'Testing',
|
|
|
|
|
'content_model' => CONTENT_MODEL_WIKITEXT,
|
|
|
|
|
] ),
|
|
|
|
|
],
|
|
|
|
|
415
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideHeaderValidation
|
|
|
|
|
*/
|
|
|
|
|
public function testHeaderValidation( array $requestData, $expectedStatus ) {
|
|
|
|
|
$request = new RequestData( $requestData );
|
|
|
|
|
|
|
|
|
|
$handler = $this->newHandler( [] );
|
|
|
|
|
|
|
|
|
|
$exception = $this->executeHandlerAndGetHttpException( $handler, $request );
|
|
|
|
|
|
|
|
|
|
$this->assertSame( $expectedStatus, $exception->getCode(), 'HTTP status' );
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-08 14:30:12 +00:00
|
|
|
/**
|
|
|
|
|
* FIXME: Can't access MW services in a dataProvider.
|
|
|
|
|
*/
|
2023-03-23 11:36:19 +00:00
|
|
|
public static function provideErrorMapping() {
|
2020-03-02 12:28:10 +00:00
|
|
|
yield "missingtitle" => [
|
|
|
|
|
new ApiUsageException( null, Status::newFatal( 'apierror-missingtitle' ) ),
|
|
|
|
|
new LocalizedHttpException( new MessageValue( 'apierror-missingtitle' ), 404 ),
|
|
|
|
|
];
|
|
|
|
|
yield "protectedpage" => [
|
|
|
|
|
new ApiUsageException( null, Status::newFatal( 'apierror-protectedpage' ) ),
|
|
|
|
|
new LocalizedHttpException( new MessageValue( 'apierror-protectedpage' ), 403 ),
|
|
|
|
|
];
|
|
|
|
|
yield "articleexists" => [
|
|
|
|
|
new ApiUsageException( null, Status::newFatal( 'apierror-articleexists' ) ),
|
|
|
|
|
new LocalizedHttpException(
|
|
|
|
|
new MessageValue( 'rest-update-cannot-create-page', [ 'Foo' ] ),
|
|
|
|
|
409
|
|
|
|
|
),
|
|
|
|
|
];
|
|
|
|
|
yield "editconflict" => [
|
|
|
|
|
new ApiUsageException( null, Status::newFatal( 'apierror-editconflict' ) ),
|
|
|
|
|
new LocalizedHttpException( new MessageValue( 'apierror-editconflict' ), 409 ),
|
|
|
|
|
];
|
|
|
|
|
yield "ratelimited" => [
|
|
|
|
|
new ApiUsageException( null, Status::newFatal( 'apierror-ratelimited' ) ),
|
|
|
|
|
new LocalizedHttpException( new MessageValue( 'apierror-ratelimited' ), 429 ),
|
|
|
|
|
];
|
|
|
|
|
yield "badtoken" => [
|
|
|
|
|
new ApiUsageException(
|
|
|
|
|
null,
|
|
|
|
|
Status::newFatal( 'apierror-badtoken', [ 'plaintext' => 'BAD' ] )
|
|
|
|
|
),
|
|
|
|
|
new LocalizedHttpException(
|
|
|
|
|
new MessageValue(
|
|
|
|
|
'apierror-badtoken',
|
|
|
|
|
[ new ScalarParam( ParamType::PLAINTEXT, 'BAD' ) ]
|
|
|
|
|
), 403
|
|
|
|
|
),
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// Unmapped errors should be passed through with a status 400.
|
|
|
|
|
yield "no-direct-editing" => [
|
|
|
|
|
new ApiUsageException( null, Status::newFatal( 'apierror-no-direct-editing' ) ),
|
|
|
|
|
new LocalizedHttpException( new MessageValue( 'apierror-no-direct-editing' ), 400 ),
|
|
|
|
|
];
|
|
|
|
|
yield "badformat" => [
|
|
|
|
|
new ApiUsageException( null, Status::newFatal( 'apierror-badformat' ) ),
|
|
|
|
|
new LocalizedHttpException( new MessageValue( 'apierror-badformat' ), 400 ),
|
|
|
|
|
];
|
|
|
|
|
yield "emptypage" => [
|
|
|
|
|
new ApiUsageException( null, Status::newFatal( 'apierror-emptypage' ) ),
|
|
|
|
|
new LocalizedHttpException( new MessageValue( 'apierror-emptypage' ), 400 ),
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-08 14:30:12 +00:00
|
|
|
public function testErrorMapping() {
|
|
|
|
|
foreach ( $this->provideErrorMapping() as $expected ) {
|
|
|
|
|
$apiUsageException = $expected[0];
|
|
|
|
|
$expectedHttpException = $expected[1];
|
|
|
|
|
$requestData = [ // Request data received by UpdateHandler
|
|
|
|
|
'method' => 'POST',
|
|
|
|
|
'pathParams' => [ 'title' => 'Foo' ],
|
|
|
|
|
'headers' => [
|
|
|
|
|
'Content-Type' => 'application/json',
|
|
|
|
|
],
|
|
|
|
|
'bodyContents' => json_encode( [
|
|
|
|
|
'source' => 'Lorem Ipsum',
|
|
|
|
|
'comment' => 'Testing',
|
|
|
|
|
'content_model' => CONTENT_MODEL_WIKITEXT,
|
|
|
|
|
] ),
|
|
|
|
|
];
|
|
|
|
|
$request = new RequestData( $requestData );
|
2020-03-02 12:28:10 +00:00
|
|
|
|
2021-12-08 14:30:12 +00:00
|
|
|
$handler = $this->newHandler( [], $apiUsageException );
|
2020-03-02 12:28:10 +00:00
|
|
|
|
2021-12-08 14:30:12 +00:00
|
|
|
$exception = $this->executeHandlerAndGetHttpException( $handler, $request );
|
2020-03-02 12:28:10 +00:00
|
|
|
|
2021-12-08 14:30:12 +00:00
|
|
|
$this->assertSame( $expectedHttpException->getMessage(), $exception->getMessage() );
|
|
|
|
|
$this->assertSame( $expectedHttpException->getCode(), $exception->getCode(), 'HTTP status' );
|
2020-03-02 12:28:10 +00:00
|
|
|
|
2021-12-08 14:30:12 +00:00
|
|
|
$errorData = $exception->getErrorData();
|
|
|
|
|
if ( $expectedHttpException->getErrorData() ) {
|
|
|
|
|
foreach ( $expectedHttpException->getErrorData() as $key => $value ) {
|
|
|
|
|
$this->assertSame( $value, $errorData[$key], 'Error data key $key' );
|
|
|
|
|
}
|
2020-03-02 12:28:10 +00:00
|
|
|
}
|
|
|
|
|
|
2021-12-08 14:30:12 +00:00
|
|
|
if ( $expectedHttpException instanceof LocalizedHttpException ) {
|
|
|
|
|
/** @var LocalizedHttpException $exception */
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
$expectedHttpException->getMessageValue(),
|
|
|
|
|
$exception->getMessageValue()
|
|
|
|
|
);
|
|
|
|
|
}
|
2020-03-02 12:28:10 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-13 13:47:08 +00:00
|
|
|
public function testConflictOutput() {
|
|
|
|
|
$requestData = [ // Request data received by UpdateHandler
|
|
|
|
|
'method' => 'POST',
|
|
|
|
|
'pathParams' => [ 'title' => 'Foo' ],
|
|
|
|
|
'headers' => [
|
|
|
|
|
'Content-Type' => 'application/json',
|
|
|
|
|
],
|
|
|
|
|
'bodyContents' => json_encode( [
|
|
|
|
|
'latest' => [
|
|
|
|
|
'id' => 17,
|
|
|
|
|
],
|
|
|
|
|
'source' => 'Lorem Ipsum',
|
|
|
|
|
'comment' => 'Testing'
|
|
|
|
|
] ),
|
|
|
|
|
];
|
|
|
|
|
$request = new RequestData( $requestData );
|
|
|
|
|
|
|
|
|
|
$apiUsageException = new ApiUsageException( null, Status::newFatal( 'apierror-editconflict' ) );
|
|
|
|
|
$handler = $this->newHandler( [], $apiUsageException );
|
|
|
|
|
$handler->setJsonDiffFunction( [ $this, 'fakeJsonDiff' ] );
|
|
|
|
|
|
|
|
|
|
$exception = $this->executeHandlerAndGetHttpException( $handler, $request );
|
|
|
|
|
|
|
|
|
|
$this->assertSame( 409, $exception->getCode(), 'HTTP status' );
|
|
|
|
|
|
|
|
|
|
$expectedData = [
|
|
|
|
|
'local' => [
|
|
|
|
|
'from' => 'Content of revision 17',
|
|
|
|
|
'to' => 'Lorem Ipsum',
|
|
|
|
|
],
|
|
|
|
|
'remote' => [
|
|
|
|
|
'from' => 'Content of revision 17',
|
|
|
|
|
'to' => 'Current content of 0:Foo',
|
|
|
|
|
],
|
|
|
|
|
'base' => 17,
|
|
|
|
|
'current' => 1234
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$errorData = $exception->getErrorData();
|
|
|
|
|
foreach ( $expectedData as $key => $value ) {
|
2020-03-06 15:53:01 +00:00
|
|
|
$this->assertSame( $value, $errorData[$key], "Error data key $key" );
|
2020-03-13 13:47:08 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function fakeJsonDiff( $fromText, $toText ) {
|
|
|
|
|
return FormatJson::encode( [
|
|
|
|
|
'from' => $fromText,
|
|
|
|
|
'to' => $toText
|
|
|
|
|
] );
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-02 12:28:10 +00:00
|
|
|
}
|