浏览代码

Throw exception for undefined namespaces

euromark 12 年之前
父节点
当前提交
392f01ec09
共有 2 个文件被更改,包括 31 次插入6 次删除
  1. 25 1
      Test/Case/View/RssViewTest.php
  2. 6 5
      View/RssView.php

+ 25 - 1
Test/Case/View/RssViewTest.php

@@ -35,7 +35,7 @@ class RssViewTest extends CakeTestCase {
 
 		$this->Rss = new RssView();
 
-		$this->baseUrl = $this->baseUrl = php_sapi_name() == 'cli' ? 'http://localhost' : HTTP_BASE;
+		$this->baseUrl = php_sapi_name() === 'cli' ? 'http://localhost' : HTTP_BASE;
 	}
 
 	/**
@@ -160,6 +160,30 @@ RSS;
 	}
 
 	/**
+	 * RssViewTest::testSerializeWithUnconfiguredPrefix()
+	 *
+	 * @expectedException RuntimeException
+	 * @return void
+	 */
+	public function testSerializeWithUnconfiguredPrefix() {
+		$Request = new CakeRequest();
+		$Response = new CakeResponse();
+		$Controller = new Controller($Request, $Response);
+
+		$data = array(
+			'channel' => array(
+				'foo:bar' => 'something',
+			),
+			'items' => array(
+				array('title' => 'Title Two'),
+			)
+		);
+		$Controller->set(array('channel' => $data, '_serialize' => 'channel'));
+		$View = new RssView($Controller);
+		$result = $View->render(false);
+	}
+
+	/**
 	 * RssViewTest::testSerializeWithArrayLinks()
 	 *
 	 * `'atom:link' => array('@href' => array(...)` becomes

+ 6 - 5
View/RssView.php

@@ -114,12 +114,10 @@ class RssView extends View {
 	}
 
 	/**
-	 * Converts an array into an `<item />` element and its contents
+	 * Prepares the channel and sets default values.
 	 *
-	 * @param array $att The attributes of the `<item />` element
-	 * @param array $elements The list of elements contained in this `<item />`
-	 * @return string An RSS `<item />` element
-	 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/rss.html#RssHelper::item
+	 * @param array $channel
+	 * @return array Channel
 	 */
 	public function channel($channel) {
 		if (!isset($channel['title']) && !empty($this->pageTitle)) {
@@ -224,6 +222,9 @@ class RssView extends View {
 		);
 		$namespaces = array();
 		foreach ($this->_usedNamespaces as $usedNamespacePrefix) {
+			if (!isset($this->_namespaces[$usedNamespacePrefix])) {
+				throw new RuntimeException(__('The prefix %s is not specified.', $usedNamespacePrefix));
+			}
 			$namespaces['xmlns:' . $usedNamespacePrefix] = $this->_namespaces[$usedNamespacePrefix];
 		}
 		$array['rss'] += $namespaces;