diff options
-rw-r--r-- | tests/FeedBuilderTest.php | 212 | ||||
-rw-r--r-- | tests/LinkFilterTest.php | 10 | ||||
-rw-r--r-- | tests/utils/ReferenceLinkDB.php | 20 |
3 files changed, 227 insertions, 15 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 | |||
3 | require_once 'application/FeedBuilder.php'; | ||
4 | require_once 'application/LinkDB.php'; | ||
5 | |||
6 | /** | ||
7 | * FeedBuilderTest class. | ||
8 | * | ||
9 | * Unit tests for FeedBuilder. | ||
10 | */ | ||
11 | class 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/LinkFilterTest.php b/tests/LinkFilterTest.php index ef1cc10a..f991a9c9 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 | } |
@@ -383,7 +381,7 @@ class LinkFilterTest extends PHPUnit_Framework_TestCase | |||
383 | )) | 381 | )) |
384 | ); | 382 | ); |
385 | $this->assertEquals( | 383 | $this->assertEquals( |
386 | self::$NB_LINKS_REFDB, | 384 | ReferenceLinkDB::$NB_LINKS_TOTAL, |
387 | count(self::$linkFilter->filter( | 385 | count(self::$linkFilter->filter( |
388 | LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT, | 386 | LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT, |
389 | '' | 387 | '' |
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 | */ |
5 | class ReferenceLinkDB | 5 | class 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', |