Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
15403 manish.sha 1
<?php
2
/**
3
 * ViewTask Test file
4
 *
5
 * Test Case for view generation shell task
6
 *
7
 * CakePHP : Rapid Development Framework (http://cakephp.org)
8
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
9
 *
10
 * Licensed under The MIT License
11
 * For full copyright and license information, please see the LICENSE.txt
12
 * Redistributions of files must retain the above copyright notice.
13
 *
14
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
15
 * @link          http://cakephp.org CakePHP Project
16
 * @package       Cake.Test.Case.Console.Command.Task
17
 * @since         CakePHP v 1.2.0.7726
18
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
19
 */
20
 
21
App::uses('ShellDispatcher', 'Console');
22
App::uses('ConsoleOutput', 'Console');
23
App::uses('ConsoleInput', 'Console');
24
App::uses('Shell', 'Console');
25
App::uses('ViewTask', 'Console/Command/Task');
26
App::uses('ControllerTask', 'Console/Command/Task');
27
App::uses('TemplateTask', 'Console/Command/Task');
28
App::uses('ProjectTask', 'Console/Command/Task');
29
App::uses('DbConfigTask', 'Console/Command/Task');
30
App::uses('Model', 'Model');
31
App::uses('Controller', 'Controller');
32
App::uses('AppController', 'Controller');
33
 
34
/**
35
 * Test View Task Comment Model
36
 *
37
 * @package       Cake.Test.Case.Console.Command.Task
38
 */
39
class ViewTaskComment extends Model {
40
 
41
/**
42
 * Table name
43
 *
44
 * @var string
45
 */
46
	public $useTable = 'comments';
47
 
48
/**
49
 * Belongs To Associations
50
 *
51
 * @var array
52
 */
53
	public $belongsTo = array(
54
		'Article' => array(
55
			'className' => 'TestTest.ViewTaskArticle',
56
			'foreignKey' => 'article_id'
57
		)
58
	);
59
}
60
 
61
/**
62
 * Test View Task Article Model
63
 *
64
 * @package       Cake.Test.Case.Console.Command.Task
65
 */
66
class ViewTaskArticle extends Model {
67
 
68
/**
69
 * Table name
70
 *
71
 * @var string
72
 */
73
	public $useTable = 'articles';
74
}
75
 
76
/**
77
 * Test View Task Comments Controller
78
 *
79
 * @package       Cake.Test.Case.Console.Command.Task
80
 */
81
class ViewTaskCommentsController extends Controller {
82
 
83
/**
84
 * Testing public controller action
85
 *
86
 * @return void
87
 */
88
	public function index() {
89
	}
90
 
91
/**
92
 * Testing public controller action
93
 *
94
 * @return void
95
 */
96
	public function add() {
97
	}
98
 
99
}
100
 
101
/**
102
 * Test View Task Articles Controller
103
 *
104
 * @package       Cake.Test.Case.Console.Command.Task
105
 */
106
class ViewTaskArticlesController extends Controller {
107
 
108
/**
109
 * Test public controller action
110
 *
111
 * @return void
112
 */
113
	public function index() {
114
	}
115
 
116
/**
117
 * Test public controller action
118
 *
119
 * @return void
120
 */
121
	public function add() {
122
	}
123
 
124
/**
125
 * Test admin prefixed controller action
126
 *
127
 * @return void
128
 */
129
	public function admin_index() {
130
	}
131
 
132
/**
133
 * Test admin prefixed controller action
134
 *
135
 * @return void
136
 */
137
	public function admin_add() {
138
	}
139
 
140
/**
141
 * Test admin prefixed controller action
142
 *
143
 * @return void
144
 */
145
	public function admin_view() {
146
	}
147
 
148
/**
149
 * Test admin prefixed controller action
150
 *
151
 * @return void
152
 */
153
	public function admin_edit() {
154
	}
155
 
156
/**
157
 * Test admin prefixed controller action
158
 *
159
 * @return void
160
 */
161
	public function admin_delete() {
162
	}
163
 
164
}
165
 
166
/**
167
 * ViewTaskTest class
168
 *
169
 * @package       Cake.Test.Case.Console.Command.Task
170
 */
171
class ViewTaskTest extends CakeTestCase {
172
 
173
/**
174
 * Fixtures
175
 *
176
 * @var array
177
 */
178
	public $fixtures = array('core.article', 'core.comment', 'core.articles_tag', 'core.tag');
179
 
180
/**
181
 * setUp method
182
 *
183
 * Ensure that the default theme is used
184
 *
185
 * @return void
186
 */
187
	public function setUp() {
188
		parent::setUp();
189
		$out = $this->getMock('ConsoleOutput', array(), array(), '', false);
190
		$in = $this->getMock('ConsoleInput', array(), array(), '', false);
191
 
192
		$this->Task = $this->getMock('ViewTask',
193
			array('in', 'err', 'createFile', '_stop'),
194
			array($out, $out, $in)
195
		);
196
		$this->Task->Template = new TemplateTask($out, $out, $in);
197
		$this->Task->Controller = $this->getMock('ControllerTask', array(), array($out, $out, $in));
198
		$this->Task->Project = $this->getMock('ProjectTask', array(), array($out, $out, $in));
199
		$this->Task->DbConfig = $this->getMock('DbConfigTask', array(), array($out, $out, $in));
200
 
201
		$this->Task->path = TMP;
202
		$this->Task->Template->params['theme'] = 'default';
203
		$this->Task->Template->templatePaths = array('default' => CAKE . 'Console' . DS . 'Templates' . DS . 'default' . DS);
204
	}
205
 
206
/**
207
 * tearDown method
208
 *
209
 * @return void
210
 */
211
	public function tearDown() {
212
		parent::tearDown();
213
		unset($this->Task, $this->Dispatch);
214
	}
215
 
216
/**
217
 * Test getContent and parsing of Templates.
218
 *
219
 * @return void
220
 */
221
	public function testGetContent() {
222
		$vars = array(
223
			'modelClass' => 'TestViewModel',
224
			'schema' => array(),
225
			'primaryKey' => 'id',
226
			'displayField' => 'name',
227
			'singularVar' => 'testViewModel',
228
			'pluralVar' => 'testViewModels',
229
			'singularHumanName' => 'Test View Model',
230
			'pluralHumanName' => 'Test View Models',
231
			'fields' => array('id', 'name', 'body'),
232
			'associations' => array()
233
		);
234
		$result = $this->Task->getContent('view', $vars);
235
 
236
		$this->assertRegExp('/Delete Test View Model/', $result);
237
		$this->assertRegExp('/Edit Test View Model/', $result);
238
		$this->assertRegExp('/List Test View Models/', $result);
239
		$this->assertRegExp('/New Test View Model/', $result);
240
 
241
		$this->assertRegExp('/testViewModel\[\'TestViewModel\'\]\[\'id\'\]/', $result);
242
		$this->assertRegExp('/testViewModel\[\'TestViewModel\'\]\[\'name\'\]/', $result);
243
		$this->assertRegExp('/testViewModel\[\'TestViewModel\'\]\[\'body\'\]/', $result);
244
	}
245
 
246
/**
247
 * test getContent() using an admin_prefixed action.
248
 *
249
 * @return void
250
 */
251
	public function testGetContentWithAdminAction() {
252
		$_back = Configure::read('Routing');
253
		Configure::write('Routing.prefixes', array('admin'));
254
		$vars = array(
255
			'modelClass' => 'TestViewModel',
256
			'schema' => array(),
257
			'primaryKey' => 'id',
258
			'displayField' => 'name',
259
			'singularVar' => 'testViewModel',
260
			'pluralVar' => 'testViewModels',
261
			'singularHumanName' => 'Test View Model',
262
			'pluralHumanName' => 'Test View Models',
263
			'fields' => array('id', 'name', 'body'),
264
			'associations' => array()
265
		);
266
		$result = $this->Task->getContent('admin_view', $vars);
267
 
268
		$this->assertRegExp('/Delete Test View Model/', $result);
269
		$this->assertRegExp('/Edit Test View Model/', $result);
270
		$this->assertRegExp('/List Test View Models/', $result);
271
		$this->assertRegExp('/New Test View Model/', $result);
272
 
273
		$this->assertRegExp('/testViewModel\[\'TestViewModel\'\]\[\'id\'\]/', $result);
274
		$this->assertRegExp('/testViewModel\[\'TestViewModel\'\]\[\'name\'\]/', $result);
275
		$this->assertRegExp('/testViewModel\[\'TestViewModel\'\]\[\'body\'\]/', $result);
276
 
277
		$result = $this->Task->getContent('admin_add', $vars);
278
		$this->assertRegExp("/input\('name'\)/", $result);
279
		$this->assertRegExp("/input\('body'\)/", $result);
280
		$this->assertRegExp('/List Test View Models/', $result);
281
 
282
		Configure::write('Routing', $_back);
283
	}
284
 
285
/**
286
 * test Bake method
287
 *
288
 * @return void
289
 */
290
	public function testBakeView() {
291
		$this->Task->controllerName = 'ViewTaskComments';
292
 
293
		$this->Task->expects($this->at(0))->method('createFile')
294
			->with(
295
				TMP . 'ViewTaskComments' . DS . 'view.ctp',
296
				$this->stringContains('View Task Articles')
297
			);
298
 
299
		$this->Task->bake('view', true);
300
	}
301
 
302
/**
303
 * test baking an edit file
304
 *
305
 * @return void
306
 */
307
	public function testBakeEdit() {
308
		$this->Task->controllerName = 'ViewTaskComments';
309
 
310
		$this->Task->expects($this->at(0))->method('createFile')
311
			->with(
312
				TMP . 'ViewTaskComments' . DS . 'edit.ctp',
313
				new PHPUnit_Framework_Constraint_IsAnything()
314
			);
315
		$this->Task->bake('edit', true);
316
	}
317
 
318
/**
319
 * test baking an index
320
 *
321
 * @return void
322
 */
323
	public function testBakeIndex() {
324
		$this->Task->controllerName = 'ViewTaskComments';
325
 
326
		$expected = file_get_contents(CAKE . 'Test' . DS . 'bake_compare' . DS . 'View' . DS . 'index.ctp');
327
		$this->Task->expects($this->at(0))->method('createFile')
328
			->with(
329
				TMP . 'ViewTaskComments' . DS . 'index.ctp',
330
				$expected
331
			);
332
		$this->Task->bake('index', true);
333
	}
334
 
335
/**
336
 * test that baking a view with no template doesn't make a file.
337
 *
338
 * @return void
339
 */
340
	public function testBakeWithNoTemplate() {
341
		$this->Task->controllerName = 'ViewTaskComments';
342
 
343
		$this->Task->expects($this->never())->method('createFile');
344
		$this->Task->bake('delete', true);
345
	}
346
 
347
/**
348
 * test bake() with a -plugin param
349
 *
350
 * @return void
351
 */
352
	public function testBakeWithPlugin() {
353
		$this->Task->controllerName = 'ViewTaskComments';
354
		$this->Task->plugin = 'TestTest';
355
		$this->Task->name = 'View';
356
 
357
		//fake plugin path
358
		CakePlugin::load('TestTest', array('path' => APP . 'Plugin' . DS . 'TestTest' . DS));
359
		$path = APP . 'Plugin' . DS . 'TestTest' . DS . 'View' . DS . 'ViewTaskComments' . DS . 'view.ctp';
360
 
361
		$result = $this->Task->getContent('index');
362
		$this->assertNotContains('List Test Test.view Task Articles', $result);
363
 
364
		$this->Task->expects($this->once())
365
			->method('createFile')
366
			->with($path, $this->anything());
367
 
368
		$this->Task->bake('view', true);
369
		CakePlugin::unload();
370
	}
371
 
372
/**
373
 * test bake actions baking multiple actions.
374
 *
375
 * @return void
376
 */
377
	public function testBakeActions() {
378
		$this->Task->controllerName = 'ViewTaskComments';
379
 
380
		$this->Task->expects($this->at(0))->method('createFile')
381
			->with(
382
				TMP . 'ViewTaskComments' . DS . 'view.ctp',
383
				$this->stringContains('View Task Comments')
384
			);
385
		$this->Task->expects($this->at(1))->method('createFile')
386
			->with(
387
				TMP . 'ViewTaskComments' . DS . 'edit.ctp',
388
				$this->stringContains('Edit View Task Comment')
389
			);
390
		$this->Task->expects($this->at(2))->method('createFile')
391
			->with(
392
				TMP . 'ViewTaskComments' . DS . 'index.ctp',
393
				$this->stringContains('ViewTaskComment')
394
			);
395
 
396
		$this->Task->bakeActions(array('view', 'edit', 'index'), array());
397
	}
398
 
399
/**
400
 * test baking a customAction (non crud)
401
 *
402
 * @return void
403
 */
404
	public function testCustomAction() {
405
		$this->Task->controllerName = 'ViewTaskComments';
406
 
407
		$this->Task->expects($this->any())->method('in')
408
			->will($this->onConsecutiveCalls('', 'my_action', 'y'));
409
 
410
		$this->Task->expects($this->once())->method('createFile')
411
			->with(
412
				TMP . 'ViewTaskComments' . DS . 'my_action.ctp',
413
				$this->anything()
414
			);
415
 
416
		$this->Task->customAction();
417
	}
418
 
419
/**
420
 * Test all()
421
 *
422
 * @return void
423
 */
424
	public function testExecuteIntoAll() {
425
		$this->Task->args[0] = 'all';
426
 
427
		$this->Task->Controller->expects($this->once())->method('listAll')
428
			->will($this->returnValue(array('view_task_comments')));
429
 
430
		$this->Task->expects($this->at(0))->method('createFile')
431
			->with(
432
				TMP . 'ViewTaskComments' . DS . 'index.ctp',
433
				$this->anything()
434
			);
435
		$this->Task->expects($this->at(1))->method('createFile')
436
			->with(
437
				TMP . 'ViewTaskComments' . DS . 'add.ctp',
438
				$this->anything()
439
			);
440
		$this->Task->expects($this->exactly(2))->method('createFile');
441
 
442
		$this->Task->execute();
443
	}
444
 
445
/**
446
 * Test all() with action parameter
447
 *
448
 * @return void
449
 */
450
	public function testExecuteIntoAllWithActionName() {
451
		$this->Task->args = array('all', 'index');
452
 
453
		$this->Task->Controller->expects($this->once())->method('listAll')
454
			->will($this->returnValue(array('view_task_comments')));
455
 
456
		$this->Task->expects($this->once())->method('createFile')
457
			->with(
458
				TMP . 'ViewTaskComments' . DS . 'index.ctp',
459
				$this->anything()
460
			);
461
 
462
		$this->Task->execute();
463
	}
464
 
465
/**
466
 * test `cake bake view $controller view`
467
 *
468
 * @return void
469
 */
470
	public function testExecuteWithActionParam() {
471
		$this->Task->args[0] = 'ViewTaskComments';
472
		$this->Task->args[1] = 'view';
473
 
474
		$this->Task->expects($this->once())->method('createFile')
475
			->with(
476
				TMP . 'ViewTaskComments' . DS . 'view.ctp',
477
				$this->anything()
478
			);
479
		$this->Task->execute();
480
	}
481
 
482
/**
483
 * test `cake bake view $controller`
484
 * Ensure that views are only baked for actions that exist in the controller.
485
 *
486
 * @return void
487
 */
488
	public function testExecuteWithController() {
489
		$this->Task->args[0] = 'ViewTaskComments';
490
 
491
		$this->Task->expects($this->at(0))->method('createFile')
492
			->with(
493
				TMP . 'ViewTaskComments' . DS . 'index.ctp',
494
				$this->anything()
495
			);
496
		$this->Task->expects($this->at(1))->method('createFile')
497
			->with(
498
				TMP . 'ViewTaskComments' . DS . 'add.ctp',
499
				$this->anything()
500
			);
501
		$this->Task->expects($this->exactly(2))->method('createFile');
502
 
503
		$this->Task->execute();
504
	}
505
 
506
/**
507
 * static dataprovider for test cases
508
 *
509
 * @return void
510
 */
511
	public static function nameVariations() {
512
		return array(array('ViewTaskComments'), array('ViewTaskComment'), array('view_task_comment'));
513
	}
514
 
515
/**
516
 * test that both plural and singular forms can be used for baking views.
517
 *
518
 * @dataProvider nameVariations
519
 * @return void
520
 */
521
	public function testExecuteWithControllerVariations($name) {
522
		$this->Task->args = array($name);
523
 
524
		$this->Task->expects($this->at(0))->method('createFile')
525
			->with(
526
				TMP . 'ViewTaskComments' . DS . 'index.ctp',
527
				$this->anything()
528
			);
529
		$this->Task->expects($this->at(1))->method('createFile')
530
			->with(
531
				TMP . 'ViewTaskComments' . DS . 'add.ctp',
532
				$this->anything()
533
			);
534
		$this->Task->execute();
535
	}
536
 
537
/**
538
 * test `cake bake view $controller --admin`
539
 * Which only bakes admin methods, not non-admin methods.
540
 *
541
 * @return void
542
 */
543
	public function testExecuteWithControllerAndAdminFlag() {
544
		$_back = Configure::read('Routing');
545
		Configure::write('Routing.prefixes', array('admin'));
546
		$this->Task->args[0] = 'ViewTaskArticles';
547
		$this->Task->params['admin'] = 1;
548
 
549
		$this->Task->Project->expects($this->any())->method('getPrefix')->will($this->returnValue('admin_'));
550
 
551
		$this->Task->expects($this->exactly(4))->method('createFile');
552
 
553
		$views = array('admin_index.ctp', 'admin_add.ctp', 'admin_view.ctp', 'admin_edit.ctp');
554
		foreach ($views as $i => $view) {
555
			$this->Task->expects($this->at($i))->method('createFile')
556
				->with(
557
					TMP . 'ViewTaskArticles' . DS . $view,
558
					$this->anything()
559
				);
560
		}
561
		$this->Task->execute();
562
		Configure::write('Routing', $_back);
563
	}
564
 
565
/**
566
 * test execute into interactive.
567
 *
568
 * @return void
569
 */
570
	public function testExecuteInteractive() {
571
		$this->Task->connection = 'test';
572
		$this->Task->args = array();
573
		$this->Task->params = array();
574
 
575
		$this->Task->Controller->expects($this->once())->method('getName')
576
			->will($this->returnValue('ViewTaskComments'));
577
 
578
		$this->Task->expects($this->any())->method('in')
579
			->will($this->onConsecutiveCalls('y', 'y', 'n'));
580
 
581
		$this->Task->expects($this->at(3))->method('createFile')
582
			->with(
583
				TMP . 'ViewTaskComments' . DS . 'index.ctp',
584
				$this->stringContains('ViewTaskComment')
585
			);
586
 
587
		$this->Task->expects($this->at(4))->method('createFile')
588
			->with(
589
				TMP . 'ViewTaskComments' . DS . 'view.ctp',
590
				$this->stringContains('ViewTaskComment')
591
			);
592
 
593
		$this->Task->expects($this->at(5))->method('createFile')
594
			->with(
595
				TMP . 'ViewTaskComments' . DS . 'add.ctp',
596
				$this->stringContains('Add View Task Comment')
597
			);
598
 
599
		$this->Task->expects($this->at(6))->method('createFile')
600
			->with(
601
				TMP . 'ViewTaskComments' . DS . 'edit.ctp',
602
				$this->stringContains('Edit View Task Comment')
603
			);
604
 
605
		$this->Task->expects($this->exactly(4))->method('createFile');
606
		$this->Task->execute();
607
	}
608
 
609
/**
610
 * test `cake bake view posts index list`
611
 *
612
 * @return void
613
 */
614
	public function testExecuteWithAlternateTemplates() {
615
		$this->Task->connection = 'test';
616
		$this->Task->args = array('ViewTaskComments', 'index', 'list');
617
		$this->Task->params = array();
618
 
619
		$this->Task->expects($this->once())->method('createFile')
620
			->with(
621
				TMP . 'ViewTaskComments' . DS . 'list.ctp',
622
				$this->stringContains('ViewTaskComment')
623
			);
624
		$this->Task->execute();
625
	}
626
 
627
/**
628
 * test execute into interactive() with admin methods.
629
 *
630
 * @return void
631
 */
632
	public function testExecuteInteractiveWithAdmin() {
633
		Configure::write('Routing.prefixes', array('admin'));
634
		$this->Task->connection = 'test';
635
		$this->Task->args = array();
636
 
637
		$this->Task->Controller->expects($this->once())->method('getName')
638
			->will($this->returnValue('ViewTaskComments'));
639
 
640
		$this->Task->Project->expects($this->once())->method('getPrefix')
641
			->will($this->returnValue('admin_'));
642
 
643
		$this->Task->expects($this->any())->method('in')
644
			->will($this->onConsecutiveCalls('y', 'n', 'y'));
645
 
646
		$this->Task->expects($this->at(3))->method('createFile')
647
			->with(
648
				TMP . 'ViewTaskComments' . DS . 'admin_index.ctp',
649
				$this->stringContains('ViewTaskComment')
650
			);
651
 
652
		$this->Task->expects($this->at(4))->method('createFile')
653
			->with(
654
				TMP . 'ViewTaskComments' . DS . 'admin_view.ctp',
655
				$this->stringContains('ViewTaskComment')
656
			);
657
 
658
		$this->Task->expects($this->at(5))->method('createFile')
659
			->with(
660
				TMP . 'ViewTaskComments' . DS . 'admin_add.ctp',
661
				$this->stringContains('Add View Task Comment')
662
			);
663
 
664
		$this->Task->expects($this->at(6))->method('createFile')
665
			->with(
666
				TMP . 'ViewTaskComments' . DS . 'admin_edit.ctp',
667
				$this->stringContains('Edit View Task Comment')
668
			);
669
 
670
		$this->Task->expects($this->exactly(4))->method('createFile');
671
		$this->Task->execute();
672
	}
673
 
674
/**
675
 * test getting templates, make sure noTemplateActions works and prefixed template is used before generic one.
676
 *
677
 * @return void
678
 */
679
	public function testGetTemplate() {
680
		$result = $this->Task->getTemplate('delete');
681
		$this->assertFalse($result);
682
 
683
		$result = $this->Task->getTemplate('add');
684
		$this->assertEquals('form', $result);
685
 
686
		Configure::write('Routing.prefixes', array('admin'));
687
 
688
		$result = $this->Task->getTemplate('admin_add');
689
		$this->assertEquals('form', $result);
690
 
691
		$this->Task->Template->templatePaths = array(
692
			'test' => CAKE . 'Test' . DS . 'test_app' . DS . 'Console' . DS . 'Templates' . DS . 'test' . DS
693
		);
694
		$this->Task->Template->params['theme'] = 'test';
695
 
696
		$result = $this->Task->getTemplate('admin_edit');
697
		$this->assertEquals('admin_edit', $result);
698
	}
699
 
700
}