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