Debian Codegraph @

tests/phpunit/includes/parser/BeforeParserFetchTemplateRevisionRecordTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Parser;
4
5 use MediaWiki\Content\WikitextContent;
6 use MediaWiki\Linker\LinkTarget;
7 use MediaWiki\Parser\Parser;
8 use MediaWiki\Parser\ParserOptions;
9 use MediaWiki\Revision\MutableRevisionRecord;
10 use MediaWiki\Revision\RevisionRecord;
11 use MediaWiki\Revision\SlotRecord;
12 use MediaWikiLangTestCase;
13 use MockTitleTrait;
14
15 /**
16 * @group Database
17 * @covers \MediaWiki\Parser\Parser
18 */
19 class BeforeParserFetchTemplateRevisionRecordTest extends MediaWikiLangTestCase {
20 use MockTitleTrait;
21
22 private function checkResult( $expected, $actual ) {
23 if ( ( $expected['revision-record'] ?? true ) === false ) {
24 $this->assertSame( false, $actual['revision-record'] );
25 } else {
26 $this->assertNotNull( $actual['revision-record'] );
27 }
28 $this->assertSame( $expected['text'], $actual['text'] );
29 $this->assertSame( $expected['finalTitle'], $actual['finalTitle']->getPrefixedText() );
30 // Simplify 'deps'
31 $simpleActualDeps = array_map(
32 fn ( $dep ) => $dep['title']->getPrefixedText(),
33 $actual['deps']
34 );
35 $this->assertArrayEquals( $expected['deps'], $simpleActualDeps );
36 }
37
38 public static function provideWithParser() {
39 yield "Without \$parser" => [ false ];
40 yield "With \$parser" => [ true ];
41 }
42
43 private function commonSetup( $suffix = null ) {
44 $suffix ??= $this->getCallerName();
45 $parser = $this->getServiceContainer()->getParserFactory()->create();
46 $parser->setOptions( ParserOptions::newFromAnon() );
47
48 $page = $this->getNonexistingTestPage( "Base $suffix" );
49 $this->editPage( $page, 'Base page content', 'Make testing content' );
50
51 $redirectPage = $this->getNonexistingTestPage( "Redirect $suffix" );
52 $this->editPage(
53 $redirectPage,
54 '#REDIRECT [[' . $page->getTitle()->getPrefixedText() . ']]',
55 "Make redirect link for testing"
56 );
57
58 return [ $parser, $page, $redirectPage ];
59 }
60
61 /**
62 * @covers \MediaWiki\Parser\Parser::statelessFetchTemplate
63 * @dataProvider provideWithParser
64 */
65 public function testStatelessFetchTemplateBasic( bool $withParser ) {
66 [ $parser, $page, $redirectPage ] = $this->commonSetup( __FUNCTION__ );
67
68 // Basic redirect test
69 $ret = Parser::statelessFetchTemplate(
70 $redirectPage->getTitle(), $withParser ? $parser : null
71 );
72 $this->checkResult( [
73 'text' => 'Base page content',
74 'finalTitle' => 'Base ' . __FUNCTION__,
75 'deps' => [
76 'Base ' . __FUNCTION__,
77 'Redirect ' . __FUNCTION__,
78 ],
79 ], $ret );
80 }
81
82 /**
83 * @covers \MediaWiki\Parser\Parser::statelessFetchTemplate
84 * @dataProvider provideWithParser
85 */
86 public function testStatelessFetchTemplateSkip( bool $withParser ) {
87 [ $parser, $page, $redirectPage ] = $this->commonSetup( __FUNCTION__ );
88
89 // Create a hook to prevent resolution of the redirect
90 $this->setTemporaryHook(
91 'BeforeParserFetchTemplateRevisionRecord',
92 static function ( ?LinkTarget $contextTitle, LinkTarget $title, bool &$skip, ?RevisionRecord &$revRecord ) {
93 $skip = true;
94 }
95 );
96
97 $ret = Parser::statelessFetchTemplate(
98 $redirectPage->getTitle(), $withParser ? $parser : null
99 );
100 $this->checkResult( [
101 'text' => false,
102 'finalTitle' => 'Redirect ' . __FUNCTION__,
103 'deps' => [
104 'Redirect ' . __FUNCTION__,
105 ],
106 ], $ret );
107 }
108
109 /**
110 * @covers \MediaWiki\Parser\Parser::statelessFetchTemplate
111 * @dataProvider provideWithParser
112 */
113 public function testStatelessFetchTemplateMissing( bool $withParser ) {
114 [ $parser, $page, $redirectPage ] = $this->commonSetup( __FUNCTION__ );
115
116 // Create a hook to redirect to a non-existing page
117 $baseTitle = 'Base ' . __FUNCTION__;
118 $missing = $this->getNonexistingTestPage( 'Missing ' . __FUNCTION__ );
119 $this->setTemporaryHook(
120 'BeforeParserFetchTemplateRevisionRecord',
121 static function ( ?LinkTarget $contextTitle, LinkTarget $title, bool &$skip, ?RevisionRecord &$revRecord ) use ( $baseTitle, $missing ) {
122 if ( $title->getPrefixedText() === $baseTitle ) {
123 $revRecord = new MutableRevisionRecord( $missing->getTitle() );
124 }
125 }
126 );
127 $ret = Parser::statelessFetchTemplate(
128 $redirectPage->getTitle(), $withParser ? $parser : null
129 );
130 $this->checkResult( [
131 'text' => false,
132 'finalTitle' => 'Missing ' . __FUNCTION__,
133 'deps' => [
134 'Base ' . __FUNCTION__,
135 # The $missing page is duplicated in the deps here, but that's
136 # harmless.
137 'Missing ' . __FUNCTION__,
138 'Missing ' . __FUNCTION__,
139 'Redirect ' . __FUNCTION__,
140 ],
141 ], $ret );
142 }
143
144 /**
145 * @covers \MediaWiki\Parser\Parser::statelessFetchTemplate
146 * @dataProvider provideWithParser
147 */
148 public function testStatelessFetchTemplateSubstituted( bool $withParser ) {
149 [ $parser, $page, $redirectPage ] = $this->commonSetup( __FUNCTION__ );
150
151 // Create a hook to redirect to a non-existing page
152 $baseTitle = 'Base ' . __FUNCTION__;
153 $subst = $this->getNonexistingTestPage( 'Subst ' . __FUNCTION__ );
154 $this->setTemporaryHook(
155 'BeforeParserFetchTemplateRevisionRecord',
156 static function ( ?LinkTarget $contextTitle, LinkTarget $title, bool &$skip, ?RevisionRecord &$revRecord ) use ( $baseTitle, $subst ) {
157 if ( $title->getPrefixedText() === $baseTitle ) {
158 $revRecord = new MutableRevisionRecord( $subst->getTitle() );
159 $revRecord->setContent( SlotRecord::MAIN, new WikitextContent( 'foo' ) );
160 }
161 }
162 );
163 $ret = Parser::statelessFetchTemplate(
164 $redirectPage->getTitle(), $withParser ? $parser : null
165 );
166 $this->checkResult( [
167 'text' => 'foo',
168 'finalTitle' => 'Subst ' . __FUNCTION__,
169 'deps' => [
170 'Base ' . __FUNCTION__,
171 'Subst ' . __FUNCTION__,
172 'Redirect ' . __FUNCTION__,
173 ],
174 ], $ret );
175 }
176 }