]>
Commit | Line | Data |
---|---|---|
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\TestCase; | |
15 | ||
16 | /** | |
17 | * FeedBuilderTest class. | |
18 | * | |
19 | * Unit tests for FeedBuilder. | |
20 | */ | |
21 | class FeedBuilderTest extends TestCase | |
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 | ||
40 | public static $bookmarkService; | |
41 | ||
42 | public static $formatter; | |
43 | ||
44 | public static $serverInfo; | |
45 | ||
46 | /** | |
47 | * Called before every test method. | |
48 | */ | |
49 | public static function setUpBeforeClass(): void | |
50 | { | |
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); | |
60 | ||
61 | self::$serverInfo = array( | |
62 | 'HTTPS' => 'Off', | |
63 | 'SERVER_NAME' => 'host.tld', | |
64 | 'SERVER_PORT' => '80', | |
65 | 'SCRIPT_NAME' => '/index.php', | |
66 | 'REQUEST_URI' => '/feed/atom', | |
67 | ); | |
68 | } | |
69 | ||
70 | /** | |
71 | * Test buildData with RSS feed. | |
72 | */ | |
73 | public function testRSSBuildData() | |
74 | { | |
75 | $feedBuilder = new FeedBuilder( | |
76 | self::$bookmarkService, | |
77 | self::$formatter, | |
78 | static::$serverInfo, | |
79 | false | |
80 | ); | |
81 | $feedBuilder->setLocale(self::$LOCALE); | |
82 | $data = $feedBuilder->buildData(FeedBuilder::$FEED_RSS, null); | |
83 | // Test headers (RSS) | |
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'])); | |
91 | ||
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]); | |
107 | ||
108 | // Test URL with external link. | |
109 | $this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $data['links'][8]['url']); | |
110 | ||
111 | // Test multitags. | |
112 | $this->assertEquals(5, count($data['links'][6]['taglist'])); | |
113 | $this->assertEquals('css', $data['links'][6]['taglist'][0]); | |
114 | ||
115 | // Test update date | |
116 | $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['links'][8]['up_iso_date']); | |
117 | } | |
118 | ||
119 | /** | |
120 | * Test buildData with ATOM feed (test only specific to ATOM). | |
121 | */ | |
122 | public function testAtomBuildData() | |
123 | { | |
124 | $feedBuilder = new FeedBuilder( | |
125 | self::$bookmarkService, | |
126 | self::$formatter, | |
127 | static::$serverInfo, | |
128 | false | |
129 | ); | |
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']); | |
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 | ); | |
148 | $feedBuilder = new FeedBuilder( | |
149 | self::$bookmarkService, | |
150 | self::$formatter, | |
151 | static::$serverInfo, | |
152 | false | |
153 | ); | |
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']); | |
160 | } | |
161 | ||
162 | /** | |
163 | * Test buildData with nb limit. | |
164 | */ | |
165 | public function testBuildDataCount() | |
166 | { | |
167 | $criteria = array( | |
168 | 'nb' => '3', | |
169 | ); | |
170 | $feedBuilder = new FeedBuilder( | |
171 | self::$bookmarkService, | |
172 | self::$formatter, | |
173 | static::$serverInfo, | |
174 | false | |
175 | ); | |
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']); | |
182 | } | |
183 | ||
184 | /** | |
185 | * Test buildData with permalinks on. | |
186 | */ | |
187 | public function testBuildDataPermalinks() | |
188 | { | |
189 | $feedBuilder = new FeedBuilder( | |
190 | self::$bookmarkService, | |
191 | self::$formatter, | |
192 | static::$serverInfo, | |
193 | false | |
194 | ); | |
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']); | |
216 | } | |
217 | ||
218 | /** | |
219 | * Test buildData with hide dates settings. | |
220 | */ | |
221 | public function testBuildDataHideDates() | |
222 | { | |
223 | $feedBuilder = new FeedBuilder( | |
224 | self::$bookmarkService, | |
225 | self::$formatter, | |
226 | static::$serverInfo, | |
227 | false | |
228 | ); | |
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']); | |
234 | ||
235 | // Show dates while logged in | |
236 | $feedBuilder = new FeedBuilder( | |
237 | self::$bookmarkService, | |
238 | self::$formatter, | |
239 | static::$serverInfo, | |
240 | true | |
241 | ); | |
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']); | |
247 | } | |
248 | ||
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', | |
259 | 'REQUEST_URI' => '/~user/shaarli/feed/atom', | |
260 | ); | |
261 | $feedBuilder = new FeedBuilder( | |
262 | self::$bookmarkService, | |
263 | self::$formatter, | |
264 | $serverInfo, | |
265 | false | |
266 | ); | |
267 | $feedBuilder->setLocale(self::$LOCALE); | |
268 | $data = $feedBuilder->buildData(FeedBuilder::$FEED_ATOM, null); | |
269 | ||
270 | $this->assertEquals( | |
271 | 'http://host.tld:8080/~user/shaarli/feed/atom', | |
272 | $data['self_link'] | |
273 | ); | |
274 | ||
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']); | |
280 | } | |
281 | } |