Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 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
		$this->Task->expects($this->at(0))->method('createFile')
327
			->with(
328
				TMP . 'ViewTaskComments' . DS . 'index.ctp',
329
				$this->stringContains("\$viewTaskComment['Article']['title']")
330
			);
331
		$this->Task->bake('index', true);
332
	}
333
 
334
/**
335
 * test that baking a view with no template doesn't make a file.
336
 *
337
 * @return void
338
 */
339
	public function testBakeWithNoTemplate() {
340
		$this->Task->controllerName = 'ViewTaskComments';
341
 
342
		$this->Task->expects($this->never())->method('createFile');
343
		$this->Task->bake('delete', true);
344
	}
345
 
346
/**
347
 * test bake() with a -plugin param
348
 *
349
 * @return void
350
 */
351
	public function testBakeWithPlugin() {
352
		$this->Task->controllerName = 'ViewTaskComments';
353
		$this->Task->plugin = 'TestTest';
354
		$this->Task->name = 'View';
355
 
356
		//fake plugin path
357
		CakePlugin::load('TestTest', array('path' => APP . 'Plugin' . DS . 'TestTest' . DS));
358
		$path = APP . 'Plugin' . DS . 'TestTest' . DS . 'View' . DS . 'ViewTaskComments' . DS . 'view.ctp';
359
 
360
		$result = $this->Task->getContent('index');
361
		$this->assertNotContains('List Test Test.view Task Articles', $result);
362
 
363
		$this->Task->expects($this->once())
364
			->method('createFile')
365
			->with($path, $this->anything());
366
 
367
		$this->Task->bake('view', true);
368
		CakePlugin::unload();
369
	}
370
 
371
/**
372
 * test bake actions baking multiple actions.
373
 *
374
 * @return void
375
 */
376
	public function testBakeActions() {
377
		$this->Task->controllerName = 'ViewTaskComments';
378
 
379
		$this->Task->expects($this->at(0))->method('createFile')
380
			->with(
381
				TMP . 'ViewTaskComments' . DS . 'view.ctp',
382
				$this->stringContains('View Task Comments')
383
			);
384
		$this->Task->expects($this->at(1))->method('createFile')
385
			->with(
386
				TMP . 'ViewTaskComments' . DS . 'edit.ctp',
387
				$this->stringContains('Edit View Task Comment')
388
			);
389
		$this->Task->expects($this->at(2))->method('createFile')
390
			->with(
391
				TMP . 'ViewTaskComments' . DS . 'index.ctp',
392
				$this->stringContains('ViewTaskComment')
393
			);
394
 
395
		$this->Task->bakeActions(array('view', 'edit', 'index'), array());
396
	}
397
 
398
/**
399
 * test baking a customAction (non crud)
400
 *
401
 * @return void
402
 */
403
	public function testCustomAction() {
404
		$this->Task->controllerName = 'ViewTaskComments';
405
 
406
		$this->Task->expects($this->any())->method('in')
407
			->will($this->onConsecutiveCalls('', 'my_action', 'y'));
408
 
409
		$this->Task->expects($this->once())->method('createFile')
410
			->with(
411
				TMP . 'ViewTaskComments' . DS . 'my_action.ctp',
412
				$this->anything()
413
			);
414
 
415
		$this->Task->customAction();
416
	}
417
 
418
/**
419
 * Test all()
420
 *
421
 * @return void
422
 */
423
	public function testExecuteIntoAll() {
424
		$this->Task->args[0] = 'all';
425
 
426
		$this->Task->Controller->expects($this->once())->method('listAll')
427
			->will($this->returnValue(array('view_task_comments')));
428
 
429
		$this->Task->expects($this->at(0))->method('createFile')
430
			->with(
431
				TMP . 'ViewTaskComments' . DS . 'index.ctp',
432
				$this->anything()
433
			);
434
		$this->Task->expects($this->at(1))->method('createFile')
435
			->with(
436
				TMP . 'ViewTaskComments' . DS . 'add.ctp',
437
				$this->anything()
438
			);
439
		$this->Task->expects($this->exactly(2))->method('createFile');
440
 
441
		$this->Task->execute();
442
	}
443
 
444
/**
445
 * Test all() with action parameter
446
 *
447
 * @return void
448
 */
449
	public function testExecuteIntoAllWithActionName() {
450
		$this->Task->args = array('all', 'index');
451
 
452
		$this->Task->Controller->expects($this->once())->method('listAll')
453
			->will($this->returnValue(array('view_task_comments')));
454
 
455
		$this->Task->expects($this->once())->method('createFile')
456
			->with(
457
				TMP . 'ViewTaskComments' . DS . 'index.ctp',
458
				$this->anything()
459
			);
460
 
461
		$this->Task->execute();
462
	}
463
 
464
/**
465
 * test `cake bake view $controller view`
466
 *
467
 * @return void
468
 */
469
	public function testExecuteWithActionParam() {
470
		$this->Task->args[0] = 'ViewTaskComments';
471
		$this->Task->args[1] = 'view';
472
 
473
		$this->Task->expects($this->once())->method('createFile')
474
			->with(
475
				TMP . 'ViewTaskComments' . DS . 'view.ctp',
476
				$this->anything()
477
			);
478
		$this->Task->execute();
479
	}
480
 
481
/**
482
 * test `cake bake view $controller`
483
 * Ensure that views are only baked for actions that exist in the controller.
484
 *
485
 * @return void
486
 */
487
	public function testExecuteWithController() {
488
		$this->Task->args[0] = 'ViewTaskComments';
489
 
490
		$this->Task->expects($this->at(0))->method('createFile')
491
			->with(
492
				TMP . 'ViewTaskComments' . DS . 'index.ctp',
493
				$this->anything()
494
			);
495
		$this->Task->expects($this->at(1))->method('createFile')
496
			->with(
497
				TMP . 'ViewTaskComments' . DS . 'add.ctp',
498
				$this->anything()
499
			);
500
		$this->Task->expects($this->exactly(2))->method('createFile');
501
 
502
		$this->Task->execute();
503
	}
504
 
505
/**
506
 * static dataprovider for test cases
507
 *
508
 * @return void
509
 */
510
	public static function nameVariations() {
511
		return array(array('ViewTaskComments'), array('ViewTaskComment'), array('view_task_comment'));
512
	}
513
 
514
/**
515
 * test that both plural and singular forms can be used for baking views.
516
 *
517
 * @dataProvider nameVariations
518
 * @return void
519
 */
520
	public function testExecuteWithControllerVariations($name) {
521
		$this->Task->args = array($name);
522
 
523
		$this->Task->expects($this->at(0))->method('createFile')
524
			->with(
525
				TMP . 'ViewTaskComments' . DS . 'index.ctp',
526
				$this->anything()
527
			);
528
		$this->Task->expects($this->at(1))->method('createFile')
529
			->with(
530
				TMP . 'ViewTaskComments' . DS . 'add.ctp',
531
				$this->anything()
532
			);
533
		$this->Task->execute();
534
	}
535
 
536
/**
537
 * test `cake bake view $controller --admin`
538
 * Which only bakes admin methods, not non-admin methods.
539
 *
540
 * @return void
541
 */
542
	public function testExecuteWithControllerAndAdminFlag() {
543
		$_back = Configure::read('Routing');
544
		Configure::write('Routing.prefixes', array('admin'));
545
		$this->Task->args[0] = 'ViewTaskArticles';
546
		$this->Task->params['admin'] = 1;
547
 
548
		$this->Task->Project->expects($this->any())->method('getPrefix')->will($this->returnValue('admin_'));
549
 
550
		$this->Task->expects($this->exactly(4))->method('createFile');
551
 
552
		$views = array('admin_index.ctp', 'admin_add.ctp', 'admin_view.ctp', 'admin_edit.ctp');
553
		foreach ($views as $i => $view) {
554
			$this->Task->expects($this->at($i))->method('createFile')
555
				->with(
556
					TMP . 'ViewTaskArticles' . DS . $view,
557
					$this->anything()
558
				);
559
		}
560
		$this->Task->execute();
561
		Configure::write('Routing', $_back);
562
	}
563
 
564
/**
565
 * test execute into interactive.
566
 *
567
 * @return void
568
 */
569
	public function testExecuteInteractive() {
570
		$this->Task->connection = 'test';
571
		$this->Task->args = array();
572
		$this->Task->params = array();
573
 
574
		$this->Task->Controller->expects($this->once())->method('getName')
575
			->will($this->returnValue('ViewTaskComments'));
576
 
577
		$this->Task->expects($this->any())->method('in')
578
			->will($this->onConsecutiveCalls('y', 'y', 'n'));
579
 
580
		$this->Task->expects($this->at(3))->method('createFile')
581
			->with(
582
				TMP . 'ViewTaskComments' . DS . 'index.ctp',
583
				$this->stringContains('ViewTaskComment')
584
			);
585
 
586
		$this->Task->expects($this->at(4))->method('createFile')
587
			->with(
588
				TMP . 'ViewTaskComments' . DS . 'view.ctp',
589
				$this->stringContains('ViewTaskComment')
590
			);
591
 
592
		$this->Task->expects($this->at(5))->method('createFile')
593
			->with(
594
				TMP . 'ViewTaskComments' . DS . 'add.ctp',
595
				$this->stringContains('Add View Task Comment')
596
			);
597
 
598
		$this->Task->expects($this->at(6))->method('createFile')
599
			->with(
600
				TMP . 'ViewTaskComments' . DS . 'edit.ctp',
601
				$this->stringContains('Edit View Task Comment')
602
			);
603
 
604
		$this->Task->expects($this->exactly(4))->method('createFile');
605
		$this->Task->execute();
606
	}
607
 
608
/**
609
 * test `cake bake view posts index list`
610
 *
611
 * @return void
612
 */
613
	public function testExecuteWithAlternateTemplates() {
614
		$this->Task->connection = 'test';
615
		$this->Task->args = array('ViewTaskComments', 'index', 'list');
616
		$this->Task->params = array();
617
 
618
		$this->Task->expects($this->once())->method('createFile')
619
			->with(
620
				TMP . 'ViewTaskComments' . DS . 'list.ctp',
621
				$this->stringContains('ViewTaskComment')
622
			);
623
		$this->Task->execute();
624
	}
625
 
626
/**
627
 * test execute into interactive() with admin methods.
628
 *
629
 * @return void
630
 */
631
	public function testExecuteInteractiveWithAdmin() {
632
		Configure::write('Routing.prefixes', array('admin'));
633
		$this->Task->connection = 'test';
634
		$this->Task->args = array();
635
 
636
		$this->Task->Controller->expects($this->once())->method('getName')
637
			->will($this->returnValue('ViewTaskComments'));
638
 
639
		$this->Task->Project->expects($this->once())->method('getPrefix')
640
			->will($this->returnValue('admin_'));
641
 
642
		$this->Task->expects($this->any())->method('in')
643
			->will($this->onConsecutiveCalls('y', 'n', 'y'));
644
 
645
		$this->Task->expects($this->at(3))->method('createFile')
646
			->with(
647
				TMP . 'ViewTaskComments' . DS . 'admin_index.ctp',
648
				$this->stringContains('ViewTaskComment')
649
			);
650
 
651
		$this->Task->expects($this->at(4))->method('createFile')
652
			->with(
653
				TMP . 'ViewTaskComments' . DS . 'admin_view.ctp',
654
				$this->stringContains('ViewTaskComment')
655
			);
656
 
657
		$this->Task->expects($this->at(5))->method('createFile')
658
			->with(
659
				TMP . 'ViewTaskComments' . DS . 'admin_add.ctp',
660
				$this->stringContains('Add View Task Comment')
661
			);
662
 
663
		$this->Task->expects($this->at(6))->method('createFile')
664
			->with(
665
				TMP . 'ViewTaskComments' . DS . 'admin_edit.ctp',
666
				$this->stringContains('Edit View Task Comment')
667
			);
668
 
669
		$this->Task->expects($this->exactly(4))->method('createFile');
670
		$this->Task->execute();
671
	}
672
 
673
/**
674
 * test getting templates, make sure noTemplateActions works and prefixed template is used before generic one.
675
 *
676
 * @return void
677
 */
678
	public function testGetTemplate() {
679
		$result = $this->Task->getTemplate('delete');
680
		$this->assertFalse($result);
681
 
682
		$result = $this->Task->getTemplate('add');
683
		$this->assertEquals('form', $result);
684
 
685
		Configure::write('Routing.prefixes', array('admin'));
686
 
687
		$result = $this->Task->getTemplate('admin_add');
688
		$this->assertEquals('form', $result);
689
 
690
		$this->Task->Template->templatePaths = array(
691
			'test' => CAKE . 'Test' . DS . 'test_app' . DS . 'Console' . DS . 'Templates' . DS . 'test' . DS
692
		);
693
		$this->Task->Template->params['theme'] = 'test';
694
 
695
		$result = $this->Task->getTemplate('admin_edit');
696
		$this->assertEquals('admin_edit', $result);
697
	}
698
 
699
}