MediaWiki  REL1_19
PathRouterTest.php
Go to the documentation of this file.
00001 <?php
00006 class PathRouterTest extends MediaWikiTestCase {
00007 
00008         public function setUp() {
00009                 $router = new PathRouter;
00010                 $router->add("/wiki/$1");
00011                 $this->basicRouter = $router;
00012         }
00013 
00017         public function testBasic() {
00018                 $matches = $this->basicRouter->parse( "/wiki/Foo" );
00019                 $this->assertEquals( $matches, array( 'title' => "Foo" ) );
00020         }
00021 
00025         public function testLoose() {
00026                 $router = new PathRouter;
00027                 $router->add("/"); # Should be the same as "/$1"
00028                 $matches = $router->parse( "/Foo" );
00029                 $this->assertEquals( $matches, array( 'title' => "Foo" ) );
00030 
00031                 $router = new PathRouter;
00032                 $router->add("/wiki"); # Should be the same as /wiki/$1
00033                 $matches = $router->parse( "/wiki/Foo" );
00034                 $this->assertEquals( $matches, array( 'title' => "Foo" ) );
00035 
00036                 $router = new PathRouter;
00037                 $router->add("/wiki/"); # Should be the same as /wiki/$1
00038                 $matches = $router->parse( "/wiki/Foo" );
00039                 $this->assertEquals( $matches, array( 'title' => "Foo" ) );
00040         }
00041 
00045         public function testOrder() {
00046                 $router = new PathRouter;
00047                 $router->add("/$1");
00048                 $router->add("/a/$1");
00049                 $router->add("/b/$1");
00050                 $matches = $router->parse( "/a/Foo" );
00051                 $this->assertEquals( $matches, array( 'title' => "Foo" ) );
00052 
00053                 $router = new PathRouter;
00054                 $router->add("/b/$1");
00055                 $router->add("/a/$1");
00056                 $router->add("/$1");
00057                 $matches = $router->parse( "/a/Foo" );
00058                 $this->assertEquals( $matches, array( 'title' => "Foo" ) );
00059         }
00060 
00064         public function testKeyParameter() {
00065                 $router = new PathRouter;
00066                 $router->add( array( 'edit' => "/edit/$1" ), array( 'action' => '$key' ) );
00067                 $matches = $router->parse( "/edit/Foo" );
00068                 $this->assertEquals( $matches, array( 'title' => "Foo", 'action' => 'edit' ) );
00069         }
00070 
00074         public function testAdditionalParameter() {
00075                 // Basic $2
00076                 $router = new PathRouter;
00077                 $router->add( '/$2/$1', array( 'test' => '$2' ) );
00078                 $matches = $router->parse( "/asdf/Foo" );
00079                 $this->assertEquals( $matches, array( 'title' => "Foo", 'test' => 'asdf' ) );
00080         }
00081 
00085         public function testRestrictedValue() {
00086                 $router = new PathRouter;
00087                 $router->add( '/$2/$1',
00088                         array( 'test' => '$2' ),
00089                         array( '$2' => array( 'a', 'b' ) )
00090                 );
00091                 $router->add( '/$2/$1',
00092                         array( 'test2' => '$2' ),
00093                         array( '$2' => 'c' )
00094                 );
00095                 $router->add( '/$1' );
00096 
00097                 $matches = $router->parse( "/asdf/Foo" );
00098                 $this->assertEquals( $matches, array( 'title' => "asdf/Foo" ) );
00099 
00100                 $matches = $router->parse( "/a/Foo" );
00101                 $this->assertEquals( $matches, array( 'title' => "Foo", 'test' => 'a' ) );
00102 
00103                 $matches = $router->parse( "/c/Foo" );
00104                 $this->assertEquals( $matches, array( 'title' => "Foo", 'test2' => 'c' ) );
00105         }
00106 
00107         public function callbackForTest( &$matches, $data ) {
00108                 $matches['x'] = $data['$1'];
00109                 $matches['foo'] = $data['foo'];
00110         }
00111 
00112         public function testCallback() {
00113                 $router = new PathRouter;
00114                 $router->add( "/$1",
00115                         array( 'a' => 'b', 'data:foo' => 'bar' ),
00116                         array( 'callback' => array( $this, 'callbackForTest' ) )
00117                 );
00118                 $matches = $router->parse( '/Foo' );
00119                 $this->assertEquals( $matches, array(
00120                         'title' => "Foo",
00121                         'x' => 'Foo',
00122                         'a' => 'b',
00123                         'foo' => 'bar'
00124                 ) );
00125         }
00126 
00130         public function testFail() {
00131                 $router = new PathRouter;
00132                 $router->add( "/wiki/$1", array( 'title' => "$1$2" ) );
00133                 $matches = $router->parse( "/wiki/A" );
00134                 $this->assertEquals( array(), $matches );
00135         }
00136 
00140         public function testWeight() {
00141                 $router = new PathRouter;
00142                 $router->addStrict( "/Bar", array( 'ping' => 'pong' ) );
00143                 $router->add( "/asdf-$1", array( 'title' => 'qwerty-$1' ) );
00144                 $router->add( "/$1" );
00145                 $router->add( "/qwerty-$1", array( 'title' => 'asdf-$1' ) );
00146                 $router->addStrict( "/Baz", array( 'marco' => 'polo' ) );
00147                 $router->add( "/a/$1" );
00148                 $router->add( "/asdf/$1" );
00149                 $router->add( "/$2/$1", array( 'unrestricted' => '$2' ) );
00150                 $router->add( array( 'qwerty' => "/qwerty/$1" ), array( 'qwerty' => '$key' ) );
00151                 $router->add( "/$2/$1", array( 'restricted-to-y' => '$2' ), array( '$2' => 'y' ) );
00152 
00153                 foreach( array(
00154                         "/Foo" => array( 'title' => "Foo" ),
00155                         "/Bar" => array( 'ping' => 'pong' ),
00156                         "/Baz" => array( 'marco' => 'polo' ),
00157                         "/asdf-foo" => array( 'title' => "qwerty-foo" ),
00158                         "/qwerty-bar" => array( 'title' => "asdf-bar" ),
00159                         "/a/Foo" => array( 'title' => "Foo" ),
00160                         "/asdf/Foo" => array( 'title' => "Foo" ),
00161                         "/qwerty/Foo" => array( 'title' => "Foo", 'qwerty' => 'qwerty' ),
00162                         "/baz/Foo" => array( 'title' => "Foo", 'unrestricted' => 'baz' ),
00163                         "/y/Foo" => array( 'title' => "Foo", 'restricted-to-y' => 'y' ),
00164                 ) as $path => $result ) {
00165                         $this->assertEquals( $router->parse( $path ), $result );
00166                 }
00167         }
00168 
00172         public function testSpecial() {
00173                 $matches = $this->basicRouter->parse( "/wiki/Special:Recentchanges" );
00174                 $this->assertEquals( $matches, array( 'title' => "Special:Recentchanges" ) );
00175         }
00176 
00180         public function testUrlencoding() {
00181                 $matches = $this->basicRouter->parse( "/wiki/Title_With%20Space" );
00182                 $this->assertEquals( $matches, array( 'title' => "Title_With Space" ) );
00183         }
00184 
00185         public function dataRegexpChars() {
00186                 return array(
00187                         array( "$" ),
00188                         array( "$1" ),
00189                         array( "\\" ),
00190                         array( "\\$1" ),
00191                 );
00192         }
00193 
00198         public function testRegexpChars( $char ) {
00199                 $matches = $this->basicRouter->parse( "/wiki/$char" );
00200                 $this->assertEquals( $matches, array( 'title' => "$char" ) );
00201         }
00202 
00206         public function testCharacters() {
00207                 $matches = $this->basicRouter->parse( "/wiki/Plus+And&Dollar\\Stuff();[]{}*" );
00208                 $this->assertEquals( $matches, array( 'title' => "Plus+And&Dollar\\Stuff();[]{}*" ) );
00209         }
00210 
00217         public function testUnicode() {
00218                 $matches = $this->basicRouter->parse( "/wiki/Spécial:Modifications_récentes" );
00219                 $this->assertEquals( $matches, array( 'title' => "Spécial:Modifications_récentes" ) );
00220 
00221                 $matches = $this->basicRouter->parse( "/wiki/Sp%C3%A9cial:Modifications_r%C3%A9centes" );
00222                 $this->assertEquals( $matches, array( 'title' => "Spécial:Modifications_récentes" ) );
00223         }
00224 
00228         public function testLength() {
00229                 $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." );
00230                 $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." ) );
00231         }
00232 
00233 
00237         public function testPatternUrlencoding() {
00238                 $router = new PathRouter;
00239                 $router->add( "/wiki/$1", array( 'title' => '%20:$1' ) );
00240                 $matches = $router->parse( "/wiki/Foo" );
00241                 $this->assertEquals( $matches, array( 'title' => '%20:Foo' ) );
00242         }
00243 
00247         public function testRawParamValue() {
00248                 $router = new PathRouter;
00249                 $router->add( "/wiki/$1", array( 'title' => array( 'value' => 'bar%20$1' ) ) );
00250                 $matches = $router->parse( "/wiki/Foo" );
00251                 $this->assertEquals( $matches, array( 'title' => 'bar%20$1' ) );
00252         }
00253 
00254 }