aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/FeedBuilderTest.php
blob: ea1dde25cd09674775b4f5f2effcdb8a0d8f8e33 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<?php

require_once 'application/FeedBuilder.php';
require_once 'application/LinkDB.php';

/**
 * FeedBuilderTest class.
 *
 * Unit tests for FeedBuilder.
 */
class FeedBuilderTest extends PHPUnit_Framework_TestCase
{
    /**
     * @var string locale Basque (Spain).
     */
    public static $LOCALE = 'eu_ES';

    /**
     * @var string language in RSS format.
     */
    public static $RSS_LANGUAGE = 'eu-es';

    /**
     * @var string language in ATOM format.
     */
    public static $ATOM_LANGUAGUE = 'eu';

    protected static $testDatastore = 'sandbox/datastore.php';

    public static $linkDB;

    public static $serverInfo;

    /**
     * Called before every test method.
     */
    public static function setUpBeforeClass()
    {
        $refLinkDB = new ReferenceLinkDB();
        $refLinkDB->write(self::$testDatastore);
        self::$linkDB = new LinkDB(self::$testDatastore, true, false);
        self::$serverInfo = array(
            'HTTPS' => 'Off',
            'SERVER_NAME' => 'host.tld',
            'SERVER_PORT' => '80',
            'SCRIPT_NAME' => '/index.php',
            'REQUEST_URI' => '/index.php?do=feed',
        );
    }

    /**
     * Test GetTypeLanguage().
     */
    public function testGetTypeLanguage()
    {
        $feedBuilder = new FeedBuilder(null, FeedBuilder::$FEED_ATOM, null, null, false);
        $feedBuilder->setLocale(self::$LOCALE);
        $this->assertEquals(self::$ATOM_LANGUAGUE, $feedBuilder->getTypeLanguage());
        $feedBuilder = new FeedBuilder(null, FeedBuilder::$FEED_RSS, null, null, false);
        $feedBuilder->setLocale(self::$LOCALE);
        $this->assertEquals(self::$RSS_LANGUAGE, $feedBuilder->getTypeLanguage());
        $feedBuilder = new FeedBuilder(null, FeedBuilder::$FEED_ATOM, null, null, false);
        $this->assertEquals('en', $feedBuilder->getTypeLanguage());
        $feedBuilder = new FeedBuilder(null, FeedBuilder::$FEED_RSS, null, null, false);
        $this->assertEquals('en-en', $feedBuilder->getTypeLanguage());
    }

    /**
     * Test buildData with RSS feed.
     */
    public function testRSSBuildData()
    {
        $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_RSS, self::$serverInfo, null, false);
        $feedBuilder->setLocale(self::$LOCALE);
        $data = $feedBuilder->buildData();
        // Test headers (RSS)
        $this->assertEquals(self::$RSS_LANGUAGE, $data['language']);
        $this->assertEmpty($data['pubsubhub_url']);
        $this->assertRegExp('/Wed, 03 Aug 2016 09:30:33 \+\d{4}/', $data['last_update']);
        $this->assertEquals(true, $data['show_dates']);
        $this->assertEquals('http://host.tld/index.php?do=feed', $data['self_link']);
        $this->assertEquals('http://host.tld/', $data['index_url']);
        $this->assertFalse($data['usepermalinks']);
        $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));

        // Test first link (note link)
        $link = reset($data['links']);
        $this->assertEquals(41, $link['id']);
        $this->assertEquals(DateTime::createFromFormat('Ymd_His', '20150310_114651'), $link['created']);
        $this->assertEquals('http://host.tld/?WDWyig', $link['guid']);
        $this->assertEquals('http://host.tld/?WDWyig', $link['url']);
        $this->assertRegExp('/Tue, 10 Mar 2015 11:46:51 \+\d{4}/', $link['pub_iso_date']);
        $pub = DateTime::createFromFormat(DateTime::RSS, $link['pub_iso_date']);
        $up  = DateTime::createFromFormat(DateTime::ATOM, $link['up_iso_date']);
        $this->assertEquals($pub, $up);
        $this->assertContains('Stallman has a beard', $link['description']);
        $this->assertContains('Permalink', $link['description']);
        $this->assertContains('http://host.tld/?WDWyig', $link['description']);
        $this->assertEquals(1, count($link['taglist']));
        $this->assertEquals('sTuff', $link['taglist'][0]);

        // Test URL with external link.
        $this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $data['links'][8]['url']);

        // Test multitags.
        $this->assertEquals(5, count($data['links'][6]['taglist']));
        $this->assertEquals('css', $data['links'][6]['taglist'][0]);

        // Test update date
        $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['links'][8]['up_iso_date']);
    }

    /**
     * Test buildData with ATOM feed (test only specific to ATOM).
     */
    public function testAtomBuildData()
    {
        $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, false);
        $feedBuilder->setLocale(self::$LOCALE);
        $data = $feedBuilder->buildData();
        $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
        $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['last_update']);
        $link = reset($data['links']);
        $this->assertRegExp('/2015-03-10T11:46:51\+\d{2}:\d{2}/', $link['pub_iso_date']);
        $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['links'][8]['up_iso_date']);
    }

    /**
     * Test buildData with search criteria.
     */
    public function testBuildDataFiltered()
    {
        $criteria = array(
            'searchtags' => 'stuff',
            'searchterm' => 'beard',
        );
        $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, $criteria, false);
        $feedBuilder->setLocale(self::$LOCALE);
        $data = $feedBuilder->buildData();
        $this->assertEquals(1, count($data['links']));
        $link = array_shift($data['links']);
        $this->assertEquals(41, $link['id']);
        $this->assertEquals(DateTime::createFromFormat('Ymd_His', '20150310_114651'), $link['created']);
    }

    /**
     * Test buildData with nb limit.
     */
    public function testBuildDataCount()
    {
        $criteria = array(
            'nb' => '1',
        );
        $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, $criteria, false);
        $feedBuilder->setLocale(self::$LOCALE);
        $data = $feedBuilder->buildData();
        $this->assertEquals(1, count($data['links']));
        $link = array_shift($data['links']);
        $this->assertEquals(41, $link['id']);
        $this->assertEquals(DateTime::createFromFormat('Ymd_His', '20150310_114651'), $link['created']);
    }

    /**
     * Test buildData with permalinks on.
     */
    public function testBuildDataPermalinks()
    {
        $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, false);
        $feedBuilder->setLocale(self::$LOCALE);
        $feedBuilder->setUsePermalinks(true);
        $data = $feedBuilder->buildData();
        $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
        $this->assertTrue($data['usepermalinks']);
        // First link is a permalink
        $link = array_shift($data['links']);
        $this->assertEquals(41, $link['id']);
        $this->assertEquals(DateTime::createFromFormat('Ymd_His', '20150310_114651'), $link['created']);
        $this->assertEquals('http://host.tld/?WDWyig', $link['guid']);
        $this->assertEquals('http://host.tld/?WDWyig', $link['url']);
        $this->assertContains('Direct link', $link['description']);
        $this->assertContains('http://host.tld/?WDWyig', $link['description']);
        // Second link is a direct link
        $link = array_shift($data['links']);
        $this->assertEquals(8, $link['id']);
        $this->assertEquals(DateTime::createFromFormat('Ymd_His', '20150310_114633'), $link['created']);
        $this->assertEquals('http://host.tld/?RttfEw', $link['guid']);
        $this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['url']);
        $this->assertContains('Direct link', $link['description']);
        $this->assertContains('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['description']);
    }

    /**
     * Test buildData with hide dates settings.
     */
    public function testBuildDataHideDates()
    {
        $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, false);
        $feedBuilder->setLocale(self::$LOCALE);
        $feedBuilder->setHideDates(true);
        $data = $feedBuilder->buildData();
        $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
        $this->assertFalse($data['show_dates']);

        // Show dates while logged in
        $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, true);
        $feedBuilder->setLocale(self::$LOCALE);
        $feedBuilder->setHideDates(true);
        $data = $feedBuilder->buildData();
        $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
        $this->assertTrue($data['show_dates']);
    }

    /**
     * Test buildData with hide dates settings.
     */
    public function testBuildDataPubsubhub()
    {
        $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, false);
        $feedBuilder->setLocale(self::$LOCALE);
        $feedBuilder->setPubsubhubUrl('http://pubsubhub.io');
        $data = $feedBuilder->buildData();
        $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
        $this->assertEquals('http://pubsubhub.io', $data['pubsubhub_url']);
    }

    /**
     * Test buildData when Shaarli is served from a subdirectory
     */
    public function testBuildDataServerSubdir()
    {
        $serverInfo = array(
            'HTTPS' => 'Off',
            'SERVER_NAME' => 'host.tld',
            'SERVER_PORT' => '8080',
            'SCRIPT_NAME' => '/~user/shaarli/index.php',
            'REQUEST_URI' => '/~user/shaarli/index.php?do=feed',
        );
        $feedBuilder = new FeedBuilder(
            self::$linkDB,
            FeedBuilder::$FEED_ATOM,
            $serverInfo,
            null,
            false
        );
        $feedBuilder->setLocale(self::$LOCALE);
        $data = $feedBuilder->buildData();

        $this->assertEquals(
            'http://host.tld:8080/~user/shaarli/index.php?do=feed',
            $data['self_link']
        );

        // Test first link (note link)
        $link = array_shift($data['links']);
        $this->assertEquals('http://host.tld:8080/~user/shaarli/?WDWyig', $link['guid']);
        $this->assertEquals('http://host.tld:8080/~user/shaarli/?WDWyig', $link['url']);
        $this->assertContains('http://host.tld:8080/~user/shaarli/?addtag=hashtag', $link['description']);
    }
}