Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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