diff --git a/cake/libs/view/helpers/rss.php b/cake/libs/view/helpers/rss.php index 31effa21a..a9c893556 100644 --- a/cake/libs/view/helpers/rss.php +++ b/cake/libs/view/helpers/rss.php @@ -17,18 +17,16 @@ * @since CakePHP(tm) v 1.2 * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ -App::import('Helper', 'Xml'); +App::import('Core', 'Xml'); /** - * XML Helper class for easy output of XML structures. - * - * XmlHelper encloses all methods needed while working with XML documents. + * RSS Helper class for easy output RSS structures. * * @package cake * @subpackage cake.cake.libs.view.helpers * @link http://book.cakephp.org/view/1460/RSS */ -class RssHelper extends XmlHelper { +class RssHelper extends AppHelper { /** * Helpers used by RSS Helper @@ -270,7 +268,7 @@ class RssHelper extends XmlHelper { if (!empty($elements)) { $content = implode('', $elements); } - return $this->elem('item', $att, $content, !($content === null)); + return $this->elem('item', (array)$att, $content, !($content === null)); } /** @@ -283,4 +281,62 @@ class RssHelper extends XmlHelper { function time($time) { return $this->Time->toRSS($time); } + +/** + * Generates an XML element + * + * @param string $name The name of the XML element + * @param array $attrib The attributes of the XML element + * @param mixed $content XML element content + * @param boolean $endTag Whether the end tag of the element should be printed + * @return string XML + */ + function elem($name, $attrib = array(), $content = null, $endTag = true) { + $namespace = null; + if (isset($attrib['namespace'])) { + $namespace = $attrib['namespace']; + unset($attrib['namespace']); + } + $cdata = false; + if (is_array($content) && isset($content['cdata'])) { + $cdata = true; + unset($content['cdata']); + } + if (is_array($content) && array_key_exists('value', $content)) { + $content = $content['value']; + } + $children = array(); + if (is_array($content)) { + $children = $content; + $content = null; + } + + $xml = '<' . $name; + if (!empty($namespace)) { + $xml .= ' xmlns:"' . $namespace . '"'; + } + if (strpos($name, ':') !== false) { + list($prefix, ) = explode(':', $name, 2); + switch ($prefix) { + case 'atom': + $xml .= ' xmlns:atom="http://www.w3.org/2005/Atom"'; + break; + } + } + if ($cdata && !empty($content)) { + $content = ''; + } + $xml .= '>' . $content . ''; + $elem = Xml::build($xml); + foreach ($attrib as $key => $value) { + $elem->addAttribute($key, $value); + } + foreach ($children as $child) { + $elem->addChild($child); + } + + $xml = $elem->asXML(); + $xml = trim(substr($xml, strpos($xml, '?>') + 2)); + return $xml; + } } diff --git a/cake/tests/cases/libs/view/helpers/rss.test.php b/cake/tests/cases/libs/view/helpers/rss.test.php index 2b083c5ab..ed554db8e 100644 --- a/cake/tests/cases/libs/view/helpers/rss.test.php +++ b/cake/tests/cases/libs/view/helpers/rss.test.php @@ -37,9 +37,6 @@ class RssHelperTest extends CakeTestCase { $this->Rss =& new RssHelper(); $this->Rss->Time =& new TimeHelper(); $this->Rss->beforeRender(); - - $manager =& XmlManager::getInstance(); - $manager->namespaces = array(); } /** @@ -53,52 +50,6 @@ class RssHelperTest extends CakeTestCase { } /** - * testAddNamespace method - * - * @access public - * @return void - */ - function testAddNamespace() { - $this->Rss->addNs('custom', 'http://example.com/dtd.xml'); - $manager =& XmlManager::getInstance(); - - $expected = array('custom' => 'http://example.com/dtd.xml'); - $this->assertEqual($manager->namespaces, $expected); - - $this->Rss->removeNs('custom'); - - $this->Rss->addNs('dummy', 'http://dummy.com/1.0/'); - $result = $this->Rss->document(); - $expected = array( - 'rss' => array( - 'xmlns:dummy' => 'http://dummy.com/1.0/', - 'version' => '2.0' - ) - ); - $this->assertTags($result, $expected); - - $this->Rss->removeNs('dummy'); - } - -/** - * testRemoveNamespace method - * - * @access public - * @return void - */ - function testRemoveNamespace() { - $this->Rss->addNs('custom', 'http://example.com/dtd.xml'); - $this->Rss->addNs('custom2', 'http://example.com/dtd2.xml'); - $manager =& XmlManager::getInstance(); - - $expected = array('custom' => 'http://example.com/dtd.xml', 'custom2' => 'http://example.com/dtd2.xml'); - $this->assertEqual($manager->namespaces, $expected); - - $this->Rss->removeNs('custom'); - $expected = array('custom2' => 'http://example.com/dtd2.xml'); - $this->assertEqual($manager->namespaces, $expected); - } - /** * testDocument method * * @access public @@ -252,6 +203,7 @@ class RssHelperTest extends CakeTestCase { ' array( + 'xmlns:atom' => 'http://www.w3.org/2005/Atom', 'href' => "http://www.example.com/rss.xml", 'rel' => "self", 'type' =>"application/rss+xml" @@ -386,22 +338,6 @@ class RssHelperTest extends CakeTestCase { ); $this->assertTags($result, $expected); - $item = array( - 'title' => array( - 'value' => 'My Title & more', - 'convertEntities' => false - ) - ); - $result = $this->Rss->item(null, $item); - $expected = array( - 'assertTags($result, $expected); - $item = array( 'title' => array( 'value' => 'My Title & more',