Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 1
<?php
2
/**
3
 * DispatcherTest file
4
 *
5
 * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
6
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
7
 *
8
 * Licensed under The MIT License
9
 * For full copyright and license information, please see the LICENSE.txt
10
 * Redistributions of files must retain the above copyright notice
11
 *
12
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13
 * @link          http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
14
 * @package       Cake.Test.Case.Routing
15
 * @since         CakePHP(tm) v 1.2.0.4206
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18
 
19
App::uses('Dispatcher', 'Routing');
20
 
21
if (!class_exists('AppController', false)) {
22
	require_once CAKE . 'Test' . DS . 'test_app' . DS . 'Controller' . DS . 'AppController.php';
23
} elseif (!defined('APP_CONTROLLER_EXISTS')) {
24
	define('APP_CONTROLLER_EXISTS', true);
25
}
26
 
27
/**
28
 * A testing stub that doesn't send headers.
29
 *
30
 * @package       Cake.Test.Case.Routing
31
 */
32
class DispatcherMockCakeResponse extends CakeResponse {
33
 
34
	protected function _sendHeader($name, $value = null) {
35
		return $name . ' ' . $value;
36
	}
37
 
38
}
39
 
40
/**
41
 * TestDispatcher class
42
 *
43
 * @package       Cake.Test.Case.Routing
44
 */
45
class TestDispatcher extends Dispatcher {
46
 
47
/**
48
 * Controller instance, made publicly available for testing
49
 *
50
 * @var Controller
51
 */
52
	public $controller;
53
 
54
/**
55
 * invoke method
56
 *
57
 * @param Controller $controller
58
 * @param CakeRequest $request
59
 * @param CakeResponse $response
60
 * @return void
61
 */
62
	protected function _invoke(Controller $controller, CakeRequest $request, CakeResponse $response) {
63
		$this->controller = $controller;
64
		return parent::_invoke($controller, $request, $response);
65
	}
66
 
67
/**
68
 * Helper function to test single method attaching for dispatcher filters
69
 *
70
 * @param CakeEvent $event
71
 * @return void
72
 */
73
	public function filterTest($event) {
74
		$event->data['request']->params['eventName'] = $event->name();
75
	}
76
 
77
/**
78
 * Helper function to test single method attaching for dispatcher filters
79
 *
80
 * @param CakeEvent
81
 * @return void
82
 */
83
	public function filterTest2($event) {
84
		$event->stopPropagation();
85
		return $event->data['response'];
86
	}
87
 
88
}
89
 
90
/**
91
 * MyPluginAppController class
92
 *
93
 * @package       Cake.Test.Case.Routing
94
 */
95
class MyPluginAppController extends AppController {
96
}
97
 
98
/**
99
 * Abstract Class DispatcherTestAbstractController
100
 */
101
abstract class DispatcherTestAbstractController extends Controller {
102
 
103
	abstract public function index();
104
 
105
}
106
 
107
/**
108
 * Interface DispatcherTestInterfaceController
109
 */
110
interface DispatcherTestInterfaceController {
111
 
112
	public function index();
113
 
114
}
115
 
116
/**
117
 * MyPluginController class
118
 *
119
 * @package       Cake.Test.Case.Routing
120
 */
121
class MyPluginController extends MyPluginAppController {
122
 
123
/**
124
 * uses property
125
 *
126
 * @var array
127
 */
128
	public $uses = array();
129
 
130
/**
131
 * index method
132
 *
133
 * @return void
134
 */
135
	public function index() {
136
		return true;
137
	}
138
 
139
/**
140
 * add method
141
 *
142
 * @return void
143
 */
144
	public function add() {
145
		return true;
146
	}
147
 
148
/**
149
 * admin_add method
150
 *
151
 * @param mixed $id
152
 * @return void
153
 */
154
	public function admin_add($id = null) {
155
		return $id;
156
	}
157
 
158
}
159
 
160
/**
161
 * SomePagesController class
162
 *
163
 * @package       Cake.Test.Case.Routing
164
 */
165
class SomePagesController extends AppController {
166
 
167
/**
168
 * uses property
169
 *
170
 * @var array
171
 */
172
	public $uses = array();
173
 
174
/**
175
 * display method
176
 *
177
 * @param string $page
178
 * @return void
179
 */
180
	public function display($page = null) {
181
		return $page;
182
	}
183
 
184
/**
185
 * index method
186
 *
187
 * @return void
188
 */
189
	public function index() {
190
		return true;
191
	}
192
 
193
/**
194
 * Test method for returning responses.
195
 *
196
 * @return CakeResponse
197
 */
198
	public function responseGenerator() {
199
		return new CakeResponse(array('body' => 'new response'));
200
	}
201
 
202
/**
203
 * Test file sending
204
 *
205
 * @return CakeResponse
206
 */
207
	public function sendfile() {
208
		$this->response->file(CAKE . 'Test' . DS . 'test_app' . DS . 'Vendor' . DS . 'css' . DS . 'test_asset.css');
209
		return $this->response;
210
	}
211
 
212
}
213
 
214
/**
215
 * OtherPagesController class
216
 *
217
 * @package       Cake.Test.Case.Routing
218
 */
219
class OtherPagesController extends MyPluginAppController {
220
 
221
/**
222
 * uses property
223
 *
224
 * @var array
225
 */
226
	public $uses = array();
227
 
228
/**
229
 * display method
230
 *
231
 * @param string $page
232
 * @return void
233
 */
234
	public function display($page = null) {
235
		return $page;
236
	}
237
 
238
/**
239
 * index method
240
 *
241
 * @return void
242
 */
243
	public function index() {
244
		return true;
245
	}
246
 
247
}
248
 
249
/**
250
 * TestDispatchPagesController class
251
 *
252
 * @package       Cake.Test.Case.Routing
253
 */
254
class TestDispatchPagesController extends AppController {
255
 
256
/**
257
 * uses property
258
 *
259
 * @var array
260
 */
261
	public $uses = array();
262
 
263
/**
264
 * admin_index method
265
 *
266
 * @return void
267
 */
268
	public function admin_index() {
269
		return true;
270
	}
271
 
272
/**
273
 * camelCased method
274
 *
275
 * @return void
276
 */
277
	public function camelCased() {
278
		return true;
279
	}
280
 
281
}
282
 
283
/**
284
 * ArticlesTestAppController class
285
 *
286
 * @package       Cake.Test.Case.Routing
287
 */
288
class ArticlesTestAppController extends AppController {
289
}
290
 
291
/**
292
 * ArticlesTestController class
293
 *
294
 * @package       Cake.Test.Case.Routing
295
 */
296
class ArticlesTestController extends ArticlesTestAppController {
297
 
298
/**
299
 * uses property
300
 *
301
 * @var array
302
 */
303
	public $uses = array();
304
 
305
/**
306
 * admin_index method
307
 *
308
 * @return void
309
 */
310
	public function admin_index() {
311
		return true;
312
	}
313
 
314
/**
315
 * fake index method.
316
 *
317
 * @return void
318
 */
319
	public function index() {
320
		return true;
321
	}
322
 
323
}
324
 
325
/**
326
 * SomePostsController class
327
 *
328
 * @package       Cake.Test.Case.Routing
329
 */
330
class SomePostsController extends AppController {
331
 
332
/**
333
 * uses property
334
 *
335
 * @var array
336
 */
337
	public $uses = array();
338
 
339
/**
340
 * autoRender property
341
 *
342
 * @var boolean
343
 */
344
	public $autoRender = false;
345
 
346
/**
347
 * beforeFilter method
348
 *
349
 * @return void
350
 */
351
	public function beforeFilter() {
352
		if ($this->params['action'] === 'index') {
353
			$this->params['action'] = 'view';
354
		} else {
355
			$this->params['action'] = 'change';
356
		}
357
		$this->params['pass'] = array('changed');
358
	}
359
 
360
/**
361
 * index method
362
 *
363
 * @return void
364
 */
365
	public function index() {
366
		return true;
367
	}
368
 
369
/**
370
 * change method
371
 *
372
 * @return void
373
 */
374
	public function change() {
375
		return true;
376
	}
377
 
378
}
379
 
380
/**
381
 * TestCachedPagesController class
382
 *
383
 * @package       Cake.Test.Case.Routing
384
 */
385
class TestCachedPagesController extends Controller {
386
 
387
/**
388
 * uses property
389
 *
390
 * @var array
391
 */
392
	public $uses = array();
393
 
394
/**
395
 * helpers property
396
 *
397
 * @var array
398
 */
399
	public $helpers = array('Cache', 'Html');
400
 
401
/**
402
 * cacheAction property
403
 *
404
 * @var array
405
 */
406
	public $cacheAction = array(
407
		'index' => '+2 sec',
408
		'test_nocache_tags' => '+2 sec',
409
		'view' => '+2 sec'
410
	);
411
 
412
/**
413
 * Mock out the response object so it doesn't send headers.
414
 *
415
 * @var string
416
 */
417
	protected $_responseClass = 'DispatcherMockCakeResponse';
418
 
419
/**
420
 * viewPath property
421
 *
422
 * @var string
423
 */
424
	public $viewPath = 'Posts';
425
 
426
/**
427
 * index method
428
 *
429
 * @return void
430
 */
431
	public function index() {
432
		$this->render();
433
	}
434
 
435
/**
436
 * test_nocache_tags method
437
 *
438
 * @return void
439
 */
440
	public function test_nocache_tags() {
441
		$this->render();
442
	}
443
 
444
/**
445
 * view method
446
 *
447
 * @return void
448
 */
449
	public function view($id = null) {
450
		$this->render('index');
451
	}
452
 
453
/**
454
 * test cached forms / tests view object being registered
455
 *
456
 * @return void
457
 */
458
	public function cache_form() {
459
		$this->cacheAction = 10;
460
		$this->helpers[] = 'Form';
461
	}
462
 
463
/**
464
 * Test cached views with themes.
465
 */
466
	public function themed() {
467
		$this->cacheAction = 10;
468
		$this->viewClass = 'Theme';
469
		$this->theme = 'TestTheme';
470
	}
471
 
472
}
473
 
474
/**
475
 * TimesheetsController class
476
 *
477
 * @package       Cake.Test.Case.Routing
478
 */
479
class TimesheetsController extends Controller {
480
 
481
/**
482
 * uses property
483
 *
484
 * @var array
485
 */
486
	public $uses = array();
487
 
488
/**
489
 * index method
490
 *
491
 * @return void
492
 */
493
	public function index() {
494
		return true;
495
	}
496
 
497
}
498
 
499
/**
500
 * DispatcherTest class
501
 *
502
 * @package       Cake.Test.Case.Routing
503
 */
504
class DispatcherTest extends CakeTestCase {
505
 
506
/**
507
 * setUp method
508
 *
509
 * @return void
510
 */
511
	public function setUp() {
512
		parent::setUp();
513
		$this->_get = $_GET;
514
		$_GET = array();
515
		$this->_post = $_POST;
516
		$this->_files = $_FILES;
517
		$this->_server = $_SERVER;
518
 
519
		$this->_app = Configure::read('App');
520
		Configure::write('App.base', false);
521
		Configure::write('App.baseUrl', false);
522
		Configure::write('App.dir', 'app');
523
		Configure::write('App.webroot', 'webroot');
524
 
525
		$this->_cache = Configure::read('Cache');
526
		Configure::write('Cache.disable', true);
527
 
528
		$this->_debug = Configure::read('debug');
529
 
530
		App::build();
531
		App::objects('plugin', null, false);
532
	}
533
 
534
/**
535
 * tearDown method
536
 *
537
 * @return void
538
 */
539
	public function tearDown() {
540
		parent::tearDown();
541
		$_GET = $this->_get;
542
		$_POST = $this->_post;
543
		$_FILES = $this->_files;
544
		$_SERVER = $this->_server;
545
		App::build();
546
		CakePlugin::unload();
547
		Configure::write('App', $this->_app);
548
		Configure::write('Cache', $this->_cache);
549
		Configure::write('debug', $this->_debug);
550
		Configure::write('Dispatcher.filters', array());
551
	}
552
 
553
/**
554
 * testParseParamsWithoutZerosAndEmptyPost method
555
 *
556
 * @return void
557
 */
558
	public function testParseParamsWithoutZerosAndEmptyPost() {
559
		$Dispatcher = new Dispatcher();
560
		$request = new CakeRequest("/testcontroller/testaction/params1/params2/params3");
561
		$event = new CakeEvent('DispatcherTest', $Dispatcher, array('request' => $request));
562
		$Dispatcher->parseParams($event);
563
		$this->assertSame($request['controller'], 'testcontroller');
564
		$this->assertSame($request['action'], 'testaction');
565
		$this->assertSame($request['pass'][0], 'params1');
566
		$this->assertSame($request['pass'][1], 'params2');
567
		$this->assertSame($request['pass'][2], 'params3');
568
		$this->assertFalse(!empty($request['form']));
569
	}
570
 
571
/**
572
 * testParseParamsReturnsPostedData method
573
 *
574
 * @return void
575
 */
576
	public function testParseParamsReturnsPostedData() {
577
		$_POST['testdata'] = "My Posted Content";
578
		$Dispatcher = new Dispatcher();
579
		$request = new CakeRequest("/");
580
		$event = new CakeEvent('DispatcherTest', $Dispatcher, array('request' => $request));
581
		$Dispatcher->parseParams($event);
582
		$Dispatcher->parseParams($event);
583
		$this->assertEquals("My Posted Content", $request['data']['testdata']);
584
	}
585
 
586
/**
587
 * testParseParamsWithSingleZero method
588
 *
589
 * @return void
590
 */
591
	public function testParseParamsWithSingleZero() {
592
		$Dispatcher = new Dispatcher();
593
		$test = new CakeRequest("/testcontroller/testaction/1/0/23");
594
		$event = new CakeEvent('DispatcherTest', $Dispatcher, array('request' => $test));
595
		$Dispatcher->parseParams($event);
596
 
597
		$this->assertSame($test['controller'], 'testcontroller');
598
		$this->assertSame($test['action'], 'testaction');
599
		$this->assertSame($test['pass'][0], '1');
600
		$this->assertRegExp('/\\A(?:0)\\z/', $test['pass'][1]);
601
		$this->assertSame($test['pass'][2], '23');
602
	}
603
 
604
/**
605
 * testParseParamsWithManySingleZeros method
606
 *
607
 * @return void
608
 */
609
	public function testParseParamsWithManySingleZeros() {
610
		$Dispatcher = new Dispatcher();
611
		$test = new CakeRequest("/testcontroller/testaction/0/0/0/0/0/0");
612
		$event = new CakeEvent('DispatcherTest', $Dispatcher, array('request' => $test));
613
		$Dispatcher->parseParams($event);
614
 
615
		$this->assertRegExp('/\\A(?:0)\\z/', $test['pass'][0]);
616
		$this->assertRegExp('/\\A(?:0)\\z/', $test['pass'][1]);
617
		$this->assertRegExp('/\\A(?:0)\\z/', $test['pass'][2]);
618
		$this->assertRegExp('/\\A(?:0)\\z/', $test['pass'][3]);
619
		$this->assertRegExp('/\\A(?:0)\\z/', $test['pass'][4]);
620
		$this->assertRegExp('/\\A(?:0)\\z/', $test['pass'][5]);
621
	}
622
 
623
/**
624
 * testParseParamsWithManyZerosInEachSectionOfUrl method
625
 *
626
 * @return void
627
 */
628
	public function testParseParamsWithManyZerosInEachSectionOfUrl() {
629
		$Dispatcher = new Dispatcher();
630
		$test = new CakeRequest("/testcontroller/testaction/000/0000/00000/000000/000000/0000000");
631
		$event = new CakeEvent('DispatcherTest', $Dispatcher, array('request' => $test));
632
		$Dispatcher->parseParams($event);
633
 
634
		$this->assertRegExp('/\\A(?:000)\\z/', $test['pass'][0]);
635
		$this->assertRegExp('/\\A(?:0000)\\z/', $test['pass'][1]);
636
		$this->assertRegExp('/\\A(?:00000)\\z/', $test['pass'][2]);
637
		$this->assertRegExp('/\\A(?:000000)\\z/', $test['pass'][3]);
638
		$this->assertRegExp('/\\A(?:000000)\\z/', $test['pass'][4]);
639
		$this->assertRegExp('/\\A(?:0000000)\\z/', $test['pass'][5]);
640
	}
641
 
642
/**
643
 * testParseParamsWithMixedOneToManyZerosInEachSectionOfUrl method
644
 *
645
 * @return void
646
 */
647
	public function testParseParamsWithMixedOneToManyZerosInEachSectionOfUrl() {
648
		$Dispatcher = new Dispatcher();
649
		$test = new CakeRequest("/testcontroller/testaction/01/0403/04010/000002/000030/0000400");
650
		$event = new CakeEvent('DispatcherTest', $Dispatcher, array('request' => $test));
651
		$Dispatcher->parseParams($event);
652
 
653
		$this->assertRegExp('/\\A(?:01)\\z/', $test['pass'][0]);
654
		$this->assertRegExp('/\\A(?:0403)\\z/', $test['pass'][1]);
655
		$this->assertRegExp('/\\A(?:04010)\\z/', $test['pass'][2]);
656
		$this->assertRegExp('/\\A(?:000002)\\z/', $test['pass'][3]);
657
		$this->assertRegExp('/\\A(?:000030)\\z/', $test['pass'][4]);
658
		$this->assertRegExp('/\\A(?:0000400)\\z/', $test['pass'][5]);
659
	}
660
 
661
/**
662
 * testQueryStringOnRoot method
663
 *
664
 * @return void
665
 */
666
	public function testQueryStringOnRoot() {
667
		Router::reload();
668
		Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
669
		Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
670
		Router::connect('/:controller/:action/*');
671
 
672
		$_GET = array('coffee' => 'life', 'sleep' => 'sissies');
673
		$Dispatcher = new Dispatcher();
674
		$request = new CakeRequest('posts/home/?coffee=life&sleep=sissies');
675
		$event = new CakeEvent('DispatcherTest', $Dispatcher, array('request' => $request));
676
		$Dispatcher->parseParams($event);
677
 
678
		$this->assertRegExp('/posts/', $request['controller']);
679
		$this->assertRegExp('/home/', $request['action']);
680
		$this->assertTrue(isset($request['url']['sleep']));
681
		$this->assertTrue(isset($request['url']['coffee']));
682
 
683
		$Dispatcher = new Dispatcher();
684
		$request = new CakeRequest('/?coffee=life&sleep=sissy');
685
 
686
		$event = new CakeEvent('DispatcherTest', $Dispatcher, array('request' => $request));
687
		$Dispatcher->parseParams($event);
688
		$this->assertRegExp('/pages/', $request['controller']);
689
		$this->assertRegExp('/display/', $request['action']);
690
		$this->assertTrue(isset($request['url']['sleep']));
691
		$this->assertTrue(isset($request['url']['coffee']));
692
		$this->assertEquals('life', $request['url']['coffee']);
693
	}
694
 
695
/**
696
 * testMissingController method
697
 *
698
 * @expectedException MissingControllerException
699
 * @expectedExceptionMessage Controller class SomeControllerController could not be found.
700
 * @return void
701
 */
702
	public function testMissingController() {
703
		Router::connect('/:controller/:action/*');
704
 
705
		$Dispatcher = new TestDispatcher();
706
		Configure::write('App.baseUrl', '/index.php');
707
		$url = new CakeRequest('some_controller/home/param:value/param2:value2');
708
		$response = $this->getMock('CakeResponse');
709
 
710
		$Dispatcher->dispatch($url, $response, array('return' => 1));
711
	}
712
 
713
/**
714
 * testMissingControllerInterface method
715
 *
716
 * @expectedException MissingControllerException
717
 * @expectedExceptionMessage Controller class DispatcherTestInterfaceController could not be found.
718
 * @return void
719
 */
720
	public function testMissingControllerInterface() {
721
		Router::connect('/:controller/:action/*');
722
 
723
		$Dispatcher = new TestDispatcher();
724
		Configure::write('App.baseUrl', '/index.php');
725
		$url = new CakeRequest('dispatcher_test_interface/index');
726
		$response = $this->getMock('CakeResponse');
727
 
728
		$Dispatcher->dispatch($url, $response, array('return' => 1));
729
	}
730
 
731
/**
732
 * testMissingControllerInterface method
733
 *
734
 * @expectedException MissingControllerException
735
 * @expectedExceptionMessage Controller class DispatcherTestAbstractController could not be found.
736
 * @return void
737
 */
738
	public function testMissingControllerAbstract() {
739
		Router::connect('/:controller/:action/*');
740
 
741
		$Dispatcher = new TestDispatcher();
742
		Configure::write('App.baseUrl', '/index.php');
743
		$url = new CakeRequest('dispatcher_test_abstract/index');
744
		$response = $this->getMock('CakeResponse');
745
 
746
		$Dispatcher->dispatch($url, $response, array('return' => 1));
747
	}
748
 
749
/**
750
 * testDispatch method
751
 *
752
 * @return void
753
 */
754
	public function testDispatchBasic() {
755
		App::build(array(
756
			'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
757
		));
758
		$Dispatcher = new TestDispatcher();
759
		Configure::write('App.baseUrl', '/index.php');
760
		$url = new CakeRequest('pages/home/param:value/param2:value2');
761
		$response = $this->getMock('CakeResponse');
762
 
763
		$Dispatcher->dispatch($url, $response, array('return' => 1));
764
		$expected = 'Pages';
765
		$this->assertEquals($expected, $Dispatcher->controller->name);
766
 
767
		$expected = array('0' => 'home', 'param' => 'value', 'param2' => 'value2');
768
		$this->assertSame($expected, $Dispatcher->controller->passedArgs);
769
 
770
		Configure::write('App.baseUrl', '/pages/index.php');
771
 
772
		$url = new CakeRequest('pages/home');
773
		$Dispatcher->dispatch($url, $response, array('return' => 1));
774
 
775
		$expected = 'Pages';
776
		$this->assertEquals($expected, $Dispatcher->controller->name);
777
 
778
		$url = new CakeRequest('pages/home/');
779
		$Dispatcher->dispatch($url, $response, array('return' => 1));
780
		$this->assertNull($Dispatcher->controller->plugin);
781
 
782
		$expected = 'Pages';
783
		$this->assertEquals($expected, $Dispatcher->controller->name);
784
 
785
		unset($Dispatcher);
786
 
787
		require CAKE . 'Config' . DS . 'routes.php';
788
		$Dispatcher = new TestDispatcher();
789
		Configure::write('App.baseUrl', '/timesheets/index.php');
790
 
791
		$url = new CakeRequest('timesheets');
792
		$Dispatcher->dispatch($url, $response, array('return' => 1));
793
 
794
		$expected = 'Timesheets';
795
		$this->assertEquals($expected, $Dispatcher->controller->name);
796
 
797
		$url = new CakeRequest('timesheets/');
798
		$Dispatcher->dispatch($url, $response, array('return' => 1));
799
 
800
		$this->assertEquals('Timesheets', $Dispatcher->controller->name);
801
		$this->assertEquals('/timesheets/index.php', $url->base);
802
 
803
		$url = new CakeRequest('test_dispatch_pages/camelCased');
804
		$Dispatcher->dispatch($url, $response, array('return' => 1));
805
		$this->assertEquals('TestDispatchPages', $Dispatcher->controller->name);
806
 
807
		$url = new CakeRequest('test_dispatch_pages/camelCased/something. .');
808
		$Dispatcher->dispatch($url, $response, array('return' => 1));
809
		$this->assertEquals('something. .', $Dispatcher->controller->params['pass'][0], 'Period was chopped off. %s');
810
	}
811
 
812
/**
813
 * Test that Dispatcher handles actions that return response objects.
814
 *
815
 * @return void
816
 */
817
	public function testDispatchActionReturnsResponse() {
818
		Router::connect('/:controller/:action');
819
		$Dispatcher = new Dispatcher();
820
		$request = new CakeRequest('some_pages/responseGenerator');
821
		$response = $this->getMock('CakeResponse', array('_sendHeader'));
822
 
823
		ob_start();
824
		$Dispatcher->dispatch($request, $response);
825
		$result = ob_get_clean();
826
 
827
		$this->assertEquals('new response', $result);
828
	}
829
 
830
/**
831
 * testDispatchActionSendsFile
832
 *
833
 * @return void
834
 */
835
	public function testDispatchActionSendsFile() {
836
		Router::connect('/:controller/:action');
837
		$Dispatcher = new Dispatcher();
838
		$request = new CakeRequest('some_pages/sendfile');
839
		$response = $this->getMock('CakeResponse', array(
840
			'header',
841
			'type',
842
			'download',
843
			'_sendHeader',
844
			'_setContentType',
845
			'_isActive',
846
			'_clearBuffer',
847
			'_flushBuffer'
848
		));
849
 
850
		$response->expects($this->never())
851
			->method('body');
852
 
853
		$response->expects($this->exactly(1))
854
			->method('_isActive')
855
			->will($this->returnValue(true));
856
 
857
		ob_start();
858
		$Dispatcher->dispatch($request, $response);
859
		$result = ob_get_clean();
860
 
861
		$this->assertEquals("/* this is the test asset css file */\n", $result);
862
	}
863
 
864
/**
865
 * testAdminDispatch method
866
 *
867
 * @return void
868
 */
869
	public function testAdminDispatch() {
870
		$_POST = array();
871
		$Dispatcher = new TestDispatcher();
872
		Configure::write('Routing.prefixes', array('admin'));
873
		Configure::write('App.baseUrl', '/cake/repo/branches/1.2.x.x/index.php');
874
		$url = new CakeRequest('admin/test_dispatch_pages/index/param:value/param2:value2');
875
		$response = $this->getMock('CakeResponse');
876
 
877
		Router::reload();
878
		$Dispatcher->dispatch($url, $response, array('return' => 1));
879
 
880
		$this->assertEquals('TestDispatchPages', $Dispatcher->controller->name);
881
 
882
		$this->assertSame($Dispatcher->controller->passedArgs, array('param' => 'value', 'param2' => 'value2'));
883
		$this->assertTrue($Dispatcher->controller->params['admin']);
884
 
885
		$expected = '/cake/repo/branches/1.2.x.x/index.php/admin/test_dispatch_pages/index/param:value/param2:value2';
886
		$this->assertSame($expected, $Dispatcher->controller->here);
887
 
888
		$expected = '/cake/repo/branches/1.2.x.x/index.php';
889
		$this->assertSame($expected, $Dispatcher->controller->base);
890
	}
891
 
892
/**
893
 * testPluginDispatch method
894
 *
895
 * @return void
896
 */
897
	public function testPluginDispatch() {
898
		$_POST = array();
899
 
900
		Router::reload();
901
		$Dispatcher = new TestDispatcher();
902
		Router::connect(
903
			'/my_plugin/:controller/*',
904
			array('plugin' => 'my_plugin', 'controller' => 'pages', 'action' => 'display')
905
		);
906
 
907
		$url = new CakeRequest('my_plugin/some_pages/home/param:value/param2:value2');
908
		$response = $this->getMock('CakeResponse');
909
		$Dispatcher->dispatch($url, $response, array('return' => 1));
910
 
911
		$event = new CakeEvent('DispatcherTest', $Dispatcher, array('request' => $url));
912
		$Dispatcher->parseParams($event);
913
		$expected = array(
914
			'pass' => array('home'),
915
			'named' => array('param' => 'value', 'param2' => 'value2'), 'plugin' => 'my_plugin',
916
			'controller' => 'some_pages', 'action' => 'display'
917
		);
918
		foreach ($expected as $key => $value) {
919
			$this->assertEquals($value, $url[$key], 'Value mismatch ' . $key . ' %');
920
		}
921
 
922
		$this->assertSame($Dispatcher->controller->plugin, 'MyPlugin');
923
		$this->assertSame($Dispatcher->controller->name, 'SomePages');
924
		$this->assertSame($Dispatcher->controller->params['controller'], 'some_pages');
925
		$this->assertSame($Dispatcher->controller->passedArgs, array('0' => 'home', 'param' => 'value', 'param2' => 'value2'));
926
	}
927
 
928
/**
929
 * testAutomaticPluginDispatch method
930
 *
931
 * @return void
932
 */
933
	public function testAutomaticPluginDispatch() {
934
		$_POST = array();
935
		$_SERVER['PHP_SELF'] = '/cake/repo/branches/1.2.x.x/index.php';
936
 
937
		Router::reload();
938
		$Dispatcher = new TestDispatcher();
939
		Router::connect(
940
			'/my_plugin/:controller/:action/*',
941
			array('plugin' => 'my_plugin', 'controller' => 'pages', 'action' => 'display')
942
		);
943
 
944
		$Dispatcher->base = false;
945
 
946
		$url = new CakeRequest('my_plugin/other_pages/index/param:value/param2:value2');
947
		$response = $this->getMock('CakeResponse');
948
		$Dispatcher->dispatch($url, $response, array('return' => 1));
949
 
950
		$this->assertSame($Dispatcher->controller->plugin, 'MyPlugin');
951
		$this->assertSame($Dispatcher->controller->name, 'OtherPages');
952
		$this->assertSame($Dispatcher->controller->action, 'index');
953
		$this->assertSame($Dispatcher->controller->passedArgs, array('param' => 'value', 'param2' => 'value2'));
954
 
955
		$expected = '/cake/repo/branches/1.2.x.x/my_plugin/other_pages/index/param:value/param2:value2';
956
		$this->assertSame($expected, $url->here);
957
 
958
		$expected = '/cake/repo/branches/1.2.x.x';
959
		$this->assertSame($expected, $url->base);
960
	}
961
 
962
/**
963
 * testAutomaticPluginControllerDispatch method
964
 *
965
 * @return void
966
 */
967
	public function testAutomaticPluginControllerDispatch() {
968
		$plugins = App::objects('plugin');
969
		$plugins[] = 'MyPlugin';
970
		$plugins[] = 'ArticlesTest';
971
 
972
		CakePlugin::load('MyPlugin', array('path' => '/fake/path'));
973
 
974
		Router::reload();
975
		$Dispatcher = new TestDispatcher();
976
		$Dispatcher->base = false;
977
 
978
		$url = new CakeRequest('my_plugin/my_plugin/add/param:value/param2:value2');
979
		$response = $this->getMock('CakeResponse');
980
 
981
		$Dispatcher->dispatch($url, $response, array('return' => 1));
982
 
983
		$this->assertSame($Dispatcher->controller->plugin, 'MyPlugin');
984
		$this->assertSame($Dispatcher->controller->name, 'MyPlugin');
985
		$this->assertSame($Dispatcher->controller->action, 'add');
986
		$this->assertEquals(array('param' => 'value', 'param2' => 'value2'), $Dispatcher->controller->params['named']);
987
 
988
		Router::reload();
989
		require CAKE . 'Config' . DS . 'routes.php';
990
		$Dispatcher = new TestDispatcher();
991
		$Dispatcher->base = false;
992
 
993
		// Simulates the Route for a real plugin, installed in APP/plugins
994
		Router::connect('/my_plugin/:controller/:action/*', array('plugin' => 'my_plugin'));
995
 
996
		$plugin = 'MyPlugin';
997
		$pluginUrl = Inflector::underscore($plugin);
998
 
999
		$url = new CakeRequest($pluginUrl);
1000
		$Dispatcher->dispatch($url, $response, array('return' => 1));
1001
		$this->assertSame($Dispatcher->controller->plugin, 'MyPlugin');
1002
		$this->assertSame($Dispatcher->controller->name, 'MyPlugin');
1003
		$this->assertSame($Dispatcher->controller->action, 'index');
1004
 
1005
		$expected = $pluginUrl;
1006
		$this->assertEquals($expected, $Dispatcher->controller->params['controller']);
1007
 
1008
		Configure::write('Routing.prefixes', array('admin'));
1009
 
1010
		Router::reload();
1011
		require CAKE . 'Config' . DS . 'routes.php';
1012
		$Dispatcher = new TestDispatcher();
1013
 
1014
		$url = new CakeRequest('admin/my_plugin/my_plugin/add/5/param:value/param2:value2');
1015
		$response = $this->getMock('CakeResponse');
1016
 
1017
		$Dispatcher->dispatch($url, $response, array('return' => 1));
1018
 
1019
		$this->assertEquals('my_plugin', $Dispatcher->controller->params['plugin']);
1020
		$this->assertEquals('my_plugin', $Dispatcher->controller->params['controller']);
1021
		$this->assertEquals('admin_add', $Dispatcher->controller->params['action']);
1022
		$this->assertEquals(array(5), $Dispatcher->controller->params['pass']);
1023
		$this->assertEquals(array('param' => 'value', 'param2' => 'value2'), $Dispatcher->controller->params['named']);
1024
		$this->assertSame($Dispatcher->controller->plugin, 'MyPlugin');
1025
		$this->assertSame($Dispatcher->controller->name, 'MyPlugin');
1026
		$this->assertSame($Dispatcher->controller->action, 'admin_add');
1027
 
1028
		$expected = array(0 => 5, 'param' => 'value', 'param2' => 'value2');
1029
		$this->assertEquals($expected, $Dispatcher->controller->passedArgs);
1030
 
1031
		Configure::write('Routing.prefixes', array('admin'));
1032
		CakePlugin::load('ArticlesTest', array('path' => '/fake/path'));
1033
		Router::reload();
1034
		require CAKE . 'Config' . DS . 'routes.php';
1035
 
1036
		$Dispatcher = new TestDispatcher();
1037
 
1038
		$Dispatcher->dispatch(new CakeRequest('admin/articles_test'), $response, array('return' => 1));
1039
		$this->assertSame($Dispatcher->controller->plugin, 'ArticlesTest');
1040
		$this->assertSame($Dispatcher->controller->name, 'ArticlesTest');
1041
		$this->assertSame($Dispatcher->controller->action, 'admin_index');
1042
 
1043
		$expected = array(
1044
			'pass' => array(),
1045
			'named' => array(),
1046
			'controller' => 'articles_test',
1047
			'plugin' => 'articles_test',
1048
			'action' => 'admin_index',
1049
			'prefix' => 'admin',
1050
			'admin' => true,
1051
			'return' => 1
1052
		);
1053
		foreach ($expected as $key => $value) {
1054
			$this->assertEquals($expected[$key], $Dispatcher->controller->request[$key], 'Value mismatch ' . $key);
1055
		}
1056
	}
1057
 
1058
/**
1059
 * test Plugin dispatching without controller name and using
1060
 * plugin short form instead.
1061
 *
1062
 * @return void
1063
 */
1064
	public function testAutomaticPluginDispatchWithShortAccess() {
1065
		CakePlugin::load('MyPlugin', array('path' => '/fake/path'));
1066
		Router::reload();
1067
 
1068
		$Dispatcher = new TestDispatcher();
1069
		$Dispatcher->base = false;
1070
 
1071
		$url = new CakeRequest('my_plugin/');
1072
		$response = $this->getMock('CakeResponse');
1073
 
1074
		$Dispatcher->dispatch($url, $response, array('return' => 1));
1075
		$this->assertEquals('my_plugin', $Dispatcher->controller->params['controller']);
1076
		$this->assertEquals('my_plugin', $Dispatcher->controller->params['plugin']);
1077
		$this->assertEquals('index', $Dispatcher->controller->params['action']);
1078
		$this->assertFalse(isset($Dispatcher->controller->params['pass'][0]));
1079
	}
1080
 
1081
/**
1082
 * test plugin shortcut URLs with controllers that need to be loaded,
1083
 * the above test uses a controller that has already been included.
1084
 *
1085
 * @return void
1086
 */
1087
	public function testPluginShortCutUrlsWithControllerThatNeedsToBeLoaded() {
1088
		$loaded = class_exists('TestPluginController', false);
1089
		$this->skipIf($loaded, 'TestPluginController already loaded.');
1090
 
1091
		Router::reload();
1092
		App::build(array(
1093
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
1094
		), App::RESET);
1095
		CakePlugin::load(array('TestPlugin', 'TestPluginTwo'));
1096
 
1097
		$Dispatcher = new TestDispatcher();
1098
		$Dispatcher->base = false;
1099
 
1100
		$url = new CakeRequest('test_plugin/');
1101
		$response = $this->getMock('CakeResponse');
1102
 
1103
		$Dispatcher->dispatch($url, $response, array('return' => 1));
1104
		$this->assertEquals('test_plugin', $Dispatcher->controller->params['controller']);
1105
		$this->assertEquals('test_plugin', $Dispatcher->controller->params['plugin']);
1106
		$this->assertEquals('index', $Dispatcher->controller->params['action']);
1107
		$this->assertFalse(isset($Dispatcher->controller->params['pass'][0]));
1108
 
1109
		$url = new CakeRequest('/test_plugin/tests/index');
1110
		$Dispatcher->dispatch($url, $response, array('return' => 1));
1111
		$this->assertEquals('tests', $Dispatcher->controller->params['controller']);
1112
		$this->assertEquals('test_plugin', $Dispatcher->controller->params['plugin']);
1113
		$this->assertEquals('index', $Dispatcher->controller->params['action']);
1114
		$this->assertFalse(isset($Dispatcher->controller->params['pass'][0]));
1115
 
1116
		$url = new CakeRequest('/test_plugin/tests/index/some_param');
1117
		$Dispatcher->dispatch($url, $response, array('return' => 1));
1118
		$this->assertEquals('tests', $Dispatcher->controller->params['controller']);
1119
		$this->assertEquals('test_plugin', $Dispatcher->controller->params['plugin']);
1120
		$this->assertEquals('index', $Dispatcher->controller->params['action']);
1121
		$this->assertEquals('some_param', $Dispatcher->controller->params['pass'][0]);
1122
 
1123
		App::build();
1124
	}
1125
 
1126
/**
1127
 * testAutomaticPluginControllerMissingActionDispatch method
1128
 *
1129
 * @expectedException MissingActionException
1130
 * @expectedExceptionMessage Action MyPluginController::not_here() could not be found.
1131
 * @return void
1132
 */
1133
	public function testAutomaticPluginControllerMissingActionDispatch() {
1134
		Router::reload();
1135
		$Dispatcher = new TestDispatcher();
1136
 
1137
		$url = new CakeRequest('my_plugin/not_here/param:value/param2:value2');
1138
		$response = $this->getMock('CakeResponse');
1139
 
1140
		$Dispatcher->dispatch($url, $response, array('return' => 1));
1141
	}
1142
 
1143
/**
1144
 * testAutomaticPluginControllerMissingActionDispatch method
1145
 *
1146
 * @expectedException MissingActionException
1147
 * @expectedExceptionMessage Action MyPluginController::param:value() could not be found.
1148
 * @return void
1149
 */
1150
 
1151
	public function testAutomaticPluginControllerIndexMissingAction() {
1152
		Router::reload();
1153
		$Dispatcher = new TestDispatcher();
1154
 
1155
		$url = new CakeRequest('my_plugin/param:value/param2:value2');
1156
		$response = $this->getMock('CakeResponse');
1157
 
1158
		$Dispatcher->dispatch($url, $response, array('return' => 1));
1159
	}
1160
 
1161
/**
1162
 * Test dispatching into the TestPlugin in the test_app
1163
 *
1164
 * @return void
1165
 */
1166
	public function testTestPluginDispatch() {
1167
		$Dispatcher = new TestDispatcher();
1168
		App::build(array(
1169
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
1170
		), App::RESET);
1171
		CakePlugin::load(array('TestPlugin', 'TestPluginTwo'));
1172
		Router::reload();
1173
		Router::parse('/');
1174
 
1175
		$url = new CakeRequest('/test_plugin/tests/index');
1176
		$response = $this->getMock('CakeResponse');
1177
		$Dispatcher->dispatch($url, $response, array('return' => 1));
1178
		$this->assertTrue(class_exists('TestsController'));
1179
		$this->assertTrue(class_exists('TestPluginAppController'));
1180
		$this->assertTrue(class_exists('PluginsComponent'));
1181
 
1182
		$this->assertEquals('tests', $Dispatcher->controller->params['controller']);
1183
		$this->assertEquals('test_plugin', $Dispatcher->controller->params['plugin']);
1184
		$this->assertEquals('index', $Dispatcher->controller->params['action']);
1185
 
1186
		App::build();
1187
	}
1188
 
1189
/**
1190
 * Tests that it is possible to attach filter classes to the dispatch cycle
1191
 *
1192
 * @return void
1193
 */
1194
	public function testDispatcherFilterSubscriber() {
1195
		App::build(array(
1196
			'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS),
1197
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
1198
		), App::RESET);
1199
 
1200
		CakePlugin::load('TestPlugin');
1201
		Configure::write('Dispatcher.filters', array(
1202
			array('callable' => 'TestPlugin.TestDispatcherFilter')
1203
		));
1204
		$dispatcher = new TestDispatcher();
1205
		$request = new CakeRequest('/');
1206
		$request->params['altered'] = false;
1207
		$response = $this->getMock('CakeResponse', array('send'));
1208
 
1209
		$dispatcher->dispatch($request, $response);
1210
		$this->assertTrue($request->params['altered']);
1211
		$this->assertEquals(304, $response->statusCode());
1212
 
1213
		Configure::write('Dispatcher.filters', array(
1214
			'TestPlugin.Test2DispatcherFilter',
1215
			'TestPlugin.TestDispatcherFilter'
1216
		));
1217
		$dispatcher = new TestDispatcher();
1218
		$request = new CakeRequest('/');
1219
		$request->params['altered'] = false;
1220
		$response = $this->getMock('CakeResponse', array('send'));
1221
 
1222
		$dispatcher->dispatch($request, $response);
1223
		$this->assertFalse($request->params['altered']);
1224
		$this->assertEquals(500, $response->statusCode());
1225
		$this->assertNull($dispatcher->controller);
1226
	}
1227
 
1228
/**
1229
 * Tests that attaching an inexistent class as filter will throw an exception
1230
 *
1231
 * @expectedException MissingDispatcherFilterException
1232
 * @return void
1233
 */
1234
	public function testDispatcherFilterSuscriberMissing() {
1235
		App::build(array(
1236
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
1237
		), App::RESET);
1238
 
1239
		CakePlugin::load('TestPlugin');
1240
		Configure::write('Dispatcher.filters', array(
1241
			array('callable' => 'TestPlugin.NotAFilter')
1242
		));
1243
		$dispatcher = new TestDispatcher();
1244
		$request = new CakeRequest('/');
1245
		$response = $this->getMock('CakeResponse', array('send'));
1246
		$dispatcher->dispatch($request, $response);
1247
	}
1248
 
1249
/**
1250
 * Tests it is possible to attach single callables as filters
1251
 *
1252
 * @return void
1253
 */
1254
	public function testDispatcherFilterCallable() {
1255
		App::build(array(
1256
			'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
1257
		), App::RESET);
1258
 
1259
		$dispatcher = new TestDispatcher();
1260
		Configure::write('Dispatcher.filters', array(
1261
			array('callable' => array($dispatcher, 'filterTest'), 'on' => 'before')
1262
		));
1263
 
1264
		$request = new CakeRequest('/');
1265
		$response = $this->getMock('CakeResponse', array('send'));
1266
		$dispatcher->dispatch($request, $response);
1267
		$this->assertEquals('Dispatcher.beforeDispatch', $request->params['eventName']);
1268
 
1269
		$dispatcher = new TestDispatcher();
1270
		Configure::write('Dispatcher.filters', array(
1271
			array('callable' => array($dispatcher, 'filterTest'), 'on' => 'after')
1272
		));
1273
 
1274
		$request = new CakeRequest('/');
1275
		$response = $this->getMock('CakeResponse', array('send'));
1276
		$dispatcher->dispatch($request, $response);
1277
		$this->assertEquals('Dispatcher.afterDispatch', $request->params['eventName']);
1278
 
1279
		// Test that it is possible to skip the route connection process
1280
		$dispatcher = new TestDispatcher();
1281
		Configure::write('Dispatcher.filters', array(
1282
			array('callable' => array($dispatcher, 'filterTest2'), 'on' => 'before', 'priority' => 1)
1283
		));
1284
 
1285
		$request = new CakeRequest('/');
1286
		$response = $this->getMock('CakeResponse', array('send'));
1287
		$dispatcher->dispatch($request, $response);
1288
		$this->assertEmpty($dispatcher->controller);
1289
		$expected = array('controller' => null, 'action' => null, 'plugin' => null, 'named' => array(), 'pass' => array());
1290
		$this->assertEquals($expected, $request->params);
1291
 
1292
		$dispatcher = new TestDispatcher();
1293
		Configure::write('Dispatcher.filters', array(
1294
			array('callable' => array($dispatcher, 'filterTest2'), 'on' => 'before', 'priority' => 1)
1295
		));
1296
 
1297
		$request = new CakeRequest('/');
1298
		$request->params['return'] = true;
1299
		$response = $this->getMock('CakeResponse', array('send'));
1300
		$response->body('this is a body');
1301
		$result = $dispatcher->dispatch($request, $response);
1302
		$this->assertEquals('this is a body', $result);
1303
 
1304
		$request = new CakeRequest('/');
1305
		$response = $this->getMock('CakeResponse', array('send'));
1306
		$response->expects($this->once())->method('send');
1307
		$response->body('this is a body');
1308
		$result = $dispatcher->dispatch($request, $response);
1309
		$this->assertNull($result);
1310
	}
1311
 
1312
/**
1313
 * testChangingParamsFromBeforeFilter method
1314
 *
1315
 * @return void
1316
 */
1317
	public function testChangingParamsFromBeforeFilter() {
1318
		$Dispatcher = new TestDispatcher();
1319
		$response = $this->getMock('CakeResponse');
1320
		$url = new CakeRequest('some_posts/index/param:value/param2:value2');
1321
 
1322
		try {
1323
			$Dispatcher->dispatch($url, $response, array('return' => 1));
1324
			$this->fail('No exception.');
1325
		} catch (MissingActionException $e) {
1326
			$this->assertEquals('Action SomePostsController::view() could not be found.', $e->getMessage());
1327
		}
1328
 
1329
		$url = new CakeRequest('some_posts/something_else/param:value/param2:value2');
1330
		$Dispatcher->dispatch($url, $response, array('return' => 1));
1331
 
1332
		$expected = 'SomePosts';
1333
		$this->assertEquals($expected, $Dispatcher->controller->name);
1334
 
1335
		$expected = 'change';
1336
		$this->assertEquals($expected, $Dispatcher->controller->action);
1337
 
1338
		$expected = array('changed');
1339
		$this->assertSame($expected, $Dispatcher->controller->params['pass']);
1340
	}
1341
 
1342
/**
1343
 * testStaticAssets method
1344
 *
1345
 * @return void
1346
 */
1347
	public function testAssets() {
1348
		Router::reload();
1349
 
1350
		App::build(array(
1351
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
1352
			'Vendor' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Vendor' . DS),
1353
			'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
1354
		));
1355
		CakePlugin::load(array('TestPlugin', 'TestPluginTwo'));
1356
		Configure::write('Dispatcher.filters', array('AssetDispatcher'));
1357
 
1358
		$Dispatcher = new TestDispatcher();
1359
		$response = $this->getMock('CakeResponse', array('_sendHeader'));
1360
 
1361
		try {
1362
			$Dispatcher->dispatch(new CakeRequest('theme/test_theme/../webroot/css/test_asset.css'), $response);
1363
			$this->fail('No exception');
1364
		} catch (MissingControllerException $e) {
1365
			$this->assertEquals('Controller class ThemeController could not be found.', $e->getMessage());
1366
		}
1367
 
1368
		try {
1369
			$Dispatcher->dispatch(new CakeRequest('theme/test_theme/pdfs'), $response);
1370
			$this->fail('No exception');
1371
		} catch (MissingControllerException $e) {
1372
			$this->assertEquals('Controller class ThemeController could not be found.', $e->getMessage());
1373
		}
1374
	}
1375
 
1376
/**
1377
 * Data provider for asset filter
1378
 *
1379
 * - theme assets.
1380
 * - plugin assets.
1381
 * - plugin assets in sub directories.
1382
 * - unknown plugin assets.
1383
 *
1384
 * @return array
1385
 */
1386
	public static function assetProvider() {
1387
		return array(
1388
			array(
1389
				'theme/test_theme/flash/theme_test.swf',
1390
				'View/Themed/TestTheme/webroot/flash/theme_test.swf'
1391
			),
1392
			array(
1393
				'theme/test_theme/pdfs/theme_test.pdf',
1394
				'View/Themed/TestTheme/webroot/pdfs/theme_test.pdf'
1395
			),
1396
			array(
1397
				'theme/test_theme/img/test.jpg',
1398
				'View/Themed/TestTheme/webroot/img/test.jpg'
1399
			),
1400
			array(
1401
				'theme/test_theme/css/test_asset.css',
1402
				'View/Themed/TestTheme/webroot/css/test_asset.css'
1403
			),
1404
			array(
1405
				'theme/test_theme/js/theme.js',
1406
				'View/Themed/TestTheme/webroot/js/theme.js'
1407
			),
1408
			array(
1409
				'theme/test_theme/js/one/theme_one.js',
1410
				'View/Themed/TestTheme/webroot/js/one/theme_one.js'
1411
			),
1412
			array(
1413
				'theme/test_theme/space%20image.text',
1414
				'View/Themed/TestTheme/webroot/space image.text'
1415
			),
1416
			array(
1417
				'test_plugin/root.js',
1418
				'Plugin/TestPlugin/webroot/root.js'
1419
			),
1420
			array(
1421
				'test_plugin/flash/plugin_test.swf',
1422
				'Plugin/TestPlugin/webroot/flash/plugin_test.swf'
1423
			),
1424
			array(
1425
				'test_plugin/pdfs/plugin_test.pdf',
1426
				'Plugin/TestPlugin/webroot/pdfs/plugin_test.pdf'
1427
			),
1428
			array(
1429
				'test_plugin/js/test_plugin/test.js',
1430
				'Plugin/TestPlugin/webroot/js/test_plugin/test.js'
1431
			),
1432
			array(
1433
				'test_plugin/css/test_plugin_asset.css',
1434
				'Plugin/TestPlugin/webroot/css/test_plugin_asset.css'
1435
			),
1436
			array(
1437
				'test_plugin/img/cake.icon.gif',
1438
				'Plugin/TestPlugin/webroot/img/cake.icon.gif'
1439
			),
1440
			array(
1441
				'plugin_js/js/plugin_js.js',
1442
				'Plugin/PluginJs/webroot/js/plugin_js.js'
1443
			),
1444
			array(
1445
				'plugin_js/js/one/plugin_one.js',
1446
				'Plugin/PluginJs/webroot/js/one/plugin_one.js'
1447
			),
1448
			array(
1449
				'test_plugin/css/unknown.extension',
1450
				'Plugin/TestPlugin/webroot/css/unknown.extension'
1451
			),
1452
			array(
1453
				'test_plugin/css/theme_one.htc',
1454
				'Plugin/TestPlugin/webroot/css/theme_one.htc'
1455
			),
1456
		);
1457
	}
1458
 
1459
/**
1460
 * Test assets
1461
 *
1462
 * @dataProvider assetProvider
1463
 * @outputBuffering enabled
1464
 * @return void
1465
 */
1466
	public function testAsset($url, $file) {
1467
		Router::reload();
1468
 
1469
		App::build(array(
1470
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
1471
			'Vendor' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Vendor' . DS),
1472
			'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
1473
		));
1474
		CakePlugin::load(array('TestPlugin', 'PluginJs'));
1475
		Configure::write('Dispatcher.filters', array('AssetDispatcher'));
1476
 
1477
		$Dispatcher = new TestDispatcher();
1478
		$response = $this->getMock('CakeResponse', array('_sendHeader'));
1479
 
1480
		$Dispatcher->dispatch(new CakeRequest($url), $response);
1481
		$result = ob_get_clean();
1482
 
1483
		$path = CAKE . 'Test' . DS . 'test_app' . DS . str_replace('/', DS, $file);
1484
		$file = file_get_contents($path);
1485
		$this->assertEquals($file, $result);
1486
 
1487
		$expected = filesize($path);
1488
		$headers = $response->header();
1489
		$this->assertEquals($expected, $headers['Content-Length']);
1490
	}
1491
 
1492
/**
1493
 * test that missing asset processors trigger a 404 with no response body.
1494
 *
1495
 * @return void
1496
 */
1497
	public function testMissingAssetProcessor404() {
1498
		$response = $this->getMock('CakeResponse', array('send'));
1499
		$Dispatcher = new TestDispatcher();
1500
		Configure::write('Asset.filter', array(
1501
			'js' => '',
1502
			'css' => null
1503
		));
1504
		Configure::write('Dispatcher.filters', array('AssetDispatcher'));
1505
 
1506
		$request = new CakeRequest('ccss/cake.generic.css');
1507
		$Dispatcher->dispatch($request, $response);
1508
		$this->assertEquals('404', $response->statusCode());
1509
	}
1510
 
1511
/**
1512
 * Data provider for cached actions.
1513
 *
1514
 * - Test simple views
1515
 * - Test views with nocache tags
1516
 * - Test requests with named + passed params.
1517
 * - Test requests with query string params
1518
 * - Test themed views.
1519
 *
1520
 * @return array
1521
 */
1522
	public static function cacheActionProvider() {
1523
		return array(
1524
			array('/'),
1525
			array('test_cached_pages/index'),
1526
			array('TestCachedPages/index'),
1527
			array('test_cached_pages/test_nocache_tags'),
1528
			array('TestCachedPages/test_nocache_tags'),
1529
			array('test_cached_pages/view/param/param'),
1530
			array('test_cached_pages/view/foo:bar/value:goo'),
1531
			array('test_cached_pages/view?q=cakephp'),
1532
			array('test_cached_pages/themed'),
1533
		);
1534
	}
1535
 
1536
/**
1537
 * testFullPageCachingDispatch method
1538
 *
1539
 * @dataProvider cacheActionProvider
1540
 * @return void
1541
 */
1542
	public function testFullPageCachingDispatch($url) {
1543
		Configure::write('Cache.disable', false);
1544
		Configure::write('Cache.check', true);
1545
		Configure::write('debug', 2);
1546
 
1547
		Router::reload();
1548
		Router::connect('/', array('controller' => 'test_cached_pages', 'action' => 'index'));
1549
		Router::connect('/:controller/:action/*');
1550
 
1551
		App::build(array(
1552
			'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS),
1553
		), App::RESET);
1554
 
1555
		$dispatcher = new TestDispatcher();
1556
		$request = new CakeRequest($url);
1557
		$response = $this->getMock('CakeResponse', array('send'));
1558
 
1559
		$dispatcher->dispatch($request, $response);
1560
		$out = $response->body();
1561
 
1562
		Configure::write('Dispatcher.filters', array('CacheDispatcher'));
1563
		$request = new CakeRequest($url);
1564
		$response = $this->getMock('CakeResponse', array('send'));
1565
		$dispatcher = new TestDispatcher();
1566
		$dispatcher->dispatch($request, $response);
1567
		$cached = $response->body();
1568
 
1569
		$cached = preg_replace('/<!--+[^<>]+-->/', '', $cached);
1570
 
1571
		$this->assertTextEquals($out, $cached);
1572
 
1573
		$filename = $this->_cachePath($request->here());
1574
		unlink($filename);
1575
	}
1576
 
1577
/**
1578
 * testHttpMethodOverrides method
1579
 *
1580
 * @return void
1581
 */
1582
	public function testHttpMethodOverrides() {
1583
		Router::reload();
1584
		Router::mapResources('Posts');
1585
 
1586
		$_SERVER['REQUEST_METHOD'] = 'POST';
1587
		$dispatcher = new Dispatcher();
1588
 
1589
		$request = new CakeRequest('/posts');
1590
		$event = new CakeEvent('DispatcherTest', $dispatcher, array('request' => $request));
1591
		$dispatcher->parseParams($event);
1592
		$expected = array('pass' => array(), 'named' => array(), 'plugin' => null, 'controller' => 'posts', 'action' => 'add', '[method]' => 'POST');
1593
		foreach ($expected as $key => $value) {
1594
			$this->assertEquals($value, $request[$key], 'Value mismatch for ' . $key . ' %s');
1595
		}
1596
 
1597
		$_SERVER['REQUEST_METHOD'] = 'GET';
1598
		$_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] = 'PUT';
1599
 
1600
		$request = new CakeRequest('/posts/5');
1601
		$event = new CakeEvent('DispatcherTest', $dispatcher, array('request' => $request));
1602
		$dispatcher->parseParams($event);
1603
		$expected = array(
1604
			'pass' => array('5'),
1605
			'named' => array(),
1606
			'id' => '5',
1607
			'plugin' => null,
1608
			'controller' => 'posts',
1609
			'action' => 'edit',
1610
			'[method]' => 'PUT'
1611
		);
1612
		foreach ($expected as $key => $value) {
1613
			$this->assertEquals($value, $request[$key], 'Value mismatch for ' . $key . ' %s');
1614
		}
1615
 
1616
		unset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']);
1617
		$_SERVER['REQUEST_METHOD'] = 'GET';
1618
 
1619
		$request = new CakeRequest('/posts/5');
1620
		$event = new CakeEvent('DispatcherTest', $dispatcher, array('request' => $request));
1621
		$dispatcher->parseParams($event);
1622
		$expected = array('pass' => array('5'), 'named' => array(), 'id' => '5', 'plugin' => null, 'controller' => 'posts', 'action' => 'view', '[method]' => 'GET');
1623
		foreach ($expected as $key => $value) {
1624
			$this->assertEquals($value, $request[$key], 'Value mismatch for ' . $key . ' %s');
1625
		}
1626
 
1627
		$_POST['_method'] = 'PUT';
1628
 
1629
		$request = new CakeRequest('/posts/5');
1630
		$event = new CakeEvent('DispatcherTest', $dispatcher, array('request' => $request));
1631
		$dispatcher->parseParams($event);
1632
		$expected = array('pass' => array('5'), 'named' => array(), 'id' => '5', 'plugin' => null, 'controller' => 'posts', 'action' => 'edit', '[method]' => 'PUT');
1633
		foreach ($expected as $key => $value) {
1634
			$this->assertEquals($value, $request[$key], 'Value mismatch for ' . $key . ' %s');
1635
		}
1636
 
1637
		$_POST['_method'] = 'POST';
1638
		$_POST['data'] = array('Post' => array('title' => 'New Post'));
1639
		$_POST['extra'] = 'data';
1640
		$_SERVER = array();
1641
 
1642
		$request = new CakeRequest('/posts');
1643
		$event = new CakeEvent('DispatcherTest', $dispatcher, array('request' => $request));
1644
		$dispatcher->parseParams($event);
1645
		$expected = array(
1646
			'pass' => array(), 'named' => array(), 'plugin' => null, 'controller' => 'posts', 'action' => 'add',
1647
			'[method]' => 'POST', 'data' => array('extra' => 'data', 'Post' => array('title' => 'New Post')),
1648
		);
1649
		foreach ($expected as $key => $value) {
1650
			$this->assertEquals($value, $request[$key], 'Value mismatch for ' . $key . ' %s');
1651
		}
1652
 
1653
		unset($_POST['_method']);
1654
	}
1655
 
1656
/**
1657
 * cachePath method
1658
 *
1659
 * @param string $here
1660
 * @return string
1661
 */
1662
	protected function _cachePath($here) {
1663
		$path = $here;
1664
		if ($here === '/') {
1665
			$path = 'home';
1666
		}
1667
		$path = strtolower(Inflector::slug($path));
1668
 
1669
		$filename = CACHE . 'views' . DS . $path . '.php';
1670
 
1671
		if (!file_exists($filename)) {
1672
			$filename = CACHE . 'views' . DS . $path . '_index.php';
1673
		}
1674
		return $filename;
1675
	}
1676
}