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

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

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

1
<?php
2
/**
3
 * HelperTest file
4
 *
5
 * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
6
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
7
 *
8
 * Licensed under The MIT License
9
 * For full copyright and license information, please see the LICENSE.txt
10
 * Redistributions of files must retain the above copyright notice
11
 *
12
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13
 * @link          http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
14
 * @package       Cake.Test.Case.View
15
 * @since         CakePHP(tm) v 1.2.0.4206
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18

    
19
App::uses('View', 'View');
20
App::uses('Helper', 'View');
21
App::uses('Model', 'Model');
22
App::uses('Router', 'Routing');
23

    
24
/**
25
 * HelperTestPost class
26
 *
27
 * @package       Cake.Test.Case.View
28
 */
29
class HelperTestPost extends Model {
30

    
31
/**
32
 * useTable property
33
 *
34
 * @var bool
35
 */
36
        public $useTable = false;
37

    
38
/**
39
 * schema method
40
 *
41
 * @return void
42
 */
43
        public function schema($field = false) {
44
                $this->_schema = array(
45
                        'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
46
                        'title' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
47
                        'body' => array('type' => 'string', 'null' => true, 'default' => '', 'length' => ''),
48
                        'number' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
49
                        'date' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
50
                        'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
51
                        'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
52
                );
53
                return $this->_schema;
54
        }
55

    
56
/**
57
 * hasAndBelongsToMany property
58
 *
59
 * @var array
60
 */
61
        public $hasAndBelongsToMany = array('HelperTestTag' => array('with' => 'HelperTestPostsTag'));
62
}
63

    
64
/**
65
 * HelperTestComment class
66
 *
67
 * @package       Cake.Test.Case.View
68
 */
69
class HelperTestComment extends Model {
70

    
71
/**
72
 * useTable property
73
 *
74
 * @var bool
75
 */
76
        public $useTable = false;
77

    
78
/**
79
 * schema method
80
 *
81
 * @return void
82
 */
83
        public function schema($field = false) {
84
                $this->_schema = array(
85
                        'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
86
                        'author_id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
87
                        'title' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
88
                        'body' => array('type' => 'string', 'null' => true, 'default' => '', 'length' => ''),
89
                        'BigField' => array('type' => 'string', 'null' => true, 'default' => '', 'length' => ''),
90
                        'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
91
                        'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
92
                );
93
                return $this->_schema;
94
        }
95

    
96
}
97

    
98
/**
99
 * HelperTestTag class
100
 *
101
 * @package       Cake.Test.Case.View
102
 */
103
class HelperTestTag extends Model {
104

    
105
/**
106
 * useTable property
107
 *
108
 * @var bool
109
 */
110
        public $useTable = false;
111

    
112
/**
113
 * schema method
114
 *
115
 * @return void
116
 */
117
        public function schema($field = false) {
118
                $this->_schema = array(
119
                        'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
120
                        'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
121
                        'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
122
                        'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
123
                );
124
                return $this->_schema;
125
        }
126

    
127
}
128

    
129
/**
130
 * HelperTestPostsTag class
131
 *
132
 * @package       Cake.Test.Case.View
133
 */
134
class HelperTestPostsTag extends Model {
135

    
136
/**
137
 * useTable property
138
 *
139
 * @var bool
140
 */
141
        public $useTable = false;
142

    
143
/**
144
 * schema method
145
 *
146
 * @return void
147
 */
148
        public function schema($field = false) {
149
                $this->_schema = array(
150
                        'helper_test_post_id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
151
                        'helper_test_tag_id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
152
                );
153
                return $this->_schema;
154
        }
155

    
156
}
157

    
158
class TestHelper extends Helper {
159

    
160
/**
161
 * Settings for this helper.
162
 *
163
 * @var array
164
 */
165
        public $settings = array(
166
                'key1' => 'val1',
167
                'key2' => array('key2.1' => 'val2.1', 'key2.2' => 'val2.2')
168
        );
169

    
170
/**
171
 * Helpers for this helper.
172
 *
173
 * @var array
174
 */
175
        public $helpers = array('Html', 'TestPlugin.OtherHelper');
176

    
177
/**
178
 * expose a method as public
179
 *
180
 * @param string $options
181
 * @param string $exclude
182
 * @param string $insertBefore
183
 * @param string $insertAfter
184
 * @return void
185
 */
186
        public function parseAttributes($options, $exclude = null, $insertBefore = ' ', $insertAfter = null) {
187
                return $this->_parseAttributes($options, $exclude, $insertBefore, $insertAfter);
188
        }
189

    
190
}
191

    
192
/**
193
 * HelperTest class
194
 *
195
 * @package       Cake.Test.Case.View
196
 */
197
class HelperTest extends CakeTestCase {
198

    
199
/**
200
 * setUp method
201
 *
202
 * @return void
203
 */
204
        public function setUp() {
205
                parent::setUp();
206

    
207
                ClassRegistry::flush();
208
                Router::reload();
209
                $null = null;
210
                $this->View = new View($null);
211
                $this->Helper = new Helper($this->View);
212
                $this->Helper->request = new CakeRequest(null, false);
213

    
214
                ClassRegistry::addObject('HelperTestPost', new HelperTestPost());
215
                ClassRegistry::addObject('HelperTestComment', new HelperTestComment());
216
                ClassRegistry::addObject('HelperTestTag', new HelperTestTag());
217

    
218
                App::build(array(
219
                        'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
220
                ));
221
        }
222

    
223
/**
224
 * tearDown method
225
 *
226
 * @return void
227
 */
228
        public function tearDown() {
229
                parent::tearDown();
230
                Configure::delete('Asset');
231

    
232
                CakePlugin::unload();
233
                unset($this->Helper, $this->View);
234
        }
235

    
236
/**
237
 * Provider for setEntity test.
238
 *
239
 * @return array
240
 */
241
        public static function entityProvider() {
242
                return array(
243
                        array(
244
                                'HelperTestPost.id',
245
                                array('HelperTestPost', 'id'),
246
                                'HelperTestPost',
247
                                'id'
248
                        ),
249
                        array(
250
                                'HelperTestComment.body',
251
                                array('HelperTestComment', 'body'),
252
                                'HelperTestComment',
253
                                'body'
254
                        ),
255
                        array(
256
                                'HelperTest.1.Comment.body',
257
                                array('HelperTest', '1', 'Comment', 'body'),
258
                                'Comment',
259
                                'body'
260
                        ),
261
                        array(
262
                                'HelperTestComment.BigField',
263
                                array('HelperTestComment', 'BigField'),
264
                                'HelperTestComment',
265
                                'BigField'
266
                        ),
267
                        array(
268
                                'HelperTestComment.min',
269
                                array('HelperTestComment', 'min'),
270
                                'HelperTestComment',
271
                                'min'
272
                        )
273
                );
274
        }
275

    
276
/**
277
 * Test settings merging
278
 *
279
 * @return void
280
 */
281
        public function testSettingsMerging() {
282
                $Helper = new TestHelper($this->View, array(
283
                        'key3' => 'val3',
284
                        'key2' => array('key2.2' => 'newval')
285
                ));
286
                $expected = array(
287
                        'key1' => 'val1',
288
                        'key2' => array('key2.1' => 'val2.1', 'key2.2' => 'newval'),
289
                        'key3' => 'val3'
290
                );
291
                $this->assertEquals($expected, $Helper->settings);
292
        }
293

    
294
/**
295
 * Test setting an entity and retrieving the entity, model and field.
296
 *
297
 * @dataProvider entityProvider
298
 * @return void
299
 */
300
        public function testSetEntity($entity, $expected, $modelKey, $fieldKey) {
301
                $this->Helper->setEntity($entity);
302
                $this->assertEquals($expected, $this->Helper->entity());
303
                $this->assertEquals($modelKey, $this->Helper->model());
304
                $this->assertEquals($fieldKey, $this->Helper->field());
305
        }
306

    
307
/**
308
 * test setEntity with setting a scope.
309
 *
310
 * @return void
311
 */
312
        public function testSetEntityScoped() {
313
                $this->Helper->setEntity('HelperTestPost', true);
314
                $this->assertEquals(array('HelperTestPost'), $this->Helper->entity());
315

    
316
                $this->Helper->setEntity('id');
317
                $expected = array('HelperTestPost', 'id');
318
                $this->assertEquals($expected, $this->Helper->entity());
319

    
320
                $this->Helper->setEntity('HelperTestComment.body');
321
                $expected = array('HelperTestComment', 'body');
322
                $this->assertEquals($expected, $this->Helper->entity());
323

    
324
                $this->Helper->setEntity('body');
325
                $expected = array('HelperTestPost', 'body');
326
                $this->assertEquals($expected, $this->Helper->entity());
327

    
328
                $this->Helper->setEntity('2.body');
329
                $expected = array('HelperTestPost', '2', 'body');
330
                $this->assertEquals($expected, $this->Helper->entity());
331

    
332
                $this->Helper->setEntity('Something.else');
333
                $expected = array('Something', 'else');
334
                $this->assertEquals($expected, $this->Helper->entity());
335

    
336
                $this->Helper->setEntity('HelperTestComment.5.id');
337
                $expected = array('HelperTestComment', 5, 'id');
338
                $this->assertEquals($expected, $this->Helper->entity());
339

    
340
                $this->Helper->setEntity('HelperTestComment.id.time');
341
                $expected = array('HelperTestComment', 'id', 'time');
342
                $this->assertEquals($expected, $this->Helper->entity());
343

    
344
                $this->Helper->setEntity('HelperTestComment.created.year');
345
                $expected = array('HelperTestComment', 'created', 'year');
346
                $this->assertEquals($expected, $this->Helper->entity());
347

    
348
                $this->Helper->setEntity(null);
349
                $this->Helper->setEntity('ModelThatDoesntExist.field_that_doesnt_exist');
350
                $expected = array('ModelThatDoesntExist', 'field_that_doesnt_exist');
351
                $this->assertEquals($expected, $this->Helper->entity());
352
        }
353

    
354
/**
355
 * Test that setEntity() and model()/field() work with associated models.
356
 *
357
 * @return void
358
 */
359
        public function testSetEntityAssociated() {
360
                $this->Helper->setEntity('HelperTestPost', true);
361

    
362
                $this->Helper->setEntity('HelperTestPost.1.HelperTestComment.1.title');
363
                $expected = array('HelperTestPost', '1', 'HelperTestComment', '1', 'title');
364
                $this->assertEquals($expected, $this->Helper->entity());
365

    
366
                $this->assertEquals('HelperTestComment', $this->Helper->model());
367
        }
368

    
369
/**
370
 * Test creating saveMany() compatible entities
371
 *
372
 * @return void
373
 */
374
        public function testSetEntitySaveMany() {
375
                $this->Helper->setEntity('HelperTestPost', true);
376

    
377
                $this->Helper->setEntity('0.HelperTestPost.id');
378
                $expected = array('0', 'HelperTestPost', 'id');
379
                $this->assertEquals($expected, $this->Helper->entity());
380
        }
381

    
382
/**
383
 * Test that setEntity doesn't make CamelCase fields that are not associations an
384
 * associated model.
385
 *
386
 * @return void
387
 */
388
        public function testSetEntityAssociatedCamelCaseField() {
389
                $this->Helper->fieldset = array(
390
                        'HelperTestComment' => array(
391
                                'fields' => array('BigField' => array('type' => 'integer'))
392
                        )
393
                );
394
                $this->Helper->setEntity('HelperTestComment', true);
395
                $this->Helper->setEntity('HelperTestComment.BigField');
396

    
397
                $this->assertEquals('HelperTestComment', $this->Helper->model());
398
                $this->assertEquals('BigField', $this->Helper->field());
399
        }
400

    
401
/**
402
 * Test that multiple fields work when they are camelcase and in fieldset
403
 *
404
 * @return void
405
 */
406
        public function testSetEntityAssociatedCamelCaseFieldHabtmMultiple() {
407
                $this->Helper->fieldset = array(
408
                        'HelperTestComment' => array(
409
                                'fields' => array('Tag' => array('type' => 'multiple'))
410
                        )
411
                );
412
                $this->Helper->setEntity('HelperTestComment', true);
413
                $this->Helper->setEntity('Tag');
414

    
415
                $this->assertEquals('Tag', $this->Helper->model());
416
                $this->assertEquals('Tag', $this->Helper->field());
417
                $this->assertEquals(array('Tag', 'Tag'), $this->Helper->entity());
418
        }
419

    
420
/**
421
 * Test that habtm associations can have property fields created.
422
 *
423
 * @return void
424
 */
425
        public function testSetEntityHabtmPropertyFieldNames() {
426
                $this->Helper->fieldset = array(
427
                        'HelperTestComment' => array(
428
                                'fields' => array('Tag' => array('type' => 'multiple'))
429
                        )
430
                );
431
                $this->Helper->setEntity('HelperTestComment', true);
432

    
433
                $this->Helper->setEntity('Tag.name');
434
                $this->assertEquals('Tag', $this->Helper->model());
435
                $this->assertEquals('name', $this->Helper->field());
436
                $this->assertEquals(array('Tag', 'name'), $this->Helper->entity());
437
        }
438

    
439
/**
440
 * test that 'view' doesn't break things.
441
 *
442
 * @return void
443
 */
444
        public function testSetEntityWithView() {
445
                $this->assertNull($this->Helper->setEntity('Allow.view.group_id'));
446
                $this->assertNull($this->Helper->setEntity('Allow.view'));
447
                $this->assertNull($this->Helper->setEntity('View.view'));
448
        }
449

    
450
/**
451
 * test getting values from Helper
452
 *
453
 * @return void
454
 */
455
        public function testValue() {
456
                $this->Helper->request->data = array('fullname' => 'This is me');
457
                $this->Helper->setEntity('fullname');
458
                $result = $this->Helper->value('fullname');
459
                $this->assertEquals('This is me', $result);
460

    
461
                $this->Helper->request->data = array(
462
                        'Post' => array('name' => 'First Post')
463
                );
464
                $this->Helper->setEntity('Post.name');
465
                $result = $this->Helper->value('Post.name');
466
                $this->assertEquals('First Post', $result);
467

    
468
                $this->Helper->request->data = array(
469
                        'Post' => array(2 => array('name' => 'First Post'))
470
                );
471
                $this->Helper->setEntity('Post.2.name');
472
                $result = $this->Helper->value('Post.2.name');
473
                $this->assertEquals('First Post', $result);
474

    
475
                $this->Helper->request->data = array(
476
                        'Post' => array(
477
                                2 => array('created' => array('year' => '2008'))
478
                        )
479
                );
480
                $this->Helper->setEntity('Post.2.created');
481
                $result = $this->Helper->value('Post.2.created');
482
                $this->assertEquals(array('year' => '2008'), $result);
483

    
484
                $this->Helper->request->data = array(
485
                        'Post' => array(
486
                                2 => array('created' => array('year' => '2008'))
487
                        )
488
                );
489
                $this->Helper->setEntity('Post.2.created.year');
490
                $result = $this->Helper->value('Post.2.created.year');
491
                $this->assertEquals('2008', $result);
492
        }
493

    
494
/**
495
 * Test default values with value()
496
 *
497
 * @return void
498
 */
499
        public function testValueWithDefault() {
500
                $this->Helper->request->data = array('zero' => 0);
501
                $this->Helper->setEntity('zero');
502
                $result = $this->Helper->value(array('default' => 'something'), 'zero');
503
                $this->assertEquals(array('value' => 0), $result);
504

    
505
                $this->Helper->request->data = array('zero' => '0');
506
                $result = $this->Helper->value(array('default' => 'something'), 'zero');
507
                $this->assertEquals(array('value' => '0'), $result);
508

    
509
                $this->Helper->setEntity('inexistent');
510
                $result = $this->Helper->value(array('default' => 'something'), 'inexistent');
511
                $this->assertEquals(array('value' => 'something'), $result);
512
        }
513

    
514
/**
515
 * Test habtm data fetching and ensure no pollution happens.
516
 *
517
 * @return void
518
 */
519
        public function testValueHabtmKeys() {
520
                $this->Helper->request->data = array(
521
                        'HelperTestTag' => array('HelperTestTag' => '')
522
                );
523
                $this->Helper->setEntity('HelperTestTag.HelperTestTag');
524
                $result = $this->Helper->value('HelperTestTag.HelperTestTag');
525
                $this->assertEquals('', $result);
526

    
527
                $this->Helper->request->data = array(
528
                        'HelperTestTag' => array(
529
                                'HelperTestTag' => array(2, 3, 4)
530
                        )
531
                );
532
                $this->Helper->setEntity('HelperTestTag.HelperTestTag');
533
                $result = $this->Helper->value('HelperTestTag.HelperTestTag');
534
                $this->assertEquals(array(2, 3, 4), $result);
535

    
536
                $this->Helper->request->data = array(
537
                        'HelperTestTag' => array(
538
                                array('id' => 3),
539
                                array('id' => 5)
540
                        )
541
                );
542
                $this->Helper->setEntity('HelperTestTag.HelperTestTag');
543
                $result = $this->Helper->value('HelperTestTag.HelperTestTag');
544
                $this->assertEquals(array(3 => 3, 5 => 5), $result);
545

    
546
                $this->Helper->request->data = array(
547
                        'HelperTestTag' => array(
548
                                'body' => '',
549
                                'title' => 'winning'
550
                        ),
551
                );
552
                $this->Helper->setEntity('HelperTestTag.body');
553
                $result = $this->Helper->value('HelperTestTag.body');
554
                $this->assertEquals('', $result);
555
        }
556

    
557
/**
558
 * Ensure HTML escaping of URL params. So link addresses are valid and not exploited
559
 *
560
 * @return void
561
 */
562
        public function testUrlConversion() {
563
                $result = $this->Helper->url('/controller/action/1');
564
                $this->assertEquals('/controller/action/1', $result);
565

    
566
                $result = $this->Helper->url('/controller/action/1?one=1&two=2');
567
                $this->assertEquals('/controller/action/1?one=1&amp;two=2', $result);
568

    
569
                $result = $this->Helper->url(array('controller' => 'posts', 'action' => 'index', 'page' => '1" onclick="alert(\'XSS\');"'));
570
                $this->assertEquals("/posts/index/page:1%22%20onclick%3D%22alert%28%27XSS%27%29%3B%22", $result);
571

    
572
                $result = $this->Helper->url('/controller/action/1/param:this+one+more');
573
                $this->assertEquals('/controller/action/1/param:this+one+more', $result);
574

    
575
                $result = $this->Helper->url('/controller/action/1/param:this%20one%20more');
576
                $this->assertEquals('/controller/action/1/param:this%20one%20more', $result);
577

    
578
                $result = $this->Helper->url('/controller/action/1/param:%7Baround%20here%7D%5Bthings%5D%5Bare%5D%24%24');
579
                $this->assertEquals('/controller/action/1/param:%7Baround%20here%7D%5Bthings%5D%5Bare%5D%24%24', $result);
580

    
581
                $result = $this->Helper->url(array(
582
                        'controller' => 'posts', 'action' => 'index', 'param' => '%7Baround%20here%7D%5Bthings%5D%5Bare%5D%24%24'
583
                ));
584
                $this->assertEquals("/posts/index/param:%257Baround%2520here%257D%255Bthings%255D%255Bare%255D%2524%2524", $result);
585

    
586
                $result = $this->Helper->url(array(
587
                        'controller' => 'posts', 'action' => 'index', 'page' => '1',
588
                        '?' => array('one' => 'value', 'two' => 'value', 'three' => 'purple')
589
                ));
590
                $this->assertEquals("/posts/index/page:1?one=value&amp;two=value&amp;three=purple", $result);
591
        }
592

    
593
/**
594
 * test assetTimestamp application
595
 *
596
 * @return void
597
 */
598
        public function testAssetTimestamp() {
599
                Configure::write('Foo.bar', 'test');
600
                Configure::write('Asset.timestamp', false);
601
                $result = $this->Helper->assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css');
602
                $this->assertEquals(Configure::read('App.cssBaseUrl') . 'cake.generic.css', $result);
603

    
604
                Configure::write('Asset.timestamp', true);
605
                Configure::write('debug', 0);
606

    
607
                $result = $this->Helper->assetTimestamp('/%3Cb%3E/cake.generic.css');
608
                $this->assertEquals('/%3Cb%3E/cake.generic.css', $result);
609

    
610
                $result = $this->Helper->assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css');
611
                $this->assertEquals(Configure::read('App.cssBaseUrl') . 'cake.generic.css', $result);
612

    
613
                Configure::write('Asset.timestamp', true);
614
                Configure::write('debug', 2);
615
                $result = $this->Helper->assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css');
616
                $this->assertRegExp('/' . preg_quote(Configure::read('App.cssBaseUrl') . 'cake.generic.css?', '/') . '[0-9]+/', $result);
617

    
618
                Configure::write('Asset.timestamp', 'force');
619
                Configure::write('debug', 0);
620
                $result = $this->Helper->assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css');
621
                $this->assertRegExp('/' . preg_quote(Configure::read('App.cssBaseUrl') . 'cake.generic.css?', '/') . '[0-9]+/', $result);
622

    
623
                $result = $this->Helper->assetTimestamp(Configure::read('App.cssBaseUrl') . 'cake.generic.css?someparam');
624
                $this->assertEquals(Configure::read('App.cssBaseUrl') . 'cake.generic.css?someparam', $result);
625

    
626
                $this->Helper->request->webroot = '/some/dir/';
627
                $result = $this->Helper->assetTimestamp('/some/dir/' . Configure::read('App.cssBaseUrl') . 'cake.generic.css');
628
                $this->assertRegExp('/' . preg_quote(Configure::read('App.cssBaseUrl') . 'cake.generic.css?', '/') . '[0-9]+/', $result);
629
        }
630

    
631
/**
632
 * test assetUrl application
633
 *
634
 * @return void
635
 */
636
        public function testAssetUrl() {
637
                $this->Helper->webroot = '';
638
                $result = $this->Helper->assetUrl(array(
639
                                'controller' => 'js',
640
                                'action' => 'post',
641
                                'ext' => 'js'
642
                        ),
643
                        array('fullBase' => true)
644
                );
645
                $this->assertEquals(Router::fullBaseUrl() . '/js/post.js', $result);
646

    
647
                $result = $this->Helper->assetUrl('foo.jpg', array('pathPrefix' => 'img/'));
648
                $this->assertEquals('img/foo.jpg', $result);
649

    
650
                $result = $this->Helper->assetUrl('foo.jpg', array('fullBase' => true));
651
                $this->assertEquals(Router::fullBaseUrl() . '/foo.jpg', $result);
652

    
653
                $result = $this->Helper->assetUrl('style', array('ext' => '.css'));
654
                $this->assertEquals('style.css', $result);
655

    
656
                $result = $this->Helper->assetUrl('dir/sub dir/my image', array('ext' => '.jpg'));
657
                $this->assertEquals('dir/sub%20dir/my%20image.jpg', $result);
658

    
659
                $result = $this->Helper->assetUrl('foo.jpg?one=two&three=four');
660
                $this->assertEquals('foo.jpg?one=two&amp;three=four', $result);
661

    
662
                $result = $this->Helper->assetUrl('dir/big+tall/image', array('ext' => '.jpg'));
663
                $this->assertEquals('dir/big%2Btall/image.jpg', $result);
664
        }
665

    
666
/**
667
 * Test assetUrl with no rewriting.
668
 *
669
 * @return void
670
 */
671
        public function testAssetUrlNoRewrite() {
672
                $this->Helper->request->addPaths(array(
673
                        'base' => '/cake_dev/index.php',
674
                        'webroot' => '/cake_dev/app/webroot/',
675
                        'here' => '/cake_dev/index.php/tasks',
676
                ));
677
                $result = $this->Helper->assetUrl('img/cake.icon.png', array('fullBase' => true));
678
                $expected = FULL_BASE_URL . '/cake_dev/app/webroot/img/cake.icon.png';
679
                $this->assertEquals($expected, $result);
680
        }
681

    
682
/**
683
 * Test assetUrl with plugins.
684
 *
685
 * @return void
686
 */
687
        public function testAssetUrlPlugin() {
688
                $this->Helper->webroot = '';
689
                CakePlugin::load('TestPlugin');
690

    
691
                $result = $this->Helper->assetUrl('TestPlugin.style', array('ext' => '.css'));
692
                $this->assertEquals('test_plugin/style.css', $result);
693

    
694
                $result = $this->Helper->assetUrl('TestPlugin.style', array('ext' => '.css', 'plugin' => false));
695
                $this->assertEquals('TestPlugin.style.css', $result);
696

    
697
                CakePlugin::unload('TestPlugin');
698
        }
699

    
700
/**
701
 * test assetUrl and Asset.timestamp = force
702
 *
703
 * @return void
704
 */
705
        public function testAssetUrlTimestampForce() {
706
                $this->Helper->webroot = '';
707
                Configure::write('Asset.timestamp', 'force');
708

    
709
                $result = $this->Helper->assetUrl('cake.generic.css', array('pathPrefix' => Configure::read('App.cssBaseUrl')));
710
                $this->assertRegExp('/' . preg_quote(Configure::read('App.cssBaseUrl') . 'cake.generic.css?', '/') . '[0-9]+/', $result);
711
        }
712

    
713
/**
714
 * test assetTimestamp with plugins and themes
715
 *
716
 * @return void
717
 */
718
        public function testAssetTimestampPluginsAndThemes() {
719
                Configure::write('Asset.timestamp', 'force');
720
                App::build(array(
721
                        'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS),
722
                ));
723
                CakePlugin::load(array('TestPlugin'));
724

    
725
                $result = $this->Helper->assetTimestamp('/test_plugin/css/test_plugin_asset.css');
726
                $this->assertRegExp('#/test_plugin/css/test_plugin_asset.css\?[0-9]+$#', $result, 'Missing timestamp plugin');
727

    
728
                $result = $this->Helper->assetTimestamp('/test_plugin/css/i_dont_exist.css');
729
                $this->assertRegExp('#/test_plugin/css/i_dont_exist.css\?$#', $result, 'No error on missing file');
730

    
731
                $result = $this->Helper->assetTimestamp('/theme/test_theme/js/theme.js');
732
                $this->assertRegExp('#/theme/test_theme/js/theme.js\?[0-9]+$#', $result, 'Missing timestamp theme');
733

    
734
                $result = $this->Helper->assetTimestamp('/theme/test_theme/js/non_existant.js');
735
                $this->assertRegExp('#/theme/test_theme/js/non_existant.js\?$#', $result, 'No error on missing file');
736
        }
737

    
738
/**
739
 * testFieldsWithSameName method
740
 *
741
 * @return void
742
 */
743
        public function testFieldsWithSameName() {
744
                $this->Helper->setEntity('HelperTestTag', true);
745

    
746
                $this->Helper->setEntity('HelperTestTag.id');
747
                $expected = array('HelperTestTag', 'id');
748
                $this->assertEquals($expected, $this->Helper->entity());
749

    
750
                $this->Helper->setEntity('My.id');
751
                $expected = array('My', 'id');
752
                $this->assertEquals($expected, $this->Helper->entity());
753

    
754
                $this->Helper->setEntity('MyOther.id');
755
                $expected = array('MyOther', 'id');
756
                $this->assertEquals($expected, $this->Helper->entity());
757
        }
758

    
759
/**
760
 * testFieldSameAsModel method
761
 *
762
 * @return void
763
 */
764
        public function testFieldSameAsModel() {
765
                $this->Helper->setEntity('HelperTestTag', true);
766

    
767
                $this->Helper->setEntity('helper_test_post');
768
                $expected = array('HelperTestTag', 'helper_test_post');
769
                $this->assertEquals($expected, $this->Helper->entity());
770

    
771
                $this->Helper->setEntity('HelperTestTag');
772
                $expected = array('HelperTestTag', 'HelperTestTag');
773
                $this->assertEquals($expected, $this->Helper->entity());
774
        }
775

    
776
/**
777
 * testFieldSuffixForDate method
778
 *
779
 * @return void
780
 */
781
        public function testFieldSuffixForDate() {
782
                $this->Helper->setEntity('HelperTestPost', true);
783
                $expected = array('HelperTestPost');
784
                $this->assertEquals($expected, $this->Helper->entity());
785

    
786
                foreach (array('year', 'month', 'day', 'hour', 'min', 'meridian') as $d) {
787
                        $this->Helper->setEntity('date.' . $d);
788
                        $expected = array('HelperTestPost', 'date', $d);
789
                        $this->assertEquals($expected, $this->Helper->entity());
790
                }
791
        }
792

    
793
/**
794
 * testMulitDimensionValue method
795
 *
796
 * @return void
797
 */
798
        public function testMultiDimensionValue() {
799
                $this->Helper->data = array();
800
                for ($i = 0; $i < 2; $i++) {
801
                        $this->Helper->request->data['Model'][$i] = 'what';
802
                        $result[] = $this->Helper->value("Model.{$i}");
803
                        $this->Helper->request->data['Model'][$i] = array();
804
                        for ($j = 0; $j < 2; $j++) {
805
                                $this->Helper->request->data['Model'][$i][$j] = 'how';
806
                                $result[] = $this->Helper->value("Model.{$i}.{$j}");
807
                        }
808
                }
809
                $expected = array('what', 'how', 'how', 'what', 'how', 'how');
810
                $this->assertEquals($expected, $result);
811

    
812
                $this->Helper->request->data['HelperTestComment']['5']['id'] = 'ok';
813
                $result = $this->Helper->value('HelperTestComment.5.id');
814
                $this->assertEquals('ok', $result);
815

    
816
                $this->Helper->setEntity('HelperTestPost', true);
817
                $this->Helper->request->data['HelperTestPost']['5']['created']['month'] = '10';
818
                $result = $this->Helper->value('5.created.month');
819
                $this->assertEquals(10, $result);
820

    
821
                $this->Helper->request->data['HelperTestPost']['0']['id'] = 100;
822
                $result = $this->Helper->value('HelperTestPost.0.id');
823
                $this->assertEquals(100, $result);
824
        }
825

    
826
/**
827
 * testClean method
828
 *
829
 * @return void
830
 */
831
        public function testClean() {
832
                $result = $this->Helper->clean(array());
833
                $this->assertEquals(null, $result);
834

    
835
                $result = $this->Helper->clean(array('<script>with something</script>', '<applet>something else</applet>'));
836
                $this->assertEquals(array('with something', 'something else'), $result);
837

    
838
                $result = $this->Helper->clean('<script>with something</script>');
839
                $this->assertEquals('with something', $result);
840

    
841
                $result = $this->Helper->clean('<script type="text/javascript">alert("ruined");</script>');
842
                $this->assertNotRegExp('#</*script#', $result);
843

    
844
                $result = $this->Helper->clean("<script \ntype=\"text/javascript\">\n\talert('ruined');\n\n\t\t</script>");
845
                $this->assertNotRegExp('#</*script#', $result);
846

    
847
                $result = $this->Helper->clean('<body/onload=do(/something/)>');
848
                $this->assertEquals('<body/>', $result);
849

    
850
                $result = $this->Helper->clean('&lt;script&gt;alert(document.cookie)&lt;/script&gt;');
851
                $this->assertEquals('&amp;lt;script&amp;gt;alert(document.cookie)&amp;lt;/script&amp;gt;', $result);
852
        }
853

    
854
/**
855
 * testDomId method
856
 *
857
 * @return void
858
 */
859
        public function testDomId() {
860
                $result = $this->Helper->domId('Foo.bar');
861
                $this->assertEquals('FooBar', $result);
862
        }
863

    
864
/**
865
 * testMultiDimensionalField method
866
 *
867
 * @return void
868
 */
869
        public function testMultiDimensionalField() {
870
                $this->Helper->setEntity('HelperTestPost', true);
871

    
872
                $entity = 'HelperTestPost.2.HelperTestComment.1.title';
873
                $this->Helper->setEntity($entity);
874
                $expected = array(
875
                        'HelperTestPost', '2', 'HelperTestComment', '1', 'title'
876
                );
877
                $this->assertEquals($expected, $this->Helper->entity());
878

    
879
                $entity = 'HelperTestPost.1.HelperTestComment.1.HelperTestTag.1.created';
880
                $this->Helper->setEntity($entity);
881
                $expected = array(
882
                        'HelperTestPost', '1', 'HelperTestComment', '1',
883
                        'HelperTestTag', '1', 'created'
884
                );
885
                $this->assertEquals($expected, $this->Helper->entity());
886

    
887
                $entity = 'HelperTestPost.0.HelperTestComment.1.HelperTestTag.1.fake';
888
                $expected = array(
889
                        'HelperTestPost', '0', 'HelperTestComment', '1',
890
                        'HelperTestTag', '1', 'fake'
891
                );
892
                $this->Helper->setEntity($entity);
893

    
894
                $entity = '1.HelperTestComment.1.HelperTestTag.created.year';
895
                $this->Helper->setEntity($entity);
896

    
897
                $this->Helper->request->data['HelperTestPost'][2]['HelperTestComment'][1]['title'] = 'My Title';
898
                $result = $this->Helper->value('HelperTestPost.2.HelperTestComment.1.title');
899
                $this->assertEquals('My Title', $result);
900

    
901
                $this->Helper->request->data['HelperTestPost'][2]['HelperTestComment'][1]['created']['year'] = 2008;
902
                $result = $this->Helper->value('HelperTestPost.2.HelperTestComment.1.created.year');
903
                $this->assertEquals(2008, $result);
904

    
905
                $this->Helper->request->data[2]['HelperTestComment'][1]['created']['year'] = 2008;
906
                $result = $this->Helper->value('HelperTestPost.2.HelperTestComment.1.created.year');
907
                $this->assertEquals(2008, $result);
908

    
909
                $this->Helper->request->data['HelperTestPost']['title'] = 'My Title';
910
                $result = $this->Helper->value('title');
911
                $this->assertEquals('My Title', $result);
912

    
913
                $this->Helper->request->data['My']['title'] = 'My Title';
914
                $result = $this->Helper->value('My.title');
915
                $this->assertEquals('My Title', $result);
916
        }
917

    
918
        public function testWebrootPaths() {
919
                $this->Helper->request->webroot = '/';
920
                $result = $this->Helper->webroot('/img/cake.power.gif');
921
                $expected = '/img/cake.power.gif';
922
                $this->assertEquals($expected, $result);
923

    
924
                $this->Helper->theme = 'test_theme';
925

    
926
                App::build(array(
927
                        'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
928
                ));
929

    
930
                $result = $this->Helper->webroot('/img/cake.power.gif');
931
                $expected = '/theme/test_theme/img/cake.power.gif';
932
                $this->assertEquals($expected, $result);
933

    
934
                $result = $this->Helper->webroot('/img/test.jpg');
935
                $expected = '/theme/test_theme/img/test.jpg';
936
                $this->assertEquals($expected, $result);
937

    
938
                $webRoot = Configure::read('App.www_root');
939
                Configure::write('App.www_root', CAKE . 'Test' . DS . 'test_app' . DS . 'webroot' . DS);
940

    
941
                $result = $this->Helper->webroot('/img/cake.power.gif');
942
                $expected = '/theme/test_theme/img/cake.power.gif';
943
                $this->assertEquals($expected, $result);
944

    
945
                $result = $this->Helper->webroot('/img/test.jpg');
946
                $expected = '/theme/test_theme/img/test.jpg';
947
                $this->assertEquals($expected, $result);
948

    
949
                $result = $this->Helper->webroot('/img/cake.icon.gif');
950
                $expected = '/img/cake.icon.gif';
951
                $this->assertEquals($expected, $result);
952

    
953
                $result = $this->Helper->webroot('/img/cake.icon.gif?some=param');
954
                $expected = '/img/cake.icon.gif?some=param';
955
                $this->assertEquals($expected, $result);
956

    
957
                Configure::write('App.www_root', $webRoot);
958
        }
959

    
960
/**
961
 * test lazy loading helpers is seamless
962
 *
963
 * @return void
964
 */
965
        public function testLazyLoadingHelpers() {
966
                App::build(array(
967
                        'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
968
                ));
969
                CakePlugin::load(array('TestPlugin'));
970
                $Helper = new TestHelper($this->View);
971
                $this->assertInstanceOf('OtherHelperHelper', $Helper->OtherHelper);
972
                $this->assertInstanceOf('HtmlHelper', $Helper->Html);
973
                App::build();
974
        }
975

    
976
/**
977
 * test that a helpers Helper is not 'attached' to the collection
978
 *
979
 * @return void
980
 */
981
        public function testThatHelperHelpersAreNotAttached() {
982
                $Helper = new TestHelper($this->View);
983
                $Helper->OtherHelper;
984

    
985
                $result = $this->View->Helpers->enabled();
986
                $this->assertEquals(array(), $result, 'Helper helpers were attached to the collection.');
987
        }
988

    
989
/**
990
 * test that the lazy loader doesn't duplicate objects on each access.
991
 *
992
 * @return void
993
 */
994
        public function testLazyLoadingUsesReferences() {
995
                $Helper = new TestHelper($this->View);
996
                $resultA = $Helper->Html;
997
                $resultB = $Helper->Html;
998

    
999
                $resultA->testprop = 1;
1000
                $this->assertEquals($resultA->testprop, $resultB->testprop);
1001
        }
1002

    
1003
}