Adding test for RssHelper.

Added syntax for adding attributes to channel child elements
Fixed bug in item method that added bogus attributes
Added default description to channel method


git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5670 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
phpnut 2007-09-18 18:00:22 +00:00
parent bd3e702d77
commit b1d79fb0d3
2 changed files with 74 additions and 12 deletions

View file

@ -131,12 +131,20 @@ class RssHelper extends XmlHelper {
}
if (!isset($elements['link'])) {
$elements['link'] = '/';
}
}
if (!isset($elements['description'])) {
$elements['description'] = '';
}
$elements['link'] = $this->url($elements['link'], true);
$elems = '';
foreach ($elements as $elem => $data) {
$elems .= $this->elem($elem, array(), $data);
$attributes = array();
if (is_array($data) && isset($data['attrib']) && is_array($data['attrib'])) {
$attributes = $data['attrib'];
unset($data['attrib']);
}
$elems .= $this->elem($elem, $attributes, $data);
}
return $this->elem('channel', $attrib, $elems . $this->__composeContent($content), !($content === null));
}
@ -169,7 +177,7 @@ class RssHelper extends XmlHelper {
* @param array $elements The list of elements contained in this <item />
* @return string An RSS <item /> element
*/
function item($attrib = array(), $elements = array()) {
function item($att = array(), $elements = array()) {
$content = null;
foreach ($elements as $key => $val) {
@ -210,7 +218,7 @@ class RssHelper extends XmlHelper {
$attrib = $val;
$val = null;
break;
}
}
if ($val != null) {
$val = h($val);
}
@ -224,8 +232,7 @@ class RssHelper extends XmlHelper {
if (!empty($elements)) {
$content = join('', $elements);
}
return $this->output($this->elem('item', $attrib, $content, !($content === null)));
return $this->output($this->elem('item', $att, $content, !($content === null)));
}
/**
* Converts a time in any format to an RSS time
@ -238,5 +245,4 @@ class RssHelper extends XmlHelper {
return $this->Time->toRSS($time);
}
}
?>

View file

@ -41,10 +41,6 @@ uses('controller'.DS.'controller', 'model'.DS.'model', 'view'.DS.'helper', 'view
*/
class RssTest extends UnitTestCase {
function skip() {
$this->skipif (true, 'RssHelper test not implemented');
}
function setUp() {
$this->Rss = new RssHelper();
}
@ -52,6 +48,66 @@ class RssTest extends UnitTestCase {
function tearDown() {
unset($this->Rss);
}
}
function testDocument() {
$res = $this->Rss->document();
$this->assertPattern('/^<rss version="2.0" \/>$/', $res);
$res = $this->Rss->document(array('contrived' => 'parameter'));
$this->assertPattern('/^<rss version="2.0"><\/rss>$/', $res);
$res = $this->Rss->document(null, 'content');
$this->assertPattern('/^<rss version="2.0">content<\/rss>$/', $res);
$res = $this->Rss->document(array('contrived' => 'parameter'), 'content');
$this->assertPattern('/^<rss[^<>]+version="2.0"[^<>]*>/', $res);
$this->assertPattern('/<rss[^<>]+contrived="parameter"[^<>]*>/', $res);
$this->assertNoPattern('/<rss[^<>]+[^version|contrived]=[^<>]*>/', $res);
}
function testChannel() {
$attrib = array('a' => '1', 'b' => '2');
$elements['title'] = 'title';
$content = 'content';
$res = $this->Rss->channel($attrib, $elements, $content);
$this->assertPattern('/^<channel[^<>]+a="1"[^<>]*>/', $res);
$this->assertPattern('/^<channel[^<>]+b="2"[^<>]*>/', $res);
$this->assertNoPattern('/^<channel[^<>]+[^a|b]=[^<>]*/', $res);
$this->assertPattern('/<title>title<\/title>/', $res);
$this->assertPattern('/<link>'.str_replace('/', '\/', RssHelper::url('/', true)).'<\/link>/', $res);
$this->assertPattern('/<description \/>/', $res);
$this->assertPattern('/content<\/channel>$/', $res);
}
function testChannelElementLevelAttrib() {
$attrib = array();
$elements['title'] = 'title';
$elements['image'] = array('myImage', 'attrib' => array('href' => 'http://localhost'));
$content = 'content';
$res = $this->Rss->channel($attrib, $elements, $content);
$this->assertPattern('/^<channel>/', $res);
$this->assertPattern('/<title>title<\/title>/', $res);
$this->assertPattern('/<image[^<>]+href="http:\/\/localhost">myImage<\/image>/', $res);
$this->assertPattern('/<link>'.str_replace('/', '\/', RssHelper::url('/', true)).'<\/link>/', $res);
$this->assertPattern('/<description \/>/', $res);
$this->assertPattern('/content<\/channel>$/', $res);
}
function testItems() {
}
function testItem() {
}
function testTime() {
}
function testElementAttrNotInParent() {
$attributes = array('title' => 'Some Title', 'link' => 'http://link.com', 'description' => 'description');
$elements = array('enclosure' => array('url' => 'http://somewhere.com'));
$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);
}
}
?>