aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/FeedBuilderTest.php
diff options
context:
space:
mode:
authorArthur <arthur@hoa.ro>2016-03-25 19:20:55 +0100
committerArthur <arthur@hoa.ro>2016-03-25 19:20:55 +0100
commitf66a1990e5d93a6f302ce594968e5e717b93da72 (patch)
tree22e5da81979bc7ee596fcd5c9c56d4c01c599aa4 /tests/FeedBuilderTest.php
parentb2764886c7b52ed98debb90b2ebf075dec5ae2e8 (diff)
parent528a6f8a232c060faf024008e4f8a09b4aa8dabc (diff)
downloadShaarli-f66a1990e5d93a6f302ce594968e5e717b93da72.tar.gz
Shaarli-f66a1990e5d93a6f302ce594968e5e717b93da72.tar.zst
Shaarli-f66a1990e5d93a6f302ce594968e5e717b93da72.zip
Merge pull request #515 from ArthurHoaro/template-feeds
Refactor RSS feeds generation, and do it through templates
Diffstat (limited to 'tests/FeedBuilderTest.php')
-rw-r--r--tests/FeedBuilderTest.php212
1 files changed, 212 insertions, 0 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}