2008-03-21 06:58:50 +01:00
|
|
|
<?php
|
2016-06-15 06:03:16 +02:00
|
|
|
|
|
|
|
use SilverStripe\ORM\ArrayList;
|
2008-06-15 15:33:53 +02:00
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-06-15 15:33:53 +02:00
|
|
|
* @subpackage tests
|
|
|
|
*/
|
2008-03-21 06:58:50 +01:00
|
|
|
class RSSFeedTest extends SapphireTest {
|
2010-12-05 09:35:33 +01:00
|
|
|
|
|
|
|
protected static $original_host;
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testRSSFeed() {
|
2011-05-05 12:40:24 +02:00
|
|
|
$list = new ArrayList();
|
2008-03-21 06:58:50 +01:00
|
|
|
$list->push(new RSSFeedTest_ItemA());
|
|
|
|
$list->push(new RSSFeedTest_ItemB());
|
|
|
|
$list->push(new RSSFeedTest_ItemC());
|
2008-05-22 01:51:17 +02:00
|
|
|
|
2008-03-21 06:58:50 +01:00
|
|
|
$rssFeed = new RSSFeed($list, "http://www.example.com", "Test RSS Feed", "Test RSS Feed Description");
|
2012-07-31 10:38:12 +02:00
|
|
|
$content = $rssFeed->outputToBrowser();
|
2008-03-21 06:58:50 +01:00
|
|
|
|
2008-05-22 01:51:17 +02:00
|
|
|
$this->assertContains('<link>http://www.example.org/item-a/</link>', $content);
|
2008-03-21 06:58:50 +01:00
|
|
|
$this->assertContains('<link>http://www.example.com/item-b.html</link>', $content);
|
|
|
|
$this->assertContains('<link>http://www.example.com/item-c.html</link>', $content);
|
|
|
|
|
|
|
|
$this->assertContains('<title>ItemA</title>', $content);
|
|
|
|
$this->assertContains('<title>ItemB</title>', $content);
|
|
|
|
$this->assertContains('<title>ItemC</title>', $content);
|
|
|
|
|
|
|
|
$this->assertContains('<description>ItemA Content</description>', $content);
|
|
|
|
$this->assertContains('<description>ItemB Content</description>', $content);
|
|
|
|
$this->assertContains('<description>ItemC Content</description>', $content);
|
|
|
|
|
|
|
|
|
|
|
|
// Feed #2 - put Content() into <title> and AltContent() into <description>
|
2012-09-26 23:34:00 +02:00
|
|
|
$rssFeed = new RSSFeed($list, "http://www.example.com", "Test RSS Feed", "Test RSS Feed Description",
|
|
|
|
"Content", "AltContent");
|
2012-07-31 10:38:12 +02:00
|
|
|
$content = $rssFeed->outputToBrowser();
|
2008-03-21 06:58:50 +01:00
|
|
|
|
|
|
|
$this->assertContains('<title>ItemA Content</title>', $content);
|
|
|
|
$this->assertContains('<title>ItemB Content</title>', $content);
|
|
|
|
$this->assertContains('<title>ItemC Content</title>', $content);
|
|
|
|
|
|
|
|
$this->assertContains('<description>ItemA AltContent</description>', $content);
|
|
|
|
$this->assertContains('<description>ItemB AltContent</description>', $content);
|
|
|
|
$this->assertContains('<description>ItemC AltContent</description>', $content);
|
2010-12-05 09:35:33 +01:00
|
|
|
}
|
|
|
|
|
2015-11-01 23:33:04 +01:00
|
|
|
public function testLinkEncoding() {
|
|
|
|
$list = new ArrayList();
|
|
|
|
$rssFeed = new RSSFeed($list, "http://www.example.com/?param1=true¶m2=true", "Test RSS Feed");
|
|
|
|
$content = $rssFeed->outputToBrowser();
|
|
|
|
$this->assertContains('<link>http://www.example.com/?param1=true&param2=true', $content);
|
|
|
|
}
|
|
|
|
|
2013-10-10 10:04:03 +02:00
|
|
|
public function testRSSFeedWithShortcode() {
|
|
|
|
$list = new ArrayList();
|
|
|
|
$list->push(new RSSFeedTest_ItemD());
|
|
|
|
|
|
|
|
$rssFeed = new RSSFeed($list, "http://www.example.com", "Test RSS Feed", "Test RSS Feed Description");
|
|
|
|
$content = $rssFeed->outputToBrowser();
|
|
|
|
|
|
|
|
$this->assertContains('<link>http://www.example.org/item-d.html</link>', $content);
|
|
|
|
|
|
|
|
$this->assertContains('<title>ItemD</title>', $content);
|
|
|
|
|
|
|
|
$this->assertContains(
|
|
|
|
'<description><p>ItemD Content test shortcode output</p></description>',
|
|
|
|
$content
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-07-01 11:25:57 +02:00
|
|
|
public function testRenderWithTemplate() {
|
|
|
|
$rssFeed = new RSSFeed(new ArrayList(), "", "", "");
|
|
|
|
$rssFeed->setTemplate('RSSFeedTest');
|
|
|
|
|
2012-07-31 10:38:12 +02:00
|
|
|
$content = $rssFeed->outputToBrowser();
|
2012-07-01 11:25:57 +02:00
|
|
|
$this->assertContains('<title>Test Custom Template</title>', $content);
|
|
|
|
|
|
|
|
$rssFeed->setTemplate('RSSFeed');
|
2012-07-31 10:38:12 +02:00
|
|
|
$content = $rssFeed->outputToBrowser();
|
2012-07-01 11:25:57 +02:00
|
|
|
$this->assertNotContains('<title>Test Custom Template</title>', $content);
|
|
|
|
}
|
|
|
|
|
2010-12-05 09:35:33 +01:00
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
2013-03-21 19:48:54 +01:00
|
|
|
Config::inst()->update('Director', 'alternate_base_url', '/');
|
2010-12-05 09:35:33 +01:00
|
|
|
if(!self::$original_host) self::$original_host = $_SERVER['HTTP_HOST'];
|
|
|
|
$_SERVER['HTTP_HOST'] = 'www.example.org';
|
2013-10-10 10:04:03 +02:00
|
|
|
|
|
|
|
ShortcodeParser::get('default')->register('test_shortcode', function() {
|
|
|
|
return 'test shortcode output';
|
|
|
|
});
|
2010-12-05 09:35:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown() {
|
|
|
|
parent::tearDown();
|
2013-03-21 19:48:54 +01:00
|
|
|
Config::inst()->update('Director', 'alternate_base_url', null);
|
2010-12-05 09:35:33 +01:00
|
|
|
$_SERVER['HTTP_HOST'] = self::$original_host;
|
2008-03-21 06:58:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class RSSFeedTest_ItemA extends ViewableData {
|
|
|
|
// RSS-feed items must have $casting/$db information.
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $casting = array(
|
2008-03-21 06:58:50 +01:00
|
|
|
'Title' => 'Varchar',
|
|
|
|
'Content' => 'Text',
|
|
|
|
'AltContent' => 'Text',
|
|
|
|
);
|
2013-10-10 10:04:03 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getTitle() {
|
2008-03-21 06:58:50 +01:00
|
|
|
return "ItemA";
|
|
|
|
}
|
2012-07-31 10:38:12 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getContent() {
|
2008-03-21 06:58:50 +01:00
|
|
|
return "ItemA Content";
|
|
|
|
}
|
2012-07-31 10:38:12 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getAltContent() {
|
2008-03-21 06:58:50 +01:00
|
|
|
return "ItemA AltContent";
|
|
|
|
}
|
2013-10-10 10:04:03 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Link($action = null) {
|
2009-10-11 02:07:16 +02:00
|
|
|
return Controller::join_links("item-a/", $action);
|
2008-03-21 06:58:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class RSSFeedTest_ItemB extends ViewableData {
|
|
|
|
// ItemB tests without $casting
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Title() {
|
2008-03-21 06:58:50 +01:00
|
|
|
return "ItemB";
|
|
|
|
}
|
2012-07-31 10:38:12 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function AbsoluteLink() {
|
2008-03-21 06:58:50 +01:00
|
|
|
return "http://www.example.com/item-b.html";
|
|
|
|
}
|
2012-07-31 10:38:12 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Content() {
|
2008-03-21 06:58:50 +01:00
|
|
|
return "ItemB Content";
|
|
|
|
}
|
2012-07-31 10:38:12 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function AltContent() {
|
2008-03-21 06:58:50 +01:00
|
|
|
return "ItemB AltContent";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class RSSFeedTest_ItemC extends ViewableData {
|
|
|
|
// ItemC tests fields - Title has casting, Content doesn't.
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $casting = array(
|
2008-03-21 06:58:50 +01:00
|
|
|
'Title' => 'Varchar',
|
|
|
|
'AltContent' => 'Text',
|
|
|
|
);
|
|
|
|
|
|
|
|
public $Title = "ItemC";
|
|
|
|
public $Content = "ItemC Content";
|
|
|
|
public $AltContent = "ItemC AltContent";
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Link() {
|
2008-03-21 06:58:50 +01:00
|
|
|
return "item-c.html";
|
|
|
|
}
|
2012-07-31 10:38:12 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function AbsoluteLink() {
|
2008-03-21 06:58:50 +01:00
|
|
|
return "http://www.example.com/item-c.html";
|
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|
2013-10-10 10:04:03 +02:00
|
|
|
|
|
|
|
class RSSFeedTest_ItemD extends ViewableData {
|
|
|
|
// ItemD test fields - all fields use casting but Content & AltContent cast as HTMLText
|
|
|
|
private static $casting = array(
|
|
|
|
'Title' => 'Varchar',
|
|
|
|
'Content' => 'HTMLText'
|
|
|
|
);
|
|
|
|
|
|
|
|
public $Title = 'ItemD';
|
|
|
|
public $Content = '<p>ItemD Content [test_shortcode]</p>';
|
|
|
|
|
|
|
|
public function Link() {
|
|
|
|
return 'item-d.html';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function AbsoluteLink() {
|
|
|
|
return 'http://www.example.org/item-d.html';
|
|
|
|
}
|
|
|
|
}
|