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
This commit is contained in:
mark_story 2008-05-28 02:19:55 +00:00
parent 027a90fab3
commit ca48055ddb
2 changed files with 97 additions and 6 deletions

View file

@ -32,7 +32,8 @@
* @package cake * @package cake
* @subpackage cake.cake.libs.view.helpers * @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 { class RssHelper extends XmlHelper {
@ -186,6 +187,9 @@ class RssHelper extends XmlHelper {
foreach ($elements as $key => $val) { foreach ($elements as $key => $val) {
$attrib = array(); $attrib = array();
$cdata = false;
$strip = true;
switch ($key) { switch ($key) {
case 'pubDate' : case 'pubDate' :
$val = $this->time($val); $val = $this->time($val);
@ -223,9 +227,23 @@ class RssHelper extends XmlHelper {
$val = null; $val = null;
break; 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); $val = h($val);
} }
if ($cdata) {
$val = '<![CDATA['. $val. ']]>';
}
$elements[$key] = $this->elem($key, $attrib, $val); $elements[$key] = $this->elem($key, $attrib, $val);
} }

View file

@ -29,7 +29,7 @@
if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) { if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) {
define('CAKEPHP_UNIT_TEST_EXECUTION', 1); 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. * Short description for class.
@ -37,10 +37,11 @@ uses('view'.DS.'helpers'.DS.'app_helper', 'controller'.DS.'controller', 'model'.
* @package cake.tests * @package cake.tests
* @subpackage cake.tests.cases.libs.view.helpers * @subpackage cake.tests.cases.libs.view.helpers
*/ */
class RssTest extends UnitTestCase { class RssTest extends CakeTestCase {
function setUp() { function setUp() {
$this->Rss =& new RssHelper(); $this->Rss =& new RssHelper();
$this->Rss->Time = new TimeHelper();
} }
function tearDown() { 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/")); $result = $this->Rss->item(null, array("title"=>"My title","description"=>"My description","link"=>"http://www.google.com/"));
$expecting = '<item><title>My title</title><description>My description</description><link>http://www.google.com/</link><guid>http://www.google.com/</guid></item>'; $expecting = '<item><title>My title</title><description>My description</description><link>http://www.google.com/</link><guid>http://www.google.com/</guid></item>';
$this->assertEqual($result, $expecting); $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(
'<item',
'<title',
'<![CDATA[My Title]]',
'/title',
'<link',
'http://www.example.com/1',
'/link',
'<description',
'<![CDATA[descriptive words]]',
'/description',
'<pubDate',
'Sat, 31 May 2008 12:00:00 -0400',
'/pubDate',
'<guid',
'http://www.example.com/1',
'/guid',
'/item'
);
$this->assertTags($result, $expected);
$item = array(
'title' => array(
'value' => 'My Title & more',
'cdata' => true
)
);
$result = $this->Rss->item(null, $item);
$expected = array(
'<item',
'<title',
'<![CDATA[My Title &amp; more]]',
'/title',
'/item'
);
$this->assertTags($result, $expected);
$item = array(
'title' => array(
'value' => 'My Title & more',
'strip' => false
)
);
$result = $this->Rss->item(null, $item);
$expected = array(
'<item',
'<title',
'My Title & more',
'/title',
'/item'
);
$this->assertTags($result, $expected);
} }
function testTime() { function testTime() {
@ -128,10 +196,15 @@ class RssTest extends UnitTestCase {
function testElementAttrNotInParent() { function testElementAttrNotInParent() {
$attributes = array('title' => 'Some Title', 'link' => 'http://link.com', 'description' => 'description'); $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); $result = $this->Rss->item($attributes, $elements);
$this->assertPattern('/^<item title="Some Title" link="http:\/\/link.com" description="description"><enclosure url="http:\/\/somewhere.com" \/><\/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);
} }
} }
?> ?>