Skip to content

Commit

Permalink
Fixing issues with RssHelper and updating tests to reflect changes in…
Browse files Browse the repository at this point in the history
… how Xml::build() differs from previous versions' Xml.
  • Loading branch information
markstory committed Oct 23, 2010
1 parent 3e2d09a commit bf7b8b0
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 66 deletions.
15 changes: 9 additions & 6 deletions cake/libs/view/helpers/rss.php
Expand Up @@ -315,8 +315,9 @@ function elem($name, $attrib = array(), $content = null, $endTag = true) {
if (!empty($namespace)) {
$xml .= ' xmlns:"' . $namespace . '"';
}
$bareName = $name;
if (strpos($name, ':') !== false) {
list($prefix, ) = explode(':', $name, 2);
list($prefix, $bareName) = explode(':', $name, 2);
switch ($prefix) {
case 'atom':
$xml .= ' xmlns:atom="http://www.w3.org/2005/Atom"';
Expand All @@ -327,15 +328,17 @@ function elem($name, $attrib = array(), $content = null, $endTag = true) {
$content = '<![CDATA[' . $content . ']]>';
}
$xml .= '>' . $content . '</' . $name. '>';
$elem = Xml::build($xml);
$elem = Xml::build($xml, array('return' => 'domdocument'));
$nodes = $elem->getElementsByTagName($bareName);
foreach ($attrib as $key => $value) {
$elem->addAttribute($key, $value);
$nodes->item(0)->setAttribute($key, $value);
}
foreach ($children as $child) {
$elem->addChild($child);
foreach ($children as $k => $child) {
$child = $elem->createElement($name, $child);
$nodes->item(0)->appendChild($child);
}

$xml = $elem->asXML();
$xml = $elem->saveXml();
$xml = trim(substr($xml, strpos($xml, '?>') + 2));
return $xml;
}
Expand Down
109 changes: 49 additions & 60 deletions cake/tests/cases/libs/view/helpers/rss.test.php
Expand Up @@ -35,7 +35,7 @@ class RssHelperTest extends CakeTestCase {
* @return void
*/
function setUp() {
$controller = null;
parent::setUp();
$this->View = new View($controller);
$this->Rss = new RssHelper($this->View);
}
Expand All @@ -47,6 +47,7 @@ function setUp() {
* @return void
*/
function tearDown() {
parent::tearDown();
unset($this->Rss);
}

Expand All @@ -65,15 +66,6 @@ function testDocument() {
);
$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');
$expected = array(
'rss' => array(
Expand Down Expand Up @@ -173,6 +165,7 @@ function testChannelElements() {
);
$this->assertTags($result, $expected);
}

function testChannelElementAttributes() {
$attrib = array();
$elements = array(
Expand Down Expand Up @@ -288,15 +281,9 @@ function testItem() {
$this->assertTags($result, $expected);

$item = array(
'title' => array(
'value' => 'My Title',
'cdata' => true,
),
'title' => 'My Title',
'link' => 'http://www.example.com/1',
'description' => array(
'value' => 'descriptive words',
'cdata' => true,
),
'description' => 'descriptive words',
'pubDate' => '2008-05-31 12:00:00',
'guid' => 'http://www.example.com/1'
);
Expand All @@ -305,13 +292,13 @@ function testItem() {
$expected = array(
'<item',
'<title',
'<![CDATA[My Title]]',
'My Title',
'/title',
'<link',
'http://www.example.com/1',
'/link',
'<description',
'<![CDATA[descriptive words]]',
'descriptive words',
'/description',
'<pubDate',
date('r', strtotime('2008-05-31 12:00:00')),
Expand All @@ -324,21 +311,57 @@ function testItem() {
$this->assertTags($result, $expected);

$item = array(
'title' => array(
'value' => 'My Title & more',
'cdata' => true
)
'title' => 'My Title & more'
);
$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);
$expected = array(
'<item',
'<title',
'<![CDATA[My Title &amp; more]]',
'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);
}

/**
* test item() with cdata blocks.
*
* @return void
*/
function testItemCdata() {
$item = array(
'title' => array(
'value' => 'My Title & more',
Expand All @@ -360,7 +383,7 @@ function testItem() {
'category' => array(
'value' => 'CakePHP',
'cdata' => true,
'domain' => 'http://www.cakephp.org'
'domain' => 'http://www.cakephp.org',
)
);
$result = $this->Rss->item(null, $item);
Expand Down Expand Up @@ -454,40 +477,6 @@ function testItem() {
'/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);
$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);
}

/**
Expand Down

0 comments on commit bf7b8b0

Please sign in to comment.