aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/FeedBuilderTest.php212
-rw-r--r--tests/LinkDBTest.php56
-rw-r--r--tests/LinkFilterTest.php17
-rw-r--r--tests/LinkUtilsTest.php2
-rw-r--r--tests/Updater/UpdaterTest.php4
-rw-r--r--tests/utils/ReferenceLinkDB.php20
6 files changed, 286 insertions, 25 deletions
diff --git a/tests/FeedBuilderTest.php b/tests/FeedBuilderTest.php
new file mode 100644
index 00000000..069b1581
--- /dev/null
+++ b/tests/FeedBuilderTest.php
@@ -0,0 +1,212 @@
1<?php
2
3require_once 'application/FeedBuilder.php';
4require_once 'application/LinkDB.php';
5
6/**
7 * FeedBuilderTest class.
8 *
9 * Unit tests for FeedBuilder.
10 */
11class FeedBuilderTest extends PHPUnit_Framework_TestCase
12{
13 /**
14 * @var string locale Basque (Spain).
15 */
16 public static $LOCALE = 'eu_ES';
17
18 /**
19 * @var string language in RSS format.
20 */
21 public static $RSS_LANGUAGE = 'eu-es';
22
23 /**
24 * @var string language in ATOM format.
25 */
26 public static $ATOM_LANGUAGUE = 'eu';
27
28 protected static $testDatastore = 'sandbox/datastore.php';
29
30 public static $linkDB;
31
32 public static $serverInfo;
33
34 /**
35 * Called before every test method.
36 */
37 public static function setUpBeforeClass()
38 {
39 $refLinkDB = new ReferenceLinkDB();
40 $refLinkDB->write(self::$testDatastore);
41 self::$linkDB = new LinkDB(self::$testDatastore, true, false);
42 self::$serverInfo = array(
43 'HTTPS' => 'Off',
44 'SERVER_NAME' => 'host.tld',
45 'SERVER_PORT' => '80',
46 'SCRIPT_NAME' => '/index.php',
47 'REQUEST_URI' => '/index.php?do=feed',
48 );
49 }
50
51 /**
52 * Test GetTypeLanguage().
53 */
54 public function testGetTypeLanguage()
55 {
56 $feedBuilder = new FeedBuilder(null, FeedBuilder::$FEED_ATOM, null, null, false);
57 $feedBuilder->setLocale(self::$LOCALE);
58 $this->assertEquals(self::$ATOM_LANGUAGUE, $feedBuilder->getTypeLanguage());
59 $feedBuilder = new FeedBuilder(null, FeedBuilder::$FEED_RSS, null, null, false);
60 $feedBuilder->setLocale(self::$LOCALE);
61 $this->assertEquals(self::$RSS_LANGUAGE, $feedBuilder->getTypeLanguage());
62 $feedBuilder = new FeedBuilder(null, FeedBuilder::$FEED_ATOM, null, null, false);
63 $this->assertEquals('en', $feedBuilder->getTypeLanguage());
64 $feedBuilder = new FeedBuilder(null, FeedBuilder::$FEED_RSS, null, null, false);
65 $this->assertEquals('en-en', $feedBuilder->getTypeLanguage());
66 }
67
68 /**
69 * Test buildData with RSS feed.
70 */
71 public function testRSSBuildData()
72 {
73 $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_RSS, self::$serverInfo, null, false);
74 $feedBuilder->setLocale(self::$LOCALE);
75 $data = $feedBuilder->buildData();
76 // Test headers (RSS)
77 $this->assertEquals(self::$RSS_LANGUAGE, $data['language']);
78 $this->assertEmpty($data['pubsubhub_url']);
79 $this->assertEquals('Tue, 10 Mar 2015 11:46:51 +0100', $data['last_update']);
80 $this->assertEquals(true, $data['show_dates']);
81 $this->assertEquals('http://host.tld/index.php?do=feed', $data['self_link']);
82 $this->assertEquals('http://host.tld/', $data['index_url']);
83 $this->assertFalse($data['usepermalinks']);
84 $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
85
86 // Test first link (note link)
87 $link = array_shift($data['links']);
88 $this->assertEquals('20150310_114651', $link['linkdate']);
89 $this->assertEquals('http://host.tld/?WDWyig', $link['guid']);
90 $this->assertEquals('http://host.tld/?WDWyig', $link['url']);
91 $this->assertEquals('Tue, 10 Mar 2015 11:46:51 +0100', $link['iso_date']);
92 $this->assertContains('Stallman has a beard', $link['description']);
93 $this->assertContains('Permalink', $link['description']);
94 $this->assertContains('http://host.tld/?WDWyig', $link['description']);
95 $this->assertEquals(1, count($link['taglist']));
96 $this->assertEquals('stuff', $link['taglist'][0]);
97
98 // Test URL with external link.
99 $this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $data['links']['20150310_114633']['url']);
100
101 // Test multitags.
102 $this->assertEquals(5, count($data['links']['20141125_084734']['taglist']));
103 $this->assertEquals('css', $data['links']['20141125_084734']['taglist'][0]);
104 }
105
106 /**
107 * Test buildData with ATOM feed (test only specific to ATOM).
108 */
109 public function testAtomBuildData()
110 {
111 $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, false);
112 $feedBuilder->setLocale(self::$LOCALE);
113 $data = $feedBuilder->buildData();
114 $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
115 $link = array_shift($data['links']);
116 $this->assertEquals('2015-03-10T11:46:51+01:00', $link['iso_date']);
117 }
118
119 /**
120 * Test buildData with search criteria.
121 */
122 public function testBuildDataFiltered()
123 {
124 $criteria = array(
125 'searchtags' => 'stuff',
126 'searchterm' => 'beard',
127 );
128 $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, $criteria, false);
129 $feedBuilder->setLocale(self::$LOCALE);
130 $data = $feedBuilder->buildData();
131 $this->assertEquals(1, count($data['links']));
132 $link = array_shift($data['links']);
133 $this->assertEquals('20150310_114651', $link['linkdate']);
134 }
135
136 /**
137 * Test buildData with nb limit.
138 */
139 public function testBuildDataCount()
140 {
141 $criteria = array(
142 'nb' => '1',
143 );
144 $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, $criteria, false);
145 $feedBuilder->setLocale(self::$LOCALE);
146 $data = $feedBuilder->buildData();
147 $this->assertEquals(1, count($data['links']));
148 $link = array_shift($data['links']);
149 $this->assertEquals('20150310_114651', $link['linkdate']);
150 }
151
152 /**
153 * Test buildData with permalinks on.
154 */
155 public function testBuildDataPermalinks()
156 {
157 $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, false);
158 $feedBuilder->setLocale(self::$LOCALE);
159 $feedBuilder->setUsePermalinks(true);
160 $data = $feedBuilder->buildData();
161 $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
162 $this->assertTrue($data['usepermalinks']);
163 // First link is a permalink
164 $link = array_shift($data['links']);
165 $this->assertEquals('20150310_114651', $link['linkdate']);
166 $this->assertEquals('http://host.tld/?WDWyig', $link['guid']);
167 $this->assertEquals('http://host.tld/?WDWyig', $link['url']);
168 $this->assertContains('Direct link', $link['description']);
169 $this->assertContains('http://host.tld/?WDWyig', $link['description']);
170 // Second link is a direct link
171 $link = array_shift($data['links']);
172 $this->assertEquals('20150310_114633', $link['linkdate']);
173 $this->assertEquals('http://host.tld/?kLHmZg', $link['guid']);
174 $this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['url']);
175 $this->assertContains('Direct link', $link['description']);
176 $this->assertContains('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['description']);
177 }
178
179 /**
180 * Test buildData with hide dates settings.
181 */
182 public function testBuildDataHideDates()
183 {
184 $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, false);
185 $feedBuilder->setLocale(self::$LOCALE);
186 $feedBuilder->setHideDates(true);
187 $data = $feedBuilder->buildData();
188 $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
189 $this->assertFalse($data['show_dates']);
190
191 // Show dates while logged in
192 $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, true);
193 $feedBuilder->setLocale(self::$LOCALE);
194 $feedBuilder->setHideDates(true);
195 $data = $feedBuilder->buildData();
196 $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
197 $this->assertTrue($data['show_dates']);
198 }
199
200 /**
201 * Test buildData with hide dates settings.
202 */
203 public function testBuildDataPubsubhub()
204 {
205 $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, false);
206 $feedBuilder->setLocale(self::$LOCALE);
207 $feedBuilder->setPubsubhubUrl('http://pubsubhub.io');
208 $data = $feedBuilder->buildData();
209 $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
210 $this->assertEquals('http://pubsubhub.io', $data['pubsubhub_url']);
211 }
212}
diff --git a/tests/LinkDBTest.php b/tests/LinkDBTest.php
index b6a273b3..52d31400 100644
--- a/tests/LinkDBTest.php
+++ b/tests/LinkDBTest.php
@@ -17,8 +17,20 @@ class LinkDBTest extends PHPUnit_Framework_TestCase
17{ 17{
18 // datastore to test write operations 18 // datastore to test write operations
19 protected static $testDatastore = 'sandbox/datastore.php'; 19 protected static $testDatastore = 'sandbox/datastore.php';
20
21 /**
22 * @var ReferenceLinkDB instance.
23 */
20 protected static $refDB = null; 24 protected static $refDB = null;
25
26 /**
27 * @var LinkDB public LinkDB instance.
28 */
21 protected static $publicLinkDB = null; 29 protected static $publicLinkDB = null;
30
31 /**
32 * @var LinkDB private LinkDB instance.
33 */
22 protected static $privateLinkDB = null; 34 protected static $privateLinkDB = null;
23 35
24 /** 36 /**
@@ -335,9 +347,10 @@ class LinkDBTest extends PHPUnit_Framework_TestCase
335 public function testFilterString() 347 public function testFilterString()
336 { 348 {
337 $tags = 'dev cartoon'; 349 $tags = 'dev cartoon';
350 $request = array('searchtags' => $tags);
338 $this->assertEquals( 351 $this->assertEquals(
339 2, 352 2,
340 count(self::$privateLinkDB->filter(LinkFilter::$FILTER_TAG, $tags, true, false)) 353 count(self::$privateLinkDB->filterSearch($request, true, false))
341 ); 354 );
342 } 355 }
343 356
@@ -347,9 +360,10 @@ class LinkDBTest extends PHPUnit_Framework_TestCase
347 public function testFilterArray() 360 public function testFilterArray()
348 { 361 {
349 $tags = array('dev', 'cartoon'); 362 $tags = array('dev', 'cartoon');
363 $request = array('searchtags' => $tags);
350 $this->assertEquals( 364 $this->assertEquals(
351 2, 365 2,
352 count(self::$privateLinkDB->filter(LinkFilter::$FILTER_TAG, $tags, true, false)) 366 count(self::$privateLinkDB->filterSearch($request, true, false))
353 ); 367 );
354 } 368 }
355 369
@@ -360,14 +374,48 @@ class LinkDBTest extends PHPUnit_Framework_TestCase
360 public function testHiddenTags() 374 public function testHiddenTags()
361 { 375 {
362 $tags = '.hidden'; 376 $tags = '.hidden';
377 $request = array('searchtags' => $tags);
363 $this->assertEquals( 378 $this->assertEquals(
364 1, 379 1,
365 count(self::$privateLinkDB->filter(LinkFilter::$FILTER_TAG, $tags, true, false)) 380 count(self::$privateLinkDB->filterSearch($request, true, false))
366 ); 381 );
367 382
368 $this->assertEquals( 383 $this->assertEquals(
369 0, 384 0,
370 count(self::$publicLinkDB->filter(LinkFilter::$FILTER_TAG, $tags, true, false)) 385 count(self::$publicLinkDB->filterSearch($request, true, false))
371 ); 386 );
372 } 387 }
388
389 /**
390 * Test filterHash() with a valid smallhash.
391 */
392 public function testFilterHashValid()
393 {
394 $request = smallHash('20150310_114651');
395 $this->assertEquals(
396 1,
397 count(self::$publicLinkDB->filterHash($request))
398 );
399 }
400
401 /**
402 * Test filterHash() with an invalid smallhash.
403 *
404 * @expectedException LinkNotFoundException
405 */
406 public function testFilterHashInValid1()
407 {
408 $request = 'blabla';
409 self::$publicLinkDB->filterHash($request);
410 }
411
412 /**
413 * Test filterHash() with an empty smallhash.
414 *
415 * @expectedException LinkNotFoundException
416 */
417 public function testFilterHashInValid()
418 {
419 self::$publicLinkDB->filterHash('');
420 }
373} 421}
diff --git a/tests/LinkFilterTest.php b/tests/LinkFilterTest.php
index ef1cc10a..1620bb78 100644
--- a/tests/LinkFilterTest.php
+++ b/tests/LinkFilterTest.php
@@ -12,8 +12,6 @@ class LinkFilterTest extends PHPUnit_Framework_TestCase
12 */ 12 */
13 protected static $linkFilter; 13 protected static $linkFilter;
14 14
15 protected static $NB_LINKS_REFDB = 7;
16
17 /** 15 /**
18 * Instanciate linkFilter with ReferenceLinkDB data. 16 * Instanciate linkFilter with ReferenceLinkDB data.
19 */ 17 */
@@ -29,7 +27,7 @@ class LinkFilterTest extends PHPUnit_Framework_TestCase
29 public function testFilter() 27 public function testFilter()
30 { 28 {
31 $this->assertEquals( 29 $this->assertEquals(
32 self::$NB_LINKS_REFDB, 30 ReferenceLinkDB::$NB_LINKS_TOTAL,
33 count(self::$linkFilter->filter('', '')) 31 count(self::$linkFilter->filter('', ''))
34 ); 32 );
35 33
@@ -40,12 +38,12 @@ class LinkFilterTest extends PHPUnit_Framework_TestCase
40 ); 38 );
41 39
42 $this->assertEquals( 40 $this->assertEquals(
43 self::$NB_LINKS_REFDB, 41 ReferenceLinkDB::$NB_LINKS_TOTAL,
44 count(self::$linkFilter->filter(LinkFilter::$FILTER_TAG, '')) 42 count(self::$linkFilter->filter(LinkFilter::$FILTER_TAG, ''))
45 ); 43 );
46 44
47 $this->assertEquals( 45 $this->assertEquals(
48 self::$NB_LINKS_REFDB, 46 ReferenceLinkDB::$NB_LINKS_TOTAL,
49 count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, '')) 47 count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, ''))
50 ); 48 );
51 } 49 }
@@ -167,13 +165,12 @@ class LinkFilterTest extends PHPUnit_Framework_TestCase
167 165
168 /** 166 /**
169 * No link for this hash 167 * No link for this hash
168 *
169 * @expectedException LinkNotFoundException
170 */ 170 */
171 public function testFilterUnknownSmallHash() 171 public function testFilterUnknownSmallHash()
172 { 172 {
173 $this->assertEquals( 173 self::$linkFilter->filter(LinkFilter::$FILTER_HASH, 'Iblaah');
174 0,
175 count(self::$linkFilter->filter(LinkFilter::$FILTER_HASH, 'Iblaah'))
176 );
177 } 174 }
178 175
179 /** 176 /**
@@ -383,7 +380,7 @@ class LinkFilterTest extends PHPUnit_Framework_TestCase
383 )) 380 ))
384 ); 381 );
385 $this->assertEquals( 382 $this->assertEquals(
386 self::$NB_LINKS_REFDB, 383 ReferenceLinkDB::$NB_LINKS_TOTAL,
387 count(self::$linkFilter->filter( 384 count(self::$linkFilter->filter(
388 LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT, 385 LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT,
389 '' 386 ''
diff --git a/tests/LinkUtilsTest.php b/tests/LinkUtilsTest.php
index c2257590..609a80cb 100644
--- a/tests/LinkUtilsTest.php
+++ b/tests/LinkUtilsTest.php
@@ -15,6 +15,8 @@ class LinkUtilsTest extends PHPUnit_Framework_TestCase
15 $title = 'Read me please.'; 15 $title = 'Read me please.';
16 $html = '<html><meta>stuff</meta><title>'. $title .'</title></html>'; 16 $html = '<html><meta>stuff</meta><title>'. $title .'</title></html>';
17 $this->assertEquals($title, html_extract_title($html)); 17 $this->assertEquals($title, html_extract_title($html));
18 $html = '<html><title>'. $title .'</title>blabla<title>another</title></html>';
19 $this->assertEquals($title, html_extract_title($html));
18 } 20 }
19 21
20 /** 22 /**
diff --git a/tests/Updater/UpdaterTest.php b/tests/Updater/UpdaterTest.php
index d865066b..a29d9067 100644
--- a/tests/Updater/UpdaterTest.php
+++ b/tests/Updater/UpdaterTest.php
@@ -236,9 +236,9 @@ class UpdaterTest extends PHPUnit_Framework_TestCase
236 $refDB = new ReferenceLinkDB(); 236 $refDB = new ReferenceLinkDB();
237 $refDB->write(self::$testDatastore); 237 $refDB->write(self::$testDatastore);
238 $linkDB = new LinkDB(self::$testDatastore, true, false); 238 $linkDB = new LinkDB(self::$testDatastore, true, false);
239 $this->assertEmpty($linkDB->filter(LinkFilter::$FILTER_TAG, 'exclude')); 239 $this->assertEmpty($linkDB->filterSearch(array('searchtags' => 'exclude')));
240 $updater = new Updater(array(), self::$configFields, $linkDB, true); 240 $updater = new Updater(array(), self::$configFields, $linkDB, true);
241 $updater->updateMethodRenameDashTags(); 241 $updater->updateMethodRenameDashTags();
242 $this->assertNotEmpty($linkDB->filter(LinkFilter::$FILTER_TAG, 'exclude')); 242 $this->assertNotEmpty($linkDB->filterSearch(array('searchtags' => 'exclude')));
243 } 243 }
244} 244}
diff --git a/tests/utils/ReferenceLinkDB.php b/tests/utils/ReferenceLinkDB.php
index 61faef05..dc4f5dfa 100644
--- a/tests/utils/ReferenceLinkDB.php
+++ b/tests/utils/ReferenceLinkDB.php
@@ -4,6 +4,8 @@
4 */ 4 */
5class ReferenceLinkDB 5class ReferenceLinkDB
6{ 6{
7 public static $NB_LINKS_TOTAL = 7;
8
7 private $_links = array(); 9 private $_links = array();
8 private $_publicCount = 0; 10 private $_publicCount = 0;
9 private $_privateCount = 0; 11 private $_privateCount = 0;
@@ -14,6 +16,15 @@ class ReferenceLinkDB
14 function __construct() 16 function __construct()
15 { 17 {
16 $this->addLink( 18 $this->addLink(
19 'Link title: @website',
20 '?WDWyig',
21 'Stallman has a beard and is part of the Free Software Foundation (or not). Seriously, read this.',
22 0,
23 '20150310_114651',
24 'stuff'
25 );
26
27 $this->addLink(
17 'Free as in Freedom 2.0 @website', 28 'Free as in Freedom 2.0 @website',
18 'https://static.fsf.org/nosvn/faif-2.0.pdf', 29 'https://static.fsf.org/nosvn/faif-2.0.pdf',
19 'Richard Stallman and the Free Software Revolution. Read this.', 30 'Richard Stallman and the Free Software Revolution. Read this.',
@@ -23,15 +34,6 @@ class ReferenceLinkDB
23 ); 34 );
24 35
25 $this->addLink( 36 $this->addLink(
26 'Link title: @website',
27 'local',
28 'Stallman has a beard and is part of the Free Software Foundation (or not). Seriously, read this.',
29 0,
30 '20150310_114651',
31 'stuff'
32 );
33
34 $this->addLink(
35 'MediaGoblin', 37 'MediaGoblin',
36 'http://mediagoblin.org/', 38 'http://mediagoblin.org/',
37 'A free software media publishing platform', 39 'A free software media publishing platform',