統計
| ブランチ: | リビジョン:

pictcode / lib / Cake / Test / Case / View / Helper / HtmlHelperTest.php @ 0b1b8047

履歴 | 表示 | アノテート | ダウンロード (72.419 KB)

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