Skip to content
This repository
Browse code

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
Will Rossiter authored July 31, 2012
32  api/RSSFeed.php
@@ -144,6 +144,7 @@ static function linkToFeed($url, $title = null) {
144 144
 	 */
145 145
 	function Entries() {
146 146
 		$output = new ArrayList();
  147
+
147 148
 		if(isset($this->entries)) {
148 149
 			foreach($this->entries as $entry) {
149 150
 				$output->push(new RSSFeed_Entry($entry, $this->titleField, $this->descriptionField, $this->authorField));
@@ -183,7 +184,10 @@ function Description() {
183 184
 	/**
184 185
 	 * Output the feed to the browser
185 186
 	 */
186  
-	function outputToBrowser() {
  187
+	public function outputToBrowser() {
  188
+		$prevState = SSViewer::get_source_file_comments();
  189
+		SSViewer::set_source_file_comments(false);
  190
+
187 191
 		if(is_int($this->lastModified)) {
188 192
 			HTTP::register_modification_timestamp($this->lastModified);
189 193
 			header('Last-Modified: ' . gmdate("D, d M Y H:i:s", $this->lastModified) . ' GMT');
@@ -192,26 +196,14 @@ function outputToBrowser() {
192 196
 			HTTP::register_etag($this->etag);
193 197
 		}
194 198
 
195  
-		$body = $this->feedContent();
196  
-		HTTP::add_cache_headers();
197  
-		header("Content-type: text/xml");
198  
-		echo $body;
199  
-	}
200  
-	
201  
-	/**
202  
-	 * Return the content of the RSS feed.
203  
-	 * 
204  
-	 * Also temporarily disabled source file comments, and restores
205  
-	 * to previous state once content has been rendered.
206  
-	 * 
207  
-	 * @return string
208  
-	 */
209  
-	function feedContent() {
210  
-		$prevState = SSViewer::get_source_file_comments();
211  
-		SSViewer::set_source_file_comments(false);
212  
-		$content = str_replace(' ', ' ', $this->renderWith($this->getTemplate()));
  199
+		if(!headers_sent()) {
  200
+			HTTP::add_cache_headers();
  201
+			header("Content-type: text/xml");
  202
+		}
  203
+
213 204
 		SSViewer::set_source_file_comments($prevState);
214  
-		return $content;
  205
+
  206
+		return $this->renderWith($this->getTemplate());
215 207
 	}
216 208
 
217 209
 	/**
19  tests/api/RSSFeedTest.php
@@ -14,7 +14,7 @@ function testRSSFeed() {
14 14
 		$list->push(new RSSFeedTest_ItemC());
15 15
 
16 16
 		$rssFeed = new RSSFeed($list, "http://www.example.com", "Test RSS Feed", "Test RSS Feed Description");
17  
-		$content = $rssFeed->feedContent();
  17
+		$content = $rssFeed->outputToBrowser();
18 18
 
19 19
 		//Debug::message($content);
20 20
 		$this->assertContains('<link>http://www.example.org/item-a/</link>', $content);
@@ -32,7 +32,7 @@ function testRSSFeed() {
32 32
 
33 33
 		// Feed #2 - put Content() into <title> and AltContent() into <description>
34 34
 		$rssFeed = new RSSFeed($list, "http://www.example.com", "Test RSS Feed", "Test RSS Feed Description", "Content", "AltContent");
35  
-		$content = $rssFeed->feedContent();
  35
+		$content = $rssFeed->outputToBrowser();
36 36
 
37 37
 		$this->assertContains('<title>ItemA Content</title>', $content);
38 38
 		$this->assertContains('<title>ItemB Content</title>', $content);
@@ -47,13 +47,11 @@ public function testRenderWithTemplate() {
47 47
 		$rssFeed = new RSSFeed(new ArrayList(), "", "", "");
48 48
 		$rssFeed->setTemplate('RSSFeedTest');
49 49
 
50  
-		$content = $rssFeed->feedContent();
51  
-
  50
+		$content = $rssFeed->outputToBrowser();
52 51
 		$this->assertContains('<title>Test Custom Template</title>', $content);
53 52
 
54 53
 		$rssFeed->setTemplate('RSSFeed');
55  
-		$content = $rssFeed->feedContent();
56  
-		
  54
+		$content = $rssFeed->outputToBrowser();
57 55
 		$this->assertNotContains('<title>Test Custom Template</title>', $content);
58 56
 	}
59 57
 
@@ -69,7 +67,6 @@ public function tearDown() {
69 67
 		Director::setBaseURL(null);
70 68
 		$_SERVER['HTTP_HOST'] = self::$original_host;
71 69
 	}
72  
-
73 70
 }
74 71
 
75 72
 class RSSFeedTest_ItemA extends ViewableData {
@@ -83,9 +80,11 @@ class RSSFeedTest_ItemA extends ViewableData {
83 80
 	function getTitle() {
84 81
 		return "ItemA";
85 82
 	}
  83
+
86 84
 	function getContent() {
87 85
 		return "ItemA Content";
88 86
 	}
  87
+
89 88
 	function getAltContent() {
90 89
 		return "ItemA AltContent";
91 90
 	}
@@ -101,12 +100,15 @@ class RSSFeedTest_ItemB extends ViewableData {
101 100
 	function Title() {
102 101
 		return "ItemB";
103 102
 	}
  103
+
104 104
 	function AbsoluteLink() {
105 105
 		return "http://www.example.com/item-b.html";
106 106
 	}
  107
+
107 108
 	function Content() {
108 109
 		return "ItemB Content";
109 110
 	}
  111
+
110 112
 	function AltContent() {
111 113
 		return "ItemB AltContent";
112 114
 	}
@@ -114,7 +116,7 @@ function AltContent() {
114 116
 
115 117
 class RSSFeedTest_ItemC extends ViewableData {
116 118
 	// ItemC tests fields - Title has casting, Content doesn't.
117  
-	static $casting = array(
  119
+	public static $casting = array(
118 120
 		'Title' => 'Varchar',
119 121
 		'AltContent' => 'Text',
120 122
 	);
@@ -126,6 +128,7 @@ class RSSFeedTest_ItemC extends ViewableData {
126 128
 	function Link() {
127 129
 		return "item-c.html";
128 130
 	}
  131
+
129 132
 	function AbsoluteLink() {
130 133
 		return "http://www.example.com/item-c.html";
131 134
 	}

0 notes on commit 7558d32

Please sign in to comment.
Something went wrong with that request. Please try again.