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 = << Channel title http://channel.example.org Channel description Title One http://example.org/one one@example.org Content one http://foo.bar Title Two http://example.org/two two@example.org Content two Foo bar 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 = << Channel title http://channel.example.org Channel description hourly 1 Title One http://example.org/one Author One $time Title Two http://example.org/two Author Two $time http://foo.bar 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 = << Channel title http://channel.example.org Channel description Title One $this->baseUrl/foo/bar Content one Title Two $this->baseUrl/foo/bar Content two 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 content one'), array('title' => 'Title Two', 'link' => array('controller' => 'foo', 'action' => 'bar'), 'description' => 'Content two', 'content:encoded' => 'HTML content two'), ) ); $viewVars = array('channel' => $data, '_serialize' => 'channel'); $View = new RssView($Request, $Response, null, ['viewVars' => $viewVars]); $result = $View->render(false); $expected = << Channel title http://channel.example.org http://channel.example.org Title One $this->baseUrl/foo/bar Content one content one]]> Title Two $this->baseUrl/foo/bar Content two content two]]> 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 = << Channel title $this->baseUrl/ Title One $this->baseUrl/foo/bar 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 = << Channel title $this->baseUrl/topics/feed.rss $this->baseUrl/img/logo_rss.png $this->baseUrl/ Channel title $this->baseUrl/ Title One $this->baseUrl/foo/bar 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 = << Channel title http://channel.example.org IT/Internet/Web development & more Title One $this->baseUrl/foo/bar Content one Internet Title Two $this->baseUrl/foo/bar Content two News Tutorial $this->baseUrl/foo/bar.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 = << Channel title http://channel.example.org Title One $this->baseUrl/foo/bar Content one 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 = << Channel title http://channel.example.org Title One $this->baseUrl/foo/bar Content one 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 array('controller' => 'foo', 'action' => 'bar'), 'description' => 'My content "&" and 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 = << Channel title with äöü umlauts and <!> special chars http://channel.example.org A <unsafe title $this->baseUrl/foo/bar My content "&" and <other> stuff here should also be escaped safely RSS; $this->assertTextEquals($expected, $result); } }