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

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

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

1
<?php
2
/**
3
 * RssHelperTest 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('View', 'View');
20
App::uses('RssHelper', 'View/Helper');
21
App::uses('TimeHelper', 'View/Helper');
22
App::uses('File', 'Utility');
23

    
24
/**
25
 * RssHelperTest class
26
 *
27
 * @package       Cake.Test.Case.View.Helper
28
 */
29
class RssHelperTest extends CakeTestCase {
30

    
31
/**
32
 * setUp method
33
 *
34
 * @return void
35
 */
36
        public function setUp() {
37
                parent::setUp();
38
                $controller = null;
39
                $this->View = new View($controller);
40
                $this->Rss = new RssHelper($this->View);
41
        }
42

    
43
/**
44
 * tearDown method
45
 *
46
 * @return void
47
 */
48
        public function tearDown() {
49
                parent::tearDown();
50
                unset($this->Rss);
51
        }
52

    
53
/**
54
 * testDocument method
55
 *
56
 * @return void
57
 */
58
        public function testDocument() {
59
                $result = $this->Rss->document();
60
                $expected = array(
61
                        'rss' => array(
62
                                'version' => '2.0'
63
                        )
64
                );
65
                $this->assertTags($result, $expected);
66

    
67
                $result = $this->Rss->document(null, 'content');
68
                $expected = array(
69
                        'rss' => array(
70
                                'version' => '2.0'
71
                        ),
72
                        'content'
73
                );
74
                $this->assertTags($result, $expected);
75

    
76
                $result = $this->Rss->document(array('contrived' => 'parameter'), 'content');
77
                $expected = array(
78
                        'rss' => array(
79
                                'contrived' => 'parameter',
80
                                'version' => '2.0'
81
                        ),
82
                        'content'
83
                );
84
                $this->assertTags($result, $expected);
85
        }
86

    
87
/**
88
 * testChannel method
89
 *
90
 * @return void
91
 */
92
        public function testChannel() {
93
                $attrib = array('a' => '1', 'b' => '2');
94
                $elements = array('title' => 'Title');
95
                $content = 'content';
96

    
97
                $result = $this->Rss->channel($attrib, $elements, $content);
98
                $expected = array(
99
                        'channel' => array(
100
                                'a' => '1',
101
                                'b' => '2'
102
                        ),
103
                        '<title',
104
                        'Title',
105
                        '/title',
106
                        '<link',
107
                        $this->Rss->url('/', true),
108
                        '/link',
109
                        '<description',
110
                        'content',
111
                        '/channel'
112
                );
113
                $this->assertTags($result, $expected);
114
        }
115

    
116
/**
117
 * test correct creation of channel sub elements.
118
 *
119
 * @return void
120
 */
121
        public function testChannelElements() {
122
                $attrib = array();
123
                $elements = array(
124
                        'title' => 'Title of RSS Feed',
125
                        'link' => 'http://example.com',
126
                        'description' => 'Description of RSS Feed',
127
                        'image' => array(
128
                                'title' => 'Title of image',
129
                                'url' => 'http://example.com/example.png',
130
                                'link' => 'http://example.com'
131
                        ),
132
                        'cloud' => array(
133
                                'domain' => "rpc.sys.com",
134
                                'port' => "80",
135
                                'path' => "/RPC2",
136
                                'registerProcedure' => "myCloud.rssPleaseNotify",
137
                                'protocol' => "xml-rpc"
138
                        )
139
                );
140
                $content = 'content-here';
141
                $result = $this->Rss->channel($attrib, $elements, $content);
142
                $expected = array(
143
                        '<channel',
144
                                '<title', 'Title of RSS Feed', '/title',
145
                                '<link', 'http://example.com', '/link',
146
                                '<description', 'Description of RSS Feed', '/description',
147
                                '<image',
148
                                        '<title', 'Title of image', '/title',
149
                                        '<url', 'http://example.com/example.png', '/url',
150
                                        '<link', 'http://example.com', '/link',
151
                                '/image',
152
                                'cloud' => array(
153
                                        'domain' => "rpc.sys.com",
154
                                        'port' => "80",
155
                                        'path' => "/RPC2",
156
                                        'registerProcedure' => "myCloud.rssPleaseNotify",
157
                                        'protocol' => "xml-rpc"
158
                                ),
159
                        'content-here',
160
                        '/channel',
161
                );
162
                $this->assertTags($result, $expected);
163
        }
164

    
165
        public function testChannelElementAttributes() {
166
                $attrib = array();
167
                $elements = array(
168
                        'title' => 'Title of RSS Feed',
169
                        'link' => 'http://example.com',
170
                        'description' => 'Description of RSS Feed',
171
                        'image' => array(
172
                                'title' => 'Title of image',
173
                                'url' => 'http://example.com/example.png',
174
                                'link' => 'http://example.com'
175
                        ),
176
                        'atom:link' => array(
177
                                'attrib' => array(
178
                                        'href' => 'http://www.example.com/rss.xml',
179
                                        'rel' => 'self',
180
                                        'type' => 'application/rss+xml')
181
                        )
182
                );
183
                $content = 'content-here';
184
                $result = $this->Rss->channel($attrib, $elements, $content);
185
                $expected = array(
186
                        '<channel',
187
                                '<title', 'Title of RSS Feed', '/title',
188
                                '<link', 'http://example.com', '/link',
189
                                '<description', 'Description of RSS Feed', '/description',
190
                                '<image',
191
                                        '<title', 'Title of image', '/title',
192
                                        '<url', 'http://example.com/example.png', '/url',
193
                                        '<link', 'http://example.com', '/link',
194
                                '/image',
195
                                'atom:link' => array(
196
                                        'xmlns:atom' => 'http://www.w3.org/2005/Atom',
197
                                        'href' => "http://www.example.com/rss.xml",
198
                                        'rel' => "self",
199
                                        'type' => "application/rss+xml"
200
                                ),
201
                        'content-here',
202
                        '/channel',
203
                );
204
                $this->assertTags($result, $expected);
205
        }
206

    
207
/**
208
 * testItems method
209
 *
210
 * @return void
211
 */
212
        public function testItems() {
213
                $items = array(
214
                        array('title' => 'title1', 'guid' => 'http://www.example.com/guid1', 'link' => 'http://www.example.com/link1', 'description' => 'description1'),
215
                        array('title' => 'title2', 'guid' => 'http://www.example.com/guid2', 'link' => 'http://www.example.com/link2', 'description' => 'description2'),
216
                        array('title' => 'title3', 'guid' => 'http://www.example.com/guid3', 'link' => 'http://www.example.com/link3', 'description' => 'description3')
217
                );
218

    
219
                $result = $this->Rss->items($items);
220
                $expected = array(
221
                        '<item',
222
                                '<title', 'title1', '/title',
223
                                '<guid', 'http://www.example.com/guid1', '/guid',
224
                                '<link', 'http://www.example.com/link1', '/link',
225
                                '<description', 'description1', '/description',
226
                        '/item',
227
                        '<item',
228
                                '<title', 'title2', '/title',
229
                                '<guid', 'http://www.example.com/guid2', '/guid',
230
                                '<link', 'http://www.example.com/link2', '/link',
231
                                '<description', 'description2', '/description',
232
                        '/item',
233
                        '<item',
234
                                '<title', 'title3', '/title',
235
                                '<guid', 'http://www.example.com/guid3', '/guid',
236
                                '<link', 'http://www.example.com/link3', '/link',
237
                                '<description', 'description3', '/description',
238
                        '/item'
239
                );
240
                $this->assertTags($result, $expected);
241

    
242
                $items = array(
243
                        array('title' => 'title1', 'guid' => 'http://www.example.com/guid1', 'link' => 'http://www.example.com/link1', 'description' => 'description1'),
244
                        array('title' => 'title2', 'guid' => 'http://www.example.com/guid2', 'link' => 'http://www.example.com/link2', 'description' => 'description2'),
245
                        array('title' => 'title3', 'guid' => 'http://www.example.com/guid3', 'link' => 'http://www.example.com/link3', 'description' => 'description3')
246
                );
247

    
248
                $result = $this->Rss->items($items, create_function('$v', '$v[\'title\'] = $v[\'title\'] . \'-transformed\'; return $v;'));
249
                $expected = array(
250
                        '<item',
251
                                '<title', 'title1-transformed', '/title',
252
                                '<guid', 'http://www.example.com/guid1', '/guid',
253
                                '<link', 'http://www.example.com/link1', '/link',
254
                                '<description', 'description1', '/description',
255
                        '/item',
256
                        '<item',
257
                                '<title', 'title2-transformed', '/title',
258
                                '<guid', 'http://www.example.com/guid2', '/guid',
259
                                '<link', 'http://www.example.com/link2', '/link',
260
                                '<description', 'description2', '/description',
261
                        '/item',
262
                        '<item',
263
                                '<title', 'title3-transformed', '/title',
264
                                '<guid', 'http://www.example.com/guid3', '/guid',
265
                                '<link', 'http://www.example.com/link3', '/link',
266
                                '<description', 'description3', '/description',
267
                        '/item'
268
                );
269
                $this->assertTags($result, $expected);
270

    
271
                $result = $this->Rss->items(array());
272
                $expected = '';
273
                $this->assertEquals($expected, $result);
274
        }
275

    
276
/**
277
 * testItem method
278
 *
279
 * @return void
280
 */
281
        public function testItem() {
282
                $item = array(
283
                        'title' => 'My title',
284
                        'description' => 'My description',
285
                        'link' => 'http://www.google.com/'
286
                );
287
                $result = $this->Rss->item(null, $item);
288
                $expected = array(
289
                        '<item',
290
                        '<title',
291
                        'My title',
292
                        '/title',
293
                        '<description',
294
                        'My description',
295
                        '/description',
296
                        '<link',
297
                        'http://www.google.com/',
298
                        '/link',
299
                        '<guid',
300
                        'http://www.google.com/',
301
                        '/guid',
302
                        '/item'
303
                );
304
                $this->assertTags($result, $expected);
305

    
306
                $item = array(
307
                        'title' => 'My Title',
308
                        'link' => 'http://www.example.com/1',
309
                        'description' => 'descriptive words',
310
                        'pubDate' => '2008-05-31 12:00:00',
311
                        'source' => array('http://www.google.com/', 'Google'),
312
                        'guid' => 'http://www.example.com/1'
313
                );
314
                $result = $this->Rss->item(null, $item);
315

    
316
                $expected = array(
317
                        '<item',
318
                        '<title',
319
                        'My Title',
320
                        '/title',
321
                        '<link',
322
                        'http://www.example.com/1',
323
                        '/link',
324
                        '<description',
325
                        'descriptive words',
326
                        '/description',
327
                        '<pubDate',
328
                        date('r', strtotime('2008-05-31 12:00:00')),
329
                        '/pubDate',
330
                        'source' => array('url' => 'http://www.google.com/'),
331
                        'Google',
332
                        '/source',
333
                        '<guid',
334
                        'http://www.example.com/1',
335
                        '/guid',
336
                        '/item'
337
                );
338
                $this->assertTags($result, $expected);
339

    
340
                $item = array(
341
                        'title' => 'My Title & more'
342
                );
343
                $result = $this->Rss->item(null, $item);
344
                $expected = array(
345
                        '<item',
346
                        '<title', 'My Title &amp; more', '/title',
347
                        '/item'
348
                );
349
                $this->assertTags($result, $expected);
350

    
351
                $item = array(
352
                        'title' => 'Foo bar',
353
                        'link' => array(
354
                                'url' => 'http://example.com/foo?a=1&b=2',
355
                                'convertEntities' => false
356
                        ),
357
                        'description' => array(
358
                                'value' => 'descriptive words',
359
                                'cdata' => true,
360
                        ),
361
                        'pubDate' => '2008-05-31 12:00:00',
362
                        'source' => 'http://www.google.com/'
363
                );
364
                $result = $this->Rss->item(null, $item);
365
                $expected = array(
366
                        '<item',
367
                        '<title',
368
                        'Foo bar',
369
                        '/title',
370
                        '<link',
371
                        'http://example.com/foo?a=1&amp;b=2',
372
                        '/link',
373
                        '<description',
374
                        '<![CDATA[descriptive words]]',
375
                        '/description',
376
                        '<pubDate',
377
                        date('r', strtotime('2008-05-31 12:00:00')),
378
                        '/pubDate',
379
                        '<source',
380
                        'http://www.google.com/',
381
                        '/source',
382
                        '<guid',
383
                        'http://example.com/foo?a=1&amp;b=2',
384
                        '/guid',
385
                        '/item'
386
                );
387
                $this->assertTags($result, $expected);
388

    
389
                $item = array(
390
                        'title' => 'My title',
391
                        'description' => 'My description',
392
                        'link' => 'http://www.google.com/',
393
                        'source' => array('url' => 'http://www.example.com/', 'title' => 'Example website')
394
                );
395
                $result = $this->Rss->item(null, $item);
396
                $expected = array(
397
                        '<item',
398
                        '<title',
399
                        'My title',
400
                        '/title',
401
                        '<description',
402
                        'My description',
403
                        '/description',
404
                        '<link',
405
                        'http://www.google.com/',
406
                        '/link',
407
                        'source' => array('url' => 'http://www.example.com/'),
408
                        'Example website',
409
                        '/source',
410
                        '<guid',
411
                        'http://www.google.com/',
412
                        '/guid',
413
                        '/item'
414
                );
415
                $this->assertTags($result, $expected);
416

    
417
                $item = array(
418
                        'title' => 'My title',
419
                        'description' => 'My description',
420
                        'link' => 'http://www.google.com/',
421
                        'category' => array('Category One', 'Category Two')
422
                );
423
                $result = $this->Rss->item(null, $item);
424
                $expected = array(
425
                        '<item',
426
                        '<title',
427
                        'My title',
428
                        '/title',
429
                        '<description',
430
                        'My description',
431
                        '/description',
432
                        '<link',
433
                        'http://www.google.com/',
434
                        '/link',
435
                        '<category',
436
                        'Category One',
437
                        '/category',
438
                        '<category',
439
                        'Category Two',
440
                        '/category',
441
                        '<guid',
442
                        'http://www.google.com/',
443
                        '/guid',
444
                        '/item'
445
                );
446
                $this->assertTags($result, $expected);
447
        }
448

    
449
/**
450
 * test item() with cdata blocks.
451
 *
452
 * @return void
453
 */
454
        public function testItemCdata() {
455
                $item = array(
456
                        'title' => array(
457
                                'value' => 'My Title & more',
458
                                'cdata' => true,
459
                                'convertEntities' => false,
460
                        )
461
                );
462
                $result = $this->Rss->item(null, $item);
463
                $expected = array(
464
                        '<item',
465
                        '<title',
466
                        '<![CDATA[My Title & more]]',
467
                        '/title',
468
                        '/item'
469
                );
470
                $this->assertTags($result, $expected);
471

    
472
                $item = array(
473
                        'category' => array(
474
                                'value' => 'CakePHP',
475
                                'cdata' => true,
476
                                'domain' => 'http://www.cakephp.org',
477
                        )
478
                );
479
                $result = $this->Rss->item(null, $item);
480
                $expected = array(
481
                        '<item',
482
                        'category' => array('domain' => 'http://www.cakephp.org'),
483
                        '<![CDATA[CakePHP]]',
484
                        '/category',
485
                        '/item'
486
                );
487
                $this->assertTags($result, $expected);
488

    
489
                $item = array(
490
                        'category' => array(
491
                                array(
492
                                        'value' => 'CakePHP',
493
                                        'cdata' => true,
494
                                        'domain' => 'http://www.cakephp.org'
495
                                ),
496
                                array(
497
                                        'value' => 'Bakery',
498
                                        'cdata' => true
499
                                )
500
                        )
501
                );
502
                $result = $this->Rss->item(null, $item);
503
                $expected = array(
504
                        '<item',
505
                        'category' => array('domain' => 'http://www.cakephp.org'),
506
                        '<![CDATA[CakePHP]]',
507
                        '/category',
508
                        '<category',
509
                        '<![CDATA[Bakery]]',
510
                        '/category',
511
                        '/item'
512
                );
513
                $this->assertTags($result, $expected);
514

    
515
                $item = array(
516
                        'title' => array(
517
                                'value' => 'My Title',
518
                                'cdata' => true,
519
                        ),
520
                        'link' => 'http://www.example.com/1',
521
                        'description' => array(
522
                                'value' => 'descriptive words',
523
                                'cdata' => true,
524
                        ),
525
                        'enclosure' => array(
526
                                'url' => '/test.flv'
527
                        ),
528
                        'pubDate' => '2008-05-31 12:00:00',
529
                        'guid' => 'http://www.example.com/1',
530
                        'category' => array(
531
                                array(
532
                                        'value' => 'CakePHP',
533
                                        'cdata' => true,
534
                                        'domain' => 'http://www.cakephp.org'
535
                                ),
536
                                array(
537
                                        'value' => 'Bakery',
538
                                        'cdata' => true
539
                                )
540
                        )
541
                );
542
                $result = $this->Rss->item(null, $item);
543
                $expected = array(
544
                        '<item',
545
                        '<title',
546
                        '<![CDATA[My Title]]',
547
                        '/title',
548
                        '<link',
549
                        'http://www.example.com/1',
550
                        '/link',
551
                        '<description',
552
                        '<![CDATA[descriptive words]]',
553
                        '/description',
554
                        'enclosure' => array('url' => $this->Rss->url('/test.flv', true)),
555
                        '<pubDate',
556
                        date('r', strtotime('2008-05-31 12:00:00')),
557
                        '/pubDate',
558
                        '<guid',
559
                        'http://www.example.com/1',
560
                        '/guid',
561
                        'category' => array('domain' => 'http://www.cakephp.org'),
562
                        '<![CDATA[CakePHP]]',
563
                        '/category',
564
                        '<category',
565
                        '<![CDATA[Bakery]]',
566
                        '/category',
567
                        '/item'
568
                );
569
                $this->assertTags($result, $expected);
570
        }
571

    
572
/**
573
 * test item() with enclosure data.
574
 *
575
 * @return void
576
 */
577
        public function testItemEnclosureLength() {
578
                if (!is_writable(WWW_ROOT)) {
579
                        $this->markTestSkipped(__d('cake_dev', 'Webroot is not writable.'));
580
                }
581
                $testExists = is_dir(WWW_ROOT . 'tests');
582

    
583
                $tmpFile = WWW_ROOT . 'tests' . DS . 'cakephp.file.test.tmp';
584
                $File = new File($tmpFile, true);
585

    
586
                $this->assertTrue($File->write('123'), 'Could not write to ' . $tmpFile);
587

    
588
                if (PHP_VERSION_ID >= 50300) {
589
                        clearstatcache(true, $tmpFile);
590
                } else {
591
                        clearstatcache();
592
                }
593

    
594
                $item = array(
595
                        'title' => array(
596
                                'value' => 'My Title',
597
                                'cdata' => true,
598
                        ),
599
                        'link' => 'http://www.example.com/1',
600
                        'description' => array(
601
                                'value' => 'descriptive words',
602
                                'cdata' => true,
603
                        ),
604
                        'enclosure' => array(
605
                                'url' => '/tests/cakephp.file.test.tmp'
606
                        ),
607
                        'pubDate' => '2008-05-31 12:00:00',
608
                        'guid' => 'http://www.example.com/1',
609
                        'category' => array(
610
                                array(
611
                                        'value' => 'CakePHP',
612
                                        'cdata' => true,
613
                                        'domain' => 'http://www.cakephp.org'
614
                                ),
615
                                array(
616
                                        'value' => 'Bakery',
617
                                        'cdata' => true
618
                                )
619
                        )
620
                );
621
                $result = $this->Rss->item(null, $item);
622
                if (!function_exists('mime_content_type')) {
623
                        $type = null;
624
                } else {
625
                        $type = mime_content_type($tmpFile);
626
                }
627

    
628
                $expected = array(
629
                        '<item',
630
                        '<title',
631
                        '<![CDATA[My Title]]',
632
                        '/title',
633
                        '<link',
634
                        'http://www.example.com/1',
635
                        '/link',
636
                        '<description',
637
                        '<![CDATA[descriptive words]]',
638
                        '/description',
639
                        'enclosure' => array(
640
                                'url' => $this->Rss->url('/tests/cakephp.file.test.tmp', true),
641
                                'length' => filesize($tmpFile),
642
                                'type' => $type
643
                        ),
644
                        '<pubDate',
645
                        date('r', strtotime('2008-05-31 12:00:00')),
646
                        '/pubDate',
647
                        '<guid',
648
                        'http://www.example.com/1',
649
                        '/guid',
650
                        'category' => array('domain' => 'http://www.cakephp.org'),
651
                        '<![CDATA[CakePHP]]',
652
                        '/category',
653
                        '<category',
654
                        '<![CDATA[Bakery]]',
655
                        '/category',
656
                        '/item'
657
                );
658
                if ($type === null) {
659
                        unset($expected['enclosure']['type']);
660
                }
661
                $this->assertTags($result, $expected);
662

    
663
                $File->delete();
664

    
665
                if (!$testExists) {
666
                        $Folder = new Folder(WWW_ROOT . 'tests');
667
                        $Folder->delete();
668
                }
669
        }
670

    
671
/**
672
 * testElementAttrNotInParent method
673
 *
674
 * @return void
675
 */
676
        public function testElementAttrNotInParent() {
677
                $attributes = array(
678
                        'title' => 'Some Title',
679
                        'link' => 'http://link.com',
680
                        'description' => 'description'
681
                );
682
                $elements = array('enclosure' => array('url' => 'http://test.com'));
683

    
684
                $result = $this->Rss->item($attributes, $elements);
685
                $expected = array(
686
                        'item' => array(
687
                                'title' => 'Some Title',
688
                                'link' => 'http://link.com',
689
                                'description' => 'description'
690
                        ),
691
                        'enclosure' => array(
692
                                'url' => 'http://test.com'
693
                        ),
694
                        '/item'
695
                );
696
                $this->assertTags($result, $expected);
697
        }
698

    
699
        public function testElementNamespaceWithoutPrefix() {
700
                $item = array(
701
                                'creator' => 'Alex',
702
                        );
703
                $attributes = array(
704
                                'namespace' => 'http://link.com'
705
                );
706
                $result = $this->Rss->item($attributes, $item);
707
                $expected = array(
708
                        'item' => array(
709
                                        'xmlns' => 'http://link.com'
710
                        ),
711
                        'creator' => array(
712
                                        'xmlns' => 'http://link.com'
713
                        ),
714
                        'Alex',
715
                        '/creator',
716
                        '/item'
717
                );
718
                $this->assertTags($result, $expected, true);
719
        }
720

    
721
        public function testElementNamespaceWithPrefix() {
722
                $item = array(
723
                        'title' => 'Title',
724
                        'dc:creator' => 'Alex',
725
                        'dc:description' => 'descriptive words'
726
                );
727
                $attributes = array(
728
                        'namespace' => array(
729
                                'prefix' => 'dc',
730
                                'url' => 'http://link.com'
731
                        )
732
                );
733
                $result = $this->Rss->item($attributes, $item);
734
                $expected = array(
735
                        'item' => array(
736
                                'xmlns:dc' => 'http://link.com'
737
                        ),
738
                        'title' => array(
739
                                'xmlns:dc' => 'http://link.com'
740
                        ),
741
                        'Title',
742
                        '/title',
743
                        'dc:creator' => array(
744
                                'xmlns:dc' => 'http://link.com'
745
                        ),
746
                        'Alex',
747
                        '/dc:creator',
748
                        'dc:description' => array(
749
                                'xmlns:dc' => 'http://link.com'
750
                        ),
751
                        'descriptive words',
752
                        '/dc:description',
753
                        '/item'
754
                );
755
                $this->assertTags($result, $expected, true);
756
        }
757
}