Fixing issues with RssHelper and updating tests to reflect changes in how Xml::build() differs from previous versions' Xml.

This commit is contained in:
mark_story 2010-10-23 13:11:50 -04:00
parent 3e2d09a802
commit bf7b8b0e93
2 changed files with 58 additions and 66 deletions

View file

@ -315,8 +315,9 @@ class RssHelper extends AppHelper {
if (!empty($namespace)) { if (!empty($namespace)) {
$xml .= ' xmlns:"' . $namespace . '"'; $xml .= ' xmlns:"' . $namespace . '"';
} }
$bareName = $name;
if (strpos($name, ':') !== false) { if (strpos($name, ':') !== false) {
list($prefix, ) = explode(':', $name, 2); list($prefix, $bareName) = explode(':', $name, 2);
switch ($prefix) { switch ($prefix) {
case 'atom': case 'atom':
$xml .= ' xmlns:atom="http://www.w3.org/2005/Atom"'; $xml .= ' xmlns:atom="http://www.w3.org/2005/Atom"';
@ -327,15 +328,17 @@ class RssHelper extends AppHelper {
$content = '<![CDATA[' . $content . ']]>'; $content = '<![CDATA[' . $content . ']]>';
} }
$xml .= '>' . $content . '</' . $name. '>'; $xml .= '>' . $content . '</' . $name. '>';
$elem = Xml::build($xml); $elem = Xml::build($xml, array('return' => 'domdocument'));
$nodes = $elem->getElementsByTagName($bareName);
foreach ($attrib as $key => $value) { foreach ($attrib as $key => $value) {
$elem->addAttribute($key, $value); $nodes->item(0)->setAttribute($key, $value);
} }
foreach ($children as $child) { foreach ($children as $k => $child) {
$elem->addChild($child); $child = $elem->createElement($name, $child);
$nodes->item(0)->appendChild($child);
} }
$xml = $elem->asXML(); $xml = $elem->saveXml();
$xml = trim(substr($xml, strpos($xml, '?>') + 2)); $xml = trim(substr($xml, strpos($xml, '?>') + 2));
return $xml; return $xml;
} }

View file

@ -35,7 +35,7 @@ class RssHelperTest extends CakeTestCase {
* @return void * @return void
*/ */
function setUp() { function setUp() {
$controller = null; parent::setUp();
$this->View = new View($controller); $this->View = new View($controller);
$this->Rss = new RssHelper($this->View); $this->Rss = new RssHelper($this->View);
} }
@ -47,6 +47,7 @@ class RssHelperTest extends CakeTestCase {
* @return void * @return void
*/ */
function tearDown() { function tearDown() {
parent::tearDown();
unset($this->Rss); unset($this->Rss);
} }
@ -65,15 +66,6 @@ class RssHelperTest extends CakeTestCase {
); );
$this->assertTags($result, $expected); $this->assertTags($result, $expected);
$result = $this->Rss->document(array('contrived' => 'parameter'));
$expected = array(
'rss' => array(
'version' => '2.0'
),
'<parameter'
);
$this->assertTags($result, $expected);
$result = $this->Rss->document(null, 'content'); $result = $this->Rss->document(null, 'content');
$expected = array( $expected = array(
'rss' => array( 'rss' => array(
@ -173,6 +165,7 @@ class RssHelperTest extends CakeTestCase {
); );
$this->assertTags($result, $expected); $this->assertTags($result, $expected);
} }
function testChannelElementAttributes() { function testChannelElementAttributes() {
$attrib = array(); $attrib = array();
$elements = array( $elements = array(
@ -288,15 +281,9 @@ class RssHelperTest extends CakeTestCase {
$this->assertTags($result, $expected); $this->assertTags($result, $expected);
$item = array( $item = array(
'title' => array( 'title' => 'My Title',
'value' => 'My Title',
'cdata' => true,
),
'link' => 'http://www.example.com/1', 'link' => 'http://www.example.com/1',
'description' => array( 'description' => 'descriptive words',
'value' => 'descriptive words',
'cdata' => true,
),
'pubDate' => '2008-05-31 12:00:00', 'pubDate' => '2008-05-31 12:00:00',
'guid' => 'http://www.example.com/1' 'guid' => 'http://www.example.com/1'
); );
@ -305,13 +292,13 @@ class RssHelperTest extends CakeTestCase {
$expected = array( $expected = array(
'<item', '<item',
'<title', '<title',
'<![CDATA[My Title]]', 'My Title',
'/title', '/title',
'<link', '<link',
'http://www.example.com/1', 'http://www.example.com/1',
'/link', '/link',
'<description', '<description',
'<![CDATA[descriptive words]]', 'descriptive words',
'/description', '/description',
'<pubDate', '<pubDate',
date('r', strtotime('2008-05-31 12:00:00')), date('r', strtotime('2008-05-31 12:00:00')),
@ -324,21 +311,57 @@ class RssHelperTest extends CakeTestCase {
$this->assertTags($result, $expected); $this->assertTags($result, $expected);
$item = array( $item = array(
'title' => array( 'title' => 'My Title & more'
'value' => 'My Title & more', );
'cdata' => true $result = $this->Rss->item(null, $item);
) $expected = array(
'<item',
'<title', 'My Title &amp; more', '/title',
'/item'
);
$this->assertTags($result, $expected);
$item = array(
'title' => 'Foo bar',
'link' => array(
'url' => 'http://example.com/foo?a=1&b=2',
'convertEntities' => false
),
'description' => array(
'value' => 'descriptive words',
'cdata' => true,
),
'pubDate' => '2008-05-31 12:00:00'
); );
$result = $this->Rss->item(null, $item); $result = $this->Rss->item(null, $item);
$expected = array( $expected = array(
'<item', '<item',
'<title', '<title',
'<![CDATA[My Title &amp; more]]', 'Foo bar',
'/title', '/title',
'<link',
'http://example.com/foo?a=1&amp;b=2',
'/link',
'<description',
'<![CDATA[descriptive words]]',
'/description',
'<pubDate',
date('r', strtotime('2008-05-31 12:00:00')),
'/pubDate',
'<guid',
'http://example.com/foo?a=1&amp;b=2',
'/guid',
'/item' '/item'
); );
$this->assertTags($result, $expected); $this->assertTags($result, $expected);
}
/**
* test item() with cdata blocks.
*
* @return void
*/
function testItemCdata() {
$item = array( $item = array(
'title' => array( 'title' => array(
'value' => 'My Title & more', 'value' => 'My Title & more',
@ -360,7 +383,7 @@ class RssHelperTest extends CakeTestCase {
'category' => array( 'category' => array(
'value' => 'CakePHP', 'value' => 'CakePHP',
'cdata' => true, 'cdata' => true,
'domain' => 'http://www.cakephp.org' 'domain' => 'http://www.cakephp.org',
) )
); );
$result = $this->Rss->item(null, $item); $result = $this->Rss->item(null, $item);
@ -454,40 +477,6 @@ class RssHelperTest extends CakeTestCase {
'/item' '/item'
); );
$this->assertTags($result, $expected); $this->assertTags($result, $expected);
$item = array(
'title' => 'Foo bar',
'link' => array(
'url' => 'http://example.com/foo?a=1&b=2',
'convertEntities' => false
),
'description' => array(
'value' => 'descriptive words',
'cdata' => true,
),
'pubDate' => '2008-05-31 12:00:00'
);
$result = $this->Rss->item(null, $item);
$expected = array(
'<item',
'<title',
'Foo bar',
'/title',
'<link',
'http://example.com/foo?a=1&amp;b=2',
'/link',
'<description',
'<![CDATA[descriptive words]]',
'/description',
'<pubDate',
date('r', strtotime('2008-05-31 12:00:00')),
'/pubDate',
'<guid',
'http://example.com/foo?a=1&amp;b=2',
'/guid',
'/item'
);
$this->assertTags($result, $expected);
} }
/** /**