mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
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.
This commit is contained in:
parent
ec25afc61e
commit
7558d32da6
@ -144,6 +144,7 @@ class RSSFeed extends ViewableData {
|
|||||||
*/
|
*/
|
||||||
function Entries() {
|
function Entries() {
|
||||||
$output = new ArrayList();
|
$output = new ArrayList();
|
||||||
|
|
||||||
if(isset($this->entries)) {
|
if(isset($this->entries)) {
|
||||||
foreach($this->entries as $entry) {
|
foreach($this->entries as $entry) {
|
||||||
$output->push(new RSSFeed_Entry($entry, $this->titleField, $this->descriptionField, $this->authorField));
|
$output->push(new RSSFeed_Entry($entry, $this->titleField, $this->descriptionField, $this->authorField));
|
||||||
@ -183,7 +184,10 @@ class RSSFeed extends ViewableData {
|
|||||||
/**
|
/**
|
||||||
* Output the feed to the browser
|
* 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)) {
|
if(is_int($this->lastModified)) {
|
||||||
HTTP::register_modification_timestamp($this->lastModified);
|
HTTP::register_modification_timestamp($this->lastModified);
|
||||||
header('Last-Modified: ' . gmdate("D, d M Y H:i:s", $this->lastModified) . ' GMT');
|
header('Last-Modified: ' . gmdate("D, d M Y H:i:s", $this->lastModified) . ' GMT');
|
||||||
@ -192,26 +196,14 @@ class RSSFeed extends ViewableData {
|
|||||||
HTTP::register_etag($this->etag);
|
HTTP::register_etag($this->etag);
|
||||||
}
|
}
|
||||||
|
|
||||||
$body = $this->feedContent();
|
if(!headers_sent()) {
|
||||||
HTTP::add_cache_headers();
|
HTTP::add_cache_headers();
|
||||||
header("Content-type: text/xml");
|
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()));
|
|
||||||
SSViewer::set_source_file_comments($prevState);
|
SSViewer::set_source_file_comments($prevState);
|
||||||
return $content;
|
|
||||||
|
return $this->renderWith($this->getTemplate());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -14,7 +14,7 @@ class RSSFeedTest extends SapphireTest {
|
|||||||
$list->push(new RSSFeedTest_ItemC());
|
$list->push(new RSSFeedTest_ItemC());
|
||||||
|
|
||||||
$rssFeed = new RSSFeed($list, "http://www.example.com", "Test RSS Feed", "Test RSS Feed Description");
|
$rssFeed = new RSSFeed($list, "http://www.example.com", "Test RSS Feed", "Test RSS Feed Description");
|
||||||
$content = $rssFeed->feedContent();
|
$content = $rssFeed->outputToBrowser();
|
||||||
|
|
||||||
//Debug::message($content);
|
//Debug::message($content);
|
||||||
$this->assertContains('<link>http://www.example.org/item-a/</link>', $content);
|
$this->assertContains('<link>http://www.example.org/item-a/</link>', $content);
|
||||||
@ -32,7 +32,7 @@ class RSSFeedTest extends SapphireTest {
|
|||||||
|
|
||||||
// Feed #2 - put Content() into <title> and AltContent() into <description>
|
// 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");
|
$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>ItemA Content</title>', $content);
|
||||||
$this->assertContains('<title>ItemB Content</title>', $content);
|
$this->assertContains('<title>ItemB Content</title>', $content);
|
||||||
@ -47,13 +47,11 @@ class RSSFeedTest extends SapphireTest {
|
|||||||
$rssFeed = new RSSFeed(new ArrayList(), "", "", "");
|
$rssFeed = new RSSFeed(new ArrayList(), "", "", "");
|
||||||
$rssFeed->setTemplate('RSSFeedTest');
|
$rssFeed->setTemplate('RSSFeedTest');
|
||||||
|
|
||||||
$content = $rssFeed->feedContent();
|
$content = $rssFeed->outputToBrowser();
|
||||||
|
|
||||||
$this->assertContains('<title>Test Custom Template</title>', $content);
|
$this->assertContains('<title>Test Custom Template</title>', $content);
|
||||||
|
|
||||||
$rssFeed->setTemplate('RSSFeed');
|
$rssFeed->setTemplate('RSSFeed');
|
||||||
$content = $rssFeed->feedContent();
|
$content = $rssFeed->outputToBrowser();
|
||||||
|
|
||||||
$this->assertNotContains('<title>Test Custom Template</title>', $content);
|
$this->assertNotContains('<title>Test Custom Template</title>', $content);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -69,7 +67,6 @@ class RSSFeedTest extends SapphireTest {
|
|||||||
Director::setBaseURL(null);
|
Director::setBaseURL(null);
|
||||||
$_SERVER['HTTP_HOST'] = self::$original_host;
|
$_SERVER['HTTP_HOST'] = self::$original_host;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class RSSFeedTest_ItemA extends ViewableData {
|
class RSSFeedTest_ItemA extends ViewableData {
|
||||||
@ -83,9 +80,11 @@ class RSSFeedTest_ItemA extends ViewableData {
|
|||||||
function getTitle() {
|
function getTitle() {
|
||||||
return "ItemA";
|
return "ItemA";
|
||||||
}
|
}
|
||||||
|
|
||||||
function getContent() {
|
function getContent() {
|
||||||
return "ItemA Content";
|
return "ItemA Content";
|
||||||
}
|
}
|
||||||
|
|
||||||
function getAltContent() {
|
function getAltContent() {
|
||||||
return "ItemA AltContent";
|
return "ItemA AltContent";
|
||||||
}
|
}
|
||||||
@ -101,12 +100,15 @@ class RSSFeedTest_ItemB extends ViewableData {
|
|||||||
function Title() {
|
function Title() {
|
||||||
return "ItemB";
|
return "ItemB";
|
||||||
}
|
}
|
||||||
|
|
||||||
function AbsoluteLink() {
|
function AbsoluteLink() {
|
||||||
return "http://www.example.com/item-b.html";
|
return "http://www.example.com/item-b.html";
|
||||||
}
|
}
|
||||||
|
|
||||||
function Content() {
|
function Content() {
|
||||||
return "ItemB Content";
|
return "ItemB Content";
|
||||||
}
|
}
|
||||||
|
|
||||||
function AltContent() {
|
function AltContent() {
|
||||||
return "ItemB AltContent";
|
return "ItemB AltContent";
|
||||||
}
|
}
|
||||||
@ -114,7 +116,7 @@ class RSSFeedTest_ItemB extends ViewableData {
|
|||||||
|
|
||||||
class RSSFeedTest_ItemC extends ViewableData {
|
class RSSFeedTest_ItemC extends ViewableData {
|
||||||
// ItemC tests fields - Title has casting, Content doesn't.
|
// ItemC tests fields - Title has casting, Content doesn't.
|
||||||
static $casting = array(
|
public static $casting = array(
|
||||||
'Title' => 'Varchar',
|
'Title' => 'Varchar',
|
||||||
'AltContent' => 'Text',
|
'AltContent' => 'Text',
|
||||||
);
|
);
|
||||||
@ -126,6 +128,7 @@ class RSSFeedTest_ItemC extends ViewableData {
|
|||||||
function Link() {
|
function Link() {
|
||||||
return "item-c.html";
|
return "item-c.html";
|
||||||
}
|
}
|
||||||
|
|
||||||
function AbsoluteLink() {
|
function AbsoluteLink() {
|
||||||
return "http://www.example.com/item-c.html";
|
return "http://www.example.com/item-c.html";
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user