-- improved support in params in the route
[limb:trunk.git] / web_app / tests / cases / plain / request / lmbRoutesDispatchTest.class.php
1 <?php
2 /*
3  * Limb PHP Framework
4  *
5  * @link http://limb-project.com
6  * @copyright  Copyright &copy; 2004-2009 BIT(http://bit-creative.com)
7  * @license    LGPL http://www.gnu.org/copyleft/lesser.html
8  */
9 lmb_require('limb/web_app/src/request/lmbRoutes.class.php');
10 lmb_require('limb/net/src/lmbUri.class.php');
11
12 class lmbRoutesDispatchTest extends UnitTestCase
13 {
14   function setUp()
15   {
16     lmbToolkit :: save();
17   }
18
19   function tearDown()
20   {
21     lmbToolkit :: restore();
22   }
23
24   function testControllerAndDefaultAction()
25   {
26     $config = array(array('path' => '/blog',
27                           'defaults' => array('controller' => 'Blog',
28                                               'action' => 'display')),
29                     array('path' => '/news',
30                           'defaults' => array('controller' => 'Newsline',
31                                               'action' => 'display')));
32
33     $routes = new lmbRoutes($config);
34     $result = $routes->dispatch('/blog');
35
36     $this->assertEqual($result['controller'], 'Blog');
37     $this->assertEqual($result['action'], 'display');
38
39     $result = $routes->dispatch('/news');
40
41     $this->assertEqual($result['controller'], 'Newsline');
42     $this->assertEqual($result['action'], 'display');
43
44     $this->assertEqual($routes->dispatch('/no_such_url'), array());
45   }
46
47   function testAnyController()
48   {
49     $config = array(array('path' => '/:controller',
50                           'defaults' => array('action' => 'display')));
51
52     $routes = new lmbRoutes($config);
53     $result = $routes->dispatch('/blog');
54
55     $this->assertEqual($result['controller'], 'blog');
56     $this->assertEqual($result['action'], 'display');
57
58     $result = $routes->dispatch('/news');
59
60     $this->assertEqual($result['controller'], 'news');
61     $this->assertEqual($result['action'], 'display');
62   }
63
64   function testAnyControllerAndAction()
65   {
66     $config = array(array('path' => '/:controller/:action',
67                           'defaults' => array('action' => 'display')));
68
69     $routes = new lmbRoutes($config);
70     $result = $routes->dispatch('/blog/index');
71
72     $this->assertEqual($result['controller'], 'blog');
73     $this->assertEqual($result['action'], 'index');
74
75     $result = $routes->dispatch('/blog');
76
77     $this->assertEqual($result['controller'], 'blog');
78     $this->assertEqual($result['action'], 'display');
79
80     $result = $routes->dispatch('/news/last_news');
81
82     $this->assertEqual($result['controller'], 'news');
83     $this->assertEqual($result['action'], 'last_news');
84   }
85
86   function testConcreteControllerAndAnyAction()
87   {
88     $config = array(array('path' => '/blog/:action',
89                           'defaults' => array('controller' => 'Blog',
90                                               'action' => 'display')),
91                     array('path' => '/news/:action',
92                           'defaults' => array('controller' => 'News',
93                                               'action' => 'display')));
94
95     $routes = new lmbRoutes($config);
96     $result = $routes->dispatch('/blog/index');
97
98     $this->assertEqual($result['controller'], 'Blog');
99     $this->assertEqual($result['action'], 'index');
100
101     $result = $routes->dispatch('/blog');
102
103     $this->assertEqual($result['controller'], 'Blog');
104     $this->assertEqual($result['action'], 'display');
105
106     $result = $routes->dispatch('/news/last_news');
107
108     $this->assertEqual($result['controller'], 'News');
109     $this->assertEqual($result['action'], 'last_news');
110   }
111
112   function testUrlToMatchAll()
113   {
114     $config = array(array('path' => '*',
115                           'defaults' => array('controller' => '404',
116                                               'action' => 'display')));
117
118     $routes = new lmbRoutes($config);
119     $result = $routes->dispatch('/blog/index');
120
121     $this->assertEqual($result['controller'], '404');
122     $this->assertEqual($result['action'], 'display');
123
124     $result = $routes->dispatch('/path/to/heaven');
125
126     $this->assertEqual($result['controller'], '404');
127     $this->assertEqual($result['action'], 'display');
128   }
129
130   function testExtraParamAfterOthers()
131   {
132     $config = array(array('path' => '/:controller/:action/*additional',
133                           'defaults' => array('controller' => '404',
134                                               'action' => 'display')));
135
136     $routes = new lmbRoutes($config);
137     $result = $routes->dispatch('/blog/index/and/many/params');
138
139     $this->assertEqual($result['controller'], 'blog');
140     $this->assertEqual($result['action'], 'index');
141     $this->assertEqual($result['additional'], 'and/many/params');
142   }
143
144   function testExtraParamDefaultName()
145   {
146     $config = array(array('path' => '/:controller/:action/*',
147                           'defaults' => array('controller' => '404',
148                                               'action' => 'display')));
149
150     $routes = new lmbRoutes($config);
151     $result = $routes->dispatch('/blog/index/and/many/params');
152
153     $this->assertEqual($result['controller'], 'blog');
154     $this->assertEqual($result['action'], 'index');
155     $this->assertEqual($result['extra'], 'and/many/params');
156   }
157
158   function testWithRequirements()
159   {
160     $config = array(array('path' => 'blog/',
161                           'defaults' => array('controller' => 'Blog',
162                                               'action' => 'display')),
163                     array('path' => 'blog/:year/:month/:day',
164                           'defaults' => array('controller' => 'Blog',
165                                               'action' => 'archive',
166                                               'year' => date('Y'),
167                                               'month' => $default_month = date('m'),
168                                               'day' => $default_day = date('d')),
169                           'requirements' => array('year' => '/(19|20)\d\d/',
170                                                   'month' => '/[01]?\d/',
171                                                   'day' => '/[0-3]?\d/')),
172                     array('path' => 'blog/:action',
173                           'defaults' => array('controller' => 'Blog',
174                                               'action' => 'display')));
175
176     $routes = new lmbRoutes($config);
177     $result = $routes->dispatch('/blog/2004/12');
178
179     $this->assertEqual($result['controller'], 'Blog');
180     $this->assertEqual($result['action'], 'archive');
181     $this->assertEqual($result['year'], '2004');
182     $this->assertEqual($result['month'], '12');
183     $this->assertEqual($result['day'], $default_day);
184
185     $result = $routes->dispatch('/blog/2004');
186
187     $this->assertEqual($result['controller'], 'Blog');
188     $this->assertEqual($result['action'], 'archive');
189     $this->assertEqual($result['year'], '2004');
190     $this->assertEqual($result['month'], $default_month);
191     $this->assertEqual($result['day'], $default_day);
192
193     $result = $routes->dispatch('/blog/1865');
194
195     $this->assertEqual($result['controller'], 'Blog');
196     $this->assertEqual($result['action'], '1865');
197     $this->assertFalse(isset($result['year']));
198
199     $result = $routes->dispatch('/blog/last_articles');
200
201     $this->assertEqual($result['controller'], 'Blog');
202     $this->assertEqual($result['action'], 'last_articles');
203     $this->assertFalse(isset($result['year']));
204   }
205
206   function testUrlWithUrlEncodedParams()
207   {
208     $config = array(
209       array(
210         'path' => '/:controller/:action/:email',
211         'requirements' => array(
212           'email' => '/^[a-z@.-]+$/i'
213         )
214       )
215     );
216     $routes = new lmbRoutes($config);
217     $result = $routes->dispatch('/blog/display/bob-sinclar%40yahoo.com');
218
219     $this->assertEqual($result['controller'], 'blog');
220     $this->assertEqual($result['action'], 'display');
221     $this->assertEqual($result['email'], 'bob-sinclar@yahoo.com');
222   }
223
224   function testApplyDispatchFilter()
225   {
226     $config = array(array('path' => '/:controller/:action',
227                           'defaults' => array('action' => 'display'),
228                           'dispatch_filter' => array($this, '_processDispatchResult')));
229
230     $routes = new lmbRoutes($config);
231     $result = $routes->dispatch('/blog/display');
232
233     $this->assertEqual($result['controller'], 'Blog');
234     $this->assertEqual($result['action'], 'display');
235   }
236
237   function testExtraDataAfterSecondDelimiter()
238   {
239      $config = array(array('path' => '/:controller:test/:action',
240                            'defaults' => array('action' => 'display')),
241                      array('path' => '/test2:controller/:action',
242                            'defaults' => array('action' => 'display')),
243                      array('path' => '/test3:controller:test5/:action/:id:.htm',
244                            'defaults' => array('action' => 'display')),
245                      array('path' => '/:controller:test6/:id:.htm',
246                            'defaults' => array('action' => 'display')));
247
248     $routes = new lmbRoutes($config);
249     $result = $routes->dispatch('/blogtest/index');
250
251     $this->assertEqual($result['controller'], 'blog');
252     $this->assertEqual($result['action'], 'index');
253
254     $result = $routes->dispatch('/test2blog');
255
256     $this->assertEqual($result['controller'], 'blog');
257     $this->assertEqual($result['action'], 'display');
258
259     $result = $routes->dispatch('/test3blogtest5/display/test.htm');
260
261     $this->assertEqual($result['controller'], 'blog');
262     $this->assertEqual($result['action'], 'display');
263     $this->assertEqual($result['id'], 'test');
264
265   $result = $routes->dispatch('/blogtest6/test.htm');
266
267     $this->assertEqual($result['controller'], 'blog');
268     $this->assertEqual($result['action'], 'display');
269     $this->assertEqual($result['id'], 'test');
270   }
271
272   function _processDispatchResult(&$dispatched)
273   {
274     if(isset($dispatched['controller']))
275       $dispatched['controller'] = lmb_camel_case($dispatched['controller']);
276   }
277 }
278