]>
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 | ||
89baf23d A |
67 | /** |
68 | * Test buildData with RSS feed. | |
69 | */ | |
70 | public function testRSSBuildData() | |
71 | { | |
e26e2060 A |
72 | $feedBuilder = new FeedBuilder( |
73 | self::$bookmarkService, | |
74 | self::$formatter, | |
f4929b11 | 75 | static::$serverInfo, |
e26e2060 A |
76 | false |
77 | ); | |
89baf23d | 78 | $feedBuilder->setLocale(self::$LOCALE); |
f4929b11 | 79 | $data = $feedBuilder->buildData(FeedBuilder::$FEED_RSS, null); |
89baf23d A |
80 | // Test headers (RSS) |
81 | $this->assertEquals(self::$RSS_LANGUAGE, $data['language']); | |
c6d876bb | 82 | $this->assertRegExp('/Wed, 03 Aug 2016 09:30:33 \+\d{4}/', $data['last_update']); |
89baf23d A |
83 | $this->assertEquals(true, $data['show_dates']); |
84 | $this->assertEquals('http://host.tld/index.php?do=feed', $data['self_link']); | |
85 | $this->assertEquals('http://host.tld/', $data['index_url']); | |
86 | $this->assertFalse($data['usepermalinks']); | |
87 | $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links'])); | |
88 | ||
4154c25b | 89 | // Test first not pinned link (note link) |
a8e210fa | 90 | $link = $data['links'][array_keys($data['links'])[0]]; |
c3dfd899 | 91 | $this->assertEquals(41, $link['id']); |
e26e2060 | 92 | $this->assertEquals(DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114651'), $link['created']); |
f7f08cee A |
93 | $this->assertEquals('http://host.tld/shaare/WDWyig', $link['guid']); |
94 | $this->assertEquals('http://host.tld/shaare/WDWyig', $link['url']); | |
c6d876bb A |
95 | $this->assertRegExp('/Tue, 10 Mar 2015 11:46:51 \+\d{4}/', $link['pub_iso_date']); |
96 | $pub = DateTime::createFromFormat(DateTime::RSS, $link['pub_iso_date']); | |
dfc650aa | 97 | $up = DateTime::createFromFormat(DateTime::ATOM, $link['up_iso_date']); |
c6d876bb | 98 | $this->assertEquals($pub, $up); |
89baf23d A |
99 | $this->assertContains('Stallman has a beard', $link['description']); |
100 | $this->assertContains('Permalink', $link['description']); | |
f7f08cee | 101 | $this->assertContains('http://host.tld/shaare/WDWyig', $link['description']); |
89baf23d | 102 | $this->assertEquals(1, count($link['taglist'])); |
b1eb5d1d | 103 | $this->assertEquals('sTuff', $link['taglist'][0]); |
89baf23d A |
104 | |
105 | // Test URL with external link. | |
c3dfd899 | 106 | $this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $data['links'][8]['url']); |
89baf23d A |
107 | |
108 | // Test multitags. | |
c3dfd899 A |
109 | $this->assertEquals(5, count($data['links'][6]['taglist'])); |
110 | $this->assertEquals('css', $data['links'][6]['taglist'][0]); | |
c6d876bb A |
111 | |
112 | // Test update date | |
c3dfd899 | 113 | $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['links'][8]['up_iso_date']); |
89baf23d A |
114 | } |
115 | ||
116 | /** | |
117 | * Test buildData with ATOM feed (test only specific to ATOM). | |
118 | */ | |
119 | public function testAtomBuildData() | |
120 | { | |
e26e2060 A |
121 | $feedBuilder = new FeedBuilder( |
122 | self::$bookmarkService, | |
123 | self::$formatter, | |
f4929b11 | 124 | static::$serverInfo, |
e26e2060 A |
125 | false |
126 | ); | |
89baf23d | 127 | $feedBuilder->setLocale(self::$LOCALE); |
f4929b11 | 128 | $data = $feedBuilder->buildData(FeedBuilder::$FEED_ATOM, null); |
89baf23d | 129 | $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links'])); |
c6d876bb | 130 | $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['last_update']); |
a8e210fa | 131 | $link = $data['links'][array_keys($data['links'])[0]]; |
c6d876bb | 132 | $this->assertRegExp('/2015-03-10T11:46:51\+\d{2}:\d{2}/', $link['pub_iso_date']); |
c3dfd899 | 133 | $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['links'][8]['up_iso_date']); |
89baf23d A |
134 | } |
135 | ||
136 | /** | |
137 | * Test buildData with search criteria. | |
138 | */ | |
139 | public function testBuildDataFiltered() | |
140 | { | |
141 | $criteria = array( | |
142 | 'searchtags' => 'stuff', | |
143 | 'searchterm' => 'beard', | |
144 | ); | |
e26e2060 A |
145 | $feedBuilder = new FeedBuilder( |
146 | self::$bookmarkService, | |
147 | self::$formatter, | |
f4929b11 | 148 | static::$serverInfo, |
e26e2060 A |
149 | false |
150 | ); | |
89baf23d | 151 | $feedBuilder->setLocale(self::$LOCALE); |
f4929b11 | 152 | $data = $feedBuilder->buildData(FeedBuilder::$FEED_ATOM, $criteria); |
89baf23d A |
153 | $this->assertEquals(1, count($data['links'])); |
154 | $link = array_shift($data['links']); | |
c3dfd899 | 155 | $this->assertEquals(41, $link['id']); |
e26e2060 | 156 | $this->assertEquals(DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114651'), $link['created']); |
89baf23d A |
157 | } |
158 | ||
159 | /** | |
160 | * Test buildData with nb limit. | |
161 | */ | |
162 | public function testBuildDataCount() | |
163 | { | |
164 | $criteria = array( | |
4154c25b | 165 | 'nb' => '3', |
89baf23d | 166 | ); |
e26e2060 A |
167 | $feedBuilder = new FeedBuilder( |
168 | self::$bookmarkService, | |
169 | self::$formatter, | |
f4929b11 | 170 | static::$serverInfo, |
e26e2060 A |
171 | false |
172 | ); | |
89baf23d | 173 | $feedBuilder->setLocale(self::$LOCALE); |
f4929b11 | 174 | $data = $feedBuilder->buildData(FeedBuilder::$FEED_ATOM, $criteria); |
4154c25b | 175 | $this->assertEquals(3, count($data['links'])); |
a8e210fa | 176 | $link = $data['links'][array_keys($data['links'])[0]]; |
c3dfd899 | 177 | $this->assertEquals(41, $link['id']); |
e26e2060 | 178 | $this->assertEquals(DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114651'), $link['created']); |
89baf23d A |
179 | } |
180 | ||
181 | /** | |
182 | * Test buildData with permalinks on. | |
183 | */ | |
184 | public function testBuildDataPermalinks() | |
185 | { | |
e26e2060 A |
186 | $feedBuilder = new FeedBuilder( |
187 | self::$bookmarkService, | |
188 | self::$formatter, | |
f4929b11 | 189 | static::$serverInfo, |
e26e2060 A |
190 | false |
191 | ); | |
89baf23d A |
192 | $feedBuilder->setLocale(self::$LOCALE); |
193 | $feedBuilder->setUsePermalinks(true); | |
f4929b11 | 194 | $data = $feedBuilder->buildData(FeedBuilder::$FEED_ATOM, null); |
89baf23d A |
195 | $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links'])); |
196 | $this->assertTrue($data['usepermalinks']); | |
197 | // First link is a permalink | |
a8e210fa | 198 | $link = $data['links'][array_keys($data['links'])[0]]; |
c3dfd899 | 199 | $this->assertEquals(41, $link['id']); |
e26e2060 | 200 | $this->assertEquals(DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114651'), $link['created']); |
f7f08cee A |
201 | $this->assertEquals('http://host.tld/shaare/WDWyig', $link['guid']); |
202 | $this->assertEquals('http://host.tld/shaare/WDWyig', $link['url']); | |
89baf23d | 203 | $this->assertContains('Direct link', $link['description']); |
f7f08cee | 204 | $this->assertContains('http://host.tld/shaare/WDWyig', $link['description']); |
89baf23d | 205 | // Second link is a direct link |
a8e210fa | 206 | $link = $data['links'][array_keys($data['links'])[1]]; |
c3dfd899 | 207 | $this->assertEquals(8, $link['id']); |
e26e2060 | 208 | $this->assertEquals(DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114633'), $link['created']); |
f7f08cee | 209 | $this->assertEquals('http://host.tld/shaare/RttfEw', $link['guid']); |
89baf23d A |
210 | $this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['url']); |
211 | $this->assertContains('Direct link', $link['description']); | |
212 | $this->assertContains('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['description']); | |
213 | } | |
214 | ||
215 | /** | |
216 | * Test buildData with hide dates settings. | |
217 | */ | |
218 | public function testBuildDataHideDates() | |
219 | { | |
e26e2060 A |
220 | $feedBuilder = new FeedBuilder( |
221 | self::$bookmarkService, | |
222 | self::$formatter, | |
f4929b11 | 223 | static::$serverInfo, |
e26e2060 A |
224 | false |
225 | ); | |
89baf23d A |
226 | $feedBuilder->setLocale(self::$LOCALE); |
227 | $feedBuilder->setHideDates(true); | |
f4929b11 | 228 | $data = $feedBuilder->buildData(FeedBuilder::$FEED_ATOM, null); |
89baf23d A |
229 | $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links'])); |
230 | $this->assertFalse($data['show_dates']); | |
231 | ||
232 | // Show dates while logged in | |
e26e2060 A |
233 | $feedBuilder = new FeedBuilder( |
234 | self::$bookmarkService, | |
235 | self::$formatter, | |
f4929b11 | 236 | static::$serverInfo, |
e26e2060 A |
237 | true |
238 | ); | |
89baf23d A |
239 | $feedBuilder->setLocale(self::$LOCALE); |
240 | $feedBuilder->setHideDates(true); | |
f4929b11 | 241 | $data = $feedBuilder->buildData(FeedBuilder::$FEED_ATOM, null); |
89baf23d A |
242 | $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links'])); |
243 | $this->assertTrue($data['show_dates']); | |
244 | } | |
245 | ||
44a71809 V |
246 | /** |
247 | * Test buildData when Shaarli is served from a subdirectory | |
248 | */ | |
249 | public function testBuildDataServerSubdir() | |
250 | { | |
251 | $serverInfo = array( | |
252 | 'HTTPS' => 'Off', | |
253 | 'SERVER_NAME' => 'host.tld', | |
254 | 'SERVER_PORT' => '8080', | |
255 | 'SCRIPT_NAME' => '/~user/shaarli/index.php', | |
256 | 'REQUEST_URI' => '/~user/shaarli/index.php?do=feed', | |
257 | ); | |
258 | $feedBuilder = new FeedBuilder( | |
e26e2060 A |
259 | self::$bookmarkService, |
260 | self::$formatter, | |
44a71809 | 261 | $serverInfo, |
44a71809 V |
262 | false |
263 | ); | |
264 | $feedBuilder->setLocale(self::$LOCALE); | |
f4929b11 | 265 | $data = $feedBuilder->buildData(FeedBuilder::$FEED_ATOM, null); |
44a71809 V |
266 | |
267 | $this->assertEquals( | |
268 | 'http://host.tld:8080/~user/shaarli/index.php?do=feed', | |
269 | $data['self_link'] | |
270 | ); | |
271 | ||
272 | // Test first link (note link) | |
a8e210fa | 273 | $link = $data['links'][array_keys($data['links'])[0]]; |
f7f08cee A |
274 | $this->assertEquals('http://host.tld:8080/~user/shaarli/shaare/WDWyig', $link['guid']); |
275 | $this->assertEquals('http://host.tld:8080/~user/shaarli/shaare/WDWyig', $link['url']); | |
03340c18 | 276 | $this->assertContains('http://host.tld:8080/~user/shaarli/./add-tag/hashtag', $link['description']); |
44a71809 | 277 | } |
89baf23d | 278 | } |