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 CakeRequest(); $Response = new CakeResponse(); $Controller = new Controller($Request, $Response); $data = [ 'channel' => [ 'title' => 'Channel title', 'link' => 'http://channel.example.org', 'description' => 'Channel description' ], 'items' => [ ['title' => 'Title One', 'link' => 'http://example.org/one', 'author' => 'one@example.org', 'description' => 'Content one', 'source' => ['url' => 'http://foo.bar']], ['title' => 'Title Two', 'link' => 'http://example.org/two', 'author' => 'two@example.org', 'description' => 'Content two', 'source' => ['url' => 'http://foo.bar', 'content' => 'Foo bar']], ] ]; $Controller->set(['channel' => $data, '_serialize' => 'channel']); $View = new RssView($Controller); $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 CakeRequest(); $Response = new CakeResponse(); $Controller = new Controller($Request, $Response); $time = time(); $data = [ 'channel' => [ 'title' => 'Channel title', 'link' => 'http://channel.example.org', 'description' => 'Channel description', 'sy:updatePeriod' => 'hourly', 'sy:updateFrequency' => 1 ], 'items' => [ ['title' => 'Title One', 'link' => 'http://example.org/one', 'dc:creator' => 'Author One', 'pubDate' => $time], ['title' => 'Title Two', 'link' => 'http://example.org/two', 'dc:creator' => 'Author Two', 'pubDate' => $time, 'source' => 'http://foo.bar'], ] ]; $Controller->set(['channel' => $data, '_serialize' => 'channel']); $View = new RssView($Controller); $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; //debug($result); $this->assertSame('application/rss+xml', $Response->type()); $this->assertTextEquals($expected, $result); } /** * RssViewTest::testSerializeWithUnconfiguredPrefix() * * @expectedException RuntimeException * @return void */ public function testSerializeWithUnconfiguredPrefix() { $Request = new CakeRequest(); $Response = new CakeResponse(); $Controller = new Controller($Request, $Response); $data = [ 'channel' => [ 'foo:bar' => 'something', ], 'items' => [ ['title' => 'Title Two'], ] ]; $Controller->set(['channel' => $data, '_serialize' => 'channel']); $View = new RssView($Controller); $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 CakeRequest(); $Response = new CakeResponse(); $Controller = new Controller($Request, $Response); $data = [ 'channel' => [ 'title' => 'Channel title', 'link' => 'http://channel.example.org', 'atom:link' => ['@href' => ['controller' => 'foo', 'action' => 'bar']], 'description' => 'Channel description', ], 'items' => [ ['title' => 'Title One', 'link' => ['controller' => 'foo', 'action' => 'bar'], 'description' => 'Content one'], ['title' => 'Title Two', 'link' => ['controller' => 'foo', 'action' => 'bar'], 'description' => 'Content two'], ] ]; $Controller->set(['channel' => $data, '_serialize' => 'channel']); $View = new RssView($Controller); $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 CakeRequest(); $Response = new CakeResponse(); $Controller = new Controller($Request, $Response); $data = [ 'channel' => [ 'title' => 'Channel title', 'link' => 'http://channel.example.org', 'guid' => ['url' => 'http://channel.example.org', '@isPermaLink' => 'true'], 'atom:link' => ['@href' => ['controller' => 'foo', 'action' => 'bar']], ], 'items' => [ ['title' => 'Title One', 'link' => ['controller' => 'foo', 'action' => 'bar'], 'description' => 'Content one', 'content:encoded' => 'HTML content one'], ['title' => 'Title Two', 'link' => ['controller' => 'foo', 'action' => 'bar'], 'description' => 'Content two', 'content:encoded' => 'HTML content two'], ] ]; $Controller->set(['channel' => $data, '_serialize' => 'channel']); $View = new RssView($Controller); $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 CakeRequest(); $Response = new CakeResponse(); $Controller = new Controller($Request, $Response); $data = [ 'document' => [ 'namespace' => [ 'admin' => 'http://webns.net/mvcb/', 'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#' ] ], 'channel' => [ 'title' => 'Channel title', 'admin:errorReportsTo' => ['@rdf:resource' => 'mailto:me@example.com'] ], 'items' => [ ['title' => 'Title One', 'link' => ['controller' => 'foo', 'action' => 'bar']], ] ]; $Controller->set(['channel' => $data, '_serialize' => 'channel']); $View = new RssView($Controller); $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 CakeRequest(); $Response = new CakeResponse(); $Controller = new Controller($Request, $Response); $url = ['controller' => 'topics', 'action' => 'feed', 'ext' => 'rss']; $data = [ 'channel' => [ 'title' => 'Channel title', 'guid' => ['url' => $url, '@isPermaLink' => 'true'], 'image' => [ 'url' => '/img/logo_rss.png', 'link' => '/' ] ], 'items' => [ ['title' => 'Title One', 'link' => ['controller' => 'foo', 'action' => 'bar']], ] ]; $Controller->set(['channel' => $data, '_serialize' => 'channel']); $View = new RssView($Controller); $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 CakeRequest(); $Response = new CakeResponse(); $Controller = new Controller($Request, $Response); $data = [ 'channel' => [ 'title' => 'Channel title', 'link' => 'http://channel.example.org', 'category' => 'IT/Internet/Web development & more', ], 'items' => [ ['title' => 'Title One', 'link' => ['controller' => 'foo', 'action' => 'bar'], 'description' => 'Content one', 'category' => 'Internet'], ['title' => 'Title Two', 'link' => ['controller' => 'foo', 'action' => 'bar'], 'description' => 'Content two', 'category' => ['News', 'Tutorial'], 'comments' => ['controller' => 'foo', 'action' => 'bar', 'ext' => 'rss']], ] ]; $Controller->set(['channel' => $data, '_serialize' => 'channel']); $View = new RssView($Controller); $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 CakeRequest(); $Response = new CakeResponse(); $Controller = new Controller($Request, $Response); $data = [ 'channel' => [ 'title' => 'Channel title', 'link' => 'http://channel.example.org', ], 'items' => [ ['title' => 'Title One', 'link' => ['controller' => 'foo', 'action' => 'bar'], 'description' => 'Content one', 'enclosure' => ['url' => 'http://www.example.com/media/3d.wmv', 'length' => 78645, 'type' => 'video/wmv']], ] ]; $Controller->set(['channel' => $data, '_serialize' => 'channel']); $View = new RssView($Controller); $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 CakeRequest(); $Response = new CakeResponse(); $Controller = new Controller($Request, $Response); $data = [ 'channel' => [ 'title' => 'Channel title', 'link' => 'http://channel.example.org', ], 'items' => [ ['title' => 'Title One', 'link' => ['controller' => 'foo', 'action' => 'bar'], 'description' => 'Content one', 'foo' => ['@url' => 'http://www.example.com/media/3d.wmv', '@length' => 78645, '@type' => 'video/wmv']], ] ]; $Controller->set(['channel' => $data, '_serialize' => 'channel']); $View = new RssView($Controller); $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 CakeRequest(); $Response = new CakeResponse(); $Controller = new Controller($Request, $Response); $data = [ 'channel' => [ 'title' => 'Channel title with äöü umlauts and special chars', 'link' => 'http://channel.example.org', ], 'items' => [ [ 'title' => 'A ['controller' => 'foo', 'action' => 'bar'], 'description' => 'My content "&" and stuff here should also be escaped safely'], ] ]; $Controller->set(['channel' => $data, '_serialize' => 'channel']); $View = new RssView($Controller); $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); } }