Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
15403 manish.sha 1
<?php
2
/**
3
 * ViewTest 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.View
15
 * @since         CakePHP(tm) v 1.2.0.4206
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18
 
19
App::uses('View', 'View');
20
App::uses('Helper', 'View');
21
App::uses('Controller', 'Controller');
22
App::uses('CacheHelper', 'View/Helper');
23
App::uses('HtmlHelper', 'View/Helper');
24
App::uses('ErrorHandler', 'Error');
25
App::uses('CakeEventManager', 'Event');
26
App::uses('CakeEventListener', 'Event');
27
 
28
/**
29
 * ViewPostsController class
30
 *
31
 * @package       Cake.Test.Case.View
32
 */
33
class ViewPostsController extends Controller {
34
 
35
/**
36
 * name property
37
 *
38
 * @var string
39
 */
40
	public $name = 'Posts';
41
 
42
/**
43
 * uses property
44
 *
45
 * @var mixed
46
 */
47
	public $uses = null;
48
 
49
/**
50
 * index method
51
 *
52
 * @return void
53
 */
54
	public function index() {
55
		$this->set(array(
56
			'testData' => 'Some test data',
57
			'test2' => 'more data',
58
			'test3' => 'even more data',
59
		));
60
	}
61
 
62
/**
63
 * nocache_tags_with_element method
64
 *
65
 * @return void
66
 */
67
	public function nocache_multiple_element() {
68
		$this->set('foo', 'this is foo var');
69
		$this->set('bar', 'this is bar var');
70
	}
71
 
72
}
73
 
74
/**
75
 * ThemePostsController class
76
 *
77
 * @package       Cake.Test.Case.View
78
 */
79
class ThemePostsController extends Controller {
80
 
81
	public $theme = null;
82
 
83
/**
84
 * index method
85
 *
86
 * @return void
87
 */
88
	public function index() {
89
		$this->set('testData', 'Some test data');
90
		$test2 = 'more data';
91
		$test3 = 'even more data';
92
		$this->set(compact('test2', 'test3'));
93
	}
94
 
95
}
96
 
97
/**
98
 * TestThemeView class
99
 *
100
 * @package       Cake.Test.Case.View
101
 */
102
class TestThemeView extends View {
103
 
104
/**
105
 * renderElement method
106
 *
107
 * @param string $name
108
 * @param array $params
109
 * @return string The given name
110
 */
111
	public function renderElement($name, $params = array()) {
112
		return $name;
113
	}
114
 
115
/**
116
 * getViewFileName method
117
 *
118
 * @param string $name Controller action to find template filename for
119
 * @return string Template filename
120
 */
121
	public function getViewFileName($name = null) {
122
		return $this->_getViewFileName($name);
123
	}
124
 
125
/**
126
 * getLayoutFileName method
127
 *
128
 * @param string $name The name of the layout to find.
129
 * @return string Filename for layout file (.ctp).
130
 */
131
	public function getLayoutFileName($name = null) {
132
		return $this->_getLayoutFileName($name);
133
	}
134
 
135
}
136
 
137
/**
138
 * TestView class
139
 *
140
 * @package       Cake.Test.Case.View
141
 */
142
class TestView extends View {
143
 
144
/**
145
 * getViewFileName method
146
 *
147
 * @param string $name Controller action to find template filename for
148
 * @return string Template filename
149
 */
150
	public function getViewFileName($name = null) {
151
		return $this->_getViewFileName($name);
152
	}
153
 
154
/**
155
 * getLayoutFileName method
156
 *
157
 * @param string $name The name of the layout to find.
158
 * @return string Filename for layout file (.ctp).
159
 */
160
	public function getLayoutFileName($name = null) {
161
		return $this->_getLayoutFileName($name);
162
	}
163
 
164
/**
165
 * paths method
166
 *
167
 * @param string $plugin Optional plugin name to scan for view files.
168
 * @param bool $cached Set to true to force a refresh of view paths.
169
 * @return array paths
170
 */
171
	public function paths($plugin = null, $cached = true) {
172
		return $this->_paths($plugin, $cached);
173
	}
174
 
175
/**
176
 * Test only function to return instance scripts.
177
 *
178
 * @return array Scripts
179
 */
180
	public function scripts() {
181
		return $this->_scripts;
182
	}
183
 
184
}
185
 
186
/**
187
 * TestBeforeAfterHelper class
188
 *
189
 * @package       Cake.Test.Case.View
190
 */
191
class TestBeforeAfterHelper extends Helper {
192
 
193
/**
194
 * property property
195
 *
196
 * @var string
197
 */
198
	public $property = '';
199
 
200
/**
201
 * beforeLayout method
202
 *
203
 * @param string $viewFile
204
 * @return void
205
 */
206
	public function beforeLayout($viewFile) {
207
		$this->property = 'Valuation';
208
	}
209
 
210
/**
211
 * afterLayout method
212
 *
213
 * @param string $layoutFile
214
 * @return void
215
 */
216
	public function afterLayout($layoutFile) {
217
		$this->_View->output .= 'modified in the afterlife';
218
	}
219
 
220
}
221
 
222
/**
223
 * Class TestObjectWithToString
224
 *
225
 * An object with the magic method __toString() for testing with view blocks.
226
 */
227
class TestObjectWithToString {
228
 
229
	public function __toString() {
230
		return "I'm ObjectWithToString";
231
	}
232
 
233
}
234
 
235
/**
236
 * Class TestObjectWithoutToString
237
 *
238
 * An object without the magic method __toString() for testing with view blocks.
239
 */
240
class TestObjectWithoutToString {
241
}
242
 
243
/**
244
 * Class TestViewEventListener
245
 *
246
 * An event listener to test cakePHP events
247
 */
248
class TestViewEventListener implements CakeEventListener {
249
 
250
/**
251
 * type of view before rendering has occurred
252
 *
253
 * @var string
254
 */
255
	public $beforeRenderViewType;
256
 
257
/**
258
 * type of view after rendering has occurred
259
 *
260
 * @var string
261
 */
262
	public $afterRenderViewType;
263
 
264
/**
265
 * implementedEvents method
266
 *
267
 * @return array
268
 */
269
	public function implementedEvents() {
270
		return array(
271
				'View.beforeRender' => 'beforeRender',
272
				'View.afterRender' => 'afterRender'
273
				);
274
	}
275
 
276
/**
277
 * beforeRender method
278
 *
279
 * @param CakeEvent $event the event being sent
280
 * @return void
281
 */
282
	public function beforeRender($event) {
283
		$this->beforeRenderViewType = $event->subject()->getCurrentType();
284
	}
285
 
286
/**
287
 * afterRender method
288
 *
289
 * @param CakeEvent $event the event being sent
290
 * @return void
291
 */
292
	public function afterRender($event) {
293
		$this->afterRenderViewType = $event->subject()->getCurrentType();
294
	}
295
 
296
}
297
 
298
/**
299
 * ViewTest class
300
 *
301
 * @package       Cake.Test.Case.View
302
 */
303
class ViewTest extends CakeTestCase {
304
 
305
/**
306
 * Fixtures used in this test.
307
 *
308
 * @var array
309
 */
310
	public $fixtures = array('core.user', 'core.post');
311
 
312
/**
313
 * setUp method
314
 *
315
 * @return void
316
 */
317
	public function setUp() {
318
		parent::setUp();
319
 
320
		$request = $this->getMock('CakeRequest');
321
		$this->Controller = new Controller($request);
322
		$this->PostsController = new ViewPostsController($request);
323
		$this->PostsController->viewPath = 'Posts';
324
		$this->PostsController->index();
325
		$this->View = new View($this->PostsController);
326
 
327
		$themeRequest = new CakeRequest('posts/index');
328
		$this->ThemeController = new Controller($themeRequest);
329
		$this->ThemePostsController = new ThemePostsController($themeRequest);
330
		$this->ThemePostsController->viewPath = 'posts';
331
		$this->ThemePostsController->index();
332
		$this->ThemeView = new View($this->ThemePostsController);
333
 
334
		App::build(array(
335
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
336
			'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
337
		), App::RESET);
338
		App::objects('plugins', null, false);
339
 
340
		CakePlugin::load(array('TestPlugin', 'TestPlugin', 'PluginJs'));
341
		Configure::write('debug', 2);
342
	}
343
 
344
/**
345
 * tearDown method
346
 *
347
 * @return void
348
 */
349
	public function tearDown() {
350
		parent::tearDown();
351
		CakePlugin::unload();
352
		unset($this->View);
353
		unset($this->PostsController);
354
		unset($this->Controller);
355
		unset($this->ThemeView);
356
		unset($this->ThemePostsController);
357
		unset($this->ThemeController);
358
	}
359
 
360
/**
361
 * Test getViewFileName method
362
 *
363
 * @return void
364
 */
365
	public function testGetTemplate() {
366
		$this->Controller->plugin = null;
367
		$this->Controller->name = 'Pages';
368
		$this->Controller->viewPath = 'Pages';
369
		$this->Controller->action = 'display';
370
		$this->Controller->params['pass'] = array('home');
371
 
372
		$ThemeView = new TestThemeView($this->Controller);
373
		$ThemeView->theme = 'test_theme';
374
		$expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Pages' . DS . 'home.ctp';
375
		$result = $ThemeView->getViewFileName('home');
376
		$this->assertEquals($expected, $result);
377
 
378
		$expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Themed' . DS . 'TestTheme' . DS . 'Posts' . DS . 'index.ctp';
379
		$result = $ThemeView->getViewFileName('/Posts/index');
380
		$this->assertEquals($expected, $result);
381
 
382
		$ThemeView->theme = 'TestTheme';
383
		$expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Themed' . DS . 'TestTheme' . DS . 'Layouts' . DS . 'default.ctp';
384
		$result = $ThemeView->getLayoutFileName();
385
		$this->assertEquals($expected, $result);
386
 
387
		$ThemeView->layoutPath = 'rss';
388
		$expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Layouts' . DS . 'rss' . DS . 'default.ctp';
389
		$result = $ThemeView->getLayoutFileName();
390
		$this->assertEquals($expected, $result);
391
 
392
		$ThemeView->layoutPath = 'Emails' . DS . 'html';
393
		$expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Layouts' . DS . 'Emails' . DS . 'html' . DS . 'default.ctp';
394
		$result = $ThemeView->getLayoutFileName();
395
		$this->assertEquals($expected, $result);
396
	}
397
 
398
/**
399
 * Test getLayoutFileName method on plugin
400
 *
401
 * @return void
402
 */
403
	public function testPluginGetTemplate() {
404
		$this->Controller->plugin = 'TestPlugin';
405
		$this->Controller->name = 'TestPlugin';
406
		$this->Controller->viewPath = 'Tests';
407
		$this->Controller->action = 'index';
408
 
409
		$View = new TestView($this->Controller);
410
 
411
		$expected = CakePlugin::path('TestPlugin') . 'View' . DS . 'Tests' . DS . 'index.ctp';
412
		$result = $View->getViewFileName('index');
413
		$this->assertEquals($expected, $result);
414
 
415
		$expected = CakePlugin::path('TestPlugin') . 'View' . DS . 'Layouts' . DS . 'default.ctp';
416
		$result = $View->getLayoutFileName();
417
		$this->assertEquals($expected, $result);
418
	}
419
 
420
/**
421
 * Test getViewFileName method on plugin
422
 *
423
 * @return void
424
 */
425
	public function testPluginThemedGetTemplate() {
426
		$this->Controller->plugin = 'TestPlugin';
427
		$this->Controller->name = 'TestPlugin';
428
		$this->Controller->viewPath = 'Tests';
429
		$this->Controller->action = 'index';
430
		$this->Controller->theme = 'TestTheme';
431
 
432
		$ThemeView = new TestThemeView($this->Controller);
433
		$expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Themed' . DS . 'TestTheme' . DS . 'Plugin' . DS . 'TestPlugin' . DS . 'Tests' . DS . 'index.ctp';
434
		$result = $ThemeView->getViewFileName('index');
435
		$this->assertEquals($expected, $result);
436
 
437
		$expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Themed' . DS . 'TestTheme' . DS . 'Plugin' . DS . 'TestPlugin' . DS . 'Layouts' . DS . 'plugin_default.ctp';
438
		$result = $ThemeView->getLayoutFileName('plugin_default');
439
		$this->assertEquals($expected, $result);
440
 
441
		$expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Themed' . DS . 'TestTheme' . DS . 'Layouts' . DS . 'default.ctp';
442
		$result = $ThemeView->getLayoutFileName('default');
443
		$this->assertEquals($expected, $result);
444
	}
445
 
446
/**
447
 * Test that plugin/$plugin_name is only appended to the paths it should be.
448
 *
449
 * @return void
450
 */
451
	public function testPluginPathGeneration() {
452
		$this->Controller->plugin = 'TestPlugin';
453
		$this->Controller->name = 'TestPlugin';
454
		$this->Controller->viewPath = 'Tests';
455
		$this->Controller->action = 'index';
456
 
457
		$View = new TestView($this->Controller);
458
		$paths = $View->paths();
459
		$expected = array_merge(App::path('View'), App::core('View'), App::core('Console/Templates/skel/View'));
460
		$this->assertEquals($expected, $paths);
461
 
462
		$paths = $View->paths('TestPlugin');
463
		$pluginPath = CakePlugin::path('TestPlugin');
464
		$expected = array(
465
			CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Plugin' . DS . 'TestPlugin' . DS,
466
			$pluginPath . 'View' . DS,
467
			CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS,
468
			CAKE . 'View' . DS,
469
			CAKE . 'Console' . DS . 'Templates' . DS . 'skel' . DS . 'View' . DS
470
		);
471
		$this->assertEquals($expected, $paths);
472
	}
473
 
474
/**
475
 * Test that CamelCase'd plugins still find their view files.
476
 *
477
 * @return void
478
 */
479
	public function testCamelCasePluginGetTemplate() {
480
		$this->Controller->plugin = 'TestPlugin';
481
		$this->Controller->name = 'TestPlugin';
482
		$this->Controller->viewPath = 'Tests';
483
		$this->Controller->action = 'index';
484
 
485
		$View = new TestView($this->Controller);
486
		App::build(array(
487
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
488
			'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
489
		));
490
 
491
		$pluginPath = CakePlugin::path('TestPlugin');
492
		$expected = CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS . 'TestPlugin' . DS . 'View' . DS . 'Tests' . DS . 'index.ctp';
493
		$result = $View->getViewFileName('index');
494
		$this->assertEquals($expected, $result);
495
 
496
		$expected = $pluginPath . 'View' . DS . 'Layouts' . DS . 'default.ctp';
497
		$result = $View->getLayoutFileName();
498
		$this->assertEquals($expected, $result);
499
	}
500
 
501
/**
502
 * Test getViewFileName method
503
 *
504
 * @return void
505
 */
506
	public function testGetViewFileNames() {
507
		$this->Controller->plugin = null;
508
		$this->Controller->name = 'Pages';
509
		$this->Controller->viewPath = 'Pages';
510
		$this->Controller->action = 'display';
511
		$this->Controller->params['pass'] = array('home');
512
 
513
		$View = new TestView($this->Controller);
514
 
515
		$expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Pages' . DS . 'home.ctp';
516
		$result = $View->getViewFileName('home');
517
		$this->assertEquals($expected, $result);
518
 
519
		$expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Posts' . DS . 'index.ctp';
520
		$result = $View->getViewFileName('/Posts/index');
521
		$this->assertEquals($expected, $result);
522
 
523
		$expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Posts' . DS . 'index.ctp';
524
		$result = $View->getViewFileName('../Posts/index');
525
		$this->assertEquals($expected, $result);
526
 
527
		$expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Pages' . DS . 'page.home.ctp';
528
		$result = $View->getViewFileName('page.home');
529
		$this->assertEquals($expected, $result, 'Should not ruin files with dots.');
530
 
531
		CakePlugin::load('TestPlugin');
532
		$expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Pages' . DS . 'home.ctp';
533
		$result = $View->getViewFileName('TestPlugin.home');
534
		$this->assertEquals($expected, $result, 'Plugin is missing the view, cascade to app.');
535
 
536
		$View->viewPath = 'Tests';
537
		$expected = CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS . 'TestPlugin' . DS . 'View' . DS . 'Tests' . DS . 'index.ctp';
538
		$result = $View->getViewFileName('TestPlugin.index');
539
		$this->assertEquals($expected, $result);
540
	}
541
 
542
/**
543
 * Test getting layout filenames
544
 *
545
 * @return void
546
 */
547
	public function testGetLayoutFileName() {
548
		$this->Controller->plugin = null;
549
		$this->Controller->name = 'Pages';
550
		$this->Controller->viewPath = 'Pages';
551
		$this->Controller->action = 'display';
552
 
553
		$View = new TestView($this->Controller);
554
 
555
		$expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Layouts' . DS . 'default.ctp';
556
		$result = $View->getLayoutFileName();
557
		$this->assertEquals($expected, $result);
558
 
559
		$View->layoutPath = 'rss';
560
		$expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Layouts' . DS . 'rss' . DS . 'default.ctp';
561
		$result = $View->getLayoutFileName();
562
		$this->assertEquals($expected, $result);
563
 
564
		$View->layoutPath = 'Emails' . DS . 'html';
565
		$expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Layouts' . DS . 'Emails' . DS . 'html' . DS . 'default.ctp';
566
		$result = $View->getLayoutFileName();
567
		$this->assertEquals($expected, $result);
568
	}
569
 
570
/**
571
 * Test getting layout filenames for plugins.
572
 *
573
 * @return void
574
 */
575
	public function testGetLayoutFileNamePlugin() {
576
		$this->Controller->plugin = null;
577
		$this->Controller->name = 'Pages';
578
		$this->Controller->viewPath = 'Pages';
579
		$this->Controller->action = 'display';
580
 
581
		$View = new TestView($this->Controller);
582
		CakePlugin::load('TestPlugin');
583
 
584
		$expected = CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS . 'TestPlugin' . DS . 'View' . DS . 'Layouts' . DS . 'default.ctp';
585
		$result = $View->getLayoutFileName('TestPlugin.default');
586
		$this->assertEquals($expected, $result);
587
 
588
		$View->plugin = 'TestPlugin';
589
		$expected = CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS . 'TestPlugin' . DS . 'View' . DS . 'Layouts' . DS . 'default.ctp';
590
		$result = $View->getLayoutFileName('default');
591
		$this->assertEquals($expected, $result);
592
	}
593
 
594
/**
595
 * Test for missing views
596
 *
597
 * @expectedException MissingViewException
598
 * @return void
599
 */
600
	public function testMissingView() {
601
		$this->Controller->plugin = null;
602
		$this->Controller->name = 'Pages';
603
		$this->Controller->viewPath = 'Pages';
604
		$this->Controller->action = 'display';
605
		$this->Controller->params['pass'] = array('home');
606
 
607
		$View = new TestView($this->Controller);
608
		ob_start();
609
		$View->getViewFileName('does_not_exist');
610
 
611
		$this->ThemeController->plugin = null;
612
		$this->ThemeController->name = 'Pages';
613
		$this->ThemeController->viewPath = 'Pages';
614
		$this->ThemeController->action = 'display';
615
		$this->ThemeController->theme = 'my_theme';
616
 
617
		$this->ThemeController->params['pass'] = array('home');
618
 
619
		$View = new TestThemeView($this->ThemeController);
620
		$View->getViewFileName('does_not_exist');
621
	}
622
 
623
/**
624
 * Test for missing layouts
625
 *
626
 * @expectedException MissingLayoutException
627
 * @return void
628
 */
629
	public function testMissingLayout() {
630
		$this->Controller->plugin = null;
631
		$this->Controller->name = 'Posts';
632
		$this->Controller->viewPath = 'Posts';
633
		$this->Controller->layout = 'whatever';
634
 
635
		$View = new TestView($this->Controller);
636
		ob_start();
637
		$View->getLayoutFileName();
638
		ob_get_clean();
639
 
640
		$this->ThemeController->plugin = null;
641
		$this->ThemeController->name = 'Posts';
642
		$this->ThemeController->viewPath = 'posts';
643
		$this->ThemeController->layout = 'whatever';
644
		$this->ThemeController->theme = 'my_theme';
645
 
646
		$View = new TestThemeView($this->ThemeController);
647
		$View->getLayoutFileName();
648
	}
649
 
650
/**
651
 * Test viewVars method
652
 *
653
 * @return void
654
 */
655
	public function testViewVars() {
656
		$this->assertEquals(array('testData' => 'Some test data', 'test2' => 'more data', 'test3' => 'even more data'), $this->View->viewVars);
657
	}
658
 
659
/**
660
 * Test generation of UUIDs method
661
 *
662
 * @return void
663
 */
664
	public function testUUIDGeneration() {
665
		$result = $this->View->uuid('form', array('controller' => 'posts', 'action' => 'index'));
666
		$this->assertEquals('form5988016017', $result);
667
		$result = $this->View->uuid('form', array('controller' => 'posts', 'action' => 'index'));
668
		$this->assertEquals('formc3dc6be854', $result);
669
		$result = $this->View->uuid('form', array('controller' => 'posts', 'action' => 'index'));
670
		$this->assertEquals('form28f92cc87f', $result);
671
	}
672
 
673
/**
674
 * Test addInlineScripts method
675
 *
676
 * @return void
677
 */
678
	public function testAddInlineScripts() {
679
		$View = new TestView($this->Controller);
680
		$View->addScript('prototype.js');
681
		$View->addScript('prototype.js');
682
		$this->assertEquals(array('prototype.js'), $View->scripts());
683
 
684
		$View->addScript('mainEvent', 'Event.observe(window, "load", function() { doSomething(); }, true);');
685
		$this->assertEquals(array('prototype.js', 'mainEvent' => 'Event.observe(window, "load", function() { doSomething(); }, true);'), $View->scripts());
686
	}
687
 
688
/**
689
 * Test elementExists method
690
 *
691
 * @return void
692
 */
693
	public function testElementExists() {
694
		$result = $this->View->elementExists('test_element');
695
		$this->assertTrue($result);
696
 
697
		$result = $this->View->elementExists('TestPlugin.plugin_element');
698
		$this->assertTrue($result);
699
 
700
		$result = $this->View->elementExists('non_existent_element');
701
		$this->assertFalse($result);
702
 
703
		$result = $this->View->elementExists('TestPlugin.element');
704
		$this->assertFalse($result);
705
 
706
		$this->View->plugin = 'TestPlugin';
707
		$result = $this->View->elementExists('test_plugin_element');
708
		$this->assertTrue($result);
709
	}
710
 
711
/**
712
 * Test element method
713
 *
714
 * @return void
715
 */
716
	public function testElement() {
717
		$result = $this->View->element('test_element');
718
		$this->assertEquals('this is the test element', $result);
719
 
720
		$result = $this->View->element('plugin_element', array(), array('plugin' => 'TestPlugin'));
721
		$this->assertEquals('this is the plugin element using params[plugin]', $result);
722
 
723
		$result = $this->View->element('plugin_element', array(), array('plugin' => 'test_plugin'));
724
		$this->assertEquals('this is the plugin element using params[plugin]', $result);
725
 
726
		$result = $this->View->element('TestPlugin.plugin_element');
727
		$this->assertEquals('this is the plugin element using params[plugin]', $result);
728
 
729
		$this->View->plugin = 'TestPlugin';
730
		$result = $this->View->element('test_plugin_element');
731
		$this->assertEquals('this is the test set using View::$plugin plugin element', $result);
732
	}
733
 
734
/**
735
 * Test elementInexistent method
736
 *
737
 * @expectedException PHPUnit_Framework_Error_Notice
738
 * @return void
739
 */
740
	public function testElementInexistent() {
741
		$this->View->element('non_existent_element');
742
	}
743
 
744
/**
745
 * Test elementInexistent2 method
746
 *
747
 * @expectedException PHPUnit_Framework_Error_Notice
748
 * @return void
749
 */
750
	public function testElementInexistent2() {
751
		$this->View->element('TestPlugin.plugin_element', array(), array('plugin' => 'test_plugin'));
752
	}
753
 
754
/**
755
 * Test elementInexistent3 method
756
 *
757
 * @expectedException PHPUnit_Framework_Error_Notice
758
 * @return void
759
 */
760
	public function testElementInexistent3() {
761
		$this->View->element('test_plugin.plugin_element');
762
	}
763
 
764
/**
765
 * Test that elements can have callbacks
766
 *
767
 * @return void
768
 */
769
	public function testElementCallbacks() {
770
		$Helper = $this->getMock('Helper', array(), array($this->View), 'ElementCallbackMockHtmlHelper');
771
		$this->View->helpers = array('ElementCallbackMockHtml');
772
		$this->View->loadHelpers();
773
 
774
		$this->View->Helpers->set('ElementCallbackMockHtml', $Helper);
775
		$this->View->ElementCallbackMockHtml = $Helper;
776
 
777
		$this->View->ElementCallbackMockHtml->expects($this->at(0))->method('beforeRender');
778
		$this->View->ElementCallbackMockHtml->expects($this->at(1))->method('afterRender');
779
 
780
		$this->View->element('test_element', array(), array('callbacks' => true));
781
	}
782
 
783
/**
784
 * Test that additional element viewVars don't get overwritten with helpers.
785
 *
786
 * @return void
787
 */
788
	public function testElementParamsDontOverwriteHelpers() {
789
		$Controller = new ViewPostsController();
790
		$Controller->helpers = array('Form');
791
 
792
		$View = new View($Controller);
793
		$result = $View->element('type_check', array('form' => 'string'), array('callbacks' => true));
794
		$this->assertEquals('string', $result);
795
 
796
		$View->set('form', 'string');
797
		$result = $View->element('type_check', array(), array('callbacks' => true));
798
		$this->assertEquals('string', $result);
799
	}
800
 
801
/**
802
 * Test elementCacheHelperNoCache method
803
 *
804
 * @return void
805
 */
806
	public function testElementCacheHelperNoCache() {
807
		$Controller = new ViewPostsController();
808
		$View = new TestView($Controller);
809
		$View->loadHelpers();
810
		$result = $View->element('test_element', array('ram' => 'val', 'test' => array('foo', 'bar')));
811
		$this->assertEquals('this is the test element', $result);
812
	}
813
 
814
/**
815
 * Test elementCache method
816
 *
817
 * @return void
818
 */
819
	public function testElementCache() {
820
		Cache::drop('test_view');
821
		Cache::config('test_view', array(
822
			'engine' => 'File',
823
			'duration' => '+1 day',
824
			'path' => CACHE . 'views' . DS,
825
			'prefix' => ''
826
		));
827
		Cache::clear(true, 'test_view');
828
 
829
		$View = new TestView($this->PostsController);
830
		$View->elementCache = 'test_view';
831
 
832
		$result = $View->element('test_element', array(), array('cache' => true));
833
		$expected = 'this is the test element';
834
		$this->assertEquals($expected, $result);
835
 
836
		$result = Cache::read('element__test_element_cache_callbacks', 'test_view');
837
		$this->assertEquals($expected, $result);
838
 
839
		$result = $View->element('test_element', array('param' => 'one', 'foo' => 'two'), array('cache' => true));
840
		$this->assertEquals($expected, $result);
841
 
842
		$result = Cache::read('element__test_element_cache_callbacks_param_foo', 'test_view');
843
		$this->assertEquals($expected, $result);
844
 
845
		$View->element('test_element', array(
846
			'param' => 'one',
847
			'foo' => 'two'
848
		), array(
849
			'cache' => array('key' => 'custom_key')
850
		));
851
		$result = Cache::read('element_custom_key', 'test_view');
852
		$this->assertEquals($expected, $result);
853
 
854
		$View->elementCache = 'default';
855
		$View->element('test_element', array(
856
			'param' => 'one',
857
			'foo' => 'two'
858
		), array(
859
			'cache' => array('config' => 'test_view'),
860
		));
861
		$result = Cache::read('element__test_element_cache_callbacks_param_foo', 'test_view');
862
		$this->assertEquals($expected, $result);
863
 
864
		Cache::clear(true, 'test_view');
865
		Cache::drop('test_view');
866
	}
867
 
868
/**
869
 * Test element events
870
 *
871
 * @return void
872
 */
873
	public function testViewEvent() {
874
		$View = new View($this->PostsController);
875
		$View->autoLayout = false;
876
		$listener = new TestViewEventListener();
877
 
878
		$View->getEventManager()->attach($listener);
879
 
880
		$View->render('index');
881
		$this->assertEquals(View::TYPE_VIEW, $listener->beforeRenderViewType);
882
		$this->assertEquals(View::TYPE_VIEW, $listener->afterRenderViewType);
883
 
884
		$this->assertEquals($View->getCurrentType(), View::TYPE_VIEW);
885
		$View->element('test_element', array(), array('callbacks' => true));
886
		$this->assertEquals($View->getCurrentType(), View::TYPE_VIEW);
887
 
888
		$this->assertEquals(View::TYPE_ELEMENT, $listener->beforeRenderViewType);
889
		$this->assertEquals(View::TYPE_ELEMENT, $listener->afterRenderViewType);
890
	}
891
 
892
/**
893
 * Test __get allowing access to helpers.
894
 *
895
 * @return void
896
 */
897
	public function testMagicGet() {
898
		$View = new View($this->PostsController);
899
		$View->loadHelper('Html');
900
		$this->assertInstanceOf('HtmlHelper', $View->Html);
901
	}
902
 
903
/**
904
 * Test that ctp is used as a fallback file extension for elements
905
 *
906
 * @return void
907
 */
908
	public function testElementCtpFallback() {
909
		$View = new TestView($this->PostsController);
910
		$View->ext = '.missing';
911
		$element = 'test_element';
912
		$expected = 'this is the test element';
913
		$result = $View->element($element);
914
 
915
		$this->assertEquals($expected, $result);
916
	}
917
 
918
/**
919
 * Test loadHelpers method
920
 *
921
 * @return void
922
 */
923
	public function testLoadHelpers() {
924
		$View = new View($this->PostsController);
925
 
926
		$View->helpers = array('Html', 'Form');
927
		$View->loadHelpers();
928
 
929
		$this->assertInstanceOf('HtmlHelper', $View->Html, 'Object type is wrong.');
930
		$this->assertInstanceOf('FormHelper', $View->Form, 'Object type is wrong.');
931
	}
932
 
933
/**
934
 * Test lazy loading helpers
935
 *
936
 * @return void
937
 */
938
	public function testLazyLoadHelpers() {
939
		$View = new View($this->PostsController);
940
 
941
		$View->helpers = array();
942
		$this->assertInstanceOf('HtmlHelper', $View->Html, 'Object type is wrong.');
943
		$this->assertInstanceOf('FormHelper', $View->Form, 'Object type is wrong.');
944
	}
945
 
946
/**
947
 * Test the correct triggering of helper callbacks
948
 *
949
 * @return void
950
 */
951
	public function testHelperCallbackTriggering() {
952
		$View = new View($this->PostsController);
953
		$View->helpers = array();
954
		$View->Helpers = $this->getMock('HelperCollection', array('trigger'), array($View));
955
 
956
		$View->Helpers->expects($this->at(0))->method('trigger')
957
			->with(
958
				$this->logicalAnd(
959
					$this->isInstanceOf('CakeEvent'),
960
					$this->attributeEqualTo('_name', 'View.beforeRender'),
961
					$this->attributeEqualTo('_subject', $View)
962
				)
963
			);
964
		$View->Helpers->expects($this->at(1))->method('trigger')
965
			->with(
966
				$this->logicalAnd(
967
					$this->isInstanceOf('CakeEvent'),
968
					$this->attributeEqualTo('_name', 'View.beforeRenderFile'),
969
					$this->attributeEqualTo('_subject', $View)
970
				)
971
			);
972
 
973
		$View->Helpers->expects($this->at(2))->method('trigger')
974
			->with(
975
				$this->logicalAnd(
976
					$this->isInstanceOf('CakeEvent'),
977
					$this->attributeEqualTo('_name', 'View.afterRenderFile'),
978
					$this->attributeEqualTo('_subject', $View)
979
				)
980
			);
981
		$View->Helpers->expects($this->at(3))->method('trigger')
982
			->with(
983
				$this->logicalAnd(
984
					$this->isInstanceOf('CakeEvent'),
985
					$this->attributeEqualTo('_name', 'View.afterRender'),
986
					$this->attributeEqualTo('_subject', $View)
987
				)
988
			);
989
 
990
		$View->Helpers->expects($this->at(4))->method('trigger')
991
			->with(
992
				$this->logicalAnd(
993
					$this->isInstanceOf('CakeEvent'),
994
					$this->attributeEqualTo('_name', 'View.beforeLayout'),
995
					$this->attributeEqualTo('_subject', $View)
996
				)
997
			);
998
 
999
		$View->Helpers->expects($this->at(5))->method('trigger')
1000
			->with(
1001
				$this->logicalAnd(
1002
					$this->isInstanceOf('CakeEvent'),
1003
					$this->attributeEqualTo('_name', 'View.beforeRenderFile'),
1004
					$this->attributeEqualTo('_subject', $View)
1005
				)
1006
			);
1007
 
1008
		$View->Helpers->expects($this->at(6))->method('trigger')
1009
			->with(
1010
				$this->logicalAnd(
1011
					$this->isInstanceOf('CakeEvent'),
1012
					$this->attributeEqualTo('_name', 'View.afterRenderFile'),
1013
					$this->attributeEqualTo('_subject', $View)
1014
				)
1015
			);
1016
 
1017
		$View->Helpers->expects($this->at(7))->method('trigger')
1018
			->with(
1019
				$this->logicalAnd(
1020
					$this->isInstanceOf('CakeEvent'),
1021
					$this->attributeEqualTo('_name', 'View.afterLayout'),
1022
					$this->attributeEqualTo('_subject', $View)
1023
				)
1024
			);
1025
 
1026
		$View->render('index');
1027
	}
1028
 
1029
/**
1030
 * Test beforeLayout method
1031
 *
1032
 * @return void
1033
 */
1034
	public function testBeforeLayout() {
1035
		$this->PostsController->helpers = array('Session', 'TestBeforeAfter', 'Html');
1036
		$View = new View($this->PostsController);
1037
		$View->render('index');
1038
		$this->assertEquals('Valuation', $View->Helpers->TestBeforeAfter->property);
1039
	}
1040
 
1041
/**
1042
 * Test afterLayout method
1043
 *
1044
 * @return void
1045
 */
1046
	public function testAfterLayout() {
1047
		$this->PostsController->helpers = array('Session', 'TestBeforeAfter', 'Html');
1048
		$this->PostsController->set('variable', 'values');
1049
 
1050
		$View = new View($this->PostsController);
1051
		ClassRegistry::addObject('afterView', $View);
1052
 
1053
		$content = 'This is my view output';
1054
		$result = $View->renderLayout($content, 'default');
1055
		$this->assertRegExp('/modified in the afterlife/', $result);
1056
		$this->assertRegExp('/This is my view output/', $result);
1057
	}
1058
 
1059
/**
1060
 * Test renderLoadHelper method
1061
 *
1062
 * @return void
1063
 */
1064
	public function testRenderLoadHelper() {
1065
		$this->PostsController->helpers = array('Session', 'Html', 'Form', 'Number');
1066
		$View = new TestView($this->PostsController);
1067
 
1068
		$result = $View->render('index', false);
1069
		$this->assertEquals('posts index', $result);
1070
 
1071
		$attached = $View->Helpers->loaded();
1072
		$this->assertEquals(array('Session', 'Html', 'Form', 'Number'), $attached);
1073
 
1074
		$this->PostsController->helpers = array('Html', 'Form', 'Number', 'TestPlugin.PluggedHelper');
1075
		$View = new TestView($this->PostsController);
1076
 
1077
		$result = $View->render('index', false);
1078
		$this->assertEquals('posts index', $result);
1079
 
1080
		$attached = $View->Helpers->loaded();
1081
		$expected = array('Html', 'Form', 'Number', 'PluggedHelper');
1082
		$this->assertEquals($expected, $attached, 'Attached helpers are wrong.');
1083
	}
1084
 
1085
/**
1086
 * Test render method
1087
 *
1088
 * @return void
1089
 */
1090
	public function testRender() {
1091
		$View = new TestView($this->PostsController);
1092
		$result = $View->render('index');
1093
 
1094
		$this->assertRegExp("/<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/>\s*<title>/", $result);
1095
		$this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result);
1096
		$this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result);
1097
 
1098
		$this->assertTrue(isset($View->viewVars['content_for_layout']), 'content_for_layout should be a view var');
1099
		$this->assertTrue(isset($View->viewVars['scripts_for_layout']), 'scripts_for_layout should be a view var');
1100
 
1101
		$this->PostsController->set('url', 'flash');
1102
		$this->PostsController->set('message', 'yo what up');
1103
		$this->PostsController->set('pause', 3);
1104
		$this->PostsController->set('pageTitle', 'yo what up');
1105
 
1106
		$View = new TestView($this->PostsController);
1107
		$result = $View->render(false, 'flash');
1108
 
1109
		$this->assertRegExp("/<title>yo what up<\/title>/", $result);
1110
		$this->assertRegExp("/<p><a href=\"flash\">yo what up<\/a><\/p>/", $result);
1111
 
1112
		$this->assertNull($View->render(false, 'flash'));
1113
 
1114
		$this->PostsController->helpers = array('Session', 'Cache', 'Html');
1115
		$this->PostsController->constructClasses();
1116
		$this->PostsController->cacheAction = array('index' => 3600);
1117
		$this->PostsController->request->params['action'] = 'index';
1118
		Configure::write('Cache.check', true);
1119
 
1120
		$View = new TestView($this->PostsController);
1121
		$result = $View->render('index');
1122
 
1123
		$this->assertRegExp("/<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/>\s*<title>/", $result);
1124
		$this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result);
1125
		$this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result);
1126
	}
1127
 
1128
/**
1129
 * Test that View::$view works
1130
 *
1131
 * @return void
1132
 */
1133
	public function testRenderUsingViewProperty() {
1134
		$this->PostsController->view = 'cache_form';
1135
		$View = new TestView($this->PostsController);
1136
 
1137
		$this->assertEquals('cache_form', $View->view);
1138
		$result = $View->render();
1139
		$this->assertRegExp('/Add User/', $result);
1140
	}
1141
 
1142
/**
1143
 * Test render()ing a file in a subdir from a custom viewPath
1144
 * in a plugin.
1145
 *
1146
 * @return void
1147
 */
1148
	public function testGetViewFileNameSubdirWithPluginAndViewPath() {
1149
		$this->PostsController->plugin = 'TestPlugin';
1150
		$this->PostsController->viewPath = 'Elements';
1151
		$this->PostsController->name = 'Posts';
1152
		$View = new TestView($this->PostsController);
1153
 
1154
		$expected = CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS . 'TestPlugin' .
1155
			DS . 'View' . DS . 'Elements' . DS . 'sub_dir' . DS . 'sub_element.ctp';
1156
		$this->assertEquals($expected, $View->getViewFileName('sub_dir/sub_element'));
1157
	}
1158
 
1159
/**
1160
 * Test that view vars can replace the local helper variables
1161
 * and not overwrite the $this->Helper references
1162
 *
1163
 * @return void
1164
 */
1165
	public function testViewVarOverwritingLocalHelperVar() {
1166
		$Controller = new ViewPostsController();
1167
		$Controller->helpers = array('Session', 'Html');
1168
		$Controller->set('html', 'I am some test html');
1169
		$View = new View($Controller);
1170
		$result = $View->render('helper_overwrite', false);
1171
 
1172
		$this->assertRegExp('/I am some test html/', $result);
1173
		$this->assertRegExp('/Test link/', $result);
1174
	}
1175
 
1176
/**
1177
 * Test getViewFileName method
1178
 *
1179
 * @return void
1180
 */
1181
	public function testViewFileName() {
1182
		$View = new TestView($this->PostsController);
1183
 
1184
		$result = $View->getViewFileName('index');
1185
		$this->assertRegExp('/Posts(\/|\\\)index.ctp/', $result);
1186
 
1187
		$result = $View->getViewFileName('TestPlugin.index');
1188
		$this->assertRegExp('/Posts(\/|\\\)index.ctp/', $result);
1189
 
1190
		$result = $View->getViewFileName('/Pages/home');
1191
		$this->assertRegExp('/Pages(\/|\\\)home.ctp/', $result);
1192
 
1193
		$result = $View->getViewFileName('../Elements/test_element');
1194
		$this->assertRegExp('/Elements(\/|\\\)test_element.ctp/', $result);
1195
 
1196
		$result = $View->getViewFileName('../Themed/TestTheme/Posts/index');
1197
		$this->assertRegExp('/Themed(\/|\\\)TestTheme(\/|\\\)Posts(\/|\\\)index.ctp/', $result);
1198
 
1199
		$expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Posts' . DS . 'index.ctp';
1200
		$result = $View->getViewFileName('../Posts/index');
1201
		$this->assertEquals($expected, $result);
1202
	}
1203
 
1204
/**
1205
 * Test renderCache method
1206
 *
1207
 * @return void
1208
 */
1209
	public function testRenderCache() {
1210
		$this->skipIf(!is_writable(CACHE . 'views' . DS), 'CACHE/views dir is not writable, cannot test renderCache.');
1211
 
1212
		$view = 'test_view';
1213
		$View = new View($this->PostsController);
1214
		$path = CACHE . 'views' . DS . 'view_cache_' . $view;
1215
 
1216
		$cacheText = '<!--cachetime:' . time() . '-->some cacheText';
1217
		$f = fopen($path, 'w+');
1218
		fwrite($f, $cacheText);
1219
		fclose($f);
1220
 
1221
		$result = $View->renderCache($path, '+1 second');
1222
		$this->assertFalse($result);
1223
		if (file_exists($path)) {
1224
			unlink($path);
1225
		}
1226
 
1227
		$cacheText = '<!--cachetime:' . (time() + 10) . '-->some cacheText';
1228
		$f = fopen($path, 'w+');
1229
		fwrite($f, $cacheText);
1230
		fclose($f);
1231
		$result = $View->renderCache($path, '+1 second');
1232
 
1233
		$this->assertRegExp('/^some cacheText/', $result);
1234
 
1235
		if (file_exists($path)) {
1236
			unlink($path);
1237
		}
1238
	}
1239
 
1240
/**
1241
 * Test that render() will remove the cake:nocache tags when only the cachehelper is present.
1242
 *
1243
 * @return void
1244
 */
1245
	public function testRenderStrippingNoCacheTagsOnlyCacheHelper() {
1246
		Configure::write('Cache.check', false);
1247
		$View = new View($this->PostsController);
1248
		$View->set(array('superman' => 'clark', 'variable' => 'var'));
1249
		$View->helpers = array('Html', 'Form', 'Cache');
1250
		$View->layout = 'cache_layout';
1251
		$result = $View->render('index');
1252
		$this->assertNotRegExp('/cake:nocache/', $result);
1253
	}
1254
 
1255
/**
1256
 * Test that render() will remove the cake:nocache tags when only the Cache.check is true.
1257
 *
1258
 * @return void
1259
 */
1260
	public function testRenderStrippingNoCacheTagsOnlyCacheCheck() {
1261
		Configure::write('Cache.check', true);
1262
		$View = new View($this->PostsController);
1263
		$View->set(array('superman' => 'clark', 'variable' => 'var'));
1264
		$View->helpers = array('Html', 'Form');
1265
		$View->layout = 'cache_layout';
1266
		$result = $View->render('index');
1267
		$this->assertNotRegExp('/cake:nocache/', $result);
1268
	}
1269
 
1270
/**
1271
 * testSet method
1272
 *
1273
 * @return void
1274
 */
1275
	public function testSet() {
1276
		$View = new TestView($this->PostsController);
1277
		$View->viewVars = array();
1278
		$View->set('somekey', 'someValue');
1279
		$this->assertSame($View->viewVars, array('somekey' => 'someValue'));
1280
		$this->assertSame($View->getVars(), array('somekey'));
1281
 
1282
		$View->viewVars = array();
1283
		$keys = array('key1', 'key2');
1284
		$values = array('value1', 'value2');
1285
		$View->set($keys, $values);
1286
		$this->assertSame($View->viewVars, array('key1' => 'value1', 'key2' => 'value2'));
1287
		$this->assertSame($View->getVars(), array('key1', 'key2'));
1288
		$this->assertSame($View->getVar('key1'), 'value1');
1289
		$this->assertNull($View->getVar('key3'));
1290
 
1291
		$View->set(array('key3' => 'value3'));
1292
		$this->assertSame($View->getVar('key3'), 'value3');
1293
 
1294
		$View->viewVars = array();
1295
		$View->set(array(3 => 'three', 4 => 'four'));
1296
		$View->set(array(1 => 'one', 2 => 'two'));
1297
		$expected = array(3 => 'three', 4 => 'four', 1 => 'one', 2 => 'two');
1298
		$this->assertEquals($expected, $View->viewVars);
1299
	}
1300
 
1301
/**
1302
 * testBadExt method
1303
 *
1304
 * @expectedException MissingViewException
1305
 * @return void
1306
 */
1307
	public function testBadExt() {
1308
		$this->PostsController->action = 'something';
1309
		$this->PostsController->ext = '.whatever';
1310
 
1311
		$View = new TestView($this->PostsController);
1312
		$View->render('this_is_missing');
1313
	}
1314
 
1315
/**
1316
 * testAltExt method
1317
 *
1318
 * @return void
1319
 */
1320
	public function testAltExt() {
1321
		$this->PostsController->ext = '.alt';
1322
		$View = new TestView($this->PostsController);
1323
		$result = $View->render('alt_ext', false);
1324
		$this->assertEquals('alt ext', $result);
1325
	}
1326
 
1327
/**
1328
 * testAltBadExt method
1329
 *
1330
 * @expectedException MissingViewException
1331
 * @return void
1332
 */
1333
	public function testAltBadExt() {
1334
		$View = new TestView($this->PostsController);
1335
		$View->render('alt_ext');
1336
	}
1337
 
1338
/**
1339
 * Test creating a block with capturing output.
1340
 *
1341
 * @return void
1342
 */
1343
	public function testBlockCapture() {
1344
		$this->View->start('test');
1345
		echo 'Block content';
1346
		$this->View->end();
1347
 
1348
		$result = $this->View->fetch('test');
1349
		$this->assertEquals('Block content', $result);
1350
	}
1351
 
1352
/**
1353
 * Test block with startIfEmpty
1354
 *
1355
 * @return void
1356
 */
1357
	public function testBlockCaptureStartIfEmpty() {
1358
		$this->View->startIfEmpty('test');
1359
		echo "Block content 1";
1360
		$this->View->end();
1361
 
1362
		$this->View->startIfEmpty('test');
1363
		echo "Block content 2";
1364
		$this->View->end();
1365
 
1366
		$result = $this->View->fetch('test');
1367
		$this->assertEquals('Block content 1', $result);
1368
	}
1369
 
1370
/**
1371
 * Test block with startIfEmpty
1372
 *
1373
 * @return void
1374
 */
1375
	public function testBlockCaptureStartStartIfEmpty() {
1376
		$this->View->start('test');
1377
		echo "Block content 1";
1378
		$this->View->end();
1379
 
1380
		$this->View->startIfEmpty('test');
1381
		echo "Block content 2";
1382
		$this->View->end();
1383
 
1384
		$result = $this->View->fetch('test');
1385
		$this->assertEquals('Block content 1', $result);
1386
	}
1387
 
1388
/**
1389
 * Test appending to a block with capturing output.
1390
 *
1391
 * @return void
1392
 */
1393
	public function testBlockCaptureAppend() {
1394
		$this->View->start('test');
1395
		echo 'Block';
1396
		$this->View->end();
1397
 
1398
		$this->View->append('test');
1399
		echo ' content';
1400
		$this->View->end();
1401
 
1402
		$result = $this->View->fetch('test');
1403
		$this->assertEquals('Block content', $result);
1404
	}
1405
 
1406
/**
1407
 * Test setting a block's content.
1408
 *
1409
 * @return void
1410
 */
1411
	public function testBlockSet() {
1412
		$this->View->assign('test', 'Block content');
1413
		$result = $this->View->fetch('test');
1414
		$this->assertEquals('Block content', $result);
1415
	}
1416
 
1417
/**
1418
 * Test resetting a block's content.
1419
 *
1420
 * @return void
1421
 */
1422
	public function testBlockReset() {
1423
		$this->View->assign('test', '');
1424
		$result = $this->View->fetch('test', 'This should not be returned');
1425
		$this->assertSame('', $result);
1426
	}
1427
 
1428
/**
1429
 * Test setting a block's content to null
1430
 *
1431
 * @return void
1432
 * @link https://cakephp.lighthouseapp.com/projects/42648/tickets/3938-this-redirectthis-auth-redirecturl-broken
1433
 */
1434
	public function testBlockSetNull() {
1435
		$this->View->assign('testWithNull', null);
1436
		$result = $this->View->fetch('testWithNull');
1437
		$this->assertSame('', $result);
1438
	}
1439
 
1440
/**
1441
 * Test setting a block's content to an object with __toString magic method
1442
 *
1443
 * @return void
1444
 */
1445
	public function testBlockSetObjectWithToString() {
1446
		$objectWithToString = new TestObjectWithToString();
1447
		$this->View->assign('testWithObjectWithToString', $objectWithToString);
1448
		$result = $this->View->fetch('testWithObjectWithToString');
1449
		$this->assertSame("I'm ObjectWithToString", $result);
1450
	}
1451
 
1452
/**
1453
 * Test setting a block's content to an object without __toString magic method
1454
 *
1455
 * This should produce a "Object of class TestObjectWithoutToString could not be converted to string" error
1456
 * which gets thrown as a PHPUnit_Framework_Error Exception by PHPUnit.
1457
 *
1458
 * @expectedException PHPUnit_Framework_Error
1459
 * @return void
1460
 */
1461
	public function testBlockSetObjectWithoutToString() {
1462
		$objectWithToString = new TestObjectWithoutToString();
1463
		$this->View->assign('testWithObjectWithoutToString', $objectWithToString);
1464
	}
1465
 
1466
/**
1467
 * Test setting a block's content to a decimal
1468
 *
1469
 * @return void
1470
 */
1471
	public function testBlockSetDecimal() {
1472
		$this->View->assign('testWithDecimal', 1.23456789);
1473
		$result = $this->View->fetch('testWithDecimal');
1474
		$this->assertEquals('1.23456789', $result);
1475
	}
1476
 
1477
/**
1478
 * Data provider for block related tests.
1479
 *
1480
 * @return array
1481
 */
1482
	public static function blockValueProvider() {
1483
		return array(
1484
			'string' => array('A string value'),
1485
			'null' => array(null),
1486
			'decimal' => array(1.23456),
1487
			'object with __toString' => array(new TestObjectWithToString()),
1488
		);
1489
	}
1490
 
1491
/**
1492
 * Test appending to a block with append.
1493
 *
1494
 * @dataProvider blockValueProvider
1495
 * @return void
1496
 */
1497
	public function testBlockAppend($value) {
1498
		$this->View->assign('testBlock', 'Block');
1499
		$this->View->append('testBlock', $value);
1500
 
1501
		$result = $this->View->fetch('testBlock');
1502
		$this->assertSame('Block' . $value, $result);
1503
	}
1504
 
1505
/**
1506
 * Test appending an object without __toString magic method to a block with append.
1507
 *
1508
 * This should produce a "Object of class TestObjectWithoutToString could not be converted to string" error
1509
 * which gets thrown as a PHPUnit_Framework_Error Exception by PHPUnit.
1510
 *
1511
 * @expectedException PHPUnit_Framework_Error
1512
 * @return void
1513
 */
1514
	public function testBlockAppendObjectWithoutToString() {
1515
		$object = new TestObjectWithoutToString();
1516
		$this->View->assign('testBlock', 'Block ');
1517
		$this->View->append('testBlock', $object);
1518
	}
1519
 
1520
/**
1521
 * Test prepending to a block with prepend.
1522
 *
1523
 * @dataProvider blockValueProvider
1524
 * @return void
1525
 */
1526
	public function testBlockPrepend($value) {
1527
		$this->View->assign('test', 'Block');
1528
		$this->View->prepend('test', $value);
1529
 
1530
		$result = $this->View->fetch('test');
1531
		$this->assertEquals($value . 'Block', $result);
1532
	}
1533
 
1534
/**
1535
 * Test prepending an object without __toString magic method to a block with prepend.
1536
 *
1537
 * This should produce a "Object of class TestObjectWithoutToString could not be converted to string" error
1538
 * which gets thrown as a PHPUnit_Framework_Error Exception by PHPUnit.
1539
 *
1540
 * @expectedException PHPUnit_Framework_Error
1541
 * @return void
1542
 */
1543
	public function testBlockPrependObjectWithoutToString() {
1544
		$object = new TestObjectWithoutToString();
1545
		$this->View->assign('test', 'Block ');
1546
		$this->View->prepend('test', $object);
1547
	}
1548
 
1549
/**
1550
 * You should be able to append to undefined blocks.
1551
 *
1552
 * @return void
1553
 */
1554
	public function testBlockAppendUndefined() {
1555
		$this->View->append('test', 'Unknown');
1556
		$result = $this->View->fetch('test');
1557
		$this->assertEquals('Unknown', $result);
1558
	}
1559
 
1560
/**
1561
 * You should be able to prepend to undefined blocks.
1562
 *
1563
 * @return void
1564
 */
1565
	public function testBlockPrependUndefined() {
1566
		$this->View->prepend('test', 'Unknown');
1567
		$result = $this->View->fetch('test');
1568
		$this->assertEquals('Unknown', $result);
1569
	}
1570
 
1571
/**
1572
 * Test getting block names
1573
 *
1574
 * @return void
1575
 */
1576
	public function testBlocks() {
1577
		$this->View->append('test', 'one');
1578
		$this->View->assign('test1', 'one');
1579
 
1580
		$this->assertEquals(array('test', 'test1'), $this->View->blocks());
1581
	}
1582
 
1583
/**
1584
 * Test that blocks can be nested.
1585
 *
1586
 * @return void
1587
 */
1588
	public function testNestedBlocks() {
1589
		$this->View->start('first');
1590
		echo 'In first ';
1591
		$this->View->start('second');
1592
		echo 'In second';
1593
		$this->View->end();
1594
		echo 'In first';
1595
		$this->View->end();
1596
 
1597
		$this->assertEquals('In first In first', $this->View->fetch('first'));
1598
		$this->assertEquals('In second', $this->View->fetch('second'));
1599
	}
1600
 
1601
/**
1602
 * Test that starting the same block twice throws an exception
1603
 *
1604
 * @expectedException CakeException
1605
 * @return void
1606
 */
1607
	public function testStartBlocksTwice() {
1608
		$this->View->start('first');
1609
		echo 'In first ';
1610
		$this->View->start('second');
1611
		echo 'In second';
1612
		$this->View->start('first');
1613
	}
1614
 
1615
/**
1616
 * Test that an exception gets thrown when you leave a block open at the end
1617
 * of a view.
1618
 *
1619
 * @expectedException CakeException
1620
 * @return void
1621
 */
1622
	public function testExceptionOnOpenBlock() {
1623
		$this->View->render('open_block');
1624
	}
1625
 
1626
/**
1627
 * Test nested extended views.
1628
 *
1629
 * @return void
1630
 */
1631
	public function testExtendNested() {
1632
		$this->View->layout = false;
1633
		$content = $this->View->render('nested_extends');
1634
		$expected = <<<TEXT
1635
This is the second parent.
1636
This is the first parent.
1637
This is the first template.
1638
Sidebar Content.
1639
TEXT;
1640
		$this->assertEquals($expected, $content);
1641
	}
1642
 
1643
/**
1644
 * Make sure that extending the current view with itself causes an exception
1645
 *
1646
 * @expectedException LogicException
1647
 * @return void
1648
 */
1649
	public function testExtendSelf() {
1650
		$this->View->layout = false;
1651
		$this->View->render('extend_self');
1652
	}
1653
 
1654
/**
1655
 * Make sure that extending in a loop causes an exception
1656
 *
1657
 * @expectedException LogicException
1658
 * @return void
1659
 */
1660
	public function testExtendLoop() {
1661
		$this->View->layout = false;
1662
		$this->View->render('extend_loop');
1663
	}
1664
 
1665
/**
1666
 * Test extend() in an element and a view.
1667
 *
1668
 * @return void
1669
 */
1670
	public function testExtendElement() {
1671
		$this->View->layout = false;
1672
		$content = $this->View->render('extend_element');
1673
		$expected = <<<TEXT
1674
Parent View.
1675
View content.
1676
Parent Element.
1677
Element content.
1678
 
1679
TEXT;
1680
		$this->assertEquals($expected, $content);
1681
	}
1682
 
1683
/**
1684
 * Extending an element which doesn't exist should throw a missing view exception
1685
 *
1686
 * @expectedException LogicException
1687
 * @return void
1688
 */
1689
	public function testExtendMissingElement() {
1690
		$this->View->layout = false;
1691
		$this->View->render('extend_missing_element');
1692
	}
1693
 
1694
/**
1695
 * Test extend() preceeded by an element()
1696
 *
1697
 * @return void
1698
 */
1699
	public function testExtendWithElementBeforeExtend() {
1700
		$this->View->layout = false;
1701
		$result = $this->View->render('extend_with_element');
1702
		$expected = <<<TEXT
1703
Parent View.
1704
this is the test elementThe view
1705
 
1706
TEXT;
1707
		$this->assertEquals($expected, $result);
1708
	}
1709
 
1710
/**
1711
 * Test that setting arbitrary properties still works.
1712
 *
1713
 * @return void
1714
 */
1715
	public function testPropertySettingMagicGet() {
1716
		$this->assertFalse(isset($this->View->action));
1717
		$this->View->request->params['action'] = 'login';
1718
		$this->assertEquals('login', $this->View->action);
1719
		$this->assertTrue(isset($this->View->action));
1720
		$this->assertTrue(!empty($this->View->action));
1721
	}
1722
 
1723
/**
1724
 * Test memory leaks that existed in _paths at one point.
1725
 *
1726
 * @return void
1727
 */
1728
	public function testMemoryLeakInPaths() {
1729
		$this->ThemeController->plugin = null;
1730
		$this->ThemeController->name = 'Posts';
1731
		$this->ThemeController->viewPath = 'posts';
1732
		$this->ThemeController->layout = 'whatever';
1733
		$this->ThemeController->theme = 'TestTheme';
1734
 
1735
		$View = new View($this->ThemeController);
1736
		$View->element('test_element');
1737
 
1738
		$start = memory_get_usage();
1739
		for ($i = 0; $i < 10; $i++) {
1740
			$View->element('test_element');
1741
		}
1742
		$end = memory_get_usage();
1743
		$this->assertLessThanOrEqual($start + 5000, $end);
1744
	}
1745
 
1746
/**
1747
 * Tests that a view block uses default value when not assigned and uses assigned value when it is
1748
 *
1749
 * @return void
1750
 */
1751
	public function testBlockDefaultValue() {
1752
		$default = 'Default';
1753
		$result = $this->View->fetch('title', $default);
1754
		$this->assertEquals($default, $result);
1755
 
1756
		$expected = 'My Title';
1757
		$this->View->assign('title', $expected);
1758
		$result = $this->View->fetch('title', $default);
1759
		$this->assertEquals($expected, $result);
1760
	}
1761
 
1762
/**
1763
 * Tests that a view variable uses default value when not assigned and uses assigned value when it is
1764
 *
1765
 * @return void
1766
 */
1767
	public function testViewVarDefaultValue() {
1768
		$default = 'Default';
1769
		$result = $this->View->get('title', $default);
1770
		$this->assertEquals($default, $result);
1771
 
1772
		$expected = 'Back to the Future';
1773
		$this->View->set('title', $expected);
1774
		$result = $this->View->get('title', $default);
1775
		$this->assertEquals($expected, $result);
1776
	}
1777
}