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