aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/feed/FeedBuilderTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'tests/feed/FeedBuilderTest.php')
-rw-r--r--tests/feed/FeedBuilderTest.php248
1 files changed, 248 insertions, 0 deletions
diff --git a/tests/feed/FeedBuilderTest.php b/tests/feed/FeedBuilderTest.php
new file mode 100644
index 00000000..b496cb4c
--- /dev/null
+++ b/tests/feed/FeedBuilderTest.php
@@ -0,0 +1,248 @@
1<?php
2
3namespace Shaarli\Feed;
4
5use DateTime;
6use ReferenceLinkDB;
7use Shaarli\Bookmark\LinkDB;
8
9/**
10 * FeedBuilderTest class.
11 *
12 * Unit tests for FeedBuilder.
13 */
14class FeedBuilderTest extends \PHPUnit\Framework\TestCase
15{
16 /**
17 * @var string locale Basque (Spain).
18 */
19 public static $LOCALE = 'eu_ES';
20
21 /**
22 * @var string language in RSS format.
23 */
24 public static $RSS_LANGUAGE = 'eu-es';
25
26 /**
27 * @var string language in ATOM format.
28 */
29 public static $ATOM_LANGUAGUE = 'eu';
30
31 protected static $testDatastore = 'sandbox/datastore.php';
32
33 public static $linkDB;
34
35 public static $serverInfo;
36
37 /**
38 * Called before every test method.
39 */
40 public static function setUpBeforeClass()
41 {
42 $refLinkDB = new ReferenceLinkDB();
43 $refLinkDB->write(self::$testDatastore);
44 self::$linkDB = new LinkDB(self::$testDatastore, true, false);
45 self::$serverInfo = array(
46 'HTTPS' => 'Off',
47 'SERVER_NAME' => 'host.tld',
48 'SERVER_PORT' => '80',
49 'SCRIPT_NAME' => '/index.php',
50 'REQUEST_URI' => '/index.php?do=feed',
51 );
52 }
53
54 /**
55 * Test GetTypeLanguage().
56 */
57 public function testGetTypeLanguage()
58 {
59 $feedBuilder = new FeedBuilder(null, FeedBuilder::$FEED_ATOM, null, null, false);
60 $feedBuilder->setLocale(self::$LOCALE);
61 $this->assertEquals(self::$ATOM_LANGUAGUE, $feedBuilder->getTypeLanguage());
62 $feedBuilder = new FeedBuilder(null, FeedBuilder::$FEED_RSS, null, null, false);
63 $feedBuilder->setLocale(self::$LOCALE);
64 $this->assertEquals(self::$RSS_LANGUAGE, $feedBuilder->getTypeLanguage());
65 $feedBuilder = new FeedBuilder(null, FeedBuilder::$FEED_ATOM, null, null, false);
66 $this->assertEquals('en', $feedBuilder->getTypeLanguage());
67 $feedBuilder = new FeedBuilder(null, FeedBuilder::$FEED_RSS, null, null, false);
68 $this->assertEquals('en-en', $feedBuilder->getTypeLanguage());
69 }
70
71 /**
72 * Test buildData with RSS feed.
73 */
74 public function testRSSBuildData()
75 {
76 $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_RSS, self::$serverInfo, null, false);
77 $feedBuilder->setLocale(self::$LOCALE);
78 $data = $feedBuilder->buildData();
79 // Test headers (RSS)
80 $this->assertEquals(self::$RSS_LANGUAGE, $data['language']);
81 $this->assertRegExp('/Wed, 03 Aug 2016 09:30:33 \+\d{4}/', $data['last_update']);
82 $this->assertEquals(true, $data['show_dates']);
83 $this->assertEquals('http://host.tld/index.php?do=feed', $data['self_link']);
84 $this->assertEquals('http://host.tld/', $data['index_url']);
85 $this->assertFalse($data['usepermalinks']);
86 $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
87
88 // Test first not pinned link (note link)
89 $link = $data['links'][array_keys($data['links'])[2]];
90 $this->assertEquals(41, $link['id']);
91 $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114651'), $link['created']);
92 $this->assertEquals('http://host.tld/?WDWyig', $link['guid']);
93 $this->assertEquals('http://host.tld/?WDWyig', $link['url']);
94 $this->assertRegExp('/Tue, 10 Mar 2015 11:46:51 \+\d{4}/', $link['pub_iso_date']);
95 $pub = DateTime::createFromFormat(DateTime::RSS, $link['pub_iso_date']);
96 $up = DateTime::createFromFormat(DateTime::ATOM, $link['up_iso_date']);
97 $this->assertEquals($pub, $up);
98 $this->assertContains('Stallman has a beard', $link['description']);
99 $this->assertContains('Permalink', $link['description']);
100 $this->assertContains('http://host.tld/?WDWyig', $link['description']);
101 $this->assertEquals(1, count($link['taglist']));
102 $this->assertEquals('sTuff', $link['taglist'][0]);
103
104 // Test URL with external link.
105 $this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $data['links'][8]['url']);
106
107 // Test multitags.
108 $this->assertEquals(5, count($data['links'][6]['taglist']));
109 $this->assertEquals('css', $data['links'][6]['taglist'][0]);
110
111 // Test update date
112 $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['links'][8]['up_iso_date']);
113 }
114
115 /**
116 * Test buildData with ATOM feed (test only specific to ATOM).
117 */
118 public function testAtomBuildData()
119 {
120 $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, false);
121 $feedBuilder->setLocale(self::$LOCALE);
122 $data = $feedBuilder->buildData();
123 $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
124 $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['last_update']);
125 $link = $data['links'][array_keys($data['links'])[2]];
126 $this->assertRegExp('/2015-03-10T11:46:51\+\d{2}:\d{2}/', $link['pub_iso_date']);
127 $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['links'][8]['up_iso_date']);
128 }
129
130 /**
131 * Test buildData with search criteria.
132 */
133 public function testBuildDataFiltered()
134 {
135 $criteria = array(
136 'searchtags' => 'stuff',
137 'searchterm' => 'beard',
138 );
139 $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, $criteria, false);
140 $feedBuilder->setLocale(self::$LOCALE);
141 $data = $feedBuilder->buildData();
142 $this->assertEquals(1, count($data['links']));
143 $link = array_shift($data['links']);
144 $this->assertEquals(41, $link['id']);
145 $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114651'), $link['created']);
146 }
147
148 /**
149 * Test buildData with nb limit.
150 */
151 public function testBuildDataCount()
152 {
153 $criteria = array(
154 'nb' => '3',
155 );
156 $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, $criteria, false);
157 $feedBuilder->setLocale(self::$LOCALE);
158 $data = $feedBuilder->buildData();
159 $this->assertEquals(3, count($data['links']));
160 $link = $data['links'][array_keys($data['links'])[2]];
161 $this->assertEquals(41, $link['id']);
162 $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114651'), $link['created']);
163 }
164
165 /**
166 * Test buildData with permalinks on.
167 */
168 public function testBuildDataPermalinks()
169 {
170 $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, false);
171 $feedBuilder->setLocale(self::$LOCALE);
172 $feedBuilder->setUsePermalinks(true);
173 $data = $feedBuilder->buildData();
174 $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
175 $this->assertTrue($data['usepermalinks']);
176 // First link is a permalink
177 $link = $data['links'][array_keys($data['links'])[2]];
178 $this->assertEquals(41, $link['id']);
179 $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114651'), $link['created']);
180 $this->assertEquals('http://host.tld/?WDWyig', $link['guid']);
181 $this->assertEquals('http://host.tld/?WDWyig', $link['url']);
182 $this->assertContains('Direct link', $link['description']);
183 $this->assertContains('http://host.tld/?WDWyig', $link['description']);
184 // Second link is a direct link
185 $link = $data['links'][array_keys($data['links'])[3]];
186 $this->assertEquals(8, $link['id']);
187 $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114633'), $link['created']);
188 $this->assertEquals('http://host.tld/?RttfEw', $link['guid']);
189 $this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['url']);
190 $this->assertContains('Direct link', $link['description']);
191 $this->assertContains('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['description']);
192 }
193
194 /**
195 * Test buildData with hide dates settings.
196 */
197 public function testBuildDataHideDates()
198 {
199 $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, false);
200 $feedBuilder->setLocale(self::$LOCALE);
201 $feedBuilder->setHideDates(true);
202 $data = $feedBuilder->buildData();
203 $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
204 $this->assertFalse($data['show_dates']);
205
206 // Show dates while logged in
207 $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, true);
208 $feedBuilder->setLocale(self::$LOCALE);
209 $feedBuilder->setHideDates(true);
210 $data = $feedBuilder->buildData();
211 $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
212 $this->assertTrue($data['show_dates']);
213 }
214
215 /**
216 * Test buildData when Shaarli is served from a subdirectory
217 */
218 public function testBuildDataServerSubdir()
219 {
220 $serverInfo = array(
221 'HTTPS' => 'Off',
222 'SERVER_NAME' => 'host.tld',
223 'SERVER_PORT' => '8080',
224 'SCRIPT_NAME' => '/~user/shaarli/index.php',
225 'REQUEST_URI' => '/~user/shaarli/index.php?do=feed',
226 );
227 $feedBuilder = new FeedBuilder(
228 self::$linkDB,
229 FeedBuilder::$FEED_ATOM,
230 $serverInfo,
231 null,
232 false
233 );
234 $feedBuilder->setLocale(self::$LOCALE);
235 $data = $feedBuilder->buildData();
236
237 $this->assertEquals(
238 'http://host.tld:8080/~user/shaarli/index.php?do=feed',
239 $data['self_link']
240 );
241
242 // Test first link (note link)
243 $link = $data['links'][array_keys($data['links'])[2]];
244 $this->assertEquals('http://host.tld:8080/~user/shaarli/?WDWyig', $link['guid']);
245 $this->assertEquals('http://host.tld:8080/~user/shaarli/?WDWyig', $link['url']);
246 $this->assertContains('http://host.tld:8080/~user/shaarli/?addtag=hashtag', $link['description']);
247 }
248}