Import: Handle uploads with sha1 starting with 0 properly
[mediawiki.git] / tests / phpunit / includes / PathRouterTest.php
blob0d7826871f6e4d9d67824a71598d3f3673527b36
1 <?php
3 /**
4 * Tests for the PathRouter parsing.
6 * @covers PathRouter
7 */
8 class PathRouterTest extends MediaWikiTestCase {
10 /**
11 * @var PathRouter
13 protected $basicRouter;
15 protected function setUp() {
16 parent::setUp();
17 $router = new PathRouter;
18 $router->add( "/wiki/$1" );
19 $this->basicRouter = $router;
22 /**
23 * Test basic path parsing
25 public function testBasic() {
26 $matches = $this->basicRouter->parse( "/wiki/Foo" );
27 $this->assertEquals( $matches, array( 'title' => "Foo" ) );
30 /**
31 * Test loose path auto-$1
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, array( 'title' => "Foo" ) );
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, array( 'title' => "Foo" ) );
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, array( 'title' => "Foo" ) );
50 /**
51 * Test to ensure that path is based on specifity, not order
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, array( 'title' => "Foo" ) );
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, array( 'title' => "Foo" ) );
69 /**
70 * Test the handling of key based arrays with a url parameter
72 public function testKeyParameter() {
73 $router = new PathRouter;
74 $router->add( array( 'edit' => "/edit/$1" ), array( 'action' => '$key' ) );
75 $matches = $router->parse( "/edit/Foo" );
76 $this->assertEquals( $matches, array( 'title' => "Foo", 'action' => 'edit' ) );
79 /**
80 * Test the handling of $2 inside paths
82 public function testAdditionalParameter() {
83 // Basic $2
84 $router = new PathRouter;
85 $router->add( '/$2/$1', array( 'test' => '$2' ) );
86 $matches = $router->parse( "/asdf/Foo" );
87 $this->assertEquals( $matches, array( 'title' => "Foo", 'test' => 'asdf' ) );
90 /**
91 * Test additional restricted value parameter
93 public function testRestrictedValue() {
94 $router = new PathRouter;
95 $router->add( '/$2/$1',
96 array( 'test' => '$2' ),
97 array( '$2' => array( 'a', 'b' ) )
99 $router->add( '/$2/$1',
100 array( 'test2' => '$2' ),
101 array( '$2' => 'c' )
103 $router->add( '/$1' );
105 $matches = $router->parse( "/asdf/Foo" );
106 $this->assertEquals( $matches, array( 'title' => "asdf/Foo" ) );
108 $matches = $router->parse( "/a/Foo" );
109 $this->assertEquals( $matches, array( 'title' => "Foo", 'test' => 'a' ) );
111 $matches = $router->parse( "/c/Foo" );
112 $this->assertEquals( $matches, array( 'title' => "Foo", 'test2' => 'c' ) );
115 public function callbackForTest( &$matches, $data ) {
116 $matches['x'] = $data['$1'];
117 $matches['foo'] = $data['foo'];
120 public function testCallback() {
121 $router = new PathRouter;
122 $router->add( "/$1",
123 array( 'a' => 'b', 'data:foo' => 'bar' ),
124 array( 'callback' => array( $this, 'callbackForTest' ) )
126 $matches = $router->parse( '/Foo' );
127 $this->assertEquals( $matches, array(
128 'title' => "Foo",
129 'x' => 'Foo',
130 'a' => 'b',
131 'foo' => 'bar'
132 ) );
136 * Test to ensure that matches are not made if a parameter expects nonexistent input
138 public function testFail() {
139 $router = new PathRouter;
140 $router->add( "/wiki/$1", array( 'title' => "$1$2" ) );
141 $matches = $router->parse( "/wiki/A" );
142 $this->assertEquals( array(), $matches );
146 * Test to ensure weight of paths is handled correctly
148 public function testWeight() {
149 $router = new PathRouter;
150 $router->addStrict( "/Bar", array( 'ping' => 'pong' ) );
151 $router->add( "/asdf-$1", array( 'title' => 'qwerty-$1' ) );
152 $router->add( "/$1" );
153 $router->add( "/qwerty-$1", array( 'title' => 'asdf-$1' ) );
154 $router->addStrict( "/Baz", array( 'marco' => 'polo' ) );
155 $router->add( "/a/$1" );
156 $router->add( "/asdf/$1" );
157 $router->add( "/$2/$1", array( 'unrestricted' => '$2' ) );
158 $router->add( array( 'qwerty' => "/qwerty/$1" ), array( 'qwerty' => '$key' ) );
159 $router->add( "/$2/$1", array( 'restricted-to-y' => '$2' ), array( '$2' => 'y' ) );
161 foreach (
162 array(
163 '/Foo' => array( 'title' => 'Foo' ),
164 '/Bar' => array( 'ping' => 'pong' ),
165 '/Baz' => array( 'marco' => 'polo' ),
166 '/asdf-foo' => array( 'title' => 'qwerty-foo' ),
167 '/qwerty-bar' => array( 'title' => 'asdf-bar' ),
168 '/a/Foo' => array( 'title' => 'Foo' ),
169 '/asdf/Foo' => array( 'title' => 'Foo' ),
170 '/qwerty/Foo' => array( 'title' => 'Foo', 'qwerty' => 'qwerty' ),
171 '/baz/Foo' => array( 'title' => 'Foo', 'unrestricted' => 'baz' ),
172 '/y/Foo' => array( 'title' => 'Foo', 'restricted-to-y' => 'y' ),
173 ) as $path => $result
175 $this->assertEquals( $router->parse( $path ), $result );
180 * Make sure the router handles titles like Special:Recentchanges correctly
182 public function testSpecial() {
183 $matches = $this->basicRouter->parse( "/wiki/Special:Recentchanges" );
184 $this->assertEquals( $matches, array( 'title' => "Special:Recentchanges" ) );
188 * Make sure the router decodes urlencoding properly
190 public function testUrlencoding() {
191 $matches = $this->basicRouter->parse( "/wiki/Title_With%20Space" );
192 $this->assertEquals( $matches, array( 'title' => "Title_With Space" ) );
195 public static function provideRegexpChars() {
196 return array(
197 array( "$" ),
198 array( "$1" ),
199 array( "\\" ),
200 array( "\\$1" ),
205 * Make sure the router doesn't break on special characters like $ used in regexp replacements
206 * @dataProvider provideRegexpChars
208 public function testRegexpChars( $char ) {
209 $matches = $this->basicRouter->parse( "/wiki/$char" );
210 $this->assertEquals( $matches, array( 'title' => "$char" ) );
214 * Make sure the router handles characters like +&() properly
216 public function testCharacters() {
217 $matches = $this->basicRouter->parse( "/wiki/Plus+And&Dollar\\Stuff();[]{}*" );
218 $this->assertEquals( $matches, array( 'title' => "Plus+And&Dollar\\Stuff();[]{}*" ) );
222 * Make sure the router handles unicode characters correctly
223 * @depends testSpecial
224 * @depends testUrlencoding
225 * @depends testCharacters
227 public function testUnicode() {
228 $matches = $this->basicRouter->parse( "/wiki/Spécial:Modifications_récentes" );
229 $this->assertEquals( $matches, array( 'title' => "Spécial:Modifications_récentes" ) );
231 $matches = $this->basicRouter->parse( "/wiki/Sp%C3%A9cial:Modifications_r%C3%A9centes" );
232 $this->assertEquals( $matches, array( 'title' => "Spécial:Modifications_récentes" ) );
236 * Ensure the router doesn't choke on long paths.
238 public function testLength() {
239 // @codingStandardsIgnoreStart Ignore long line warnings
240 $matches = $this->basicRouter->parse( "/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." );
241 $this->assertEquals( $matches, array( '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." ) );
242 // @codingStandardsIgnoreEnd
246 * Ensure that the php passed site of parameter values are not urldecoded
248 public function testPatternUrlencoding() {
249 $router = new PathRouter;
250 $router->add( "/wiki/$1", array( 'title' => '%20:$1' ) );
251 $matches = $router->parse( "/wiki/Foo" );
252 $this->assertEquals( $matches, array( 'title' => '%20:Foo' ) );
256 * Ensure that raw parameter values do not have any variable replacements or urldecoding
258 public function testRawParamValue() {
259 $router = new PathRouter;
260 $router->add( "/wiki/$1", array( 'title' => array( 'value' => 'bar%20$1' ) ) );
261 $matches = $router->parse( "/wiki/Foo" );
262 $this->assertEquals( $matches, array( 'title' => 'bar%20$1' ) );