]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/FeedBuilderTest.php
Unit Test for the new ID system
[github/shaarli/Shaarli.git] / tests / FeedBuilderTest.php
CommitLineData
89baf23d
A
1<?php
2
3require_once 'application/FeedBuilder.php';
4require_once 'application/LinkDB.php';
5
6/**
7 * FeedBuilderTest class.
8 *
9 * Unit tests for FeedBuilder.
10 */
11class 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']);
c6d876bb 79 $this->assertRegExp('/Wed, 03 Aug 2016 09:30:33 \+\d{4}/', $data['last_update']);
89baf23d
A
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)
c3dfd899
A
87 $link = reset($data['links']);
88 $this->assertEquals(41, $link['id']);
89 $this->assertEquals(DateTime::createFromFormat('Ymd_His', '20150310_114651'), $link['created']);
89baf23d
A
90 $this->assertEquals('http://host.tld/?WDWyig', $link['guid']);
91 $this->assertEquals('http://host.tld/?WDWyig', $link['url']);
c6d876bb
A
92 $this->assertRegExp('/Tue, 10 Mar 2015 11:46:51 \+\d{4}/', $link['pub_iso_date']);
93 $pub = DateTime::createFromFormat(DateTime::RSS, $link['pub_iso_date']);
94 $up = DateTime::createFromFormat(DateTime::ATOM, $link['up_iso_date']);
95 $this->assertEquals($pub, $up);
89baf23d
A
96 $this->assertContains('Stallman has a beard', $link['description']);
97 $this->assertContains('Permalink', $link['description']);
98 $this->assertContains('http://host.tld/?WDWyig', $link['description']);
99 $this->assertEquals(1, count($link['taglist']));
b1eb5d1d 100 $this->assertEquals('sTuff', $link['taglist'][0]);
89baf23d
A
101
102 // Test URL with external link.
c3dfd899 103 $this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $data['links'][8]['url']);
89baf23d
A
104
105 // Test multitags.
c3dfd899
A
106 $this->assertEquals(5, count($data['links'][6]['taglist']));
107 $this->assertEquals('css', $data['links'][6]['taglist'][0]);
c6d876bb
A
108
109 // Test update date
c3dfd899 110 $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['links'][8]['up_iso_date']);
89baf23d
A
111 }
112
113 /**
114 * Test buildData with ATOM feed (test only specific to ATOM).
115 */
116 public function testAtomBuildData()
117 {
118 $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, false);
119 $feedBuilder->setLocale(self::$LOCALE);
120 $data = $feedBuilder->buildData();
121 $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
c6d876bb 122 $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['last_update']);
c3dfd899 123 $link = reset($data['links']);
c6d876bb 124 $this->assertRegExp('/2015-03-10T11:46:51\+\d{2}:\d{2}/', $link['pub_iso_date']);
c3dfd899 125 $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['links'][8]['up_iso_date']);
89baf23d
A
126 }
127
128 /**
129 * Test buildData with search criteria.
130 */
131 public function testBuildDataFiltered()
132 {
133 $criteria = array(
134 'searchtags' => 'stuff',
135 'searchterm' => 'beard',
136 );
137 $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, $criteria, false);
138 $feedBuilder->setLocale(self::$LOCALE);
139 $data = $feedBuilder->buildData();
140 $this->assertEquals(1, count($data['links']));
141 $link = array_shift($data['links']);
c3dfd899
A
142 $this->assertEquals(41, $link['id']);
143 $this->assertEquals(DateTime::createFromFormat('Ymd_His', '20150310_114651'), $link['created']);
89baf23d
A
144 }
145
146 /**
147 * Test buildData with nb limit.
148 */
149 public function testBuildDataCount()
150 {
151 $criteria = array(
152 'nb' => '1',
153 );
154 $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, $criteria, false);
155 $feedBuilder->setLocale(self::$LOCALE);
156 $data = $feedBuilder->buildData();
157 $this->assertEquals(1, count($data['links']));
158 $link = array_shift($data['links']);
c3dfd899
A
159 $this->assertEquals(41, $link['id']);
160 $this->assertEquals(DateTime::createFromFormat('Ymd_His', '20150310_114651'), $link['created']);
89baf23d
A
161 }
162
163 /**
164 * Test buildData with permalinks on.
165 */
166 public function testBuildDataPermalinks()
167 {
168 $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, false);
169 $feedBuilder->setLocale(self::$LOCALE);
170 $feedBuilder->setUsePermalinks(true);
171 $data = $feedBuilder->buildData();
172 $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
173 $this->assertTrue($data['usepermalinks']);
174 // First link is a permalink
175 $link = array_shift($data['links']);
c3dfd899
A
176 $this->assertEquals(41, $link['id']);
177 $this->assertEquals(DateTime::createFromFormat('Ymd_His', '20150310_114651'), $link['created']);
89baf23d
A
178 $this->assertEquals('http://host.tld/?WDWyig', $link['guid']);
179 $this->assertEquals('http://host.tld/?WDWyig', $link['url']);
180 $this->assertContains('Direct link', $link['description']);
181 $this->assertContains('http://host.tld/?WDWyig', $link['description']);
182 // Second link is a direct link
183 $link = array_shift($data['links']);
c3dfd899
A
184 $this->assertEquals(8, $link['id']);
185 $this->assertEquals(DateTime::createFromFormat('Ymd_His', '20150310_114633'), $link['created']);
186 $this->assertEquals('http://host.tld/?RttfEw', $link['guid']);
89baf23d
A
187 $this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['url']);
188 $this->assertContains('Direct link', $link['description']);
189 $this->assertContains('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['description']);
190 }
191
192 /**
193 * Test buildData with hide dates settings.
194 */
195 public function testBuildDataHideDates()
196 {
197 $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, false);
198 $feedBuilder->setLocale(self::$LOCALE);
199 $feedBuilder->setHideDates(true);
200 $data = $feedBuilder->buildData();
201 $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
202 $this->assertFalse($data['show_dates']);
203
204 // Show dates while logged in
205 $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, true);
206 $feedBuilder->setLocale(self::$LOCALE);
207 $feedBuilder->setHideDates(true);
208 $data = $feedBuilder->buildData();
209 $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
210 $this->assertTrue($data['show_dates']);
211 }
212
213 /**
214 * Test buildData with hide dates settings.
215 */
216 public function testBuildDataPubsubhub()
217 {
218 $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, false);
219 $feedBuilder->setLocale(self::$LOCALE);
220 $feedBuilder->setPubsubhubUrl('http://pubsubhub.io');
221 $data = $feedBuilder->buildData();
222 $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
223 $this->assertEquals('http://pubsubhub.io', $data['pubsubhub_url']);
224 }
44a71809
V
225
226 /**
227 * Test buildData when Shaarli is served from a subdirectory
228 */
229 public function testBuildDataServerSubdir()
230 {
231 $serverInfo = array(
232 'HTTPS' => 'Off',
233 'SERVER_NAME' => 'host.tld',
234 'SERVER_PORT' => '8080',
235 'SCRIPT_NAME' => '/~user/shaarli/index.php',
236 'REQUEST_URI' => '/~user/shaarli/index.php?do=feed',
237 );
238 $feedBuilder = new FeedBuilder(
239 self::$linkDB,
240 FeedBuilder::$FEED_ATOM,
241 $serverInfo,
242 null,
243 false
244 );
245 $feedBuilder->setLocale(self::$LOCALE);
246 $data = $feedBuilder->buildData();
247
248 $this->assertEquals(
249 'http://host.tld:8080/~user/shaarli/index.php?do=feed',
250 $data['self_link']
251 );
252
253 // Test first link (note link)
254 $link = array_shift($data['links']);
255 $this->assertEquals('http://host.tld:8080/~user/shaarli/?WDWyig', $link['guid']);
256 $this->assertEquals('http://host.tld:8080/~user/shaarli/?WDWyig', $link['url']);
fbc28ff1 257 $this->assertContains('http://host.tld:8080/~user/shaarli/?addtag=hashtag', $link['description']);
44a71809 258 }
89baf23d 259}