Browse Source

RssView updates

euromark 11 years ago
parent
commit
4a6cec877b
2 changed files with 191 additions and 31 deletions
  1. 158 8
      Test/Case/View/RssViewTest.php
  2. 33 23
      View/RssView.php

+ 158 - 8
Test/Case/View/RssViewTest.php

@@ -35,7 +35,7 @@ class RssViewTest extends CakeTestCase {
 
 
 		$this->Rss = new RssView();
 		$this->Rss = new RssView();
 
 
-		$this->baseUrl = php_sapi_name() === 'cli' ? 'http://localhost' : HTTP_BASE;
+		$this->baseUrl = trim(Router::url('/', true), '/');
 	}
 	}
 
 
 	/**
 	/**
@@ -65,9 +65,14 @@ class RssViewTest extends CakeTestCase {
 				'description' => 'Channel description'
 				'description' => 'Channel description'
 			),
 			),
 			'items' => array(
 			'items' => array(
-				array('title' => 'Title One', 'link' => 'http://example.org/one', 'author' => 'one@example.org', 'description' => 'Content one'),
-				array('title' => 'Title Two', 'link' => 'http://example.org/two', 'author' => 'two@example.org', 'description' => 'Content two'),
-			));
+				array('title' => 'Title One', 'link' => 'http://example.org/one',
+					'author' => 'one@example.org', 'description' => 'Content one',
+					'source' => array('url' => 'http://foo.bar')),
+				array('title' => 'Title Two', 'link' => 'http://example.org/two',
+					'author' => 'two@example.org', 'description' => 'Content two',
+					'source' => array('url' => 'http://foo.bar', 'content' => 'Foo bar')),
+			)
+		);
 		$Controller->set(array('channel' => $data, '_serialize' => 'channel'));
 		$Controller->set(array('channel' => $data, '_serialize' => 'channel'));
 		$View = new RssView($Controller);
 		$View = new RssView($Controller);
 		$result = $View->render(false);
 		$result = $View->render(false);
@@ -84,18 +89,19 @@ class RssViewTest extends CakeTestCase {
       <link>http://example.org/one</link>
       <link>http://example.org/one</link>
       <author>one@example.org</author>
       <author>one@example.org</author>
       <description>Content one</description>
       <description>Content one</description>
+      <source url="http://foo.bar">http://foo.bar</source>
     </item>
     </item>
     <item>
     <item>
       <title>Title Two</title>
       <title>Title Two</title>
       <link>http://example.org/two</link>
       <link>http://example.org/two</link>
       <author>two@example.org</author>
       <author>two@example.org</author>
       <description>Content two</description>
       <description>Content two</description>
+      <source url="http://foo.bar">Foo bar</source>
     </item>
     </item>
   </channel>
   </channel>
 </rss>
 </rss>
 
 
 RSS;
 RSS;
-		//debug($result);
 		$this->assertSame('application/rss+xml', $Response->type());
 		$this->assertSame('application/rss+xml', $Response->type());
 		$this->assertTextEquals($expected, $result);
 		$this->assertTextEquals($expected, $result);
 	}
 	}
@@ -120,8 +126,11 @@ RSS;
 				'sy:updateFrequency' => 1
 				'sy:updateFrequency' => 1
 			),
 			),
 			'items' => array(
 			'items' => array(
-				array('title' => 'Title One', 'link' => 'http://example.org/one', 'dc:creator' => 'Author One', 'pubDate' => $time),
-				array('title' => 'Title Two', 'link' => 'http://example.org/two', 'dc:creator' => 'Author Two', 'pubDate' => $time),
+				array('title' => 'Title One', 'link' => 'http://example.org/one',
+					'dc:creator' => 'Author One', 'pubDate' => $time),
+				array('title' => 'Title Two', 'link' => 'http://example.org/two',
+					'dc:creator' => 'Author Two', 'pubDate' => $time,
+					'source' => 'http://foo.bar'),
 			)
 			)
 		);
 		);
 		$Controller->set(array('channel' => $data, '_serialize' => 'channel'));
 		$Controller->set(array('channel' => $data, '_serialize' => 'channel'));
@@ -149,6 +158,7 @@ RSS;
       <link>http://example.org/two</link>
       <link>http://example.org/two</link>
       <dc:creator>Author Two</dc:creator>
       <dc:creator>Author Two</dc:creator>
       <pubDate>$time</pubDate>
       <pubDate>$time</pubDate>
+      <source url="http://foo.bar">http://foo.bar</source>
     </item>
     </item>
   </channel>
   </channel>
 </rss>
 </rss>
@@ -390,7 +400,147 @@ RSS;
 </rss>
 </rss>
 
 
 RSS;
 RSS;
-		//debug($result);
 		$this->assertTextEquals($expected, $result);
 		$this->assertTextEquals($expected, $result);
 	}
 	}
+
+	/**
+	 * RssViewTest::testSerializeWithCategories()
+	 *
+	 * @return void
+	 */
+	public function testSerializeWithCategories() {
+		$Request = new CakeRequest();
+		$Response = new CakeResponse();
+		$Controller = new Controller($Request, $Response);
+		$data = array(
+			'channel' => array(
+				'title' => 'Channel title',
+				'link' => 'http://channel.example.org',
+				'category' => 'IT/Internet/Web development & more',
+			),
+			'items' => array(
+				array('title' => 'Title One', 'link' => array('controller' => 'foo', 'action' => 'bar'), 'description' => 'Content one',
+					'category' => 'Internet'),
+				array('title' => 'Title Two', 'link' => array('controller' => 'foo', 'action' => 'bar'), 'description' => 'Content two',
+					'category' => array('News', 'Tutorial')),
+			)
+		);
+		$Controller->set(array('channel' => $data, '_serialize' => 'channel'));
+		$View = new RssView($Controller);
+		$result = $View->render(false);
+
+		$expected = <<<RSS
+<?xml version="1.0" encoding="UTF-8"?>
+<rss version="2.0">
+  <channel>
+    <title>Channel title</title>
+    <link>http://channel.example.org</link>
+    <category>IT/Internet/Web development &amp; more</category>
+    <description/>
+    <item>
+      <title>Title One</title>
+      <link>$this->baseUrl/foo/bar</link>
+      <description>Content one</description>
+      <category>Internet</category>
+    </item>
+    <item>
+      <title>Title Two</title>
+      <link>$this->baseUrl/foo/bar</link>
+      <description>Content two</description>
+      <category>News</category>
+      <category>Tutorial</category>
+    </item>
+  </channel>
+</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 = array(
+			'channel' => array(
+				'title' => 'Channel title',
+				'link' => 'http://channel.example.org',
+			),
+			'items' => array(
+				array('title' => 'Title One', 'link' => array('controller' => 'foo', 'action' => 'bar'), 'description' => 'Content one',
+					'enclosure' => array('url' => 'http://www.w3schools.com/media/3d.wmv', 'length' => 78645, 'type' => 'video/wmv')),
+			)
+		);
+		$Controller->set(array('channel' => $data, '_serialize' => 'channel'));
+		$View = new RssView($Controller);
+		$result = $View->render(false);
+
+		$expected = <<<RSS
+<?xml version="1.0" encoding="UTF-8"?>
+<rss version="2.0">
+  <channel>
+    <title>Channel title</title>
+    <link>http://channel.example.org</link>
+    <description/>
+    <item>
+      <title>Title One</title>
+      <link>$this->baseUrl/foo/bar</link>
+      <description>Content one</description>
+      <enclosure url="http://www.w3schools.com/media/3d.wmv" length="78645" type="video/wmv"/>
+    </item>
+  </channel>
+</rss>
+
+RSS;
+		$this->assertTextEquals($expected, $result);
+	}
+
+	/**
+	 * RssViewTest::testSerializeWithCustomTags()
+	 *
+	 * @return void
+	 */
+	public function testSerializeWithCustomTags() {
+		$Request = new CakeRequest();
+		$Response = new CakeResponse();
+		$Controller = new Controller($Request, $Response);
+		$data = array(
+			'channel' => array(
+				'title' => 'Channel title',
+				'link' => 'http://channel.example.org',
+			),
+			'items' => array(
+				array('title' => 'Title One', 'link' => array('controller' => 'foo', 'action' => 'bar'), 'description' => 'Content one',
+					'foo' => array('@url' => 'http://www.w3schools.com/media/3d.wmv', '@length' => 78645, '@type' => 'video/wmv')),
+			)
+		);
+		$Controller->set(array('channel' => $data, '_serialize' => 'channel'));
+		$View = new RssView($Controller);
+		$result = $View->render(false);
+
+		$expected = <<<RSS
+<?xml version="1.0" encoding="UTF-8"?>
+<rss version="2.0">
+  <channel>
+    <title>Channel title</title>
+    <link>http://channel.example.org</link>
+    <description/>
+    <item>
+      <title>Title One</title>
+      <link>$this->baseUrl/foo/bar</link>
+      <description>Content one</description>
+      <foo url="http://www.w3schools.com/media/3d.wmv" length="78645" type="video/wmv"/>
+    </item>
+  </channel>
+</rss>
+
+RSS;
+		$this->assertTextEquals($expected, $result);
+	}
+
 }
 }

+ 33 - 23
View/RssView.php

@@ -244,9 +244,11 @@ class RssView extends View {
 	 */
 	 */
 	protected function _prepareOutput($item) {
 	protected function _prepareOutput($item) {
 		foreach ($item as $key => $val) {
 		foreach ($item as $key => $val) {
-			// Detect namespaces
 			$prefix = null;
 			$prefix = null;
-			$bareKey = $key;
+			// The cast prevents a PHP bug for switch() and false positives with integers
+			$bareKey = (string)$key;
+
+			// Detect namespaces
 			if (strpos($key, ':') !== false) {
 			if (strpos($key, ':') !== false) {
 				list($prefix, $bareKey) = explode(':', $key, 2);
 				list($prefix, $bareKey) = explode(':', $key, 2);
 				if (strpos($prefix, '@') !== false) {
 				if (strpos($prefix, '@') !== false) {
@@ -266,24 +268,27 @@ class RssView extends View {
 				case 'pubDate':
 				case 'pubDate':
 					$val = $this->time($val);
 					$val = $this->time($val);
 					break;
 					break;
-				/*
-				case 'category' :
-					if (is_array($val) && !empty($val[0])) {
+
+				case 'category':
+					if (is_array($val) && isset($val['domain'])) {
+						$attrib['@domain'] = $val['domain'];
+						$attrib['@'] = isset($val['content']) ? $val['content'] : $attrib['@domain'];
+						$val = $attrib;
+					} elseif (is_array($val) && !empty($val[0])) {
+						$categories = array();
 						foreach ($val as $category) {
 						foreach ($val as $category) {
 							$attrib = array();
 							$attrib = array();
 							if (is_array($category) && isset($category['domain'])) {
 							if (is_array($category) && isset($category['domain'])) {
-								$attrib['domain'] = $category['domain'];
-								unset($category['domain']);
+								$attrib['@domain'] = $category['domain'];
+								$attrib['@'] = isset($val['content']) ? $val['content'] : $attrib['@domain'];
+								$category = $attrib;
 							}
 							}
-							$categories[] = $this->elem($key, $attrib, $category);
+							$categories[] = $category;
 						}
 						}
-						$elements[$key] = implode('', $categories);
-						continue 2;
-					} elseif (is_array($val) && isset($val['domain'])) {
-						$attrib['domain'] = $val['domain'];
+						$val = $categories;
 					}
 					}
 					break;
 					break;
-				*/
+
 				case 'link':
 				case 'link':
 				case 'url':
 				case 'url':
 				case 'guid':
 				case 'guid':
@@ -307,17 +312,20 @@ class RssView extends View {
 						$val = Router::url($val, true);
 						$val = Router::url($val, true);
 					}
 					}
 					break;
 					break;
+
 				case 'source':
 				case 'source':
 					if (is_array($val) && isset($val['url'])) {
 					if (is_array($val) && isset($val['url'])) {
-						$attrib['url'] = Router::url($val['url'], true);
-						$val = $val['title'];
-					} elseif (is_array($val)) {
-						$attrib['url'] = Router::url($val[0], true);
-						$val = $val[1];
+						$attrib['@url'] = Router::url($val['url'], true);
+						$attrib['@'] = isset($val['content']) ? $val['content'] : $attrib['@url'];
+					} elseif (!is_array($val)) {
+						$attrib['@url'] = Router::url($val, true);
+						$attrib['@'] = $attrib['@url'];
 					}
 					}
+					$val = $attrib;
 					break;
 					break;
+
 				case 'enclosure':
 				case 'enclosure':
-					if (is_string($val['url']) && is_file(WWW_ROOT . $val['url']) && file_exists(WWW_ROOT . $val['url'])) {
+					if (isset($val['url']) && is_string($val['url']) && is_file(WWW_ROOT . $val['url']) && file_exists(WWW_ROOT . $val['url'])) {
 						if (!isset($val['length']) && strpos($val['url'], '://') === false) {
 						if (!isset($val['length']) && strpos($val['url'], '://') === false) {
 							$val['length'] = sprintf("%u", filesize(WWW_ROOT . $val['url']));
 							$val['length'] = sprintf("%u", filesize(WWW_ROOT . $val['url']));
 						}
 						}
@@ -325,12 +333,14 @@ class RssView extends View {
 							$val['type'] = mime_content_type(WWW_ROOT . $val['url']);
 							$val['type'] = mime_content_type(WWW_ROOT . $val['url']);
 						}
 						}
 					}
 					}
-					$val['url'] = Router::url($val['url'], true);
-					$attrib = $val;
-					$val = null;
+					$attrib['@url'] = Router::url($val['url'], true);
+					$attrib['@length'] = $val['length'];
+					$attrib['@type'] = $val['type'];
+					$val = $attrib;
 					break;
 					break;
+
 				default:
 				default:
-					//$attrib = $att;
+					//nothing
 			}
 			}
 
 
 			if (is_array($val)) {
 			if (is_array($val)) {