| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591 |
- <?php
- /**
- * PHP 5
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice
- *
- * @author Mark Scherer
- * @license http://www.opensource.org/licenses/mit-license.php MIT License
- */
- namespace Tools\Test\TestCase\View;
- use Cake\Routing\Router;
- use Cake\Controller\Controller;
- use Cake\Network\Request;
- use Cake\Network\Response;
- use Tools\TestSuite\TestCase;
- use Tools\View\RssView;
- /**
- * RssViewTest
- *
- */
- class RssViewTest extends TestCase {
- public $Rss;
- public $baseUrl;
- /**
- * RssViewTest::setUp()
- *
- * @return void
- */
- public function setUp() {
- parent::setUp();
- $this->Rss = new RssView();
- $this->baseUrl = trim(Router::url('/', true), '/');
- }
- /**
- * TestTime method
- *
- * @return void
- */
- public function testTime() {
- $now = time();
- $time = $this->Rss->time($now);
- $this->assertEquals(date('r', $now), $time);
- }
- /**
- * RssViewTest::testSerialize()
- *
- * @return void
- */
- public function testSerialize() {
- $Request = new Request();
- $Response = new Response();
- $data = array(
- 'channel' => array(
- 'title' => 'Channel title',
- 'link' => 'http://channel.example.org',
- 'description' => 'Channel description'
- ),
- 'items' => array(
- array('title' => 'Title One', 'link' => 'http://example.org/one',
- 'author' => 'one@example.org', 'description' => 'Content one',
- 'source' => array('url' => 'http://foo.bar')),
- array('title' => 'Title Two', 'link' => 'http://example.org/two',
- 'author' => 'two@example.org', 'description' => 'Content two',
- 'source' => array('url' => 'http://foo.bar', 'content' => 'Foo bar')),
- )
- );
- $viewVars = array('channel' => $data, '_serialize' => 'channel');
- $View = new RssView($Request, $Response, null, ['viewVars' => $viewVars]);
- $result = $View->render(false);
- $expected = <<<RSS
- <?xml version="1.0" encoding="UTF-8"?>
- <rss version="2.0">
- <channel>
- <title>Channel title</title>
- <link>http://channel.example.org</link>
- <description>Channel description</description>
- <item>
- <title>Title One</title>
- <link>http://example.org/one</link>
- <author>one@example.org</author>
- <description>Content one</description>
- <source url="http://foo.bar">http://foo.bar</source>
- </item>
- <item>
- <title>Title Two</title>
- <link>http://example.org/two</link>
- <author>two@example.org</author>
- <description>Content two</description>
- <source url="http://foo.bar">Foo bar</source>
- </item>
- </channel>
- </rss>
- RSS;
- $this->assertSame('application/rss+xml', $Response->type());
- $this->assertTextEquals($expected, $result);
- }
- /**
- * RssViewTest::testSerialize()
- *
- * @return void
- */
- public function testSerializeWithPrefixes() {
- $Request = new Request();
- $Response = new Response();
- $time = time();
- $data = array(
- 'channel' => array(
- 'title' => 'Channel title',
- 'link' => 'http://channel.example.org',
- 'description' => 'Channel description',
- 'sy:updatePeriod' => 'hourly',
- 'sy:updateFrequency' => 1
- ),
- 'items' => array(
- array('title' => 'Title One', 'link' => 'http://example.org/one',
- 'dc:creator' => 'Author One', 'pubDate' => $time),
- array('title' => 'Title Two', 'link' => 'http://example.org/two',
- 'dc:creator' => 'Author Two', 'pubDate' => $time,
- 'source' => 'http://foo.bar'),
- )
- );
- $viewVars = array('channel' => $data, '_serialize' => 'channel');
- $View = new RssView($Request, $Response, null, ['viewVars' => $viewVars]);
- $result = $View->render(false);
- $time = date('r', $time);
- $expected = <<<RSS
- <?xml version="1.0" encoding="UTF-8"?>
- <rss xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
- <channel>
- <title>Channel title</title>
- <link>http://channel.example.org</link>
- <description>Channel description</description>
- <sy:updatePeriod>hourly</sy:updatePeriod>
- <sy:updateFrequency>1</sy:updateFrequency>
- <item>
- <title>Title One</title>
- <link>http://example.org/one</link>
- <dc:creator>Author One</dc:creator>
- <pubDate>$time</pubDate>
- </item>
- <item>
- <title>Title Two</title>
- <link>http://example.org/two</link>
- <dc:creator>Author Two</dc:creator>
- <pubDate>$time</pubDate>
- <source url="http://foo.bar">http://foo.bar</source>
- </item>
- </channel>
- </rss>
- RSS;
- $this->assertSame('application/rss+xml', $Response->type());
- $this->assertTextEquals($expected, $result);
- }
- /**
- * RssViewTest::testSerializeWithUnconfiguredPrefix()
- *
- * @expectedException RuntimeException
- * @return void
- */
- public function testSerializeWithUnconfiguredPrefix() {
- $Request = new Request();
- $Response = new Response();
- $data = array(
- 'channel' => array(
- 'foo:bar' => 'something',
- ),
- 'items' => array(
- array('title' => 'Title Two'),
- )
- );
- $viewVars = array('channel' => $data, '_serialize' => 'channel');
- $View = new RssView($Request, $Response, null, ['viewVars' => $viewVars]);
- $result = $View->render(false);
- }
- /**
- * RssViewTest::testSerializeWithArrayLinks()
- *
- * `'atom:link' => array('@href' => array(...)` becomes
- * '@rel' => 'self', '@type' => 'application/rss+xml' automatically set for atom:link
- *
- * @return void
- */
- public function testSerializeWithArrayLinks() {
- $Request = new Request();
- $Response = new Response();
- $data = array(
- 'channel' => array(
- 'title' => 'Channel title',
- 'link' => 'http://channel.example.org',
- 'atom:link' => array('@href' => array('controller' => 'foo', 'action' => 'bar')),
- 'description' => 'Channel description',
- ),
- 'items' => array(
- array('title' => 'Title One', 'link' => array('controller' => 'foo', 'action' => 'bar'), 'description' => 'Content one'),
- array('title' => 'Title Two', 'link' => array('controller' => 'foo', 'action' => 'bar'), 'description' => 'Content two'),
- )
- );
- $viewVars = array('channel' => $data, '_serialize' => 'channel');
- $View = new RssView($Request, $Response, null, ['viewVars' => $viewVars]);
- $result = $View->render(false);
- $expected = <<<RSS
- <?xml version="1.0" encoding="UTF-8"?>
- <rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
- <channel>
- <title>Channel title</title>
- <link>http://channel.example.org</link>
- <atom:link href="$this->baseUrl/foo/bar" rel="self" type="application/rss+xml"/>
- <description>Channel description</description>
- <item>
- <title>Title One</title>
- <link>$this->baseUrl/foo/bar</link>
- <description>Content one</description>
- </item>
- <item>
- <title>Title Two</title>
- <link>$this->baseUrl/foo/bar</link>
- <description>Content two</description>
- </item>
- </channel>
- </rss>
- RSS;
- //debug($result);
- $this->assertSame('application/rss+xml', $Response->type());
- $this->assertTextEquals($expected, $result);
- }
- /**
- * RssViewTest::testSerializeWithContent()
- *
- * @return void
- */
- public function testSerializeWithContent() {
- $Request = new Request();
- $Response = new Response();
- $data = array(
- 'channel' => array(
- 'title' => 'Channel title',
- 'link' => 'http://channel.example.org',
- 'guid' => array('url' => 'http://channel.example.org', '@isPermaLink' => 'true'),
- 'atom:link' => array('@href' => array('controller' => 'foo', 'action' => 'bar')),
- ),
- 'items' => array(
- array('title' => 'Title One', 'link' => array('controller' => 'foo', 'action' => 'bar'), 'description' => 'Content one',
- 'content:encoded' => 'HTML <img src="http://domain.com/some/link/to/image.jpg"/> <b>content</b> one'),
- array('title' => 'Title Two', 'link' => array('controller' => 'foo', 'action' => 'bar'), 'description' => 'Content two',
- 'content:encoded' => 'HTML <img src="http://domain.com/some/link/to/image.jpg"/> <b>content</b> two'),
- )
- );
- $viewVars = array('channel' => $data, '_serialize' => 'channel');
- $View = new RssView($Request, $Response, null, ['viewVars' => $viewVars]);
- $result = $View->render(false);
- $expected = <<<RSS
- <?xml version="1.0" encoding="UTF-8"?>
- <rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0">
- <channel>
- <title>Channel title</title>
- <link>http://channel.example.org</link>
- <guid isPermaLink="true">http://channel.example.org</guid>
- <atom:link href="$this->baseUrl/foo/bar" rel="self" type="application/rss+xml"/>
- <description/>
- <item>
- <title>Title One</title>
- <link>$this->baseUrl/foo/bar</link>
- <description>Content one</description>
- <content:encoded><![CDATA[HTML <img src="http://domain.com/some/link/to/image.jpg"/> <b>content</b> one]]></content:encoded>
- </item>
- <item>
- <title>Title Two</title>
- <link>$this->baseUrl/foo/bar</link>
- <description>Content two</description>
- <content:encoded><![CDATA[HTML <img src="http://domain.com/some/link/to/image.jpg"/> <b>content</b> two]]></content:encoded>
- </item>
- </channel>
- </rss>
- RSS;
- //debug($output);
- $this->assertTextEquals($expected, $result);
- }
- /**
- * RssViewTest::testSerializeWithCustomNamespace()
- *
- * @return void
- */
- public function testSerializeWithCustomNamespace() {
- $Request = new Request();
- $Response = new Response();
- $data = array(
- 'document' => array(
- 'namespace' => array(
- 'admin' => 'http://webns.net/mvcb/',
- 'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'
- )
- ),
- 'channel' => array(
- 'title' => 'Channel title',
- 'admin:errorReportsTo' => array('@rdf:resource' => 'mailto:me@example.com')
- ),
- 'items' => array(
- array('title' => 'Title One', 'link' => array('controller' => 'foo', 'action' => 'bar')),
- )
- );
- $viewVars = array('channel' => $data, '_serialize' => 'channel');
- $View = new RssView($Request, $Response, null, ['viewVars' => $viewVars]);
- $result = $View->render(false);
- $expected = <<<RSS
- <?xml version="1.0" encoding="UTF-8"?>
- <rss xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" version="2.0">
- <channel>
- <title>Channel title</title>
- <admin:errorReportsTo rdf:resource="mailto:me@example.com"/>
- <link>$this->baseUrl/</link>
- <description/>
- <item>
- <title>Title One</title>
- <link>$this->baseUrl/foo/bar</link>
- </item>
- </channel>
- </rss>
- RSS;
- //debug($result);
- $this->assertTextEquals($expected, $result);
- }
- /**
- * RssViewTest::testSerializeWithImage()
- *
- * @return void
- */
- public function testSerializeWithImage() {
- $Request = new Request();
- $Response = new Response();
- $url = array('controller' => 'topics', 'action' => 'feed', '_ext' => 'rss');
- $data = array(
- 'channel' => array(
- 'title' => 'Channel title',
- 'guid' => array('url' => $url, '@isPermaLink' => 'true'),
- 'image' => array(
- 'url' => '/img/logo_rss.png',
- 'link' => '/'
- )
- ),
- 'items' => array(
- array('title' => 'Title One', 'link' => array('controller' => 'foo', 'action' => 'bar')),
- )
- );
- $viewVars = array('channel' => $data, '_serialize' => 'channel');
- $View = new RssView($Request, $Response, null, ['viewVars' => $viewVars]);
- $result = $View->render(false);
- $expected = <<<RSS
- <?xml version="1.0" encoding="UTF-8"?>
- <rss version="2.0">
- <channel>
- <title>Channel title</title>
- <guid isPermaLink="true">$this->baseUrl/topics/feed.rss</guid>
- <image>
- <url>$this->baseUrl/img/logo_rss.png</url>
- <link>$this->baseUrl/</link>
- <title>Channel title</title>
- </image>
- <link>$this->baseUrl/</link>
- <description/>
- <item>
- <title>Title One</title>
- <link>$this->baseUrl/foo/bar</link>
- </item>
- </channel>
- </rss>
- RSS;
- $this->assertTextEquals($expected, $result);
- }
- /**
- * RssViewTest::testSerializeWithCategories()
- *
- * @return void
- */
- public function testSerializeWithCategories() {
- $Request = new Request();
- $Response = new Response();
- $data = array(
- 'channel' => array(
- 'title' => 'Channel title',
- 'link' => 'http://channel.example.org',
- 'category' => 'IT/Internet/Web development & more',
- ),
- 'items' => array(
- array('title' => 'Title One', 'link' => array('controller' => 'foo', 'action' => 'bar'), 'description' => 'Content one',
- 'category' => 'Internet'),
- array('title' => 'Title Two', 'link' => array('controller' => 'foo', 'action' => 'bar'), 'description' => 'Content two',
- 'category' => array('News', 'Tutorial'),
- 'comments' => array('controller' => 'foo', 'action' => 'bar', '_ext' => 'rss')),
- )
- );
- $viewVars = array('channel' => $data, '_serialize' => 'channel');
- $View = new RssView($Request, $Response, null, ['viewVars' => $viewVars]);
- $result = $View->render(false);
- $expected = <<<RSS
- <?xml version="1.0" encoding="UTF-8"?>
- <rss version="2.0">
- <channel>
- <title>Channel title</title>
- <link>http://channel.example.org</link>
- <category>IT/Internet/Web development & more</category>
- <description/>
- <item>
- <title>Title One</title>
- <link>$this->baseUrl/foo/bar</link>
- <description>Content one</description>
- <category>Internet</category>
- </item>
- <item>
- <title>Title Two</title>
- <link>$this->baseUrl/foo/bar</link>
- <description>Content two</description>
- <category>News</category>
- <category>Tutorial</category>
- <comments>$this->baseUrl/foo/bar.rss</comments>
- </item>
- </channel>
- </rss>
- RSS;
- $this->assertTextEquals($expected, $result);
- }
- /**
- * RssViewTest::testSerializeWithEnclosure()
- *
- * @return void
- */
- public function testSerializeWithEnclosure() {
- $Request = new Request();
- $Response = new Response();
- $data = array(
- 'channel' => array(
- 'title' => 'Channel title',
- 'link' => 'http://channel.example.org',
- ),
- 'items' => array(
- array('title' => 'Title One', 'link' => array('controller' => 'foo', 'action' => 'bar'), 'description' => 'Content one',
- 'enclosure' => array('url' => 'http://www.example.com/media/3d.wmv', 'length' => 78645, 'type' => 'video/wmv')),
- )
- );
- $viewVars = array('channel' => $data, '_serialize' => 'channel');
- $View = new RssView($Request, $Response, null, ['viewVars' => $viewVars]);
- $result = $View->render(false);
- $expected = <<<RSS
- <?xml version="1.0" encoding="UTF-8"?>
- <rss version="2.0">
- <channel>
- <title>Channel title</title>
- <link>http://channel.example.org</link>
- <description/>
- <item>
- <title>Title One</title>
- <link>$this->baseUrl/foo/bar</link>
- <description>Content one</description>
- <enclosure url="http://www.example.com/media/3d.wmv" length="78645" type="video/wmv"/>
- </item>
- </channel>
- </rss>
- RSS;
- $this->assertTextEquals($expected, $result);
- }
- /**
- * RssViewTest::testSerializeWithCustomTags()
- *
- * @return void
- */
- public function testSerializeWithCustomTags() {
- $Request = new Request();
- $Response = new Response();
- $data = array(
- 'channel' => array(
- 'title' => 'Channel title',
- 'link' => 'http://channel.example.org',
- ),
- 'items' => array(
- array('title' => 'Title One', 'link' => array('controller' => 'foo', 'action' => 'bar'), 'description' => 'Content one',
- 'foo' => array('@url' => 'http://www.example.com/media/3d.wmv', '@length' => 78645, '@type' => 'video/wmv')),
- )
- );
- $viewVars = array('channel' => $data, '_serialize' => 'channel');
- $View = new RssView($Request, $Response, null, ['viewVars' => $viewVars]);
- $result = $View->render(false);
- $expected = <<<RSS
- <?xml version="1.0" encoding="UTF-8"?>
- <rss version="2.0">
- <channel>
- <title>Channel title</title>
- <link>http://channel.example.org</link>
- <description/>
- <item>
- <title>Title One</title>
- <link>$this->baseUrl/foo/bar</link>
- <description>Content one</description>
- <foo url="http://www.example.com/media/3d.wmv" length="78645" type="video/wmv"/>
- </item>
- </channel>
- </rss>
- RSS;
- $this->assertTextEquals($expected, $result);
- }
- /**
- * RssViewTest::testSerializeWithSpecialChars()
- *
- * @return void
- */
- public function testSerializeWithSpecialChars() {
- $Request = new Request();
- $Response = new Response();
- $data = array(
- 'channel' => array(
- 'title' => 'Channel title with äöü umlauts and <!> special chars',
- 'link' => 'http://channel.example.org',
- ),
- 'items' => array(
- array(
- 'title' => 'A <unsafe title',
- 'link' => array('controller' => 'foo', 'action' => 'bar'),
- 'description' => 'My content "&" and <other> stuff here should also be escaped safely'),
- )
- );
- $viewVars = array('channel' => $data, '_serialize' => 'channel');
- $View = new RssView($Request, $Response, null, ['viewVars' => $viewVars]);
- $result = $View->render(false);
- $expected = <<<RSS
- <?xml version="1.0" encoding="UTF-8"?>
- <rss version="2.0">
- <channel>
- <title>Channel title with äöü umlauts and <!> special chars</title>
- <link>http://channel.example.org</link>
- <description/>
- <item>
- <title>A <unsafe title</title>
- <link>$this->baseUrl/foo/bar</link>
- <description>My content "&" and <other> stuff here should also be escaped safely</description>
- </item>
- </channel>
- </rss>
- RSS;
- $this->assertTextEquals($expected, $result);
- }
- }
|