Skip to content

Commit

Permalink
Merge pull request #198 from jellehenkens/2.0-rss-helper-tests
Browse files Browse the repository at this point in the history
Added tests for the RssHelper for code that was not covered
  • Loading branch information
markstory committed Sep 13, 2011
2 parents 97a975c + 236200c commit 0e9b3dd
Showing 1 changed file with 187 additions and 1 deletion.
188 changes: 187 additions & 1 deletion lib/Cake/Test/Case/View/Helper/RssHelperTest.php
Expand Up @@ -19,6 +19,7 @@
App::uses('View', 'View');
App::uses('RssHelper', 'View/Helper');
App::uses('TimeHelper', 'View/Helper');
App::uses('File', 'Utility');

/**
* RssHelperTest class
Expand Down Expand Up @@ -110,6 +111,29 @@ public function testChannel() {
'/channel'
);
$this->assertTags($result, $expected);

$this->View->pageTitle = 'title';
$attrib = array('a' => '1', 'b' => '2');
$elements = array();
$content = 'content';

$result = $this->Rss->channel($attrib, $elements, $content);
$expected = array(
'channel' => array(
'a' => '1',
'b' => '2'
),
'<title',
'title',
'/title',
'<link',
$this->Rss->url('/', true),
'/link',
'<description',
'content',
'/channel'
);
$this->assertTags($result, $expected);
}

/**
Expand Down Expand Up @@ -238,6 +262,35 @@ public function testItems() {
);
$this->assertTags($result, $expected);

$items = array(
array('title' => 'title1', 'guid' => 'http://www.example.com/guid1', 'link' => 'http://www.example.com/link1', 'description' => 'description1'),
array('title' => 'title2', 'guid' => 'http://www.example.com/guid2', 'link' => 'http://www.example.com/link2', 'description' => 'description2'),
array('title' => 'title3', 'guid' => 'http://www.example.com/guid3', 'link' => 'http://www.example.com/link3', 'description' => 'description3')
);

$result = $this->Rss->items($items, create_function('$v', '$v[\'title\'] = $v[\'title\'] . \'-transformed\'; return $v;'));
$expected = array(
'<item',
'<title', 'title1-transformed', '/title',
'<guid', 'http://www.example.com/guid1', '/guid',
'<link', 'http://www.example.com/link1', '/link',
'<description', 'description1', '/description',
'/item',
'<item',
'<title', 'title2-transformed', '/title',
'<guid', 'http://www.example.com/guid2', '/guid',
'<link', 'http://www.example.com/link2', '/link',
'<description', 'description2', '/description',
'/item',
'<item',
'<title', 'title3-transformed', '/title',
'<guid', 'http://www.example.com/guid3', '/guid',
'<link', 'http://www.example.com/link3', '/link',
'<description', 'description3', '/description',
'/item'
);
$this->assertTags($result, $expected);

$result = $this->Rss->items(array());
$expected = '';
$this->assertEqual($expected, $result);
Expand Down Expand Up @@ -278,6 +331,7 @@ public function testItem() {
'link' => 'http://www.example.com/1',
'description' => 'descriptive words',
'pubDate' => '2008-05-31 12:00:00',
'source' => array('http://www.google.com/', 'Google'),
'guid' => 'http://www.example.com/1'
);
$result = $this->Rss->item(null, $item);
Expand All @@ -296,6 +350,9 @@ public function testItem() {
'<pubDate',
date('r', strtotime('2008-05-31 12:00:00')),
'/pubDate',
'source' => array('url' => 'http://www.google.com/'),
'Google',
'/source',
'<guid',
'http://www.example.com/1',
'/guid',
Expand Down Expand Up @@ -324,7 +381,8 @@ public function testItem() {
'value' => 'descriptive words',
'cdata' => true,
),
'pubDate' => '2008-05-31 12:00:00'
'pubDate' => '2008-05-31 12:00:00',
'source' => 'http://www.google.com/'
);
$result = $this->Rss->item(null, $item);
$expected = array(
Expand All @@ -341,12 +399,43 @@ public function testItem() {
'<pubDate',
date('r', strtotime('2008-05-31 12:00:00')),
'/pubDate',
'<source',
'http://www.google.com/',
'/source',
'<guid',
'http://example.com/foo?a=1&amp;b=2',
'/guid',
'/item'
);
$this->assertTags($result, $expected);

$item = array(
'title' => 'My title',
'description' => 'My description',
'link' => 'http://www.google.com/',
'source' => array('url' => 'http://www.example.com/', 'title' => 'Example website')
);
$result = $this->Rss->item(null, $item);
$expected = array(
'<item',
'<title',
'My title',
'/title',
'<description',
'My description',
'/description',
'<link',
'http://www.google.com/',
'/link',
'source' => array('url' => 'http://www.example.com/'),
'Example website',
'/source',
'<guid',
'http://www.google.com/',
'/guid',
'/item'
);
$this->assertTags($result, $expected);
}

/**
Expand Down Expand Up @@ -472,6 +561,85 @@ public function testItemCdata() {
$this->assertTags($result, $expected);
}

/**
* test item() with enclosure data.
*
* @return void
*/
public function testItemEnclosureLength() {
$tmpFile = $this->_getWwwTmpFile();

if (file_exists($tmpFile)) {
unlink($tmpFile);
}

$File = new File($tmpFile, true, '0777');
$this->assertTrue($File->write('123'), 'Could not write to ' . $tmpFile);
clearstatcache(true, $tmpFile);

$item = array(
'title' => array(
'value' => 'My Title',
'cdata' => true,
),
'link' => 'http://www.example.com/1',
'description' => array(
'value' => 'descriptive words',
'cdata' => true,
),
'enclosure' => array(
'url' => '/tests/cakephp.file.test.tmp'
),
'pubDate' => '2008-05-31 12:00:00',
'guid' => 'http://www.example.com/1',
'category' => array(
array(
'value' => 'CakePHP',
'cdata' => true,
'domain' => 'http://www.cakephp.org'
),
array(
'value' => 'Bakery',
'cdata' => true
)
)
);
$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',
'enclosure' => array(
'url' => $this->Rss->url('/tests/cakephp.file.test.tmp', true),
'length' => filesize($tmpFile),
'type' => 'text/plain'
),
'<pubDate',
date('r', strtotime('2008-05-31 12:00:00')),
'/pubDate',
'<guid',
'http://www.example.com/1',
'/guid',
'category' => array('domain' => 'http://www.cakephp.org'),
'<![CDATA[CakePHP]]',
'/category',
'<category',
'<![CDATA[Bakery]]',
'/category',
'/item'
);
$this->assertTags($result, $expected);

unlink($tmpFile);
}

/**
* testTime method
*
Expand Down Expand Up @@ -507,4 +675,22 @@ public function testElementAttrNotInParent() {
);
$this->assertTags($result, $expected);
}

/**
* getWwwTmpFile method
*
* @param bool $paintSkip
* @return void
*/
function _getWwwTmpFile() {
$path = WWW_ROOT . 'tests' . DS;
$tmpFile = $path. 'cakephp.file.test.tmp';
if (is_writable(dirname($tmpFile)) && (!file_exists($tmpFile) || is_writable($tmpFile))) {
return $tmpFile;
};

$message = __d('cake_dev', '%s is not writeable', $path );
$this->markTestSkipped($message);
return false;
}
}

0 comments on commit 0e9b3dd

Please sign in to comment.