MediaWiki  REL1_22
PathRouterTest.php
Go to the documentation of this file.
00001 <?php
00008 class PathRouterTest extends MediaWikiTestCase {
00009 
00013     protected $basicRouter;
00014 
00015     protected function setUp() {
00016         parent::setUp();
00017         $router = new PathRouter;
00018         $router->add( "/wiki/$1" );
00019         $this->basicRouter = $router;
00020     }
00021 
00025     public function testBasic() {
00026         $matches = $this->basicRouter->parse( "/wiki/Foo" );
00027         $this->assertEquals( $matches, array( 'title' => "Foo" ) );
00028     }
00029 
00033     public function testLoose() {
00034         $router = new PathRouter;
00035         $router->add( "/" ); # Should be the same as "/$1"
00036         $matches = $router->parse( "/Foo" );
00037         $this->assertEquals( $matches, array( 'title' => "Foo" ) );
00038 
00039         $router = new PathRouter;
00040         $router->add( "/wiki" ); # Should be the same as /wiki/$1
00041         $matches = $router->parse( "/wiki/Foo" );
00042         $this->assertEquals( $matches, array( 'title' => "Foo" ) );
00043 
00044         $router = new PathRouter;
00045         $router->add( "/wiki/" ); # Should be the same as /wiki/$1
00046         $matches = $router->parse( "/wiki/Foo" );
00047         $this->assertEquals( $matches, array( 'title' => "Foo" ) );
00048     }
00049 
00053     public function testOrder() {
00054         $router = new PathRouter;
00055         $router->add( "/$1" );
00056         $router->add( "/a/$1" );
00057         $router->add( "/b/$1" );
00058         $matches = $router->parse( "/a/Foo" );
00059         $this->assertEquals( $matches, array( 'title' => "Foo" ) );
00060 
00061         $router = new PathRouter;
00062         $router->add( "/b/$1" );
00063         $router->add( "/a/$1" );
00064         $router->add( "/$1" );
00065         $matches = $router->parse( "/a/Foo" );
00066         $this->assertEquals( $matches, array( 'title' => "Foo" ) );
00067     }
00068 
00072     public function testKeyParameter() {
00073         $router = new PathRouter;
00074         $router->add( array( 'edit' => "/edit/$1" ), array( 'action' => '$key' ) );
00075         $matches = $router->parse( "/edit/Foo" );
00076         $this->assertEquals( $matches, array( 'title' => "Foo", 'action' => 'edit' ) );
00077     }
00078 
00082     public function testAdditionalParameter() {
00083         // Basic $2
00084         $router = new PathRouter;
00085         $router->add( '/$2/$1', array( 'test' => '$2' ) );
00086         $matches = $router->parse( "/asdf/Foo" );
00087         $this->assertEquals( $matches, array( 'title' => "Foo", 'test' => 'asdf' ) );
00088     }
00089 
00093     public function testRestrictedValue() {
00094         $router = new PathRouter;
00095         $router->add( '/$2/$1',
00096             array( 'test' => '$2' ),
00097             array( '$2' => array( 'a', 'b' ) )
00098         );
00099         $router->add( '/$2/$1',
00100             array( 'test2' => '$2' ),
00101             array( '$2' => 'c' )
00102         );
00103         $router->add( '/$1' );
00104 
00105         $matches = $router->parse( "/asdf/Foo" );
00106         $this->assertEquals( $matches, array( 'title' => "asdf/Foo" ) );
00107 
00108         $matches = $router->parse( "/a/Foo" );
00109         $this->assertEquals( $matches, array( 'title' => "Foo", 'test' => 'a' ) );
00110 
00111         $matches = $router->parse( "/c/Foo" );
00112         $this->assertEquals( $matches, array( 'title' => "Foo", 'test2' => 'c' ) );
00113     }
00114 
00115     public function callbackForTest( &$matches, $data ) {
00116         $matches['x'] = $data['$1'];
00117         $matches['foo'] = $data['foo'];
00118     }
00119 
00120     public function testCallback() {
00121         $router = new PathRouter;
00122         $router->add( "/$1",
00123             array( 'a' => 'b', 'data:foo' => 'bar' ),
00124             array( 'callback' => array( $this, 'callbackForTest' ) )
00125         );
00126         $matches = $router->parse( '/Foo' );
00127         $this->assertEquals( $matches, array(
00128             'title' => "Foo",
00129             'x' => 'Foo',
00130             'a' => 'b',
00131             'foo' => 'bar'
00132         ) );
00133     }
00134 
00138     public function testFail() {
00139         $router = new PathRouter;
00140         $router->add( "/wiki/$1", array( 'title' => "$1$2" ) );
00141         $matches = $router->parse( "/wiki/A" );
00142         $this->assertEquals( array(), $matches );
00143     }
00144 
00148     public function testWeight() {
00149         $router = new PathRouter;
00150         $router->addStrict( "/Bar", array( 'ping' => 'pong' ) );
00151         $router->add( "/asdf-$1", array( 'title' => 'qwerty-$1' ) );
00152         $router->add( "/$1" );
00153         $router->add( "/qwerty-$1", array( 'title' => 'asdf-$1' ) );
00154         $router->addStrict( "/Baz", array( 'marco' => 'polo' ) );
00155         $router->add( "/a/$1" );
00156         $router->add( "/asdf/$1" );
00157         $router->add( "/$2/$1", array( 'unrestricted' => '$2' ) );
00158         $router->add( array( 'qwerty' => "/qwerty/$1" ), array( 'qwerty' => '$key' ) );
00159         $router->add( "/$2/$1", array( 'restricted-to-y' => '$2' ), array( '$2' => 'y' ) );
00160 
00161         foreach (
00162             array(
00163                 '/Foo' => array( 'title' => 'Foo' ),
00164                 '/Bar' => array( 'ping' => 'pong' ),
00165                 '/Baz' => array( 'marco' => 'polo' ),
00166                 '/asdf-foo' => array( 'title' => 'qwerty-foo' ),
00167                 '/qwerty-bar' => array( 'title' => 'asdf-bar' ),
00168                 '/a/Foo' => array( 'title' => 'Foo' ),
00169                 '/asdf/Foo' => array( 'title' => 'Foo' ),
00170                 '/qwerty/Foo' => array( 'title' => 'Foo', 'qwerty' => 'qwerty' ),
00171                 '/baz/Foo' => array( 'title' => 'Foo', 'unrestricted' => 'baz' ),
00172                 '/y/Foo' => array( 'title' => 'Foo', 'restricted-to-y' => 'y' ),
00173             ) as $path => $result
00174         ) {
00175             $this->assertEquals( $router->parse( $path ), $result );
00176         }
00177     }
00178 
00182     public function testSpecial() {
00183         $matches = $this->basicRouter->parse( "/wiki/Special:Recentchanges" );
00184         $this->assertEquals( $matches, array( 'title' => "Special:Recentchanges" ) );
00185     }
00186 
00190     public function testUrlencoding() {
00191         $matches = $this->basicRouter->parse( "/wiki/Title_With%20Space" );
00192         $this->assertEquals( $matches, array( 'title' => "Title_With Space" ) );
00193     }
00194 
00195     public static function provideRegexpChars() {
00196         return array(
00197             array( "$" ),
00198             array( "$1" ),
00199             array( "\\" ),
00200             array( "\\$1" ),
00201         );
00202     }
00203 
00208     public function testRegexpChars( $char ) {
00209         $matches = $this->basicRouter->parse( "/wiki/$char" );
00210         $this->assertEquals( $matches, array( 'title' => "$char" ) );
00211     }
00212 
00216     public function testCharacters() {
00217         $matches = $this->basicRouter->parse( "/wiki/Plus+And&Dollar\\Stuff();[]{}*" );
00218         $this->assertEquals( $matches, array( 'title' => "Plus+And&Dollar\\Stuff();[]{}*" ) );
00219     }
00220 
00227     public function testUnicode() {
00228         $matches = $this->basicRouter->parse( "/wiki/Spécial:Modifications_récentes" );
00229         $this->assertEquals( $matches, array( 'title' => "Spécial:Modifications_récentes" ) );
00230 
00231         $matches = $this->basicRouter->parse( "/wiki/Sp%C3%A9cial:Modifications_r%C3%A9centes" );
00232         $this->assertEquals( $matches, array( 'title' => "Spécial:Modifications_récentes" ) );
00233     }
00234 
00238     public function testLength() {
00239         $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." );
00240         $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." ) );
00241     }
00242 
00243 
00247     public function testPatternUrlencoding() {
00248         $router = new PathRouter;
00249         $router->add( "/wiki/$1", array( 'title' => '%20:$1' ) );
00250         $matches = $router->parse( "/wiki/Foo" );
00251         $this->assertEquals( $matches, array( 'title' => '%20:Foo' ) );
00252     }
00253 
00257     public function testRawParamValue() {
00258         $router = new PathRouter;
00259         $router->add( "/wiki/$1", array( 'title' => array( 'value' => 'bar%20$1' ) ) );
00260         $matches = $router->parse( "/wiki/Foo" );
00261         $this->assertEquals( $matches, array( 'title' => 'bar%20$1' ) );
00262     }
00263 }