Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 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 null
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 boolean $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
	public function testElementCallbacks() {
711
		$this->getMock('Helper', array(), array($this->View), 'ElementCallbackMockHtmlHelper');
712
		$this->View->helpers = array('ElementCallbackMockHtml');
713
		$this->View->loadHelpers();
714
 
715
		$this->View->ElementCallbackMockHtml->expects($this->at(0))->method('beforeRender');
716
		$this->View->ElementCallbackMockHtml->expects($this->at(1))->method('afterRender');
717
 
718
		$this->View->element('test_element', array(), array('callbacks' => true));
719
		$this->mockObjects[] = $this->View->ElementCallbackMockHtml;
720
	}
721
 
722
/**
723
 * Test that additional element viewVars don't get overwritten with helpers.
724
 *
725
 * @return void
726
 */
727
	public function testElementParamsDontOverwriteHelpers() {
728
		$Controller = new ViewPostsController();
729
		$Controller->helpers = array('Form');
730
 
731
		$View = new View($Controller);
732
		$result = $View->element('type_check', array('form' => 'string'), array('callbacks' => true));
733
		$this->assertEquals('string', $result);
734
 
735
		$View->set('form', 'string');
736
		$result = $View->element('type_check', array(), array('callbacks' => true));
737
		$this->assertEquals('string', $result);
738
	}
739
 
740
/**
741
 * Test elementCacheHelperNoCache method
742
 *
743
 * @return void
744
 */
745
	public function testElementCacheHelperNoCache() {
746
		$Controller = new ViewPostsController();
747
		$View = new TestView($Controller);
748
		$View->loadHelpers();
749
		$result = $View->element('test_element', array('ram' => 'val', 'test' => array('foo', 'bar')));
750
		$this->assertEquals('this is the test element', $result);
751
	}
752
 
753
/**
754
 * Test elementCache method
755
 *
756
 * @return void
757
 */
758
	public function testElementCache() {
759
		Cache::drop('test_view');
760
		Cache::config('test_view', array(
761
			'engine' => 'File',
762
			'duration' => '+1 day',
763
			'path' => CACHE . 'views' . DS,
764
			'prefix' => ''
765
		));
766
		Cache::clear(true, 'test_view');
767
 
768
		$View = new TestView($this->PostsController);
769
		$View->elementCache = 'test_view';
770
 
771
		$result = $View->element('test_element', array(), array('cache' => true));
772
		$expected = 'this is the test element';
773
		$this->assertEquals($expected, $result);
774
 
775
		$result = Cache::read('element__test_element_cache_callbacks', 'test_view');
776
		$this->assertEquals($expected, $result);
777
 
778
		$result = $View->element('test_element', array('param' => 'one', 'foo' => 'two'), array('cache' => true));
779
		$this->assertEquals($expected, $result);
780
 
781
		$result = Cache::read('element__test_element_cache_callbacks_param_foo', 'test_view');
782
		$this->assertEquals($expected, $result);
783
 
784
		$View->element('test_element', array(
785
			'param' => 'one',
786
			'foo' => 'two'
787
		), array(
788
			'cache' => array('key' => 'custom_key')
789
		));
790
		$result = Cache::read('element_custom_key', 'test_view');
791
		$this->assertEquals($expected, $result);
792
 
793
		$View->elementCache = 'default';
794
		$View->element('test_element', array(
795
			'param' => 'one',
796
			'foo' => 'two'
797
		), array(
798
			'cache' => array('config' => 'test_view'),
799
		));
800
		$result = Cache::read('element__test_element_cache_callbacks_param_foo', 'test_view');
801
		$this->assertEquals($expected, $result);
802
 
803
		Cache::clear(true, 'test_view');
804
		Cache::drop('test_view');
805
	}
806
 
807
/**
808
 * Test __get allowing access to helpers.
809
 *
810
 * @return void
811
 */
812
	public function testMagicGet() {
813
		$View = new View($this->PostsController);
814
		$View->loadHelper('Html');
815
		$this->assertInstanceOf('HtmlHelper', $View->Html);
816
	}
817
 
818
/**
819
 * Test that ctp is used as a fallback file extension for elements
820
 *
821
 * @return void
822
 */
823
	public function testElementCtpFallback() {
824
		$View = new TestView($this->PostsController);
825
		$View->ext = '.missing';
826
		$element = 'test_element';
827
		$expected = 'this is the test element';
828
		$result = $View->element($element);
829
 
830
		$this->assertEquals($expected, $result);
831
	}
832
 
833
/**
834
 * Test loadHelpers method
835
 *
836
 * @return void
837
 */
838
	public function testLoadHelpers() {
839
		$View = new View($this->PostsController);
840
 
841
		$View->helpers = array('Html', 'Form');
842
		$View->loadHelpers();
843
 
844
		$this->assertInstanceOf('HtmlHelper', $View->Html, 'Object type is wrong.');
845
		$this->assertInstanceOf('FormHelper', $View->Form, 'Object type is wrong.');
846
	}
847
 
848
/**
849
 * Test lazy loading helpers
850
 *
851
 * @return void
852
 */
853
	public function testLazyLoadHelpers() {
854
		$View = new View($this->PostsController);
855
 
856
		$View->helpers = array();
857
		$this->assertInstanceOf('HtmlHelper', $View->Html, 'Object type is wrong.');
858
		$this->assertInstanceOf('FormHelper', $View->Form, 'Object type is wrong.');
859
	}
860
 
861
/**
862
 * Test the correct triggering of helper callbacks
863
 *
864
 * @return void
865
 */
866
	public function testHelperCallbackTriggering() {
867
		$View = new View($this->PostsController);
868
		$View->helpers = array();
869
		$View->Helpers = $this->getMock('HelperCollection', array('trigger'), array($View));
870
 
871
		$View->Helpers->expects($this->at(0))->method('trigger')
872
			->with(
873
				$this->logicalAnd(
874
					$this->isInstanceOf('CakeEvent'),
875
					$this->attributeEqualTo('_name', 'View.beforeRender'),
876
					$this->attributeEqualTo('_subject', $View)
877
				)
878
			);
879
		$View->Helpers->expects($this->at(1))->method('trigger')
880
			->with(
881
				$this->logicalAnd(
882
					$this->isInstanceOf('CakeEvent'),
883
					$this->attributeEqualTo('_name', 'View.beforeRenderFile'),
884
					$this->attributeEqualTo('_subject', $View)
885
				)
886
			);
887
 
888
		$View->Helpers->expects($this->at(2))->method('trigger')
889
			->with(
890
				$this->logicalAnd(
891
					$this->isInstanceOf('CakeEvent'),
892
					$this->attributeEqualTo('_name', 'View.afterRenderFile'),
893
					$this->attributeEqualTo('_subject', $View)
894
				)
895
			);
896
		$View->Helpers->expects($this->at(3))->method('trigger')
897
			->with(
898
				$this->logicalAnd(
899
					$this->isInstanceOf('CakeEvent'),
900
					$this->attributeEqualTo('_name', 'View.afterRender'),
901
					$this->attributeEqualTo('_subject', $View)
902
				)
903
			);
904
 
905
		$View->Helpers->expects($this->at(4))->method('trigger')
906
			->with(
907
				$this->logicalAnd(
908
					$this->isInstanceOf('CakeEvent'),
909
					$this->attributeEqualTo('_name', 'View.beforeLayout'),
910
					$this->attributeEqualTo('_subject', $View)
911
				)
912
			);
913
 
914
		$View->Helpers->expects($this->at(5))->method('trigger')
915
			->with(
916
				$this->logicalAnd(
917
					$this->isInstanceOf('CakeEvent'),
918
					$this->attributeEqualTo('_name', 'View.beforeRenderFile'),
919
					$this->attributeEqualTo('_subject', $View)
920
				)
921
			);
922
 
923
		$View->Helpers->expects($this->at(6))->method('trigger')
924
			->with(
925
				$this->logicalAnd(
926
					$this->isInstanceOf('CakeEvent'),
927
					$this->attributeEqualTo('_name', 'View.afterRenderFile'),
928
					$this->attributeEqualTo('_subject', $View)
929
				)
930
			);
931
 
932
		$View->Helpers->expects($this->at(7))->method('trigger')
933
			->with(
934
				$this->logicalAnd(
935
					$this->isInstanceOf('CakeEvent'),
936
					$this->attributeEqualTo('_name', 'View.afterLayout'),
937
					$this->attributeEqualTo('_subject', $View)
938
				)
939
			);
940
 
941
		$View->render('index');
942
	}
943
 
944
/**
945
 * Test beforeLayout method
946
 *
947
 * @return void
948
 */
949
	public function testBeforeLayout() {
950
		$this->PostsController->helpers = array('Session', 'TestBeforeAfter', 'Html');
951
		$View = new View($this->PostsController);
952
		$View->render('index');
953
		$this->assertEquals('Valuation', $View->Helpers->TestBeforeAfter->property);
954
	}
955
 
956
/**
957
 * Test afterLayout method
958
 *
959
 * @return void
960
 */
961
	public function testAfterLayout() {
962
		$this->PostsController->helpers = array('Session', 'TestBeforeAfter', 'Html');
963
		$this->PostsController->set('variable', 'values');
964
 
965
		$View = new View($this->PostsController);
966
		ClassRegistry::addObject('afterView', $View);
967
 
968
		$content = 'This is my view output';
969
		$result = $View->renderLayout($content, 'default');
970
		$this->assertRegExp('/modified in the afterlife/', $result);
971
		$this->assertRegExp('/This is my view output/', $result);
972
	}
973
 
974
/**
975
 * Test renderLoadHelper method
976
 *
977
 * @return void
978
 */
979
	public function testRenderLoadHelper() {
980
		$this->PostsController->helpers = array('Session', 'Html', 'Form', 'Number');
981
		$View = new TestView($this->PostsController);
982
 
983
		$result = $View->render('index', false);
984
		$this->assertEquals('posts index', $result);
985
 
986
		$attached = $View->Helpers->loaded();
987
		$this->assertEquals(array('Session', 'Html', 'Form', 'Number'), $attached);
988
 
989
		$this->PostsController->helpers = array('Html', 'Form', 'Number', 'TestPlugin.PluggedHelper');
990
		$View = new TestView($this->PostsController);
991
 
992
		$result = $View->render('index', false);
993
		$this->assertEquals('posts index', $result);
994
 
995
		$attached = $View->Helpers->loaded();
996
		$expected = array('Html', 'Form', 'Number', 'PluggedHelper');
997
		$this->assertEquals($expected, $attached, 'Attached helpers are wrong.');
998
	}
999
 
1000
/**
1001
 * Test render method
1002
 *
1003
 * @return void
1004
 */
1005
	public function testRender() {
1006
		$View = new TestView($this->PostsController);
1007
		$result = $View->render('index');
1008
 
1009
		$this->assertRegExp("/<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/>\s*<title>/", $result);
1010
		$this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result);
1011
		$this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result);
1012
 
1013
		$this->assertTrue(isset($View->viewVars['content_for_layout']), 'content_for_layout should be a view var');
1014
		$this->assertTrue(isset($View->viewVars['scripts_for_layout']), 'scripts_for_layout should be a view var');
1015
 
1016
		$this->PostsController->set('url', 'flash');
1017
		$this->PostsController->set('message', 'yo what up');
1018
		$this->PostsController->set('pause', 3);
1019
		$this->PostsController->set('page_title', 'yo what up');
1020
 
1021
		$View = new TestView($this->PostsController);
1022
		$result = $View->render(false, 'flash');
1023
 
1024
		$this->assertRegExp("/<title>yo what up<\/title>/", $result);
1025
		$this->assertRegExp("/<p><a href=\"flash\">yo what up<\/a><\/p>/", $result);
1026
 
1027
		$this->assertTrue($View->render(false, 'flash'));
1028
 
1029
		$this->PostsController->helpers = array('Session', 'Cache', 'Html');
1030
		$this->PostsController->constructClasses();
1031
		$this->PostsController->cacheAction = array('index' => 3600);
1032
		$this->PostsController->request->params['action'] = 'index';
1033
		Configure::write('Cache.check', true);
1034
 
1035
		$View = new TestView($this->PostsController);
1036
		$result = $View->render('index');
1037
 
1038
		$this->assertRegExp("/<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/>\s*<title>/", $result);
1039
		$this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result);
1040
		$this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result);
1041
	}
1042
 
1043
/**
1044
 * Test that View::$view works
1045
 *
1046
 * @return void
1047
 */
1048
	public function testRenderUsingViewProperty() {
1049
		$this->PostsController->view = 'cache_form';
1050
		$View = new TestView($this->PostsController);
1051
 
1052
		$this->assertEquals('cache_form', $View->view);
1053
		$result = $View->render();
1054
		$this->assertRegExp('/Add User/', $result);
1055
	}
1056
 
1057
/**
1058
 * Test render()ing a file in a subdir from a custom viewPath
1059
 * in a plugin.
1060
 *
1061
 * @return void
1062
 */
1063
	public function testGetViewFileNameSubdirWithPluginAndViewPath() {
1064
		$this->PostsController->plugin = 'TestPlugin';
1065
		$this->PostsController->viewPath = 'Elements';
1066
		$this->PostsController->name = 'Posts';
1067
		$View = new TestView($this->PostsController);
1068
 
1069
		$expected = CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS . 'TestPlugin' .
1070
			DS . 'View' . DS . 'Elements' . DS . 'sub_dir' . DS . 'sub_element.ctp';
1071
		$this->assertEquals($expected, $View->getViewFileName('sub_dir/sub_element'));
1072
	}
1073
 
1074
/**
1075
 * Test that view vars can replace the local helper variables
1076
 * and not overwrite the $this->Helper references
1077
 *
1078
 * @return void
1079
 */
1080
	public function testViewVarOverwritingLocalHelperVar() {
1081
		$Controller = new ViewPostsController();
1082
		$Controller->helpers = array('Session', 'Html');
1083
		$Controller->set('html', 'I am some test html');
1084
		$View = new View($Controller);
1085
		$result = $View->render('helper_overwrite', false);
1086
 
1087
		$this->assertRegExp('/I am some test html/', $result);
1088
		$this->assertRegExp('/Test link/', $result);
1089
	}
1090
 
1091
/**
1092
 * Test getViewFileName method
1093
 *
1094
 * @return void
1095
 */
1096
	public function testViewFileName() {
1097
		$View = new TestView($this->PostsController);
1098
 
1099
		$result = $View->getViewFileName('index');
1100
		$this->assertRegExp('/Posts(\/|\\\)index.ctp/', $result);
1101
 
1102
		$result = $View->getViewFileName('TestPlugin.index');
1103
		$this->assertRegExp('/Posts(\/|\\\)index.ctp/', $result);
1104
 
1105
		$result = $View->getViewFileName('/Pages/home');
1106
		$this->assertRegExp('/Pages(\/|\\\)home.ctp/', $result);
1107
 
1108
		$result = $View->getViewFileName('../Elements/test_element');
1109
		$this->assertRegExp('/Elements(\/|\\\)test_element.ctp/', $result);
1110
 
1111
		$result = $View->getViewFileName('../Themed/TestTheme/Posts/index');
1112
		$this->assertRegExp('/Themed(\/|\\\)TestTheme(\/|\\\)Posts(\/|\\\)index.ctp/', $result);
1113
 
1114
		$expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Posts' . DS . 'index.ctp';
1115
		$result = $View->getViewFileName('../Posts/index');
1116
		$this->assertEquals($expected, $result);
1117
	}
1118
 
1119
/**
1120
 * Test renderCache method
1121
 *
1122
 * @return void
1123
 */
1124
	public function testRenderCache() {
1125
		$this->skipIf(!is_writable(CACHE . 'views' . DS), 'CACHE/views dir is not writable, cannot test renderCache.');
1126
 
1127
		$view = 'test_view';
1128
		$View = new View($this->PostsController);
1129
		$path = CACHE . 'views' . DS . 'view_cache_' . $view;
1130
 
1131
		$cacheText = '<!--cachetime:' . time() . '-->some cacheText';
1132
		$f = fopen($path, 'w+');
1133
		fwrite($f, $cacheText);
1134
		fclose($f);
1135
 
1136
		$result = $View->renderCache($path, '+1 second');
1137
		$this->assertFalse($result);
1138
		if (file_exists($path)) {
1139
			unlink($path);
1140
		}
1141
 
1142
		$cacheText = '<!--cachetime:' . (time() + 10) . '-->some cacheText';
1143
		$f = fopen($path, 'w+');
1144
		fwrite($f, $cacheText);
1145
		fclose($f);
1146
		$result = $View->renderCache($path, '+1 second');
1147
 
1148
		$this->assertRegExp('/^some cacheText/', $result);
1149
 
1150
		if (file_exists($path)) {
1151
			unlink($path);
1152
		}
1153
	}
1154
 
1155
/**
1156
 * Test that render() will remove the cake:nocache tags when only the cachehelper is present.
1157
 *
1158
 * @return void
1159
 */
1160
	public function testRenderStrippingNoCacheTagsOnlyCacheHelper() {
1161
		Configure::write('Cache.check', false);
1162
		$View = new View($this->PostsController);
1163
		$View->set(array('superman' => 'clark', 'variable' => 'var'));
1164
		$View->helpers = array('Html', 'Form', 'Cache');
1165
		$View->layout = 'cache_layout';
1166
		$result = $View->render('index');
1167
		$this->assertNotRegExp('/cake:nocache/', $result);
1168
	}
1169
 
1170
/**
1171
 * Test that render() will remove the cake:nocache tags when only the Cache.check is true.
1172
 *
1173
 * @return void
1174
 */
1175
	public function testRenderStrippingNoCacheTagsOnlyCacheCheck() {
1176
		Configure::write('Cache.check', true);
1177
		$View = new View($this->PostsController);
1178
		$View->set(array('superman' => 'clark', 'variable' => 'var'));
1179
		$View->helpers = array('Html', 'Form');
1180
		$View->layout = 'cache_layout';
1181
		$result = $View->render('index');
1182
		$this->assertNotRegExp('/cake:nocache/', $result);
1183
	}
1184
 
1185
/**
1186
 * testSet method
1187
 *
1188
 * @return void
1189
 */
1190
	public function testSet() {
1191
		$View = new TestView($this->PostsController);
1192
		$View->viewVars = array();
1193
		$View->set('somekey', 'someValue');
1194
		$this->assertSame($View->viewVars, array('somekey' => 'someValue'));
1195
		$this->assertSame($View->getVars(), array('somekey'));
1196
 
1197
		$View->viewVars = array();
1198
		$keys = array('key1', 'key2');
1199
		$values = array('value1', 'value2');
1200
		$View->set($keys, $values);
1201
		$this->assertSame($View->viewVars, array('key1' => 'value1', 'key2' => 'value2'));
1202
		$this->assertSame($View->getVars(), array('key1', 'key2'));
1203
		$this->assertSame($View->getVar('key1'), 'value1');
1204
		$this->assertNull($View->getVar('key3'));
1205
 
1206
		$View->set(array('key3' => 'value3'));
1207
		$this->assertSame($View->getVar('key3'), 'value3');
1208
 
1209
		$View->viewVars = array();
1210
		$View->set(array(3 => 'three', 4 => 'four'));
1211
		$View->set(array(1 => 'one', 2 => 'two'));
1212
		$expected = array(3 => 'three', 4 => 'four', 1 => 'one', 2 => 'two');
1213
		$this->assertEquals($expected, $View->viewVars);
1214
	}
1215
 
1216
/**
1217
 * testBadExt method
1218
 *
1219
 * @expectedException MissingViewException
1220
 * @return void
1221
 */
1222
	public function testBadExt() {
1223
		$this->PostsController->action = 'something';
1224
		$this->PostsController->ext = '.whatever';
1225
 
1226
		$View = new TestView($this->PostsController);
1227
		$View->render('this_is_missing');
1228
	}
1229
 
1230
/**
1231
 * testAltExt method
1232
 *
1233
 * @return void
1234
 */
1235
	public function testAltExt() {
1236
		$this->PostsController->ext = '.alt';
1237
		$View = new TestView($this->PostsController);
1238
		$result = $View->render('alt_ext', false);
1239
		$this->assertEquals('alt ext', $result);
1240
	}
1241
 
1242
/**
1243
 * testAltBadExt method
1244
 *
1245
 * @expectedException MissingViewException
1246
 * @return void
1247
 */
1248
	public function testAltBadExt() {
1249
		$View = new TestView($this->PostsController);
1250
		$View->render('alt_ext');
1251
	}
1252
 
1253
/**
1254
 * Test creating a block with capturing output.
1255
 *
1256
 * @return void
1257
 */
1258
	public function testBlockCapture() {
1259
		$this->View->start('test');
1260
		echo 'Block content';
1261
		$this->View->end();
1262
 
1263
		$result = $this->View->fetch('test');
1264
		$this->assertEquals('Block content', $result);
1265
	}
1266
 
1267
/**
1268
 * Test block with startIfEmpty
1269
 *
1270
 * @return void
1271
 */
1272
	public function testBlockCaptureStartIfEmpty() {
1273
		$this->View->startIfEmpty('test');
1274
		echo "Block content 1";
1275
		$this->View->end();
1276
 
1277
		$this->View->startIfEmpty('test');
1278
		echo "Block content 2";
1279
		$this->View->end();
1280
 
1281
		$result = $this->View->fetch('test');
1282
		$this->assertEquals('Block content 1', $result);
1283
	}
1284
 
1285
/**
1286
 * Test block with startIfEmpty
1287
 *
1288
 * @return void
1289
 */
1290
	public function testBlockCaptureStartStartIfEmpty() {
1291
		$this->View->start('test');
1292
		echo "Block content 1";
1293
		$this->View->end();
1294
 
1295
		$this->View->startIfEmpty('test');
1296
		echo "Block content 2";
1297
		$this->View->end();
1298
 
1299
		$result = $this->View->fetch('test');
1300
		$this->assertEquals('Block content 1', $result);
1301
	}
1302
 
1303
/**
1304
 * Test appending to a block with capturing output.
1305
 *
1306
 * @return void
1307
 */
1308
	public function testBlockCaptureAppend() {
1309
		$this->View->start('test');
1310
		echo 'Block';
1311
		$this->View->end();
1312
 
1313
		$this->View->append('test');
1314
		echo ' content';
1315
		$this->View->end();
1316
 
1317
		$result = $this->View->fetch('test');
1318
		$this->assertEquals('Block content', $result);
1319
	}
1320
 
1321
/**
1322
 * Test setting a block's content.
1323
 *
1324
 * @return void
1325
 */
1326
	public function testBlockSet() {
1327
		$this->View->assign('test', 'Block content');
1328
		$result = $this->View->fetch('test');
1329
		$this->assertEquals('Block content', $result);
1330
	}
1331
 
1332
/**
1333
 * Test resetting a block's content.
1334
 *
1335
 * @return void
1336
 */
1337
	public function testBlockReset() {
1338
		$this->View->assign('test', '');
1339
		$result = $this->View->fetch('test', 'This should not be returned');
1340
		$this->assertSame('', $result);
1341
	}
1342
 
1343
/**
1344
 * Test setting a block's content to null
1345
 *
1346
 * @return void
1347
 * @link https://cakephp.lighthouseapp.com/projects/42648/tickets/3938-this-redirectthis-auth-redirecturl-broken
1348
 */
1349
	public function testBlockSetNull() {
1350
		$this->View->assign('testWithNull', null);
1351
		$result = $this->View->fetch('testWithNull');
1352
		$this->assertSame('', $result);
1353
	}
1354
 
1355
/**
1356
 * Test setting a block's content to an object with __toString magic method
1357
 *
1358
 * @return void
1359
 */
1360
	public function testBlockSetObjectWithToString() {
1361
		$objectWithToString = new TestObjectWithToString();
1362
		$this->View->assign('testWithObjectWithToString', $objectWithToString);
1363
		$result = $this->View->fetch('testWithObjectWithToString');
1364
		$this->assertSame("I'm ObjectWithToString", $result);
1365
	}
1366
 
1367
/**
1368
 * Test setting a block's content to an object without __toString magic method
1369
 *
1370
 * This should produce a "Object of class TestObjectWithoutToString could not be converted to string" error
1371
 * which gets thrown as a PHPUnit_Framework_Error Exception by PHPUnit.
1372
 *
1373
 * @expectedException PHPUnit_Framework_Error
1374
 * @return void
1375
 */
1376
	public function testBlockSetObjectWithoutToString() {
1377
		$objectWithToString = new TestObjectWithoutToString();
1378
		$this->View->assign('testWithObjectWithoutToString', $objectWithToString);
1379
	}
1380
 
1381
/**
1382
 * Test setting a block's content to a decimal
1383
 *
1384
 * @return void
1385
 */
1386
	public function testBlockSetDecimal() {
1387
		$this->View->assign('testWithDecimal', 1.23456789);
1388
		$result = $this->View->fetch('testWithDecimal');
1389
		$this->assertEquals('1.23456789', $result);
1390
	}
1391
 
1392
/**
1393
 * Data provider for block related tests.
1394
 *
1395
 * @return array
1396
 */
1397
	public static function blockValueProvider() {
1398
		return array(
1399
			'string' => array('A string value'),
1400
			'null' => array(null),
1401
			'decimal' => array(1.23456),
1402
			'object with __toString' => array(new TestObjectWithToString()),
1403
		);
1404
	}
1405
 
1406
/**
1407
 * Test appending to a block with append.
1408
 *
1409
 * @dataProvider blockValueProvider
1410
 * @return void
1411
 */
1412
	public function testBlockAppend($value) {
1413
		$this->View->assign('testBlock', 'Block');
1414
		$this->View->append('testBlock', $value);
1415
 
1416
		$result = $this->View->fetch('testBlock');
1417
		$this->assertSame('Block' . $value, $result);
1418
	}
1419
 
1420
/**
1421
 * Test appending an object without __toString magic method to a block with append.
1422
 *
1423
 * This should produce a "Object of class TestObjectWithoutToString could not be converted to string" error
1424
 * which gets thrown as a PHPUnit_Framework_Error Exception by PHPUnit.
1425
 *
1426
 * @expectedException PHPUnit_Framework_Error
1427
 * @return void
1428
 */
1429
	public function testBlockAppendObjectWithoutToString() {
1430
		$object = new TestObjectWithoutToString();
1431
		$this->View->assign('testBlock', 'Block ');
1432
		$this->View->append('testBlock', $object);
1433
	}
1434
 
1435
/**
1436
 * Test prepending to a block with prepend.
1437
 *
1438
 * @dataProvider blockValueProvider
1439
 * @return void
1440
 */
1441
	public function testBlockPrepend($value) {
1442
		$this->View->assign('test', 'Block');
1443
		$this->View->prepend('test', $value);
1444
 
1445
		$result = $this->View->fetch('test');
1446
		$this->assertEquals($value . 'Block', $result);
1447
	}
1448
 
1449
/**
1450
 * Test prepending an object without __toString magic method to a block with prepend.
1451
 *
1452
 * This should produce a "Object of class TestObjectWithoutToString could not be converted to string" error
1453
 * which gets thrown as a PHPUnit_Framework_Error Exception by PHPUnit.
1454
 *
1455
 * @expectedException PHPUnit_Framework_Error
1456
 * @return void
1457
 */
1458
	public function testBlockPrependObjectWithoutToString() {
1459
		$object = new TestObjectWithoutToString();
1460
		$this->View->assign('test', 'Block ');
1461
		$this->View->prepend('test', $object);
1462
	}
1463
 
1464
/**
1465
 * You should be able to append to undefined blocks.
1466
 *
1467
 * @return void
1468
 */
1469
	public function testBlockAppendUndefined() {
1470
		$this->View->append('test', 'Unknown');
1471
		$result = $this->View->fetch('test');
1472
		$this->assertEquals('Unknown', $result);
1473
	}
1474
 
1475
/**
1476
 * You should be able to prepend to undefined blocks.
1477
 *
1478
 * @return void
1479
 */
1480
	public function testBlockPrependUndefined() {
1481
		$this->View->prepend('test', 'Unknown');
1482
		$result = $this->View->fetch('test');
1483
		$this->assertEquals('Unknown', $result);
1484
	}
1485
 
1486
/**
1487
 * Test getting block names
1488
 *
1489
 * @return void
1490
 */
1491
	public function testBlocks() {
1492
		$this->View->append('test', 'one');
1493
		$this->View->assign('test1', 'one');
1494
 
1495
		$this->assertEquals(array('test', 'test1'), $this->View->blocks());
1496
	}
1497
 
1498
/**
1499
 * Test that blocks can be nested.
1500
 *
1501
 * @return void
1502
 */
1503
	public function testNestedBlocks() {
1504
		$this->View->start('first');
1505
		echo 'In first ';
1506
		$this->View->start('second');
1507
		echo 'In second';
1508
		$this->View->end();
1509
		echo 'In first';
1510
		$this->View->end();
1511
 
1512
		$this->assertEquals('In first In first', $this->View->fetch('first'));
1513
		$this->assertEquals('In second', $this->View->fetch('second'));
1514
	}
1515
 
1516
/**
1517
 * Test that starting the same block twice throws an exception
1518
 *
1519
 * @expectedException CakeException
1520
 * @return void
1521
 */
1522
	public function testStartBlocksTwice() {
1523
		$this->View->start('first');
1524
		echo 'In first ';
1525
		$this->View->start('second');
1526
		echo 'In second';
1527
		$this->View->start('first');
1528
	}
1529
 
1530
/**
1531
 * Test that an exception gets thrown when you leave a block open at the end
1532
 * of a view.
1533
 *
1534
 * @expectedException CakeException
1535
 * @return void
1536
 */
1537
	public function testExceptionOnOpenBlock() {
1538
		$this->View->render('open_block');
1539
	}
1540
 
1541
/**
1542
 * Test nested extended views.
1543
 *
1544
 * @return void
1545
 */
1546
	public function testExtendNested() {
1547
		$this->View->layout = false;
1548
		$content = $this->View->render('nested_extends');
1549
		$expected = <<<TEXT
1550
This is the second parent.
1551
This is the first parent.
1552
This is the first template.
1553
Sidebar Content.
1554
TEXT;
1555
		$this->assertEquals($expected, $content);
1556
	}
1557
 
1558
/**
1559
 * Make sure that extending the current view with itself causes an exception
1560
 *
1561
 * @expectedException LogicException
1562
 * @return void
1563
 */
1564
	public function testExtendSelf() {
1565
		$this->View->layout = false;
1566
		$this->View->render('extend_self');
1567
	}
1568
 
1569
/**
1570
 * Make sure that extending in a loop causes an exception
1571
 *
1572
 * @expectedException LogicException
1573
 * @return void
1574
 */
1575
	public function testExtendLoop() {
1576
		$this->View->layout = false;
1577
		$this->View->render('extend_loop');
1578
	}
1579
 
1580
/**
1581
 * Test extend() in an element and a view.
1582
 *
1583
 * @return void
1584
 */
1585
	public function testExtendElement() {
1586
		$this->View->layout = false;
1587
		$content = $this->View->render('extend_element');
1588
		$expected = <<<TEXT
1589
Parent View.
1590
View content.
1591
Parent Element.
1592
Element content.
1593
 
1594
TEXT;
1595
		$this->assertEquals($expected, $content);
1596
	}
1597
 
1598
/**
1599
 * Extending an element which doesn't exist should throw a missing view exception
1600
 *
1601
 * @expectedException LogicException
1602
 * @return void
1603
 */
1604
	public function testExtendMissingElement() {
1605
		$this->View->layout = false;
1606
		$this->View->render('extend_missing_element');
1607
	}
1608
 
1609
/**
1610
 * Test extend() preceeded by an element()
1611
 *
1612
 * @return void
1613
 */
1614
	public function testExtendWithElementBeforeExtend() {
1615
		$this->View->layout = false;
1616
		$result = $this->View->render('extend_with_element');
1617
		$expected = <<<TEXT
1618
Parent View.
1619
this is the test elementThe view
1620
 
1621
TEXT;
1622
		$this->assertEquals($expected, $result);
1623
	}
1624
 
1625
/**
1626
 * Test that setting arbitrary properties still works.
1627
 *
1628
 * @return void
1629
 */
1630
	public function testPropertySetting() {
1631
		$this->assertFalse(isset($this->View->pageTitle));
1632
		$this->View->pageTitle = 'test';
1633
		$this->assertTrue(isset($this->View->pageTitle));
1634
		$this->assertTrue(!empty($this->View->pageTitle));
1635
		$this->assertEquals('test', $this->View->pageTitle);
1636
	}
1637
 
1638
/**
1639
 * Test that setting arbitrary properties still works.
1640
 *
1641
 * @return void
1642
 */
1643
	public function testPropertySettingMagicGet() {
1644
		$this->assertFalse(isset($this->View->action));
1645
		$this->View->request->params['action'] = 'login';
1646
		$this->assertEquals('login', $this->View->action);
1647
		$this->assertTrue(isset($this->View->action));
1648
		$this->assertTrue(!empty($this->View->action));
1649
	}
1650
 
1651
/**
1652
 * Test memory leaks that existed in _paths at one point.
1653
 *
1654
 * @return void
1655
 */
1656
	public function testMemoryLeakInPaths() {
1657
		$this->ThemeController->plugin = null;
1658
		$this->ThemeController->name = 'Posts';
1659
		$this->ThemeController->viewPath = 'posts';
1660
		$this->ThemeController->layout = 'whatever';
1661
		$this->ThemeController->theme = 'TestTheme';
1662
 
1663
		$View = new View($this->ThemeController);
1664
		$View->element('test_element');
1665
 
1666
		$start = memory_get_usage();
1667
		for ($i = 0; $i < 10; $i++) {
1668
			$View->element('test_element');
1669
		}
1670
		$end = memory_get_usage();
1671
		$this->assertLessThanOrEqual($start + 5000, $end);
1672
	}
1673
 
1674
/**
1675
 * Tests that a vew block uses default value when not assigned and uses assigned value when it is
1676
 *
1677
 * @return void
1678
 */
1679
	public function testBlockDefaultValue() {
1680
		$default = 'Default';
1681
		$result = $this->View->fetch('title', $default);
1682
		$this->assertEquals($default, $result);
1683
 
1684
		$expected = 'My Title';
1685
		$this->View->assign('title', $expected);
1686
		$result = $this->View->fetch('title', $default);
1687
		$this->assertEquals($expected, $result);
1688
	}
1689
}