2013-02-07 17:50:21 +00:00
|
|
|
<?php
|
|
|
|
|
2020-12-22 14:21:38 +00:00
|
|
|
use MediaWiki\Interwiki\ClassicInterwikiLookup;
|
2021-09-05 14:25:27 +00:00
|
|
|
use MediaWiki\Page\PageIdentityValue;
|
2021-10-13 20:43:09 +00:00
|
|
|
use MediaWiki\Parser\ParserOutputFlags;
|
2021-09-05 14:25:27 +00:00
|
|
|
use MediaWiki\Permissions\RestrictionStore;
|
2019-10-06 03:17:05 +00:00
|
|
|
|
2018-05-27 00:50:43 +00:00
|
|
|
/**
|
|
|
|
* @covers Scribunto_LuaTitleLibrary
|
2019-08-03 18:05:42 +00:00
|
|
|
* @group Database
|
2018-05-27 00:50:43 +00:00
|
|
|
*/
|
2018-03-10 13:15:42 +00:00
|
|
|
class Scribunto_LuaTitleLibraryTest extends Scribunto_LuaEngineTestBase {
|
2021-01-23 01:33:17 +00:00
|
|
|
/** @inheritDoc */
|
2013-02-07 17:50:21 +00:00
|
|
|
protected static $moduleName = 'TitleLibraryTests';
|
|
|
|
|
2020-01-02 19:51:52 +00:00
|
|
|
/** @var Title|null */
|
|
|
|
private $testTitle = null;
|
|
|
|
|
2021-01-23 01:33:17 +00:00
|
|
|
/** @var int */
|
2020-01-02 19:51:52 +00:00
|
|
|
private $testPageId = null;
|
|
|
|
|
2021-07-23 10:49:13 +00:00
|
|
|
protected function setUp(): void {
|
2020-01-02 19:51:52 +00:00
|
|
|
$this->setTestTitle( null );
|
2013-02-07 17:50:21 +00:00
|
|
|
parent::setUp();
|
|
|
|
|
2020-12-22 14:21:38 +00:00
|
|
|
// Set up interwikis (via wgInterwikiCache) before creating any Titles
|
|
|
|
$this->setMwGlobals( [
|
|
|
|
'wgServer' => '//wiki.local',
|
|
|
|
'wgCanonicalServer' => 'http://wiki.local',
|
|
|
|
'wgUsePathInfo' => true,
|
|
|
|
'wgActionPaths' => [],
|
|
|
|
'wgScript' => '/w/index.php',
|
|
|
|
'wgScriptPath' => '/w',
|
|
|
|
'wgArticlePath' => '/wiki/$1',
|
|
|
|
'wgInterwikiCache' => ClassicInterwikiLookup::buildCdbHash( [
|
|
|
|
[
|
|
|
|
'iw_prefix' => 'interwikiprefix',
|
|
|
|
'iw_url' => '//test.wikipedia.org/wiki/$1',
|
|
|
|
'iw_local' => 0,
|
|
|
|
],
|
|
|
|
] ),
|
|
|
|
] );
|
2013-03-07 00:04:24 +00:00
|
|
|
|
2021-06-24 05:57:16 +00:00
|
|
|
$editor = $this->getTestSysop()->getUser();
|
|
|
|
|
2021-12-16 21:03:32 +00:00
|
|
|
$wikiPageFactory = $this->getServiceContainer()->getWikiPageFactory();
|
|
|
|
|
2013-03-05 02:11:23 +00:00
|
|
|
// Page for getContent test
|
2021-12-16 21:03:32 +00:00
|
|
|
$page = $wikiPageFactory->newFromTitle( Title::newFromText( 'ScribuntoTestPage' ) );
|
2021-06-24 05:57:16 +00:00
|
|
|
$page->doUserEditContent(
|
2016-05-17 14:52:05 +00:00
|
|
|
new WikitextContent(
|
|
|
|
'{{int:mainpage}}<includeonly>...</includeonly><noinclude>...</noinclude>'
|
|
|
|
),
|
2021-06-24 05:57:16 +00:00
|
|
|
$editor,
|
2013-03-05 02:11:23 +00:00
|
|
|
'Summary'
|
|
|
|
);
|
2020-01-02 19:51:52 +00:00
|
|
|
$this->testPageId = $page->getId();
|
2014-10-23 13:15:52 +00:00
|
|
|
|
|
|
|
// Pages for redirectTarget tests
|
2021-12-16 21:03:32 +00:00
|
|
|
$page = $wikiPageFactory->newFromTitle( Title::newFromText( 'ScribuntoTestRedirect' ) );
|
2021-06-24 05:57:16 +00:00
|
|
|
$page->doUserEditContent(
|
2014-10-23 13:15:52 +00:00
|
|
|
new WikitextContent( '#REDIRECT [[ScribuntoTestTarget]]' ),
|
2021-06-24 05:57:16 +00:00
|
|
|
$editor,
|
2014-10-23 13:15:52 +00:00
|
|
|
'Summary'
|
|
|
|
);
|
2021-12-16 21:03:32 +00:00
|
|
|
$page = $wikiPageFactory->newFromTitle( Title::newFromText( 'ScribuntoTestNonRedirect' ) );
|
2021-06-24 05:57:16 +00:00
|
|
|
$page->doUserEditContent(
|
2014-10-23 13:15:52 +00:00
|
|
|
new WikitextContent( 'Not a redirect.' ),
|
2021-06-24 05:57:16 +00:00
|
|
|
$editor,
|
2014-10-23 13:15:52 +00:00
|
|
|
'Summary'
|
|
|
|
);
|
2013-03-05 02:11:23 +00:00
|
|
|
|
2014-05-10 19:05:45 +00:00
|
|
|
// Set restrictions for protectionLevels and cascadingProtection tests
|
2021-09-05 14:25:27 +00:00
|
|
|
|
|
|
|
$restrictionStore = $this->createNoOpMock(
|
|
|
|
RestrictionStore::class,
|
|
|
|
[
|
|
|
|
'getCascadeProtectionSources',
|
|
|
|
'getRestrictions',
|
|
|
|
'areRestrictionsLoaded',
|
|
|
|
'areCascadeProtectionSourcesLoaded',
|
|
|
|
'getAllRestrictions',
|
|
|
|
'registerOldRestrictions' // just do nothing
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->setService( 'RestrictionStore', $restrictionStore );
|
|
|
|
|
|
|
|
$restrictionStore->method( 'areRestrictionsLoaded' )->willReturn( true );
|
|
|
|
$restrictionStore->method( 'areCascadeProtectionSourcesLoaded' )->willReturn( true );
|
|
|
|
|
|
|
|
$restrictions = [
|
|
|
|
'Main_Page' => [ 'edit' => [], 'move' => [] ],
|
|
|
|
'Module:TestFramework' => [
|
|
|
|
'edit' => [ 'sysop', 'bogus' ],
|
|
|
|
'move' => [ 'sysop', 'bogus' ],
|
|
|
|
],
|
|
|
|
'interwikiprefix:Module:TestFramework' => [],
|
|
|
|
'Talk:Has/A/Subpage' => [ 'create' => [ 'sysop' ] ],
|
|
|
|
'Not/A/Subpage' => [ 'edit' => [ 'autoconfirmed' ], 'move' => [ 'sysop' ] ],
|
|
|
|
'Module_talk:Test_Framework' => [ 'edit' => [], 'move' => [ 'sysop' ] ],
|
2017-06-15 17:19:00 +00:00
|
|
|
];
|
2021-09-05 14:25:27 +00:00
|
|
|
|
|
|
|
$restrictionStore->method( 'getAllRestrictions' )
|
|
|
|
->willReturnCallback( static function ( $title ) use ( $restrictions ) {
|
|
|
|
$key = $title->getPrefixedDBkey();
|
|
|
|
return $restrictions[$key] ?? [];
|
|
|
|
} );
|
|
|
|
|
|
|
|
$restrictionStore->method( 'getRestrictions' )
|
|
|
|
->willReturnCallback( static function ( $title, $action ) {
|
|
|
|
$key = $title->getPrefixedDBkey();
|
|
|
|
$pageRestrictions = $restrictions[$key] ?? [];
|
|
|
|
return $pageRestrictions[$action] ?? [];
|
|
|
|
} );
|
|
|
|
|
|
|
|
$restrictionStore->method( 'getCascadeProtectionSources' )
|
|
|
|
->willReturnCallback( static function ( $title ) {
|
|
|
|
if ( $title->getPrefixedDBkey() === 'Main_Page' ) {
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
PageIdentityValue::localIdentity( 5678, NS_MAIN, "Lockbox" ),
|
|
|
|
PageIdentityValue::localIdentity( 8765, NS_MAIN, "Lockbox2" ),
|
|
|
|
],
|
|
|
|
[ 'edit' => [ 'sysop' ] ]
|
|
|
|
];
|
|
|
|
} else {
|
|
|
|
return [ [], [] ];
|
|
|
|
}
|
|
|
|
} );
|
2014-05-10 19:05:45 +00:00
|
|
|
|
2013-02-07 17:50:21 +00:00
|
|
|
// Note this depends on every iteration of the data provider running with a clean parser
|
|
|
|
$this->getEngine()->getParser()->getOptions()->setExpensiveParserFunctionLimit( 10 );
|
|
|
|
|
2013-03-07 00:04:24 +00:00
|
|
|
// Indicate to the tests that it's safe to create the title objects
|
|
|
|
$interpreter = $this->getEngine()->getInterpreter();
|
|
|
|
$interpreter->callFunction(
|
2020-01-02 19:51:52 +00:00
|
|
|
$interpreter->loadString( "mw.title.testPageId = $this->testPageId", 'fortest' )
|
2013-03-07 00:04:24 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-01-02 19:51:52 +00:00
|
|
|
protected function getTestTitle() {
|
|
|
|
return $this->testTitle ?? parent::getTestTitle();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function setTestTitle( $title ) {
|
|
|
|
$this->testTitle = $title !== null ? Title::newFromText( $title ) : null;
|
|
|
|
$this->resetEngine();
|
|
|
|
}
|
|
|
|
|
2014-11-12 11:21:38 +00:00
|
|
|
protected function getTestModules() {
|
2017-06-15 17:19:00 +00:00
|
|
|
return parent::getTestModules() + [
|
2013-02-07 17:50:21 +00:00
|
|
|
'TitleLibraryTests' => __DIR__ . '/TitleLibraryTests.lua',
|
2017-06-15 17:19:00 +00:00
|
|
|
];
|
2013-02-07 17:50:21 +00:00
|
|
|
}
|
|
|
|
|
2014-11-12 11:43:44 +00:00
|
|
|
public function testAddsLinks() {
|
2013-02-07 17:50:21 +00:00
|
|
|
$engine = $this->getEngine();
|
|
|
|
$interpreter = $engine->getInterpreter();
|
|
|
|
|
2013-03-07 18:02:41 +00:00
|
|
|
// Loading a title should create a link
|
2013-02-07 17:50:21 +00:00
|
|
|
$links = $engine->getParser()->getOutput()->getLinks();
|
|
|
|
$this->assertFalse( isset( $links[NS_PROJECT]['Referenced_from_Lua'] ) );
|
|
|
|
|
2016-05-17 14:52:05 +00:00
|
|
|
$interpreter->callFunction( $interpreter->loadString(
|
|
|
|
'local _ = mw.title.new( "Project:Referenced from Lua" ).id', 'reference title'
|
|
|
|
) );
|
2013-02-07 17:50:21 +00:00
|
|
|
|
|
|
|
$links = $engine->getParser()->getOutput()->getLinks();
|
|
|
|
$this->assertArrayHasKey( NS_PROJECT, $links );
|
|
|
|
$this->assertArrayHasKey( 'Referenced_from_Lua', $links[NS_PROJECT] );
|
2013-03-07 18:02:41 +00:00
|
|
|
|
|
|
|
// Loading the page content should create a templatelink
|
|
|
|
$templates = $engine->getParser()->getOutput()->getTemplates();
|
|
|
|
$this->assertFalse( isset( $links[NS_PROJECT]['Loaded_from_Lua'] ) );
|
|
|
|
|
2016-05-17 14:52:05 +00:00
|
|
|
$interpreter->callFunction( $interpreter->loadString(
|
|
|
|
'mw.title.new( "Project:Loaded from Lua" ):getContent()', 'load title'
|
|
|
|
) );
|
2013-03-07 18:02:41 +00:00
|
|
|
|
|
|
|
$templates = $engine->getParser()->getOutput()->getTemplates();
|
|
|
|
$this->assertArrayHasKey( NS_PROJECT, $templates );
|
|
|
|
$this->assertArrayHasKey( 'Loaded_from_Lua', $templates[NS_PROJECT] );
|
2013-02-07 17:50:21 +00:00
|
|
|
}
|
2020-01-02 19:51:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideVaryPageId
|
|
|
|
*/
|
|
|
|
public function testVaryPageId( $testTitle, $code, $flag ) {
|
|
|
|
$this->setTestTitle( $testTitle );
|
|
|
|
|
|
|
|
$code = strtr( $code, [ '$$ID$$' => $this->testPageId ] );
|
|
|
|
|
|
|
|
$engine = $this->getEngine();
|
|
|
|
$interpreter = $engine->getInterpreter();
|
|
|
|
$this->assertFalse(
|
2021-10-13 20:43:09 +00:00
|
|
|
$engine->getParser()->getOutput()->getOutputFlag( ParserOutputFlags::VARY_PAGE_ID ), 'sanity check'
|
2020-01-02 19:51:52 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$interpreter->callFunction( $interpreter->loadString(
|
|
|
|
"local _ = $code", 'reference title but not id'
|
|
|
|
) );
|
2021-10-13 20:43:09 +00:00
|
|
|
$this->assertFalse( $engine->getParser()->getOutput()->getOutputFlag( ParserOutputFlags::VARY_PAGE_ID ) );
|
2020-01-02 19:51:52 +00:00
|
|
|
|
|
|
|
$interpreter->callFunction( $interpreter->loadString(
|
|
|
|
"local _ = $code.id", 'reference id'
|
|
|
|
) );
|
2021-10-13 20:43:09 +00:00
|
|
|
$this->assertSame( $flag, $engine->getParser()->getOutput()->getOutputFlag( ParserOutputFlags::VARY_PAGE_ID ) );
|
2020-01-02 19:51:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function provideVaryPageId() {
|
|
|
|
return [
|
|
|
|
'by getCurrentTitle()' => [
|
|
|
|
'ScribuntoTestPage',
|
|
|
|
'mw.title.getCurrentTitle()',
|
|
|
|
true
|
|
|
|
],
|
|
|
|
'by name' => [
|
|
|
|
'ScribuntoTestPage',
|
|
|
|
'mw.title.new("ScribuntoTestPage")',
|
|
|
|
true
|
|
|
|
],
|
|
|
|
'by id' => [
|
|
|
|
'ScribuntoTestPage',
|
|
|
|
'mw.title.new( $$ID$$ )',
|
|
|
|
true
|
|
|
|
],
|
|
|
|
|
|
|
|
'other page by name' => [
|
|
|
|
'ScribuntoTestRedirect',
|
|
|
|
'mw.title.new("ScribuntoTestPage")',
|
|
|
|
false
|
|
|
|
],
|
|
|
|
'other page by id' => [
|
|
|
|
'ScribuntoTestRedirect',
|
|
|
|
'mw.title.new( $$ID$$ )',
|
|
|
|
false
|
|
|
|
],
|
|
|
|
|
|
|
|
'new page by getCurrentTitle()' => [
|
|
|
|
'ScribuntoTestPage/DoesNotExist',
|
|
|
|
'mw.title.getCurrentTitle()',
|
|
|
|
true
|
|
|
|
],
|
|
|
|
'new page by name' => [
|
|
|
|
'ScribuntoTestPage/DoesNotExist',
|
|
|
|
'mw.title.new("ScribuntoTestPage/DoesNotExist")',
|
|
|
|
true
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
2013-02-07 17:50:21 +00:00
|
|
|
}
|