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
;
17 * FeedBuilderTest class.
19 * Unit tests for FeedBuilder.
21 class FeedBuilderTest
extends TestCase
24 * @var string locale Basque (Spain).
26 public static $LOCALE = 'eu_ES';
29 * @var string language in RSS format.
31 public static $RSS_LANGUAGE = 'eu-es';
34 * @var string language in ATOM format.
36 public static $ATOM_LANGUAGUE = 'eu';
38 protected static $testDatastore = 'sandbox/datastore.php';
40 public static $bookmarkService;
42 public static $formatter;
44 public static $serverInfo;
47 * Called before every test method.
49 public static function setUpBeforeClass(): void
51 $mutex = new NoMutex();
52 $conf = new ConfigManager('tests/utils/config/configJson');
53 $conf->set('resource.datastore', self
::$testDatastore);
54 $refLinkDB = new \
ReferenceLinkDB();
55 $refLinkDB->write(self
::$testDatastore);
56 $history = new History('sandbox/history.php');
57 $factory = new FormatterFactory($conf, true);
58 self
::$formatter = $factory->getFormatter();
59 self
::$bookmarkService = new BookmarkFileService($conf, $history, $mutex, true);
61 self
::$serverInfo = array(
63 'SERVER_NAME' => 'host.tld',
64 'SERVER_PORT' => '80',
65 'SCRIPT_NAME' => '/index.php',
66 'REQUEST_URI' => '/feed/atom',
71 * Test buildData with RSS feed.
73 public function testRSSBuildData()
75 $feedBuilder = new FeedBuilder(
76 self
::$bookmarkService,
81 $feedBuilder->setLocale(self
::$LOCALE);
82 $data = $feedBuilder->buildData(FeedBuilder
::$FEED_RSS, null);
84 $this->assertEquals(self
::$RSS_LANGUAGE, $data['language']);
85 $this->assertRegExp('/Wed, 03 Aug 2016 09:30:33 \+\d{4}/', $data['last_update']);
86 $this->assertEquals(true, $data['show_dates']);
87 $this->assertEquals('http://host.tld/feed/atom', $data['self_link']);
88 $this->assertEquals('http://host.tld/', $data['index_url']);
89 $this->assertFalse($data['usepermalinks']);
90 $this->assertEquals(ReferenceLinkDB
::$NB_LINKS_TOTAL, count($data['links']));
92 // Test first not pinned link (note link)
93 $link = $data['links'][array_keys($data['links'])[0]];
94 $this->assertEquals(41, $link['id']);
95 $this->assertEquals(DateTime
::createFromFormat(Bookmark
::LINK_DATE_FORMAT
, '20150310_114651'), $link['created']);
96 $this->assertEquals('http://host.tld/shaare/WDWyig', $link['guid']);
97 $this->assertEquals('http://host.tld/shaare/WDWyig', $link['url']);
98 $this->assertRegExp('/Tue, 10 Mar 2015 11:46:51 \+\d{4}/', $link['pub_iso_date']);
99 $pub = DateTime
::createFromFormat(DateTime
::RSS
, $link['pub_iso_date']);
100 $up = DateTime
::createFromFormat(DateTime
::ATOM
, $link['up_iso_date']);
101 $this->assertEquals($pub, $up);
102 $this->assertContainsPolyfill('Stallman has a beard', $link['description']);
103 $this->assertContainsPolyfill('Permalink', $link['description']);
104 $this->assertContainsPolyfill('http://host.tld/shaare/WDWyig', $link['description']);
105 $this->assertEquals(1, count($link['taglist']));
106 $this->assertEquals('sTuff', $link['taglist'][0]);
108 // Test URL with external link.
109 $this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $data['links'][8]['url']);
112 $this->assertEquals(5, count($data['links'][6]['taglist']));
113 $this->assertEquals('css', $data['links'][6]['taglist'][0]);
116 $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['links'][8]['up_iso_date']);
120 * Test buildData with ATOM feed (test only specific to ATOM).
122 public function testAtomBuildData()
124 $feedBuilder = new FeedBuilder(
125 self
::$bookmarkService,
130 $feedBuilder->setLocale(self
::$LOCALE);
131 $data = $feedBuilder->buildData(FeedBuilder
::$FEED_ATOM, null);
132 $this->assertEquals(ReferenceLinkDB
::$NB_LINKS_TOTAL, count($data['links']));
133 $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['last_update']);
134 $link = $data['links'][array_keys($data['links'])[0]];
135 $this->assertRegExp('/2015-03-10T11:46:51\+\d{2}:\d{2}/', $link['pub_iso_date']);
136 $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['links'][8]['up_iso_date']);
140 * Test buildData with search criteria.
142 public function testBuildDataFiltered()
145 'searchtags' => 'stuff',
146 'searchterm' => 'beard',
148 $feedBuilder = new FeedBuilder(
149 self
::$bookmarkService,
154 $feedBuilder->setLocale(self
::$LOCALE);
155 $data = $feedBuilder->buildData(FeedBuilder
::$FEED_ATOM, $criteria);
156 $this->assertEquals(1, count($data['links']));
157 $link = array_shift($data['links']);
158 $this->assertEquals(41, $link['id']);
159 $this->assertEquals(DateTime
::createFromFormat(Bookmark
::LINK_DATE_FORMAT
, '20150310_114651'), $link['created']);
163 * Test buildData with nb limit.
165 public function testBuildDataCount()
170 $feedBuilder = new FeedBuilder(
171 self
::$bookmarkService,
176 $feedBuilder->setLocale(self
::$LOCALE);
177 $data = $feedBuilder->buildData(FeedBuilder
::$FEED_ATOM, $criteria);
178 $this->assertEquals(3, count($data['links']));
179 $link = $data['links'][array_keys($data['links'])[0]];
180 $this->assertEquals(41, $link['id']);
181 $this->assertEquals(DateTime
::createFromFormat(Bookmark
::LINK_DATE_FORMAT
, '20150310_114651'), $link['created']);
185 * Test buildData with permalinks on.
187 public function testBuildDataPermalinks()
189 $feedBuilder = new FeedBuilder(
190 self
::$bookmarkService,
195 $feedBuilder->setLocale(self
::$LOCALE);
196 $feedBuilder->setUsePermalinks(true);
197 $data = $feedBuilder->buildData(FeedBuilder
::$FEED_ATOM, null);
198 $this->assertEquals(ReferenceLinkDB
::$NB_LINKS_TOTAL, count($data['links']));
199 $this->assertTrue($data['usepermalinks']);
200 // First link is a permalink
201 $link = $data['links'][array_keys($data['links'])[0]];
202 $this->assertEquals(41, $link['id']);
203 $this->assertEquals(DateTime
::createFromFormat(Bookmark
::LINK_DATE_FORMAT
, '20150310_114651'), $link['created']);
204 $this->assertEquals('http://host.tld/shaare/WDWyig', $link['guid']);
205 $this->assertEquals('http://host.tld/shaare/WDWyig', $link['url']);
206 $this->assertContainsPolyfill('Direct link', $link['description']);
207 $this->assertContainsPolyfill('http://host.tld/shaare/WDWyig', $link['description']);
208 // Second link is a direct link
209 $link = $data['links'][array_keys($data['links'])[1]];
210 $this->assertEquals(8, $link['id']);
211 $this->assertEquals(DateTime
::createFromFormat(Bookmark
::LINK_DATE_FORMAT
, '20150310_114633'), $link['created']);
212 $this->assertEquals('http://host.tld/shaare/RttfEw', $link['guid']);
213 $this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['url']);
214 $this->assertContainsPolyfill('Direct link', $link['description']);
215 $this->assertContainsPolyfill('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['description']);
219 * Test buildData with hide dates settings.
221 public function testBuildDataHideDates()
223 $feedBuilder = new FeedBuilder(
224 self
::$bookmarkService,
229 $feedBuilder->setLocale(self
::$LOCALE);
230 $feedBuilder->setHideDates(true);
231 $data = $feedBuilder->buildData(FeedBuilder
::$FEED_ATOM, null);
232 $this->assertEquals(ReferenceLinkDB
::$NB_LINKS_TOTAL, count($data['links']));
233 $this->assertFalse($data['show_dates']);
235 // Show dates while logged in
236 $feedBuilder = new FeedBuilder(
237 self
::$bookmarkService,
242 $feedBuilder->setLocale(self
::$LOCALE);
243 $feedBuilder->setHideDates(true);
244 $data = $feedBuilder->buildData(FeedBuilder
::$FEED_ATOM, null);
245 $this->assertEquals(ReferenceLinkDB
::$NB_LINKS_TOTAL, count($data['links']));
246 $this->assertTrue($data['show_dates']);
250 * Test buildData when Shaarli is served from a subdirectory
252 public function testBuildDataServerSubdir()
256 'SERVER_NAME' => 'host.tld',
257 'SERVER_PORT' => '8080',
258 'SCRIPT_NAME' => '/~user/shaarli/index.php',
259 'REQUEST_URI' => '/~user/shaarli/feed/atom',
261 $feedBuilder = new FeedBuilder(
262 self
::$bookmarkService,
267 $feedBuilder->setLocale(self
::$LOCALE);
268 $data = $feedBuilder->buildData(FeedBuilder
::$FEED_ATOM, null);
271 'http://host.tld:8080/~user/shaarli/feed/atom',
275 // Test first link (note link)
276 $link = $data['links'][array_keys($data['links'])[0]];
277 $this->assertEquals('http://host.tld:8080/~user/shaarli/shaare/WDWyig', $link['guid']);
278 $this->assertEquals('http://host.tld:8080/~user/shaarli/shaare/WDWyig', $link['url']);
279 $this->assertContainsPolyfill('http://host.tld:8080/~user/shaarli/./add-tag/hashtag', $link['description']);