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