MediaWiki  REL1_31
PathRouterTest.php
Go to the documentation of this file.
1 <?php
2 
9 
13  protected $basicRouter;
14 
15  protected function setUp() {
16  parent::setUp();
17  $router = new PathRouter;
18  $router->add( "/wiki/$1" );
19  $this->basicRouter = $router;
20  }
21 
25  public function testBasic() {
26  $matches = $this->basicRouter->parse( "/wiki/Foo" );
27  $this->assertEquals( $matches, [ 'title' => "Foo" ] );
28  }
29 
33  public function testLoose() {
34  $router = new PathRouter;
35  $router->add( "/" ); # Should be the same as "/$1"
36  $matches = $router->parse( "/Foo" );
37  $this->assertEquals( $matches, [ 'title' => "Foo" ] );
38 
39  $router = new PathRouter;
40  $router->add( "/wiki" ); # Should be the same as /wiki/$1
41  $matches = $router->parse( "/wiki/Foo" );
42  $this->assertEquals( $matches, [ 'title' => "Foo" ] );
43 
44  $router = new PathRouter;
45  $router->add( "/wiki/" ); # Should be the same as /wiki/$1
46  $matches = $router->parse( "/wiki/Foo" );
47  $this->assertEquals( $matches, [ 'title' => "Foo" ] );
48  }
49 
53  public function testOrder() {
54  $router = new PathRouter;
55  $router->add( "/$1" );
56  $router->add( "/a/$1" );
57  $router->add( "/b/$1" );
58  $matches = $router->parse( "/a/Foo" );
59  $this->assertEquals( $matches, [ 'title' => "Foo" ] );
60 
61  $router = new PathRouter;
62  $router->add( "/b/$1" );
63  $router->add( "/a/$1" );
64  $router->add( "/$1" );
65  $matches = $router->parse( "/a/Foo" );
66  $this->assertEquals( $matches, [ 'title' => "Foo" ] );
67  }
68 
72  public function testKeyParameter() {
73  $router = new PathRouter;
74  $router->add( [ 'edit' => "/edit/$1" ], [ 'action' => '$key' ] );
75  $matches = $router->parse( "/edit/Foo" );
76  $this->assertEquals( $matches, [ 'title' => "Foo", 'action' => 'edit' ] );
77  }
78 
82  public function testAdditionalParameter() {
83  // Basic $2
84  $router = new PathRouter;
85  $router->add( '/$2/$1', [ 'test' => '$2' ] );
86  $matches = $router->parse( "/asdf/Foo" );
87  $this->assertEquals( $matches, [ 'title' => "Foo", 'test' => 'asdf' ] );
88  }
89 
93  public function testRestrictedValue() {
94  $router = new PathRouter;
95  $router->add( '/$2/$1',
96  [ 'test' => '$2' ],
97  [ '$2' => [ 'a', 'b' ] ]
98  );
99  $router->add( '/$2/$1',
100  [ 'test2' => '$2' ],
101  [ '$2' => 'c' ]
102  );
103  $router->add( '/$1' );
104 
105  $matches = $router->parse( "/asdf/Foo" );
106  $this->assertEquals( $matches, [ 'title' => "asdf/Foo" ] );
107 
108  $matches = $router->parse( "/a/Foo" );
109  $this->assertEquals( $matches, [ 'title' => "Foo", 'test' => 'a' ] );
110 
111  $matches = $router->parse( "/c/Foo" );
112  $this->assertEquals( $matches, [ 'title' => "Foo", 'test2' => 'c' ] );
113  }
114 
115  public function callbackForTest( &$matches, $data ) {
116  $matches['x'] = $data['$1'];
117  $matches['foo'] = $data['foo'];
118  }
119 
120  public function testCallback() {
121  $router = new PathRouter;
122  $router->add( "/$1",
123  [ 'a' => 'b', 'data:foo' => 'bar' ],
124  [ 'callback' => [ $this, 'callbackForTest' ] ]
125  );
126  $matches = $router->parse( '/Foo' );
127  $this->assertEquals( $matches, [
128  'title' => "Foo",
129  'x' => 'Foo',
130  'a' => 'b',
131  'foo' => 'bar'
132  ] );
133  }
134 
138  public function testFail() {
139  $router = new PathRouter;
140  $router->add( "/wiki/$1", [ 'title' => "$1$2" ] );
141  $matches = $router->parse( "/wiki/A" );
142  $this->assertEquals( [], $matches );
143  }
144 
148  public function testWeight() {
149  $router = new PathRouter;
150  $router->addStrict( "/Bar", [ 'ping' => 'pong' ] );
151  $router->add( "/asdf-$1", [ 'title' => 'qwerty-$1' ] );
152  $router->add( "/$1" );
153  $router->add( "/qwerty-$1", [ 'title' => 'asdf-$1' ] );
154  $router->addStrict( "/Baz", [ 'marco' => 'polo' ] );
155  $router->add( "/a/$1" );
156  $router->add( "/asdf/$1" );
157  $router->add( "/$2/$1", [ 'unrestricted' => '$2' ] );
158  $router->add( [ 'qwerty' => "/qwerty/$1" ], [ 'qwerty' => '$key' ] );
159  $router->add( "/$2/$1", [ 'restricted-to-y' => '$2' ], [ '$2' => 'y' ] );
160 
161  foreach (
162  [
163  '/Foo' => [ 'title' => 'Foo' ],
164  '/Bar' => [ 'ping' => 'pong' ],
165  '/Baz' => [ 'marco' => 'polo' ],
166  '/asdf-foo' => [ 'title' => 'qwerty-foo' ],
167  '/qwerty-bar' => [ 'title' => 'asdf-bar' ],
168  '/a/Foo' => [ 'title' => 'Foo' ],
169  '/asdf/Foo' => [ 'title' => 'Foo' ],
170  '/qwerty/Foo' => [ 'title' => 'Foo', 'qwerty' => 'qwerty' ],
171  '/baz/Foo' => [ 'title' => 'Foo', 'unrestricted' => 'baz' ],
172  '/y/Foo' => [ 'title' => 'Foo', 'restricted-to-y' => 'y' ],
173  ] as $path => $result
174  ) {
175  $this->assertEquals( $router->parse( $path ), $result );
176  }
177  }
178 
182  public function testSpecial() {
183  $matches = $this->basicRouter->parse( "/wiki/Special:Recentchanges" );
184  $this->assertEquals( $matches, [ 'title' => "Special:Recentchanges" ] );
185  }
186 
190  public function testUrlencoding() {
191  $matches = $this->basicRouter->parse( "/wiki/Title_With%20Space" );
192  $this->assertEquals( $matches, [ 'title' => "Title_With Space" ] );
193  }
194 
195  public static function provideRegexpChars() {
196  return [
197  [ "$" ],
198  [ "$1" ],
199  [ "\\" ],
200  [ "\\$1" ],
201  ];
202  }
203 
208  public function testRegexpChars( $char ) {
209  $matches = $this->basicRouter->parse( "/wiki/$char" );
210  $this->assertEquals( $matches, [ 'title' => "$char" ] );
211  }
212 
216  public function testCharacters() {
217  $matches = $this->basicRouter->parse( "/wiki/Plus+And&Dollar\\Stuff();[]{}*" );
218  $this->assertEquals( $matches, [ 'title' => "Plus+And&Dollar\\Stuff();[]{}*" ] );
219  }
220 
227  public function testUnicode() {
228  $matches = $this->basicRouter->parse( "/wiki/Spécial:Modifications_récentes" );
229  $this->assertEquals( $matches, [ 'title' => "Spécial:Modifications_récentes" ] );
230 
231  $matches = $this->basicRouter->parse( "/wiki/Sp%C3%A9cial:Modifications_r%C3%A9centes" );
232  $this->assertEquals( $matches, [ 'title' => "Spécial:Modifications_récentes" ] );
233  }
234 
238  public function testLength() {
239  // phpcs:disable Generic.Files.LineLength
240  $matches = $this->basicRouter->parse(
241  "/wiki/Lorem_ipsum_dolor_sit_amet,_consectetur_adipisicing_elit,_sed_do_eiusmod_tempor_incididunt_ut_labore_et_dolore_magna_aliqua._Ut_enim_ad_minim_veniam,_quis_nostrud_exercitation_ullamco_laboris_nisi_ut_aliquip_ex_ea_commodo_consequat._Duis_aute_irure_dolor_in_reprehenderit_in_voluptate_velit_esse_cillum_dolore_eu_fugiat_nulla_pariatur._Excepteur_sint_occaecat_cupidatat_non_proident,_sunt_in_culpa_qui_officia_deserunt_mollit_anim_id_est_laborum."
242  );
243  $this->assertEquals(
244  $matches,
245  [ 'title' => "Lorem_ipsum_dolor_sit_amet,_consectetur_adipisicing_elit,_sed_do_eiusmod_tempor_incididunt_ut_labore_et_dolore_magna_aliqua._Ut_enim_ad_minim_veniam,_quis_nostrud_exercitation_ullamco_laboris_nisi_ut_aliquip_ex_ea_commodo_consequat._Duis_aute_irure_dolor_in_reprehenderit_in_voluptate_velit_esse_cillum_dolore_eu_fugiat_nulla_pariatur._Excepteur_sint_occaecat_cupidatat_non_proident,_sunt_in_culpa_qui_officia_deserunt_mollit_anim_id_est_laborum." ]
246  );
247  // phpcs:enable
248  }
249 
253  public function testPatternUrlencoding() {
254  $router = new PathRouter;
255  $router->add( "/wiki/$1", [ 'title' => '%20:$1' ] );
256  $matches = $router->parse( "/wiki/Foo" );
257  $this->assertEquals( $matches, [ 'title' => '%20:Foo' ] );
258  }
259 
263  public function testRawParamValue() {
264  $router = new PathRouter;
265  $router->add( "/wiki/$1", [ 'title' => [ 'value' => 'bar%20$1' ] ] );
266  $matches = $router->parse( "/wiki/Foo" );
267  $this->assertEquals( $matches, [ 'title' => 'bar%20$1' ] );
268  }
269 }
PathRouterTest\testKeyParameter
testKeyParameter()
Test the handling of key based arrays with a url parameter.
Definition: PathRouterTest.php:72
PathRouter\add
add( $path, $params=[], $options=[])
Add a new path pattern to the path router.
Definition: PathRouter.php:160
PathRouterTest\testFail
testFail()
Test to ensure that matches are not made if a parameter expects nonexistent input.
Definition: PathRouterTest.php:138
PathRouterTest\testSpecial
testSpecial()
Make sure the router handles titles like Special:Recentchanges correctly.
Definition: PathRouterTest.php:182
PathRouterTest\testWeight
testWeight()
Test to ensure weight of paths is handled correctly.
Definition: PathRouterTest.php:148
PathRouterTest\testOrder
testOrder()
Test to ensure that path is based on specifity, not order.
Definition: PathRouterTest.php:53
PathRouter\addStrict
addStrict( $path, $params=[], $options=[])
Add a new path pattern to the path router with the strict option on.
Definition: PathRouter.php:177
wiki
Prior to maintenance scripts were a hodgepodge of code that had no cohesion or formal method of action Beginning maintenance scripts have been cleaned up to use a unified class Directory structure How to run a script How to write your own DIRECTORY STRUCTURE The maintenance directory of a MediaWiki installation contains several all of which have unique purposes HOW TO RUN A SCRIPT Ridiculously just call php someScript php that s in the top level maintenance directory if not default wiki
Definition: maintenance.txt:27
PathRouterTest\testRestrictedValue
testRestrictedValue()
Test additional restricted value parameter.
Definition: PathRouterTest.php:93
PathRouterTest\provideRegexpChars
static provideRegexpChars()
Definition: PathRouterTest.php:195
PathRouterTest\testCharacters
testCharacters()
Make sure the router handles characters like +&() properly.
Definition: PathRouterTest.php:216
$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. 'ImgAuthModifyHeaders':Executed just before a file is streamed to a user via img_auth.php, allowing headers to be modified beforehand. $title:LinkTarget object & $headers:HTTP headers(name=> value, names are case insensitive). Two headers get special handling:If-Modified-Since(value must be a valid HTTP date) and Range(must be of the form "bytes=(\d*-\d*)") will be honored when streaming the file. '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. 'LanguageGetMagic':DEPRECATED! Use $magicWords in a file listed in $wgExtensionMessagesFiles instead. Use this to define synonyms of magic words depending of the language & $magicExtensions:associative array of magic words synonyms $lang:language code(string) '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 'LanguageGetSpecialPageAliases':DEPRECATED! Use $specialPageAliases in a file listed in $wgExtensionMessagesFiles instead. Use to define aliases of special pages names depending of the language & $specialPageAliases:associative array of magic words synonyms $lang:language code(string) '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! 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:1993
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:37
PathRouterTest\testLoose
testLoose()
Test loose path auto-$1.
Definition: PathRouterTest.php:33
PathRouterTest
Tests for the PathRouter parsing.
Definition: PathRouterTest.php:8
PathRouterTest\callbackForTest
callbackForTest(&$matches, $data)
Definition: PathRouterTest.php:115
$matches
$matches
Definition: NoLocalSettings.php:24
MediaWikiTestCase
Definition: MediaWikiTestCase.php:17
PathRouterTest\testRegexpChars
testRegexpChars( $char)
Make sure the router doesn't break on special characters like $ used in regexp replacements provideRe...
Definition: PathRouterTest.php:208
PathRouterTest\testUnicode
testUnicode()
Make sure the router handles unicode characters correctly @depends testSpecial @depends testUrlencodi...
Definition: PathRouterTest.php:227
PathRouterTest\testCallback
testCallback()
Definition: PathRouterTest.php:120
PathRouterTest\setUp
setUp()
Definition: PathRouterTest.php:15
PathRouterTest\$basicRouter
PathRouter $basicRouter
Definition: PathRouterTest.php:13
PathRouterTest\testLength
testLength()
Ensure the router doesn't choke on long paths.
Definition: PathRouterTest.php:238
PathRouterTest\testAdditionalParameter
testAdditionalParameter()
Test the handling of $2 inside paths.
Definition: PathRouterTest.php:82
PathRouterTest\testUrlencoding
testUrlencoding()
Make sure the router decodes urlencoding properly.
Definition: PathRouterTest.php:190
PathRouterTest\testRawParamValue
testRawParamValue()
Ensure that raw parameter values do not have any variable replacements or urldecoding.
Definition: PathRouterTest.php:263
PathRouterTest\testPatternUrlencoding
testPatternUrlencoding()
Ensure that the php passed site of parameter values are not urldecoded.
Definition: PathRouterTest.php:253
$path
$path
Definition: NoLocalSettings.php:25
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:22
PathRouterTest\testBasic
testBasic()
Test basic path parsing.
Definition: PathRouterTest.php:25
PathRouter
PathRouter class.
Definition: PathRouter.php:73