3 require_once 'application/FeedBuilder.php';
4 require_once 'application/LinkDB.php';
7 * FeedBuilderTest class.
9 * Unit tests for FeedBuilder.
11 class FeedBuilderTest
extends PHPUnit_Framework_TestCase
14 * @var string locale Basque (Spain).
16 public static $LOCALE = 'eu_ES';
19 * @var string language in RSS format.
21 public static $RSS_LANGUAGE = 'eu-es';
24 * @var string language in ATOM format.
26 public static $ATOM_LANGUAGUE = 'eu';
28 protected static $testDatastore = 'sandbox/datastore.php';
30 public static $linkDB;
32 public static $serverInfo;
35 * Called before every test method.
37 public static function setUpBeforeClass()
39 $refLinkDB = new ReferenceLinkDB();
40 $refLinkDB->write(self
::$testDatastore);
41 self
::$linkDB = new LinkDB(self
::$testDatastore, true, false);
42 self
::$serverInfo = array(
44 'SERVER_NAME' => 'host.tld',
45 'SERVER_PORT' => '80',
46 'SCRIPT_NAME' => '/index.php',
47 'REQUEST_URI' => '/index.php?do=feed',
52 * Test GetTypeLanguage().
54 public function testGetTypeLanguage()
56 $feedBuilder = new FeedBuilder(null, FeedBuilder
::$FEED_ATOM, null, null, false);
57 $feedBuilder->setLocale(self
::$LOCALE);
58 $this->assertEquals(self
::$ATOM_LANGUAGUE, $feedBuilder->getTypeLanguage());
59 $feedBuilder = new FeedBuilder(null, FeedBuilder
::$FEED_RSS, null, null, false);
60 $feedBuilder->setLocale(self
::$LOCALE);
61 $this->assertEquals(self
::$RSS_LANGUAGE, $feedBuilder->getTypeLanguage());
62 $feedBuilder = new FeedBuilder(null, FeedBuilder
::$FEED_ATOM, null, null, false);
63 $this->assertEquals('en', $feedBuilder->getTypeLanguage());
64 $feedBuilder = new FeedBuilder(null, FeedBuilder
::$FEED_RSS, null, null, false);
65 $this->assertEquals('en-en', $feedBuilder->getTypeLanguage());
69 * Test buildData with RSS feed.
71 public function testRSSBuildData()
73 $feedBuilder = new FeedBuilder(self
::$linkDB, FeedBuilder
::$FEED_RSS, self
::$serverInfo, null, false);
74 $feedBuilder->setLocale(self
::$LOCALE);
75 $data = $feedBuilder->buildData();
77 $this->assertEquals(self
::$RSS_LANGUAGE, $data['language']);
78 $this->assertEmpty($data['pubsubhub_url']);
79 $this->assertRegExp('/Wed, 03 Aug 2016 09:30:33 \+\d{4}/', $data['last_update']);
80 $this->assertEquals(true, $data['show_dates']);
81 $this->assertEquals('http://host.tld/index.php?do=feed', $data['self_link']);
82 $this->assertEquals('http://host.tld/', $data['index_url']);
83 $this->assertFalse($data['usepermalinks']);
84 $this->assertEquals(ReferenceLinkDB
::$NB_LINKS_TOTAL, count($data['links']));
86 // Test first link (note link)
87 $link = array_shift($data['links']);
88 $this->assertEquals('20150310_114651', $link['linkdate']);
89 $this->assertEquals('http://host.tld/?WDWyig', $link['guid']);
90 $this->assertEquals('http://host.tld/?WDWyig', $link['url']);
91 $this->assertRegExp('/Tue, 10 Mar 2015 11:46:51 \+\d{4}/', $link['pub_iso_date']);
92 $pub = DateTime
::createFromFormat(DateTime
::RSS
, $link['pub_iso_date']);
93 $up = DateTime
::createFromFormat(DateTime
::ATOM
, $link['up_iso_date']);
94 $this->assertEquals($pub, $up);
95 $this->assertContains('Stallman has a beard', $link['description']);
96 $this->assertContains('Permalink', $link['description']);
97 $this->assertContains('http://host.tld/?WDWyig', $link['description']);
98 $this->assertEquals(1, count($link['taglist']));
99 $this->assertEquals('sTuff', $link['taglist'][0]);
101 // Test URL with external link.
102 $this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $data['links']['20150310_114633']['url']);
105 $this->assertEquals(5, count($data['links']['20141125_084734']['taglist']));
106 $this->assertEquals('css', $data['links']['20141125_084734']['taglist'][0]);
109 $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['links']['20150310_114633']['up_iso_date']);
113 * Test buildData with ATOM feed (test only specific to ATOM).
115 public function testAtomBuildData()
117 $feedBuilder = new FeedBuilder(self
::$linkDB, FeedBuilder
::$FEED_ATOM, self
::$serverInfo, null, false);
118 $feedBuilder->setLocale(self
::$LOCALE);
119 $data = $feedBuilder->buildData();
120 $this->assertEquals(ReferenceLinkDB
::$NB_LINKS_TOTAL, count($data['links']));
121 $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['last_update']);
122 $link = array_shift($data['links']);
123 $this->assertRegExp('/2015-03-10T11:46:51\+\d{2}:\d{2}/', $link['pub_iso_date']);
124 $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['links']['20150310_114633']['up_iso_date']);
128 * Test buildData with search criteria.
130 public function testBuildDataFiltered()
133 'searchtags' => 'stuff',
134 'searchterm' => 'beard',
136 $feedBuilder = new FeedBuilder(self
::$linkDB, FeedBuilder
::$FEED_ATOM, self
::$serverInfo, $criteria, false);
137 $feedBuilder->setLocale(self
::$LOCALE);
138 $data = $feedBuilder->buildData();
139 $this->assertEquals(1, count($data['links']));
140 $link = array_shift($data['links']);
141 $this->assertEquals('20150310_114651', $link['linkdate']);
145 * Test buildData with nb limit.
147 public function testBuildDataCount()
152 $feedBuilder = new FeedBuilder(self
::$linkDB, FeedBuilder
::$FEED_ATOM, self
::$serverInfo, $criteria, false);
153 $feedBuilder->setLocale(self
::$LOCALE);
154 $data = $feedBuilder->buildData();
155 $this->assertEquals(1, count($data['links']));
156 $link = array_shift($data['links']);
157 $this->assertEquals('20150310_114651', $link['linkdate']);
161 * Test buildData with permalinks on.
163 public function testBuildDataPermalinks()
165 $feedBuilder = new FeedBuilder(self
::$linkDB, FeedBuilder
::$FEED_ATOM, self
::$serverInfo, null, false);
166 $feedBuilder->setLocale(self
::$LOCALE);
167 $feedBuilder->setUsePermalinks(true);
168 $data = $feedBuilder->buildData();
169 $this->assertEquals(ReferenceLinkDB
::$NB_LINKS_TOTAL, count($data['links']));
170 $this->assertTrue($data['usepermalinks']);
171 // First link is a permalink
172 $link = array_shift($data['links']);
173 $this->assertEquals('20150310_114651', $link['linkdate']);
174 $this->assertEquals('http://host.tld/?WDWyig', $link['guid']);
175 $this->assertEquals('http://host.tld/?WDWyig', $link['url']);
176 $this->assertContains('Direct link', $link['description']);
177 $this->assertContains('http://host.tld/?WDWyig', $link['description']);
178 // Second link is a direct link
179 $link = array_shift($data['links']);
180 $this->assertEquals('20150310_114633', $link['linkdate']);
181 $this->assertEquals('http://host.tld/?kLHmZg', $link['guid']);
182 $this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['url']);
183 $this->assertContains('Direct link', $link['description']);
184 $this->assertContains('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['description']);
188 * Test buildData with hide dates settings.
190 public function testBuildDataHideDates()
192 $feedBuilder = new FeedBuilder(self
::$linkDB, FeedBuilder
::$FEED_ATOM, self
::$serverInfo, null, false);
193 $feedBuilder->setLocale(self
::$LOCALE);
194 $feedBuilder->setHideDates(true);
195 $data = $feedBuilder->buildData();
196 $this->assertEquals(ReferenceLinkDB
::$NB_LINKS_TOTAL, count($data['links']));
197 $this->assertFalse($data['show_dates']);
199 // Show dates while logged in
200 $feedBuilder = new FeedBuilder(self
::$linkDB, FeedBuilder
::$FEED_ATOM, self
::$serverInfo, null, true);
201 $feedBuilder->setLocale(self
::$LOCALE);
202 $feedBuilder->setHideDates(true);
203 $data = $feedBuilder->buildData();
204 $this->assertEquals(ReferenceLinkDB
::$NB_LINKS_TOTAL, count($data['links']));
205 $this->assertTrue($data['show_dates']);
209 * Test buildData with hide dates settings.
211 public function testBuildDataPubsubhub()
213 $feedBuilder = new FeedBuilder(self
::$linkDB, FeedBuilder
::$FEED_ATOM, self
::$serverInfo, null, false);
214 $feedBuilder->setLocale(self
::$LOCALE);
215 $feedBuilder->setPubsubhubUrl('http://pubsubhub.io');
216 $data = $feedBuilder->buildData();
217 $this->assertEquals(ReferenceLinkDB
::$NB_LINKS_TOTAL, count($data['links']));
218 $this->assertEquals('http://pubsubhub.io', $data['pubsubhub_url']);
222 * Test buildData when Shaarli is served from a subdirectory
224 public function testBuildDataServerSubdir()
228 'SERVER_NAME' => 'host.tld',
229 'SERVER_PORT' => '8080',
230 'SCRIPT_NAME' => '/~user/shaarli/index.php',
231 'REQUEST_URI' => '/~user/shaarli/index.php?do=feed',
233 $feedBuilder = new FeedBuilder(
235 FeedBuilder
::$FEED_ATOM,
240 $feedBuilder->setLocale(self
::$LOCALE);
241 $data = $feedBuilder->buildData();
244 'http://host.tld:8080/~user/shaarli/index.php?do=feed',
248 // Test first link (note link)
249 $link = array_shift($data['links']);
250 $this->assertEquals('http://host.tld:8080/~user/shaarli/?WDWyig', $link['guid']);
251 $this->assertEquals('http://host.tld:8080/~user/shaarli/?WDWyig', $link['url']);
252 $this->assertContains('http://host.tld:8080/~user/shaarli/?addtag=hashtag', $link['description']);