From ca48055ddb336fc97f460b061a066c58b653dfb2 Mon Sep 17 00:00:00 2001 From: mark_story Date: Wed, 28 May 2008 02:19:55 +0000 Subject: [PATCH] Added ability to specify cdata blocks and ignore special character stripping in RssHelper. Tests updated as well. Closes #4491 git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7051 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/view/helpers/rss.php | 22 ++++- .../cases/libs/view/helpers/rss.test.php | 81 ++++++++++++++++++- 2 files changed, 97 insertions(+), 6 deletions(-) diff --git a/cake/libs/view/helpers/rss.php b/cake/libs/view/helpers/rss.php index 4cfe27477..b23b9c70b 100644 --- a/cake/libs/view/helpers/rss.php +++ b/cake/libs/view/helpers/rss.php @@ -32,7 +32,8 @@ * @package cake * @subpackage cake.cake.libs.view.helpers */ -uses('view' . DS . 'helpers' . DS . 'xml'); +App::import('Helper', 'Xml'); +//uses('view' . DS . 'helpers' . DS . 'xml'); class RssHelper extends XmlHelper { @@ -186,6 +187,9 @@ class RssHelper extends XmlHelper { foreach ($elements as $key => $val) { $attrib = array(); + $cdata = false; + $strip = true; + switch ($key) { case 'pubDate' : $val = $this->time($val); @@ -223,9 +227,23 @@ class RssHelper extends XmlHelper { $val = null; break; } - if ($val != null) { + + if (is_array($val)) { + $_keys = array('cdata', 'strip'); + foreach($_keys as $index) { + if (isset($val[$index])){ + $$index = $val[$index]; + unset($val[$index]); + } + } + $val = $val['value']; + } + if ($val != null && $strip) { $val = h($val); } + if ($cdata) { + $val = ''; + } $elements[$key] = $this->elem($key, $attrib, $val); } diff --git a/cake/tests/cases/libs/view/helpers/rss.test.php b/cake/tests/cases/libs/view/helpers/rss.test.php index e25764324..47f6729cb 100644 --- a/cake/tests/cases/libs/view/helpers/rss.test.php +++ b/cake/tests/cases/libs/view/helpers/rss.test.php @@ -29,7 +29,7 @@ if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) { define('CAKEPHP_UNIT_TEST_EXECUTION', 1); } -uses('view'.DS.'helpers'.DS.'app_helper', 'controller'.DS.'controller', 'model'.DS.'model', 'view'.DS.'helper', 'view'.DS.'helpers'.DS.'rss'); +App::import('Helper', array('Rss', 'Time')); /** * Short description for class. @@ -37,10 +37,11 @@ uses('view'.DS.'helpers'.DS.'app_helper', 'controller'.DS.'controller', 'model'. * @package cake.tests * @subpackage cake.tests.cases.libs.view.helpers */ -class RssTest extends UnitTestCase { +class RssTest extends CakeTestCase { function setUp() { $this->Rss =& new RssHelper(); + $this->Rss->Time = new TimeHelper(); } function tearDown() { @@ -121,6 +122,73 @@ class RssTest extends UnitTestCase { $result = $this->Rss->item(null, array("title"=>"My title","description"=>"My description","link"=>"http://www.google.com/")); $expecting = 'My titleMy descriptionhttp://www.google.com/http://www.google.com/'; $this->assertEqual($result, $expecting); + + $item = array( + 'title' => array( + 'value' => 'My Title', + 'cdata' => true, + ), + 'link' => 'http://www.example.com/1', + 'description' => array( + 'value' => 'descriptive words', + 'cdata' => true, + ), + 'pubDate' => '2008-05-31 12:00:00', + 'guid' => 'http://www.example.com/1' + ); + $result = $this->Rss->item(null, $item); + $expected = array( + 'assertTags($result, $expected); + + $item = array( + 'title' => array( + 'value' => 'My Title & more', + 'cdata' => true + ) + ); + $result = $this->Rss->item(null, $item); + $expected = array( + 'assertTags($result, $expected); + + $item = array( + 'title' => array( + 'value' => 'My Title & more', + 'strip' => false + ) + ); + $result = $this->Rss->item(null, $item); + $expected = array( + 'assertTags($result, $expected); } function testTime() { @@ -128,10 +196,15 @@ class RssTest extends UnitTestCase { function testElementAttrNotInParent() { $attributes = array('title' => 'Some Title', 'link' => 'http://link.com', 'description' => 'description'); - $elements = array('enclosure' => array('url' => 'http://somewhere.com')); + $elements = array('enclosure' => array('url' => 'http://test.com')); $result = $this->Rss->item($attributes, $elements); - $this->assertPattern('/^<\/item>$/', $result); + $expected = array( + 'item' => array('title' => 'Some Title', 'link' => 'http://link.com', 'description' => 'description'), + 'enclosure' => array('url' => 'http://test.com'), + '/item' + ); + $this->assertTags($result, $expected); } } ?> \ No newline at end of file