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