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 = reset($data['links']);
88 $this->assertEquals(41, $link['id']);
89 $this->assertEquals(DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20150310_114651'), $link['created']);
90 $this->assertEquals('http://host.tld/?WDWyig', $link['guid']);
91 $this->assertEquals('http://host.tld/?WDWyig', $link['url']);
92 $this->assertRegExp('/Tue, 10 Mar 2015 11:46:51 \+\d{4}/', $link['pub_iso_date']);
93 $pub = DateTime
::createFromFormat(DateTime
::RSS
, $link['pub_iso_date']);
94 $up = DateTime
::createFromFormat(DateTime
::ATOM
, $link['up_iso_date']);
95 $this->assertEquals($pub, $up);
96 $this->assertContains('Stallman has a beard', $link['description']);
97 $this->assertContains('Permalink', $link['description']);
98 $this->assertContains('http://host.tld/?WDWyig', $link['description']);
99 $this->assertEquals(1, count($link['taglist']));
100 $this->assertEquals('sTuff', $link['taglist'][0]);
102 // Test URL with external link.
103 $this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $data['links'][8]['url']);
106 $this->assertEquals(5, count($data['links'][6]['taglist']));
107 $this->assertEquals('css', $data['links'][6]['taglist'][0]);
110 $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['links'][8]['up_iso_date']);
114 * Test buildData with ATOM feed (test only specific to ATOM).
116 public function testAtomBuildData()
118 $feedBuilder = new FeedBuilder(self
::$linkDB, FeedBuilder
::$FEED_ATOM, self
::$serverInfo, null, false);
119 $feedBuilder->setLocale(self
::$LOCALE);
120 $data = $feedBuilder->buildData();
121 $this->assertEquals(ReferenceLinkDB
::$NB_LINKS_TOTAL, count($data['links']));
122 $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['last_update']);
123 $link = reset($data['links']);
124 $this->assertRegExp('/2015-03-10T11:46:51\+\d{2}:\d{2}/', $link['pub_iso_date']);
125 $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['links'][8]['up_iso_date']);
129 * Test buildData with search criteria.
131 public function testBuildDataFiltered()
134 'searchtags' => 'stuff',
135 'searchterm' => 'beard',
137 $feedBuilder = new FeedBuilder(self
::$linkDB, FeedBuilder
::$FEED_ATOM, self
::$serverInfo, $criteria, false);
138 $feedBuilder->setLocale(self
::$LOCALE);
139 $data = $feedBuilder->buildData();
140 $this->assertEquals(1, count($data['links']));
141 $link = array_shift($data['links']);
142 $this->assertEquals(41, $link['id']);
143 $this->assertEquals(DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20150310_114651'), $link['created']);
147 * Test buildData with nb limit.
149 public function testBuildDataCount()
154 $feedBuilder = new FeedBuilder(self
::$linkDB, FeedBuilder
::$FEED_ATOM, self
::$serverInfo, $criteria, false);
155 $feedBuilder->setLocale(self
::$LOCALE);
156 $data = $feedBuilder->buildData();
157 $this->assertEquals(1, count($data['links']));
158 $link = array_shift($data['links']);
159 $this->assertEquals(41, $link['id']);
160 $this->assertEquals(DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20150310_114651'), $link['created']);
164 * Test buildData with permalinks on.
166 public function testBuildDataPermalinks()
168 $feedBuilder = new FeedBuilder(self
::$linkDB, FeedBuilder
::$FEED_ATOM, self
::$serverInfo, null, false);
169 $feedBuilder->setLocale(self
::$LOCALE);
170 $feedBuilder->setUsePermalinks(true);
171 $data = $feedBuilder->buildData();
172 $this->assertEquals(ReferenceLinkDB
::$NB_LINKS_TOTAL, count($data['links']));
173 $this->assertTrue($data['usepermalinks']);
174 // First link is a permalink
175 $link = array_shift($data['links']);
176 $this->assertEquals(41, $link['id']);
177 $this->assertEquals(DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20150310_114651'), $link['created']);
178 $this->assertEquals('http://host.tld/?WDWyig', $link['guid']);
179 $this->assertEquals('http://host.tld/?WDWyig', $link['url']);
180 $this->assertContains('Direct link', $link['description']);
181 $this->assertContains('http://host.tld/?WDWyig', $link['description']);
182 // Second link is a direct link
183 $link = array_shift($data['links']);
184 $this->assertEquals(8, $link['id']);
185 $this->assertEquals(DateTime
::createFromFormat(LinkDB
::LINK_DATE_FORMAT
, '20150310_114633'), $link['created']);
186 $this->assertEquals('http://host.tld/?RttfEw', $link['guid']);
187 $this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['url']);
188 $this->assertContains('Direct link', $link['description']);
189 $this->assertContains('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['description']);
193 * Test buildData with hide dates settings.
195 public function testBuildDataHideDates()
197 $feedBuilder = new FeedBuilder(self
::$linkDB, FeedBuilder
::$FEED_ATOM, self
::$serverInfo, null, false);
198 $feedBuilder->setLocale(self
::$LOCALE);
199 $feedBuilder->setHideDates(true);
200 $data = $feedBuilder->buildData();
201 $this->assertEquals(ReferenceLinkDB
::$NB_LINKS_TOTAL, count($data['links']));
202 $this->assertFalse($data['show_dates']);
204 // Show dates while logged in
205 $feedBuilder = new FeedBuilder(self
::$linkDB, FeedBuilder
::$FEED_ATOM, self
::$serverInfo, null, true);
206 $feedBuilder->setLocale(self
::$LOCALE);
207 $feedBuilder->setHideDates(true);
208 $data = $feedBuilder->buildData();
209 $this->assertEquals(ReferenceLinkDB
::$NB_LINKS_TOTAL, count($data['links']));
210 $this->assertTrue($data['show_dates']);
214 * Test buildData with hide dates settings.
216 public function testBuildDataPubsubhub()
218 $feedBuilder = new FeedBuilder(self
::$linkDB, FeedBuilder
::$FEED_ATOM, self
::$serverInfo, null, false);
219 $feedBuilder->setLocale(self
::$LOCALE);
220 $feedBuilder->setPubsubhubUrl('http://pubsubhub.io');
221 $data = $feedBuilder->buildData();
222 $this->assertEquals(ReferenceLinkDB
::$NB_LINKS_TOTAL, count($data['links']));
223 $this->assertEquals('http://pubsubhub.io', $data['pubsubhub_url']);
227 * Test buildData when Shaarli is served from a subdirectory
229 public function testBuildDataServerSubdir()
233 'SERVER_NAME' => 'host.tld',
234 'SERVER_PORT' => '8080',
235 'SCRIPT_NAME' => '/~user/shaarli/index.php',
236 'REQUEST_URI' => '/~user/shaarli/index.php?do=feed',
238 $feedBuilder = new FeedBuilder(
240 FeedBuilder
::$FEED_ATOM,
245 $feedBuilder->setLocale(self
::$LOCALE);
246 $data = $feedBuilder->buildData();
249 'http://host.tld:8080/~user/shaarli/index.php?do=feed',
253 // Test first link (note link)
254 $link = array_shift($data['links']);
255 $this->assertEquals('http://host.tld:8080/~user/shaarli/?WDWyig', $link['guid']);
256 $this->assertEquals('http://host.tld:8080/~user/shaarli/?WDWyig', $link['url']);
257 $this->assertContains('http://host.tld:8080/~user/shaarli/?addtag=hashtag', $link['description']);