Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 1
<?php
2
/**
3
 * ObjectTest 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.Core
15
 * @since         CakePHP(tm) v 1.2.0.5432
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18
 
19
App::uses('Object', 'Core');
20
App::uses('Router', 'Routing');
21
App::uses('Controller', 'Controller');
22
App::uses('Model', 'Model');
23
 
24
/**
25
 * RequestActionPost class
26
 *
27
 * @package       Cake.Test.Case.Core
28
 */
29
class RequestActionPost extends CakeTestModel {
30
 
31
/**
32
 * useTable property
33
 *
34
 * @var string
35
 */
36
	public $useTable = 'posts';
37
}
38
 
39
/**
40
 * RequestActionController class
41
 *
42
 * @package       Cake.Test.Case.Core
43
 */
44
class RequestActionController extends Controller {
45
 
46
/**
47
 * uses property
48
 *
49
 * @var array
50
 */
51
	public $uses = array('RequestActionPost');
52
 
53
/**
54
 * test_request_action method
55
 *
56
 * @return void
57
 */
58
	public function test_request_action() {
59
		return 'This is a test';
60
	}
61
 
62
/**
63
 * another_ra_test method
64
 *
65
 * @param mixed $id
66
 * @param mixed $other
67
 * @return void
68
 */
69
	public function another_ra_test($id, $other) {
70
		return $id + $other;
71
	}
72
 
73
/**
74
 * normal_request_action method
75
 *
76
 * @return void
77
 */
78
	public function normal_request_action() {
79
		return 'Hello World';
80
	}
81
 
82
/**
83
 * returns $this->here
84
 *
85
 * @return void
86
 */
87
	public function return_here() {
88
		return $this->here;
89
	}
90
 
91
/**
92
 * paginate_request_action method
93
 *
94
 * @return void
95
 */
96
	public function paginate_request_action() {
97
		$this->paginate();
98
		return true;
99
	}
100
 
101
/**
102
 * post pass, testing post passing
103
 *
104
 * @return array
105
 */
106
	public function post_pass() {
107
		return $this->request->data;
108
	}
109
 
110
/**
111
 * test param passing and parsing.
112
 *
113
 * @return array
114
 */
115
	public function params_pass() {
116
		return $this->request;
117
	}
118
 
119
	public function param_check() {
120
		$this->autoRender = false;
121
		$content = '';
122
		if (isset($this->request->params[0])) {
123
			$content = 'return found';
124
		}
125
		$this->response->body($content);
126
	}
127
 
128
}
129
 
130
/**
131
 * TestObject class
132
 *
133
 * @package       Cake.Test.Case.Core
134
 */
135
class TestObject extends Object {
136
 
137
/**
138
 * firstName property
139
 *
140
 * @var string
141
 */
142
	public $firstName = 'Joel';
143
 
144
/**
145
 * lastName property
146
 *
147
 * @var string
148
 */
149
	public $lastName = 'Moss';
150
 
151
/**
152
 * methodCalls property
153
 *
154
 * @var array
155
 */
156
	public $methodCalls = array();
157
 
158
/**
159
 * emptyMethod method
160
 *
161
 * @return void
162
 */
163
	public function emptyMethod() {
164
		$this->methodCalls[] = 'emptyMethod';
165
	}
166
 
167
/**
168
 * oneParamMethod method
169
 *
170
 * @param mixed $param
171
 * @return void
172
 */
173
	public function oneParamMethod($param) {
174
		$this->methodCalls[] = array('oneParamMethod' => array($param));
175
	}
176
 
177
/**
178
 * twoParamMethod method
179
 *
180
 * @param mixed $param
181
 * @param mixed $paramTwo
182
 * @return void
183
 */
184
	public function twoParamMethod($param, $paramTwo) {
185
		$this->methodCalls[] = array('twoParamMethod' => array($param, $paramTwo));
186
	}
187
 
188
/**
189
 * threeParamMethod method
190
 *
191
 * @param mixed $param
192
 * @param mixed $paramTwo
193
 * @param mixed $paramThree
194
 * @return void
195
 */
196
	public function threeParamMethod($param, $paramTwo, $paramThree) {
197
		$this->methodCalls[] = array('threeParamMethod' => array($param, $paramTwo, $paramThree));
198
	}
199
 
200
/**
201
 * fourParamMethod method
202
 *
203
 * @param mixed $param
204
 * @param mixed $paramTwo
205
 * @param mixed $paramThree
206
 * @param mixed $paramFour
207
 * @return void
208
 */
209
	public function fourParamMethod($param, $paramTwo, $paramThree, $paramFour) {
210
		$this->methodCalls[] = array('fourParamMethod' => array($param, $paramTwo, $paramThree, $paramFour));
211
	}
212
 
213
/**
214
 * fiveParamMethod method
215
 *
216
 * @param mixed $param
217
 * @param mixed $paramTwo
218
 * @param mixed $paramThree
219
 * @param mixed $paramFour
220
 * @param mixed $paramFive
221
 * @return void
222
 */
223
	public function fiveParamMethod($param, $paramTwo, $paramThree, $paramFour, $paramFive) {
224
		$this->methodCalls[] = array('fiveParamMethod' => array($param, $paramTwo, $paramThree, $paramFour, $paramFive));
225
	}
226
 
227
/**
228
 * crazyMethod method
229
 *
230
 * @param mixed $param
231
 * @param mixed $paramTwo
232
 * @param mixed $paramThree
233
 * @param mixed $paramFour
234
 * @param mixed $paramFive
235
 * @param mixed $paramSix
236
 * @param mixed $paramSeven
237
 * @return void
238
 */
239
	public function crazyMethod($param, $paramTwo, $paramThree, $paramFour, $paramFive, $paramSix, $paramSeven = null) {
240
		$this->methodCalls[] = array('crazyMethod' => array($param, $paramTwo, $paramThree, $paramFour, $paramFive, $paramSix, $paramSeven));
241
	}
242
 
243
/**
244
 * methodWithOptionalParam method
245
 *
246
 * @param mixed $param
247
 * @return void
248
 */
249
	public function methodWithOptionalParam($param = null) {
250
		$this->methodCalls[] = array('methodWithOptionalParam' => array($param));
251
	}
252
 
253
/**
254
 * undocumented function
255
 *
256
 * @return void
257
 */
258
	public function set($properties = array()) {
259
		return parent::_set($properties);
260
	}
261
 
262
}
263
 
264
/**
265
 * ObjectTestModel class
266
 *
267
 * @package       Cake.Test.Case.Core
268
 */
269
class ObjectTestModel extends CakeTestModel {
270
 
271
	public $useTable = false;
272
 
273
}
274
 
275
/**
276
 * Object Test class
277
 *
278
 * @package       Cake.Test.Case.Core
279
 */
280
class ObjectTest extends CakeTestCase {
281
 
282
/**
283
 * fixtures
284
 *
285
 * @var string
286
 */
287
	public $fixtures = array('core.post', 'core.test_plugin_comment', 'core.comment');
288
 
289
/**
290
 * setUp method
291
 *
292
 * @return void
293
 */
294
	public function setUp() {
295
		parent::setUp();
296
		$this->object = new TestObject();
297
	}
298
 
299
/**
300
 * tearDown method
301
 *
302
 * @return void
303
 */
304
	public function tearDown() {
305
		parent::tearDown();
306
		CakePlugin::unload();
307
		unset($this->object);
308
	}
309
 
310
/**
311
 * testLog method
312
 *
313
 * @return void
314
 */
315
	public function testLog() {
316
		if (file_exists(LOGS . 'error.log')) {
317
			unlink(LOGS . 'error.log');
318
		}
319
		$this->assertTrue($this->object->log('Test warning 1'));
320
		$this->assertTrue($this->object->log(array('Test' => 'warning 2')));
321
		$result = file(LOGS . 'error.log');
322
		$this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Error: Test warning 1$/', $result[0]);
323
		$this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Error: Array$/', $result[1]);
324
		$this->assertRegExp('/^\($/', $result[2]);
325
		$this->assertRegExp('/\[Test\] => warning 2$/', $result[3]);
326
		$this->assertRegExp('/^\)$/', $result[4]);
327
		unlink(LOGS . 'error.log');
328
 
329
		$this->assertTrue($this->object->log('Test warning 1', LOG_WARNING));
330
		$this->assertTrue($this->object->log(array('Test' => 'warning 2'), LOG_WARNING));
331
		$result = file(LOGS . 'error.log');
332
		$this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Test warning 1$/', $result[0]);
333
		$this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Array$/', $result[1]);
334
		$this->assertRegExp('/^\($/', $result[2]);
335
		$this->assertRegExp('/\[Test\] => warning 2$/', $result[3]);
336
		$this->assertRegExp('/^\)$/', $result[4]);
337
		unlink(LOGS . 'error.log');
338
	}
339
 
340
/**
341
 * testSet method
342
 *
343
 * @return void
344
 */
345
	public function testSet() {
346
		$this->object->set('a string');
347
		$this->assertEquals('Joel', $this->object->firstName);
348
 
349
		$this->object->set(array('firstName'));
350
		$this->assertEquals('Joel', $this->object->firstName);
351
 
352
		$this->object->set(array('firstName' => 'Ashley'));
353
		$this->assertEquals('Ashley', $this->object->firstName);
354
 
355
		$this->object->set(array('firstName' => 'Joel', 'lastName' => 'Moose'));
356
		$this->assertEquals('Joel', $this->object->firstName);
357
		$this->assertEquals('Moose', $this->object->lastName);
358
	}
359
 
360
/**
361
 * testToString method
362
 *
363
 * @return void
364
 */
365
	public function testToString() {
366
		$result = strtolower($this->object->toString());
367
		$this->assertEquals('testobject', $result);
368
	}
369
 
370
/**
371
 * testMethodDispatching method
372
 *
373
 * @return void
374
 */
375
	public function testMethodDispatching() {
376
		$this->object->emptyMethod();
377
		$expected = array('emptyMethod');
378
		$this->assertSame($this->object->methodCalls, $expected);
379
 
380
		$this->object->oneParamMethod('Hello');
381
		$expected[] = array('oneParamMethod' => array('Hello'));
382
		$this->assertSame($this->object->methodCalls, $expected);
383
 
384
		$this->object->twoParamMethod(true, false);
385
		$expected[] = array('twoParamMethod' => array(true, false));
386
		$this->assertSame($this->object->methodCalls, $expected);
387
 
388
		$this->object->threeParamMethod(true, false, null);
389
		$expected[] = array('threeParamMethod' => array(true, false, null));
390
		$this->assertSame($this->object->methodCalls, $expected);
391
 
392
		$this->object->crazyMethod(1, 2, 3, 4, 5, 6, 7);
393
		$expected[] = array('crazyMethod' => array(1, 2, 3, 4, 5, 6, 7));
394
		$this->assertSame($this->object->methodCalls, $expected);
395
 
396
		$this->object = new TestObject();
397
		$this->assertSame($this->object->methodCalls, array());
398
 
399
		$this->object->dispatchMethod('emptyMethod');
400
		$expected = array('emptyMethod');
401
		$this->assertSame($this->object->methodCalls, $expected);
402
 
403
		$this->object->dispatchMethod('oneParamMethod', array('Hello'));
404
		$expected[] = array('oneParamMethod' => array('Hello'));
405
		$this->assertSame($this->object->methodCalls, $expected);
406
 
407
		$this->object->dispatchMethod('twoParamMethod', array(true, false));
408
		$expected[] = array('twoParamMethod' => array(true, false));
409
		$this->assertSame($this->object->methodCalls, $expected);
410
 
411
		$this->object->dispatchMethod('threeParamMethod', array(true, false, null));
412
		$expected[] = array('threeParamMethod' => array(true, false, null));
413
		$this->assertSame($this->object->methodCalls, $expected);
414
 
415
		$this->object->dispatchMethod('fourParamMethod', array(1, 2, 3, 4));
416
		$expected[] = array('fourParamMethod' => array(1, 2, 3, 4));
417
		$this->assertSame($this->object->methodCalls, $expected);
418
 
419
		$this->object->dispatchMethod('fiveParamMethod', array(1, 2, 3, 4, 5));
420
		$expected[] = array('fiveParamMethod' => array(1, 2, 3, 4, 5));
421
		$this->assertSame($this->object->methodCalls, $expected);
422
 
423
		$this->object->dispatchMethod('crazyMethod', array(1, 2, 3, 4, 5, 6, 7));
424
		$expected[] = array('crazyMethod' => array(1, 2, 3, 4, 5, 6, 7));
425
		$this->assertSame($this->object->methodCalls, $expected);
426
 
427
		$this->object->dispatchMethod('methodWithOptionalParam', array('Hello'));
428
		$expected[] = array('methodWithOptionalParam' => array("Hello"));
429
		$this->assertSame($this->object->methodCalls, $expected);
430
 
431
		$this->object->dispatchMethod('methodWithOptionalParam');
432
		$expected[] = array('methodWithOptionalParam' => array(null));
433
		$this->assertSame($this->object->methodCalls, $expected);
434
	}
435
 
436
/**
437
 * testRequestAction method
438
 *
439
 * @return void
440
 */
441
	public function testRequestAction() {
442
		App::build(array(
443
			'Model' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS),
444
			'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS),
445
			'Controller' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Controller' . DS)
446
		), App::RESET);
447
		$this->assertNull(Router::getRequest(), 'request stack should be empty.');
448
 
449
		$result = $this->object->requestAction('');
450
		$this->assertFalse($result);
451
 
452
		$result = $this->object->requestAction('/request_action/test_request_action');
453
		$expected = 'This is a test';
454
		$this->assertEquals($expected, $result);
455
 
456
		$result = $this->object->requestAction(
457
			Configure::read('App.fullBaseUrl') . '/request_action/test_request_action'
458
		);
459
		$expected = 'This is a test';
460
		$this->assertEquals($expected, $result);
461
 
462
		$result = $this->object->requestAction('/request_action/another_ra_test/2/5');
463
		$expected = 7;
464
		$this->assertEquals($expected, $result);
465
 
466
		$result = $this->object->requestAction('/tests_apps/index', array('return'));
467
		$expected = 'This is the TestsAppsController index view ';
468
		$this->assertEquals($expected, $result);
469
 
470
		$result = $this->object->requestAction('/tests_apps/some_method');
471
		$expected = 5;
472
		$this->assertEquals($expected, $result);
473
 
474
		$result = $this->object->requestAction('/request_action/paginate_request_action');
475
		$this->assertTrue($result);
476
 
477
		$result = $this->object->requestAction('/request_action/normal_request_action');
478
		$expected = 'Hello World';
479
		$this->assertEquals($expected, $result);
480
 
481
		$this->assertNull(Router::getRequest(), 'requests were not popped off the stack, this will break url generation');
482
	}
483
 
484
/**
485
 * test requestAction() and plugins.
486
 *
487
 * @return void
488
 */
489
	public function testRequestActionPlugins() {
490
		App::build(array(
491
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
492
		), App::RESET);
493
		CakePlugin::load('TestPlugin');
494
		Router::reload();
495
 
496
		$result = $this->object->requestAction('/test_plugin/tests/index', array('return'));
497
		$expected = 'test plugin index';
498
		$this->assertEquals($expected, $result);
499
 
500
		$result = $this->object->requestAction('/test_plugin/tests/index/some_param', array('return'));
501
		$expected = 'test plugin index';
502
		$this->assertEquals($expected, $result);
503
 
504
		$result = $this->object->requestAction(
505
			array('controller' => 'tests', 'action' => 'index', 'plugin' => 'test_plugin'), array('return')
506
		);
507
		$expected = 'test plugin index';
508
		$this->assertEquals($expected, $result);
509
 
510
		$result = $this->object->requestAction('/test_plugin/tests/some_method');
511
		$expected = 25;
512
		$this->assertEquals($expected, $result);
513
 
514
		$result = $this->object->requestAction(
515
			array('controller' => 'tests', 'action' => 'some_method', 'plugin' => 'test_plugin')
516
		);
517
		$expected = 25;
518
		$this->assertEquals($expected, $result);
519
	}
520
 
521
/**
522
 * test requestAction() with arrays.
523
 *
524
 * @return void
525
 */
526
	public function testRequestActionArray() {
527
		App::build(array(
528
			'Model' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS),
529
			'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS),
530
			'Controller' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Controller' . DS),
531
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
532
		), App::RESET);
533
		CakePlugin::load(array('TestPlugin'));
534
 
535
		$result = $this->object->requestAction(
536
			array('controller' => 'request_action', 'action' => 'test_request_action')
537
		);
538
		$expected = 'This is a test';
539
		$this->assertEquals($expected, $result);
540
 
541
		$result = $this->object->requestAction(
542
			array('controller' => 'request_action', 'action' => 'another_ra_test'),
543
			array('pass' => array('5', '7'))
544
		);
545
		$expected = 12;
546
		$this->assertEquals($expected, $result);
547
 
548
		$result = $this->object->requestAction(
549
			array('controller' => 'tests_apps', 'action' => 'index'), array('return')
550
		);
551
		$expected = 'This is the TestsAppsController index view ';
552
		$this->assertEquals($expected, $result);
553
 
554
		$result = $this->object->requestAction(array('controller' => 'tests_apps', 'action' => 'some_method'));
555
		$expected = 5;
556
		$this->assertEquals($expected, $result);
557
 
558
		$result = $this->object->requestAction(
559
			array('controller' => 'request_action', 'action' => 'normal_request_action')
560
		);
561
		$expected = 'Hello World';
562
		$this->assertEquals($expected, $result);
563
 
564
		$result = $this->object->requestAction(
565
			array('controller' => 'request_action', 'action' => 'paginate_request_action')
566
		);
567
		$this->assertTrue($result);
568
 
569
		$result = $this->object->requestAction(
570
			array('controller' => 'request_action', 'action' => 'paginate_request_action'),
571
			array('pass' => array(5), 'named' => array('param' => 'value'))
572
		);
573
		$this->assertTrue($result);
574
	}
575
 
576
/**
577
 * Test that requestAction() does not forward the 0 => return value.
578
 *
579
 * @return void
580
 */
581
	public function testRequestActionRemoveReturnParam() {
582
		$result = $this->object->requestAction(
583
			'/request_action/param_check', array('return')
584
		);
585
		$this->assertEquals('', $result, 'Return key was found');
586
	}
587
 
588
/**
589
 * Test that requestAction() is populating $this->params properly
590
 *
591
 * @return void
592
 */
593
	public function testRequestActionParamParseAndPass() {
594
		$result = $this->object->requestAction('/request_action/params_pass');
595
		$this->assertEquals('request_action/params_pass', $result->url);
596
		$this->assertEquals('request_action', $result['controller']);
597
		$this->assertEquals('params_pass', $result['action']);
598
		$this->assertEquals(null, $result['plugin']);
599
 
600
		$result = $this->object->requestAction('/request_action/params_pass/sort:desc/limit:5');
601
		$expected = array('sort' => 'desc', 'limit' => 5);
602
		$this->assertEquals($expected, $result['named']);
603
 
604
		$result = $this->object->requestAction(
605
			array('controller' => 'request_action', 'action' => 'params_pass'),
606
			array('named' => array('sort' => 'desc', 'limit' => 5))
607
		);
608
		$this->assertEquals($expected, $result['named']);
609
	}
610
 
611
/**
612
 * Test that requestAction handles get parameters correctly.
613
 *
614
 * @return void
615
 */
616
	public function testRequestActionGetParameters() {
617
		$result = $this->object->requestAction(
618
			'/request_action/params_pass?get=value&limit=5'
619
		);
620
		$this->assertEquals('value', $result->query['get']);
621
 
622
		$result = $this->object->requestAction(
623
			array('controller' => 'request_action', 'action' => 'params_pass'),
624
			array('url' => array('get' => 'value', 'limit' => 5))
625
		);
626
		$this->assertEquals('value', $result->query['get']);
627
	}
628
 
629
/**
630
 * test that requestAction does not fish data out of the POST
631
 * superglobal.
632
 *
633
 * @return void
634
 */
635
	public function testRequestActionNoPostPassing() {
636
		$_tmp = $_POST;
637
 
638
		$_POST = array('data' => array(
639
			'item' => 'value'
640
		));
641
		$result = $this->object->requestAction(array('controller' => 'request_action', 'action' => 'post_pass'));
642
		$this->assertEmpty($result);
643
 
644
		$result = $this->object->requestAction(
645
			array('controller' => 'request_action', 'action' => 'post_pass'),
646
			array('data' => $_POST['data'])
647
		);
648
		$expected = $_POST['data'];
649
		$this->assertEquals($expected, $result);
650
 
651
		$result = $this->object->requestAction('/request_action/post_pass');
652
		$expected = $_POST['data'];
653
		$this->assertEquals($expected, $result);
654
 
655
		$_POST = $_tmp;
656
	}
657
 
658
/**
659
 * Test requestAction with post data.
660
 *
661
 * @return void
662
 */
663
	public function testRequestActionPostWithData() {
664
		$data = array(
665
			'Post' => array('id' => 2)
666
		);
667
		$result = $this->object->requestAction(
668
			array('controller' => 'request_action', 'action' => 'post_pass'),
669
			array('data' => $data)
670
		);
671
		$this->assertEquals($data, $result);
672
 
673
		$result = $this->object->requestAction(
674
			'/request_action/post_pass',
675
			array('data' => $data)
676
		);
677
		$this->assertEquals($data, $result);
678
	}
679
}