Debian Codegraph @

tests/phpunit/integration/includes/Rest/Handler/HTMLHandlerTestTrait.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Rest\Handler;
4
5 use Exception;
6 use MediaWiki\Block\BlockErrorFormatter;
7 use MediaWiki\Context\IContextSource;
8 use MediaWiki\Edit\ParsoidOutputStash;
9 use MediaWiki\Edit\ParsoidRenderID;
10 use MediaWiki\Edit\SimpleParsoidOutputStash;
11 use MediaWiki\Permissions\Authority;
12 use MediaWiki\Permissions\UserAuthority;
13 use MediaWiki\Request\FauxRequest;
14 use MediaWiki\Rest\RequestData;
15 use MediaWiki\User\User;
16 use Wikimedia\ObjectCache\HashBagOStuff;
17 use WikiPage;
18
19 /**
20 * This trait is used in PageHTMLHandlerTest.php & RevisionHTMLHandlerTest.php
21 * to construct requests and perform stashing for the Parsoid Output stash feature.
22 */
23 trait HTMLHandlerTestTrait {
24
25 /** @var ParsoidOutputStash|null */
26 private $parsoidOutputStash = null;
27
28 private function getParsoidOutputStash(): ParsoidOutputStash {
29 if ( !$this->parsoidOutputStash ) {
30 $jsonCodec = $this->getServiceContainer()->getJsonCodec();
31 $this->parsoidOutputStash = new SimpleParsoidOutputStash( $jsonCodec, new HashBagOStuff(), 120 );
32 }
33 return $this->parsoidOutputStash;
34 }
35
36 private function getAuthority(): Authority {
37 $services = $this->getServiceContainer();
38 return new UserAuthority(
39 // We need a newly created user because we want IP and newbie to apply.
40 new User(),
41 new FauxRequest(),
42 $this->createMock( IContextSource::class ),
43 $services->getPermissionManager(),
44 $services->getRateLimiter(),
45 $this->createMock( BlockErrorFormatter::class )
46 );
47 }
48
49 /**
50 * @param WikiPage $page
51 * @param array $queryParams
52 * @param array $config
53 *
54 * @return array
55 * @throws Exception
56 */
57 private function executePageHTMLRequest(
58 WikiPage $page,
59 array $queryParams = [],
60 array $config = [],
61 ?Authority $authority = null
62 ): array {
63 $handler = $this->newHandler();
64 $request = new RequestData( [
65 'pathParams' => [ 'title' => $page->getTitle()->getPrefixedDBkey() ],
66 'queryParams' => $queryParams,
67 ] );
68 $result = $this->executeHandler(
69 $handler,
70 $request,
71 $config + [ 'format' => 'html' ],
72 [],
73 [],
74 [],
75 $authority
76 );
77 $etag = $result->getHeaderLine( 'ETag' );
78 $stashKey = ParsoidRenderID::newFromETag( $etag );
79
80 return [ $result->getBody()->getContents(), $etag, $stashKey ];
81 }
82
83 /**
84 * @param int $revId
85 * @param array $queryParams
86 * @param array $config
87 *
88 * @return array
89 * @throws Exception
90 */
91 private function executeRevisionHTMLRequest(
92 int $revId,
93 array $queryParams = [],
94 array $config = [],
95 ?Authority $authority = null
96 ): array {
97 $handler = $this->newHandler();
98 $request = new RequestData( [
99 'pathParams' => [ 'id' => $revId ],
100 'queryParams' => $queryParams,
101 ] );
102 $result = $this->executeHandler(
103 $handler,
104 $request,
105 $config + [ 'format' => 'html' ],
106 [],
107 [],
108 [],
109 $authority
110 );
111 $etag = $result->getHeaderLine( 'ETag' );
112 $stashKey = ParsoidRenderID::newFromETag( $etag );
113
114 return [ $result->getBody()->getContents(), $etag, $stashKey ];
115 }
116 }