3 require_once(dirname(__FILE__
).'/../../fixtures/config/config.php');
5 require_once(AK_LIB_DIR
.DS
.'AkRouter.php');
7 class Test_of_AkRouter_Class
extends UnitTestCase
13 function Test_of_AkRouter_Class()
15 $this->Router
=& new AkRouter();
17 $this->Router
->_loadUrlRewriteSettings();
18 $this->url_prefix
= AK_URL_REWRITE_ENABLED ?
'' : '/?ak=';
20 $this->Router
->connect('/topic/:id', array('controller' => 'topic', 'action'=>'view', 'id'=>COMPULSORY
), array('id'=>'[0-9]+'));
21 $this->Router
->connect('/topic/:id/unread', array('controller' => 'topic','action'=>'unread','id'=>COMPULSORY
), array('id'=>'[0-9]+'));
23 $this->Router
->connect('/lists/:action/:id/:option', array('controller'=>'todo','option'=>COMPULSORY
));
24 $this->Router
->connect('/setup/*config_settings',array('controller'=>'setup'));
25 $this->Router
->connect('/redirect/:url',array('controller'=>'redirect'));
26 $this->Router
->connect('/regex/:text/:int',array('text'=>'/[A-Za-z]+/','int'=>'/[0-9]+/','controller'=>'regex'));
27 $this->Router
->connect('/customize/*options/:action',array('controller'=>'themes','options'=>3));
28 $this->Router
->connect('/blog/:action/:id',array('controller'=>'post','action'=>'list','id'=>OPTIONAL
, 'requirements'=>array('id'=>'/\d{1,}/')));
29 $this->Router
->connect('/:year/:month/:day',
30 array('controller' => 'articles','action' => 'view_headlines','year' => COMPULSORY
,'month' => 'all','day' => OPTIONAL
) ,
31 array('year'=>'/(20){1}\d{2}/','month'=>'/((1)?\d{1,2}){2}/','day'=>'/(([1-3])?\d{1,2}){2}/'));
32 $this->Router
->connect('/:webpage', array('controller' => 'page', 'action' => 'view_page', 'webpage' => 'index'),array('webpage'=>'/(\w|_)+/'));
33 $this->Router
->connect('/', array('controller' => 'page', 'action' => 'view_page', 'webpage'=>'index'));
34 $this->Router
->connect('/:controller/:action/:id');
38 function Test_connect()
40 $this->assertEqual(count($this->Router
->getRoutes()) , 12,'Wrong number of routes loaded. We expected 12');
43 function Test_toParams()
45 $input_value = '/lists/show/123/featured=1';
46 $expected = array('controller'=>'todo','action'=>'show','id'=>123,'option'=>'featured=1');
47 $this->assertEqual($this->Router
->toParams($input_value),$expected);
49 $input_value = '/lists/show/123';
50 $expected = array('controller'=>'lists','action'=>'show','id'=>123);
51 $this->assertEqual($this->Router
->toParams($input_value),$expected);
54 $input_value = '/redirect/'.urlencode('http://www.akelos.com/buscar_dominio');
55 $expected = array('controller'=>'redirect','url'=>'http://www.akelos.com/buscar_dominio');
56 $this->assertEqual($this->Router
->toParams($input_value),$expected);
58 $input_value = '/regex/abc/123';
59 $expected = array('controller'=>'regex','text'=>'abc','int'=>'123');
60 $this->assertEqual($this->Router
->toParams($input_value),$expected);
62 $input_value = '/regex/abc1/123';
63 $not_expected = array('controller'=>'regex','text'=>'abc1','int'=>'123');
64 $this->assertNotEqual($this->Router
->toParams($input_value),$not_expected);
66 $input_value = '/regex/abc/text';
67 $not_expected = array('controller'=>'regex','text'=>'abc','int'=>'text');
68 $this->assertNotEqual($this->Router
->toParams($input_value),$not_expected);
70 $input_value = '/contact_us';
71 $expected = array('controller'=>'page','action'=>'view_page','webpage'=>'contact_us');
72 $this->assertEqual($this->Router
->toParams($input_value),$expected);
75 $expected = array('controller'=>'page','action'=>'view_page','webpage'=>'index');
76 $this->assertEqual($this->Router
->toParams($input_value),$expected);
79 $expected = array('controller'=>'page','action'=>'view_page','webpage'=>'index');
80 $this->assertEqual($this->Router
->toParams($input_value),$expected);
82 $input_value = '/blog/';
83 $expected = array('controller'=>'post','action'=>'list','id'=>null);
84 $this->assertEqual($this->Router
->toParams($input_value),$expected);
87 $input_value = '/blog/view';
88 $expected = array('controller'=>'post','action'=>'view','id'=>null);
89 $this->assertEqual($this->Router
->toParams($input_value),$expected);
91 $input_value = '/blog/view/10/';
92 $expected = array('controller'=>'post','action'=>'view','id'=>'10');
93 $this->assertEqual($this->Router
->toParams($input_value),$expected);
95 $input_value = '/blog/view/newest/';
96 $expected = array('controller'=>'blog','action'=>'view','id'=>'newest');
97 $this->assertEqual($this->Router
->toParams($input_value),$expected);
99 $input_value = '/2005/10/';
100 $expected = array('controller' => 'articles','action' => 'view_headlines','year' => '2005','month' => '10', 'day' => null);
101 $this->assertEqual($this->Router
->toParams($input_value),$expected);
103 $input_value = '/2006/';
104 $expected = array('controller' => 'articles','action' => 'view_headlines','year' => '2006','month' => 'all', 'day' => null);
105 $this->assertEqual($this->Router
->toParams($input_value),$expected);
107 $input_value = '/user/list/12';
108 $expected = array('controller' => 'user','action' => 'list','id' => '12');
109 $this->assertEqual($this->Router
->toParams($input_value),$expected);
111 $input_value = '/setup/themes/clone/12/';
112 $expected = array('controller' => 'setup','config_settings' => array('themes','clone','12'));
113 $this->assertEqual($this->Router
->toParams($input_value),$expected);
115 $input_value = '/customize/blue/css/sans_serif/clone/';
116 $expected = array('controller' => 'themes','options' => array('blue','css','sans_serif'), 'action'=>'clone');
117 $this->assertEqual($this->Router
->toParams($input_value),$expected);
119 $input_value = '/customize/blue/css/invalid/sans_serif/clone/';
120 $not_expected = array('controller' => 'themes','options' => array('blue','css','invalid','sans_serif'), 'action'=>'clone');
121 $this->assertNotEqual($this->Router
->toParams($input_value),$not_expected);
126 function Test_toUrl()
128 $input_value = array('controller'=>'page','action'=>'view_page','webpage'=>'index');
130 $this->assertEqual($this->Router
->toUrl($input_value),$expected);
132 $input_value = array('controller'=>'page','action'=>'view_page','webpage'=>'contact_us');
133 $expected = $this->url_prefix
.'/contact_us/';
134 $this->assertEqual($this->Router
->toUrl($input_value),$expected);
137 $input_value = array('controller'=>'post','action'=>'list','id'=>null);
138 $expected = $this->url_prefix
.'/blog/';
139 $this->assertEqual($this->Router
->toUrl($input_value),$expected);
141 $input_value = array('controller'=>'post','action'=>'view','id'=>null);
142 $expected = $this->url_prefix
.'/blog/view/';
143 $this->assertEqual($this->Router
->toUrl($input_value),$expected);
145 $input_value = array('controller'=>'error','action'=>'database', 'id'=>null);
146 $expected = $this->url_prefix
.'/error/database/';
147 $this->assertEqual($this->Router
->toUrl($input_value),$expected);
149 $input_value = array('controller'=>'post','action'=>'view','id'=>'10');
150 $expected = $this->url_prefix
.'/blog/view/10/';
151 $this->assertEqual($this->Router
->toUrl($input_value),$expected);
153 $input_value = array('controller'=>'blog','action'=>'view','id'=>'newest');
154 $expected = $this->url_prefix
.'/blog/view/newest/';
155 $this->assertEqual($this->Router
->toUrl($input_value),$expected);
157 $input_value = array('controller'=>'blog','action'=>'view','id'=>'newest','format'=>'printer_friendly');
158 $expected = AK_URL_REWRITE_ENABLED ?
'/blog/view/newest/?format=printer_friendly' : '/?ak=/blog/view/newest/&format=printer_friendly';
159 $this->assertEqual($this->Router
->toUrl($input_value),$expected);
161 $input_value = array('controller' => 'articles','action' => 'view_headlines','year' => '2005','month' => '10', 'day' => null);
162 $expected = $this->url_prefix
.'/2005/10/';
163 $this->assertEqual($this->Router
->toUrl($input_value),$expected);
165 $input_value = array('controller' => 'articles','action' => 'view_headlines','year' => '2006','month' => 'all', 'day' => null);
166 $expected = $this->url_prefix
. '/2006/';
167 $this->assertEqual($this->Router
->toUrl($input_value),$expected);
169 $input_value = array('controller' => 'user','action' => 'list','id' => '12');
170 $expected = $this->url_prefix
.'/user/list/12/';
171 $this->assertEqual($this->Router
->toUrl($input_value),$expected);
173 $input_value = array('controller' => 'setup','config_settings' => array('themes','clone','12'));
174 $expected = $this->url_prefix
.'/setup/themes/clone/12/';
175 $this->assertEqual($this->Router
->toUrl($input_value),$expected);
177 $input_value = array('controller' => 'themes','options' => array('blue','css','sans_serif'), 'action'=>'clone');
178 $expected = $this->url_prefix
.'/customize/blue/css/sans_serif/clone/';
179 $this->assertEqual($this->Router
->toUrl($input_value),$expected);
183 function test_url_with_optional_variables()
185 $input_value = array('controller'=>'topic','action'=>'view','id'=>4);
186 $expected = $this->url_prefix
.'/topic/4/';
187 $this->assertEqual($this->Router
->toUrl($input_value),$expected);
189 $input_value = array('controller'=>'topic','action'=>'unread','id'=>4);
190 $expected = $this->url_prefix
.'/topic/4/unread/';
191 $this->assertEqual($this->Router
->toUrl($input_value),$expected);
196 class Test_for_default_routes
extends UnitTestCase
200 var $url_prefix = '';
202 function Test_for_default_routes()
204 $this->Router
=& new AkRouter();
206 $this->Router
->_loadUrlRewriteSettings();
207 $this->url_prefix
= AK_URL_REWRITE_ENABLED ?
'' : '/?ak=';
209 $this->Router
->connect('/:controller/:action/:id', array('controller' => 'page', 'action' => 'index'));
210 $this->Router
->connect('/', array('controller' => 'page', 'action' => 'index'));
215 function Test_connect()
217 $this->assertEqual(count($this->Router
->getRoutes()) , 2,'Wrong number of routes loaded. We expected 12');
220 function Test_toUrl()
222 $input_value = array('controller'=>'page','action'=>'listing');
223 $expected = '/page/listing/';
224 $this->assertEqual($this->Router
->toUrl($input_value),$expected);
229 # Fixes issue 27 reported by Jacek Jedrzejewski
230 class Tests_for_url_constants_named_as_url_variables
extends UnitTestCase
234 var $url_prefix = '';
238 $this->Router
=& new AkRouter();
239 $this->Router
->_loadUrlRewriteSettings();
240 $this->url_prefix
= AK_URL_REWRITE_ENABLED ?
'' : '/?ak=';
243 function test_same_pieces_1()
245 $this->Router
->connect('/foo/id/:id', array('controller'=>'some'), array('id'=>'[0-9]+'));
246 $this->assertEqual($this->Router
->toParams('/foo/id/1'), array('controller'=>'some', 'id'=>'1'));
249 function test_same_pieces_4()
251 $this->Router
->connect('/foo/bar/*bar', array('controller'=>'some'));
252 $this->assertEqual($this->Router
->toParams('/foo/bar/foobar'), array ('bar' => array ( 0 => 'foobar'), 'controller' => 'some'));
253 $this->assertEqual($this->Router
->toParams('/foo/bar/foobar/foobar2'), array('controller'=>'some', 'bar'=>array(0=>'foobar',1=>'foobar2')));
256 function test_same_pieces_5()
258 $this->Router
->connect('/foo/bar/*bar', array('controller'=>'some', 'bar'=>1));
259 $this->assertEqual($this->Router
->toParams('/foo/bar/foobar'), array('controller'=>'some', 'bar'=>array(0=>'foobar')));
262 function test_same_pieces_6()
264 $this->Router
->connect('/foo/:bar', array('variable'=>'ok'));
265 $this->Router
->connect('/baz/:bar', array('variable2'=>'ok', 'bar'=>COMPULSORY
));
266 $this->Router
->connect('/:controller');
268 $this->assertEqual($this->Router
->toParams('/foo/baz'), array('variable'=>'ok','bar'=>'baz'));
269 $this->assertEqual($this->Router
->toParams('/abc'), array('controller'=>'abc'));
270 $this->assertEqual($this->Router
->toParams('/fooabc'), array('controller'=>'fooabc'));
271 $this->assertEqual($this->Router
->toParams('/baz/bar'), array('variable2'=>'ok','bar'=>'bar'));
272 $this->assertEqual($this->Router
->toParams('/bazabc'), array('controller'=>'bazabc'));
277 class Test_for_middle_optional_values_when_generating_urls
extends UnitTestCase
280 var $url_prefix = '';
284 $this->Router
=& new AkRouter();
285 $this->Router
->_loadUrlRewriteSettings();
286 $this->url_prefix
= AK_URL_REWRITE_ENABLED ?
'' : '/?ak=';
289 function test_middle_values()
291 $this->Router
->connect('/news/feed/:type/:category',
292 array('controller'=>'news','action'=>'feed','type'=>'atom','category'=>'all'));
295 $input_value = array('controller'=>'news','action'=>'feed','type'=>'atom','category'=>'foobar');
296 $expected = $this->url_prefix
.'/news/feed/atom/foobar/';
297 $this->assertEqual($this->Router
->toUrl($input_value),$expected);
299 $input_value = array('controller'=>'news','action'=>'feed');
300 $expected = $this->url_prefix
.'/news/feed/';
301 $this->assertEqual($this->Router
->toUrl($input_value),$expected);
306 ak_test('Test_of_AkRouter_Class');
307 ak_test('Test_for_default_routes');
308 ak_test('Tests_for_url_constants_named_as_url_variables');
309 ak_test('Test_for_middle_optional_values_when_generating_urls');