MediaWiki  1.34.4
TitleLibraryTest.php
Go to the documentation of this file.
1 <?php
2 
8  protected static $moduleName = 'TitleLibraryTests';
9 
10  public static function suite( $className ) {
11  global $wgInterwikiCache;
12  if ( $wgInterwikiCache ) {
13  $suite = new PHPUnit_Framework_TestSuite;
14  $suite->setName( $className );
15  $suite->addTest(
17  $className, 'Cannot run TitleLibrary tests when $wgInterwikiCache is set'
18  ), [ 'Lua' ]
19  );
20  return $suite;
21  }
22 
23  return parent::suite( $className );
24  }
25 
26  protected function setUp() {
27  global $wgHooks;
28 
29  parent::setUp();
30 
31  // Hook to inject our interwiki prefix
32  $this->hooks = $wgHooks;
33  $wgHooks['InterwikiLoadPrefix'][] = function ( $prefix, &$data ) {
34  if ( $prefix !== 'interwikiprefix' ) {
35  return true;
36  }
37 
38  $data = [
39  'iw_prefix' => 'interwikiprefix',
40  'iw_url' => '//test.wikipedia.org/wiki/$1',
41  'iw_api' => 1,
42  'iw_wikiid' => 0,
43  'iw_local' => 0,
44  'iw_trans' => 0,
45  ];
46  return false;
47  };
48 
49  // Page for getContent test
50  $page = WikiPage::factory( Title::newFromText( 'ScribuntoTestPage' ) );
51  $page->doEditContent(
52  new WikitextContent(
53  '{{int:mainpage}}<includeonly>...</includeonly><noinclude>...</noinclude>'
54  ),
55  'Summary'
56  );
57  $testPageId = $page->getId();
58 
59  // Pages for redirectTarget tests
60  $page = WikiPage::factory( Title::newFromText( 'ScribuntoTestRedirect' ) );
61  $page->doEditContent(
62  new WikitextContent( '#REDIRECT [[ScribuntoTestTarget]]' ),
63  'Summary'
64  );
65  $page = WikiPage::factory( Title::newFromText( 'ScribuntoTestNonRedirect' ) );
66  $page->doEditContent(
67  new WikitextContent( 'Not a redirect.' ),
68  'Summary'
69  );
70 
71  // Set restrictions for protectionLevels and cascadingProtection tests
72  // Since mRestrictionsLoaded is true, they don't count as expensive
73  $title = Title::newFromText( 'Main Page' );
74  $title->mRestrictionsLoaded = true;
75  $title->mRestrictions = [ 'edit' => [], 'move' => [] ];
76  $title->mCascadeSources = [
77  Title::makeTitle( NS_MAIN, "Lockbox" ),
78  Title::makeTitle( NS_MAIN, "Lockbox2" ),
79  ];
80  $title->mCascadingRestrictions = [ 'edit' => [ 'sysop' ] ];
81  $title = Title::newFromText( 'Module:TestFramework' );
82  $title->mRestrictionsLoaded = true;
83  $title->mRestrictions = [
84  'edit' => [ 'sysop', 'bogus' ],
85  'move' => [ 'sysop', 'bogus' ],
86  ];
87  $title->mCascadeSources = [];
88  $title->mCascadingRestrictions = [];
89  $title = Title::newFromText( 'interwikiprefix:Module:TestFramework' );
90  $title->mRestrictionsLoaded = true;
91  $title->mRestrictions = [];
92  $title->mCascadeSources = [];
93  $title->mCascadingRestrictions = [];
94  $title = Title::newFromText( 'Talk:Has/A/Subpage' );
95  $title->mRestrictionsLoaded = true;
96  $title->mRestrictions = [ 'create' => [ 'sysop' ] ];
97  $title->mCascadeSources = [];
98  $title->mCascadingRestrictions = [];
99  $title = Title::newFromText( 'Not/A/Subpage' );
100  $title->mRestrictionsLoaded = true;
101  $title->mRestrictions = [ 'edit' => [ 'autoconfirmed' ], 'move' => [ 'sysop' ] ];
102  $title->mCascadeSources = [];
103  $title->mCascadingRestrictions = [];
104  $title = Title::newFromText( 'Module talk:Test Framework' );
105  $title->mRestrictionsLoaded = true;
106  $title->mRestrictions = [ 'edit' => [], 'move' => [ 'sysop' ] ];
107  $title->mCascadeSources = [];
108  $title->mCascadingRestrictions = [];
109 
110  // Note this depends on every iteration of the data provider running with a clean parser
111  $this->getEngine()->getParser()->getOptions()->setExpensiveParserFunctionLimit( 10 );
112 
113  // Indicate to the tests that it's safe to create the title objects
114  $interpreter = $this->getEngine()->getInterpreter();
115  $interpreter->callFunction(
116  $interpreter->loadString( "mw.title.testPageId = $testPageId", 'fortest' )
117  );
118 
119  $this->setMwGlobals( [
120  'wgServer' => '//wiki.local',
121  'wgCanonicalServer' => 'http://wiki.local',
122  'wgUsePathInfo' => true,
123  'wgActionPaths' => [],
124  'wgScript' => '/w/index.php',
125  'wgScriptPath' => '/w',
126  'wgArticlePath' => '/wiki/$1',
127  ] );
128  }
129 
130  protected function tearDown() {
131  global $wgHooks;
132  $wgHooks = $this->hooks;
133  parent::tearDown();
134  }
135 
136  protected function getTestModules() {
137  return parent::getTestModules() + [
138  'TitleLibraryTests' => __DIR__ . '/TitleLibraryTests.lua',
139  ];
140  }
141 
142  public function testAddsLinks() {
143  $engine = $this->getEngine();
144  $interpreter = $engine->getInterpreter();
145 
146  // Loading a title should create a link
147  $links = $engine->getParser()->getOutput()->getLinks();
148  $this->assertFalse( isset( $links[NS_PROJECT]['Referenced_from_Lua'] ) );
149 
150  $interpreter->callFunction( $interpreter->loadString(
151  'local _ = mw.title.new( "Project:Referenced from Lua" ).id', 'reference title'
152  ) );
153 
154  $links = $engine->getParser()->getOutput()->getLinks();
155  $this->assertArrayHasKey( NS_PROJECT, $links );
156  $this->assertArrayHasKey( 'Referenced_from_Lua', $links[NS_PROJECT] );
157 
158  // Loading the page content should create a templatelink
159  $templates = $engine->getParser()->getOutput()->getTemplates();
160  $this->assertFalse( isset( $links[NS_PROJECT]['Loaded_from_Lua'] ) );
161 
162  $interpreter->callFunction( $interpreter->loadString(
163  'mw.title.new( "Project:Loaded from Lua" ):getContent()', 'load title'
164  ) );
165 
166  $templates = $engine->getParser()->getOutput()->getTemplates();
167  $this->assertArrayHasKey( NS_PROJECT, $templates );
168  $this->assertArrayHasKey( 'Loaded_from_Lua', $templates[NS_PROJECT] );
169  }
170 }
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:316
Scribunto_LuaEngineTestSkip
Definition: LuaEngineTestBase.php:128
$wgInterwikiCache
bool array string $wgInterwikiCache
Interwiki cache, either as an associative array or a path to a constant database (....
Definition: DefaultSettings.php:3989
$wgHooks
$wgHooks['AdminLinks'][]
Definition: ReplaceText.php:58
Scribunto_LuaTitleLibraryTest\setUp
setUp()
Definition: TitleLibraryTest.php:26
NS_MAIN
const NS_MAIN
Definition: Defines.php:69
Scribunto_LuaTitleLibraryTest\getTestModules
getTestModules()
Definition: TitleLibraryTest.php:136
WikiPage\factory
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:142
NS_PROJECT
const NS_PROJECT
Definition: Defines.php:73
Scribunto_LuaTitleLibraryTest
@covers Scribunto_LuaTitleLibrary @group Database
Definition: TitleLibraryTest.php:7
$title
$title
Definition: testCompression.php:34
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:586
WikitextContent
Content object for wiki text pages.
Definition: WikitextContent.php:36
Scribunto_LuaEngineTestBase
This is the subclass for Lua library tests.
Definition: LuaEngineTestBase.php:12
Scribunto_LuaTitleLibraryTest\suite
static suite( $className)
Definition: TitleLibraryTest.php:10
Scribunto_LuaEngineTestBase\$engine
$engine
Definition: LuaEngineTestBase.php:17
Scribunto_LuaTitleLibraryTest\$moduleName
static $moduleName
Definition: TitleLibraryTest.php:8
getEngine
getEngine()
Definition: LuaEngineTestHelper.php:109
Scribunto_LuaTitleLibraryTest\tearDown
tearDown()
Definition: TitleLibraryTest.php:130
Scribunto_LuaTitleLibraryTest\testAddsLinks
testAddsLinks()
Definition: TitleLibraryTest.php:142