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