]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/feed/FeedBuilderTest.php
Merge pull request #1697 from ArthurHoaro/feature/pagination
[github/shaarli/Shaarli.git] / tests / feed / FeedBuilderTest.php
CommitLineData
89baf23d
A
1<?php
2
dfc650aa
V
3namespace Shaarli\Feed;
4
5use DateTime;
fd1ddad9 6use malkusch\lock\mutex\NoMutex;
dfc650aa 7use ReferenceLinkDB;
e26e2060
A
8use Shaarli\Bookmark\Bookmark;
9use Shaarli\Bookmark\BookmarkFileService;
dea72c71 10use Shaarli\Bookmark\LinkDB;
e26e2060
A
11use Shaarli\Config\ConfigManager;
12use Shaarli\Formatter\FormatterFactory;
13use Shaarli\History;
a5a9cf23 14use Shaarli\TestCase;
dfc650aa 15
89baf23d
A
16/**
17 * FeedBuilderTest class.
18 *
19 * Unit tests for FeedBuilder.
20 */
b93cfeba 21class FeedBuilderTest extends TestCase
89baf23d
A
22{
23 /**
24 * @var string locale Basque (Spain).
25 */
26 public static $LOCALE = 'eu_ES';
27
28 /**
29 * @var string language in RSS format.
30 */
31 public static $RSS_LANGUAGE = 'eu-es';
32
33 /**
34 * @var string language in ATOM format.
35 */
36 public static $ATOM_LANGUAGUE = 'eu';
37
38 protected static $testDatastore = 'sandbox/datastore.php';
39
e26e2060
A
40 public static $bookmarkService;
41
42 public static $formatter;
89baf23d
A
43
44 public static $serverInfo;
45
46 /**
47 * Called before every test method.
48 */
b93cfeba 49 public static function setUpBeforeClass(): void
89baf23d 50 {
fd1ddad9 51 $mutex = new NoMutex();
e26e2060
A
52 $conf = new ConfigManager('tests/utils/config/configJson');
53 $conf->set('resource.datastore', self::$testDatastore);
54 $refLinkDB = new \ReferenceLinkDB();
89baf23d 55 $refLinkDB->write(self::$testDatastore);
e26e2060 56 $history = new History('sandbox/history.php');
a39acb25 57 $factory = new FormatterFactory($conf, true);
e26e2060 58 self::$formatter = $factory->getFormatter();
fd1ddad9 59 self::$bookmarkService = new BookmarkFileService($conf, $history, $mutex, true);
e26e2060 60
89baf23d
A
61 self::$serverInfo = array(
62 'HTTPS' => 'Off',
63 'SERVER_NAME' => 'host.tld',
64 'SERVER_PORT' => '80',
65 'SCRIPT_NAME' => '/index.php',
b93cfeba 66 'REQUEST_URI' => '/feed/atom',
89baf23d
A
67 );
68 }
69
89baf23d
A
70 /**
71 * Test buildData with RSS feed.
72 */
73 public function testRSSBuildData()
74 {
e26e2060
A
75 $feedBuilder = new FeedBuilder(
76 self::$bookmarkService,
77 self::$formatter,
f4929b11 78 static::$serverInfo,
e26e2060
A
79 false
80 );
89baf23d 81 $feedBuilder->setLocale(self::$LOCALE);
f4929b11 82 $data = $feedBuilder->buildData(FeedBuilder::$FEED_RSS, null);
89baf23d
A
83 // Test headers (RSS)
84 $this->assertEquals(self::$RSS_LANGUAGE, $data['language']);
c6d876bb 85 $this->assertRegExp('/Wed, 03 Aug 2016 09:30:33 \+\d{4}/', $data['last_update']);
89baf23d 86 $this->assertEquals(true, $data['show_dates']);
b93cfeba 87 $this->assertEquals('http://host.tld/feed/atom', $data['self_link']);
89baf23d
A
88 $this->assertEquals('http://host.tld/', $data['index_url']);
89 $this->assertFalse($data['usepermalinks']);
90 $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
91
4154c25b 92 // Test first not pinned link (note link)
a8e210fa 93 $link = $data['links'][array_keys($data['links'])[0]];
c3dfd899 94 $this->assertEquals(41, $link['id']);
e26e2060 95 $this->assertEquals(DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114651'), $link['created']);
f7f08cee
A
96 $this->assertEquals('http://host.tld/shaare/WDWyig', $link['guid']);
97 $this->assertEquals('http://host.tld/shaare/WDWyig', $link['url']);
c6d876bb
A
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']);
dfc650aa 100 $up = DateTime::createFromFormat(DateTime::ATOM, $link['up_iso_date']);
c6d876bb 101 $this->assertEquals($pub, $up);
a5a9cf23
A
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']);
89baf23d 105 $this->assertEquals(1, count($link['taglist']));
b1eb5d1d 106 $this->assertEquals('sTuff', $link['taglist'][0]);
89baf23d
A
107
108 // Test URL with external link.
c3dfd899 109 $this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $data['links'][8]['url']);
89baf23d
A
110
111 // Test multitags.
c3dfd899
A
112 $this->assertEquals(5, count($data['links'][6]['taglist']));
113 $this->assertEquals('css', $data['links'][6]['taglist'][0]);
c6d876bb
A
114
115 // Test update date
c3dfd899 116 $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['links'][8]['up_iso_date']);
89baf23d
A
117 }
118
119 /**
120 * Test buildData with ATOM feed (test only specific to ATOM).
121 */
122 public function testAtomBuildData()
123 {
e26e2060
A
124 $feedBuilder = new FeedBuilder(
125 self::$bookmarkService,
126 self::$formatter,
f4929b11 127 static::$serverInfo,
e26e2060
A
128 false
129 );
89baf23d 130 $feedBuilder->setLocale(self::$LOCALE);
f4929b11 131 $data = $feedBuilder->buildData(FeedBuilder::$FEED_ATOM, null);
89baf23d 132 $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
c6d876bb 133 $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['last_update']);
a8e210fa 134 $link = $data['links'][array_keys($data['links'])[0]];
c6d876bb 135 $this->assertRegExp('/2015-03-10T11:46:51\+\d{2}:\d{2}/', $link['pub_iso_date']);
c3dfd899 136 $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['links'][8]['up_iso_date']);
89baf23d
A
137 }
138
139 /**
140 * Test buildData with search criteria.
141 */
142 public function testBuildDataFiltered()
143 {
144 $criteria = array(
145 'searchtags' => 'stuff',
146 'searchterm' => 'beard',
147 );
e26e2060
A
148 $feedBuilder = new FeedBuilder(
149 self::$bookmarkService,
150 self::$formatter,
f4929b11 151 static::$serverInfo,
e26e2060
A
152 false
153 );
89baf23d 154 $feedBuilder->setLocale(self::$LOCALE);
f4929b11 155 $data = $feedBuilder->buildData(FeedBuilder::$FEED_ATOM, $criteria);
89baf23d
A
156 $this->assertEquals(1, count($data['links']));
157 $link = array_shift($data['links']);
c3dfd899 158 $this->assertEquals(41, $link['id']);
e26e2060 159 $this->assertEquals(DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114651'), $link['created']);
89baf23d
A
160 }
161
162 /**
163 * Test buildData with nb limit.
164 */
165 public function testBuildDataCount()
166 {
167 $criteria = array(
4154c25b 168 'nb' => '3',
89baf23d 169 );
e26e2060
A
170 $feedBuilder = new FeedBuilder(
171 self::$bookmarkService,
172 self::$formatter,
f4929b11 173 static::$serverInfo,
e26e2060
A
174 false
175 );
89baf23d 176 $feedBuilder->setLocale(self::$LOCALE);
f4929b11 177 $data = $feedBuilder->buildData(FeedBuilder::$FEED_ATOM, $criteria);
4154c25b 178 $this->assertEquals(3, count($data['links']));
a8e210fa 179 $link = $data['links'][array_keys($data['links'])[0]];
c3dfd899 180 $this->assertEquals(41, $link['id']);
e26e2060 181 $this->assertEquals(DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114651'), $link['created']);
89baf23d
A
182 }
183
184 /**
185 * Test buildData with permalinks on.
186 */
187 public function testBuildDataPermalinks()
188 {
e26e2060
A
189 $feedBuilder = new FeedBuilder(
190 self::$bookmarkService,
191 self::$formatter,
f4929b11 192 static::$serverInfo,
e26e2060
A
193 false
194 );
89baf23d
A
195 $feedBuilder->setLocale(self::$LOCALE);
196 $feedBuilder->setUsePermalinks(true);
f4929b11 197 $data = $feedBuilder->buildData(FeedBuilder::$FEED_ATOM, null);
89baf23d
A
198 $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
199 $this->assertTrue($data['usepermalinks']);
200 // First link is a permalink
a8e210fa 201 $link = $data['links'][array_keys($data['links'])[0]];
c3dfd899 202 $this->assertEquals(41, $link['id']);
e26e2060 203 $this->assertEquals(DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114651'), $link['created']);
f7f08cee
A
204 $this->assertEquals('http://host.tld/shaare/WDWyig', $link['guid']);
205 $this->assertEquals('http://host.tld/shaare/WDWyig', $link['url']);
a5a9cf23
A
206 $this->assertContainsPolyfill('Direct link', $link['description']);
207 $this->assertContainsPolyfill('http://host.tld/shaare/WDWyig', $link['description']);
89baf23d 208 // Second link is a direct link
a8e210fa 209 $link = $data['links'][array_keys($data['links'])[1]];
c3dfd899 210 $this->assertEquals(8, $link['id']);
e26e2060 211 $this->assertEquals(DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114633'), $link['created']);
f7f08cee 212 $this->assertEquals('http://host.tld/shaare/RttfEw', $link['guid']);
89baf23d 213 $this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['url']);
a5a9cf23
A
214 $this->assertContainsPolyfill('Direct link', $link['description']);
215 $this->assertContainsPolyfill('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['description']);
89baf23d
A
216 }
217
218 /**
219 * Test buildData with hide dates settings.
220 */
221 public function testBuildDataHideDates()
222 {
e26e2060
A
223 $feedBuilder = new FeedBuilder(
224 self::$bookmarkService,
225 self::$formatter,
f4929b11 226 static::$serverInfo,
e26e2060
A
227 false
228 );
89baf23d
A
229 $feedBuilder->setLocale(self::$LOCALE);
230 $feedBuilder->setHideDates(true);
f4929b11 231 $data = $feedBuilder->buildData(FeedBuilder::$FEED_ATOM, null);
89baf23d
A
232 $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
233 $this->assertFalse($data['show_dates']);
234
235 // Show dates while logged in
e26e2060
A
236 $feedBuilder = new FeedBuilder(
237 self::$bookmarkService,
238 self::$formatter,
f4929b11 239 static::$serverInfo,
e26e2060
A
240 true
241 );
89baf23d
A
242 $feedBuilder->setLocale(self::$LOCALE);
243 $feedBuilder->setHideDates(true);
f4929b11 244 $data = $feedBuilder->buildData(FeedBuilder::$FEED_ATOM, null);
89baf23d
A
245 $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
246 $this->assertTrue($data['show_dates']);
247 }
248
44a71809
V
249 /**
250 * Test buildData when Shaarli is served from a subdirectory
251 */
252 public function testBuildDataServerSubdir()
253 {
254 $serverInfo = array(
255 'HTTPS' => 'Off',
256 'SERVER_NAME' => 'host.tld',
257 'SERVER_PORT' => '8080',
258 'SCRIPT_NAME' => '/~user/shaarli/index.php',
b93cfeba 259 'REQUEST_URI' => '/~user/shaarli/feed/atom',
44a71809
V
260 );
261 $feedBuilder = new FeedBuilder(
e26e2060
A
262 self::$bookmarkService,
263 self::$formatter,
44a71809 264 $serverInfo,
44a71809
V
265 false
266 );
267 $feedBuilder->setLocale(self::$LOCALE);
f4929b11 268 $data = $feedBuilder->buildData(FeedBuilder::$FEED_ATOM, null);
44a71809
V
269
270 $this->assertEquals(
b93cfeba 271 'http://host.tld:8080/~user/shaarli/feed/atom',
44a71809
V
272 $data['self_link']
273 );
274
275 // Test first link (note link)
a8e210fa 276 $link = $data['links'][array_keys($data['links'])[0]];
f7f08cee
A
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']);
a5a9cf23 279 $this->assertContainsPolyfill('http://host.tld:8080/~user/shaarli/./add-tag/hashtag', $link['description']);
44a71809 280 }
89baf23d 281}