MediaWiki  1.33.0
SpecialPageFactoryTest.php
Go to the documentation of this file.
1 <?php
2 
4 use Wikimedia\ScopedCallback;
5 
28  public function testHookNotCalledTwice() {
29  $count = 0;
30  $this->mergeMwGlobalArrayValue( 'wgHooks', [
31  'SpecialPage_initList' => [
32  function () use ( &$count ) {
33  $count++;
34  }
35  ] ] );
36  $this->overrideMwServices();
37  $spf = MediaWikiServices::getInstance()->getSpecialPageFactory();
38  $spf->getNames();
39  $spf->getNames();
40  $this->assertEquals( 1, $count );
41  }
42 
43  public function newSpecialAllPages() {
44  return new SpecialAllPages();
45  }
46 
47  public function specialPageProvider() {
48  $specialPageTestHelper = new SpecialPageTestHelper();
49 
50  return [
51  'class name' => [ 'SpecialAllPages', false ],
52  'closure' => [ function () {
53  return new SpecialAllPages();
54  }, false ],
55  'function' => [ [ $this, 'newSpecialAllPages' ], false ],
56  'callback string' => [ 'SpecialPageTestHelper::newSpecialAllPages', false ],
57  'callback with object' => [
58  [ $specialPageTestHelper, 'newSpecialAllPages' ],
59  false
60  ],
61  'callback array' => [
62  [ 'SpecialPageTestHelper', 'newSpecialAllPages' ],
63  false
64  ]
65  ];
66  }
67 
72  public function testGetPage( $spec, $shouldReuseInstance ) {
73  $this->mergeMwGlobalArrayValue( 'wgSpecialPages', [ 'testdummy' => $spec ] );
74  $this->overrideMwServices();
75 
76  $page = SpecialPageFactory::getPage( 'testdummy' );
77  $this->assertInstanceOf( SpecialPage::class, $page );
78 
79  $page2 = SpecialPageFactory::getPage( 'testdummy' );
80  $this->assertEquals( $shouldReuseInstance, $page2 === $page, "Should re-use instance:" );
81  }
82 
86  public function testGetNames() {
87  $this->mergeMwGlobalArrayValue( 'wgSpecialPages', [ 'testdummy' => SpecialAllPages::class ] );
88  $this->overrideMwServices();
89 
91  $this->assertInternalType( 'array', $names );
92  $this->assertContains( 'testdummy', $names );
93  }
94 
98  public function testResolveAlias() {
99  $this->setContentLang( 'de' );
100  $this->overrideMwServices();
101 
102  list( $name, $param ) = SpecialPageFactory::resolveAlias( 'Spezialseiten/Foo' );
103  $this->assertEquals( 'Specialpages', $name );
104  $this->assertEquals( 'Foo', $param );
105  }
106 
110  public function testGetLocalNameFor() {
111  $this->setContentLang( 'de' );
112  $this->overrideMwServices();
113 
114  $name = SpecialPageFactory::getLocalNameFor( 'Specialpages', 'Foo' );
115  $this->assertEquals( 'Spezialseiten/Foo', $name );
116  }
117 
121  public function testGetTitleForAlias() {
122  $this->setContentLang( 'de' );
123  $this->overrideMwServices();
124 
125  $title = SpecialPageFactory::getTitleForAlias( 'Specialpages/Foo' );
126  $this->assertEquals( 'Spezialseiten/Foo', $title->getText() );
127  $this->assertEquals( NS_SPECIAL, $title->getNamespace() );
128  }
129 
133  public function testConflictResolution(
134  $test, $aliasesList, $alias, $expectedName, $expectedAlias, $expectWarnings
135  ) {
136  $lang = clone MediaWikiServices::getInstance()->getContentLanguage();
137  $lang->mExtendedSpecialPageAliases = $aliasesList;
138  $this->setMwGlobals( 'wgSpecialPages',
139  array_combine( array_keys( $aliasesList ), array_keys( $aliasesList ) )
140  );
141  $this->overrideMwServices();
142  $this->setContentLang( $lang );
143 
144  // Catch the warnings we expect to be raised
145  $warnings = [];
146  $this->setMwGlobals( 'wgDevelopmentWarnings', true );
147  set_error_handler( function ( $errno, $errstr ) use ( &$warnings ) {
148  if ( preg_match( '/First alias \'[^\']*\' for .*/', $errstr ) ||
149  preg_match( '/Did not find a usable alias for special page .*/', $errstr )
150  ) {
151  $warnings[] = $errstr;
152  return true;
153  }
154  return false;
155  } );
156  $reset = new ScopedCallback( 'restore_error_handler' );
157 
158  list( $name, /*...*/ ) = SpecialPageFactory::resolveAlias( $alias );
159  $this->assertEquals( $expectedName, $name, "$test: Alias to name" );
161  $this->assertEquals( $expectedAlias, $result, "$test: Alias to name to alias" );
162 
163  $gotWarnings = count( $warnings );
164  if ( $gotWarnings !== $expectWarnings ) {
165  $this->fail( "Expected $expectWarnings warning(s), but got $gotWarnings:\n" .
166  implode( "\n", $warnings )
167  );
168  }
169  }
170 
175  $test, $aliasesList, $alias, $expectedName, $expectedAlias, $expectWarnings
176  ) {
177  // Make sure order doesn't matter by reversing the list
178  $aliasesList = array_reverse( $aliasesList );
179  return $this->testConflictResolution(
180  $test, $aliasesList, $alias, $expectedName, $expectedAlias, $expectWarnings
181  );
182  }
183 
184  public function provideTestConflictResolution() {
185  return [
186  [
187  'Canonical name wins',
188  [ 'Foo' => [ 'Foo', 'Bar' ], 'Baz' => [ 'Foo', 'BazPage', 'Baz2' ] ],
189  'Foo',
190  'Foo',
191  'Foo',
192  1,
193  ],
194 
195  [
196  'Doesn\'t redirect to a different special page\'s canonical name',
197  [ 'Foo' => [ 'Foo', 'Bar' ], 'Baz' => [ 'Foo', 'BazPage', 'Baz2' ] ],
198  'Baz',
199  'Baz',
200  'BazPage',
201  1,
202  ],
203 
204  [
205  'Canonical name wins even if not aliased',
206  [ 'Foo' => [ 'FooPage' ], 'Baz' => [ 'Foo', 'BazPage', 'Baz2' ] ],
207  'Foo',
208  'Foo',
209  'FooPage',
210  1,
211  ],
212 
213  [
214  'Doesn\'t redirect to a different special page\'s canonical name even if not aliased',
215  [ 'Foo' => [ 'FooPage' ], 'Baz' => [ 'Foo', 'BazPage', 'Baz2' ] ],
216  'Baz',
217  'Baz',
218  'BazPage',
219  1,
220  ],
221 
222  [
223  'First local name beats non-first',
224  [ 'First' => [ 'Foo' ], 'NonFirst' => [ 'Bar', 'Foo' ] ],
225  'Foo',
226  'First',
227  'Foo',
228  0,
229  ],
230 
231  [
232  'Doesn\'t redirect to a different special page\'s first alias',
233  [
234  'Foo' => [ 'Foo' ],
235  'First' => [ 'Bar' ],
236  'Baz' => [ 'Foo', 'Bar', 'BazPage', 'Baz2' ]
237  ],
238  'Baz',
239  'Baz',
240  'BazPage',
241  1,
242  ],
243 
244  [
245  'Doesn\'t redirect wrong even if all aliases conflict',
246  [
247  'Foo' => [ 'Foo' ],
248  'First' => [ 'Bar' ],
249  'Baz' => [ 'Foo', 'Bar' ]
250  ],
251  'Baz',
252  'Baz',
253  'Baz',
254  2,
255  ],
256 
257  ];
258  }
259 
260  public function testGetAliasListRecursion() {
261  $called = false;
262  $this->mergeMwGlobalArrayValue( 'wgHooks', [
263  'SpecialPage_initList' => [
264  function () use ( &$called ) {
265  SpecialPageFactory::getLocalNameFor( 'Specialpages' );
266  $called = true;
267  }
268  ],
269  ] );
270  $this->overrideMwServices();
271  SpecialPageFactory::getLocalNameFor( 'Specialpages' );
272  $this->assertTrue( $called, 'Recursive call succeeded' );
273  }
274 
275 }
SpecialPageFactoryTest\testResolveAlias
testResolveAlias()
SpecialPageFactory::resolveAlias.
Definition: SpecialPageFactoryTest.php:98
SpecialPageFactoryTest\provideTestConflictResolution
provideTestConflictResolution()
Definition: SpecialPageFactoryTest.php:184
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
MediaWikiTestCase\mergeMwGlobalArrayValue
mergeMwGlobalArrayValue( $name, $values)
Merges the given values into a MW global array variable.
Definition: MediaWikiTestCase.php:904
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:33
captcha-old.count
count
Definition: captcha-old.py:249
SpecialPageFactoryTest\testGetTitleForAlias
testGetTitleForAlias()
SpecialPageFactory::getTitleForAlias.
Definition: SpecialPageFactoryTest.php:121
$result
The index of the header message $result[1]=The index of the body text message $result[2 through n]=Parameters passed to body text message. Please note the header message cannot receive/use parameters. 'ImportHandleLogItemXMLTag':When parsing a XML tag in a log item. Return false to stop further processing of the tag $reader:XMLReader object $logInfo:Array of information 'ImportHandlePageXMLTag':When parsing a XML tag in a page. Return false to stop further processing of the tag $reader:XMLReader object & $pageInfo:Array of information 'ImportHandleRevisionXMLTag':When parsing a XML tag in a page revision. Return false to stop further processing of the tag $reader:XMLReader object $pageInfo:Array of page information $revisionInfo:Array of revision information 'ImportHandleToplevelXMLTag':When parsing a top level XML tag. Return false to stop further processing of the tag $reader:XMLReader object 'ImportHandleUnknownUser':When a user doesn 't exist locally, this hook is called to give extensions an opportunity to auto-create it. If the auto-creation is successful, return false. $name:User name 'ImportHandleUploadXMLTag':When parsing a XML tag in a file upload. Return false to stop further processing of the tag $reader:XMLReader object $revisionInfo:Array of information 'ImportLogInterwikiLink':Hook to change the interwiki link used in log entries and edit summaries for transwiki imports. & $fullInterwikiPrefix:Interwiki prefix, may contain colons. & $pageTitle:String that contains page title. 'ImportSources':Called when reading from the $wgImportSources configuration variable. Can be used to lazy-load the import sources list. & $importSources:The value of $wgImportSources. Modify as necessary. See the comment in DefaultSettings.php for the detail of how to structure this array. 'InfoAction':When building information to display on the action=info page. $context:IContextSource object & $pageInfo:Array of information 'InitializeArticleMaybeRedirect':MediaWiki check to see if title is a redirect. & $title:Title object for the current page & $request:WebRequest & $ignoreRedirect:boolean to skip redirect check & $target:Title/string of redirect target & $article:Article object 'InternalParseBeforeLinks':during Parser 's internalParse method before links but after nowiki/noinclude/includeonly/onlyinclude and other processings. & $parser:Parser object & $text:string containing partially parsed text & $stripState:Parser 's internal StripState object 'InternalParseBeforeSanitize':during Parser 's internalParse method just before the parser removes unwanted/dangerous HTML tags and after nowiki/noinclude/includeonly/onlyinclude and other processings. Ideal for syntax-extensions after template/parser function execution which respect nowiki and HTML-comments. & $parser:Parser object & $text:string containing partially parsed text & $stripState:Parser 's internal StripState object 'InterwikiLoadPrefix':When resolving if a given prefix is an interwiki or not. Return true without providing an interwiki to continue interwiki search. $prefix:interwiki prefix we are looking for. & $iwData:output array describing the interwiki with keys iw_url, iw_local, iw_trans and optionally iw_api and iw_wikiid. 'InvalidateEmailComplete':Called after a user 's email has been invalidated successfully. $user:user(object) whose email is being invalidated 'IRCLineURL':When constructing the URL to use in an IRC notification. Callee may modify $url and $query, URL will be constructed as $url . $query & $url:URL to index.php & $query:Query string $rc:RecentChange object that triggered url generation 'IsFileCacheable':Override the result of Article::isFileCacheable()(if true) & $article:article(object) being checked 'IsTrustedProxy':Override the result of IP::isTrustedProxy() & $ip:IP being check & $result:Change this value to override the result of IP::isTrustedProxy() 'IsUploadAllowedFromUrl':Override the result of UploadFromUrl::isAllowedUrl() $url:URL used to upload from & $allowed:Boolean indicating if uploading is allowed for given URL 'isValidEmailAddr':Override the result of Sanitizer::validateEmail(), for instance to return false if the domain name doesn 't match your organization. $addr:The e-mail address entered by the user & $result:Set this and return false to override the internal checks 'isValidPassword':Override the result of User::isValidPassword() $password:The password entered by the user & $result:Set this and return false to override the internal checks $user:User the password is being validated for 'Language::getMessagesFileName':$code:The language code or the language we 're looking for a messages file for & $file:The messages file path, you can override this to change the location. 'LanguageGetNamespaces':Provide custom ordering for namespaces or remove namespaces. Do not use this hook to add namespaces. Use CanonicalNamespaces for that. & $namespaces:Array of namespaces indexed by their numbers 'LanguageGetTranslatedLanguageNames':Provide translated language names. & $names:array of language code=> language name $code:language of the preferred translations 'LanguageLinks':Manipulate a page 's language links. This is called in various places to allow extensions to define the effective language links for a page. $title:The page 's Title. & $links:Array with elements of the form "language:title" in the order that they will be output. & $linkFlags:Associative array mapping prefixed links to arrays of flags. Currently unused, but planned to provide support for marking individual language links in the UI, e.g. for featured articles. 'LanguageSelector':Hook to change the language selector available on a page. $out:The output page. $cssClassName:CSS class name of the language selector. 'LinkBegin':DEPRECATED since 1.28! Use HtmlPageLinkRendererBegin instead. Used when generating internal and interwiki links in Linker::link(), before processing starts. Return false to skip default processing and return $ret. See documentation for Linker::link() for details on the expected meanings of parameters. $skin:the Skin object $target:the Title that the link is pointing to & $html:the contents that the< a > tag should have(raw HTML) $result
Definition: hooks.txt:1983
SpecialPageFactoryTest\testConflictResolutionReversed
testConflictResolutionReversed( $test, $aliasesList, $alias, $expectedName, $expectedAlias, $expectWarnings)
provideTestConflictResolution
Definition: SpecialPageFactoryTest.php:174
SpecialPageFactoryTest\testGetNames
testGetNames()
SpecialPageFactory::getNames.
Definition: SpecialPageFactoryTest.php:86
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
SpecialPageFactoryTest\testHookNotCalledTwice
testHookNotCalledTwice()
Definition: SpecialPageFactoryTest.php:28
MediaWikiTestCase\overrideMwServices
overrideMwServices(Config $configOverrides=null, array $services=[])
Stashes the global instance of MediaWikiServices, and installs a new one, allowing test cases to over...
Definition: MediaWikiTestCase.php:937
SpecialPageFactoryTest\testConflictResolution
testConflictResolution( $test, $aliasesList, $alias, $expectedName, $expectedAlias, $expectWarnings)
provideTestConflictResolution
Definition: SpecialPageFactoryTest.php:133
MediaWikiTestCase\$called
$called
$called tracks whether the setUp and tearDown method has been called.
Definition: MediaWikiTestCase.php:47
SpecialPageTestHelper
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...
Definition: SpecialPageTestHelper.php:18
NS_SPECIAL
const NS_SPECIAL
Definition: Defines.php:53
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:925
SpecialPageFactoryTest\testGetLocalNameFor
testGetLocalNameFor()
SpecialPageFactory::getLocalNameFor.
Definition: SpecialPageFactoryTest.php:110
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
Definition: MediaWikiTestCase.php:709
MediaWikiTestCase
Definition: MediaWikiTestCase.php:17
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
SpecialPageFactoryTest\newSpecialAllPages
newSpecialAllPages()
Definition: SpecialPageFactoryTest.php:43
list
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
SpecialPageFactoryTest
Factory for handling the special page list and generating SpecialPage objects.
Definition: SpecialPageFactoryTest.php:27
MediaWikiTestCase\setContentLang
setContentLang( $lang)
Definition: MediaWikiTestCase.php:1066
SpecialPageFactoryTest\testGetPage
testGetPage( $spec, $shouldReuseInstance)
SpecialPageFactory::getPage specialPageProvider.
Definition: SpecialPageFactoryTest.php:72
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:271
SpecialPageFactoryTest\specialPageProvider
specialPageProvider()
Definition: SpecialPageFactoryTest.php:47
SpecialPageFactory\resolveAlias
static resolveAlias( $alias)
Definition: SpecialPageFactory_deprecated.php:39
SpecialPageFactory\getTitleForAlias
static getTitleForAlias( $alias)
Definition: SpecialPageFactory_deprecated.php:86
SpecialPageFactory\getLocalNameFor
static getLocalNameFor( $name, $subpage=false)
Definition: SpecialPageFactory_deprecated.php:81
SpecialAllPages
Implements Special:Allpages.
Definition: SpecialAllPages.php:30
SpecialPageFactory\getNames
static getNames()
Definition: SpecialPageFactory_deprecated.php:35
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
SpecialPageFactory\getPage
static getPage( $name)
Definition: SpecialPageFactory_deprecated.php:47
MediaWikiServices
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
SpecialPageFactoryTest\testGetAliasListRecursion
testGetAliasListRecursion()
Definition: SpecialPageFactoryTest.php:260