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