2022-05-16 16:56:20 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace MediaWiki\Tests\Rest\Handler;
|
|
|
|
|
|
2024-02-18 21:31:02 +00:00
|
|
|
use Exception;
|
2022-05-16 16:56:20 +00:00
|
|
|
use HashBagOStuff;
|
2023-11-09 13:32:23 +00:00
|
|
|
use MediaWiki\Block\BlockErrorFormatter;
|
2024-06-13 21:21:02 +00:00
|
|
|
use MediaWiki\Context\IContextSource;
|
2022-05-16 16:56:20 +00:00
|
|
|
use MediaWiki\Edit\ParsoidOutputStash;
|
2023-11-06 15:22:44 +00:00
|
|
|
use MediaWiki\Edit\ParsoidRenderID;
|
2022-05-16 16:56:20 +00:00
|
|
|
use MediaWiki\Edit\SimpleParsoidOutputStash;
|
2023-11-09 13:32:23 +00:00
|
|
|
use MediaWiki\Permissions\Authority;
|
|
|
|
|
use MediaWiki\Permissions\UserAuthority;
|
|
|
|
|
use MediaWiki\Request\FauxRequest;
|
2022-05-16 16:56:20 +00:00
|
|
|
use MediaWiki\Rest\RequestData;
|
2023-11-09 13:32:23 +00:00
|
|
|
use MediaWiki\User\User;
|
2023-08-09 15:43:50 +00:00
|
|
|
use WikiPage;
|
2022-05-16 16:56:20 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This trait is used in PageHTMLHandlerTest.php & RevisionHTMLHandlerTest.php
|
|
|
|
|
* to construct requests and perform stashing for the Parsoid Output stash feature.
|
|
|
|
|
*/
|
|
|
|
|
trait HTMLHandlerTestTrait {
|
|
|
|
|
|
|
|
|
|
private $parsoidOutputStash = null;
|
|
|
|
|
|
|
|
|
|
private function getParsoidOutputStash(): ParsoidOutputStash {
|
|
|
|
|
if ( !$this->parsoidOutputStash ) {
|
2022-10-29 18:52:23 +00:00
|
|
|
$chFactory = $this->getServiceContainer()->getContentHandlerFactory();
|
|
|
|
|
$this->parsoidOutputStash = new SimpleParsoidOutputStash( $chFactory, new HashBagOStuff(), 120 );
|
2022-05-16 16:56:20 +00:00
|
|
|
}
|
|
|
|
|
return $this->parsoidOutputStash;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-09 13:32:23 +00:00
|
|
|
private function getAuthority(): Authority {
|
|
|
|
|
$services = $this->getServiceContainer();
|
|
|
|
|
return new UserAuthority(
|
|
|
|
|
// We need a newly created user because we want IP and newbie to apply.
|
|
|
|
|
new User(),
|
|
|
|
|
new FauxRequest(),
|
2024-02-18 21:31:02 +00:00
|
|
|
$this->createMock( IContextSource::class ),
|
2023-11-09 13:32:23 +00:00
|
|
|
$services->getPermissionManager(),
|
|
|
|
|
$services->getRateLimiter(),
|
|
|
|
|
$this->createMock( BlockErrorFormatter::class )
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-16 16:56:20 +00:00
|
|
|
/**
|
2023-08-09 15:43:50 +00:00
|
|
|
* @param WikiPage $page
|
2022-05-16 16:56:20 +00:00
|
|
|
* @param array $queryParams
|
2022-05-24 21:13:42 +00:00
|
|
|
* @param array $config
|
2022-05-16 16:56:20 +00:00
|
|
|
*
|
|
|
|
|
* @return array
|
2024-02-18 21:31:02 +00:00
|
|
|
* @throws Exception
|
2022-05-16 16:56:20 +00:00
|
|
|
*/
|
2022-05-24 21:13:42 +00:00
|
|
|
private function executePageHTMLRequest(
|
2023-08-09 15:43:50 +00:00
|
|
|
WikiPage $page,
|
2022-05-24 21:13:42 +00:00
|
|
|
array $queryParams = [],
|
2023-11-09 13:32:23 +00:00
|
|
|
array $config = [],
|
|
|
|
|
Authority $authority = null
|
2022-05-24 21:13:42 +00:00
|
|
|
): array {
|
2022-05-16 16:56:20 +00:00
|
|
|
$handler = $this->newHandler();
|
|
|
|
|
$request = new RequestData( [
|
2023-08-09 15:43:50 +00:00
|
|
|
'pathParams' => [ 'title' => $page->getTitle()->getPrefixedDBkey() ],
|
2022-05-16 16:56:20 +00:00
|
|
|
'queryParams' => $queryParams,
|
|
|
|
|
] );
|
2023-11-09 13:32:23 +00:00
|
|
|
$result = $this->executeHandler(
|
|
|
|
|
$handler,
|
2022-05-16 16:56:20 +00:00
|
|
|
$request,
|
2023-11-09 13:32:23 +00:00
|
|
|
$config + [ 'format' => 'html' ],
|
|
|
|
|
[],
|
|
|
|
|
[],
|
|
|
|
|
[],
|
|
|
|
|
$authority
|
|
|
|
|
);
|
2022-05-16 16:56:20 +00:00
|
|
|
$etag = $result->getHeaderLine( 'ETag' );
|
|
|
|
|
$stashKey = ParsoidRenderID::newFromETag( $etag );
|
|
|
|
|
|
|
|
|
|
return [ $result->getBody()->getContents(), $etag, $stashKey ];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param int $revId
|
|
|
|
|
* @param array $queryParams
|
2022-05-24 21:13:42 +00:00
|
|
|
* @param array $config
|
2022-05-16 16:56:20 +00:00
|
|
|
*
|
|
|
|
|
* @return array
|
2024-02-18 21:31:02 +00:00
|
|
|
* @throws Exception
|
2022-05-16 16:56:20 +00:00
|
|
|
*/
|
2022-05-24 21:13:42 +00:00
|
|
|
private function executeRevisionHTMLRequest(
|
|
|
|
|
int $revId,
|
|
|
|
|
array $queryParams = [],
|
2023-11-09 13:32:23 +00:00
|
|
|
array $config = [],
|
|
|
|
|
Authority $authority = null
|
2022-05-24 21:13:42 +00:00
|
|
|
): array {
|
2022-05-16 16:56:20 +00:00
|
|
|
$handler = $this->newHandler();
|
|
|
|
|
$request = new RequestData( [
|
|
|
|
|
'pathParams' => [ 'id' => $revId ],
|
|
|
|
|
'queryParams' => $queryParams,
|
|
|
|
|
] );
|
2023-11-09 13:32:23 +00:00
|
|
|
$result = $this->executeHandler(
|
|
|
|
|
$handler,
|
2022-05-16 16:56:20 +00:00
|
|
|
$request,
|
2023-11-09 13:32:23 +00:00
|
|
|
$config + [ 'format' => 'html' ],
|
|
|
|
|
[],
|
|
|
|
|
[],
|
|
|
|
|
[],
|
|
|
|
|
$authority
|
|
|
|
|
);
|
2022-05-16 16:56:20 +00:00
|
|
|
$etag = $result->getHeaderLine( 'ETag' );
|
|
|
|
|
$stashKey = ParsoidRenderID::newFromETag( $etag );
|
|
|
|
|
|
|
|
|
|
return [ $result->getBody()->getContents(), $etag, $stashKey ];
|
|
|
|
|
}
|
|
|
|
|
}
|