3 namespace Shaarli\Feed
;
6 use malkusch\lock\mutex\NoMutex
;
8 use Shaarli\Bookmark\Bookmark
;
9 use Shaarli\Bookmark\BookmarkFileService
;
10 use Shaarli\Bookmark\LinkDB
;
11 use Shaarli\Config\ConfigManager
;
12 use Shaarli\Formatter\FormatterFactory
;
14 use Shaarli\Plugin\PluginManager
;
18 * FeedBuilderTest class.
20 * Unit tests for FeedBuilder.
22 class FeedBuilderTest
extends TestCase
25 * @var string locale Basque (Spain).
27 public static $LOCALE = 'eu_ES';
30 * @var string language in RSS format.
32 public static $RSS_LANGUAGE = 'eu-es';
35 * @var string language in ATOM format.
37 public static $ATOM_LANGUAGUE = 'eu';
39 protected static $testDatastore = 'sandbox/datastore.php';
41 public static $bookmarkService;
43 public static $formatter;
45 public static $serverInfo;
48 * Called before every test method.
50 public static function setUpBeforeClass(): void
52 $mutex = new NoMutex();
53 $conf = new ConfigManager('tests/utils/config/configJson');
54 $conf->set('resource.datastore', self
::$testDatastore);
55 $refLinkDB = new \
ReferenceLinkDB();
56 $refLinkDB->write(self
::$testDatastore);
57 $history = new History('sandbox/history.php');
58 $factory = new FormatterFactory($conf, true);
59 $pluginManager = new PluginManager($conf);
60 self
::$formatter = $factory->getFormatter();
61 self
::$bookmarkService = new BookmarkFileService(
69 self
::$serverInfo = array(
71 'SERVER_NAME' => 'host.tld',
72 'SERVER_PORT' => '80',
73 'SCRIPT_NAME' => '/index.php',
74 'REQUEST_URI' => '/feed/atom',
79 * Test buildData with RSS feed.
81 public function testRSSBuildData()
83 $feedBuilder = new FeedBuilder(
84 self
::$bookmarkService,
89 $feedBuilder->setLocale(self
::$LOCALE);
90 $data = $feedBuilder->buildData(FeedBuilder
::$FEED_RSS, null);
92 $this->assertEquals(self
::$RSS_LANGUAGE, $data['language']);
93 $this->assertRegExp('/Wed, 03 Aug 2016 09:30:33 \+\d{4}/', $data['last_update']);
94 $this->assertEquals(true, $data['show_dates']);
95 $this->assertEquals('http://host.tld/feed/atom', $data['self_link']);
96 $this->assertEquals('http://host.tld/', $data['index_url']);
97 $this->assertFalse($data['usepermalinks']);
98 $this->assertEquals(ReferenceLinkDB
::$NB_LINKS_TOTAL, count($data['links']));
100 // Test first not pinned link (note link)
101 $link = $data['links'][array_keys($data['links'])[0]];
102 $this->assertEquals(41, $link['id']);
103 $this->assertEquals(DateTime
::createFromFormat(Bookmark
::LINK_DATE_FORMAT
, '20150310_114651'), $link['created']);
104 $this->assertEquals('http://host.tld/shaare/WDWyig', $link['guid']);
105 $this->assertEquals('http://host.tld/shaare/WDWyig', $link['url']);
106 $this->assertRegExp('/Tue, 10 Mar 2015 11:46:51 \+\d{4}/', $link['pub_iso_date']);
107 $pub = DateTime
::createFromFormat(DateTime
::RSS
, $link['pub_iso_date']);
108 $up = DateTime
::createFromFormat(DateTime
::ATOM
, $link['up_iso_date']);
109 $this->assertEquals($pub, $up);
110 $this->assertContainsPolyfill('Stallman has a beard', $link['description']);
111 $this->assertContainsPolyfill('Permalink', $link['description']);
112 $this->assertContainsPolyfill('http://host.tld/shaare/WDWyig', $link['description']);
113 $this->assertEquals(1, count($link['taglist']));
114 $this->assertEquals('sTuff', $link['taglist'][0]);
116 // Test URL with external link.
117 $this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $data['links'][8]['url']);
120 $this->assertEquals(5, count($data['links'][6]['taglist']));
121 $this->assertEquals('css', $data['links'][6]['taglist'][0]);
124 $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['links'][8]['up_iso_date']);
128 * Test buildData with ATOM feed (test only specific to ATOM).
130 public function testAtomBuildData()
132 $feedBuilder = new FeedBuilder(
133 self
::$bookmarkService,
138 $feedBuilder->setLocale(self
::$LOCALE);
139 $data = $feedBuilder->buildData(FeedBuilder
::$FEED_ATOM, null);
140 $this->assertEquals(ReferenceLinkDB
::$NB_LINKS_TOTAL, count($data['links']));
141 $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['last_update']);
142 $link = $data['links'][array_keys($data['links'])[0]];
143 $this->assertRegExp('/2015-03-10T11:46:51\+\d{2}:\d{2}/', $link['pub_iso_date']);
144 $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['links'][8]['up_iso_date']);
148 * Test buildData with search criteria.
150 public function testBuildDataFiltered()
153 'searchtags' => 'stuff',
154 'searchterm' => 'beard',
156 $feedBuilder = new FeedBuilder(
157 self
::$bookmarkService,
162 $feedBuilder->setLocale(self
::$LOCALE);
163 $data = $feedBuilder->buildData(FeedBuilder
::$FEED_ATOM, $criteria);
164 $this->assertEquals(1, count($data['links']));
165 $link = array_shift($data['links']);
166 $this->assertEquals(41, $link['id']);
167 $this->assertEquals(DateTime
::createFromFormat(Bookmark
::LINK_DATE_FORMAT
, '20150310_114651'), $link['created']);
171 * Test buildData with nb limit.
173 public function testBuildDataCount()
178 $feedBuilder = new FeedBuilder(
179 self
::$bookmarkService,
184 $feedBuilder->setLocale(self
::$LOCALE);
185 $data = $feedBuilder->buildData(FeedBuilder
::$FEED_ATOM, $criteria);
186 $this->assertEquals(3, count($data['links']));
187 $link = $data['links'][array_keys($data['links'])[0]];
188 $this->assertEquals(41, $link['id']);
189 $this->assertEquals(DateTime
::createFromFormat(Bookmark
::LINK_DATE_FORMAT
, '20150310_114651'), $link['created']);
193 * Test buildData with permalinks on.
195 public function testBuildDataPermalinks()
197 $feedBuilder = new FeedBuilder(
198 self
::$bookmarkService,
203 $feedBuilder->setLocale(self
::$LOCALE);
204 $feedBuilder->setUsePermalinks(true);
205 $data = $feedBuilder->buildData(FeedBuilder
::$FEED_ATOM, null);
206 $this->assertEquals(ReferenceLinkDB
::$NB_LINKS_TOTAL, count($data['links']));
207 $this->assertTrue($data['usepermalinks']);
208 // First link is a permalink
209 $link = $data['links'][array_keys($data['links'])[0]];
210 $this->assertEquals(41, $link['id']);
211 $this->assertEquals(DateTime
::createFromFormat(Bookmark
::LINK_DATE_FORMAT
, '20150310_114651'), $link['created']);
212 $this->assertEquals('http://host.tld/shaare/WDWyig', $link['guid']);
213 $this->assertEquals('http://host.tld/shaare/WDWyig', $link['url']);
214 $this->assertContainsPolyfill('Direct link', $link['description']);
215 $this->assertContainsPolyfill('http://host.tld/shaare/WDWyig', $link['description']);
216 // Second link is a direct link
217 $link = $data['links'][array_keys($data['links'])[1]];
218 $this->assertEquals(8, $link['id']);
219 $this->assertEquals(DateTime
::createFromFormat(Bookmark
::LINK_DATE_FORMAT
, '20150310_114633'), $link['created']);
220 $this->assertEquals('http://host.tld/shaare/RttfEw', $link['guid']);
221 $this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['url']);
222 $this->assertContainsPolyfill('Direct link', $link['description']);
223 $this->assertContainsPolyfill('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['description']);
227 * Test buildData with hide dates settings.
229 public function testBuildDataHideDates()
231 $feedBuilder = new FeedBuilder(
232 self
::$bookmarkService,
237 $feedBuilder->setLocale(self
::$LOCALE);
238 $feedBuilder->setHideDates(true);
239 $data = $feedBuilder->buildData(FeedBuilder
::$FEED_ATOM, null);
240 $this->assertEquals(ReferenceLinkDB
::$NB_LINKS_TOTAL, count($data['links']));
241 $this->assertFalse($data['show_dates']);
243 // Show dates while logged in
244 $feedBuilder = new FeedBuilder(
245 self
::$bookmarkService,
250 $feedBuilder->setLocale(self
::$LOCALE);
251 $feedBuilder->setHideDates(true);
252 $data = $feedBuilder->buildData(FeedBuilder
::$FEED_ATOM, null);
253 $this->assertEquals(ReferenceLinkDB
::$NB_LINKS_TOTAL, count($data['links']));
254 $this->assertTrue($data['show_dates']);
258 * Test buildData when Shaarli is served from a subdirectory
260 public function testBuildDataServerSubdir()
264 'SERVER_NAME' => 'host.tld',
265 'SERVER_PORT' => '8080',
266 'SCRIPT_NAME' => '/~user/shaarli/index.php',
267 'REQUEST_URI' => '/~user/shaarli/feed/atom',
269 $feedBuilder = new FeedBuilder(
270 self
::$bookmarkService,
275 $feedBuilder->setLocale(self
::$LOCALE);
276 $data = $feedBuilder->buildData(FeedBuilder
::$FEED_ATOM, null);
279 'http://host.tld:8080/~user/shaarli/feed/atom',
283 // Test first link (note link)
284 $link = $data['links'][array_keys($data['links'])[0]];
285 $this->assertEquals('http://host.tld:8080/~user/shaarli/shaare/WDWyig', $link['guid']);
286 $this->assertEquals('http://host.tld:8080/~user/shaarli/shaare/WDWyig', $link['url']);
287 $this->assertContainsPolyfill('http://host.tld:8080/~user/shaarli/./add-tag/hashtag', $link['description']);