Merge pull request from ArthurHoaro/template-feeds

Refactor RSS feeds generation, and do it through templates
This commit is contained in:
Arthur 2016-03-25 19:20:55 +01:00
commit f66a1990e5
16 changed files with 859 additions and 339 deletions

212
tests/FeedBuilderTest.php Normal file
View file

@ -0,0 +1,212 @@
<?php
require_once 'application/FeedBuilder.php';
require_once 'application/LinkDB.php';
/**
* FeedBuilderTest class.
*
* Unit tests for FeedBuilder.
*/
class FeedBuilderTest extends PHPUnit_Framework_TestCase
{
/**
* @var string locale Basque (Spain).
*/
public static $LOCALE = 'eu_ES';
/**
* @var string language in RSS format.
*/
public static $RSS_LANGUAGE = 'eu-es';
/**
* @var string language in ATOM format.
*/
public static $ATOM_LANGUAGUE = 'eu';
protected static $testDatastore = 'sandbox/datastore.php';
public static $linkDB;
public static $serverInfo;
/**
* Called before every test method.
*/
public static function setUpBeforeClass()
{
$refLinkDB = new ReferenceLinkDB();
$refLinkDB->write(self::$testDatastore);
self::$linkDB = new LinkDB(self::$testDatastore, true, false);
self::$serverInfo = array(
'HTTPS' => 'Off',
'SERVER_NAME' => 'host.tld',
'SERVER_PORT' => '80',
'SCRIPT_NAME' => '/index.php',
'REQUEST_URI' => '/index.php?do=feed',
);
}
/**
* Test GetTypeLanguage().
*/
public function testGetTypeLanguage()
{
$feedBuilder = new FeedBuilder(null, FeedBuilder::$FEED_ATOM, null, null, false);
$feedBuilder->setLocale(self::$LOCALE);
$this->assertEquals(self::$ATOM_LANGUAGUE, $feedBuilder->getTypeLanguage());
$feedBuilder = new FeedBuilder(null, FeedBuilder::$FEED_RSS, null, null, false);
$feedBuilder->setLocale(self::$LOCALE);
$this->assertEquals(self::$RSS_LANGUAGE, $feedBuilder->getTypeLanguage());
$feedBuilder = new FeedBuilder(null, FeedBuilder::$FEED_ATOM, null, null, false);
$this->assertEquals('en', $feedBuilder->getTypeLanguage());
$feedBuilder = new FeedBuilder(null, FeedBuilder::$FEED_RSS, null, null, false);
$this->assertEquals('en-en', $feedBuilder->getTypeLanguage());
}
/**
* Test buildData with RSS feed.
*/
public function testRSSBuildData()
{
$feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_RSS, self::$serverInfo, null, false);
$feedBuilder->setLocale(self::$LOCALE);
$data = $feedBuilder->buildData();
// Test headers (RSS)
$this->assertEquals(self::$RSS_LANGUAGE, $data['language']);
$this->assertEmpty($data['pubsubhub_url']);
$this->assertEquals('Tue, 10 Mar 2015 11:46:51 +0100', $data['last_update']);
$this->assertEquals(true, $data['show_dates']);
$this->assertEquals('http://host.tld/index.php?do=feed', $data['self_link']);
$this->assertEquals('http://host.tld/', $data['index_url']);
$this->assertFalse($data['usepermalinks']);
$this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
// Test first link (note link)
$link = array_shift($data['links']);
$this->assertEquals('20150310_114651', $link['linkdate']);
$this->assertEquals('http://host.tld/?WDWyig', $link['guid']);
$this->assertEquals('http://host.tld/?WDWyig', $link['url']);
$this->assertEquals('Tue, 10 Mar 2015 11:46:51 +0100', $link['iso_date']);
$this->assertContains('Stallman has a beard', $link['description']);
$this->assertContains('Permalink', $link['description']);
$this->assertContains('http://host.tld/?WDWyig', $link['description']);
$this->assertEquals(1, count($link['taglist']));
$this->assertEquals('stuff', $link['taglist'][0]);
// Test URL with external link.
$this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $data['links']['20150310_114633']['url']);
// Test multitags.
$this->assertEquals(5, count($data['links']['20141125_084734']['taglist']));
$this->assertEquals('css', $data['links']['20141125_084734']['taglist'][0]);
}
/**
* Test buildData with ATOM feed (test only specific to ATOM).
*/
public function testAtomBuildData()
{
$feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, false);
$feedBuilder->setLocale(self::$LOCALE);
$data = $feedBuilder->buildData();
$this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
$link = array_shift($data['links']);
$this->assertEquals('2015-03-10T11:46:51+01:00', $link['iso_date']);
}
/**
* Test buildData with search criteria.
*/
public function testBuildDataFiltered()
{
$criteria = array(
'searchtags' => 'stuff',
'searchterm' => 'beard',
);
$feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, $criteria, false);
$feedBuilder->setLocale(self::$LOCALE);
$data = $feedBuilder->buildData();
$this->assertEquals(1, count($data['links']));
$link = array_shift($data['links']);
$this->assertEquals('20150310_114651', $link['linkdate']);
}
/**
* Test buildData with nb limit.
*/
public function testBuildDataCount()
{
$criteria = array(
'nb' => '1',
);
$feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, $criteria, false);
$feedBuilder->setLocale(self::$LOCALE);
$data = $feedBuilder->buildData();
$this->assertEquals(1, count($data['links']));
$link = array_shift($data['links']);
$this->assertEquals('20150310_114651', $link['linkdate']);
}
/**
* Test buildData with permalinks on.
*/
public function testBuildDataPermalinks()
{
$feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, false);
$feedBuilder->setLocale(self::$LOCALE);
$feedBuilder->setUsePermalinks(true);
$data = $feedBuilder->buildData();
$this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
$this->assertTrue($data['usepermalinks']);
// First link is a permalink
$link = array_shift($data['links']);
$this->assertEquals('20150310_114651', $link['linkdate']);
$this->assertEquals('http://host.tld/?WDWyig', $link['guid']);
$this->assertEquals('http://host.tld/?WDWyig', $link['url']);
$this->assertContains('Direct link', $link['description']);
$this->assertContains('http://host.tld/?WDWyig', $link['description']);
// Second link is a direct link
$link = array_shift($data['links']);
$this->assertEquals('20150310_114633', $link['linkdate']);
$this->assertEquals('http://host.tld/?kLHmZg', $link['guid']);
$this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['url']);
$this->assertContains('Direct link', $link['description']);
$this->assertContains('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['description']);
}
/**
* Test buildData with hide dates settings.
*/
public function testBuildDataHideDates()
{
$feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, false);
$feedBuilder->setLocale(self::$LOCALE);
$feedBuilder->setHideDates(true);
$data = $feedBuilder->buildData();
$this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
$this->assertFalse($data['show_dates']);
// Show dates while logged in
$feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, true);
$feedBuilder->setLocale(self::$LOCALE);
$feedBuilder->setHideDates(true);
$data = $feedBuilder->buildData();
$this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
$this->assertTrue($data['show_dates']);
}
/**
* Test buildData with hide dates settings.
*/
public function testBuildDataPubsubhub()
{
$feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, false);
$feedBuilder->setLocale(self::$LOCALE);
$feedBuilder->setPubsubhubUrl('http://pubsubhub.io');
$data = $feedBuilder->buildData();
$this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
$this->assertEquals('http://pubsubhub.io', $data['pubsubhub_url']);
}
}

View file

@ -17,8 +17,20 @@ class LinkDBTest extends PHPUnit_Framework_TestCase
{
// datastore to test write operations
protected static $testDatastore = 'sandbox/datastore.php';
/**
* @var ReferenceLinkDB instance.
*/
protected static $refDB = null;
/**
* @var LinkDB public LinkDB instance.
*/
protected static $publicLinkDB = null;
/**
* @var LinkDB private LinkDB instance.
*/
protected static $privateLinkDB = null;
/**
@ -335,9 +347,10 @@ class LinkDBTest extends PHPUnit_Framework_TestCase
public function testFilterString()
{
$tags = 'dev cartoon';
$request = array('searchtags' => $tags);
$this->assertEquals(
2,
count(self::$privateLinkDB->filter(LinkFilter::$FILTER_TAG, $tags, true, false))
count(self::$privateLinkDB->filterSearch($request, true, false))
);
}
@ -347,9 +360,10 @@ class LinkDBTest extends PHPUnit_Framework_TestCase
public function testFilterArray()
{
$tags = array('dev', 'cartoon');
$request = array('searchtags' => $tags);
$this->assertEquals(
2,
count(self::$privateLinkDB->filter(LinkFilter::$FILTER_TAG, $tags, true, false))
count(self::$privateLinkDB->filterSearch($request, true, false))
);
}
@ -360,14 +374,48 @@ class LinkDBTest extends PHPUnit_Framework_TestCase
public function testHiddenTags()
{
$tags = '.hidden';
$request = array('searchtags' => $tags);
$this->assertEquals(
1,
count(self::$privateLinkDB->filter(LinkFilter::$FILTER_TAG, $tags, true, false))
count(self::$privateLinkDB->filterSearch($request, true, false))
);
$this->assertEquals(
0,
count(self::$publicLinkDB->filter(LinkFilter::$FILTER_TAG, $tags, true, false))
count(self::$publicLinkDB->filterSearch($request, true, false))
);
}
/**
* Test filterHash() with a valid smallhash.
*/
public function testFilterHashValid()
{
$request = smallHash('20150310_114651');
$this->assertEquals(
1,
count(self::$publicLinkDB->filterHash($request))
);
}
/**
* Test filterHash() with an invalid smallhash.
*
* @expectedException LinkNotFoundException
*/
public function testFilterHashInValid1()
{
$request = 'blabla';
self::$publicLinkDB->filterHash($request);
}
/**
* Test filterHash() with an empty smallhash.
*
* @expectedException LinkNotFoundException
*/
public function testFilterHashInValid()
{
self::$publicLinkDB->filterHash('');
}
}

View file

@ -12,8 +12,6 @@ class LinkFilterTest extends PHPUnit_Framework_TestCase
*/
protected static $linkFilter;
protected static $NB_LINKS_REFDB = 7;
/**
* Instanciate linkFilter with ReferenceLinkDB data.
*/
@ -29,7 +27,7 @@ class LinkFilterTest extends PHPUnit_Framework_TestCase
public function testFilter()
{
$this->assertEquals(
self::$NB_LINKS_REFDB,
ReferenceLinkDB::$NB_LINKS_TOTAL,
count(self::$linkFilter->filter('', ''))
);
@ -40,12 +38,12 @@ class LinkFilterTest extends PHPUnit_Framework_TestCase
);
$this->assertEquals(
self::$NB_LINKS_REFDB,
ReferenceLinkDB::$NB_LINKS_TOTAL,
count(self::$linkFilter->filter(LinkFilter::$FILTER_TAG, ''))
);
$this->assertEquals(
self::$NB_LINKS_REFDB,
ReferenceLinkDB::$NB_LINKS_TOTAL,
count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, ''))
);
}
@ -167,13 +165,12 @@ class LinkFilterTest extends PHPUnit_Framework_TestCase
/**
* No link for this hash
*
* @expectedException LinkNotFoundException
*/
public function testFilterUnknownSmallHash()
{
$this->assertEquals(
0,
count(self::$linkFilter->filter(LinkFilter::$FILTER_HASH, 'Iblaah'))
);
self::$linkFilter->filter(LinkFilter::$FILTER_HASH, 'Iblaah');
}
/**
@ -383,7 +380,7 @@ class LinkFilterTest extends PHPUnit_Framework_TestCase
))
);
$this->assertEquals(
self::$NB_LINKS_REFDB,
ReferenceLinkDB::$NB_LINKS_TOTAL,
count(self::$linkFilter->filter(
LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT,
''

View file

@ -236,9 +236,9 @@ class UpdaterTest extends PHPUnit_Framework_TestCase
$refDB = new ReferenceLinkDB();
$refDB->write(self::$testDatastore);
$linkDB = new LinkDB(self::$testDatastore, true, false);
$this->assertEmpty($linkDB->filter(LinkFilter::$FILTER_TAG, 'exclude'));
$this->assertEmpty($linkDB->filterSearch(array('searchtags' => 'exclude')));
$updater = new Updater(array(), self::$configFields, $linkDB, true);
$updater->updateMethodRenameDashTags();
$this->assertNotEmpty($linkDB->filter(LinkFilter::$FILTER_TAG, 'exclude'));
$this->assertNotEmpty($linkDB->filterSearch(array('searchtags' => 'exclude')));
}
}

View file

@ -4,6 +4,8 @@
*/
class ReferenceLinkDB
{
public static $NB_LINKS_TOTAL = 7;
private $_links = array();
private $_publicCount = 0;
private $_privateCount = 0;
@ -13,6 +15,15 @@ class ReferenceLinkDB
*/
function __construct()
{
$this->addLink(
'Link title: @website',
'?WDWyig',
'Stallman has a beard and is part of the Free Software Foundation (or not). Seriously, read this.',
0,
'20150310_114651',
'stuff'
);
$this->addLink(
'Free as in Freedom 2.0 @website',
'https://static.fsf.org/nosvn/faif-2.0.pdf',
@ -22,15 +33,6 @@ class ReferenceLinkDB
'free gnu software stallman -exclude stuff'
);
$this->addLink(
'Link title: @website',
'local',
'Stallman has a beard and is part of the Free Software Foundation (or not). Seriously, read this.',
0,
'20150310_114651',
'stuff'
);
$this->addLink(
'MediaGoblin',
'http://mediagoblin.org/',