Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 1
<?php
2
/**
3
 * HtmlHelperTest 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.Helper
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('Controller', 'Controller');
20
App::uses('Helper', 'View');
21
App::uses('AppHelper', 'View/Helper');
22
App::uses('HtmlHelper', 'View/Helper');
23
App::uses('FormHelper', 'View/Helper');
24
App::uses('ClassRegistry', 'Utility');
25
App::uses('Folder', 'Utility');
26
 
27
if (!defined('FULL_BASE_URL')) {
28
	define('FULL_BASE_URL', 'http://cakephp.org');
29
}
30
 
31
/**
32
 * TheHtmlTestController class
33
 *
34
 * @package       Cake.Test.Case.View.Helper
35
 */
36
class TheHtmlTestController extends Controller {
37
 
38
/**
39
 * name property
40
 *
41
 * @var string
42
 */
43
	public $name = 'TheTest';
44
 
45
/**
46
 * uses property
47
 *
48
 * @var mixed null
49
 */
50
	public $uses = null;
51
}
52
 
53
class TestHtmlHelper extends HtmlHelper {
54
 
55
/**
56
 * expose a method as public
57
 *
58
 * @param string $options
59
 * @param string $exclude
60
 * @param string $insertBefore
61
 * @param string $insertAfter
62
 * @return void
63
 */
64
	public function parseAttributes($options, $exclude = null, $insertBefore = ' ', $insertAfter = null) {
65
		return $this->_parseAttributes($options, $exclude, $insertBefore, $insertAfter);
66
	}
67
 
68
/**
69
 * Get a protected attribute value
70
 *
71
 * @param string $attribute
72
 * @return mixed
73
 */
74
	public function getAttribute($attribute) {
75
		if (!isset($this->{$attribute})) {
76
			return null;
77
		}
78
		return $this->{$attribute};
79
	}
80
 
81
}
82
 
83
/**
84
 * Html5TestHelper class
85
 *
86
 * @package       Cake.Test.Case.View.Helper
87
 */
88
class Html5TestHelper extends TestHtmlHelper {
89
 
90
/**
91
 * Minimized
92
 *
93
 * @var array
94
 */
95
	protected $_minimizedAttributes = array('require', 'checked');
96
 
97
/**
98
 * Allow compact use in HTML
99
 *
100
 * @var string
101
 */
102
	protected $_minimizedAttributeFormat = '%s';
103
 
104
/**
105
 * Test to attribute format
106
 *
107
 * @var string
108
 */
109
	protected $_attributeFormat = 'data-%s="%s"';
110
}
111
 
112
/**
113
 * HtmlHelperTest class
114
 *
115
 * @package       Cake.Test.Case.View.Helper
116
 */
117
class HtmlHelperTest extends CakeTestCase {
118
 
119
/**
120
 * Regexp for CDATA start block
121
 *
122
 * @var string
123
 */
124
	public $cDataStart = 'preg:/^\/\/<!\[CDATA\[[\n\r]*/';
125
 
126
/**
127
 * Regexp for CDATA end block
128
 *
129
 * @var string
130
 */
131
	public $cDataEnd = 'preg:/[^\]]*\]\]\>[\s\r\n]*/';
132
 
133
/**
134
 * html property
135
 *
136
 * @var object
137
 */
138
	public $Html = null;
139
 
140
/**
141
 * setUp method
142
 *
143
 * @return void
144
 */
145
	public function setUp() {
146
		parent::setUp();
147
		$this->View = $this->getMock('View', array('append'), array(new TheHtmlTestController()));
148
		$this->Html = new TestHtmlHelper($this->View);
149
		$this->Html->request = new CakeRequest(null, false);
150
		$this->Html->request->webroot = '';
151
 
152
		App::build(array(
153
			'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
154
		));
155
 
156
		Configure::write('Asset.timestamp', false);
157
	}
158
 
159
/**
160
 * tearDown method
161
 *
162
 * @return void
163
 */
164
	public function tearDown() {
165
		parent::tearDown();
166
		unset($this->Html, $this->View);
167
	}
168
 
169
/**
170
 * testDocType method
171
 *
172
 * @return void
173
 */
174
	public function testDocType() {
175
		$result = $this->Html->docType();
176
		$expected = '<!DOCTYPE html>';
177
		$this->assertEquals($expected, $result);
178
 
179
		$result = $this->Html->docType('html4-strict');
180
		$expected = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">';
181
		$this->assertEquals($expected, $result);
182
 
183
		$this->assertNull($this->Html->docType('non-existing-doctype'));
184
	}
185
 
186
/**
187
 * testLink method
188
 *
189
 * @return void
190
 */
191
	public function testLink() {
192
		Router::connect('/:controller/:action/*');
193
 
194
		$this->Html->request->webroot = '';
195
 
196
		$result = $this->Html->link('/home');
197
		$expected = array('a' => array('href' => '/home'), 'preg:/\/home/', '/a');
198
		$this->assertTags($result, $expected);
199
 
200
		$result = $this->Html->link(array('action' => 'login', '<[You]>'));
201
		$expected = array(
202
			'a' => array('href' => '/login/%3C%5BYou%5D%3E'),
203
			'preg:/\/login\/&lt;\[You\]&gt;/',
204
			'/a'
205
		);
206
		$this->assertTags($result, $expected);
207
 
208
		Router::reload();
209
 
210
		$result = $this->Html->link('Posts', array('controller' => 'posts', 'action' => 'index', 'full_base' => true));
211
		$expected = array('a' => array('href' => Router::fullBaseUrl() . '/posts'), 'Posts', '/a');
212
		$this->assertTags($result, $expected);
213
 
214
		$result = $this->Html->link('Home', '/home', array('confirm' => 'Are you sure you want to do this?'));
215
		$expected = array(
216
			'a' => array('href' => '/home', 'onclick' => 'if (confirm(&quot;Are you sure you want to do this?&quot;)) { return true; } return false;'),
217
			'Home',
218
			'/a'
219
		);
220
		$this->assertTags($result, $expected);
221
 
222
		$result = $this->Html->link('Home', '/home', array('escape' => false, 'confirm' => 'Confirm\'s "nightmares"'));
223
		$expected = array(
224
			'a' => array('href' => '/home', 'onclick' => 'if (confirm(&quot;Confirm&#039;s \&quot;nightmares\&quot;&quot;)) { return true; } return false;'),
225
			'Home',
226
			'/a'
227
		);
228
		$this->assertTags($result, $expected);
229
 
230
		$result = $this->Html->link('Home', '/home', array('default' => false));
231
		$expected = array(
232
			'a' => array('href' => '/home', 'onclick' => 'event.returnValue = false; return false;'),
233
			'Home',
234
			'/a'
235
		);
236
		$this->assertTags($result, $expected);
237
 
238
		$result = $this->Html->link('Home', '/home', array('default' => false, 'onclick' => 'someFunction();'));
239
		$expected = array(
240
			'a' => array('href' => '/home', 'onclick' => 'someFunction(); event.returnValue = false; return false;'),
241
			'Home',
242
			'/a'
243
		);
244
		$this->assertTags($result, $expected);
245
 
246
		$result = $this->Html->link('Next >', '#');
247
		$expected = array(
248
			'a' => array('href' => '#'),
249
			'Next &gt;',
250
			'/a'
251
		);
252
		$this->assertTags($result, $expected);
253
 
254
		$result = $this->Html->link('Next >', '#', array('escape' => true));
255
		$expected = array(
256
			'a' => array('href' => '#'),
257
			'Next &gt;',
258
			'/a'
259
		);
260
		$this->assertTags($result, $expected);
261
 
262
		$result = $this->Html->link('Next >', '#', array('escape' => 'utf-8'));
263
		$expected = array(
264
			'a' => array('href' => '#'),
265
			'Next &gt;',
266
			'/a'
267
		);
268
		$this->assertTags($result, $expected);
269
 
270
		$result = $this->Html->link('Next >', '#', array('escape' => false));
271
		$expected = array(
272
			'a' => array('href' => '#'),
273
			'Next >',
274
			'/a'
275
		);
276
		$this->assertTags($result, $expected);
277
 
278
		$result = $this->Html->link('Next >', '#', array(
279
			'title' => 'to escape &#8230; or not escape?',
280
			'escape' => false
281
		));
282
		$expected = array(
283
			'a' => array('href' => '#', 'title' => 'to escape &#8230; or not escape?'),
284
			'Next >',
285
			'/a'
286
		);
287
		$this->assertTags($result, $expected);
288
 
289
		$result = $this->Html->link('Next >', '#', array(
290
			'title' => 'to escape &#8230; or not escape?',
291
			'escape' => true
292
		));
293
		$expected = array(
294
			'a' => array('href' => '#', 'title' => 'to escape &amp;#8230; or not escape?'),
295
			'Next &gt;',
296
			'/a'
297
		);
298
		$this->assertTags($result, $expected);
299
 
300
		$result = $this->Html->link('Next >', '#', array(
301
			'title' => 'Next >',
302
			'escapeTitle' => false
303
		));
304
		$expected = array(
305
			'a' => array('href' => '#', 'title' => 'Next &gt;'),
306
			'Next >',
307
			'/a'
308
		);
309
		$this->assertTags($result, $expected);
310
 
311
		$result = $this->Html->link('Original size', array(
312
			'controller' => 'images', 'action' => 'view', 3, '?' => array('height' => 100, 'width' => 200)
313
		));
314
		$expected = array(
315
			'a' => array('href' => '/images/view/3?height=100&amp;width=200'),
316
			'Original size',
317
			'/a'
318
		);
319
		$this->assertTags($result, $expected);
320
 
321
		Configure::write('Asset.timestamp', false);
322
 
323
		$result = $this->Html->link($this->Html->image('test.gif'), '#', array('escape' => false));
324
		$expected = array(
325
			'a' => array('href' => '#'),
326
			'img' => array('src' => 'img/test.gif', 'alt' => ''),
327
			'/a'
328
		);
329
		$this->assertTags($result, $expected);
330
 
331
		$result = $this->Html->link($this->Html->image('test.gif'), '#', array(
332
			'title' => 'hey "howdy"',
333
			'escapeTitle' => false
334
		));
335
		$expected = array(
336
			'a' => array('href' => '#', 'title' => 'hey &quot;howdy&quot;'),
337
			'img' => array('src' => 'img/test.gif', 'alt' => ''),
338
			'/a'
339
		);
340
		$this->assertTags($result, $expected);
341
 
342
		$result = $this->Html->image('test.gif', array('url' => '#'));
343
		$expected = array(
344
			'a' => array('href' => '#'),
345
			'img' => array('src' => 'img/test.gif', 'alt' => ''),
346
			'/a'
347
		);
348
		$this->assertTags($result, $expected);
349
 
350
		$result = $this->Html->link($this->Html->image('../favicon.ico'), '#', array('escape' => false));
351
		$expected = array(
352
			'a' => array('href' => '#'),
353
			'img' => array('src' => 'img/../favicon.ico', 'alt' => ''),
354
			'/a'
355
		);
356
		$this->assertTags($result, $expected);
357
 
358
		$result = $this->Html->image('../favicon.ico', array('url' => '#'));
359
		$expected = array(
360
			'a' => array('href' => '#'),
361
			'img' => array('src' => 'img/../favicon.ico', 'alt' => ''),
362
			'/a'
363
		);
364
		$this->assertTags($result, $expected);
365
 
366
		$result = $this->Html->link('http://www.example.org?param1=value1&param2=value2');
367
		$expected = array('a' => array('href' => 'http://www.example.org?param1=value1&amp;param2=value2'), 'http://www.example.org?param1=value1&amp;param2=value2', '/a');
368
		$this->assertTags($result, $expected);
369
 
370
		$result = $this->Html->link('alert', 'javascript:alert(\'cakephp\');');
371
		$expected = array('a' => array('href' => 'javascript:alert(&#039;cakephp&#039;);'), 'alert', '/a');
372
		$this->assertTags($result, $expected);
373
 
374
		$result = $this->Html->link('write me', 'mailto:example@cakephp.org');
375
		$expected = array('a' => array('href' => 'mailto:example@cakephp.org'), 'write me', '/a');
376
		$this->assertTags($result, $expected);
377
 
378
		$result = $this->Html->link('call me on 0123465-798', 'tel:0123465-798');
379
		$expected = array('a' => array('href' => 'tel:0123465-798'), 'call me on 0123465-798', '/a');
380
		$this->assertTags($result, $expected);
381
 
382
		$result = $this->Html->link('text me on 0123465-798', 'sms:0123465-798');
383
		$expected = array('a' => array('href' => 'sms:0123465-798'), 'text me on 0123465-798', '/a');
384
		$this->assertTags($result, $expected);
385
 
386
		$result = $this->Html->link('say hello to 0123465-798', 'sms:0123465-798?body=hello there');
387
		$expected = array('a' => array('href' => 'sms:0123465-798?body=hello there'), 'say hello to 0123465-798', '/a');
388
		$this->assertTags($result, $expected);
389
 
390
		$result = $this->Html->link('say hello to 0123465-798', 'sms:0123465-798?body=hello "cakephp"');
391
		$expected = array('a' => array('href' => 'sms:0123465-798?body=hello &quot;cakephp&quot;'), 'say hello to 0123465-798', '/a');
392
		$this->assertTags($result, $expected);
393
	}
394
 
395
/**
396
 * testImageTag method
397
 *
398
 * @return void
399
 */
400
	public function testImageTag() {
401
		$this->Html->request->webroot = '';
402
 
403
		$result = $this->Html->image('test.gif');
404
		$this->assertTags($result, array('img' => array('src' => 'img/test.gif', 'alt' => '')));
405
 
406
		$result = $this->Html->image('http://google.com/logo.gif');
407
		$this->assertTags($result, array('img' => array('src' => 'http://google.com/logo.gif', 'alt' => '')));
408
 
409
		$result = $this->Html->image('//google.com/logo.gif');
410
		$this->assertTags($result, array('img' => array('src' => '//google.com/logo.gif', 'alt' => '')));
411
 
412
		$result = $this->Html->image(array('controller' => 'test', 'action' => 'view', 1, 'ext' => 'gif'));
413
		$this->assertTags($result, array('img' => array('src' => '/test/view/1.gif', 'alt' => '')));
414
 
415
		$result = $this->Html->image('/test/view/1.gif');
416
		$this->assertTags($result, array('img' => array('src' => '/test/view/1.gif', 'alt' => '')));
417
	}
418
 
419
/**
420
 * Test image() with query strings.
421
 *
422
 * @return void
423
 */
424
	public function testImageQueryString() {
425
		$result = $this->Html->image('test.gif?one=two&three=four');
426
		$this->assertTags($result, array('img' => array('src' => 'img/test.gif?one=two&amp;three=four', 'alt' => '')));
427
 
428
		$result = $this->Html->image(array(
429
			'controller' => 'images',
430
			'action' => 'display',
431
			'test',
432
			'?' => array('one' => 'two', 'three' => 'four')
433
		));
434
		$this->assertTags($result, array('img' => array('src' => '/images/display/test?one=two&amp;three=four', 'alt' => '')));
435
	}
436
 
437
/**
438
 * Test that image works with pathPrefix.
439
 *
440
 * @return void
441
 */
442
	public function testImagePathPrefix() {
443
		$result = $this->Html->image('test.gif', array('pathPrefix' => '/my/custom/path/'));
444
		$this->assertTags($result, array('img' => array('src' => '/my/custom/path/test.gif', 'alt' => '')));
445
 
446
		$result = $this->Html->image('test.gif', array('pathPrefix' => 'http://cakephp.org/assets/img/'));
447
		$this->assertTags($result, array('img' => array('src' => 'http://cakephp.org/assets/img/test.gif', 'alt' => '')));
448
 
449
		$result = $this->Html->image('test.gif', array('pathPrefix' => '//cakephp.org/assets/img/'));
450
		$this->assertTags($result, array('img' => array('src' => '//cakephp.org/assets/img/test.gif', 'alt' => '')));
451
 
452
		$previousConfig = Configure::read('App.imageBaseUrl');
453
		Configure::write('App.imageBaseUrl', '//cdn.cakephp.org/img/');
454
		$result = $this->Html->image('test.gif');
455
		$this->assertTags($result, array('img' => array('src' => '//cdn.cakephp.org/img/test.gif', 'alt' => '')));
456
		Configure::write('App.imageBaseUrl', $previousConfig);
457
	}
458
 
459
/**
460
 * Test that image() works with fullBase and a webroot not equal to /
461
 *
462
 * @return void
463
 */
464
	public function testImageWithFullBase() {
465
		$result = $this->Html->image('test.gif', array('fullBase' => true));
466
		$here = $this->Html->url('/', true);
467
		$this->assertTags($result, array('img' => array('src' => $here . 'img/test.gif', 'alt' => '')));
468
 
469
		$result = $this->Html->image('sub/test.gif', array('fullBase' => true));
470
		$here = $this->Html->url('/', true);
471
		$this->assertTags($result, array('img' => array('src' => $here . 'img/sub/test.gif', 'alt' => '')));
472
 
473
		$request = $this->Html->request;
474
		$request->webroot = '/myproject/';
475
		$request->base = '/myproject';
476
		Router::setRequestInfo($request);
477
 
478
		$result = $this->Html->image('sub/test.gif', array('fullBase' => true));
479
		$here = $this->Html->url('/', true);
480
		$this->assertTags($result, array('img' => array('src' => $here . 'img/sub/test.gif', 'alt' => '')));
481
	}
482
 
483
/**
484
 * test image() with Asset.timestamp
485
 *
486
 * @return void
487
 */
488
	public function testImageWithTimestampping() {
489
		Configure::write('Asset.timestamp', 'force');
490
 
491
		$this->Html->request->webroot = '/';
492
		$result = $this->Html->image('cake.icon.png');
493
		$this->assertTags($result, array('img' => array('src' => 'preg:/\/img\/cake\.icon\.png\?\d+/', 'alt' => '')));
494
 
495
		Configure::write('debug', 0);
496
		Configure::write('Asset.timestamp', 'force');
497
 
498
		$result = $this->Html->image('cake.icon.png');
499
		$this->assertTags($result, array('img' => array('src' => 'preg:/\/img\/cake\.icon\.png\?\d+/', 'alt' => '')));
500
 
501
		$this->Html->request->webroot = '/testing/longer/';
502
		$result = $this->Html->image('cake.icon.png');
503
		$expected = array(
504
			'img' => array('src' => 'preg:/\/testing\/longer\/img\/cake\.icon\.png\?[0-9]+/', 'alt' => '')
505
		);
506
		$this->assertTags($result, $expected);
507
	}
508
 
509
/**
510
 * Tests creation of an image tag using a theme and asset timestamping
511
 *
512
 * @return void
513
 */
514
	public function testImageTagWithTheme() {
515
		$this->skipIf(!is_writable(WWW_ROOT), 'Cannot write to webroot.');
516
		$themeExists = is_dir(WWW_ROOT . 'theme');
517
 
518
		App::uses('File', 'Utility');
519
 
520
		$testfile = WWW_ROOT . 'theme' . DS . 'test_theme' . DS . 'img' . DS . '__cake_test_image.gif';
521
		new File($testfile, true);
522
 
523
		App::build(array(
524
			'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
525
		));
526
		Configure::write('Asset.timestamp', true);
527
		Configure::write('debug', 1);
528
 
529
		$this->Html->request->webroot = '/';
530
		$this->Html->theme = 'test_theme';
531
		$result = $this->Html->image('__cake_test_image.gif');
532
		$this->assertTags($result, array(
533
			'img' => array(
534
				'src' => 'preg:/\/theme\/test_theme\/img\/__cake_test_image\.gif\?\d+/',
535
				'alt' => ''
536
		)));
537
 
538
		$this->Html->request->webroot = '/testing/';
539
		$result = $this->Html->image('__cake_test_image.gif');
540
 
541
		$this->assertTags($result, array(
542
			'img' => array(
543
				'src' => 'preg:/\/testing\/theme\/test_theme\/img\/__cake_test_image\.gif\?\d+/',
544
				'alt' => ''
545
		)));
546
 
547
		$dir = new Folder(WWW_ROOT . 'theme' . DS . 'test_theme');
548
		$dir->delete();
549
		if (!$themeExists) {
550
			$dir = new Folder(WWW_ROOT . 'theme');
551
			$dir->delete();
552
		}
553
	}
554
 
555
/**
556
 * test theme assets in main webroot path
557
 *
558
 * @return void
559
 */
560
	public function testThemeAssetsInMainWebrootPath() {
561
		App::build(array(
562
			'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
563
		));
564
		$webRoot = Configure::read('App.www_root');
565
		Configure::write('App.www_root', CAKE . 'Test' . DS . 'test_app' . DS . 'webroot' . DS);
566
 
567
		$this->Html->theme = 'test_theme';
568
		$result = $this->Html->css('webroot_test');
569
		$expected = array(
570
			'link' => array('rel' => 'stylesheet', 'type' => 'text/css', 'href' => 'preg:/.*theme\/test_theme\/css\/webroot_test\.css/')
571
		);
572
		$this->assertTags($result, $expected);
573
 
574
		$this->Html->theme = 'test_theme';
575
		$result = $this->Html->css('theme_webroot');
576
		$expected = array(
577
			'link' => array('rel' => 'stylesheet', 'type' => 'text/css', 'href' => 'preg:/.*theme\/test_theme\/css\/theme_webroot\.css/')
578
		);
579
		$this->assertTags($result, $expected);
580
 
581
		Configure::write('App.www_root', $webRoot);
582
	}
583
 
584
/**
585
 * testStyle method
586
 *
587
 * @return void
588
 */
589
	public function testStyle() {
590
		$result = $this->Html->style('display: none;');
591
		$this->assertEquals('display: none;', $result);
592
 
593
		$result = $this->Html->style(array('display' => 'none', 'margin' => '10px'));
594
		$expected = 'display:none; margin:10px;';
595
		$this->assertRegExp('/^display\s*:\s*none\s*;\s*margin\s*:\s*10px\s*;?$/', $expected);
596
 
597
		$result = $this->Html->style(array('display' => 'none', 'margin' => '10px'), false);
598
		$lines = explode("\n", $result);
599
		$this->assertRegExp('/^\s*display\s*:\s*none\s*;\s*$/', $lines[0]);
600
		$this->assertRegExp('/^\s*margin\s*:\s*10px\s*;?$/', $lines[1]);
601
	}
602
 
603
/**
604
 * testCssLink method
605
 *
606
 * @return void
607
 */
608
	public function testCssLink() {
609
		Configure::write('Asset.filter.css', false);
610
 
611
		$result = $this->Html->css('screen');
612
		$expected = array(
613
			'link' => array('rel' => 'stylesheet', 'type' => 'text/css', 'href' => 'preg:/.*css\/screen\.css/')
614
		);
615
		$this->assertTags($result, $expected);
616
 
617
		$result = $this->Html->css('screen.css');
618
		$this->assertTags($result, $expected);
619
 
620
		CakePlugin::load('TestPlugin');
621
		$result = $this->Html->css('TestPlugin.style', array('plugin' => false));
622
		$expected['link']['href'] = 'preg:/.*css\/TestPlugin\.style\.css/';
623
		$this->assertTags($result, $expected);
624
		CakePlugin::unload('TestPlugin');
625
 
626
		$result = $this->Html->css('my.css.library');
627
		$expected['link']['href'] = 'preg:/.*css\/my\.css\.library\.css/';
628
		$this->assertTags($result, $expected);
629
 
630
		$result = $this->Html->css('screen.css?1234');
631
		$expected['link']['href'] = 'preg:/.*css\/screen\.css\?1234/';
632
		$this->assertTags($result, $expected);
633
 
634
		$result = $this->Html->css('screen.css?with=param&other=param');
635
		$expected['link']['href'] = 'css/screen.css?with=param&amp;other=param';
636
		$this->assertTags($result, $expected);
637
 
638
		$result = $this->Html->css('http://whatever.com/screen.css?1234');
639
		$expected['link']['href'] = 'preg:/http:\/\/.*\/screen\.css\?1234/';
640
		$this->assertTags($result, $expected);
641
 
642
		$result = $this->Html->css('cake.generic', array('pathPrefix' => '/my/custom/path/'));
643
		$expected['link']['href'] = '/my/custom/path/cake.generic.css';
644
		$this->assertTags($result, $expected);
645
 
646
		$result = $this->Html->css('cake.generic', array('pathPrefix' => 'http://cakephp.org/assets/css/'));
647
		$expected['link']['href'] = 'http://cakephp.org/assets/css/cake.generic.css';
648
		$this->assertTags($result, $expected);
649
 
650
		$previousConfig = Configure::read('App.cssBaseUrl');
651
		Configure::write('App.cssBaseUrl', '//cdn.cakephp.org/css/');
652
		$result = $this->Html->css('cake.generic');
653
		$expected['link']['href'] = '//cdn.cakephp.org/css/cake.generic.css';
654
		$this->assertTags($result, $expected);
655
		Configure::write('App.cssBaseUrl', $previousConfig);
656
 
657
		Configure::write('Asset.filter.css', 'css.php');
658
		$result = $this->Html->css('cake.generic');
659
		$expected['link']['href'] = 'preg:/.*ccss\/cake\.generic\.css/';
660
		$this->assertTags($result, $expected);
661
 
662
		$result = $this->Html->css('//example.com/css/cake.generic.css');
663
		$expected['link']['href'] = 'preg:/.*example\.com\/css\/cake\.generic\.css/';
664
		$this->assertTags($result, $expected);
665
 
666
		Configure::write('Asset.filter.css', false);
667
 
668
		$result = explode("\n", trim($this->Html->css(array('cake.generic', 'vendor.generic'))));
669
		$expected['link']['href'] = 'preg:/.*css\/cake\.generic\.css/';
670
		$this->assertTags($result[0], $expected);
671
		$expected['link']['href'] = 'preg:/.*css\/vendor\.generic\.css/';
672
		$this->assertTags($result[1], $expected);
673
		$this->assertEquals(2, count($result));
674
 
675
		$this->View->expects($this->at(0))
676
			->method('append')
677
			->with('css', $this->matchesRegularExpression('/css_in_head.css/'));
678
 
679
		$this->View->expects($this->at(1))
680
			->method('append')
681
			->with('css', $this->matchesRegularExpression('/more_css_in_head.css/'));
682
 
683
		$result = $this->Html->css('css_in_head', array('inline' => false));
684
		$this->assertNull($result);
685
 
686
		$result = $this->Html->css('more_css_in_head', array('inline' => false));
687
		$this->assertNull($result);
688
 
689
		$result = $this->Html->css('screen', array('rel' => 'import'));
690
		$expected = array(
691
			'style' => array('type' => 'text/css'),
692
			'preg:/@import url\(.*css\/screen\.css\);/',
693
			'/style'
694
		);
695
		$this->assertTags($result, $expected);
696
	}
697
 
698
/**
699
 * Test css link BC usage
700
 *
701
 * @return void
702
 */
703
	public function testCssLinkBC() {
704
		Configure::write('Asset.filter.css', false);
705
 
706
		CakePlugin::load('TestPlugin');
707
		$result = $this->Html->css('TestPlugin.style', null, array('plugin' => false));
708
		$expected = array(
709
			'link' => array(
710
				'rel' => 'stylesheet',
711
				'type' => 'text/css',
712
				'href' => 'preg:/.*css\/TestPlugin\.style\.css/'
713
			)
714
		);
715
		$this->assertTags($result, $expected);
716
		CakePlugin::unload('TestPlugin');
717
 
718
		$result = $this->Html->css('screen', 'import');
719
		$expected = array(
720
			'style' => array('type' => 'text/css'),
721
			'preg:/@import url\(.*css\/screen\.css\);/',
722
			'/style'
723
		);
724
		$this->assertTags($result, $expected);
725
 
726
		$result = $this->Html->css('css_in_head', null, array('inline' => false));
727
		$this->assertNull($result);
728
 
729
		$result = $this->Html->css('more_css_in_head', null, array('inline' => false));
730
		$this->assertNull($result);
731
	}
732
 
733
/**
734
 * testCssWithFullBase method
735
 *
736
 * @return void
737
 */
738
	public function testCssWithFullBase() {
739
		Configure::write('Asset.filter.css', false);
740
		$here = $this->Html->url('/', true);
741
 
742
		$result = $this->Html->css('screen', null, array('fullBase' => true));
743
		$expected = array(
744
			'link' => array('rel' => 'stylesheet', 'type' => 'text/css', 'href' => $here . 'css/screen.css')
745
		);
746
		$this->assertTags($result, $expected);
747
	}
748
 
749
/**
750
 * testPluginCssLink method
751
 *
752
 * @return void
753
 */
754
	public function testPluginCssLink() {
755
		Configure::write('Asset.filter.css', false);
756
		CakePlugin::load('TestPlugin');
757
 
758
		$result = $this->Html->css('TestPlugin.test_plugin_asset');
759
		$expected = array(
760
			'link' => array('rel' => 'stylesheet', 'type' => 'text/css', 'href' => 'preg:/.*test_plugin\/css\/test_plugin_asset\.css/')
761
		);
762
		$this->assertTags($result, $expected);
763
 
764
		$result = $this->Html->css('TestPlugin.test_plugin_asset.css');
765
		$this->assertTags($result, $expected);
766
 
767
		$result = $this->Html->css('TestPlugin.my.css.library');
768
		$expected['link']['href'] = 'preg:/.*test_plugin\/css\/my\.css\.library\.css/';
769
		$this->assertTags($result, $expected);
770
 
771
		$result = $this->Html->css('TestPlugin.test_plugin_asset.css?1234');
772
		$expected['link']['href'] = 'preg:/.*test_plugin\/css\/test_plugin_asset\.css\?1234/';
773
		$this->assertTags($result, $expected);
774
 
775
		Configure::write('Asset.filter.css', 'css.php');
776
		$result = $this->Html->css('TestPlugin.test_plugin_asset');
777
		$expected['link']['href'] = 'preg:/.*test_plugin\/ccss\/test_plugin_asset\.css/';
778
		$this->assertTags($result, $expected);
779
 
780
		Configure::write('Asset.filter.css', false);
781
 
782
		$result = explode("\n", trim($this->Html->css(array('TestPlugin.test_plugin_asset', 'TestPlugin.vendor.generic'))));
783
		$expected['link']['href'] = 'preg:/.*test_plugin\/css\/test_plugin_asset\.css/';
784
		$this->assertTags($result[0], $expected);
785
		$expected['link']['href'] = 'preg:/.*test_plugin\/css\/vendor\.generic\.css/';
786
		$this->assertTags($result[1], $expected);
787
		$this->assertEquals(2, count($result));
788
 
789
		CakePlugin::unload('TestPlugin');
790
	}
791
 
792
/**
793
 * test use of css() and timestamping
794
 *
795
 * @return void
796
 */
797
	public function testCssTimestamping() {
798
		Configure::write('debug', 2);
799
		Configure::write('Asset.timestamp', true);
800
 
801
		$expected = array(
802
			'link' => array('rel' => 'stylesheet', 'type' => 'text/css', 'href' => '')
803
		);
804
 
805
		$result = $this->Html->css('cake.generic');
806
		$expected['link']['href'] = 'preg:/.*css\/cake\.generic\.css\?[0-9]+/';
807
		$this->assertTags($result, $expected);
808
 
809
		Configure::write('debug', 0);
810
 
811
		$result = $this->Html->css('cake.generic');
812
		$expected['link']['href'] = 'preg:/.*css\/cake\.generic\.css/';
813
		$this->assertTags($result, $expected);
814
 
815
		Configure::write('Asset.timestamp', 'force');
816
 
817
		$result = $this->Html->css('cake.generic');
818
		$expected['link']['href'] = 'preg:/.*css\/cake\.generic\.css\?[0-9]+/';
819
		$this->assertTags($result, $expected);
820
 
821
		$this->Html->request->webroot = '/testing/';
822
		$result = $this->Html->css('cake.generic');
823
		$expected['link']['href'] = 'preg:/\/testing\/css\/cake\.generic\.css\?[0-9]+/';
824
		$this->assertTags($result, $expected);
825
 
826
		$this->Html->request->webroot = '/testing/longer/';
827
		$result = $this->Html->css('cake.generic');
828
		$expected['link']['href'] = 'preg:/\/testing\/longer\/css\/cake\.generic\.css\?[0-9]+/';
829
		$this->assertTags($result, $expected);
830
	}
831
 
832
/**
833
 * test use of css() and timestamping with plugin syntax
834
 *
835
 * @return void
836
 */
837
	public function testPluginCssTimestamping() {
838
		CakePlugin::load('TestPlugin');
839
 
840
		Configure::write('debug', 2);
841
		Configure::write('Asset.timestamp', true);
842
 
843
		$expected = array(
844
			'link' => array('rel' => 'stylesheet', 'type' => 'text/css', 'href' => '')
845
		);
846
 
847
		$result = $this->Html->css('TestPlugin.test_plugin_asset');
848
		$expected['link']['href'] = 'preg:/.*test_plugin\/css\/test_plugin_asset\.css\?[0-9]+/';
849
		$this->assertTags($result, $expected);
850
 
851
		Configure::write('debug', 0);
852
 
853
		$result = $this->Html->css('TestPlugin.test_plugin_asset');
854
		$expected['link']['href'] = 'preg:/.*test_plugin\/css\/test_plugin_asset\.css/';
855
		$this->assertTags($result, $expected);
856
 
857
		Configure::write('Asset.timestamp', 'force');
858
 
859
		$result = $this->Html->css('TestPlugin.test_plugin_asset');
860
		$expected['link']['href'] = 'preg:/.*test_plugin\/css\/test_plugin_asset\.css\?[0-9]+/';
861
		$this->assertTags($result, $expected);
862
 
863
		$this->Html->request->webroot = '/testing/';
864
		$result = $this->Html->css('TestPlugin.test_plugin_asset');
865
		$expected['link']['href'] = 'preg:/\/testing\/test_plugin\/css\/test_plugin_asset\.css\?[0-9]+/';
866
		$this->assertTags($result, $expected);
867
 
868
		$this->Html->request->webroot = '/testing/longer/';
869
		$result = $this->Html->css('TestPlugin.test_plugin_asset');
870
		$expected['link']['href'] = 'preg:/\/testing\/longer\/test_plugin\/css\/test_plugin_asset\.css\?[0-9]+/';
871
		$this->assertTags($result, $expected);
872
 
873
		CakePlugin::unload('TestPlugin');
874
	}
875
 
876
/**
877
 * test timestamp enforcement for script tags.
878
 *
879
 * @return void
880
 */
881
	public function testScriptTimestamping() {
882
		$this->skipIf(!is_writable(WWW_ROOT . 'js'), 'webroot/js is not Writable, timestamp testing has been skipped.');
883
 
884
		Configure::write('debug', 2);
885
		Configure::write('Asset.timestamp', true);
886
 
887
		touch(WWW_ROOT . 'js' . DS . '__cake_js_test.js');
888
		$timestamp = substr(strtotime('now'), 0, 8);
889
 
890
		$result = $this->Html->script('__cake_js_test', array('inline' => true, 'once' => false));
891
		$this->assertRegExp('/__cake_js_test.js\?' . $timestamp . '[0-9]{2}"/', $result, 'Timestamp value not found %s');
892
 
893
		Configure::write('debug', 0);
894
		Configure::write('Asset.timestamp', 'force');
895
		$result = $this->Html->script('__cake_js_test', array('inline' => true, 'once' => false));
896
		$this->assertRegExp('/__cake_js_test.js\?' . $timestamp . '[0-9]{2}"/', $result, 'Timestamp value not found %s');
897
		unlink(WWW_ROOT . 'js' . DS . '__cake_js_test.js');
898
		Configure::write('Asset.timestamp', false);
899
	}
900
 
901
/**
902
 * test timestamp enforcement for script tags with plugin syntax.
903
 *
904
 * @return void
905
 */
906
	public function testPluginScriptTimestamping() {
907
		CakePlugin::load('TestPlugin');
908
 
909
		$pluginPath = App::pluginPath('TestPlugin');
910
		$pluginJsPath = $pluginPath . 'webroot/js';
911
		$this->skipIf(!is_writable($pluginJsPath), $pluginJsPath . ' is not Writable, timestamp testing has been skipped.');
912
 
913
		Configure::write('debug', 2);
914
		Configure::write('Asset.timestamp', true);
915
 
916
		touch($pluginJsPath . DS . '__cake_js_test.js');
917
		$timestamp = substr(strtotime('now'), 0, 8);
918
 
919
		$result = $this->Html->script('TestPlugin.__cake_js_test', array('inline' => true, 'once' => false));
920
		$this->assertRegExp('/test_plugin\/js\/__cake_js_test.js\?' . $timestamp . '[0-9]{2}"/', $result, 'Timestamp value not found %s');
921
 
922
		Configure::write('debug', 0);
923
		Configure::write('Asset.timestamp', 'force');
924
		$result = $this->Html->script('TestPlugin.__cake_js_test', array('inline' => true, 'once' => false));
925
		$this->assertRegExp('/test_plugin\/js\/__cake_js_test.js\?' . $timestamp . '[0-9]{2}"/', $result, 'Timestamp value not found %s');
926
		unlink($pluginJsPath . DS . '__cake_js_test.js');
927
		Configure::write('Asset.timestamp', false);
928
 
929
		CakePlugin::unload('TestPlugin');
930
	}
931
 
932
/**
933
 * test that scripts added with uses() are only ever included once.
934
 * test script tag generation
935
 *
936
 * @return void
937
 */
938
	public function testScript() {
939
		$result = $this->Html->script('foo');
940
		$expected = array(
941
			'script' => array('type' => 'text/javascript', 'src' => 'js/foo.js')
942
		);
943
		$this->assertTags($result, $expected);
944
 
945
		$result = $this->Html->script(array('foobar', 'bar'));
946
		$expected = array(
947
			array('script' => array('type' => 'text/javascript', 'src' => 'js/foobar.js')),
948
			'/script',
949
			array('script' => array('type' => 'text/javascript', 'src' => 'js/bar.js')),
950
			'/script',
951
		);
952
		$this->assertTags($result, $expected);
953
 
954
		$result = $this->Html->script('jquery-1.3');
955
		$expected = array(
956
			'script' => array('type' => 'text/javascript', 'src' => 'js/jquery-1.3.js')
957
		);
958
		$this->assertTags($result, $expected);
959
 
960
		$result = $this->Html->script('test.json');
961
		$expected = array(
962
			'script' => array('type' => 'text/javascript', 'src' => 'js/test.json.js')
963
		);
964
		$this->assertTags($result, $expected);
965
 
966
		$result = $this->Html->script('http://example.com/test.json');
967
		$expected = array(
968
			'script' => array('type' => 'text/javascript', 'src' => 'http://example.com/test.json')
969
		);
970
		$this->assertTags($result, $expected);
971
 
972
		$result = $this->Html->script('/plugin/js/jquery-1.3.2.js?someparam=foo');
973
		$expected = array(
974
			'script' => array('type' => 'text/javascript', 'src' => '/plugin/js/jquery-1.3.2.js?someparam=foo')
975
		);
976
		$this->assertTags($result, $expected);
977
 
978
		$result = $this->Html->script('test.json.js?foo=bar');
979
		$expected = array(
980
			'script' => array('type' => 'text/javascript', 'src' => 'js/test.json.js?foo=bar')
981
		);
982
		$this->assertTags($result, $expected);
983
 
984
		$result = $this->Html->script('test.json.js?foo=bar&other=test');
985
		$expected = array(
986
			'script' => array('type' => 'text/javascript', 'src' => 'js/test.json.js?foo=bar&amp;other=test')
987
		);
988
		$this->assertTags($result, $expected);
989
 
990
		$result = $this->Html->script('foo2', array('pathPrefix' => '/my/custom/path/'));
991
		$expected = array(
992
			'script' => array('type' => 'text/javascript', 'src' => '/my/custom/path/foo2.js')
993
		);
994
		$this->assertTags($result, $expected);
995
 
996
		$result = $this->Html->script('foo3', array('pathPrefix' => 'http://cakephp.org/assets/js/'));
997
		$expected = array(
998
			'script' => array('type' => 'text/javascript', 'src' => 'http://cakephp.org/assets/js/foo3.js')
999
		);
1000
		$this->assertTags($result, $expected);
1001
 
1002
		$previousConfig = Configure::read('App.jsBaseUrl');
1003
		Configure::write('App.jsBaseUrl', '//cdn.cakephp.org/js/');
1004
		$result = $this->Html->script('foo4');
1005
		$expected = array(
1006
			'script' => array('type' => 'text/javascript', 'src' => '//cdn.cakephp.org/js/foo4.js')
1007
		);
1008
		$this->assertTags($result, $expected);
1009
		Configure::write('App.jsBaseUrl', $previousConfig);
1010
 
1011
		$result = $this->Html->script('foo');
1012
		$this->assertNull($result, 'Script returned upon duplicate inclusion %s');
1013
 
1014
		$result = $this->Html->script(array('foo', 'bar', 'baz'));
1015
		$this->assertNotRegExp('/foo.js/', $result);
1016
 
1017
		$result = $this->Html->script('foo', array('inline' => true, 'once' => false));
1018
		$this->assertNotNull($result);
1019
 
1020
		$result = $this->Html->script('jquery-1.3.2', array('defer' => true, 'encoding' => 'utf-8'));
1021
		$expected = array(
1022
			'script' => array('type' => 'text/javascript', 'src' => 'js/jquery-1.3.2.js', 'defer' => 'defer', 'encoding' => 'utf-8')
1023
		);
1024
		$this->assertTags($result, $expected);
1025
	}
1026
 
1027
/**
1028
 * test that plugin scripts added with uses() are only ever included once.
1029
 * test script tag generation with plugin syntax
1030
 *
1031
 * @return void
1032
 */
1033
	public function testPluginScript() {
1034
		CakePlugin::load('TestPlugin');
1035
 
1036
		$result = $this->Html->script('TestPlugin.foo');
1037
		$expected = array(
1038
			'script' => array('type' => 'text/javascript', 'src' => 'test_plugin/js/foo.js')
1039
		);
1040
		$this->assertTags($result, $expected);
1041
 
1042
		$result = $this->Html->script(array('TestPlugin.foobar', 'TestPlugin.bar'));
1043
		$expected = array(
1044
			array('script' => array('type' => 'text/javascript', 'src' => 'test_plugin/js/foobar.js')),
1045
			'/script',
1046
			array('script' => array('type' => 'text/javascript', 'src' => 'test_plugin/js/bar.js')),
1047
			'/script',
1048
		);
1049
		$this->assertTags($result, $expected);
1050
 
1051
		$result = $this->Html->script('TestPlugin.jquery-1.3');
1052
		$expected = array(
1053
			'script' => array('type' => 'text/javascript', 'src' => 'test_plugin/js/jquery-1.3.js')
1054
		);
1055
		$this->assertTags($result, $expected);
1056
 
1057
		$result = $this->Html->script('TestPlugin.test.json');
1058
		$expected = array(
1059
			'script' => array('type' => 'text/javascript', 'src' => 'test_plugin/js/test.json.js')
1060
		);
1061
		$this->assertTags($result, $expected);
1062
 
1063
		$result = $this->Html->script('TestPlugin./jquery-1.3.2.js?someparam=foo');
1064
		$expected = array(
1065
			'script' => array('type' => 'text/javascript', 'src' => 'test_plugin/jquery-1.3.2.js?someparam=foo')
1066
		);
1067
		$this->assertTags($result, $expected);
1068
 
1069
		$result = $this->Html->script('TestPlugin.test.json.js?foo=bar');
1070
		$expected = array(
1071
			'script' => array('type' => 'text/javascript', 'src' => 'test_plugin/js/test.json.js?foo=bar')
1072
		);
1073
		$this->assertTags($result, $expected);
1074
 
1075
		$result = $this->Html->script('TestPlugin.foo');
1076
		$this->assertNull($result, 'Script returned upon duplicate inclusion %s');
1077
 
1078
		$result = $this->Html->script(array('TestPlugin.foo', 'TestPlugin.bar', 'TestPlugin.baz'));
1079
		$this->assertNotRegExp('/test_plugin\/js\/foo.js/', $result);
1080
 
1081
		$result = $this->Html->script('TestPlugin.foo', array('inline' => true, 'once' => false));
1082
		$this->assertNotNull($result);
1083
 
1084
		$result = $this->Html->script('TestPlugin.jquery-1.3.2', array('defer' => true, 'encoding' => 'utf-8'));
1085
		$expected = array(
1086
			'script' => array('type' => 'text/javascript', 'src' => 'test_plugin/js/jquery-1.3.2.js', 'defer' => 'defer', 'encoding' => 'utf-8')
1087
		);
1088
		$this->assertTags($result, $expected);
1089
 
1090
		CakePlugin::unload('TestPlugin');
1091
	}
1092
 
1093
/**
1094
 * test that script() works with blocks.
1095
 *
1096
 * @return void
1097
 */
1098
	public function testScriptWithBlocks() {
1099
		$this->View->expects($this->at(0))
1100
			->method('append')
1101
			->with('script', $this->matchesRegularExpression('/script_in_head.js/'));
1102
 
1103
		$this->View->expects($this->at(1))
1104
			->method('append')
1105
			->with('script', $this->matchesRegularExpression('/bool_false.js/'));
1106
 
1107
		$this->View->expects($this->at(2))
1108
			->method('append')
1109
			->with('headScripts', $this->matchesRegularExpression('/second_script.js/'));
1110
 
1111
		$result = $this->Html->script('script_in_head', array('inline' => false));
1112
		$this->assertNull($result);
1113
 
1114
		$result = $this->Html->script('bool_false', false);
1115
		$this->assertNull($result);
1116
 
1117
		$result = $this->Html->script('second_script', array('block' => 'headScripts'));
1118
		$this->assertNull($result);
1119
	}
1120
 
1121
/**
1122
 * Test that Asset.filter.js works.
1123
 *
1124
 * @return void
1125
 */
1126
	public function testScriptAssetFilter() {
1127
		Configure::write('Asset.filter.js', 'js.php');
1128
 
1129
		$result = $this->Html->script('jquery-1.3');
1130
		$expected = array(
1131
			'script' => array('type' => 'text/javascript', 'src' => 'cjs/jquery-1.3.js')
1132
		);
1133
		$this->assertTags($result, $expected);
1134
 
1135
		$result = $this->Html->script('//example.com/js/jquery-1.3.js');
1136
		$expected = array(
1137
			'script' => array('type' => 'text/javascript', 'src' => '//example.com/js/jquery-1.3.js')
1138
		);
1139
		$this->assertTags($result, $expected);
1140
	}
1141
 
1142
/**
1143
 * testScriptWithFullBase method
1144
 *
1145
 * @return void
1146
 */
1147
	public function testScriptWithFullBase() {
1148
		$here = $this->Html->url('/', true);
1149
 
1150
		$result = $this->Html->script('foo', array('fullBase' => true));
1151
		$expected = array(
1152
			'script' => array('type' => 'text/javascript', 'src' => $here . 'js/foo.js')
1153
		);
1154
		$this->assertTags($result, $expected);
1155
 
1156
		$result = $this->Html->script(array('foobar', 'bar'), array('fullBase' => true));
1157
		$expected = array(
1158
			array('script' => array('type' => 'text/javascript', 'src' => $here . 'js/foobar.js')),
1159
			'/script',
1160
			array('script' => array('type' => 'text/javascript', 'src' => $here . 'js/bar.js')),
1161
			'/script',
1162
		);
1163
		$this->assertTags($result, $expected);
1164
	}
1165
 
1166
/**
1167
 * test a script file in the webroot/theme dir.
1168
 *
1169
 * @return void
1170
 */
1171
	public function testScriptInTheme() {
1172
		$this->skipIf(!is_writable(WWW_ROOT), 'Cannot write to webroot.');
1173
		$themeExists = is_dir(WWW_ROOT . 'theme');
1174
 
1175
		App::uses('File', 'Utility');
1176
 
1177
		$testfile = WWW_ROOT . 'theme' . DS . 'test_theme' . DS . 'js' . DS . '__test_js.js';
1178
		new File($testfile, true);
1179
 
1180
		App::build(array(
1181
			'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
1182
		));
1183
 
1184
		$this->Html->webroot = '/';
1185
		$this->Html->theme = 'test_theme';
1186
		$result = $this->Html->script('__test_js.js');
1187
		$expected = array(
1188
			'script' => array('src' => '/theme/test_theme/js/__test_js.js', 'type' => 'text/javascript')
1189
		);
1190
		$this->assertTags($result, $expected);
1191
 
1192
		$Folder = new Folder(WWW_ROOT . 'theme' . DS . 'test_theme');
1193
		$Folder->delete();
1194
 
1195
		if (!$themeExists) {
1196
			$dir = new Folder(WWW_ROOT . 'theme');
1197
			$dir->delete();
1198
		}
1199
	}
1200
 
1201
/**
1202
 * test Script block generation
1203
 *
1204
 * @return void
1205
 */
1206
	public function testScriptBlock() {
1207
		$result = $this->Html->scriptBlock('window.foo = 2;');
1208
		$expected = array(
1209
			'script' => array('type' => 'text/javascript'),
1210
			$this->cDataStart,
1211
			'window.foo = 2;',
1212
			$this->cDataEnd,
1213
			'/script',
1214
		);
1215
		$this->assertTags($result, $expected);
1216
 
1217
		$result = $this->Html->scriptBlock('window.foo = 2;', array('type' => 'text/x-handlebars-template'));
1218
		$expected = array(
1219
			'script' => array('type' => 'text/x-handlebars-template'),
1220
			$this->cDataStart,
1221
			'window.foo = 2;',
1222
			$this->cDataEnd,
1223
			'/script',
1224
		);
1225
		$this->assertTags($result, $expected);
1226
 
1227
		$result = $this->Html->scriptBlock('window.foo = 2;', array('safe' => false));
1228
		$expected = array(
1229
			'script' => array('type' => 'text/javascript'),
1230
			'window.foo = 2;',
1231
			'/script',
1232
		);
1233
		$this->assertTags($result, $expected);
1234
 
1235
		$result = $this->Html->scriptBlock('window.foo = 2;', array('safe' => true));
1236
		$expected = array(
1237
			'script' => array('type' => 'text/javascript'),
1238
			$this->cDataStart,
1239
			'window.foo = 2;',
1240
			$this->cDataEnd,
1241
			'/script',
1242
		);
1243
		$this->assertTags($result, $expected);
1244
 
1245
		$this->View->expects($this->at(0))
1246
			->method('append')
1247
			->with('script', $this->matchesRegularExpression('/window\.foo\s\=\s2;/'));
1248
 
1249
		$this->View->expects($this->at(1))
1250
			->method('append')
1251
			->with('scriptTop', $this->stringContains('alert('));
1252
 
1253
		$result = $this->Html->scriptBlock('window.foo = 2;', array('inline' => false));
1254
		$this->assertNull($result);
1255
 
1256
		$result = $this->Html->scriptBlock('alert("hi")', array('block' => 'scriptTop'));
1257
		$this->assertNull($result);
1258
 
1259
		$result = $this->Html->scriptBlock('window.foo = 2;', array('safe' => false, 'encoding' => 'utf-8'));
1260
		$expected = array(
1261
			'script' => array('type' => 'text/javascript', 'encoding' => 'utf-8'),
1262
			'window.foo = 2;',
1263
			'/script',
1264
		);
1265
		$this->assertTags($result, $expected);
1266
	}
1267
 
1268
/**
1269
 * test script tag output buffering when using scriptStart() and scriptEnd();
1270
 *
1271
 * @return void
1272
 */
1273
	public function testScriptStartAndScriptEnd() {
1274
		$result = $this->Html->scriptStart(array('safe' => true));
1275
		$this->assertNull($result);
1276
		echo 'this is some javascript';
1277
 
1278
		$result = $this->Html->scriptEnd();
1279
		$expected = array(
1280
			'script' => array('type' => 'text/javascript'),
1281
			$this->cDataStart,
1282
			'this is some javascript',
1283
			$this->cDataEnd,
1284
			'/script'
1285
		);
1286
		$this->assertTags($result, $expected);
1287
 
1288
		$result = $this->Html->scriptStart(array('safe' => false));
1289
		$this->assertNull($result);
1290
		echo 'this is some javascript';
1291
 
1292
		$result = $this->Html->scriptEnd();
1293
		$expected = array(
1294
			'script' => array('type' => 'text/javascript'),
1295
			'this is some javascript',
1296
			'/script'
1297
		);
1298
		$this->assertTags($result, $expected);
1299
 
1300
		$result = $this->Html->scriptStart(array('safe' => true, 'type' => 'text/x-handlebars-template'));
1301
		$this->assertNull($result);
1302
		echo 'this is some template';
1303
 
1304
		$result = $this->Html->scriptEnd();
1305
		$expected = array(
1306
			'script' => array('type' => 'text/x-handlebars-template'),
1307
			$this->cDataStart,
1308
			'this is some template',
1309
			$this->cDataEnd,
1310
			'/script'
1311
		);
1312
		$this->assertTags($result, $expected);
1313
 
1314
		$this->View->expects($this->once())
1315
			->method('append');
1316
		$result = $this->Html->scriptStart(array('safe' => false, 'inline' => false));
1317
		$this->assertNull($result);
1318
		echo 'this is some javascript';
1319
 
1320
		$result = $this->Html->scriptEnd();
1321
		$this->assertNull($result);
1322
	}
1323
 
1324
/**
1325
 * testCharsetTag method
1326
 *
1327
 * @return void
1328
 */
1329
	public function testCharsetTag() {
1330
		Configure::write('App.encoding', null);
1331
		$result = $this->Html->charset();
1332
		$this->assertTags($result, array('meta' => array('http-equiv' => 'Content-Type', 'content' => 'text/html; charset=utf-8')));
1333
 
1334
		Configure::write('App.encoding', 'ISO-8859-1');
1335
		$result = $this->Html->charset();
1336
		$this->assertTags($result, array('meta' => array('http-equiv' => 'Content-Type', 'content' => 'text/html; charset=iso-8859-1')));
1337
 
1338
		$result = $this->Html->charset('UTF-7');
1339
		$this->assertTags($result, array('meta' => array('http-equiv' => 'Content-Type', 'content' => 'text/html; charset=UTF-7')));
1340
	}
1341
 
1342
/**
1343
 * testGetCrumb and addCrumb method
1344
 *
1345
 * @return void
1346
 */
1347
	public function testBreadcrumb() {
1348
		$this->assertNull($this->Html->getCrumbs());
1349
 
1350
		$this->Html->addCrumb('First', '#first');
1351
		$this->Html->addCrumb('Second', '#second');
1352
		$this->Html->addCrumb('Third', '#third');
1353
 
1354
		$result = $this->Html->getCrumbs();
1355
		$expected = array(
1356
			array('a' => array('href' => '#first')),
1357
			'First',
1358
			'/a',
1359
			'&raquo;',
1360
			array('a' => array('href' => '#second')),
1361
			'Second',
1362
			'/a',
1363
			'&raquo;',
1364
			array('a' => array('href' => '#third')),
1365
			'Third',
1366
			'/a',
1367
		);
1368
		$this->assertTags($result, $expected);
1369
 
1370
		$result = $this->Html->getCrumbs(' &gt; ');
1371
		$expected = array(
1372
			array('a' => array('href' => '#first')),
1373
			'First',
1374
			'/a',
1375
			' &gt; ',
1376
			array('a' => array('href' => '#second')),
1377
			'Second',
1378
			'/a',
1379
			' &gt; ',
1380
			array('a' => array('href' => '#third')),
1381
			'Third',
1382
			'/a',
1383
		);
1384
		$this->assertTags($result, $expected);
1385
 
1386
		$this->Html->addCrumb('Fourth', null);
1387
 
1388
		$result = $this->Html->getCrumbs();
1389
		$expected = array(
1390
			array('a' => array('href' => '#first')),
1391
			'First',
1392
			'/a',
1393
			'&raquo;',
1394
			array('a' => array('href' => '#second')),
1395
			'Second',
1396
			'/a',
1397
			'&raquo;',
1398
			array('a' => array('href' => '#third')),
1399
			'Third',
1400
			'/a',
1401
			'&raquo;',
1402
			'Fourth'
1403
		);
1404
		$this->assertTags($result, $expected);
1405
 
1406
		$result = $this->Html->getCrumbs('-', 'Start');
1407
		$expected = array(
1408
			array('a' => array('href' => '/')),
1409
			'Start',
1410
			'/a',
1411
			'-',
1412
			array('a' => array('href' => '#first')),
1413
			'First',
1414
			'/a',
1415
			'-',
1416
			array('a' => array('href' => '#second')),
1417
			'Second',
1418
			'/a',
1419
			'-',
1420
			array('a' => array('href' => '#third')),
1421
			'Third',
1422
			'/a',
1423
			'-',
1424
			'Fourth'
1425
		);
1426
		$this->assertTags($result, $expected);
1427
	}
1428
 
1429
/**
1430
 * Test the array form of $startText
1431
 */
1432
	public function testGetCrumbFirstLink() {
1433
		$result = $this->Html->getCrumbList(null, 'Home');
1434
		$this->assertTags(
1435
			$result,
1436
			array(
1437
				'<ul',
1438
				array('li' => array('class' => 'first')),
1439
				array('a' => array('href' => '/')), 'Home', '/a',
1440
				'/li',
1441
				'/ul'
1442
			)
1443
		);
1444
 
1445
		$this->Html->addCrumb('First', '#first');
1446
		$this->Html->addCrumb('Second', '#second');
1447
 
1448
		$result = $this->Html->getCrumbs(' - ', array('url' => '/home', 'text' => '<img src="/home.png" />', 'escape' => false));
1449
		$expected = array(
1450
			array('a' => array('href' => '/home')),
1451
			'img' => array('src' => '/home.png'),
1452
			'/a',
1453
			' - ',
1454
			array('a' => array('href' => '#first')),
1455
			'First',
1456
			'/a',
1457
			' - ',
1458
			array('a' => array('href' => '#second')),
1459
			'Second',
1460
			'/a',
1461
		);
1462
		$this->assertTags($result, $expected);
1463
	}
1464
 
1465
/**
1466
 * testNestedList method
1467
 *
1468
 * @return void
1469
 */
1470
	public function testNestedList() {
1471
		$list = array(
1472
			'Item 1',
1473
			'Item 2' => array(
1474
				'Item 2.1'
1475
			),
1476
			'Item 3',
1477
			'Item 4' => array(
1478
				'Item 4.1',
1479
				'Item 4.2',
1480
				'Item 4.3' => array(
1481
					'Item 4.3.1',
1482
					'Item 4.3.2'
1483
				)
1484
			),
1485
			'Item 5' => array(
1486
				'Item 5.1',
1487
				'Item 5.2'
1488
			)
1489
		);
1490
 
1491
		$result = $this->Html->nestedList($list);
1492
		$expected = array(
1493
			'<ul',
1494
			'<li', 'Item 1', '/li',
1495
			'<li', 'Item 2',
1496
			'<ul', '<li', 'Item 2.1', '/li', '/ul',
1497
			'/li',
1498
			'<li', 'Item 3', '/li',
1499
			'<li', 'Item 4',
1500
			'<ul',
1501
			'<li', 'Item 4.1', '/li',
1502
			'<li', 'Item 4.2', '/li',
1503
			'<li', 'Item 4.3',
1504
			'<ul',
1505
			'<li', 'Item 4.3.1', '/li',
1506
			'<li', 'Item 4.3.2', '/li',
1507
			'/ul',
1508
			'/li',
1509
			'/ul',
1510
			'/li',
1511
			'<li', 'Item 5',
1512
			'<ul',
1513
			'<li', 'Item 5.1', '/li',
1514
			'<li', 'Item 5.2', '/li',
1515
			'/ul',
1516
			'/li',
1517
			'/ul'
1518
		);
1519
		$this->assertTags($result, $expected);
1520
 
1521
		$result = $this->Html->nestedList($list, null);
1522
		$this->assertTags($result, $expected);
1523
 
1524
		$result = $this->Html->nestedList($list, array(), array(), 'ol');
1525
		$expected = array(
1526
			'<ol',
1527
			'<li', 'Item 1', '/li',
1528
			'<li', 'Item 2',
1529
			'<ol', '<li', 'Item 2.1', '/li', '/ol',
1530
			'/li',
1531
			'<li', 'Item 3', '/li',
1532
			'<li', 'Item 4',
1533
			'<ol',
1534
			'<li', 'Item 4.1', '/li',
1535
			'<li', 'Item 4.2', '/li',
1536
			'<li', 'Item 4.3',
1537
			'<ol',
1538
			'<li', 'Item 4.3.1', '/li',
1539
			'<li', 'Item 4.3.2', '/li',
1540
			'/ol',
1541
			'/li',
1542
			'/ol',
1543
			'/li',
1544
			'<li', 'Item 5',
1545
			'<ol',
1546
			'<li', 'Item 5.1', '/li',
1547
			'<li', 'Item 5.2', '/li',
1548
			'/ol',
1549
			'/li',
1550
			'/ol'
1551
		);
1552
		$this->assertTags($result, $expected);
1553
 
1554
		$result = $this->Html->nestedList($list, 'ol');
1555
		$this->assertTags($result, $expected);
1556
 
1557
		$result = $this->Html->nestedList($list, array('class' => 'list'));
1558
		$expected = array(
1559
			array('ul' => array('class' => 'list')),
1560
			'<li', 'Item 1', '/li',
1561
			'<li', 'Item 2',
1562
			array('ul' => array('class' => 'list')), '<li', 'Item 2.1', '/li', '/ul',
1563
			'/li',
1564
			'<li', 'Item 3', '/li',
1565
			'<li', 'Item 4',
1566
			array('ul' => array('class' => 'list')),
1567
			'<li', 'Item 4.1', '/li',
1568
			'<li', 'Item 4.2', '/li',
1569
			'<li', 'Item 4.3',
1570
			array('ul' => array('class' => 'list')),
1571
			'<li', 'Item 4.3.1', '/li',
1572
			'<li', 'Item 4.3.2', '/li',
1573
			'/ul',
1574
			'/li',
1575
			'/ul',
1576
			'/li',
1577
			'<li', 'Item 5',
1578
			array('ul' => array('class' => 'list')),
1579
			'<li', 'Item 5.1', '/li',
1580
			'<li', 'Item 5.2', '/li',
1581
			'/ul',
1582
			'/li',
1583
			'/ul'
1584
		);
1585
		$this->assertTags($result, $expected);
1586
 
1587
		$result = $this->Html->nestedList($list, array(), array('class' => 'item'));
1588
		$expected = array(
1589
			'<ul',
1590
			array('li' => array('class' => 'item')), 'Item 1', '/li',
1591
			array('li' => array('class' => 'item')), 'Item 2',
1592
			'<ul', array('li' => array('class' => 'item')), 'Item 2.1', '/li', '/ul',
1593
			'/li',
1594
			array('li' => array('class' => 'item')), 'Item 3', '/li',
1595
			array('li' => array('class' => 'item')), 'Item 4',
1596
			'<ul',
1597
			array('li' => array('class' => 'item')), 'Item 4.1', '/li',
1598
			array('li' => array('class' => 'item')), 'Item 4.2', '/li',
1599
			array('li' => array('class' => 'item')), 'Item 4.3',
1600
			'<ul',
1601
			array('li' => array('class' => 'item')), 'Item 4.3.1', '/li',
1602
			array('li' => array('class' => 'item')), 'Item 4.3.2', '/li',
1603
			'/ul',
1604
			'/li',
1605
			'/ul',
1606
			'/li',
1607
			array('li' => array('class' => 'item')), 'Item 5',
1608
			'<ul',
1609
			array('li' => array('class' => 'item')), 'Item 5.1', '/li',
1610
			array('li' => array('class' => 'item')), 'Item 5.2', '/li',
1611
			'/ul',
1612
			'/li',
1613
			'/ul'
1614
		);
1615
		$this->assertTags($result, $expected);
1616
 
1617
		$result = $this->Html->nestedList($list, array(), array('even' => 'even', 'odd' => 'odd'));
1618
		$expected = array(
1619
			'<ul',
1620
			array('li' => array('class' => 'odd')), 'Item 1', '/li',
1621
			array('li' => array('class' => 'even')), 'Item 2',
1622
			'<ul', array('li' => array('class' => 'odd')), 'Item 2.1', '/li', '/ul',
1623
			'/li',
1624
			array('li' => array('class' => 'odd')), 'Item 3', '/li',
1625
			array('li' => array('class' => 'even')), 'Item 4',
1626
			'<ul',
1627
			array('li' => array('class' => 'odd')), 'Item 4.1', '/li',
1628
			array('li' => array('class' => 'even')), 'Item 4.2', '/li',
1629
			array('li' => array('class' => 'odd')), 'Item 4.3',
1630
			'<ul',
1631
			array('li' => array('class' => 'odd')), 'Item 4.3.1', '/li',
1632
			array('li' => array('class' => 'even')), 'Item 4.3.2', '/li',
1633
			'/ul',
1634
			'/li',
1635
			'/ul',
1636
			'/li',
1637
			array('li' => array('class' => 'odd')), 'Item 5',
1638
			'<ul',
1639
			array('li' => array('class' => 'odd')), 'Item 5.1', '/li',
1640
			array('li' => array('class' => 'even')), 'Item 5.2', '/li',
1641
			'/ul',
1642
			'/li',
1643
			'/ul'
1644
		);
1645
		$this->assertTags($result, $expected);
1646
 
1647
		$result = $this->Html->nestedList($list, array('class' => 'list'), array('class' => 'item'));
1648
		$expected = array(
1649
			array('ul' => array('class' => 'list')),
1650
			array('li' => array('class' => 'item')), 'Item 1', '/li',
1651
			array('li' => array('class' => 'item')), 'Item 2',
1652
			array('ul' => array('class' => 'list')), array('li' => array('class' => 'item')), 'Item 2.1', '/li', '/ul',
1653
			'/li',
1654
			array('li' => array('class' => 'item')), 'Item 3', '/li',
1655
			array('li' => array('class' => 'item')), 'Item 4',
1656
			array('ul' => array('class' => 'list')),
1657
			array('li' => array('class' => 'item')), 'Item 4.1', '/li',
1658
			array('li' => array('class' => 'item')), 'Item 4.2', '/li',
1659
			array('li' => array('class' => 'item')), 'Item 4.3',
1660
			array('ul' => array('class' => 'list')),
1661
			array('li' => array('class' => 'item')), 'Item 4.3.1', '/li',
1662
			array('li' => array('class' => 'item')), 'Item 4.3.2', '/li',
1663
			'/ul',
1664
			'/li',
1665
			'/ul',
1666
			'/li',
1667
			array('li' => array('class' => 'item')), 'Item 5',
1668
			array('ul' => array('class' => 'list')),
1669
			array('li' => array('class' => 'item')), 'Item 5.1', '/li',
1670
			array('li' => array('class' => 'item')), 'Item 5.2', '/li',
1671
			'/ul',
1672
			'/li',
1673
			'/ul'
1674
		);
1675
		$this->assertTags($result, $expected);
1676
	}
1677
 
1678
/**
1679
 * testMeta method
1680
 *
1681
 * @return void
1682
 */
1683
	public function testMeta() {
1684
		$result = $this->Html->meta('this is an rss feed', array('controller' => 'posts', 'ext' => 'rss'));
1685
		$this->assertTags($result, array('link' => array('href' => 'preg:/.*\/posts\.rss/', 'type' => 'application/rss+xml', 'rel' => 'alternate', 'title' => 'this is an rss feed')));
1686
 
1687
		$result = $this->Html->meta('rss', array('controller' => 'posts', 'ext' => 'rss'), array('title' => 'this is an rss feed'));
1688
		$this->assertTags($result, array('link' => array('href' => 'preg:/.*\/posts\.rss/', 'type' => 'application/rss+xml', 'rel' => 'alternate', 'title' => 'this is an rss feed')));
1689
 
1690
		$result = $this->Html->meta('atom', array('controller' => 'posts', 'ext' => 'xml'));
1691
		$this->assertTags($result, array('link' => array('href' => 'preg:/.*\/posts\.xml/', 'type' => 'application/atom+xml', 'title' => 'atom')));
1692
 
1693
		$result = $this->Html->meta('non-existing');
1694
		$this->assertTags($result, array('<meta'));
1695
 
1696
		$result = $this->Html->meta('non-existing', '/posts.xpp');
1697
		$this->assertTags($result, array('link' => array('href' => 'preg:/.*\/posts\.xpp/', 'type' => 'application/rss+xml', 'rel' => 'alternate', 'title' => 'non-existing')));
1698
 
1699
		$result = $this->Html->meta('non-existing', '/posts.xpp', array('type' => 'atom'));
1700
		$this->assertTags($result, array('link' => array('href' => 'preg:/.*\/posts\.xpp/', 'type' => 'application/atom+xml', 'title' => 'non-existing')));
1701
 
1702
		$result = $this->Html->meta('atom', array('controller' => 'posts', 'ext' => 'xml'), array('link' => '/articles.rss'));
1703
		$this->assertTags($result, array('link' => array('href' => 'preg:/.*\/articles\.rss/', 'type' => 'application/atom+xml', 'title' => 'atom')));
1704
 
1705
		$result = $this->Html->meta(array('link' => 'favicon.ico', 'rel' => 'icon'));
1706
		$expected = array(
1707
			'link' => array('href' => 'preg:/.*favicon\.ico/', 'rel' => 'icon'),
1708
			array('link' => array('href' => 'preg:/.*favicon\.ico/', 'rel' => 'shortcut icon'))
1709
		);
1710
		$this->assertTags($result, $expected);
1711
 
1712
		$result = $this->Html->meta('keywords', 'these, are, some, meta, keywords');
1713
		$this->assertTags($result, array('meta' => array('name' => 'keywords', 'content' => 'these, are, some, meta, keywords')));
1714
		$this->assertRegExp('/\s+\/>$/', $result);
1715
 
1716
		$result = $this->Html->meta('description', 'this is the meta description');
1717
		$this->assertTags($result, array('meta' => array('name' => 'description', 'content' => 'this is the meta description')));
1718
 
1719
		$result = $this->Html->meta(array('name' => 'ROBOTS', 'content' => 'ALL'));
1720
		$this->assertTags($result, array('meta' => array('name' => 'ROBOTS', 'content' => 'ALL')));
1721
	}
1722
 
1723
/**
1724
 * Test generating favicon's with meta()
1725
 *
1726
 * @return void
1727
 */
1728
	public function testMetaIcon() {
1729
		$result = $this->Html->meta('icon', 'favicon.ico');
1730
		$expected = array(
1731
			'link' => array('href' => 'preg:/.*favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'icon'),
1732
			array('link' => array('href' => 'preg:/.*favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'shortcut icon'))
1733
		);
1734
		$this->assertTags($result, $expected);
1735
 
1736
		$result = $this->Html->meta('icon');
1737
		$expected = array(
1738
			'link' => array('href' => 'preg:/.*favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'icon'),
1739
			array('link' => array('href' => 'preg:/.*favicon\.ico/', 'type' => 'image/x-icon', 'rel' => 'shortcut icon'))
1740
		);
1741
		$this->assertTags($result, $expected);
1742
 
1743
		$result = $this->Html->meta('icon', '/favicon.png?one=two&three=four');
1744
		$url = '/favicon.png?one=two&amp;three=four';
1745
		$expected = array(
1746
			'link' => array(
1747
				'href' => $url,
1748
				'type' => 'image/x-icon',
1749
				'rel' => 'icon'
1750
			),
1751
			array(
1752
				'link' => array(
1753
					'href' => $url,
1754
					'type' => 'image/x-icon',
1755
					'rel' => 'shortcut icon'
1756
				)
1757
			)
1758
		);
1759
		$this->assertTags($result, $expected);
1760
 
1761
		$this->Html->request->webroot = '/testing/';
1762
		$result = $this->Html->meta('icon');
1763
		$expected = array(
1764
			'link' => array('href' => '/testing/favicon.ico', 'type' => 'image/x-icon', 'rel' => 'icon'),
1765
			array('link' => array('href' => '/testing/favicon.ico', 'type' => 'image/x-icon', 'rel' => 'shortcut icon'))
1766
		);
1767
		$this->assertTags($result, $expected);
1768
	}
1769
 
1770
/**
1771
 * Test the inline and block options for meta()
1772
 */
1773
	public function testMetaWithBlocks() {
1774
		$this->View->expects($this->at(0))
1775
			->method('append')
1776
			->with('meta', $this->stringContains('ROBOTS'));
1777
 
1778
		$this->View->expects($this->at(1))
1779
			->method('append')
1780
			->with('metaTags', $this->stringContains('favicon.ico'));
1781
 
1782
		$result = $this->Html->meta(array('name' => 'ROBOTS', 'content' => 'ALL'), null, array('inline' => false));
1783
		$this->assertNull($result);
1784
 
1785
		$result = $this->Html->meta('icon', 'favicon.ico', array('block' => 'metaTags'));
1786
		$this->assertNull($result);
1787
	}
1788
 
1789
/**
1790
 * testTableHeaders method
1791
 *
1792
 * @return void
1793
 */
1794
	public function testTableHeaders() {
1795
		$result = $this->Html->tableHeaders(array('ID', 'Name', 'Date'));
1796
		$expected = array('<tr', '<th', 'ID', '/th', '<th', 'Name', '/th', '<th', 'Date', '/th', '/tr');
1797
		$this->assertTags($result, $expected);
1798
 
1799
		$result = $this->Html->tableHeaders(array('ID', array('Name' => array('class' => 'highlight')), 'Date'));
1800
		$expected = array('<tr', '<th', 'ID', '/th', '<th class="highlight"', 'Name', '/th', '<th', 'Date', '/th', '/tr');
1801
		$this->assertTags($result, $expected);
1802
 
1803
		$result = $this->Html->tableHeaders(array('ID', array('Name' => array('class' => 'highlight', 'width' => '120px')), 'Date'));
1804
		$expected = array('<tr', '<th', 'ID', '/th', '<th class="highlight" width="120px"', 'Name', '/th', '<th', 'Date', '/th', '/tr');
1805
		$this->assertTags($result, $expected);
1806
 
1807
		$result = $this->Html->tableHeaders(array('ID', array('Name' => array()), 'Date'));
1808
		$expected = array('<tr', '<th', 'ID', '/th', '<th', 'Name', '/th', '<th', 'Date', '/th', '/tr');
1809
		$this->assertTags($result, $expected);
1810
	}
1811
 
1812
/**
1813
 * testTableCells method
1814
 *
1815
 * @return void
1816
 */
1817
	public function testTableCells() {
1818
		$tr = array(
1819
			'td content 1',
1820
			array('td content 2', array("width" => "100px")),
1821
			array('td content 3', "width=100px")
1822
		);
1823
		$result = $this->Html->tableCells($tr);
1824
		$expected = array(
1825
			'<tr',
1826
			'<td', 'td content 1', '/td',
1827
			array('td' => array('width' => '100px')), 'td content 2', '/td',
1828
			array('td' => array('width' => 'preg:/100px/')), 'td content 3', '/td',
1829
			'/tr'
1830
		);
1831
		$this->assertTags($result, $expected);
1832
 
1833
		$tr = array('td content 1', 'td content 2', 'td content 3');
1834
		$result = $this->Html->tableCells($tr, null, null, true);
1835
		$expected = array(
1836
			'<tr',
1837
			array('td' => array('class' => 'column-1')), 'td content 1', '/td',
1838
			array('td' => array('class' => 'column-2')), 'td content 2', '/td',
1839
			array('td' => array('class' => 'column-3')), 'td content 3', '/td',
1840
			'/tr'
1841
		);
1842
		$this->assertTags($result, $expected);
1843
 
1844
		$tr = array('td content 1', 'td content 2', 'td content 3');
1845
		$result = $this->Html->tableCells($tr, true);
1846
		$expected = array(
1847
			'<tr',
1848
			array('td' => array('class' => 'column-1')), 'td content 1', '/td',
1849
			array('td' => array('class' => 'column-2')), 'td content 2', '/td',
1850
			array('td' => array('class' => 'column-3')), 'td content 3', '/td',
1851
			'/tr'
1852
		);
1853
		$this->assertTags($result, $expected);
1854
 
1855
		$tr = array(
1856
			array('td content 1', 'td content 2', 'td content 3'),
1857
			array('td content 1', 'td content 2', 'td content 3'),
1858
			array('td content 1', 'td content 2', 'td content 3')
1859
		);
1860
		$result = $this->Html->tableCells($tr, array('class' => 'odd'), array('class' => 'even'));
1861
		$expected = "<tr class=\"even\"><td>td content 1</td> <td>td content 2</td> <td>td content 3</td></tr>\n<tr class=\"odd\"><td>td content 1</td> <td>td content 2</td> <td>td content 3</td></tr>\n<tr class=\"even\"><td>td content 1</td> <td>td content 2</td> <td>td content 3</td></tr>";
1862
		$this->assertEquals($expected, $result);
1863
 
1864
		$tr = array(
1865
			array('td content 1', 'td content 2', 'td content 3'),
1866
			array('td content 1', 'td content 2', 'td content 3'),
1867
			array('td content 1', 'td content 2', 'td content 3'),
1868
			array('td content 1', 'td content 2', 'td content 3')
1869
		);
1870
		$result = $this->Html->tableCells($tr, array('class' => 'odd'), array('class' => 'even'));
1871
		$expected = "<tr class=\"odd\"><td>td content 1</td> <td>td content 2</td> <td>td content 3</td></tr>\n<tr class=\"even\"><td>td content 1</td> <td>td content 2</td> <td>td content 3</td></tr>\n<tr class=\"odd\"><td>td content 1</td> <td>td content 2</td> <td>td content 3</td></tr>\n<tr class=\"even\"><td>td content 1</td> <td>td content 2</td> <td>td content 3</td></tr>";
1872
		$this->assertEquals($expected, $result);
1873
 
1874
		$tr = array(
1875
			array('td content 1', 'td content 2', 'td content 3'),
1876
			array('td content 1', 'td content 2', 'td content 3'),
1877
			array('td content 1', 'td content 2', 'td content 3')
1878
		);
1879
		$this->Html->tableCells($tr, array('class' => 'odd'), array('class' => 'even'));
1880
		$result = $this->Html->tableCells($tr, array('class' => 'odd'), array('class' => 'even'), false, false);
1881
		$expected = "<tr class=\"odd\"><td>td content 1</td> <td>td content 2</td> <td>td content 3</td></tr>\n<tr class=\"even\"><td>td content 1</td> <td>td content 2</td> <td>td content 3</td></tr>\n<tr class=\"odd\"><td>td content 1</td> <td>td content 2</td> <td>td content 3</td></tr>";
1882
		$this->assertEquals($expected, $result);
1883
	}
1884
 
1885
/**
1886
 * testTag method
1887
 *
1888
 * @return void
1889
 */
1890
	public function testTag() {
1891
		$result = $this->Html->tag('div');
1892
		$this->assertTags($result, '<div');
1893
 
1894
		$result = $this->Html->tag('div', 'text');
1895
		$this->assertTags($result, '<div', 'text', '/div');
1896
 
1897
		$result = $this->Html->tag('div', '<text>', array('class' => 'class-name', 'escape' => true));
1898
		$this->assertTags($result, array('div' => array('class' => 'class-name'), '&lt;text&gt;', '/div'));
1899
 
1900
		$result = $this->Html->tag(false, '<em>stuff</em>');
1901
		$this->assertEquals('<em>stuff</em>', $result);
1902
 
1903
		$result = $this->Html->tag(null, '<em>stuff</em>');
1904
		$this->assertEquals('<em>stuff</em>', $result);
1905
 
1906
		$result = $this->Html->tag('', '<em>stuff</em>');
1907
		$this->assertEquals('<em>stuff</em>', $result);
1908
	}
1909
 
1910
/**
1911
 * testUseTag method
1912
 *
1913
 * @return void
1914
 */
1915
	public function testUseTag() {
1916
		$result = $this->Html->useTag('unknowntag');
1917
		$this->assertEquals('', $result);
1918
 
1919
		$result = $this->Html->useTag('formend');
1920
		$this->assertTags($result, '/form');
1921
 
1922
		$result = $this->Html->useTag('form', 'url', ' test');
1923
		$this->assertEquals('<form action="url" test>', $result);
1924
 
1925
		$result = $this->Html->useTag('form', 'example.com', array('test' => 'ok'));
1926
		$this->assertTags($result, array('form' => array('test' => 'ok', 'action' => 'example.com')));
1927
	}
1928
 
1929
/**
1930
 * testDiv method
1931
 *
1932
 * @return void
1933
 */
1934
	public function testDiv() {
1935
		$result = $this->Html->div('class-name');
1936
		$this->assertTags($result, array('div' => array('class' => 'class-name')));
1937
 
1938
		$result = $this->Html->div('class-name', 'text');
1939
		$this->assertTags($result, array('div' => array('class' => 'class-name'), 'text', '/div'));
1940
 
1941
		$result = $this->Html->div('class-name', '<text>', array('escape' => true));
1942
		$this->assertTags($result, array('div' => array('class' => 'class-name'), '&lt;text&gt;', '/div'));
1943
	}
1944
 
1945
/**
1946
 * testPara method
1947
 *
1948
 * @return void
1949
 */
1950
	public function testPara() {
1951
		$result = $this->Html->para('class-name', '');
1952
		$this->assertTags($result, array('p' => array('class' => 'class-name')));
1953
 
1954
		$result = $this->Html->para('class-name', 'text');
1955
		$this->assertTags($result, array('p' => array('class' => 'class-name'), 'text', '/p'));
1956
 
1957
		$result = $this->Html->para('class-name', '<text>', array('escape' => true));
1958
		$this->assertTags($result, array('p' => array('class' => 'class-name'), '&lt;text&gt;', '/p'));
1959
	}
1960
 
1961
/**
1962
 * testMedia method
1963
 *
1964
 * @return void
1965
 */
1966
	public function testMedia() {
1967
		$result = $this->Html->media('video.webm');
1968
		$expected = array('video' => array('src' => 'files/video.webm'), '/video');
1969
		$this->assertTags($result, $expected);
1970
 
1971
		$result = $this->Html->media('video.webm', array(
1972
			'text' => 'Your browser does not support the HTML5 Video element.'
1973
		));
1974
		$expected = array('video' => array('src' => 'files/video.webm'), 'Your browser does not support the HTML5 Video element.', '/video');
1975
		$this->assertTags($result, $expected);
1976
 
1977
		$result = $this->Html->media('video.webm', array('autoload', 'muted' => 'muted'));
1978
		$expected = array(
1979
			'video' => array(
1980
				'src' => 'files/video.webm',
1981
				'autoload' => 'autoload',
1982
				'muted' => 'muted'
1983
			),
1984
			'/video'
1985
		);
1986
		$this->assertTags($result, $expected);
1987
 
1988
		$result = $this->Html->media(
1989
			array('video.webm', array('src' => 'video.ogv', 'type' => "video/ogg; codecs='theora, vorbis'")),
1990
			array('pathPrefix' => 'videos/', 'poster' => 'poster.jpg', 'text' => 'Your browser does not support the HTML5 Video element.')
1991
		);
1992
		$expected = array(
1993
			'video' => array('poster' => Configure::read('App.imageBaseUrl') . 'poster.jpg'),
1994
				array('source' => array('src' => 'videos/video.webm', 'type' => 'video/webm')),
1995
				array('source' => array('src' => 'videos/video.ogv', 'type' => 'video/ogg; codecs=&#039;theora, vorbis&#039;')),
1996
				'Your browser does not support the HTML5 Video element.',
1997
			'/video'
1998
		);
1999
		$this->assertTags($result, $expected);
2000
 
2001
		$result = $this->Html->media('video.ogv', array('tag' => 'video'));
2002
		$expected = array('video' => array('src' => 'files/video.ogv'), '/video');
2003
		$this->assertTags($result, $expected);
2004
 
2005
		$result = $this->Html->media('audio.mp3');
2006
		$expected = array('audio' => array('src' => 'files/audio.mp3'), '/audio');
2007
		$this->assertTags($result, $expected);
2008
 
2009
		$result = $this->Html->media(
2010
			array(array('src' => 'video.mov', 'type' => 'video/mp4'), 'video.webm')
2011
		);
2012
		$expected = array(
2013
			'<video',
2014
				array('source' => array('src' => 'files/video.mov', 'type' => 'video/mp4')),
2015
				array('source' => array('src' => 'files/video.webm', 'type' => 'video/webm')),
2016
			'/video'
2017
		);
2018
		$this->assertTags($result, $expected);
2019
 
2020
		$result = $this->Html->media(null, array('src' => 'video.webm'));
2021
		$expected = array(
2022
			'video' => array('src' => 'files/video.webm'),
2023
			'/video'
2024
		);
2025
		$this->assertTags($result, $expected);
2026
	}
2027
 
2028
/**
2029
 * testCrumbList method
2030
 *
2031
 *
2032
 * @return void
2033
 */
2034
	public function testCrumbList() {
2035
		$this->assertNull($this->Html->getCrumbList());
2036
 
2037
		$this->Html->addCrumb('Home', '/', array('class' => 'home'));
2038
		$this->Html->addCrumb('Some page', '/some_page');
2039
		$this->Html->addCrumb('Another page');
2040
		$result = $this->Html->getCrumbList(
2041
			array('class' => 'breadcrumbs')
2042
		);
2043
		$this->assertTags(
2044
			$result,
2045
			array(
2046
				array('ul' => array('class' => 'breadcrumbs')),
2047
				array('li' => array('class' => 'first')),
2048
				array('a' => array('class' => 'home', 'href' => '/')), 'Home', '/a',
2049
				'/li',
2050
				'<li',
2051
				array('a' => array('href' => '/some_page')), 'Some page', '/a',
2052
				'/li',
2053
				array('li' => array('class' => 'last')),
2054
				'Another page',
2055
				'/li',
2056
				'/ul'
2057
			)
2058
		);
2059
	}
2060
 
2061
/**
2062
 * Test getCrumbList startText
2063
 */
2064
	public function testCrumbListFirstLink() {
2065
		$this->Html->addCrumb('First', '#first');
2066
		$this->Html->addCrumb('Second', '#second');
2067
 
2068
		$result = $this->Html->getCrumbList(null, 'Home');
2069
		$this->assertTags(
2070
			$result,
2071
			array(
2072
				'<ul',
2073
				array('li' => array('class' => 'first')),
2074
				array('a' => array('href' => '/')), 'Home', '/a',
2075
				'/li',
2076
				'<li',
2077
				array('a' => array('href' => '#first')), 'First', '/a',
2078
				'/li',
2079
				array('li' => array('class' => 'last')),
2080
				array('a' => array('href' => '#second')), 'Second', '/a',
2081
				'/li',
2082
				'/ul'
2083
			)
2084
		);
2085
 
2086
		$result = $this->Html->getCrumbList(null, array('url' => '/home', 'text' => '<img src="/home.png" />', 'escape' => false));
2087
		$this->assertTags(
2088
			$result,
2089
			array(
2090
				'<ul',
2091
				array('li' => array('class' => 'first')),
2092
				array('a' => array('href' => '/home')), 'img' => array('src' => '/home.png'), '/a',
2093
				'/li',
2094
				'<li',
2095
				array('a' => array('href' => '#first')), 'First', '/a',
2096
				'/li',
2097
				array('li' => array('class' => 'last')),
2098
				array('a' => array('href' => '#second')), 'Second', '/a',
2099
				'/li',
2100
				'/ul'
2101
			)
2102
		);
2103
	}
2104
 
2105
/**
2106
 * test getCrumbList() in Twitter Bootstrap style.
2107
 *
2108
 * @return void
2109
 */
2110
	public function testCrumbListBootstrapStyle() {
2111
		$this->Html->addCrumb('Home', '/', array('class' => 'home'));
2112
		$this->Html->addCrumb('Library', '/lib');
2113
		$this->Html->addCrumb('Data');
2114
		$result = $this->Html->getCrumbList(array(
2115
			'class' => 'breadcrumb',
2116
			'separator' => '<span class="divider">-</span>',
2117
			'firstClass' => false,
2118
			'lastClass' => 'active'
2119
		));
2120
		$this->assertTags(
2121
			$result,
2122
			array(
2123
				array('ul' => array('class' => 'breadcrumb')),
2124
				'<li',
2125
				array('a' => array('class' => 'home', 'href' => '/')), 'Home', '/a',
2126
				array('span' => array('class' => 'divider')), '-', '/span',
2127
				'/li',
2128
				'<li',
2129
				array('a' => array('href' => '/lib')), 'Library', '/a',
2130
				array('span' => array('class' => 'divider')), '-', '/span',
2131
				'/li',
2132
				array('li' => array('class' => 'active')), 'Data', '/li',
2133
				'/ul'
2134
			)
2135
		);
2136
	}
2137
 
2138
/**
2139
 * Test GetCrumbList using style of Zurb Foundation.
2140
 *
2141
 * @return void
2142
 */
2143
	public function testCrumbListZurbStyle() {
2144
		$this->Html->addCrumb('Home', '#');
2145
		$this->Html->addCrumb('Features', '#');
2146
		$this->Html->addCrumb('Gene Splicing', '#');
2147
		$this->Html->addCrumb('Home', '#');
2148
		$result = $this->Html->getCrumbList(
2149
			array('class' => 'breadcrumbs', 'firstClass' => false, 'lastClass' => 'current')
2150
		);
2151
		$this->assertTags(
2152
			$result,
2153
			array(
2154
				array('ul' => array('class' => 'breadcrumbs')),
2155
				'<li',
2156
				array('a' => array('href' => '#')), 'Home', '/a',
2157
				'/li',
2158
				'<li',
2159
				array('a' => array('href' => '#')), 'Features', '/a',
2160
				'/li',
2161
				'<li',
2162
				array('a' => array('href' => '#')), 'Gene Splicing', '/a',
2163
				'/li',
2164
				array('li' => array('class' => 'current')),
2165
				array('a' => array('href' => '#')), 'Home', '/a',
2166
				'/li',
2167
				'/ul'
2168
			), true
2169
		);
2170
	}
2171
 
2172
/**
2173
 * testLoadConfig method
2174
 *
2175
 * @return void
2176
 */
2177
 
2178
	public function testLoadConfig() {
2179
		$path = CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS;
2180
 
2181
		$result = $this->Html->loadConfig('htmlhelper_tags', $path);
2182
		$expected = array(
2183
			'tags' => array(
2184
				'form' => 'start form',
2185
				'formend' => 'finish form',
2186
				'hiddenblock' => '<div class="hidden">%s</div>'
2187
			)
2188
		);
2189
		$this->assertEquals($expected, $result);
2190
		$tags = $this->Html->getAttribute('_tags');
2191
		$this->assertEquals('start form', $tags['form']);
2192
		$this->assertEquals('finish form', $tags['formend']);
2193
		$this->assertEquals('</select>', $tags['selectend']);
2194
 
2195
		$result = $this->Html->loadConfig(array('htmlhelper_minimized.ini', 'ini'), $path);
2196
		$expected = array(
2197
			'minimizedAttributeFormat' => 'format'
2198
		);
2199
		$this->assertEquals($expected, $result);
2200
		$this->assertEquals('format', $this->Html->getAttribute('_minimizedAttributeFormat'));
2201
	}
2202
 
2203
/**
2204
 * testLoadConfigWrongFile method
2205
 *
2206
 * @return void
2207
 * @expectedException ConfigureException
2208
 */
2209
	public function testLoadConfigWrongFile() {
2210
		$this->Html->loadConfig('wrong_file');
2211
	}
2212
 
2213
/**
2214
 * testLoadConfigWrongReader method
2215
 *
2216
 * @return void
2217
 * @expectedException ConfigureException
2218
 */
2219
	public function testLoadConfigWrongReader() {
2220
		$path = CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS;
2221
		$this->Html->loadConfig(array('htmlhelper_tags', 'wrong_reader'), $path);
2222
	}
2223
 
2224
/**
2225
 * test parsing attributes.
2226
 *
2227
 * @return void
2228
 */
2229
	public function testParseAttributeCompact() {
2230
		$helper = new TestHtmlHelper($this->View);
2231
		$compact = array('compact', 'checked', 'declare', 'readonly', 'disabled',
2232
			'selected', 'defer', 'ismap', 'nohref', 'noshade', 'nowrap', 'multiple', 'noresize');
2233
 
2234
		foreach ($compact as $attribute) {
2235
			foreach (array('true', true, 1, '1', $attribute) as $value) {
2236
				$attrs = array($attribute => $value);
2237
				$expected = ' ' . $attribute . '="' . $attribute . '"';
2238
				$this->assertEquals($expected, $helper->parseAttributes($attrs), '%s Failed on ' . $value);
2239
			}
2240
		}
2241
		$this->assertEquals(' compact="compact"', $helper->parseAttributes(array('compact')));
2242
 
2243
		$attrs = array('class' => array('foo', 'bar'));
2244
		$expected = ' class="foo bar"';
2245
		$this->assertEquals(' class="foo bar"', $helper->parseAttributes($attrs));
2246
 
2247
		$helper = new Html5TestHelper($this->View);
2248
		$expected = ' require';
2249
		$this->assertEquals($expected, $helper->parseAttributes(array('require')));
2250
		$this->assertEquals($expected, $helper->parseAttributes(array('require' => true)));
2251
		$this->assertEquals('', $helper->parseAttributes(array('require' => false)));
2252
	}
2253
 
2254
}