Recommit PathRouter code from r104274, r104284, r104285 without the WebRequest.php...
[lhc/web/wiklou.git] / tests / phpunit / includes / PathRouterTest.php
1 <?php
2 /**
3 * Tests for the PathRouter parsing
4 */
5
6 class PathRouterTest extends MediaWikiTestCase {
7
8 /**
9 * Test basic path parsing
10 */
11 public function testBasic() {
12 $router = new PathRouter;
13 $router->add("/$1");
14 $matches = $router->parse( "/Foo" );
15 $this->assertEquals( $matches, array( 'title' => "Foo" ) );
16 }
17
18 /**
19 * Test loose path auto-$1
20 */
21 public function testLoose() {
22 $router = new PathRouter;
23 $router->add("/"); # Should be the same as "/$1"
24 $matches = $router->parse( "/Foo" );
25 $this->assertEquals( $matches, array( 'title' => "Foo" ) );
26
27 $router = new PathRouter;
28 $router->add("/wiki"); # Should be the same as /wiki/$1
29 $matches = $router->parse( "/wiki/Foo" );
30 $this->assertEquals( $matches, array( 'title' => "Foo" ) );
31
32 $router = new PathRouter;
33 $router->add("/wiki/"); # Should be the same as /wiki/$1
34 $matches = $router->parse( "/wiki/Foo" );
35 $this->assertEquals( $matches, array( 'title' => "Foo" ) );
36 }
37
38 /**
39 * Test to ensure that path is based on specifity, not order
40 */
41 public function testOrder() {
42 $router = new PathRouter;
43 $router->add("/$1");
44 $router->add("/a/$1");
45 $router->add("/b/$1");
46 $matches = $router->parse( "/a/Foo" );
47 $this->assertEquals( $matches, array( 'title' => "Foo" ) );
48
49 $router = new PathRouter;
50 $router->add("/b/$1");
51 $router->add("/a/$1");
52 $router->add("/$1");
53 $matches = $router->parse( "/a/Foo" );
54 $this->assertEquals( $matches, array( 'title' => "Foo" ) );
55 }
56
57 /**
58 * Test the handling of key based arrays with a url parameter
59 */
60 public function testKeyParameter() {
61 $router = new PathRouter;
62 $router->add( array( 'edit' => "/edit/$1" ), array( 'action' => '$key' ) );
63 $matches = $router->parse( "/edit/Foo" );
64 $this->assertEquals( $matches, array( 'title' => "Foo", 'action' => 'edit' ) );
65 }
66
67 /**
68 * Test the handling of $2 inside paths
69 */
70 public function testAdditionalParameter() {
71 // Basic $2
72 $router = new PathRouter;
73 $router->add( '/$2/$1', array( 'test' => '$2' ) );
74 $matches = $router->parse( "/asdf/Foo" );
75 $this->assertEquals( $matches, array( 'title' => "Foo", 'test' => 'asdf' ) );
76 }
77
78 /**
79 * Test additional restricted value parameter
80 */
81 public function testRestrictedValue() {
82 $router = new PathRouter;
83 $router->add( '/$2/$1',
84 array( 'test' => '$2' ),
85 array( '$2' => array( 'a', 'b' ) )
86 );
87 $router->add( '/$2/$1',
88 array( 'test2' => '$2' ),
89 array( '$2' => 'c' )
90 );
91 $router->add( '/$1' );
92
93 $matches = $router->parse( "/asdf/Foo" );
94 $this->assertEquals( $matches, array( 'title' => "asdf/Foo" ) );
95
96 $matches = $router->parse( "/a/Foo" );
97 $this->assertEquals( $matches, array( 'title' => "Foo", 'test' => 'a' ) );
98
99 $matches = $router->parse( "/c/Foo" );
100 $this->assertEquals( $matches, array( 'title' => "Foo", 'test2' => 'c' ) );
101 }
102
103 public function callbackForTest( &$matches, $data ) {
104 $matches['x'] = $data['$1'];
105 $matches['foo'] = $data['foo'];
106 }
107
108 public function testCallback() {
109 $router = new PathRouter;
110 $router->add( "/$1",
111 array( 'a' => 'b', 'data:foo' => 'bar' ),
112 array( 'callback' => array( $this, 'callbackForTest' ) )
113 );
114 $matches = $router->parse( '/Foo');
115 $this->assertEquals( $matches, array(
116 'title' => "Foo",
117 'x' => 'Foo',
118 'a' => 'b',
119 'foo' => 'bar'
120 ) );
121 }
122
123 /**
124 * Test to ensure weight of paths is handled correctly
125 */
126 public function testWeight() {
127 $router = new PathRouter;
128 $router->addStrict( "/Bar", array( 'ping' => 'pong' ) );
129 $router->add( "/asdf-$1", array( 'title' => 'qwerty-$1' ) );
130 $router->add( "/$1" );
131 $router->add( "/qwerty-$1", array( 'title' => 'asdf-$1' ) );
132 $router->addStrict( "/Baz", array( 'marco' => 'polo' ) );
133 $router->add( "/a/$1" );
134 $router->add( "/asdf/$1" );
135 $router->add( "/$2/$1", array( 'unrestricted' => '$2' ) );
136 $router->add( array( 'qwerty' => "/qwerty/$1" ), array( 'qwerty' => '$key' ) );
137 $router->add( "/$2/$1", array( 'restricted-to-y' => '$2' ), array( '$2' => 'y' ) );
138
139 foreach( array(
140 "/Foo" => array( 'title' => "Foo" ),
141 "/Bar" => array( 'ping' => 'pong' ),
142 "/Baz" => array( 'marco' => 'polo' ),
143 "/asdf-foo" => array( 'title' => "qwerty-foo" ),
144 "/qwerty-bar" => array( 'title' => "asdf-bar" ),
145 "/a/Foo" => array( 'title' => "Foo" ),
146 "/asdf/Foo" => array( 'title' => "Foo" ),
147 "/qwerty/Foo" => array( 'title' => "Foo", 'qwerty' => 'qwerty' ),
148 "/baz/Foo" => array( 'title' => "Foo", 'unrestricted' => 'baz' ),
149 "/y/Foo" => array( 'title' => "Foo", 'restricted-to-y' => 'y' ),
150 ) as $path => $result ) {
151 $this->assertEquals( $router->parse( $path ), $result );
152 }
153 }
154
155 }