aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/feed/FeedBuilderTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'tests/feed/FeedBuilderTest.php')
-rw-r--r--tests/feed/FeedBuilderTest.php122
1 files changed, 46 insertions, 76 deletions
diff --git a/tests/feed/FeedBuilderTest.php b/tests/feed/FeedBuilderTest.php
index 54671891..6b9204eb 100644
--- a/tests/feed/FeedBuilderTest.php
+++ b/tests/feed/FeedBuilderTest.php
@@ -3,6 +3,7 @@
3namespace Shaarli\Feed; 3namespace Shaarli\Feed;
4 4
5use DateTime; 5use DateTime;
6use malkusch\lock\mutex\NoMutex;
6use ReferenceLinkDB; 7use ReferenceLinkDB;
7use Shaarli\Bookmark\Bookmark; 8use Shaarli\Bookmark\Bookmark;
8use Shaarli\Bookmark\BookmarkFileService; 9use Shaarli\Bookmark\BookmarkFileService;
@@ -10,13 +11,14 @@ use Shaarli\Bookmark\LinkDB;
10use Shaarli\Config\ConfigManager; 11use Shaarli\Config\ConfigManager;
11use Shaarli\Formatter\FormatterFactory; 12use Shaarli\Formatter\FormatterFactory;
12use Shaarli\History; 13use Shaarli\History;
14use Shaarli\TestCase;
13 15
14/** 16/**
15 * FeedBuilderTest class. 17 * FeedBuilderTest class.
16 * 18 *
17 * Unit tests for FeedBuilder. 19 * Unit tests for FeedBuilder.
18 */ 20 */
19class FeedBuilderTest extends \PHPUnit\Framework\TestCase 21class FeedBuilderTest extends TestCase
20{ 22{
21 /** 23 /**
22 * @var string locale Basque (Spain). 24 * @var string locale Basque (Spain).
@@ -44,8 +46,9 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase
44 /** 46 /**
45 * Called before every test method. 47 * Called before every test method.
46 */ 48 */
47 public static function setUpBeforeClass() 49 public static function setUpBeforeClass(): void
48 { 50 {
51 $mutex = new NoMutex();
49 $conf = new ConfigManager('tests/utils/config/configJson'); 52 $conf = new ConfigManager('tests/utils/config/configJson');
50 $conf->set('resource.datastore', self::$testDatastore); 53 $conf->set('resource.datastore', self::$testDatastore);
51 $refLinkDB = new \ReferenceLinkDB(); 54 $refLinkDB = new \ReferenceLinkDB();
@@ -53,35 +56,18 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase
53 $history = new History('sandbox/history.php'); 56 $history = new History('sandbox/history.php');
54 $factory = new FormatterFactory($conf, true); 57 $factory = new FormatterFactory($conf, true);
55 self::$formatter = $factory->getFormatter(); 58 self::$formatter = $factory->getFormatter();
56 self::$bookmarkService = new BookmarkFileService($conf, $history, true); 59 self::$bookmarkService = new BookmarkFileService($conf, $history, $mutex, true);
57 60
58 self::$serverInfo = array( 61 self::$serverInfo = array(
59 'HTTPS' => 'Off', 62 'HTTPS' => 'Off',
60 'SERVER_NAME' => 'host.tld', 63 'SERVER_NAME' => 'host.tld',
61 'SERVER_PORT' => '80', 64 'SERVER_PORT' => '80',
62 'SCRIPT_NAME' => '/index.php', 65 'SCRIPT_NAME' => '/index.php',
63 'REQUEST_URI' => '/index.php?do=feed', 66 'REQUEST_URI' => '/feed/atom',
64 ); 67 );
65 } 68 }
66 69
67 /** 70 /**
68 * Test GetTypeLanguage().
69 */
70 public function testGetTypeLanguage()
71 {
72 $feedBuilder = new FeedBuilder(null, self::$formatter, FeedBuilder::$FEED_ATOM, null, null, false);
73 $feedBuilder->setLocale(self::$LOCALE);
74 $this->assertEquals(self::$ATOM_LANGUAGUE, $feedBuilder->getTypeLanguage());
75 $feedBuilder = new FeedBuilder(null, self::$formatter, FeedBuilder::$FEED_RSS, null, null, false);
76 $feedBuilder->setLocale(self::$LOCALE);
77 $this->assertEquals(self::$RSS_LANGUAGE, $feedBuilder->getTypeLanguage());
78 $feedBuilder = new FeedBuilder(null, self::$formatter, FeedBuilder::$FEED_ATOM, null, null, false);
79 $this->assertEquals('en', $feedBuilder->getTypeLanguage());
80 $feedBuilder = new FeedBuilder(null, self::$formatter, FeedBuilder::$FEED_RSS, null, null, false);
81 $this->assertEquals('en-en', $feedBuilder->getTypeLanguage());
82 }
83
84 /**
85 * Test buildData with RSS feed. 71 * Test buildData with RSS feed.
86 */ 72 */
87 public function testRSSBuildData() 73 public function testRSSBuildData()
@@ -89,35 +75,33 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase
89 $feedBuilder = new FeedBuilder( 75 $feedBuilder = new FeedBuilder(
90 self::$bookmarkService, 76 self::$bookmarkService,
91 self::$formatter, 77 self::$formatter,
92 FeedBuilder::$FEED_RSS, 78 static::$serverInfo,
93 self::$serverInfo,
94 null,
95 false 79 false
96 ); 80 );
97 $feedBuilder->setLocale(self::$LOCALE); 81 $feedBuilder->setLocale(self::$LOCALE);
98 $data = $feedBuilder->buildData(); 82 $data = $feedBuilder->buildData(FeedBuilder::$FEED_RSS, null);
99 // Test headers (RSS) 83 // Test headers (RSS)
100 $this->assertEquals(self::$RSS_LANGUAGE, $data['language']); 84 $this->assertEquals(self::$RSS_LANGUAGE, $data['language']);
101 $this->assertRegExp('/Wed, 03 Aug 2016 09:30:33 \+\d{4}/', $data['last_update']); 85 $this->assertRegExp('/Wed, 03 Aug 2016 09:30:33 \+\d{4}/', $data['last_update']);
102 $this->assertEquals(true, $data['show_dates']); 86 $this->assertEquals(true, $data['show_dates']);
103 $this->assertEquals('http://host.tld/index.php?do=feed', $data['self_link']); 87 $this->assertEquals('http://host.tld/feed/atom', $data['self_link']);
104 $this->assertEquals('http://host.tld/', $data['index_url']); 88 $this->assertEquals('http://host.tld/', $data['index_url']);
105 $this->assertFalse($data['usepermalinks']); 89 $this->assertFalse($data['usepermalinks']);
106 $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links'])); 90 $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
107 91
108 // Test first not pinned link (note link) 92 // Test first not pinned link (note link)
109 $link = $data['links'][array_keys($data['links'])[2]]; 93 $link = $data['links'][array_keys($data['links'])[0]];
110 $this->assertEquals(41, $link['id']); 94 $this->assertEquals(41, $link['id']);
111 $this->assertEquals(DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114651'), $link['created']); 95 $this->assertEquals(DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114651'), $link['created']);
112 $this->assertEquals('http://host.tld/?WDWyig', $link['guid']); 96 $this->assertEquals('http://host.tld/shaare/WDWyig', $link['guid']);
113 $this->assertEquals('http://host.tld/?WDWyig', $link['url']); 97 $this->assertEquals('http://host.tld/shaare/WDWyig', $link['url']);
114 $this->assertRegExp('/Tue, 10 Mar 2015 11:46:51 \+\d{4}/', $link['pub_iso_date']); 98 $this->assertRegExp('/Tue, 10 Mar 2015 11:46:51 \+\d{4}/', $link['pub_iso_date']);
115 $pub = DateTime::createFromFormat(DateTime::RSS, $link['pub_iso_date']); 99 $pub = DateTime::createFromFormat(DateTime::RSS, $link['pub_iso_date']);
116 $up = DateTime::createFromFormat(DateTime::ATOM, $link['up_iso_date']); 100 $up = DateTime::createFromFormat(DateTime::ATOM, $link['up_iso_date']);
117 $this->assertEquals($pub, $up); 101 $this->assertEquals($pub, $up);
118 $this->assertContains('Stallman has a beard', $link['description']); 102 $this->assertContainsPolyfill('Stallman has a beard', $link['description']);
119 $this->assertContains('Permalink', $link['description']); 103 $this->assertContainsPolyfill('Permalink', $link['description']);
120 $this->assertContains('http://host.tld/?WDWyig', $link['description']); 104 $this->assertContainsPolyfill('http://host.tld/shaare/WDWyig', $link['description']);
121 $this->assertEquals(1, count($link['taglist'])); 105 $this->assertEquals(1, count($link['taglist']));
122 $this->assertEquals('sTuff', $link['taglist'][0]); 106 $this->assertEquals('sTuff', $link['taglist'][0]);
123 107
@@ -140,16 +124,14 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase
140 $feedBuilder = new FeedBuilder( 124 $feedBuilder = new FeedBuilder(
141 self::$bookmarkService, 125 self::$bookmarkService,
142 self::$formatter, 126 self::$formatter,
143 FeedBuilder::$FEED_ATOM, 127 static::$serverInfo,
144 self::$serverInfo,
145 null,
146 false 128 false
147 ); 129 );
148 $feedBuilder->setLocale(self::$LOCALE); 130 $feedBuilder->setLocale(self::$LOCALE);
149 $data = $feedBuilder->buildData(); 131 $data = $feedBuilder->buildData(FeedBuilder::$FEED_ATOM, null);
150 $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links'])); 132 $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
151 $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['last_update']); 133 $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['last_update']);
152 $link = $data['links'][array_keys($data['links'])[2]]; 134 $link = $data['links'][array_keys($data['links'])[0]];
153 $this->assertRegExp('/2015-03-10T11:46:51\+\d{2}:\d{2}/', $link['pub_iso_date']); 135 $this->assertRegExp('/2015-03-10T11:46:51\+\d{2}:\d{2}/', $link['pub_iso_date']);
154 $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['links'][8]['up_iso_date']); 136 $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['links'][8]['up_iso_date']);
155 } 137 }
@@ -166,13 +148,11 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase
166 $feedBuilder = new FeedBuilder( 148 $feedBuilder = new FeedBuilder(
167 self::$bookmarkService, 149 self::$bookmarkService,
168 self::$formatter, 150 self::$formatter,
169 FeedBuilder::$FEED_ATOM, 151 static::$serverInfo,
170 self::$serverInfo,
171 $criteria,
172 false 152 false
173 ); 153 );
174 $feedBuilder->setLocale(self::$LOCALE); 154 $feedBuilder->setLocale(self::$LOCALE);
175 $data = $feedBuilder->buildData(); 155 $data = $feedBuilder->buildData(FeedBuilder::$FEED_ATOM, $criteria);
176 $this->assertEquals(1, count($data['links'])); 156 $this->assertEquals(1, count($data['links']));
177 $link = array_shift($data['links']); 157 $link = array_shift($data['links']);
178 $this->assertEquals(41, $link['id']); 158 $this->assertEquals(41, $link['id']);
@@ -190,15 +170,13 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase
190 $feedBuilder = new FeedBuilder( 170 $feedBuilder = new FeedBuilder(
191 self::$bookmarkService, 171 self::$bookmarkService,
192 self::$formatter, 172 self::$formatter,
193 FeedBuilder::$FEED_ATOM, 173 static::$serverInfo,
194 self::$serverInfo,
195 $criteria,
196 false 174 false
197 ); 175 );
198 $feedBuilder->setLocale(self::$LOCALE); 176 $feedBuilder->setLocale(self::$LOCALE);
199 $data = $feedBuilder->buildData(); 177 $data = $feedBuilder->buildData(FeedBuilder::$FEED_ATOM, $criteria);
200 $this->assertEquals(3, count($data['links'])); 178 $this->assertEquals(3, count($data['links']));
201 $link = $data['links'][array_keys($data['links'])[2]]; 179 $link = $data['links'][array_keys($data['links'])[0]];
202 $this->assertEquals(41, $link['id']); 180 $this->assertEquals(41, $link['id']);
203 $this->assertEquals(DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114651'), $link['created']); 181 $this->assertEquals(DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114651'), $link['created']);
204 } 182 }
@@ -211,32 +189,30 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase
211 $feedBuilder = new FeedBuilder( 189 $feedBuilder = new FeedBuilder(
212 self::$bookmarkService, 190 self::$bookmarkService,
213 self::$formatter, 191 self::$formatter,
214 FeedBuilder::$FEED_ATOM, 192 static::$serverInfo,
215 self::$serverInfo,
216 null,
217 false 193 false
218 ); 194 );
219 $feedBuilder->setLocale(self::$LOCALE); 195 $feedBuilder->setLocale(self::$LOCALE);
220 $feedBuilder->setUsePermalinks(true); 196 $feedBuilder->setUsePermalinks(true);
221 $data = $feedBuilder->buildData(); 197 $data = $feedBuilder->buildData(FeedBuilder::$FEED_ATOM, null);
222 $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links'])); 198 $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
223 $this->assertTrue($data['usepermalinks']); 199 $this->assertTrue($data['usepermalinks']);
224 // First link is a permalink 200 // First link is a permalink
225 $link = $data['links'][array_keys($data['links'])[2]]; 201 $link = $data['links'][array_keys($data['links'])[0]];
226 $this->assertEquals(41, $link['id']); 202 $this->assertEquals(41, $link['id']);
227 $this->assertEquals(DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114651'), $link['created']); 203 $this->assertEquals(DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114651'), $link['created']);
228 $this->assertEquals('http://host.tld/?WDWyig', $link['guid']); 204 $this->assertEquals('http://host.tld/shaare/WDWyig', $link['guid']);
229 $this->assertEquals('http://host.tld/?WDWyig', $link['url']); 205 $this->assertEquals('http://host.tld/shaare/WDWyig', $link['url']);
230 $this->assertContains('Direct link', $link['description']); 206 $this->assertContainsPolyfill('Direct link', $link['description']);
231 $this->assertContains('http://host.tld/?WDWyig', $link['description']); 207 $this->assertContainsPolyfill('http://host.tld/shaare/WDWyig', $link['description']);
232 // Second link is a direct link 208 // Second link is a direct link
233 $link = $data['links'][array_keys($data['links'])[3]]; 209 $link = $data['links'][array_keys($data['links'])[1]];
234 $this->assertEquals(8, $link['id']); 210 $this->assertEquals(8, $link['id']);
235 $this->assertEquals(DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114633'), $link['created']); 211 $this->assertEquals(DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114633'), $link['created']);
236 $this->assertEquals('http://host.tld/?RttfEw', $link['guid']); 212 $this->assertEquals('http://host.tld/shaare/RttfEw', $link['guid']);
237 $this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['url']); 213 $this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['url']);
238 $this->assertContains('Direct link', $link['description']); 214 $this->assertContainsPolyfill('Direct link', $link['description']);
239 $this->assertContains('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['description']); 215 $this->assertContainsPolyfill('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['description']);
240 } 216 }
241 217
242 /** 218 /**
@@ -247,14 +223,12 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase
247 $feedBuilder = new FeedBuilder( 223 $feedBuilder = new FeedBuilder(
248 self::$bookmarkService, 224 self::$bookmarkService,
249 self::$formatter, 225 self::$formatter,
250 FeedBuilder::$FEED_ATOM, 226 static::$serverInfo,
251 self::$serverInfo,
252 null,
253 false 227 false
254 ); 228 );
255 $feedBuilder->setLocale(self::$LOCALE); 229 $feedBuilder->setLocale(self::$LOCALE);
256 $feedBuilder->setHideDates(true); 230 $feedBuilder->setHideDates(true);
257 $data = $feedBuilder->buildData(); 231 $data = $feedBuilder->buildData(FeedBuilder::$FEED_ATOM, null);
258 $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links'])); 232 $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
259 $this->assertFalse($data['show_dates']); 233 $this->assertFalse($data['show_dates']);
260 234
@@ -262,14 +236,12 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase
262 $feedBuilder = new FeedBuilder( 236 $feedBuilder = new FeedBuilder(
263 self::$bookmarkService, 237 self::$bookmarkService,
264 self::$formatter, 238 self::$formatter,
265 FeedBuilder::$FEED_ATOM, 239 static::$serverInfo,
266 self::$serverInfo,
267 null,
268 true 240 true
269 ); 241 );
270 $feedBuilder->setLocale(self::$LOCALE); 242 $feedBuilder->setLocale(self::$LOCALE);
271 $feedBuilder->setHideDates(true); 243 $feedBuilder->setHideDates(true);
272 $data = $feedBuilder->buildData(); 244 $data = $feedBuilder->buildData(FeedBuilder::$FEED_ATOM, null);
273 $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links'])); 245 $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
274 $this->assertTrue($data['show_dates']); 246 $this->assertTrue($data['show_dates']);
275 } 247 }
@@ -284,28 +256,26 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase
284 'SERVER_NAME' => 'host.tld', 256 'SERVER_NAME' => 'host.tld',
285 'SERVER_PORT' => '8080', 257 'SERVER_PORT' => '8080',
286 'SCRIPT_NAME' => '/~user/shaarli/index.php', 258 'SCRIPT_NAME' => '/~user/shaarli/index.php',
287 'REQUEST_URI' => '/~user/shaarli/index.php?do=feed', 259 'REQUEST_URI' => '/~user/shaarli/feed/atom',
288 ); 260 );
289 $feedBuilder = new FeedBuilder( 261 $feedBuilder = new FeedBuilder(
290 self::$bookmarkService, 262 self::$bookmarkService,
291 self::$formatter, 263 self::$formatter,
292 FeedBuilder::$FEED_ATOM,
293 $serverInfo, 264 $serverInfo,
294 null,
295 false 265 false
296 ); 266 );
297 $feedBuilder->setLocale(self::$LOCALE); 267 $feedBuilder->setLocale(self::$LOCALE);
298 $data = $feedBuilder->buildData(); 268 $data = $feedBuilder->buildData(FeedBuilder::$FEED_ATOM, null);
299 269
300 $this->assertEquals( 270 $this->assertEquals(
301 'http://host.tld:8080/~user/shaarli/index.php?do=feed', 271 'http://host.tld:8080/~user/shaarli/feed/atom',
302 $data['self_link'] 272 $data['self_link']
303 ); 273 );
304 274
305 // Test first link (note link) 275 // Test first link (note link)
306 $link = $data['links'][array_keys($data['links'])[2]]; 276 $link = $data['links'][array_keys($data['links'])[0]];
307 $this->assertEquals('http://host.tld:8080/~user/shaarli/?WDWyig', $link['guid']); 277 $this->assertEquals('http://host.tld:8080/~user/shaarli/shaare/WDWyig', $link['guid']);
308 $this->assertEquals('http://host.tld:8080/~user/shaarli/?WDWyig', $link['url']); 278 $this->assertEquals('http://host.tld:8080/~user/shaarli/shaare/WDWyig', $link['url']);
309 $this->assertContains('http://host.tld:8080/~user/shaarli/?addtag=hashtag', $link['description']); 279 $this->assertContainsPolyfill('http://host.tld:8080/~user/shaarli/./add-tag/hashtag', $link['description']);
310 } 280 }
311} 281}