2013-02-07 17:50:21 +00:00
|
|
|
<?php
|
|
|
|
|
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 {
|
2013-02-07 17:50:21 +00:00
|
|
|
protected static $moduleName = 'TitleLibraryTests';
|
|
|
|
|
|
|
|
public static function suite( $className ) {
|
|
|
|
global $wgInterwikiCache;
|
|
|
|
if ( $wgInterwikiCache ) {
|
|
|
|
$suite = new PHPUnit_Framework_TestSuite;
|
|
|
|
$suite->setName( $className );
|
|
|
|
$suite->addTest(
|
|
|
|
new Scribunto_LuaEngineTestSkip(
|
|
|
|
$className, 'Cannot run TitleLibrary tests when $wgInterwikiCache is set'
|
2017-06-15 17:19:00 +00:00
|
|
|
), [ 'Lua' ]
|
2013-02-07 17:50:21 +00:00
|
|
|
);
|
|
|
|
return $suite;
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent::suite( $className );
|
|
|
|
}
|
|
|
|
|
2014-11-12 11:21:38 +00:00
|
|
|
protected function setUp() {
|
2013-03-07 00:04:24 +00:00
|
|
|
global $wgHooks;
|
|
|
|
|
2013-02-07 17:50:21 +00:00
|
|
|
parent::setUp();
|
|
|
|
|
2013-03-07 00:04:24 +00:00
|
|
|
// Hook to inject our interwiki prefix
|
|
|
|
$this->hooks = $wgHooks;
|
|
|
|
$wgHooks['InterwikiLoadPrefix'][] = function ( $prefix, &$data ) {
|
2017-01-19 08:47:26 +00:00
|
|
|
if ( $prefix !== 'interwikiprefix' ) {
|
2013-03-07 00:04:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-06-15 17:19:00 +00:00
|
|
|
$data = [
|
2017-01-19 08:47:26 +00:00
|
|
|
'iw_prefix' => 'interwikiprefix',
|
2013-03-07 00:04:24 +00:00
|
|
|
'iw_url' => '//test.wikipedia.org/wiki/$1',
|
|
|
|
'iw_api' => 1,
|
|
|
|
'iw_wikiid' => 0,
|
|
|
|
'iw_local' => 0,
|
|
|
|
'iw_trans' => 0,
|
2017-06-15 17:19:00 +00:00
|
|
|
];
|
2013-03-07 00:04:24 +00:00
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2013-03-05 02:11:23 +00:00
|
|
|
// Page for getContent test
|
|
|
|
$page = WikiPage::factory( Title::newFromText( 'ScribuntoTestPage' ) );
|
|
|
|
$page->doEditContent(
|
2016-05-17 14:52:05 +00:00
|
|
|
new WikitextContent(
|
|
|
|
'{{int:mainpage}}<includeonly>...</includeonly><noinclude>...</noinclude>'
|
|
|
|
),
|
2013-03-05 02:11:23 +00:00
|
|
|
'Summary'
|
|
|
|
);
|
2014-10-23 13:15:52 +00:00
|
|
|
$testPageId = $page->getId();
|
|
|
|
|
|
|
|
// Pages for redirectTarget tests
|
|
|
|
$page = WikiPage::factory( Title::newFromText( 'ScribuntoTestRedirect' ) );
|
|
|
|
$page->doEditContent(
|
|
|
|
new WikitextContent( '#REDIRECT [[ScribuntoTestTarget]]' ),
|
|
|
|
'Summary'
|
|
|
|
);
|
|
|
|
$page = WikiPage::factory( Title::newFromText( 'ScribuntoTestNonRedirect' ) );
|
|
|
|
$page->doEditContent(
|
|
|
|
new WikitextContent( 'Not a redirect.' ),
|
|
|
|
'Summary'
|
|
|
|
);
|
2013-03-05 02:11:23 +00:00
|
|
|
|
2014-05-10 19:05:45 +00:00
|
|
|
// Set restrictions for protectionLevels and cascadingProtection tests
|
2014-01-12 20:08:20 +00:00
|
|
|
// Since mRestrictionsLoaded is true, they don't count as expensive
|
|
|
|
$title = Title::newFromText( 'Main Page' );
|
|
|
|
$title->mRestrictionsLoaded = true;
|
2017-06-15 17:19:00 +00:00
|
|
|
$title->mRestrictions = [ 'edit' => [], 'move' => [] ];
|
|
|
|
$title->mCascadeSources = [
|
2016-05-17 14:52:05 +00:00
|
|
|
Title::makeTitle( NS_MAIN, "Lockbox" ),
|
|
|
|
Title::makeTitle( NS_MAIN, "Lockbox2" ),
|
2017-06-15 17:19:00 +00:00
|
|
|
];
|
|
|
|
$title->mCascadingRestrictions = [ 'edit' => [ 'sysop' ] ];
|
2014-01-12 20:08:20 +00:00
|
|
|
$title = Title::newFromText( 'Module:TestFramework' );
|
|
|
|
$title->mRestrictionsLoaded = true;
|
2017-06-15 17:19:00 +00:00
|
|
|
$title->mRestrictions = [
|
|
|
|
'edit' => [ 'sysop', 'bogus' ],
|
|
|
|
'move' => [ 'sysop', 'bogus' ],
|
|
|
|
];
|
|
|
|
$title->mCascadeSources = [];
|
|
|
|
$title->mCascadingRestrictions = [];
|
2017-01-19 08:47:26 +00:00
|
|
|
$title = Title::newFromText( 'interwikiprefix:Module:TestFramework' );
|
2014-01-12 20:08:20 +00:00
|
|
|
$title->mRestrictionsLoaded = true;
|
2017-06-15 17:19:00 +00:00
|
|
|
$title->mRestrictions = [];
|
|
|
|
$title->mCascadeSources = [];
|
|
|
|
$title->mCascadingRestrictions = [];
|
2014-01-12 20:08:20 +00:00
|
|
|
$title = Title::newFromText( 'Talk:Has/A/Subpage' );
|
|
|
|
$title->mRestrictionsLoaded = true;
|
2017-06-15 17:19:00 +00:00
|
|
|
$title->mRestrictions = [ 'create' => [ 'sysop' ] ];
|
|
|
|
$title->mCascadeSources = [];
|
|
|
|
$title->mCascadingRestrictions = [];
|
2014-01-12 20:08:20 +00:00
|
|
|
$title = Title::newFromText( 'Not/A/Subpage' );
|
|
|
|
$title->mRestrictionsLoaded = true;
|
2017-06-15 17:19:00 +00:00
|
|
|
$title->mRestrictions = [ 'edit' => [ 'autoconfirmed' ], 'move' => [ 'sysop' ] ];
|
|
|
|
$title->mCascadeSources = [];
|
|
|
|
$title->mCascadingRestrictions = [];
|
2014-01-12 20:08:20 +00:00
|
|
|
$title = Title::newFromText( 'Module talk:Test Framework' );
|
|
|
|
$title->mRestrictionsLoaded = true;
|
2017-06-15 17:19:00 +00:00
|
|
|
$title->mRestrictions = [ 'edit' => [], 'move' => [ 'sysop' ] ];
|
|
|
|
$title->mCascadeSources = [];
|
|
|
|
$title->mCascadingRestrictions = [];
|
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(
|
2014-10-23 13:15:52 +00:00
|
|
|
$interpreter->loadString( "mw.title.testPageId = $testPageId", 'fortest' )
|
2013-03-07 00:04:24 +00:00
|
|
|
);
|
|
|
|
|
2017-06-15 17:19:00 +00:00
|
|
|
$this->setMwGlobals( [
|
2013-02-07 17:50:21 +00:00
|
|
|
'wgServer' => '//wiki.local',
|
|
|
|
'wgCanonicalServer' => 'http://wiki.local',
|
|
|
|
'wgUsePathInfo' => true,
|
2017-06-15 17:19:00 +00:00
|
|
|
'wgActionPaths' => [],
|
2013-02-07 17:50:21 +00:00
|
|
|
'wgScript' => '/w/index.php',
|
|
|
|
'wgScriptPath' => '/w',
|
|
|
|
'wgArticlePath' => '/wiki/$1',
|
2017-06-15 17:19:00 +00:00
|
|
|
] );
|
2013-02-07 17:50:21 +00:00
|
|
|
}
|
|
|
|
|
2014-11-12 11:21:38 +00:00
|
|
|
protected function tearDown() {
|
2013-03-07 00:04:24 +00:00
|
|
|
global $wgHooks;
|
|
|
|
$wgHooks = $this->hooks;
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|