aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/feed
diff options
context:
space:
mode:
Diffstat (limited to 'tests/feed')
-rw-r--r--tests/feed/CacheTest.php92
-rw-r--r--tests/feed/CachedPageTest.php6
-rw-r--r--tests/feed/FeedBuilderTest.php83
3 files changed, 28 insertions, 153 deletions
diff --git a/tests/feed/CacheTest.php b/tests/feed/CacheTest.php
deleted file mode 100644
index c0a9f26f..00000000
--- a/tests/feed/CacheTest.php
+++ /dev/null
@@ -1,92 +0,0 @@
1<?php
2/**
3 * Cache tests
4 */
5namespace Shaarli\Feed;
6
7// required to access $_SESSION array
8session_start();
9
10require_once 'application/feed/Cache.php';
11
12/**
13 * Unitary tests for cached pages
14 */
15class CacheTest extends \PHPUnit\Framework\TestCase
16{
17 // test cache directory
18 protected static $testCacheDir = 'sandbox/dummycache';
19
20 // dummy cached file names / content
21 protected static $pages = array('a', 'toto', 'd7b59c');
22
23
24 /**
25 * Populate the cache with dummy files
26 */
27 public function setUp()
28 {
29 if (!is_dir(self::$testCacheDir)) {
30 mkdir(self::$testCacheDir);
31 } else {
32 array_map('unlink', glob(self::$testCacheDir . '/*'));
33 }
34
35 foreach (self::$pages as $page) {
36 file_put_contents(self::$testCacheDir . '/' . $page . '.cache', $page);
37 }
38 file_put_contents(self::$testCacheDir . '/intru.der', 'ShouldNotBeThere');
39 }
40
41 /**
42 * Remove dummycache folder after each tests.
43 */
44 public function tearDown()
45 {
46 array_map('unlink', glob(self::$testCacheDir . '/*'));
47 rmdir(self::$testCacheDir);
48 }
49
50 /**
51 * Purge cached pages
52 */
53 public function testPurgeCachedPages()
54 {
55 purgeCachedPages(self::$testCacheDir);
56 foreach (self::$pages as $page) {
57 $this->assertFileNotExists(self::$testCacheDir . '/' . $page . '.cache');
58 }
59
60 $this->assertFileExists(self::$testCacheDir . '/intru.der');
61 }
62
63 /**
64 * Purge cached pages - missing directory
65 */
66 public function testPurgeCachedPagesMissingDir()
67 {
68 $oldlog = ini_get('error_log');
69 ini_set('error_log', '/dev/null');
70 $this->assertEquals(
71 'Cannot purge sandbox/dummycache_missing: no directory',
72 purgeCachedPages(self::$testCacheDir . '_missing')
73 );
74 ini_set('error_log', $oldlog);
75 }
76
77 /**
78 * Purge cached pages and session cache
79 */
80 public function testInvalidateCaches()
81 {
82 $this->assertArrayNotHasKey('tags', $_SESSION);
83 $_SESSION['tags'] = array('goodbye', 'cruel', 'world');
84
85 invalidateCaches(self::$testCacheDir);
86 foreach (self::$pages as $page) {
87 $this->assertFileNotExists(self::$testCacheDir . '/' . $page . '.cache');
88 }
89
90 $this->assertArrayNotHasKey('tags', $_SESSION);
91 }
92}
diff --git a/tests/feed/CachedPageTest.php b/tests/feed/CachedPageTest.php
index 363028a2..2e716432 100644
--- a/tests/feed/CachedPageTest.php
+++ b/tests/feed/CachedPageTest.php
@@ -11,7 +11,7 @@ class CachedPageTest extends \PHPUnit\Framework\TestCase
11{ 11{
12 // test cache directory 12 // test cache directory
13 protected static $testCacheDir = 'sandbox/pagecache'; 13 protected static $testCacheDir = 'sandbox/pagecache';
14 protected static $url = 'http://shaar.li/?do=atom'; 14 protected static $url = 'http://shaar.li/feed/atom';
15 protected static $filename; 15 protected static $filename;
16 16
17 /** 17 /**
@@ -42,8 +42,8 @@ class CachedPageTest extends \PHPUnit\Framework\TestCase
42 { 42 {
43 new CachedPage(self::$testCacheDir, '', true); 43 new CachedPage(self::$testCacheDir, '', true);
44 new CachedPage(self::$testCacheDir, '', false); 44 new CachedPage(self::$testCacheDir, '', false);
45 new CachedPage(self::$testCacheDir, 'http://shaar.li/?do=rss', true); 45 new CachedPage(self::$testCacheDir, 'http://shaar.li/feed/rss', true);
46 new CachedPage(self::$testCacheDir, 'http://shaar.li/?do=atom', false); 46 new CachedPage(self::$testCacheDir, 'http://shaar.li/feed/atom', false);
47 $this->addToAssertionCount(1); 47 $this->addToAssertionCount(1);
48 } 48 }
49 49
diff --git a/tests/feed/FeedBuilderTest.php b/tests/feed/FeedBuilderTest.php
index 54671891..5c2aaedb 100644
--- a/tests/feed/FeedBuilderTest.php
+++ b/tests/feed/FeedBuilderTest.php
@@ -65,23 +65,6 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase
65 } 65 }
66 66
67 /** 67 /**
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. 68 * Test buildData with RSS feed.
86 */ 69 */
87 public function testRSSBuildData() 70 public function testRSSBuildData()
@@ -89,13 +72,11 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase
89 $feedBuilder = new FeedBuilder( 72 $feedBuilder = new FeedBuilder(
90 self::$bookmarkService, 73 self::$bookmarkService,
91 self::$formatter, 74 self::$formatter,
92 FeedBuilder::$FEED_RSS, 75 static::$serverInfo,
93 self::$serverInfo,
94 null,
95 false 76 false
96 ); 77 );
97 $feedBuilder->setLocale(self::$LOCALE); 78 $feedBuilder->setLocale(self::$LOCALE);
98 $data = $feedBuilder->buildData(); 79 $data = $feedBuilder->buildData(FeedBuilder::$FEED_RSS, null);
99 // Test headers (RSS) 80 // Test headers (RSS)
100 $this->assertEquals(self::$RSS_LANGUAGE, $data['language']); 81 $this->assertEquals(self::$RSS_LANGUAGE, $data['language']);
101 $this->assertRegExp('/Wed, 03 Aug 2016 09:30:33 \+\d{4}/', $data['last_update']); 82 $this->assertRegExp('/Wed, 03 Aug 2016 09:30:33 \+\d{4}/', $data['last_update']);
@@ -109,15 +90,15 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase
109 $link = $data['links'][array_keys($data['links'])[2]]; 90 $link = $data['links'][array_keys($data['links'])[2]];
110 $this->assertEquals(41, $link['id']); 91 $this->assertEquals(41, $link['id']);
111 $this->assertEquals(DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114651'), $link['created']); 92 $this->assertEquals(DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114651'), $link['created']);
112 $this->assertEquals('http://host.tld/?WDWyig', $link['guid']); 93 $this->assertEquals('http://host.tld/shaare/WDWyig', $link['guid']);
113 $this->assertEquals('http://host.tld/?WDWyig', $link['url']); 94 $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']); 95 $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']); 96 $pub = DateTime::createFromFormat(DateTime::RSS, $link['pub_iso_date']);
116 $up = DateTime::createFromFormat(DateTime::ATOM, $link['up_iso_date']); 97 $up = DateTime::createFromFormat(DateTime::ATOM, $link['up_iso_date']);
117 $this->assertEquals($pub, $up); 98 $this->assertEquals($pub, $up);
118 $this->assertContains('Stallman has a beard', $link['description']); 99 $this->assertContains('Stallman has a beard', $link['description']);
119 $this->assertContains('Permalink', $link['description']); 100 $this->assertContains('Permalink', $link['description']);
120 $this->assertContains('http://host.tld/?WDWyig', $link['description']); 101 $this->assertContains('http://host.tld/shaare/WDWyig', $link['description']);
121 $this->assertEquals(1, count($link['taglist'])); 102 $this->assertEquals(1, count($link['taglist']));
122 $this->assertEquals('sTuff', $link['taglist'][0]); 103 $this->assertEquals('sTuff', $link['taglist'][0]);
123 104
@@ -140,13 +121,11 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase
140 $feedBuilder = new FeedBuilder( 121 $feedBuilder = new FeedBuilder(
141 self::$bookmarkService, 122 self::$bookmarkService,
142 self::$formatter, 123 self::$formatter,
143 FeedBuilder::$FEED_ATOM, 124 static::$serverInfo,
144 self::$serverInfo,
145 null,
146 false 125 false
147 ); 126 );
148 $feedBuilder->setLocale(self::$LOCALE); 127 $feedBuilder->setLocale(self::$LOCALE);
149 $data = $feedBuilder->buildData(); 128 $data = $feedBuilder->buildData(FeedBuilder::$FEED_ATOM, null);
150 $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links'])); 129 $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
151 $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['last_update']); 130 $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['last_update']);
152 $link = $data['links'][array_keys($data['links'])[2]]; 131 $link = $data['links'][array_keys($data['links'])[2]];
@@ -166,13 +145,11 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase
166 $feedBuilder = new FeedBuilder( 145 $feedBuilder = new FeedBuilder(
167 self::$bookmarkService, 146 self::$bookmarkService,
168 self::$formatter, 147 self::$formatter,
169 FeedBuilder::$FEED_ATOM, 148 static::$serverInfo,
170 self::$serverInfo,
171 $criteria,
172 false 149 false
173 ); 150 );
174 $feedBuilder->setLocale(self::$LOCALE); 151 $feedBuilder->setLocale(self::$LOCALE);
175 $data = $feedBuilder->buildData(); 152 $data = $feedBuilder->buildData(FeedBuilder::$FEED_ATOM, $criteria);
176 $this->assertEquals(1, count($data['links'])); 153 $this->assertEquals(1, count($data['links']));
177 $link = array_shift($data['links']); 154 $link = array_shift($data['links']);
178 $this->assertEquals(41, $link['id']); 155 $this->assertEquals(41, $link['id']);
@@ -190,13 +167,11 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase
190 $feedBuilder = new FeedBuilder( 167 $feedBuilder = new FeedBuilder(
191 self::$bookmarkService, 168 self::$bookmarkService,
192 self::$formatter, 169 self::$formatter,
193 FeedBuilder::$FEED_ATOM, 170 static::$serverInfo,
194 self::$serverInfo,
195 $criteria,
196 false 171 false
197 ); 172 );
198 $feedBuilder->setLocale(self::$LOCALE); 173 $feedBuilder->setLocale(self::$LOCALE);
199 $data = $feedBuilder->buildData(); 174 $data = $feedBuilder->buildData(FeedBuilder::$FEED_ATOM, $criteria);
200 $this->assertEquals(3, count($data['links'])); 175 $this->assertEquals(3, count($data['links']));
201 $link = $data['links'][array_keys($data['links'])[2]]; 176 $link = $data['links'][array_keys($data['links'])[2]];
202 $this->assertEquals(41, $link['id']); 177 $this->assertEquals(41, $link['id']);
@@ -211,29 +186,27 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase
211 $feedBuilder = new FeedBuilder( 186 $feedBuilder = new FeedBuilder(
212 self::$bookmarkService, 187 self::$bookmarkService,
213 self::$formatter, 188 self::$formatter,
214 FeedBuilder::$FEED_ATOM, 189 static::$serverInfo,
215 self::$serverInfo,
216 null,
217 false 190 false
218 ); 191 );
219 $feedBuilder->setLocale(self::$LOCALE); 192 $feedBuilder->setLocale(self::$LOCALE);
220 $feedBuilder->setUsePermalinks(true); 193 $feedBuilder->setUsePermalinks(true);
221 $data = $feedBuilder->buildData(); 194 $data = $feedBuilder->buildData(FeedBuilder::$FEED_ATOM, null);
222 $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links'])); 195 $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
223 $this->assertTrue($data['usepermalinks']); 196 $this->assertTrue($data['usepermalinks']);
224 // First link is a permalink 197 // First link is a permalink
225 $link = $data['links'][array_keys($data['links'])[2]]; 198 $link = $data['links'][array_keys($data['links'])[2]];
226 $this->assertEquals(41, $link['id']); 199 $this->assertEquals(41, $link['id']);
227 $this->assertEquals(DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114651'), $link['created']); 200 $this->assertEquals(DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114651'), $link['created']);
228 $this->assertEquals('http://host.tld/?WDWyig', $link['guid']); 201 $this->assertEquals('http://host.tld/shaare/WDWyig', $link['guid']);
229 $this->assertEquals('http://host.tld/?WDWyig', $link['url']); 202 $this->assertEquals('http://host.tld/shaare/WDWyig', $link['url']);
230 $this->assertContains('Direct link', $link['description']); 203 $this->assertContains('Direct link', $link['description']);
231 $this->assertContains('http://host.tld/?WDWyig', $link['description']); 204 $this->assertContains('http://host.tld/shaare/WDWyig', $link['description']);
232 // Second link is a direct link 205 // Second link is a direct link
233 $link = $data['links'][array_keys($data['links'])[3]]; 206 $link = $data['links'][array_keys($data['links'])[3]];
234 $this->assertEquals(8, $link['id']); 207 $this->assertEquals(8, $link['id']);
235 $this->assertEquals(DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114633'), $link['created']); 208 $this->assertEquals(DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114633'), $link['created']);
236 $this->assertEquals('http://host.tld/?RttfEw', $link['guid']); 209 $this->assertEquals('http://host.tld/shaare/RttfEw', $link['guid']);
237 $this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['url']); 210 $this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['url']);
238 $this->assertContains('Direct link', $link['description']); 211 $this->assertContains('Direct link', $link['description']);
239 $this->assertContains('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['description']); 212 $this->assertContains('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['description']);
@@ -247,14 +220,12 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase
247 $feedBuilder = new FeedBuilder( 220 $feedBuilder = new FeedBuilder(
248 self::$bookmarkService, 221 self::$bookmarkService,
249 self::$formatter, 222 self::$formatter,
250 FeedBuilder::$FEED_ATOM, 223 static::$serverInfo,
251 self::$serverInfo,
252 null,
253 false 224 false
254 ); 225 );
255 $feedBuilder->setLocale(self::$LOCALE); 226 $feedBuilder->setLocale(self::$LOCALE);
256 $feedBuilder->setHideDates(true); 227 $feedBuilder->setHideDates(true);
257 $data = $feedBuilder->buildData(); 228 $data = $feedBuilder->buildData(FeedBuilder::$FEED_ATOM, null);
258 $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links'])); 229 $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
259 $this->assertFalse($data['show_dates']); 230 $this->assertFalse($data['show_dates']);
260 231
@@ -262,14 +233,12 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase
262 $feedBuilder = new FeedBuilder( 233 $feedBuilder = new FeedBuilder(
263 self::$bookmarkService, 234 self::$bookmarkService,
264 self::$formatter, 235 self::$formatter,
265 FeedBuilder::$FEED_ATOM, 236 static::$serverInfo,
266 self::$serverInfo,
267 null,
268 true 237 true
269 ); 238 );
270 $feedBuilder->setLocale(self::$LOCALE); 239 $feedBuilder->setLocale(self::$LOCALE);
271 $feedBuilder->setHideDates(true); 240 $feedBuilder->setHideDates(true);
272 $data = $feedBuilder->buildData(); 241 $data = $feedBuilder->buildData(FeedBuilder::$FEED_ATOM, null);
273 $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links'])); 242 $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
274 $this->assertTrue($data['show_dates']); 243 $this->assertTrue($data['show_dates']);
275 } 244 }
@@ -289,13 +258,11 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase
289 $feedBuilder = new FeedBuilder( 258 $feedBuilder = new FeedBuilder(
290 self::$bookmarkService, 259 self::$bookmarkService,
291 self::$formatter, 260 self::$formatter,
292 FeedBuilder::$FEED_ATOM,
293 $serverInfo, 261 $serverInfo,
294 null,
295 false 262 false
296 ); 263 );
297 $feedBuilder->setLocale(self::$LOCALE); 264 $feedBuilder->setLocale(self::$LOCALE);
298 $data = $feedBuilder->buildData(); 265 $data = $feedBuilder->buildData(FeedBuilder::$FEED_ATOM, null);
299 266
300 $this->assertEquals( 267 $this->assertEquals(
301 'http://host.tld:8080/~user/shaarli/index.php?do=feed', 268 'http://host.tld:8080/~user/shaarli/index.php?do=feed',
@@ -304,8 +271,8 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase
304 271
305 // Test first link (note link) 272 // Test first link (note link)
306 $link = $data['links'][array_keys($data['links'])[2]]; 273 $link = $data['links'][array_keys($data['links'])[2]];
307 $this->assertEquals('http://host.tld:8080/~user/shaarli/?WDWyig', $link['guid']); 274 $this->assertEquals('http://host.tld:8080/~user/shaarli/shaare/WDWyig', $link['guid']);
308 $this->assertEquals('http://host.tld:8080/~user/shaarli/?WDWyig', $link['url']); 275 $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']); 276 $this->assertContains('http://host.tld:8080/~user/shaarli/./add-tag/hashtag', $link['description']);
310 } 277 }
311} 278}