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.php120
-rw-r--r--tests/feed/FeedBuilderTest.php250
3 files changed, 462 insertions, 0 deletions
diff --git a/tests/feed/CacheTest.php b/tests/feed/CacheTest.php
new file mode 100644
index 00000000..c0a9f26f
--- /dev/null
+++ b/tests/feed/CacheTest.php
@@ -0,0 +1,92 @@
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
new file mode 100644
index 00000000..0bcc1442
--- /dev/null
+++ b/tests/feed/CachedPageTest.php
@@ -0,0 +1,120 @@
1<?php
2/**
3 * PageCache tests
4 */
5namespace Shaarli\Feed;
6
7/**
8 * Unitary tests for cached pages
9 */
10class CachedPageTest extends \PHPUnit\Framework\TestCase
11{
12 // test cache directory
13 protected static $testCacheDir = 'sandbox/pagecache';
14 protected static $url = 'http://shaar.li/?do=atom';
15 protected static $filename;
16
17 /**
18 * Create the cache directory if needed
19 */
20 public static function setUpBeforeClass()
21 {
22 if (!is_dir(self::$testCacheDir)) {
23 mkdir(self::$testCacheDir);
24 }
25 self::$filename = self::$testCacheDir . '/' . sha1(self::$url) . '.cache';
26 }
27
28 /**
29 * Reset the page cache
30 */
31 public function setUp()
32 {
33 if (file_exists(self::$filename)) {
34 unlink(self::$filename);
35 }
36 }
37
38 /**
39 * Create a new cached page
40 */
41 public function testConstruct()
42 {
43 new CachedPage(self::$testCacheDir, '', true);
44 new CachedPage(self::$testCacheDir, '', false);
45 new CachedPage(self::$testCacheDir, 'http://shaar.li/?do=rss', true);
46 new CachedPage(self::$testCacheDir, 'http://shaar.li/?do=atom', false);
47 }
48
49 /**
50 * Cache a page's content
51 */
52 public function testCache()
53 {
54 $page = new CachedPage(self::$testCacheDir, self::$url, true);
55
56 $this->assertFileNotExists(self::$filename);
57 $page->cache('<p>Some content</p>');
58 $this->assertFileExists(self::$filename);
59 $this->assertEquals(
60 '<p>Some content</p>',
61 file_get_contents(self::$filename)
62 );
63 }
64
65 /**
66 * "Cache" a page's content - the page is not to be cached
67 */
68 public function testShouldNotCache()
69 {
70 $page = new CachedPage(self::$testCacheDir, self::$url, false);
71
72 $this->assertFileNotExists(self::$filename);
73 $page->cache('<p>Some content</p>');
74 $this->assertFileNotExists(self::$filename);
75 }
76
77 /**
78 * Return a page's cached content
79 */
80 public function testCachedVersion()
81 {
82 $page = new CachedPage(self::$testCacheDir, self::$url, true);
83
84 $this->assertFileNotExists(self::$filename);
85 $page->cache('<p>Some content</p>');
86 $this->assertFileExists(self::$filename);
87 $this->assertEquals(
88 '<p>Some content</p>',
89 $page->cachedVersion()
90 );
91 }
92
93 /**
94 * Return a page's cached content - the file does not exist
95 */
96 public function testCachedVersionNoFile()
97 {
98 $page = new CachedPage(self::$testCacheDir, self::$url, true);
99
100 $this->assertFileNotExists(self::$filename);
101 $this->assertEquals(
102 null,
103 $page->cachedVersion()
104 );
105 }
106
107 /**
108 * Return a page's cached content - the page is not to be cached
109 */
110 public function testNoCachedVersion()
111 {
112 $page = new CachedPage(self::$testCacheDir, self::$url, false);
113
114 $this->assertFileNotExists(self::$filename);
115 $this->assertEquals(
116 null,
117 $page->cachedVersion()
118 );
119 }
120}
diff --git a/tests/feed/FeedBuilderTest.php b/tests/feed/FeedBuilderTest.php
new file mode 100644
index 00000000..1fdbc60e
--- /dev/null
+++ b/tests/feed/FeedBuilderTest.php
@@ -0,0 +1,250 @@
1<?php
2
3namespace Shaarli\Feed;
4
5use DateTime;
6use LinkDB;
7use ReferenceLinkDB;
8
9require_once 'application/LinkDB.php';
10
11/**
12 * FeedBuilderTest class.
13 *
14 * Unit tests for FeedBuilder.
15 */
16class 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}