Debian Codegraph @

extensions/Scribunto/tests/phpunit/Engines/LuaCommon/TitleLibraryTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\Scribunto\Tests\Engines\LuaCommon;
4
5 use MediaWiki\Content\WikitextContent;
6 use MediaWiki\Interwiki\ClassicInterwikiLookup;
7 use MediaWiki\MainConfigNames;
8 use MediaWiki\Page\PageIdentityValue;
9 use MediaWiki\Parser\ParserOutputFlags;
10 use MediaWiki\Permissions\RestrictionStore;
11 use MediaWiki\Title\Title;
12
13 /**
14 * @covers \MediaWiki\Extension\Scribunto\Engines\LuaCommon\TitleLibrary
15 * @group Database
16 */
17 class TitleLibraryTest extends LuaEngineTestBase {
18 /** @inheritDoc */
19 protected static $moduleName = 'TitleLibraryTests';
20
21 /** @var Title|null */
22 private $testTitle = null;
23
24 /** @var int */
25 private $testPageId = null;
26
27 protected function setUp(): void {
28 $this->setTestTitle( null );
29 parent::setUp();
30
31 // Set up interwikis (via wgInterwikiCache) before creating any Titles
32 $this->overrideConfigValues( [
33 MainConfigNames::Server => '//wiki.local',
34 MainConfigNames::CanonicalServer => 'http://wiki.local',
35 MainConfigNames::UsePathInfo => true,
36 MainConfigNames::ActionPaths => [],
37 MainConfigNames::Script => '/w/index.php',
38 MainConfigNames::ScriptPath => '/w',
39 MainConfigNames::ArticlePath => '/wiki/$1',
40 MainConfigNames::InterwikiCache => ClassicInterwikiLookup::buildCdbHash( [ [
41 'iw_prefix' => 'interwikiprefix',
42 'iw_url' => '//test.wikipedia.org/wiki/$1',
43 'iw_local' => 0,
44 ] ] ),
45 ] );
46
47 $editor = self::getTestSysop()->getUser();
48
49 $wikiPageFactory = $this->getServiceContainer()->getWikiPageFactory();
50
51 // Page for getContent test
52 $page = $wikiPageFactory->newFromTitle( Title::newFromText( 'ScribuntoTestPage' ) );
53 $page->doUserEditContent(
54 new WikitextContent(
55 '{{int:mainpage}}<includeonly>...</includeonly><noinclude>...</noinclude>'
56 ),
57 $editor,
58 'Summary'
59 );
60 $this->testPageId = $page->getId();
61
62 // Pages for redirectTarget tests
63 $page = $wikiPageFactory->newFromTitle( Title::newFromText( 'ScribuntoTestRedirect' ) );
64 $page->doUserEditContent(
65 new WikitextContent( '#REDIRECT [[ScribuntoTestTarget]]' ),
66 $editor,
67 'Summary'
68 );
69 $page = $wikiPageFactory->newFromTitle( Title::newFromText( 'ScribuntoTestNonRedirect' ) );
70 $page->doUserEditContent(
71 new WikitextContent( 'Not a redirect.' ),
72 $editor,
73 'Summary'
74 );
75
76 // Set restrictions for protectionLevels and cascadingProtection tests
77
78 $restrictionStore = $this->createNoOpMock(
79 RestrictionStore::class,
80 [
81 'getCascadeProtectionSources',
82 'getRestrictions',
83 'areRestrictionsLoaded',
84 'areCascadeProtectionSourcesLoaded',
85 'getAllRestrictions',
86 // just do nothing
87 'registerOldRestrictions'
88 ]
89 );
90
91 $this->setService( 'RestrictionStore', $restrictionStore );
92
93 $restrictionStore->method( 'areRestrictionsLoaded' )->willReturn( true );
94 $restrictionStore->method( 'areCascadeProtectionSourcesLoaded' )->willReturn( true );
95
96 $restrictions = [
97 'Main_Page' => [ 'edit' => [], 'move' => [] ],
98 'Module:TestFramework' => [
99 'edit' => [ 'sysop', 'bogus' ],
100 'move' => [ 'sysop', 'bogus' ],
101 ],
102 'interwikiprefix:Module:TestFramework' => [],
103 'Talk:Has/A/Subpage' => [ 'create' => [ 'sysop' ] ],
104 'Not/A/Subpage' => [ 'edit' => [ 'autoconfirmed' ], 'move' => [ 'sysop' ] ],
105 'Module_talk:Test_Framework' => [ 'edit' => [], 'move' => [ 'sysop' ] ],
106 ];
107
108 $restrictionStore->method( 'getAllRestrictions' )
109 ->willReturnCallback( static function ( $title ) use ( $restrictions ) {
110 $key = $title->getPrefixedDBkey();
111 return $restrictions[$key] ?? [];
112 } );
113
114 $restrictionStore->method( 'getRestrictions' )
115 ->willReturnCallback( static function ( $title, $action ) {
116 $key = $title->getPrefixedDBkey();
117 $pageRestrictions = $restrictions[$key] ?? [];
118 return $pageRestrictions[$action] ?? [];
119 } );
120
121 $restrictionStore->method( 'getCascadeProtectionSources' )
122 ->willReturnCallback( static function ( $title ) {
123 if ( $title->getPrefixedDBkey() === 'Main_Page' ) {
124 return [
125 [
126 PageIdentityValue::localIdentity( 5678, NS_MAIN, "Lockbox" ),
127 PageIdentityValue::localIdentity( 8765, NS_MAIN, "Lockbox2" ),
128 ],
129 [ 'edit' => [ 'sysop' ] ]
130 ];
131 } else {
132 return [ [], [] ];
133 }
134 } );
135
136 // Note this depends on every iteration of the data provider running with a clean parser
137 $this->getEngine()->getParser()->getOptions()->setExpensiveParserFunctionLimit( 10 );
138
139 // Indicate to the tests that it's safe to create the title objects
140 $interpreter = $this->getEngine()->getInterpreter();
141 $interpreter->callFunction(
142 $interpreter->loadString( "mw.title.testPageId = $this->testPageId", 'fortest' )
143 );
144 }
145
146 protected function getTestTitle() {
147 return $this->testTitle ?? parent::getTestTitle();
148 }
149
150 protected function setTestTitle( $title ) {
151 $this->testTitle = $title !== null ? Title::newFromText( $title ) : null;
152 $this->resetEngine();
153 }
154
155 protected function getTestModules() {
156 return parent::getTestModules() + [
157 'TitleLibraryTests' => __DIR__ . '/TitleLibraryTests.lua',
158 ];
159 }
160
161 public function testAddsLinks() {
162 $engine = $this->getEngine();
163 $interpreter = $engine->getInterpreter();
164
165 // Loading a title should create a link
166 $links = $engine->getParser()->getOutput()->getLinks();
167 $this->assertFalse( isset( $links[NS_PROJECT]['Referenced_from_Lua'] ) );
168
169 $interpreter->callFunction( $interpreter->loadString(
170 'local _ = mw.title.new( "Project:Referenced from Lua" ).id', 'reference title'
171 ) );
172
173 $links = $engine->getParser()->getOutput()->getLinks();
174 $this->assertArrayHasKey( NS_PROJECT, $links );
175 $this->assertArrayHasKey( 'Referenced_from_Lua', $links[NS_PROJECT] );
176
177 // Loading the page content should create a templatelink
178 $templates = $engine->getParser()->getOutput()->getTemplates();
179 $this->assertFalse( isset( $links[NS_PROJECT]['Loaded_from_Lua'] ) );
180
181 $interpreter->callFunction( $interpreter->loadString(
182 'mw.title.new( "Project:Loaded from Lua" ):getContent()', 'load title'
183 ) );
184
185 $templates = $engine->getParser()->getOutput()->getTemplates();
186 $this->assertArrayHasKey( NS_PROJECT, $templates );
187 $this->assertArrayHasKey( 'Loaded_from_Lua', $templates[NS_PROJECT] );
188 }
189
190 /**
191 * @dataProvider provideVaryPageId
192 */
193 public function testVaryPageId( $testTitle, $code, $flag ) {
194 $this->setTestTitle( $testTitle );
195
196 $code = strtr( $code, [ '$$ID$$' => $this->testPageId ] );
197
198 $engine = $this->getEngine();
199 $interpreter = $engine->getInterpreter();
200 $this->assertFalse(
201 $engine->getParser()->getOutput()->getOutputFlag( ParserOutputFlags::VARY_PAGE_ID ), 'sanity check'
202 );
203
204 $interpreter->callFunction( $interpreter->loadString(
205 "local _ = $code", 'reference title but not id'
206 ) );
207 $this->assertFalse( $engine->getParser()->getOutput()->getOutputFlag( ParserOutputFlags::VARY_PAGE_ID ) );
208
209 $interpreter->callFunction( $interpreter->loadString(
210 "local _ = $code.id", 'reference id'
211 ) );
212 $this->assertSame( $flag, $engine->getParser()->getOutput()->getOutputFlag( ParserOutputFlags::VARY_PAGE_ID ) );
213 }
214
215 public static function provideVaryPageId() {
216 return [
217 'by getCurrentTitle()' => [
218 'ScribuntoTestPage',
219 'mw.title.getCurrentTitle()',
220 true
221 ],
222 'by name' => [
223 'ScribuntoTestPage',
224 'mw.title.new("ScribuntoTestPage")',
225 true
226 ],
227 'by id' => [
228 'ScribuntoTestPage',
229 'mw.title.new( $$ID$$ )',
230 true
231 ],
232
233 'other page by name' => [
234 'ScribuntoTestRedirect',
235 'mw.title.new("ScribuntoTestPage")',
236 false
237 ],
238 'other page by id' => [
239 'ScribuntoTestRedirect',
240 'mw.title.new( $$ID$$ )',
241 false
242 ],
243
244 'new page by getCurrentTitle()' => [
245 'ScribuntoTestPage/DoesNotExist',
246 'mw.title.getCurrentTitle()',
247 true
248 ],
249 'new page by name' => [
250 'ScribuntoTestPage/DoesNotExist',
251 'mw.title.new("ScribuntoTestPage/DoesNotExist")',
252 true
253 ],
254 ];
255 }
256 }