]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/FeedBuilderTest.php
Merge pull request #1234 from virtualtam/lint
[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']);
c6d876bb 78 $this->assertRegExp('/Wed, 03 Aug 2016 09:30:33 \+\d{4}/', $data['last_update']);
89baf23d
A
79 $this->assertEquals(true, $data['show_dates']);
80 $this->assertEquals('http://host.tld/index.php?do=feed', $data['self_link']);
81 $this->assertEquals('http://host.tld/', $data['index_url']);
82 $this->assertFalse($data['usepermalinks']);
83 $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
84
4154c25b
A
85 // Test first not pinned link (note link)
86 $link = $data['links'][array_keys($data['links'])[2]];
c3dfd899 87 $this->assertEquals(41, $link['id']);
d592daea 88 $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114651'), $link['created']);
89baf23d
A
89 $this->assertEquals('http://host.tld/?WDWyig', $link['guid']);
90 $this->assertEquals('http://host.tld/?WDWyig', $link['url']);
c6d876bb
A
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);
89baf23d
A
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']));
b1eb5d1d 99 $this->assertEquals('sTuff', $link['taglist'][0]);
89baf23d
A
100
101 // Test URL with external link.
c3dfd899 102 $this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $data['links'][8]['url']);
89baf23d
A
103
104 // Test multitags.
c3dfd899
A
105 $this->assertEquals(5, count($data['links'][6]['taglist']));
106 $this->assertEquals('css', $data['links'][6]['taglist'][0]);
c6d876bb
A
107
108 // Test update date
c3dfd899 109 $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['links'][8]['up_iso_date']);
89baf23d
A
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']));
c6d876bb 121 $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['last_update']);
4154c25b 122 $link = $data['links'][array_keys($data['links'])[2]];
c6d876bb 123 $this->assertRegExp('/2015-03-10T11:46:51\+\d{2}:\d{2}/', $link['pub_iso_date']);
c3dfd899 124 $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['links'][8]['up_iso_date']);
89baf23d
A
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']);
c3dfd899 141 $this->assertEquals(41, $link['id']);
d592daea 142 $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114651'), $link['created']);
89baf23d
A
143 }
144
145 /**
146 * Test buildData with nb limit.
147 */
148 public function testBuildDataCount()
149 {
150 $criteria = array(
4154c25b 151 'nb' => '3',
89baf23d
A
152 );
153 $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, $criteria, false);
154 $feedBuilder->setLocale(self::$LOCALE);
155 $data = $feedBuilder->buildData();
4154c25b
A
156 $this->assertEquals(3, count($data['links']));
157 $link = $data['links'][array_keys($data['links'])[2]];
c3dfd899 158 $this->assertEquals(41, $link['id']);
d592daea 159 $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114651'), $link['created']);
89baf23d
A
160 }
161
162 /**
163 * Test buildData with permalinks on.
164 */
165 public function testBuildDataPermalinks()
166 {
167 $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, false);
168 $feedBuilder->setLocale(self::$LOCALE);
169 $feedBuilder->setUsePermalinks(true);
170 $data = $feedBuilder->buildData();
171 $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
172 $this->assertTrue($data['usepermalinks']);
173 // First link is a permalink
4154c25b 174 $link = $data['links'][array_keys($data['links'])[2]];
c3dfd899 175 $this->assertEquals(41, $link['id']);
d592daea 176 $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114651'), $link['created']);
89baf23d
A
177 $this->assertEquals('http://host.tld/?WDWyig', $link['guid']);
178 $this->assertEquals('http://host.tld/?WDWyig', $link['url']);
179 $this->assertContains('Direct link', $link['description']);
180 $this->assertContains('http://host.tld/?WDWyig', $link['description']);
181 // Second link is a direct link
4154c25b 182 $link = $data['links'][array_keys($data['links'])[3]];
c3dfd899 183 $this->assertEquals(8, $link['id']);
d592daea 184 $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114633'), $link['created']);
c3dfd899 185 $this->assertEquals('http://host.tld/?RttfEw', $link['guid']);
89baf23d
A
186 $this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['url']);
187 $this->assertContains('Direct link', $link['description']);
188 $this->assertContains('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['description']);
189 }
190
191 /**
192 * Test buildData with hide dates settings.
193 */
194 public function testBuildDataHideDates()
195 {
196 $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, false);
197 $feedBuilder->setLocale(self::$LOCALE);
198 $feedBuilder->setHideDates(true);
199 $data = $feedBuilder->buildData();
200 $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
201 $this->assertFalse($data['show_dates']);
202
203 // Show dates while logged in
204 $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, true);
205 $feedBuilder->setLocale(self::$LOCALE);
206 $feedBuilder->setHideDates(true);
207 $data = $feedBuilder->buildData();
208 $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
209 $this->assertTrue($data['show_dates']);
210 }
211
44a71809
V
212 /**
213 * Test buildData when Shaarli is served from a subdirectory
214 */
215 public function testBuildDataServerSubdir()
216 {
217 $serverInfo = array(
218 'HTTPS' => 'Off',
219 'SERVER_NAME' => 'host.tld',
220 'SERVER_PORT' => '8080',
221 'SCRIPT_NAME' => '/~user/shaarli/index.php',
222 'REQUEST_URI' => '/~user/shaarli/index.php?do=feed',
223 );
224 $feedBuilder = new FeedBuilder(
225 self::$linkDB,
226 FeedBuilder::$FEED_ATOM,
227 $serverInfo,
228 null,
229 false
230 );
231 $feedBuilder->setLocale(self::$LOCALE);
232 $data = $feedBuilder->buildData();
233
234 $this->assertEquals(
235 'http://host.tld:8080/~user/shaarli/index.php?do=feed',
236 $data['self_link']
237 );
238
239 // Test first link (note link)
4154c25b 240 $link = $data['links'][array_keys($data['links'])[2]];
44a71809
V
241 $this->assertEquals('http://host.tld:8080/~user/shaarli/?WDWyig', $link['guid']);
242 $this->assertEquals('http://host.tld:8080/~user/shaarli/?WDWyig', $link['url']);
fbc28ff1 243 $this->assertContains('http://host.tld:8080/~user/shaarli/?addtag=hashtag', $link['description']);
44a71809 244 }
89baf23d 245}