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