Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

FIX: use standard template rendering process for RSS feeds

This changes the behaviour of output to browser to use the standard SilverStripe rendering process rather than an echo statement to enable easier testability.
  • Loading branch information...
commit 7558d32da6d8229b9c8664b881fb3a008ca42704 1 parent ec25afc
@wilr wilr authored
Showing with 23 additions and 28 deletions.
  1. +12 −20 api/RSSFeed.php
  2. +11 −8 tests/api/RSSFeedTest.php
View
32 api/RSSFeed.php
@@ -144,6 +144,7 @@ static function linkToFeed($url, $title = null) {
*/
function Entries() {
$output = new ArrayList();
+
if(isset($this->entries)) {
foreach($this->entries as $entry) {
$output->push(new RSSFeed_Entry($entry, $this->titleField, $this->descriptionField, $this->authorField));
@@ -183,7 +184,10 @@ function Description() {
/**
* Output the feed to the browser
*/
- function outputToBrowser() {
+ public function outputToBrowser() {
+ $prevState = SSViewer::get_source_file_comments();
+ SSViewer::set_source_file_comments(false);
+
if(is_int($this->lastModified)) {
HTTP::register_modification_timestamp($this->lastModified);
header('Last-Modified: ' . gmdate("D, d M Y H:i:s", $this->lastModified) . ' GMT');
@@ -192,26 +196,14 @@ function outputToBrowser() {
HTTP::register_etag($this->etag);
}
- $body = $this->feedContent();
- HTTP::add_cache_headers();
- header("Content-type: text/xml");
- echo $body;
- }
-
- /**
- * Return the content of the RSS feed.
- *
- * Also temporarily disabled source file comments, and restores
- * to previous state once content has been rendered.
- *
- * @return string
- */
- function feedContent() {
- $prevState = SSViewer::get_source_file_comments();
- SSViewer::set_source_file_comments(false);
- $content = str_replace(' ', ' ', $this->renderWith($this->getTemplate()));
+ if(!headers_sent()) {
+ HTTP::add_cache_headers();
+ header("Content-type: text/xml");
+ }
+
SSViewer::set_source_file_comments($prevState);
- return $content;
+
+ return $this->renderWith($this->getTemplate());
}
/**
View
19 tests/api/RSSFeedTest.php
@@ -14,7 +14,7 @@ function testRSSFeed() {
$list->push(new RSSFeedTest_ItemC());
$rssFeed = new RSSFeed($list, "http://www.example.com", "Test RSS Feed", "Test RSS Feed Description");
- $content = $rssFeed->feedContent();
+ $content = $rssFeed->outputToBrowser();
//Debug::message($content);
$this->assertContains('<link>http://www.example.org/item-a/</link>', $content);
@@ -32,7 +32,7 @@ function testRSSFeed() {
// Feed #2 - put Content() into <title> and AltContent() into <description>
$rssFeed = new RSSFeed($list, "http://www.example.com", "Test RSS Feed", "Test RSS Feed Description", "Content", "AltContent");
- $content = $rssFeed->feedContent();
+ $content = $rssFeed->outputToBrowser();
$this->assertContains('<title>ItemA Content</title>', $content);
$this->assertContains('<title>ItemB Content</title>', $content);
@@ -47,13 +47,11 @@ public function testRenderWithTemplate() {
$rssFeed = new RSSFeed(new ArrayList(), "", "", "");
$rssFeed->setTemplate('RSSFeedTest');
- $content = $rssFeed->feedContent();
-
+ $content = $rssFeed->outputToBrowser();
$this->assertContains('<title>Test Custom Template</title>', $content);
$rssFeed->setTemplate('RSSFeed');
- $content = $rssFeed->feedContent();
-
+ $content = $rssFeed->outputToBrowser();
$this->assertNotContains('<title>Test Custom Template</title>', $content);
}
@@ -69,7 +67,6 @@ public function tearDown() {
Director::setBaseURL(null);
$_SERVER['HTTP_HOST'] = self::$original_host;
}
-
}
class RSSFeedTest_ItemA extends ViewableData {
@@ -83,9 +80,11 @@ class RSSFeedTest_ItemA extends ViewableData {
function getTitle() {
return "ItemA";
}
+
function getContent() {
return "ItemA Content";
}
+
function getAltContent() {
return "ItemA AltContent";
}
@@ -101,12 +100,15 @@ class RSSFeedTest_ItemB extends ViewableData {
function Title() {
return "ItemB";
}
+
function AbsoluteLink() {
return "http://www.example.com/item-b.html";
}
+
function Content() {
return "ItemB Content";
}
+
function AltContent() {
return "ItemB AltContent";
}
@@ -114,7 +116,7 @@ function AltContent() {
class RSSFeedTest_ItemC extends ViewableData {
// ItemC tests fields - Title has casting, Content doesn't.
- static $casting = array(
+ public static $casting = array(
'Title' => 'Varchar',
'AltContent' => 'Text',
);
@@ -126,6 +128,7 @@ class RSSFeedTest_ItemC extends ViewableData {
function Link() {
return "item-c.html";
}
+
function AbsoluteLink() {
return "http://www.example.com/item-c.html";
}
Please sign in to comment.
Something went wrong with that request. Please try again.