diff --git a/api/RSSFeed.php b/api/RSSFeed.php index 2c569e9cb..d27f595be 100755 --- a/api/RSSFeed.php +++ b/api/RSSFeed.php @@ -189,11 +189,18 @@ class RSSFeed extends ViewableData { HTTP::register_etag($this->etag); } - $body = str_replace(' ', ' ', $this->renderWith('RSSFeed')); + $body = $this->feedContent(); HTTP::add_cache_headers($body); header("Content-type: text/xml"); echo $body; } + + /** + * Return the content of the RSS feed + */ + function feedContent() { + return str_replace(' ', ' ', $this->renderWith('RSSFeed')); + } } /** @@ -251,8 +258,7 @@ class RSSFeed_Entry extends ViewableData { * @return string Returns the description of the entry. */ function Title() { - if($this->titleField) - return $this->failover->obj($this->titleField); + return $this->rssField($this->titleField, 'Varchar'); } /** @@ -261,8 +267,7 @@ class RSSFeed_Entry extends ViewableData { * @return string Returns the description of the entry. */ function Description() { - if($this->descriptionField) - return $this->failover->obj($this->descriptionField); + return $this->rssField($this->descriptionField, 'Text'); } /** @@ -275,6 +280,22 @@ class RSSFeed_Entry extends ViewableData { return $this->failover->obj($this->authorField); } + /** + * Return the named field as an obj() call from $this->failover. + * Default to the given class if there's no casting information. + */ + function rssField($fieldName, $defaultClass = 'Varchar') { + if($fieldName) { + if($this->failover->castingHelperPair($fieldName)) { + return $this->failover->obj($fieldName); + } else { + $obj = new $defaultClass($fieldName); + $obj->setValue($this->failover->XML_val($fieldName)); + return $obj; + } + } + } + /** * Get a link to this entry * @@ -282,7 +303,7 @@ class RSSFeed_Entry extends ViewableData { */ function AbsoluteLink() { if($this->failover->hasMethod('AbsoluteLink')) return $this->failover->AbsoluteLink(); - else if($this->failover->hasMethod('Link')) return Director::absoluteURL($this->link); + else if($this->failover->hasMethod('Link')) return Director::absoluteURL($this->failover->Link()); else user_error($this->failover->class . " object has either an AbsoluteLink nor a Link method. Can't put a link in the RSS feed", E_USER_WARNING); } } diff --git a/tests/api/RSSFeedTest.php b/tests/api/RSSFeedTest.php new file mode 100644 index 000000000..868b502e7 --- /dev/null +++ b/tests/api/RSSFeedTest.php @@ -0,0 +1,101 @@ +push(new RSSFeedTest_ItemA()); + $list->push(new RSSFeedTest_ItemB()); + $list->push(new RSSFeedTest_ItemC()); + + $rssFeed = new RSSFeed($list, "http://www.example.com", "Test RSS Feed", "Test RSS Feed Description"); + $content = $rssFeed->feedContent(); + + // Debug::message($content); + + $this->assertContains('' . Director::absoluteBaseURL() . 'item-a/', $content); + $this->assertContains('http://www.example.com/item-b.html', $content); + $this->assertContains('http://www.example.com/item-c.html', $content); + + $this->assertContains('ItemA', $content); + $this->assertContains('ItemB', $content); + $this->assertContains('ItemC', $content); + + $this->assertContains('ItemA Content', $content); + $this->assertContains('ItemB Content', $content); + $this->assertContains('ItemC Content', $content); + + + // Feed #2 - put Content() into and AltContent() into <description> + $rssFeed = new RSSFeed($list, "http://www.example.com", "Test RSS Feed", "Test RSS Feed Description", "Content", "AltContent"); + $content = $rssFeed->feedContent(); + + $this->assertContains('<title>ItemA Content', $content); + $this->assertContains('ItemB Content', $content); + $this->assertContains('ItemC Content', $content); + + $this->assertContains('ItemA AltContent', $content); + $this->assertContains('ItemB AltContent', $content); + $this->assertContains('ItemC AltContent', $content); + + } +} + +class RSSFeedTest_ItemA extends ViewableData { + // RSS-feed items must have $casting/$db information. + static $casting = array( + 'Title' => 'Varchar', + 'Content' => 'Text', + 'AltContent' => 'Text', + ); + + function Title() { + return "ItemA"; + } + function Content() { + return "ItemA Content"; + } + function AltContent() { + return "ItemA AltContent"; + } + + function Link() { + return "item-a/"; + } +} + +class RSSFeedTest_ItemB extends ViewableData { + // ItemB tests without $casting + + function Title() { + return "ItemB"; + } + function AbsoluteLink() { + return "http://www.example.com/item-b.html"; + } + function Content() { + return "ItemB Content"; + } + function AltContent() { + return "ItemB AltContent"; + } +} + +class RSSFeedTest_ItemC extends ViewableData { + // ItemC tests fields - Title has casting, Content doesn't. + static $casting = array( + 'Title' => 'Varchar', + 'AltContent' => 'Text', + ); + + public $Title = "ItemC"; + public $Content = "ItemC Content"; + public $AltContent = "ItemC AltContent"; + + function Link() { + return "item-c.html"; + } + function AbsoluteLink() { + return "http://www.example.com/item-c.html"; + } +} \ No newline at end of file