3 namespace Shaarli\Feed
;
7 use Shaarli\Bookmark\LinkDB
;
10 * FeedBuilderTest class.
12 * Unit tests for FeedBuilder.
14 class FeedBuilderTest
extends \PHPUnit\Framework\TestCase
17 * @var string locale Basque (Spain).
19 public static $LOCALE = 'eu_ES';
22 * @var string language in RSS format.
24 public static $RSS_LANGUAGE = 'eu-es';
27 * @var string language in ATOM format.
29 public static $ATOM_LANGUAGUE = 'eu';
31 protected static $testDatastore = 'sandbox/datastore.php';
33 public static $linkDB;
35 public static $serverInfo;
38 * Called before every test method.
40 public static function setUpBeforeClass()
42 $refLinkDB = new ReferenceLinkDB();
43 $refLinkDB->write(self
::$testDatastore);
44 self
::$linkDB = new LinkDB(self
::$testDatastore, true, false);
45 self
::$serverInfo = array(
47 'SERVER_NAME' => 'host.tld',
48 'SERVER_PORT' => '80',
49 'SCRIPT_NAME' => '/index.php',
50 'REQUEST_URI' => '/index.php?do=feed',
55 * Test GetTypeLanguage().
57 public function testGetTypeLanguage()
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());
72 * Test buildData with RSS feed.
74 public function testRSSBuildData()
76 $feedBuilder = new FeedBuilder(self
::$linkDB, FeedBuilder
::$FEED_RSS, self
::$serverInfo, null, false);
77 $feedBuilder->setLocale(self
::$LOCALE);
78 $data = $feedBuilder->buildData();
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']));
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]);
104 // Test URL with external link.
105 $this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $data['links'][8]['url']);
108 $this->assertEquals(5, count($data['links'][6]['taglist']));
109 $this->assertEquals('css', $data['links'][6]['taglist'][0]);
112 $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['links'][8]['up_iso_date']);
116 * Test buildData with ATOM feed (test only specific to ATOM).
118 public function testAtomBuildData()
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']);
131 * Test buildData with search criteria.
133 public function testBuildDataFiltered()
136 'searchtags' => 'stuff',
137 'searchterm' => 'beard',
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']);
149 * Test buildData with nb limit.
151 public function testBuildDataCount()
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']);
166 * Test buildData with permalinks on.
168 public function testBuildDataPermalinks()
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']);
195 * Test buildData with hide dates settings.
197 public function testBuildDataHideDates()
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']);
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']);
216 * Test buildData when Shaarli is served from a subdirectory
218 public function testBuildDataServerSubdir()
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',
227 $feedBuilder = new FeedBuilder(
229 FeedBuilder
::$FEED_ATOM,
234 $feedBuilder->setLocale(self
::$LOCALE);
235 $data = $feedBuilder->buildData();
238 'http://host.tld:8080/~user/shaarli/index.php?do=feed',
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']);