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