~tsep-dev/tsep/0.9-beta

« back to all changes in this revision

Viewing changes to branches/symfony/cake/tests/cases/libs/controller/controller.test.php

  • Committer: geoffreyfishing
  • Date: 2011-01-11 23:46:12 UTC
  • Revision ID: svn-v4:ae0de26e-ed09-4cbe-9a20-e40b4c60ac6c::125
Created a symfony branch for future migration to symfony

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * ControllerTest file
 
4
 *
 
5
 * PHP versions 4 and 5
 
6
 *
 
7
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 
8
 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
 
9
 *
 
10
 * Licensed under The MIT License
 
11
 * Redistributions of files must retain the above copyright notice.
 
12
 *
 
13
 * @copyright     Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
 
14
 * @link          http://cakephp.org CakePHP Project
 
15
 * @package       cake
 
16
 * @subpackage    cake.tests.cases.libs.controller
 
17
 * @since         CakePHP(tm) v 1.2.0.5436
 
18
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 
19
 */
 
20
App::import('Controller', 'Controller', false);
 
21
App::import('Component', 'Security');
 
22
App::import('Component', 'Cookie');
 
23
 
 
24
/**
 
25
 * AppController class
 
26
 *
 
27
 * @package       cake
 
28
 * @subpackage    cake.tests.cases.libs.controller
 
29
 */
 
30
if (!class_exists('AppController')) {
 
31
        /**
 
32
         * AppController class
 
33
         *
 
34
         * @package       cake
 
35
         * @subpackage    cake.tests.cases.libs.controller
 
36
         */
 
37
        class AppController extends Controller {
 
38
        /**
 
39
         * helpers property
 
40
         *
 
41
         * @var array
 
42
         * @access public
 
43
         */
 
44
                var $helpers = array('Html', 'Javascript');
 
45
        /**
 
46
         * uses property
 
47
         *
 
48
         * @var array
 
49
         * @access public
 
50
         */
 
51
                var $uses = array('ControllerPost');
 
52
        /**
 
53
         * components property
 
54
         *
 
55
         * @var array
 
56
         * @access public
 
57
         */
 
58
                var $components = array('Cookie');
 
59
        }
 
60
} elseif (!defined('APP_CONTROLLER_EXISTS')) {
 
61
        define('APP_CONTROLLER_EXISTS', true);
 
62
}
 
63
 
 
64
/**
 
65
 * ControllerPost class
 
66
 *
 
67
 * @package       cake
 
68
 * @subpackage    cake.tests.cases.libs.controller
 
69
 */
 
70
class ControllerPost extends CakeTestModel {
 
71
 
 
72
/**
 
73
 * name property
 
74
 *
 
75
 * @var string 'ControllerPost'
 
76
 * @access public
 
77
 */
 
78
        var $name = 'ControllerPost';
 
79
 
 
80
/**
 
81
 * useTable property
 
82
 *
 
83
 * @var string 'posts'
 
84
 * @access public
 
85
 */
 
86
        var $useTable = 'posts';
 
87
 
 
88
/**
 
89
 * invalidFields property
 
90
 *
 
91
 * @var array
 
92
 * @access public
 
93
 */
 
94
        var $invalidFields = array('name' => 'error_msg');
 
95
 
 
96
/**
 
97
 * lastQuery property
 
98
 *
 
99
 * @var mixed null
 
100
 * @access public
 
101
 */
 
102
        var $lastQuery = null;
 
103
 
 
104
/**
 
105
 * beforeFind method
 
106
 *
 
107
 * @param mixed $query
 
108
 * @access public
 
109
 * @return void
 
110
 */
 
111
        function beforeFind($query) {
 
112
                $this->lastQuery = $query;
 
113
        }
 
114
 
 
115
/**
 
116
 * find method
 
117
 *
 
118
 * @param mixed $type
 
119
 * @param array $options
 
120
 * @access public
 
121
 * @return void
 
122
 */
 
123
        function find($type, $options = array()) {
 
124
                if ($type == 'popular') {
 
125
                        $conditions = array($this->name . '.' . $this->primaryKey .' > ' => '1');
 
126
                        $options = Set::merge($options, compact('conditions'));
 
127
                        return parent::find('all', $options);
 
128
                }
 
129
                return parent::find($type, $options);
 
130
        }
 
131
}
 
132
 
 
133
/**
 
134
 * ControllerPostsController class
 
135
 *
 
136
 * @package       cake
 
137
 * @subpackage    cake.tests.cases.libs.controller
 
138
 */
 
139
class ControllerCommentsController extends AppController {
 
140
 
 
141
/**
 
142
 * name property
 
143
 *
 
144
 * @var string 'ControllerPost'
 
145
 * @access public
 
146
 */
 
147
        var $name = 'ControllerComments';
 
148
}
 
149
 
 
150
/**
 
151
 * ControllerComment class
 
152
 *
 
153
 * @package       cake
 
154
 * @subpackage    cake.tests.cases.libs.controller
 
155
 */
 
156
class ControllerComment extends CakeTestModel {
 
157
 
 
158
/**
 
159
 * name property
 
160
 *
 
161
 * @var string 'ControllerComment'
 
162
 * @access public
 
163
 */
 
164
        var $name = 'Comment';
 
165
 
 
166
/**
 
167
 * useTable property
 
168
 *
 
169
 * @var string 'comments'
 
170
 * @access public
 
171
 */
 
172
        var $useTable = 'comments';
 
173
 
 
174
/**
 
175
 * data property
 
176
 *
 
177
 * @var array
 
178
 * @access public
 
179
 */
 
180
        var $data = array('name' => 'Some Name');
 
181
 
 
182
/**
 
183
 * alias property
 
184
 *
 
185
 * @var string 'ControllerComment'
 
186
 * @access public
 
187
 */
 
188
        var $alias = 'ControllerComment';
 
189
}
 
190
 
 
191
/**
 
192
 * ControllerAlias class
 
193
 *
 
194
 * @package       cake
 
195
 * @subpackage    cake.tests.cases.libs.controller
 
196
 */
 
197
class ControllerAlias extends CakeTestModel {
 
198
 
 
199
/**
 
200
 * name property
 
201
 *
 
202
 * @var string 'ControllerAlias'
 
203
 * @access public
 
204
 */
 
205
        var $name = 'ControllerAlias';
 
206
 
 
207
/**
 
208
 * alias property
 
209
 *
 
210
 * @var string 'ControllerSomeAlias'
 
211
 * @access public
 
212
 */
 
213
        var $alias = 'ControllerSomeAlias';
 
214
 
 
215
/**
 
216
 * useTable property
 
217
 *
 
218
 * @var string 'posts'
 
219
 * @access public
 
220
 */
 
221
        var $useTable = 'posts';
 
222
}
 
223
 
 
224
/**
 
225
 * ControllerPaginateModel class
 
226
 *
 
227
 * @package       cake
 
228
 * @subpackage    cake.tests.cases.libs.controller
 
229
 */
 
230
class ControllerPaginateModel extends CakeTestModel {
 
231
 
 
232
/**
 
233
 * name property
 
234
 *
 
235
 * @var string
 
236
 * @access public
 
237
 */
 
238
        var $name = 'ControllerPaginateModel';
 
239
 
 
240
/**
 
241
 * useTable property
 
242
 *
 
243
 * @var string'
 
244
 * @access public
 
245
 */
 
246
        var $useTable = 'comments';
 
247
 
 
248
/**
 
249
 * paginate method
 
250
 *
 
251
 * @return void
 
252
 * @access public
 
253
 */
 
254
        function paginate($conditions, $fields, $order, $limit, $page, $recursive, $extra) {
 
255
                $this->extra = $extra;
 
256
        }
 
257
 
 
258
/**
 
259
 * paginateCount
 
260
 *
 
261
 * @access public
 
262
 * @return void
 
263
 */
 
264
        function paginateCount($conditions, $recursive, $extra) {
 
265
                $this->extraCount = $extra;
 
266
        }
 
267
}
 
268
 
 
269
/**
 
270
 * NameTest class
 
271
 *
 
272
 * @package       cake
 
273
 * @subpackage    cake.tests.cases.libs.controller
 
274
 */
 
275
class NameTest extends CakeTestModel {
 
276
 
 
277
/**
 
278
 * name property
 
279
 * @var string 'Name'
 
280
 * @access public
 
281
 */
 
282
        var $name = 'Name';
 
283
 
 
284
/**
 
285
 * useTable property
 
286
 * @var string 'names'
 
287
 * @access public
 
288
 */
 
289
        var $useTable = 'comments';
 
290
 
 
291
/**
 
292
 * alias property
 
293
 *
 
294
 * @var string 'ControllerComment'
 
295
 * @access public
 
296
 */
 
297
        var $alias = 'Name';
 
298
}
 
299
 
 
300
/**
 
301
 * TestController class
 
302
 *
 
303
 * @package       cake
 
304
 * @subpackage    cake.tests.cases.libs.controller
 
305
 */
 
306
class TestController extends AppController {
 
307
 
 
308
/**
 
309
 * name property
 
310
 * @var string 'Name'
 
311
 * @access public
 
312
 */
 
313
        var $name = 'TestController';
 
314
 
 
315
/**
 
316
 * helpers property
 
317
 *
 
318
 * @var array
 
319
 * @access public
 
320
 */
 
321
        var $helpers = array('Session', 'Xml');
 
322
 
 
323
/**
 
324
 * components property
 
325
 *
 
326
 * @var array
 
327
 * @access public
 
328
 */
 
329
        var $components = array('Security');
 
330
 
 
331
/**
 
332
 * uses property
 
333
 *
 
334
 * @var array
 
335
 * @access public
 
336
 */
 
337
        var $uses = array('ControllerComment', 'ControllerAlias');
 
338
 
 
339
/**
 
340
 * index method
 
341
 *
 
342
 * @param mixed $testId
 
343
 * @param mixed $test2Id
 
344
 * @access public
 
345
 * @return void
 
346
 */
 
347
        function index($testId, $test2Id) {
 
348
                $this->data['testId'] = $testId;
 
349
                $this->data['test2Id'] = $test2Id;
 
350
        }
 
351
}
 
352
 
 
353
/**
 
354
 * TestComponent class
 
355
 *
 
356
 * @package       cake
 
357
 * @subpackage    cake.tests.cases.libs.controller
 
358
 */
 
359
class TestComponent extends Object {
 
360
 
 
361
/**
 
362
 * beforeRedirect method
 
363
 *
 
364
 * @access public
 
365
 * @return void
 
366
 */
 
367
        function beforeRedirect() {
 
368
        }
 
369
/**
 
370
 * initialize method
 
371
 *
 
372
 * @access public
 
373
 * @return void
 
374
 */
 
375
        function initialize(&$controller) {
 
376
        }
 
377
 
 
378
/**
 
379
 * startup method
 
380
 *
 
381
 * @access public
 
382
 * @return void
 
383
 */
 
384
        function startup(&$controller) {
 
385
        }
 
386
/**
 
387
 * shutdown method
 
388
 *
 
389
 * @access public
 
390
 * @return void
 
391
 */
 
392
        function shutdown(&$controller) {
 
393
        }
 
394
/**
 
395
 * beforeRender callback
 
396
 *
 
397
 * @return void
 
398
 */
 
399
        function beforeRender(&$controller) {
 
400
                if ($this->viewclass) {
 
401
                        $controller->view = $this->viewclass;
 
402
                }
 
403
        }
 
404
}
 
405
 
 
406
/**
 
407
 * AnotherTestController class
 
408
 *
 
409
 * @package       cake
 
410
 * @subpackage    cake.tests.cases.libs.controller
 
411
 */
 
412
class AnotherTestController extends AppController {
 
413
 
 
414
/**
 
415
 * name property
 
416
 * @var string 'Name'
 
417
 * @access public
 
418
 */
 
419
        var $name = 'AnotherTest';
 
420
/**
 
421
 * uses property
 
422
 *
 
423
 * @var array
 
424
 * @access public
 
425
 */
 
426
        var $uses = null;
 
427
}
 
428
 
 
429
/**
 
430
 * ControllerTest class
 
431
 *
 
432
 * @package       cake
 
433
 * @subpackage    cake.tests.cases.libs.controller
 
434
 */
 
435
class ControllerTest extends CakeTestCase {
 
436
 
 
437
/**
 
438
 * fixtures property
 
439
 *
 
440
 * @var array
 
441
 * @access public
 
442
 */
 
443
        var $fixtures = array('core.post', 'core.comment', 'core.name');
 
444
 
 
445
/**
 
446
 * endTest
 
447
 *
 
448
 * @access public
 
449
 * @return void
 
450
 */
 
451
        function endTest() {
 
452
                App::build();
 
453
        }
 
454
 
 
455
/**
 
456
 * testLoadModel method
 
457
 *
 
458
 * @access public
 
459
 * @return void
 
460
 */
 
461
        function testLoadModel() {
 
462
                $Controller =& new Controller();
 
463
 
 
464
                $this->assertFalse(isset($Controller->ControllerPost));
 
465
 
 
466
                $result = $Controller->loadModel('ControllerPost');
 
467
                $this->assertTrue($result);
 
468
                $this->assertTrue(is_a($Controller->ControllerPost, 'ControllerPost'));
 
469
                $this->assertTrue(in_array('ControllerPost', $Controller->modelNames));
 
470
 
 
471
                ClassRegistry::flush();
 
472
                unset($Controller);
 
473
        }
 
474
 
 
475
/**
 
476
 * testConstructClasses method
 
477
 *
 
478
 * @access public
 
479
 * @return void
 
480
 */
 
481
        function testConstructClasses() {
 
482
                $Controller =& new Controller();
 
483
                $Controller->modelClass = 'ControllerPost';
 
484
                $Controller->passedArgs[] = '1';
 
485
                $Controller->constructClasses();
 
486
                $this->assertEqual($Controller->ControllerPost->id, 1);
 
487
 
 
488
                unset($Controller);
 
489
 
 
490
                $Controller =& new Controller();
 
491
                $Controller->uses = array('ControllerPost', 'ControllerComment');
 
492
                $Controller->passedArgs[] = '1';
 
493
                $Controller->constructClasses();
 
494
                $this->assertTrue(is_a($Controller->ControllerPost, 'ControllerPost'));
 
495
                $this->assertTrue(is_a($Controller->ControllerComment, 'ControllerComment'));
 
496
 
 
497
                $this->assertEqual($Controller->ControllerComment->name, 'Comment');
 
498
 
 
499
                unset($Controller);
 
500
 
 
501
                App::build(array('plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)));
 
502
 
 
503
                $Controller =& new Controller();
 
504
                $Controller->uses = array('TestPlugin.TestPluginPost');
 
505
                $Controller->constructClasses();
 
506
 
 
507
                $this->assertEqual($Controller->modelClass, 'TestPluginPost');
 
508
                $this->assertTrue(isset($Controller->TestPluginPost));
 
509
                $this->assertTrue(is_a($Controller->TestPluginPost, 'TestPluginPost'));
 
510
 
 
511
                unset($Controller);
 
512
        }
 
513
 
 
514
/**
 
515
 * testAliasName method
 
516
 *
 
517
 * @access public
 
518
 * @return void
 
519
 */
 
520
        function testAliasName() {
 
521
                $Controller =& new Controller();
 
522
                $Controller->uses = array('NameTest');
 
523
                $Controller->constructClasses();
 
524
 
 
525
                $this->assertEqual($Controller->NameTest->name, 'Name');
 
526
                $this->assertEqual($Controller->NameTest->alias, 'Name');
 
527
 
 
528
                unset($Controller);
 
529
        }
 
530
 
 
531
/**
 
532
 * testPersistent method
 
533
 *
 
534
 * @access public
 
535
 * @return void
 
536
 */
 
537
        function testPersistent() {
 
538
                Configure::write('Cache.disable', false);
 
539
                $Controller =& new Controller();
 
540
                $Controller->modelClass = 'ControllerPost';
 
541
                $Controller->persistModel = true;
 
542
                $Controller->constructClasses();
 
543
                $this->assertTrue(file_exists(CACHE . 'persistent' . DS .'controllerpost.php'));
 
544
                $this->assertTrue(is_a($Controller->ControllerPost, 'ControllerPost'));
 
545
                @unlink(CACHE . 'persistent' . DS . 'controllerpost.php');
 
546
                @unlink(CACHE . 'persistent' . DS . 'controllerpostregistry.php');
 
547
 
 
548
                unset($Controller);
 
549
                Configure::write('Cache.disable', true);
 
550
        }
 
551
 
 
552
/**
 
553
 * testPaginate method
 
554
 *
 
555
 * @access public
 
556
 * @return void
 
557
 */
 
558
        function testPaginate() {
 
559
                $Controller =& new Controller();
 
560
                $Controller->uses = array('ControllerPost', 'ControllerComment');
 
561
                $Controller->passedArgs[] = '1';
 
562
                $Controller->params['url'] = array();
 
563
                $Controller->constructClasses();
 
564
 
 
565
                $results = Set::extract($Controller->paginate('ControllerPost'), '{n}.ControllerPost.id');
 
566
                $this->assertEqual($results, array(1, 2, 3));
 
567
 
 
568
                $results = Set::extract($Controller->paginate('ControllerComment'), '{n}.ControllerComment.id');
 
569
                $this->assertEqual($results, array(1, 2, 3, 4, 5, 6));
 
570
 
 
571
                $Controller->modelClass = null;
 
572
 
 
573
                $Controller->uses[0] = 'Plugin.ControllerPost';
 
574
                $results = Set::extract($Controller->paginate(), '{n}.ControllerPost.id');
 
575
                $this->assertEqual($results, array(1, 2, 3));
 
576
 
 
577
                $Controller->passedArgs = array('page' => '-1');
 
578
                $results = Set::extract($Controller->paginate('ControllerPost'), '{n}.ControllerPost.id');
 
579
                $this->assertEqual($Controller->params['paging']['ControllerPost']['page'], 1);
 
580
                $this->assertEqual($results, array(1, 2, 3));
 
581
 
 
582
                $Controller->passedArgs = array('sort' => 'ControllerPost.id', 'direction' => 'asc');
 
583
                $results = Set::extract($Controller->paginate('ControllerPost'), '{n}.ControllerPost.id');
 
584
                $this->assertEqual($Controller->params['paging']['ControllerPost']['page'], 1);
 
585
                $this->assertEqual($results, array(1, 2, 3));
 
586
 
 
587
                $Controller->passedArgs = array('sort' => 'ControllerPost.id', 'direction' => 'desc');
 
588
                $results = Set::extract($Controller->paginate('ControllerPost'), '{n}.ControllerPost.id');
 
589
                $this->assertEqual($Controller->params['paging']['ControllerPost']['page'], 1);
 
590
                $this->assertEqual($results, array(3, 2, 1));
 
591
 
 
592
                $Controller->passedArgs = array('sort' => 'id', 'direction' => 'desc');
 
593
                $results = Set::extract($Controller->paginate('ControllerPost'), '{n}.ControllerPost.id');
 
594
                $this->assertEqual($Controller->params['paging']['ControllerPost']['page'], 1);
 
595
                $this->assertEqual($results, array(3, 2, 1));
 
596
 
 
597
                $Controller->passedArgs = array('sort' => 'NotExisting.field', 'direction' => 'desc');
 
598
                $results = Set::extract($Controller->paginate('ControllerPost'), '{n}.ControllerPost.id');
 
599
                $this->assertEqual($Controller->params['paging']['ControllerPost']['page'], 1, 'Invalid field in query %s');
 
600
                $this->assertEqual($results, array(1, 2, 3));
 
601
 
 
602
                $Controller->passedArgs = array('sort' => 'ControllerPost.author_id', 'direction' => 'allYourBase');
 
603
                $results = Set::extract($Controller->paginate('ControllerPost'), '{n}.ControllerPost.id');
 
604
                $this->assertEqual($Controller->ControllerPost->lastQuery['order'][0], array('ControllerPost.author_id' => 'asc'));
 
605
                $this->assertEqual($results, array(1, 3, 2));
 
606
 
 
607
                $Controller->passedArgs = array('page' => '1 " onclick="alert(\'xss\');">');
 
608
                $Controller->paginate = array('limit' => 1);
 
609
                $Controller->paginate('ControllerPost');
 
610
                $this->assertIdentical($Controller->params['paging']['ControllerPost']['page'], 1, 'XSS exploit opened %s');
 
611
                $this->assertIdentical($Controller->params['paging']['ControllerPost']['options']['page'], 1, 'XSS exploit opened %s');
 
612
 
 
613
                $Controller->passedArgs = array();
 
614
                $Controller->paginate = array('limit' => 0);
 
615
                $Controller->paginate('ControllerPost');
 
616
                $this->assertIdentical($Controller->params['paging']['ControllerPost']['page'], 1);
 
617
                $this->assertIdentical($Controller->params['paging']['ControllerPost']['pageCount'], 3);
 
618
                $this->assertIdentical($Controller->params['paging']['ControllerPost']['prevPage'], false);
 
619
                $this->assertIdentical($Controller->params['paging']['ControllerPost']['nextPage'], true);
 
620
 
 
621
                $Controller->passedArgs = array();
 
622
                $Controller->paginate = array('limit' => 'garbage!');
 
623
                $Controller->paginate('ControllerPost');
 
624
                $this->assertIdentical($Controller->params['paging']['ControllerPost']['page'], 1);
 
625
                $this->assertIdentical($Controller->params['paging']['ControllerPost']['pageCount'], 3);
 
626
                $this->assertIdentical($Controller->params['paging']['ControllerPost']['prevPage'], false);
 
627
                $this->assertIdentical($Controller->params['paging']['ControllerPost']['nextPage'], true);
 
628
 
 
629
                $Controller->passedArgs = array();
 
630
                $Controller->paginate = array('limit' => '-1');
 
631
                $Controller->paginate('ControllerPost');
 
632
                $this->assertIdentical($Controller->params['paging']['ControllerPost']['page'], 1);
 
633
                $this->assertIdentical($Controller->params['paging']['ControllerPost']['pageCount'], 3);
 
634
                $this->assertIdentical($Controller->params['paging']['ControllerPost']['prevPage'], false);
 
635
                $this->assertIdentical($Controller->params['paging']['ControllerPost']['nextPage'], true);
 
636
        }
 
637
 
 
638
/**
 
639
 * testPaginateExtraParams method
 
640
 *
 
641
 * @access public
 
642
 * @return void
 
643
 */
 
644
        function testPaginateExtraParams() {
 
645
                $Controller =& new Controller();
 
646
                $Controller->uses = array('ControllerPost', 'ControllerComment');
 
647
                $Controller->passedArgs[] = '1';
 
648
                $Controller->params['url'] = array();
 
649
                $Controller->constructClasses();
 
650
 
 
651
                $Controller->passedArgs = array('page' => '-1', 'contain' => array('ControllerComment'));
 
652
                $result = $Controller->paginate('ControllerPost');
 
653
                $this->assertEqual($Controller->params['paging']['ControllerPost']['page'], 1);
 
654
                $this->assertEqual(Set::extract($result, '{n}.ControllerPost.id'), array(1, 2, 3));
 
655
                $this->assertTrue(!isset($Controller->ControllerPost->lastQuery['contain']));
 
656
 
 
657
                $Controller->passedArgs = array('page' => '-1');
 
658
                $Controller->paginate = array('ControllerPost' => array('contain' => array('ControllerComment')));
 
659
                $result = $Controller->paginate('ControllerPost');
 
660
                $this->assertEqual($Controller->params['paging']['ControllerPost']['page'], 1);
 
661
                $this->assertEqual(Set::extract($result, '{n}.ControllerPost.id'), array(1, 2, 3));
 
662
                $this->assertTrue(isset($Controller->ControllerPost->lastQuery['contain']));
 
663
 
 
664
                $Controller->paginate = array('ControllerPost' => array('popular', 'fields' => array('id', 'title')));
 
665
                $result = $Controller->paginate('ControllerPost');
 
666
                $this->assertEqual(Set::extract($result, '{n}.ControllerPost.id'), array(2, 3));
 
667
                $this->assertEqual($Controller->ControllerPost->lastQuery['conditions'], array('ControllerPost.id > ' => '1'));
 
668
 
 
669
                $Controller->passedArgs = array('limit' => 12);
 
670
                $Controller->paginate = array('limit' => 30);
 
671
                $result = $Controller->paginate('ControllerPost');
 
672
                $paging = $Controller->params['paging']['ControllerPost'];
 
673
 
 
674
                $this->assertEqual($Controller->ControllerPost->lastQuery['limit'], 12);
 
675
                $this->assertEqual($paging['options']['limit'], 12);
 
676
 
 
677
                $Controller =& new Controller();
 
678
                $Controller->uses = array('ControllerPaginateModel');
 
679
                $Controller->params['url'] = array();
 
680
                $Controller->constructClasses();
 
681
                $Controller->paginate = array(
 
682
                        'ControllerPaginateModel' => array('contain' => array('ControllerPaginateModel'), 'group' => 'Comment.author_id')
 
683
                );
 
684
                $result = $Controller->paginate('ControllerPaginateModel');
 
685
                $expected = array('contain' => array('ControllerPaginateModel'), 'group' => 'Comment.author_id');
 
686
                $this->assertEqual($Controller->ControllerPaginateModel->extra, $expected);
 
687
                $this->assertEqual($Controller->ControllerPaginateModel->extraCount, $expected);
 
688
 
 
689
                $Controller->paginate = array(
 
690
                        'ControllerPaginateModel' => array('foo', 'contain' => array('ControllerPaginateModel'), 'group' => 'Comment.author_id')
 
691
                );
 
692
                $Controller->paginate('ControllerPaginateModel');
 
693
                $expected = array('contain' => array('ControllerPaginateModel'), 'group' => 'Comment.author_id', 'type' => 'foo');
 
694
                $this->assertEqual($Controller->ControllerPaginateModel->extra, $expected);
 
695
                $this->assertEqual($Controller->ControllerPaginateModel->extraCount, $expected);
 
696
        }
 
697
 
 
698
/**
 
699
 * testPaginatePassedArgs method
 
700
 *
 
701
 * @return void
 
702
 * @access public
 
703
 */
 
704
        function testPaginatePassedArgs() {
 
705
                $Controller =& new Controller();
 
706
                $Controller->uses = array('ControllerPost');
 
707
                $Controller->passedArgs[] = array('1', '2', '3');
 
708
                $Controller->params['url'] = array();
 
709
                $Controller->constructClasses();
 
710
 
 
711
                $Controller->paginate = array(
 
712
                        'fields' => array(),
 
713
                        'order' => '',
 
714
                        'limit' => 5,
 
715
                        'page' => 1,
 
716
                        'recursive' => -1
 
717
                );
 
718
                $conditions = array();
 
719
                $Controller->paginate('ControllerPost',$conditions);
 
720
 
 
721
                $expected = array(
 
722
                        'fields' => array(),
 
723
                        'order' => '',
 
724
                        'limit' => 5,
 
725
                        'page' => 1,
 
726
                        'recursive' => -1,
 
727
                        'conditions' => array()
 
728
                );
 
729
                $this->assertEqual($Controller->params['paging']['ControllerPost']['options'],$expected);
 
730
        }
 
731
 
 
732
/**
 
733
 * Test that special paginate types are called and that the type param doesn't leak out into defaults or options.
 
734
 *
 
735
 * @return void
 
736
 */
 
737
        function testPaginateSpecialType() {
 
738
                $Controller =& new Controller();
 
739
                $Controller->uses = array('ControllerPost', 'ControllerComment');
 
740
                $Controller->passedArgs[] = '1';
 
741
                $Controller->params['url'] = array();
 
742
                $Controller->constructClasses();
 
743
 
 
744
                $Controller->paginate = array('ControllerPost' => array('popular', 'fields' => array('id', 'title')));
 
745
                $result = $Controller->paginate('ControllerPost');
 
746
 
 
747
                $this->assertEqual(Set::extract($result, '{n}.ControllerPost.id'), array(2, 3));
 
748
                $this->assertEqual($Controller->ControllerPost->lastQuery['conditions'], array('ControllerPost.id > ' => '1'));
 
749
                $this->assertFalse(isset($Controller->params['paging']['ControllerPost']['defaults'][0]));
 
750
                $this->assertFalse(isset($Controller->params['paging']['ControllerPost']['options'][0]));
 
751
        }
 
752
 
 
753
/**
 
754
 * testDefaultPaginateParams method
 
755
 *
 
756
 * @access public
 
757
 * @return void
 
758
 */
 
759
        function testDefaultPaginateParams() {
 
760
                $Controller =& new Controller();
 
761
                $Controller->modelClass = 'ControllerPost';
 
762
                $Controller->params['url'] = array();
 
763
                $Controller->paginate = array('order' => 'ControllerPost.id DESC');
 
764
                $Controller->constructClasses();
 
765
                $results = Set::extract($Controller->paginate('ControllerPost'), '{n}.ControllerPost.id');
 
766
                $this->assertEqual($Controller->params['paging']['ControllerPost']['defaults']['order'], 'ControllerPost.id DESC');
 
767
                $this->assertEqual($Controller->params['paging']['ControllerPost']['options']['order'], 'ControllerPost.id DESC');
 
768
                $this->assertEqual($results, array(3, 2, 1));
 
769
        }
 
770
 
 
771
/**
 
772
 * test paginate() and virtualField interactions
 
773
 *
 
774
 * @return void
 
775
 */
 
776
        function testPaginateOrderVirtualField() {
 
777
                $Controller =& new Controller();
 
778
                $Controller->uses = array('ControllerPost', 'ControllerComment');
 
779
                $Controller->params['url'] = array();
 
780
                $Controller->constructClasses();
 
781
                $Controller->ControllerPost->virtualFields = array(
 
782
                        'offset_test' => 'ControllerPost.id + 1'
 
783
                );
 
784
 
 
785
                $Controller->paginate = array(
 
786
                        'fields' => array('id', 'title', 'offset_test'),
 
787
                        'order' => array('offset_test' => 'DESC')
 
788
                );
 
789
                $result = $Controller->paginate('ControllerPost');
 
790
                $this->assertEqual(Set::extract($result, '{n}.ControllerPost.offset_test'), array(4, 3, 2));
 
791
 
 
792
                $Controller->passedArgs = array('sort' => 'offset_test', 'direction' => 'asc');
 
793
                $result = $Controller->paginate('ControllerPost');
 
794
                $this->assertEqual(Set::extract($result, '{n}.ControllerPost.offset_test'), array(2, 3, 4));
 
795
        }
 
796
 
 
797
/**
 
798
 * testFlash method
 
799
 *
 
800
 * @access public
 
801
 * @return void
 
802
 */
 
803
        function testFlash() {
 
804
                $Controller =& new Controller();
 
805
                $Controller->flash('this should work', '/flash');
 
806
                $result = $Controller->output;
 
807
 
 
808
                $expected = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
809
                <html xmlns="http://www.w3.org/1999/xhtml">
 
810
                <head>
 
811
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 
812
                <title>this should work</title>
 
813
                <style><!--
 
814
                P { text-align:center; font:bold 1.1em sans-serif }
 
815
                A { color:#444; text-decoration:none }
 
816
                A:HOVER { text-decoration: underline; color:#44E }
 
817
                --></style>
 
818
                </head>
 
819
                <body>
 
820
                <p><a href="/flash">this should work</a></p>
 
821
                </body>
 
822
                </html>';
 
823
                $result = str_replace(array("\t", "\r\n", "\n"), "", $result);
 
824
                $expected =  str_replace(array("\t", "\r\n", "\n"), "", $expected);
 
825
                $this->assertEqual($result, $expected);
 
826
 
 
827
                App::build(array('views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS)));
 
828
                $Controller =& new Controller();
 
829
                $Controller->flash('this should work', '/flash', 1, 'ajax2');
 
830
                $result = $Controller->output;
 
831
                $this->assertPattern('/Ajax!/', $result);
 
832
                App::build();
 
833
        }
 
834
 
 
835
/**
 
836
 * testControllerSet method
 
837
 *
 
838
 * @access public
 
839
 * @return void
 
840
 */
 
841
        function testControllerSet() {
 
842
                $Controller =& new Controller();
 
843
                $Controller->set('variable_with_underscores', null);
 
844
                $this->assertTrue(array_key_exists('variable_with_underscores', $Controller->viewVars));
 
845
 
 
846
                $Controller->viewVars = array();
 
847
                $viewVars = array('ModelName' => array('id' => 1, 'name' => 'value'));
 
848
                $Controller->set($viewVars);
 
849
                $this->assertTrue(array_key_exists('ModelName', $Controller->viewVars));
 
850
 
 
851
                $Controller->viewVars = array();
 
852
                $Controller->set('variable_with_underscores', 'value');
 
853
                $this->assertTrue(array_key_exists('variable_with_underscores', $Controller->viewVars));
 
854
 
 
855
                $Controller->viewVars = array();
 
856
                $viewVars = array('ModelName' => 'name');
 
857
                $Controller->set($viewVars);
 
858
                $this->assertTrue(array_key_exists('ModelName', $Controller->viewVars));
 
859
 
 
860
                $Controller->set('title', 'someTitle');
 
861
                $this->assertIdentical($Controller->viewVars['title'], 'someTitle');
 
862
                $this->assertTrue(empty($Controller->pageTitle));
 
863
 
 
864
                $Controller->viewVars = array();
 
865
                $expected = array('ModelName' => 'name', 'ModelName2' => 'name2');
 
866
                $Controller->set(array('ModelName', 'ModelName2'), array('name', 'name2'));
 
867
                $this->assertIdentical($Controller->viewVars, $expected);
 
868
 
 
869
                $Controller->viewVars = array();
 
870
                $Controller->set(array(3 => 'three', 4 => 'four'));
 
871
                $Controller->set(array(1 => 'one', 2 => 'two'));
 
872
                $expected = array(3 => 'three', 4 => 'four', 1 => 'one', 2 => 'two');
 
873
                $this->assertEqual($Controller->viewVars, $expected);
 
874
                
 
875
        }
 
876
 
 
877
/**
 
878
 * testRender method
 
879
 *
 
880
 * @access public
 
881
 * @return void
 
882
 */
 
883
        function testRender() {
 
884
                App::build(array(
 
885
                        'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS)
 
886
                ), true);
 
887
 
 
888
                $Controller =& new Controller();
 
889
                $Controller->viewPath = 'posts';
 
890
 
 
891
                $result = $Controller->render('index');
 
892
                $this->assertPattern('/posts index/', $result);
 
893
 
 
894
                $result = $Controller->render('/elements/test_element');
 
895
                $this->assertPattern('/this is the test element/', $result);
 
896
 
 
897
                $Controller = new TestController();
 
898
                $Controller->constructClasses();
 
899
                $Controller->ControllerComment->validationErrors = array('title' => 'tooShort');
 
900
                $expected = $Controller->ControllerComment->validationErrors;
 
901
 
 
902
                ClassRegistry::flush();
 
903
                $Controller->viewPath = 'posts';
 
904
                $result = $Controller->render('index');
 
905
                $View = ClassRegistry::getObject('view');
 
906
                $this->assertTrue(isset($View->validationErrors['ControllerComment']));
 
907
                $this->assertEqual($expected, $View->validationErrors['ControllerComment']);
 
908
 
 
909
                $Controller->ControllerComment->validationErrors = array();
 
910
                ClassRegistry::flush();
 
911
                App::build();
 
912
        }
 
913
 
 
914
/**
 
915
 * test that a component beforeRender can change the controller view class.
 
916
 *
 
917
 * @return void
 
918
 */
 
919
        function testComponentBeforeRenderChangingViewClass() {
 
920
                $core = App::core('views');
 
921
                App::build(array(
 
922
                        'views' => array(
 
923
                                TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS,
 
924
                                $core[0]
 
925
                        )
 
926
                ), true);
 
927
                $Controller =& new Controller();
 
928
                $Controller->uses = array();
 
929
                $Controller->components = array('Test');
 
930
                $Controller->constructClasses();
 
931
                $Controller->Test->viewclass = 'Theme';
 
932
                $Controller->viewPath = 'posts';
 
933
                $Controller->theme = 'test_theme';
 
934
                $result = $Controller->render('index');
 
935
                $this->assertPattern('/default test_theme layout/', $result);
 
936
                App::build();
 
937
        }
 
938
 
 
939
/**
 
940
 * testToBeInheritedGuardmethods method
 
941
 *
 
942
 * @access public
 
943
 * @return void
 
944
 */
 
945
        function testToBeInheritedGuardmethods() {
 
946
                $Controller =& new Controller();
 
947
                $this->assertTrue($Controller->_beforeScaffold(''));
 
948
                $this->assertTrue($Controller->_afterScaffoldSave(''));
 
949
                $this->assertTrue($Controller->_afterScaffoldSaveError(''));
 
950
                $this->assertFalse($Controller->_scaffoldError(''));
 
951
        }
 
952
 
 
953
/**
 
954
 * testRedirect method
 
955
 *
 
956
 * @access public
 
957
 * @return void
 
958
 */
 
959
        function testRedirect() {
 
960
                $codes = array(
 
961
                        100 => "Continue",
 
962
                        101 => "Switching Protocols",
 
963
                        200 => "OK",
 
964
                        201 => "Created",
 
965
                        202 => "Accepted",
 
966
                        203 => "Non-Authoritative Information",
 
967
                        204 => "No Content",
 
968
                        205 => "Reset Content",
 
969
                        206 => "Partial Content",
 
970
                        300 => "Multiple Choices",
 
971
                        301 => "Moved Permanently",
 
972
                        302 => "Found",
 
973
                        303 => "See Other",
 
974
                        304 => "Not Modified",
 
975
                        305 => "Use Proxy",
 
976
                        307 => "Temporary Redirect",
 
977
                        400 => "Bad Request",
 
978
                        401 => "Unauthorized",
 
979
                        402 => "Payment Required",
 
980
                        403 => "Forbidden",
 
981
                        404 => "Not Found",
 
982
                        405 => "Method Not Allowed",
 
983
                        406 => "Not Acceptable",
 
984
                        407 => "Proxy Authentication Required",
 
985
                        408 => "Request Time-out",
 
986
                        409 => "Conflict",
 
987
                        410 => "Gone",
 
988
                        411 => "Length Required",
 
989
                        412 => "Precondition Failed",
 
990
                        413 => "Request Entity Too Large",
 
991
                        414 => "Request-URI Too Large",
 
992
                        415 => "Unsupported Media Type",
 
993
                        416 => "Requested range not satisfiable",
 
994
                        417 => "Expectation Failed",
 
995
                        500 => "Internal Server Error",
 
996
                        501 => "Not Implemented",
 
997
                        502 => "Bad Gateway",
 
998
                        503 => "Service Unavailable",
 
999
                        504 => "Gateway Time-out"
 
1000
                );
 
1001
 
 
1002
                Mock::generatePartial('Controller', 'MockController', array('header'));
 
1003
                Mock::generate('TestComponent', 'MockTestComponent');
 
1004
                Mock::generate('TestComponent', 'MockTestBComponent');
 
1005
 
 
1006
                App::import('Helper', 'Cache');
 
1007
 
 
1008
                foreach ($codes as $code => $msg) {
 
1009
                        $MockController =& new MockController();
 
1010
                        $MockController->Component =& new Component();
 
1011
                        $MockController->Component->init($MockController);
 
1012
                        $MockController->expectAt(0, 'header', array("HTTP/1.1 {$code} {$msg}"));
 
1013
                        $MockController->expectAt(1, 'header', array('Location: http://cakephp.org'));
 
1014
                        $MockController->expectCallCount('header', 2);
 
1015
                        $MockController->redirect('http://cakephp.org', (int)$code, false);
 
1016
                        $this->assertFalse($MockController->autoRender);
 
1017
                }
 
1018
                foreach ($codes as $code => $msg) {
 
1019
                        $MockController =& new MockController();
 
1020
                        $MockController->Component =& new Component();
 
1021
                        $MockController->Component->init($MockController);
 
1022
                        $MockController->expectAt(0, 'header', array("HTTP/1.1 {$code} {$msg}"));
 
1023
                        $MockController->expectAt(1, 'header', array('Location: http://cakephp.org'));
 
1024
                        $MockController->expectCallCount('header', 2);
 
1025
                        $MockController->redirect('http://cakephp.org', $msg, false);
 
1026
                        $this->assertFalse($MockController->autoRender);
 
1027
                }
 
1028
 
 
1029
                $MockController =& new MockController();
 
1030
                $MockController->Component =& new Component();
 
1031
                $MockController->Component->init($MockController);
 
1032
                $MockController->expectAt(0, 'header', array('Location: http://www.example.org/users/login'));
 
1033
                $MockController->expectCallCount('header', 1);
 
1034
                $MockController->redirect('http://www.example.org/users/login', null, false);
 
1035
 
 
1036
                $MockController =& new MockController();
 
1037
                $MockController->Component =& new Component();
 
1038
                $MockController->Component->init($MockController);
 
1039
                $MockController->expectAt(0, 'header', array('HTTP/1.1 301 Moved Permanently'));
 
1040
                $MockController->expectAt(1, 'header', array('Location: http://www.example.org/users/login'));
 
1041
                $MockController->expectCallCount('header', 2);
 
1042
                $MockController->redirect('http://www.example.org/users/login', 301, false);
 
1043
 
 
1044
                $MockController =& new MockController();
 
1045
                $MockController->components = array('MockTest');
 
1046
                $MockController->Component =& new Component();
 
1047
                $MockController->Component->init($MockController);
 
1048
                $MockController->MockTest->setReturnValue('beforeRedirect', null);
 
1049
                $MockController->expectAt(0, 'header', array('HTTP/1.1 301 Moved Permanently'));
 
1050
                $MockController->expectAt(1, 'header', array('Location: http://cakephp.org'));
 
1051
                $MockController->expectCallCount('header', 2);
 
1052
                $MockController->redirect('http://cakephp.org', 301, false);
 
1053
 
 
1054
                $MockController =& new MockController();
 
1055
                $MockController->components = array('MockTest');
 
1056
                $MockController->Component =& new Component();
 
1057
                $MockController->Component->init($MockController);
 
1058
                $MockController->MockTest->setReturnValue('beforeRedirect', 'http://book.cakephp.org');
 
1059
                $MockController->expectAt(0, 'header', array('HTTP/1.1 301 Moved Permanently'));
 
1060
                $MockController->expectAt(1, 'header', array('Location: http://book.cakephp.org'));
 
1061
                $MockController->expectCallCount('header', 2);
 
1062
                $MockController->redirect('http://cakephp.org', 301, false);
 
1063
 
 
1064
                $MockController =& new MockController();
 
1065
                $MockController->components = array('MockTest');
 
1066
                $MockController->Component =& new Component();
 
1067
                $MockController->Component->init($MockController);
 
1068
                $MockController->MockTest->setReturnValue('beforeRedirect', false);
 
1069
                $MockController->expectNever('header');
 
1070
                $MockController->redirect('http://cakephp.org', 301, false);
 
1071
 
 
1072
                $MockController =& new MockController();
 
1073
                $MockController->components = array('MockTest', 'MockTestB');
 
1074
                $MockController->Component =& new Component();
 
1075
                $MockController->Component->init($MockController);
 
1076
                $MockController->MockTest->setReturnValue('beforeRedirect', 'http://book.cakephp.org');
 
1077
                $MockController->MockTestB->setReturnValue('beforeRedirect', 'http://bakery.cakephp.org');
 
1078
                $MockController->expectAt(0, 'header', array('HTTP/1.1 301 Moved Permanently'));
 
1079
                $MockController->expectAt(1, 'header', array('Location: http://bakery.cakephp.org'));
 
1080
                $MockController->expectCallCount('header', 2);
 
1081
                $MockController->redirect('http://cakephp.org', 301, false);
 
1082
        }
 
1083
 
 
1084
/**
 
1085
 * testMergeVars method
 
1086
 *
 
1087
 * @access public
 
1088
 * @return void
 
1089
 */
 
1090
        function testMergeVars() {
 
1091
                if ($this->skipIf(defined('APP_CONTROLLER_EXISTS'), '%s Need a non-existent AppController')) {
 
1092
                        return;
 
1093
                }
 
1094
 
 
1095
                $TestController =& new TestController();
 
1096
                $TestController->constructClasses();
 
1097
 
 
1098
                $testVars = get_class_vars('TestController');
 
1099
                $appVars = get_class_vars('AppController');
 
1100
 
 
1101
                $components = is_array($appVars['components'])
 
1102
                                                ? array_merge($appVars['components'], $testVars['components'])
 
1103
                                                : $testVars['components'];
 
1104
                if (!in_array('Session', $components)) {
 
1105
                        $components[] = 'Session';
 
1106
                }
 
1107
                $helpers = is_array($appVars['helpers'])
 
1108
                                        ? array_merge($appVars['helpers'], $testVars['helpers'])
 
1109
                                        : $testVars['helpers'];
 
1110
                $uses = is_array($appVars['uses'])
 
1111
                                        ? array_merge($appVars['uses'], $testVars['uses'])
 
1112
                                        : $testVars['uses'];
 
1113
 
 
1114
                $this->assertEqual(count(array_diff($TestController->helpers, $helpers)), 0);
 
1115
                $this->assertEqual(count(array_diff($TestController->uses, $uses)), 0);
 
1116
                $this->assertEqual(count(array_diff_assoc(Set::normalize($TestController->components), Set::normalize($components))), 0);
 
1117
 
 
1118
                $TestController =& new AnotherTestController();
 
1119
                $TestController->constructClasses();
 
1120
 
 
1121
                $appVars = get_class_vars('AppController');
 
1122
                $testVars = get_class_vars('AnotherTestController');
 
1123
 
 
1124
 
 
1125
                $this->assertTrue(in_array('ControllerPost', $appVars['uses']));
 
1126
                $this->assertNull($testVars['uses']);
 
1127
 
 
1128
                $this->assertFalse(isset($TestController->ControllerPost));
 
1129
 
 
1130
 
 
1131
                $TestController =& new ControllerCommentsController();
 
1132
                $TestController->constructClasses();
 
1133
 
 
1134
                $appVars = get_class_vars('AppController');
 
1135
                $testVars = get_class_vars('ControllerCommentsController');
 
1136
 
 
1137
 
 
1138
                $this->assertTrue(in_array('ControllerPost', $appVars['uses']));
 
1139
                $this->assertEqual(array('ControllerPost'), $testVars['uses']);
 
1140
 
 
1141
                $this->assertTrue(isset($TestController->ControllerPost));
 
1142
                $this->assertTrue(isset($TestController->ControllerComment));
 
1143
        }
 
1144
 
 
1145
/**
 
1146
 * test that options from child classes replace those in the parent classes.
 
1147
 *
 
1148
 * @access public
 
1149
 * @return void
 
1150
 */
 
1151
        function testChildComponentOptionsSupercedeParents() {
 
1152
                if ($this->skipIf(defined('APP_CONTROLLER_EXISTS'), '%s Need a non-existent AppController')) {
 
1153
                        return;
 
1154
                }
 
1155
                $TestController =& new TestController();
 
1156
                $expected = array('foo');
 
1157
                $TestController->components = array('Cookie' => $expected);
 
1158
                $TestController->constructClasses();
 
1159
                $this->assertEqual($TestController->components['Cookie'], $expected);
 
1160
        }
 
1161
 
 
1162
/**
 
1163
 * Ensure that __mergeVars is not being greedy and merging with
 
1164
 * AppController when you make an instance of Controller
 
1165
 *
 
1166
 * @return void
 
1167
 */
 
1168
        function testMergeVarsNotGreedy() {
 
1169
                $Controller =& new Controller();
 
1170
                $Controller->components = array();
 
1171
                $Controller->uses = array();
 
1172
                $Controller->constructClasses();
 
1173
 
 
1174
                $this->assertFalse(isset($Controller->Session));
 
1175
        }
 
1176
 
 
1177
/**
 
1178
 * testReferer method
 
1179
 *
 
1180
 * @access public
 
1181
 * @return void
 
1182
 */
 
1183
        function testReferer() {
 
1184
                $Controller =& new Controller();
 
1185
                $_SERVER['HTTP_REFERER'] = 'http://cakephp.org';
 
1186
                $result = $Controller->referer(null, false);
 
1187
                $expected = 'http://cakephp.org';
 
1188
                $this->assertIdentical($result, $expected);
 
1189
 
 
1190
                $_SERVER['HTTP_REFERER'] = '';
 
1191
                $result = $Controller->referer('http://cakephp.org', false);
 
1192
                $expected = 'http://cakephp.org';
 
1193
                $this->assertIdentical($result, $expected);
 
1194
 
 
1195
                $_SERVER['HTTP_REFERER'] = '';
 
1196
                $referer = array(
 
1197
                        'controller' => 'pages',
 
1198
                        'action' => 'display',
 
1199
                        'home'
 
1200
                );
 
1201
                $result = $Controller->referer($referer, false);
 
1202
                $expected = 'http://' . env('HTTP_HOST') . '/pages/display/home';
 
1203
                $this->assertIdentical($result, $expected);
 
1204
 
 
1205
                $_SERVER['HTTP_REFERER'] = '';
 
1206
                $result = $Controller->referer(null, false);
 
1207
                $expected = '/';
 
1208
                $this->assertIdentical($result, $expected);
 
1209
 
 
1210
                $_SERVER['HTTP_REFERER'] = FULL_BASE_URL.$Controller->webroot.'/some/path';
 
1211
                $result = $Controller->referer(null, false);
 
1212
                $expected = '/some/path';
 
1213
                $this->assertIdentical($result, $expected);
 
1214
 
 
1215
                $Controller->webroot .= '/';
 
1216
                $_SERVER['HTTP_REFERER'] = FULL_BASE_URL.$Controller->webroot.'/some/path';
 
1217
                $result = $Controller->referer(null, false);
 
1218
                $expected = '/some/path';
 
1219
                $this->assertIdentical($result, $expected);
 
1220
 
 
1221
                $_SERVER['HTTP_REFERER'] = FULL_BASE_URL.$Controller->webroot.'some/path';
 
1222
                $result = $Controller->referer(null, false);
 
1223
                $expected = '/some/path';
 
1224
                $this->assertIdentical($result, $expected);
 
1225
 
 
1226
                $Controller->webroot = '/recipe/';
 
1227
 
 
1228
                $_SERVER['HTTP_REFERER'] = FULL_BASE_URL.$Controller->webroot.'recipes/add';
 
1229
                $result = $Controller->referer();
 
1230
                $expected = '/recipes/add';
 
1231
                $this->assertIdentical($result, $expected);
 
1232
        }
 
1233
 
 
1234
/**
 
1235
 * testSetAction method
 
1236
 *
 
1237
 * @access public
 
1238
 * @return void
 
1239
 */
 
1240
        function testSetAction() {
 
1241
                $TestController =& new TestController();
 
1242
                $TestController->setAction('index', 1, 2);
 
1243
                $expected = array('testId' => 1, 'test2Id' => 2);
 
1244
                $this->assertidentical($TestController->data, $expected);
 
1245
        }
 
1246
 
 
1247
/**
 
1248
 * testUnimplementedIsAuthorized method
 
1249
 *
 
1250
 * @access public
 
1251
 * @return void
 
1252
 */
 
1253
        function testUnimplementedIsAuthorized() {
 
1254
                $TestController =& new TestController();
 
1255
                $TestController->isAuthorized();
 
1256
                $this->assertError();
 
1257
        }
 
1258
 
 
1259
/**
 
1260
 * testValidateErrors method
 
1261
 *
 
1262
 * @access public
 
1263
 * @return void
 
1264
 */
 
1265
        function testValidateErrors() {
 
1266
                $TestController =& new TestController();
 
1267
                $TestController->constructClasses();
 
1268
                $this->assertFalse($TestController->validateErrors());
 
1269
                $this->assertEqual($TestController->validate(), 0);
 
1270
 
 
1271
                $TestController->ControllerComment->invalidate('some_field', 'error_message');
 
1272
                $TestController->ControllerComment->invalidate('some_field2', 'error_message2');
 
1273
                $comment =& new ControllerComment();
 
1274
                $comment->set('someVar', 'data');
 
1275
                $result = $TestController->validateErrors($comment);
 
1276
                $expected = array('some_field' => 'error_message', 'some_field2' => 'error_message2');
 
1277
                $this->assertIdentical($result, $expected);
 
1278
                $this->assertEqual($TestController->validate($comment), 2);
 
1279
        }
 
1280
 
 
1281
/**
 
1282
 * test that validateErrors works with any old model.
 
1283
 *
 
1284
 * @return void
 
1285
 */
 
1286
        function testValidateErrorsOnArbitraryModels() {
 
1287
                $TestController =& new TestController();
 
1288
 
 
1289
                $Post = new ControllerPost();
 
1290
                $Post->validate = array('title' => 'notEmpty');
 
1291
                $Post->set('title', '');
 
1292
                $result = $TestController->validateErrors($Post);
 
1293
 
 
1294
                $expected = array('title' => 'This field cannot be left blank');
 
1295
                $this->assertEqual($result, $expected);
 
1296
        }
 
1297
 
 
1298
/**
 
1299
 * testPostConditions method
 
1300
 *
 
1301
 * @access public
 
1302
 * @return void
 
1303
 */
 
1304
        function testPostConditions() {
 
1305
                $Controller =& new Controller();
 
1306
 
 
1307
 
 
1308
                $data = array(
 
1309
                        'Model1' => array('field1' => '23'),
 
1310
                        'Model2' => array('field2' => 'string'),
 
1311
                        'Model3' => array('field3' => '23'),
 
1312
                );
 
1313
                $expected = array(
 
1314
                        'Model1.field1' => '23',
 
1315
                        'Model2.field2' => 'string',
 
1316
                        'Model3.field3' => '23',
 
1317
                );
 
1318
                $result = $Controller->postConditions($data);
 
1319
                $this->assertIdentical($result, $expected);
 
1320
 
 
1321
 
 
1322
                $data = array();
 
1323
                $Controller->data = array(
 
1324
                        'Model1' => array('field1' => '23'),
 
1325
                        'Model2' => array('field2' => 'string'),
 
1326
                        'Model3' => array('field3' => '23'),
 
1327
                );
 
1328
                $expected = array(
 
1329
                        'Model1.field1' => '23',
 
1330
                        'Model2.field2' => 'string',
 
1331
                        'Model3.field3' => '23',
 
1332
                );
 
1333
                $result = $Controller->postConditions($data);
 
1334
                $this->assertIdentical($result, $expected);
 
1335
 
 
1336
 
 
1337
                $data = array();
 
1338
                $Controller->data = array();
 
1339
                $result = $Controller->postConditions($data);
 
1340
                $this->assertNull($result);
 
1341
 
 
1342
 
 
1343
                $data = array();
 
1344
                $Controller->data = array(
 
1345
                        'Model1' => array('field1' => '23'),
 
1346
                        'Model2' => array('field2' => 'string'),
 
1347
                        'Model3' => array('field3' => '23'),
 
1348
                );
 
1349
                $ops = array(
 
1350
                        'Model1.field1' => '>',
 
1351
                        'Model2.field2' => 'LIKE',
 
1352
                        'Model3.field3' => '<=',
 
1353
                );
 
1354
                $expected = array(
 
1355
                        'Model1.field1 >' => '23',
 
1356
                        'Model2.field2 LIKE' => "%string%",
 
1357
                        'Model3.field3 <=' => '23',
 
1358
                );
 
1359
                $result = $Controller->postConditions($data, $ops);
 
1360
                $this->assertIdentical($result, $expected);
 
1361
        }
 
1362
 
 
1363
/**
 
1364
 * testRequestHandlerPrefers method
 
1365
 *
 
1366
 * @access public
 
1367
 * @return void
 
1368
 */
 
1369
        function testRequestHandlerPrefers(){
 
1370
                Configure::write('debug', 2);
 
1371
                $Controller =& new Controller();
 
1372
                $Controller->components = array("RequestHandler");
 
1373
                $Controller->modelClass='ControllerPost';
 
1374
                $Controller->params['url']['ext'] = 'rss';
 
1375
                $Controller->constructClasses();
 
1376
                $Controller->Component->initialize($Controller);
 
1377
                $Controller->beforeFilter();
 
1378
                $Controller->Component->startup($Controller);
 
1379
 
 
1380
                $this->assertEqual($Controller->RequestHandler->prefers(), 'rss');
 
1381
                unset($Controller);
 
1382
        }
 
1383
 
 
1384
/**
 
1385
 * testControllerHttpCodes method
 
1386
 *
 
1387
 * @access public
 
1388
 * @return void
 
1389
 */
 
1390
        function testControllerHttpCodes() {
 
1391
                $Controller =& new Controller();
 
1392
                $result = $Controller->httpCodes();
 
1393
                $this->assertEqual(count($result), 39);
 
1394
 
 
1395
                $result = $Controller->httpCodes(100);
 
1396
                $expected = array(100 => 'Continue');
 
1397
                $this->assertEqual($result, $expected);
 
1398
 
 
1399
                $codes = array(
 
1400
                        1337 => 'Undefined Unicorn',
 
1401
                        1729 => 'Hardy-Ramanujan Located'
 
1402
                );
 
1403
 
 
1404
                $result = $Controller->httpCodes($codes);
 
1405
                $this->assertTrue($result);
 
1406
                $this->assertEqual(count($Controller->httpCodes()), 41);
 
1407
 
 
1408
                $result = $Controller->httpCodes(1337);
 
1409
                $expected = array(1337 => 'Undefined Unicorn');
 
1410
                $this->assertEqual($result, $expected);
 
1411
 
 
1412
                $codes = array(404 => 'Sorry Bro');
 
1413
                $result = $Controller->httpCodes($codes);
 
1414
                $this->assertTrue($result);
 
1415
                $this->assertEqual(count($Controller->httpCodes()), 41);
 
1416
 
 
1417
                $result = $Controller->httpCodes(404);
 
1418
                $expected = array(404 => 'Sorry Bro');
 
1419
                $this->assertEqual($result, $expected);
 
1420
        }
 
1421
 
 
1422
/**
 
1423
 * Tests that the startup process calls the correct functions
 
1424
 *
 
1425
 * @access public
 
1426
 * @return void
 
1427
 */
 
1428
        function testStartupProcess() {
 
1429
                Mock::generatePartial('AnotherTestController','MockedController', array('beforeFilter', 'afterFilter'));
 
1430
                Mock::generate('TestComponent', 'MockTestComponent', array('startup', 'initialize'));
 
1431
                $MockedController =& new MockedController();
 
1432
                $MockedController->components = array('MockTest');
 
1433
                $MockedController->Component =& new Component();
 
1434
                $MockedController->Component->init($MockedController);
 
1435
                $MockedController->expectCallCount('beforeFilter', 1);
 
1436
                $MockedController->MockTest->expectCallCount('initialize', 1);
 
1437
                $MockedController->MockTest->expectCallCount('startup', 1);
 
1438
                $MockedController->startupProcess();
 
1439
        }
 
1440
/**
 
1441
 * Tests that the shutdown process calls the correct functions
 
1442
 *
 
1443
 * @access public
 
1444
 * @return void
 
1445
 */
 
1446
        function testShutdownProcess() {
 
1447
                Mock::generate('TestComponent', 'MockTestComponent', array('shutdown'));
 
1448
                $MockedController =& new MockedController();
 
1449
                $MockedController->components = array('MockTest');
 
1450
                $MockedController->Component =& new Component();
 
1451
                $MockedController->Component->init($MockedController);
 
1452
                $MockedController->expectCallCount('afterFilter', 1);
 
1453
                $MockedController->MockTest->expectCallCount('shutdown', 1);
 
1454
                $MockedController->shutdownProcess();
 
1455
        }
 
1456
}
 
 
b'\\ No newline at end of file'