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