/
DispatcherTest.php
1746 lines (1493 loc) · 48.5 KB
/
DispatcherTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* DispatcherTest file
*
* CakePHP(tm) Tests <https://book.cakephp.org/2.0/en/development/testing.html>
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @package Cake.Test.Case.Routing
* @since CakePHP(tm) v 1.2.0.4206
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
App::uses('Dispatcher', 'Routing');
App::uses('DispatcherFilter', 'Routing');
if (!class_exists('AppController', false)) {
require_once CAKE . 'Test' . DS . 'test_app' . DS . 'Controller' . DS . 'AppController.php';
} elseif (!defined('APP_CONTROLLER_EXISTS')) {
define('APP_CONTROLLER_EXISTS', true);
}
/**
* A testing stub that doesn't send headers.
*
* @package Cake.Test.Case.Routing
*/
class DispatcherMockCakeResponse extends CakeResponse {
protected function _sendHeader($name, $value = null) {
return $name . ' ' . $value;
}
}
/**
* TestDispatcher class
*
* @package Cake.Test.Case.Routing
*/
class TestDispatcher extends Dispatcher {
/**
* Controller instance, made publicly available for testing
*
* @var Controller
*/
public $controller;
/**
* invoke method
*
* @param Controller $controller
* @param CakeRequest $request
* @return CakeResponse
*/
protected function _invoke(Controller $controller, CakeRequest $request) {
$this->controller = $controller;
return parent::_invoke($controller, $request);
}
/**
* Helper function to test single method attaching for dispatcher filters
*
* @param CakeEvent $event
* @return void
*/
public function filterTest($event) {
$event->data['request']->params['eventName'] = $event->name();
}
/**
* Helper function to test single method attaching for dispatcher filters
*
* @param CakeEvent
* @return void
*/
public function filterTest2($event) {
$event->stopPropagation();
return $event->data['response'];
}
}
/**
* MyPluginAppController class
*
* @package Cake.Test.Case.Routing
*/
class MyPluginAppController extends AppController {
}
/**
* Abstract Class DispatcherTestAbstractController
*/
abstract class DispatcherTestAbstractController extends Controller {
abstract public function index();
}
/**
* Interface DispatcherTestInterfaceController
*/
interface DispatcherTestInterfaceController {
public function index();
}
/**
* MyPluginController class
*
* @package Cake.Test.Case.Routing
*/
class MyPluginController extends MyPluginAppController {
/**
* uses property
*
* @var array
*/
public $uses = array();
/**
* index method
*
* @return void
*/
public function index() {
return true;
}
/**
* add method
*
* @return void
*/
public function add() {
return true;
}
/**
* admin_add method
*
* @param mixed $id
* @return void
*/
public function admin_add($id = null) {
return $id;
}
}
/**
* SomePagesController class
*
* @package Cake.Test.Case.Routing
*/
class SomePagesController extends AppController {
/**
* uses property
*
* @var array
*/
public $uses = array();
/**
* display method
*
* @param string $page
* @return void
*/
public function display($page = null) {
return $page;
}
/**
* index method
*
* @return void
*/
public function index() {
return true;
}
/**
* Test method for returning responses.
*
* @return CakeResponse
*/
public function responseGenerator() {
return new CakeResponse(array('body' => 'new response'));
}
/**
* Test file sending
*
* @return CakeResponse
*/
public function sendfile() {
$this->response->file(CAKE . 'Test' . DS . 'test_app' . DS . 'Vendor' . DS . 'css' . DS . 'test_asset.css');
return $this->response;
}
}
/**
* OtherPagesController class
*
* @package Cake.Test.Case.Routing
*/
class OtherPagesController extends MyPluginAppController {
/**
* uses property
*
* @var array
*/
public $uses = array();
/**
* display method
*
* @param string $page
* @return void
*/
public function display($page = null) {
return $page;
}
/**
* index method
*
* @return void
*/
public function index() {
return true;
}
}
/**
* TestDispatchPagesController class
*
* @package Cake.Test.Case.Routing
*/
class TestDispatchPagesController extends AppController {
/**
* uses property
*
* @var array
*/
public $uses = array();
/**
* admin_index method
*
* @return void
*/
public function admin_index() {
return true;
}
/**
* camelCased method
*
* @return void
*/
public function camelCased() {
return true;
}
}
/**
* ArticlesTestAppController class
*
* @package Cake.Test.Case.Routing
*/
class ArticlesTestAppController extends AppController {
}
/**
* ArticlesTestController class
*
* @package Cake.Test.Case.Routing
*/
class ArticlesTestController extends ArticlesTestAppController {
/**
* uses property
*
* @var array
*/
public $uses = array();
/**
* admin_index method
*
* @return void
*/
public function admin_index() {
return true;
}
/**
* fake index method.
*
* @return void
*/
public function index() {
return true;
}
}
/**
* SomePostsController class
*
* @package Cake.Test.Case.Routing
*/
class SomePostsController extends AppController {
/**
* uses property
*
* @var array
*/
public $uses = array();
/**
* autoRender property
*
* @var bool
*/
public $autoRender = false;
/**
* beforeFilter method
*
* @return void
*/
public function beforeFilter() {
if ($this->params['action'] === 'index') {
$this->params['action'] = 'view';
} else {
$this->params['action'] = 'change';
}
$this->params['pass'] = array('changed');
}
/**
* index method
*
* @return void
*/
public function index() {
return true;
}
/**
* change method
*
* @return void
*/
public function change() {
return true;
}
}
/**
* TestCachedPagesController class
*
* @package Cake.Test.Case.Routing
*/
class TestCachedPagesController extends Controller {
/**
* uses property
*
* @var array
*/
public $uses = array();
/**
* helpers property
*
* @var array
*/
public $helpers = array('Cache', 'Html');
/**
* cacheAction property
*
* @var array
*/
public $cacheAction = array(
'index' => '+2 sec',
'test_nocache_tags' => '+2 sec',
'view' => '+2 sec'
);
/**
* Mock out the response object so it doesn't send headers.
*
* @var string
*/
protected $_responseClass = 'DispatcherMockCakeResponse';
/**
* viewPath property
*
* @var string
*/
public $viewPath = 'Posts';
/**
* index method
*
* @return void
*/
public function index() {
$this->render();
}
/**
* test_nocache_tags method
*
* @return void
*/
public function test_nocache_tags() {
$this->render();
}
/**
* view method
*
* @return void
*/
public function view($id = null) {
$this->render('index');
}
/**
* test cached forms / tests view object being registered
*
* @return void
*/
public function cache_form() {
$this->cacheAction = 10;
$this->helpers[] = 'Form';
}
/**
* Test cached views with themes.
*
* @return void
*/
public function themed() {
$this->cacheAction = 10;
$this->viewClass = 'Theme';
$this->theme = 'TestTheme';
}
}
/**
* TimesheetsController class
*
* @package Cake.Test.Case.Routing
*/
class TimesheetsController extends Controller {
/**
* uses property
*
* @var array
*/
public $uses = array();
/**
* index method
*
* @return void
*/
public function index() {
return true;
}
}
/**
* TestFilterDispatcher class
*
* @package Cake.Test.Case.Routing
*/
class TestFilterDispatcher extends DispatcherFilter {
public $priority = 10;
/**
* TestFilterDispatcher::beforeDispatch()
*
* @param mixed $event
* @return CakeResponse|bool
*/
public function beforeDispatch(CakeEvent $event) {
$event->stopPropagation();
$response = $event->data['request'];
$response->addParams(array('settings' => $this->settings));
return null;
}
/**
* TestFilterDispatcher::afterDispatch()
*
* @param mixed $event
* @return mixed boolean to stop the event dispatching or null to continue
*/
public function afterDispatch(CakeEvent $event) {
}
}
/**
* DispatcherTest class
*
* @package Cake.Test.Case.Routing
*/
class DispatcherTest extends CakeTestCase {
/**
* setUp method
*
* @return void
*/
public function setUp() {
parent::setUp();
$this->_get = $_GET;
$_GET = array();
$this->_post = $_POST;
$this->_files = $_FILES;
$this->_server = $_SERVER;
$this->_app = Configure::read('App');
Configure::write('App.base', false);
Configure::write('App.baseUrl', false);
Configure::write('App.dir', 'app');
Configure::write('App.webroot', 'webroot');
$this->_cache = Configure::read('Cache');
Configure::write('Cache.disable', true);
$this->_debug = Configure::read('debug');
App::build();
App::objects('plugin', null, false);
}
/**
* tearDown method
*
* @return void
*/
public function tearDown() {
parent::tearDown();
$_GET = $this->_get;
$_POST = $this->_post;
$_FILES = $this->_files;
$_SERVER = $this->_server;
App::build();
CakePlugin::unload();
Configure::write('App', $this->_app);
Configure::write('Cache', $this->_cache);
Configure::write('debug', $this->_debug);
Configure::write('Dispatcher.filters', array());
}
/**
* testParseParamsWithoutZerosAndEmptyPost method
*
* @return void
* @triggers DispatcherTest $Dispatcher, array('request' => $request)
*/
public function testParseParamsWithoutZerosAndEmptyPost() {
$Dispatcher = new Dispatcher();
$request = new CakeRequest("/testcontroller/testaction/params1/params2/params3");
$event = new CakeEvent('DispatcherTest', $Dispatcher, array('request' => $request));
$Dispatcher->parseParams($event);
$this->assertSame($request['controller'], 'testcontroller');
$this->assertSame($request['action'], 'testaction');
$this->assertSame($request['pass'][0], 'params1');
$this->assertSame($request['pass'][1], 'params2');
$this->assertSame($request['pass'][2], 'params3');
$this->assertFalse(!empty($request['form']));
}
/**
* testParseParamsReturnsPostedData method
*
* @return void
* @triggers DispatcherTest $Dispatcher, array('request' => $request)
*/
public function testParseParamsReturnsPostedData() {
$_POST['testdata'] = "My Posted Content";
$Dispatcher = new Dispatcher();
$request = new CakeRequest("/");
$event = new CakeEvent('DispatcherTest', $Dispatcher, array('request' => $request));
$Dispatcher->parseParams($event);
$Dispatcher->parseParams($event);
$this->assertEquals("My Posted Content", $request['data']['testdata']);
}
/**
* testParseParamsWithSingleZero method
*
* @return void
* @triggers DispatcherTest $Dispatcher, array('request' => $test)
*/
public function testParseParamsWithSingleZero() {
$Dispatcher = new Dispatcher();
$test = new CakeRequest("/testcontroller/testaction/1/0/23");
$event = new CakeEvent('DispatcherTest', $Dispatcher, array('request' => $test));
$Dispatcher->parseParams($event);
$this->assertSame($test['controller'], 'testcontroller');
$this->assertSame($test['action'], 'testaction');
$this->assertSame($test['pass'][0], '1');
$this->assertRegExp('/\\A(?:0)\\z/', $test['pass'][1]);
$this->assertSame($test['pass'][2], '23');
}
/**
* testParseParamsWithManySingleZeros method
*
* @return void
* @triggers DispatcherTest $Dispatcher, array('request' => $test)
*/
public function testParseParamsWithManySingleZeros() {
$Dispatcher = new Dispatcher();
$test = new CakeRequest("/testcontroller/testaction/0/0/0/0/0/0");
$event = new CakeEvent('DispatcherTest', $Dispatcher, array('request' => $test));
$Dispatcher->parseParams($event);
$this->assertRegExp('/\\A(?:0)\\z/', $test['pass'][0]);
$this->assertRegExp('/\\A(?:0)\\z/', $test['pass'][1]);
$this->assertRegExp('/\\A(?:0)\\z/', $test['pass'][2]);
$this->assertRegExp('/\\A(?:0)\\z/', $test['pass'][3]);
$this->assertRegExp('/\\A(?:0)\\z/', $test['pass'][4]);
$this->assertRegExp('/\\A(?:0)\\z/', $test['pass'][5]);
}
/**
* testParseParamsWithManyZerosInEachSectionOfUrl method
*
* @return void
* @triggers DispatcherTest $Dispatcher, array('request' => $test)
*/
public function testParseParamsWithManyZerosInEachSectionOfUrl() {
$Dispatcher = new Dispatcher();
$test = new CakeRequest("/testcontroller/testaction/000/0000/00000/000000/000000/0000000");
$event = new CakeEvent('DispatcherTest', $Dispatcher, array('request' => $test));
$Dispatcher->parseParams($event);
$this->assertRegExp('/\\A(?:000)\\z/', $test['pass'][0]);
$this->assertRegExp('/\\A(?:0000)\\z/', $test['pass'][1]);
$this->assertRegExp('/\\A(?:00000)\\z/', $test['pass'][2]);
$this->assertRegExp('/\\A(?:000000)\\z/', $test['pass'][3]);
$this->assertRegExp('/\\A(?:000000)\\z/', $test['pass'][4]);
$this->assertRegExp('/\\A(?:0000000)\\z/', $test['pass'][5]);
}
/**
* testParseParamsWithMixedOneToManyZerosInEachSectionOfUrl method
*
* @return void
* @triggers DispatcherTest $Dispatcher, array('request' => $test)
*/
public function testParseParamsWithMixedOneToManyZerosInEachSectionOfUrl() {
$Dispatcher = new Dispatcher();
$test = new CakeRequest("/testcontroller/testaction/01/0403/04010/000002/000030/0000400");
$event = new CakeEvent('DispatcherTest', $Dispatcher, array('request' => $test));
$Dispatcher->parseParams($event);
$this->assertRegExp('/\\A(?:01)\\z/', $test['pass'][0]);
$this->assertRegExp('/\\A(?:0403)\\z/', $test['pass'][1]);
$this->assertRegExp('/\\A(?:04010)\\z/', $test['pass'][2]);
$this->assertRegExp('/\\A(?:000002)\\z/', $test['pass'][3]);
$this->assertRegExp('/\\A(?:000030)\\z/', $test['pass'][4]);
$this->assertRegExp('/\\A(?:0000400)\\z/', $test['pass'][5]);
}
/**
* testQueryStringOnRoot method
*
* @return void
* @triggers DispatcherTest $Dispatcher, array('request' => $request)
* @triggers DispatcherTest $Dispatcher, array('request' => $request)
*/
public function testQueryStringOnRoot() {
Router::reload();
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
Router::connect('/:controller/:action/*');
$_GET = array('coffee' => 'life', 'sleep' => 'sissies');
$Dispatcher = new Dispatcher();
$request = new CakeRequest('posts/home/?coffee=life&sleep=sissies');
$event = new CakeEvent('DispatcherTest', $Dispatcher, array('request' => $request));
$Dispatcher->parseParams($event);
$this->assertRegExp('/posts/', $request['controller']);
$this->assertRegExp('/home/', $request['action']);
$this->assertTrue(isset($request['url']['sleep']));
$this->assertTrue(isset($request['url']['coffee']));
$Dispatcher = new Dispatcher();
$request = new CakeRequest('/?coffee=life&sleep=sissy');
$event = new CakeEvent('DispatcherTest', $Dispatcher, array('request' => $request));
$Dispatcher->parseParams($event);
$this->assertRegExp('/pages/', $request['controller']);
$this->assertRegExp('/display/', $request['action']);
$this->assertTrue(isset($request['url']['sleep']));
$this->assertTrue(isset($request['url']['coffee']));
$this->assertEquals('life', $request['url']['coffee']);
}
/**
* testMissingController method
*
* @expectedException MissingControllerException
* @expectedExceptionMessage Controller class SomeControllerController could not be found.
* @return void
*/
public function testMissingController() {
Router::connect('/:controller/:action/*');
$Dispatcher = new TestDispatcher();
Configure::write('App.baseUrl', '/index.php');
$url = new CakeRequest('some_controller/home/param:value/param2:value2');
$response = $this->getMock('CakeResponse');
$Dispatcher->dispatch($url, $response, array('return' => 1));
}
/**
* testMissingControllerInterface method
*
* @expectedException MissingControllerException
* @expectedExceptionMessage Controller class DispatcherTestInterfaceController could not be found.
* @return void
*/
public function testMissingControllerInterface() {
Router::connect('/:controller/:action/*');
$Dispatcher = new TestDispatcher();
Configure::write('App.baseUrl', '/index.php');
$url = new CakeRequest('dispatcher_test_interface/index');
$response = $this->getMock('CakeResponse');
$Dispatcher->dispatch($url, $response, array('return' => 1));
}
/**
* testMissingControllerInterface method
*
* @expectedException MissingControllerException
* @expectedExceptionMessage Controller class DispatcherTestAbstractController could not be found.
* @return void
*/
public function testMissingControllerAbstract() {
Router::connect('/:controller/:action/*');
$Dispatcher = new TestDispatcher();
Configure::write('App.baseUrl', '/index.php');
$url = new CakeRequest('dispatcher_test_abstract/index');
$response = $this->getMock('CakeResponse');
$Dispatcher->dispatch($url, $response, array('return' => 1));
}
/**
* testDispatch method
*
* @return void
*/
public function testDispatchBasic() {
App::build(array(
'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
));
$Dispatcher = new TestDispatcher();
Configure::write('App.baseUrl', '/index.php');
$url = new CakeRequest('pages/home/param:value/param2:value2');
$response = $this->getMock('CakeResponse');
$Dispatcher->dispatch($url, $response, array('return' => 1));
$expected = 'Pages';
$this->assertEquals($expected, $Dispatcher->controller->name);
$expected = array('0' => 'home', 'param' => 'value', 'param2' => 'value2');
$this->assertSame($expected, $Dispatcher->controller->passedArgs);
Configure::write('App.baseUrl', '/pages/index.php');
$url = new CakeRequest('pages/home');
$Dispatcher->dispatch($url, $response, array('return' => 1));
$expected = 'Pages';
$this->assertEquals($expected, $Dispatcher->controller->name);
$url = new CakeRequest('pages/home/');
$Dispatcher->dispatch($url, $response, array('return' => 1));
$this->assertNull($Dispatcher->controller->plugin);
$expected = 'Pages';
$this->assertEquals($expected, $Dispatcher->controller->name);
unset($Dispatcher);
require CAKE . 'Config' . DS . 'routes.php';
$Dispatcher = new TestDispatcher();
Configure::write('App.baseUrl', '/timesheets/index.php');
$url = new CakeRequest('timesheets');
$Dispatcher->dispatch($url, $response, array('return' => 1));
$expected = 'Timesheets';
$this->assertEquals($expected, $Dispatcher->controller->name);
$url = new CakeRequest('timesheets/');
$Dispatcher->dispatch($url, $response, array('return' => 1));
$this->assertEquals('Timesheets', $Dispatcher->controller->name);
$this->assertEquals('/timesheets/index.php', $url->base);
$url = new CakeRequest('test_dispatch_pages/camelCased');
$Dispatcher->dispatch($url, $response, array('return' => 1));
$this->assertEquals('TestDispatchPages', $Dispatcher->controller->name);
$url = new CakeRequest('test_dispatch_pages/camelCased/something. .');
$Dispatcher->dispatch($url, $response, array('return' => 1));
$this->assertEquals('something. .', $Dispatcher->controller->params['pass'][0], 'Period was chopped off. %s');
}
/**
* Test that Dispatcher handles actions that return response objects.
*
* @return void
*/
public function testDispatchActionReturnsResponse() {
Router::connect('/:controller/:action');
$Dispatcher = new Dispatcher();
$request = new CakeRequest('some_pages/responseGenerator');
$response = $this->getMock('CakeResponse', array('_sendHeader'));
ob_start();
$Dispatcher->dispatch($request, $response);
$result = ob_get_clean();
$this->assertEquals('new response', $result);
}
/**
* testDispatchActionSendsFile
*
* @return void
*/
public function testDispatchActionSendsFile() {
Router::connect('/:controller/:action');
$Dispatcher = new Dispatcher();
$request = new CakeRequest('some_pages/sendfile');
$response = $this->getMock('CakeResponse', array(
'header',
'type',
'download',
'_sendHeader',
'_setContentType',
'_isActive',
'_clearBuffer',
'_flushBuffer'
));
$response->expects($this->exactly(1))
->method('_isActive')
->will($this->returnValue(true));
ob_start();
$Dispatcher->dispatch($request, $response);
$result = ob_get_clean();
$this->assertEquals("/* this is the test asset css file */\n", $result);
}
/**
* testAdminDispatch method
*
* @return void
*/
public function testAdminDispatch() {
$_POST = array();
$Dispatcher = new TestDispatcher();
Configure::write('Routing.prefixes', array('admin'));
Configure::write('App.baseUrl', '/cake/repo/branches/1.2.x.x/index.php');
$url = new CakeRequest('admin/test_dispatch_pages/index/param:value/param2:value2');
$response = $this->getMock('CakeResponse');
Router::reload();
$Dispatcher->dispatch($url, $response, array('return' => 1));
$this->assertEquals('TestDispatchPages', $Dispatcher->controller->name);
$this->assertSame($Dispatcher->controller->passedArgs, array('param' => 'value', 'param2' => 'value2'));
$this->assertTrue($Dispatcher->controller->params['admin']);
$expected = '/cake/repo/branches/1.2.x.x/index.php/admin/test_dispatch_pages/index/param:value/param2:value2';
$this->assertSame($expected, $Dispatcher->controller->here);
$expected = '/cake/repo/branches/1.2.x.x/index.php';
$this->assertSame($expected, $Dispatcher->controller->base);
}
/**
* testPluginDispatch method
*
* @return void
* @triggers DispatcherTest $Dispatcher, array('request' => $url)
*/
public function testPluginDispatch() {
$_POST = array();
Router::reload();
$Dispatcher = new TestDispatcher();
Router::connect(
'/my_plugin/:controller/*',
array('plugin' => 'my_plugin', 'controller' => 'pages', 'action' => 'display')
);
$url = new CakeRequest('my_plugin/some_pages/home/param:value/param2:value2');
$response = $this->getMock('CakeResponse');
$Dispatcher->dispatch($url, $response, array('return' => 1));
$event = new CakeEvent('DispatcherTest', $Dispatcher, array('request' => $url));
$Dispatcher->parseParams($event);
$expected = array(
'pass' => array('home'),
'named' => array('param' => 'value', 'param2' => 'value2'), 'plugin' => 'my_plugin',
'controller' => 'some_pages', 'action' => 'display'
);
foreach ($expected as $key => $value) {
$this->assertEquals($value, $url[$key], 'Value mismatch ' . $key . ' %');
}
$this->assertSame($Dispatcher->controller->plugin, 'MyPlugin');
$this->assertSame($Dispatcher->controller->name, 'SomePages');
$this->assertSame($Dispatcher->controller->params['controller'], 'some_pages');
$this->assertSame($Dispatcher->controller->passedArgs, array('0' => 'home', 'param' => 'value', 'param2' => 'value2'));
}
/**
* testAutomaticPluginDispatch method
*
* @return void
*/
public function testAutomaticPluginDispatch() {
$_POST = array();
$_SERVER['PHP_SELF'] = '/cake/repo/branches/1.2.x.x/index.php';
Router::reload();
$Dispatcher = new TestDispatcher();
Router::connect(
'/my_plugin/:controller/:action/*',
array('plugin' => 'my_plugin', 'controller' => 'pages', 'action' => 'display')
);
$Dispatcher->base = false;
$url = new CakeRequest('my_plugin/other_pages/index/param:value/param2:value2');
$response = $this->getMock('CakeResponse');
$Dispatcher->dispatch($url, $response, array('return' => 1));
$this->assertSame($Dispatcher->controller->plugin, 'MyPlugin');
$this->assertSame($Dispatcher->controller->name, 'OtherPages');
$this->assertSame($Dispatcher->controller->action, 'index');
$this->assertSame($Dispatcher->controller->passedArgs, array('param' => 'value', 'param2' => 'value2'));
$expected = '/cake/repo/branches/1.2.x.x/my_plugin/other_pages/index/param:value/param2:value2';
$this->assertSame($expected, $url->here);
$expected = '/cake/repo/branches/1.2.x.x';
$this->assertSame($expected, $url->base);
}
/**