wiki.techinc.nl/tests/phpunit/integration/includes/Rest/Handler/HTMLHandlerTestTrait.php
Derick Alangi d674367af4
REST: Inject Authority instead of full User object
Per T310476, it looks like the Authority interface now supports
rate limiting, so we can just use that instead of a heavy full user
object.

Possible followups
==================
More can be done to make all consumers of the HTML helper's `init()`
method to inject Authority instead.

This patch is needed for the work happening in I08ebea5e8a601f161f.

NOTE: This is technically not a breaking change as the Authority
interface is implemented by both UserAuthority and User classes,
so passing either is fine so consumers passing a full user object
should still work even though we changed the signature of a public
method in HtmlOutputRendererHelper.

Change-Id: I025cd83cc81f73ded861fcab943ba3b942d7c390
2023-11-13 13:42:48 +01:00

113 lines
2.8 KiB
PHP

<?php
namespace MediaWiki\Tests\Rest\Handler;
use HashBagOStuff;
use MediaWiki\Block\BlockErrorFormatter;
use MediaWiki\Edit\ParsoidOutputStash;
use MediaWiki\Edit\SimpleParsoidOutputStash;
use MediaWiki\Parser\Parsoid\ParsoidRenderID;
use MediaWiki\Permissions\Authority;
use MediaWiki\Permissions\UserAuthority;
use MediaWiki\Request\FauxRequest;
use MediaWiki\Rest\RequestData;
use MediaWiki\User\User;
use WikiPage;
/**
* 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 ) {
$chFactory = $this->getServiceContainer()->getContentHandlerFactory();
$this->parsoidOutputStash = new SimpleParsoidOutputStash( $chFactory, new HashBagOStuff(), 120 );
}
return $this->parsoidOutputStash;
}
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(),
$this->createMock( \IContextSource::class ),
$services->getPermissionManager(),
$services->getRateLimiter(),
$this->createMock( BlockErrorFormatter::class )
);
}
/**
* @param WikiPage $page
* @param array $queryParams
* @param array $config
*
* @return array
* @throws \Exception
*/
private function executePageHTMLRequest(
WikiPage $page,
array $queryParams = [],
array $config = [],
Authority $authority = null
): array {
$handler = $this->newHandler();
$request = new RequestData( [
'pathParams' => [ 'title' => $page->getTitle()->getPrefixedDBkey() ],
'queryParams' => $queryParams,
] );
$result = $this->executeHandler(
$handler,
$request,
$config + [ 'format' => 'html' ],
[],
[],
[],
$authority
);
$etag = $result->getHeaderLine( 'ETag' );
$stashKey = ParsoidRenderID::newFromETag( $etag );
return [ $result->getBody()->getContents(), $etag, $stashKey ];
}
/**
* @param int $revId
* @param array $queryParams
* @param array $config
*
* @return array
* @throws \Exception
*/
private function executeRevisionHTMLRequest(
int $revId,
array $queryParams = [],
array $config = [],
Authority $authority = null
): array {
$handler = $this->newHandler();
$request = new RequestData( [
'pathParams' => [ 'id' => $revId ],
'queryParams' => $queryParams,
] );
$result = $this->executeHandler(
$handler,
$request,
$config + [ 'format' => 'html' ],
[],
[],
[],
$authority
);
$etag = $result->getHeaderLine( 'ETag' );
$stashKey = ParsoidRenderID::newFromETag( $etag );
return [ $result->getBody()->getContents(), $etag, $stashKey ];
}
}