]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/feed/FeedBuilderTest.php
New plugin hook: ability to add custom filters to Shaarli search engine
[github/shaarli/Shaarli.git] / tests / feed / FeedBuilderTest.php
1 <?php
2
3 namespace Shaarli\Feed;
4
5 use DateTime;
6 use malkusch\lock\mutex\NoMutex;
7 use ReferenceLinkDB;
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;
13 use Shaarli\History;
14 use Shaarli\Plugin\PluginManager;
15 use Shaarli\TestCase;
16
17 /**
18 * FeedBuilderTest class.
19 *
20 * Unit tests for FeedBuilder.
21 */
22 class FeedBuilderTest extends TestCase
23 {
24 /**
25 * @var string locale Basque (Spain).
26 */
27 public static $LOCALE = 'eu_ES';
28
29 /**
30 * @var string language in RSS format.
31 */
32 public static $RSS_LANGUAGE = 'eu-es';
33
34 /**
35 * @var string language in ATOM format.
36 */
37 public static $ATOM_LANGUAGUE = 'eu';
38
39 protected static $testDatastore = 'sandbox/datastore.php';
40
41 public static $bookmarkService;
42
43 public static $formatter;
44
45 public static $serverInfo;
46
47 /**
48 * Called before every test method.
49 */
50 public static function setUpBeforeClass(): void
51 {
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(
62 $conf,
63 $pluginManager,
64 $history,
65 $mutex,
66 true
67 );
68
69 self::$serverInfo = array(
70 'HTTPS' => 'Off',
71 'SERVER_NAME' => 'host.tld',
72 'SERVER_PORT' => '80',
73 'SCRIPT_NAME' => '/index.php',
74 'REQUEST_URI' => '/feed/atom',
75 );
76 }
77
78 /**
79 * Test buildData with RSS feed.
80 */
81 public function testRSSBuildData()
82 {
83 $feedBuilder = new FeedBuilder(
84 self::$bookmarkService,
85 self::$formatter,
86 static::$serverInfo,
87 false
88 );
89 $feedBuilder->setLocale(self::$LOCALE);
90 $data = $feedBuilder->buildData(FeedBuilder::$FEED_RSS, null);
91 // Test headers (RSS)
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']));
99
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]);
115
116 // Test URL with external link.
117 $this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $data['links'][8]['url']);
118
119 // Test multitags.
120 $this->assertEquals(5, count($data['links'][6]['taglist']));
121 $this->assertEquals('css', $data['links'][6]['taglist'][0]);
122
123 // Test update date
124 $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['links'][8]['up_iso_date']);
125 }
126
127 /**
128 * Test buildData with ATOM feed (test only specific to ATOM).
129 */
130 public function testAtomBuildData()
131 {
132 $feedBuilder = new FeedBuilder(
133 self::$bookmarkService,
134 self::$formatter,
135 static::$serverInfo,
136 false
137 );
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']);
145 }
146
147 /**
148 * Test buildData with search criteria.
149 */
150 public function testBuildDataFiltered()
151 {
152 $criteria = array(
153 'searchtags' => 'stuff',
154 'searchterm' => 'beard',
155 );
156 $feedBuilder = new FeedBuilder(
157 self::$bookmarkService,
158 self::$formatter,
159 static::$serverInfo,
160 false
161 );
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']);
168 }
169
170 /**
171 * Test buildData with nb limit.
172 */
173 public function testBuildDataCount()
174 {
175 $criteria = array(
176 'nb' => '3',
177 );
178 $feedBuilder = new FeedBuilder(
179 self::$bookmarkService,
180 self::$formatter,
181 static::$serverInfo,
182 false
183 );
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']);
190 }
191
192 /**
193 * Test buildData with permalinks on.
194 */
195 public function testBuildDataPermalinks()
196 {
197 $feedBuilder = new FeedBuilder(
198 self::$bookmarkService,
199 self::$formatter,
200 static::$serverInfo,
201 false
202 );
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']);
224 }
225
226 /**
227 * Test buildData with hide dates settings.
228 */
229 public function testBuildDataHideDates()
230 {
231 $feedBuilder = new FeedBuilder(
232 self::$bookmarkService,
233 self::$formatter,
234 static::$serverInfo,
235 false
236 );
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']);
242
243 // Show dates while logged in
244 $feedBuilder = new FeedBuilder(
245 self::$bookmarkService,
246 self::$formatter,
247 static::$serverInfo,
248 true
249 );
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']);
255 }
256
257 /**
258 * Test buildData when Shaarli is served from a subdirectory
259 */
260 public function testBuildDataServerSubdir()
261 {
262 $serverInfo = array(
263 'HTTPS' => 'Off',
264 'SERVER_NAME' => 'host.tld',
265 'SERVER_PORT' => '8080',
266 'SCRIPT_NAME' => '/~user/shaarli/index.php',
267 'REQUEST_URI' => '/~user/shaarli/feed/atom',
268 );
269 $feedBuilder = new FeedBuilder(
270 self::$bookmarkService,
271 self::$formatter,
272 $serverInfo,
273 false
274 );
275 $feedBuilder->setLocale(self::$LOCALE);
276 $data = $feedBuilder->buildData(FeedBuilder::$FEED_ATOM, null);
277
278 $this->assertEquals(
279 'http://host.tld:8080/~user/shaarli/feed/atom',
280 $data['self_link']
281 );
282
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']);
288 }
289 }