diff options
Diffstat (limited to 'tests/feed/FeedBuilderTest.php')
-rw-r--r-- | tests/feed/FeedBuilderTest.php | 171 |
1 files changed, 101 insertions, 70 deletions
diff --git a/tests/feed/FeedBuilderTest.php b/tests/feed/FeedBuilderTest.php index b496cb4c..c29e8ef3 100644 --- a/tests/feed/FeedBuilderTest.php +++ b/tests/feed/FeedBuilderTest.php | |||
@@ -4,14 +4,20 @@ namespace Shaarli\Feed; | |||
4 | 4 | ||
5 | use DateTime; | 5 | use DateTime; |
6 | use ReferenceLinkDB; | 6 | use ReferenceLinkDB; |
7 | use Shaarli\Bookmark\Bookmark; | ||
8 | use Shaarli\Bookmark\BookmarkFileService; | ||
7 | use Shaarli\Bookmark\LinkDB; | 9 | use Shaarli\Bookmark\LinkDB; |
10 | use Shaarli\Config\ConfigManager; | ||
11 | use Shaarli\Formatter\FormatterFactory; | ||
12 | use Shaarli\History; | ||
13 | use Shaarli\TestCase; | ||
8 | 14 | ||
9 | /** | 15 | /** |
10 | * FeedBuilderTest class. | 16 | * FeedBuilderTest class. |
11 | * | 17 | * |
12 | * Unit tests for FeedBuilder. | 18 | * Unit tests for FeedBuilder. |
13 | */ | 19 | */ |
14 | class FeedBuilderTest extends \PHPUnit\Framework\TestCase | 20 | class FeedBuilderTest extends TestCase |
15 | { | 21 | { |
16 | /** | 22 | /** |
17 | * @var string locale Basque (Spain). | 23 | * @var string locale Basque (Spain). |
@@ -30,74 +36,70 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase | |||
30 | 36 | ||
31 | protected static $testDatastore = 'sandbox/datastore.php'; | 37 | protected static $testDatastore = 'sandbox/datastore.php'; |
32 | 38 | ||
33 | public static $linkDB; | 39 | public static $bookmarkService; |
40 | |||
41 | public static $formatter; | ||
34 | 42 | ||
35 | public static $serverInfo; | 43 | public static $serverInfo; |
36 | 44 | ||
37 | /** | 45 | /** |
38 | * Called before every test method. | 46 | * Called before every test method. |
39 | */ | 47 | */ |
40 | public static function setUpBeforeClass() | 48 | public static function setUpBeforeClass(): void |
41 | { | 49 | { |
42 | $refLinkDB = new ReferenceLinkDB(); | 50 | $conf = new ConfigManager('tests/utils/config/configJson'); |
51 | $conf->set('resource.datastore', self::$testDatastore); | ||
52 | $refLinkDB = new \ReferenceLinkDB(); | ||
43 | $refLinkDB->write(self::$testDatastore); | 53 | $refLinkDB->write(self::$testDatastore); |
44 | self::$linkDB = new LinkDB(self::$testDatastore, true, false); | 54 | $history = new History('sandbox/history.php'); |
55 | $factory = new FormatterFactory($conf, true); | ||
56 | self::$formatter = $factory->getFormatter(); | ||
57 | self::$bookmarkService = new BookmarkFileService($conf, $history, true); | ||
58 | |||
45 | self::$serverInfo = array( | 59 | self::$serverInfo = array( |
46 | 'HTTPS' => 'Off', | 60 | 'HTTPS' => 'Off', |
47 | 'SERVER_NAME' => 'host.tld', | 61 | 'SERVER_NAME' => 'host.tld', |
48 | 'SERVER_PORT' => '80', | 62 | 'SERVER_PORT' => '80', |
49 | 'SCRIPT_NAME' => '/index.php', | 63 | 'SCRIPT_NAME' => '/index.php', |
50 | 'REQUEST_URI' => '/index.php?do=feed', | 64 | 'REQUEST_URI' => '/feed/atom', |
51 | ); | 65 | ); |
52 | } | 66 | } |
53 | 67 | ||
54 | /** | 68 | /** |
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. | 69 | * Test buildData with RSS feed. |
73 | */ | 70 | */ |
74 | public function testRSSBuildData() | 71 | public function testRSSBuildData() |
75 | { | 72 | { |
76 | $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_RSS, self::$serverInfo, null, false); | 73 | $feedBuilder = new FeedBuilder( |
74 | self::$bookmarkService, | ||
75 | self::$formatter, | ||
76 | static::$serverInfo, | ||
77 | false | ||
78 | ); | ||
77 | $feedBuilder->setLocale(self::$LOCALE); | 79 | $feedBuilder->setLocale(self::$LOCALE); |
78 | $data = $feedBuilder->buildData(); | 80 | $data = $feedBuilder->buildData(FeedBuilder::$FEED_RSS, null); |
79 | // Test headers (RSS) | 81 | // Test headers (RSS) |
80 | $this->assertEquals(self::$RSS_LANGUAGE, $data['language']); | 82 | $this->assertEquals(self::$RSS_LANGUAGE, $data['language']); |
81 | $this->assertRegExp('/Wed, 03 Aug 2016 09:30:33 \+\d{4}/', $data['last_update']); | 83 | $this->assertRegExp('/Wed, 03 Aug 2016 09:30:33 \+\d{4}/', $data['last_update']); |
82 | $this->assertEquals(true, $data['show_dates']); | 84 | $this->assertEquals(true, $data['show_dates']); |
83 | $this->assertEquals('http://host.tld/index.php?do=feed', $data['self_link']); | 85 | $this->assertEquals('http://host.tld/feed/atom', $data['self_link']); |
84 | $this->assertEquals('http://host.tld/', $data['index_url']); | 86 | $this->assertEquals('http://host.tld/', $data['index_url']); |
85 | $this->assertFalse($data['usepermalinks']); | 87 | $this->assertFalse($data['usepermalinks']); |
86 | $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links'])); | 88 | $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links'])); |
87 | 89 | ||
88 | // Test first not pinned link (note link) | 90 | // Test first not pinned link (note link) |
89 | $link = $data['links'][array_keys($data['links'])[2]]; | 91 | $link = $data['links'][array_keys($data['links'])[0]]; |
90 | $this->assertEquals(41, $link['id']); | 92 | $this->assertEquals(41, $link['id']); |
91 | $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114651'), $link['created']); | 93 | $this->assertEquals(DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114651'), $link['created']); |
92 | $this->assertEquals('http://host.tld/?WDWyig', $link['guid']); | 94 | $this->assertEquals('http://host.tld/shaare/WDWyig', $link['guid']); |
93 | $this->assertEquals('http://host.tld/?WDWyig', $link['url']); | 95 | $this->assertEquals('http://host.tld/shaare/WDWyig', $link['url']); |
94 | $this->assertRegExp('/Tue, 10 Mar 2015 11:46:51 \+\d{4}/', $link['pub_iso_date']); | 96 | $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']); | 97 | $pub = DateTime::createFromFormat(DateTime::RSS, $link['pub_iso_date']); |
96 | $up = DateTime::createFromFormat(DateTime::ATOM, $link['up_iso_date']); | 98 | $up = DateTime::createFromFormat(DateTime::ATOM, $link['up_iso_date']); |
97 | $this->assertEquals($pub, $up); | 99 | $this->assertEquals($pub, $up); |
98 | $this->assertContains('Stallman has a beard', $link['description']); | 100 | $this->assertContainsPolyfill('Stallman has a beard', $link['description']); |
99 | $this->assertContains('Permalink', $link['description']); | 101 | $this->assertContainsPolyfill('Permalink', $link['description']); |
100 | $this->assertContains('http://host.tld/?WDWyig', $link['description']); | 102 | $this->assertContainsPolyfill('http://host.tld/shaare/WDWyig', $link['description']); |
101 | $this->assertEquals(1, count($link['taglist'])); | 103 | $this->assertEquals(1, count($link['taglist'])); |
102 | $this->assertEquals('sTuff', $link['taglist'][0]); | 104 | $this->assertEquals('sTuff', $link['taglist'][0]); |
103 | 105 | ||
@@ -117,12 +119,17 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase | |||
117 | */ | 119 | */ |
118 | public function testAtomBuildData() | 120 | public function testAtomBuildData() |
119 | { | 121 | { |
120 | $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, false); | 122 | $feedBuilder = new FeedBuilder( |
123 | self::$bookmarkService, | ||
124 | self::$formatter, | ||
125 | static::$serverInfo, | ||
126 | false | ||
127 | ); | ||
121 | $feedBuilder->setLocale(self::$LOCALE); | 128 | $feedBuilder->setLocale(self::$LOCALE); |
122 | $data = $feedBuilder->buildData(); | 129 | $data = $feedBuilder->buildData(FeedBuilder::$FEED_ATOM, null); |
123 | $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links'])); | 130 | $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links'])); |
124 | $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['last_update']); | 131 | $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['last_update']); |
125 | $link = $data['links'][array_keys($data['links'])[2]]; | 132 | $link = $data['links'][array_keys($data['links'])[0]]; |
126 | $this->assertRegExp('/2015-03-10T11:46:51\+\d{2}:\d{2}/', $link['pub_iso_date']); | 133 | $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']); | 134 | $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['links'][8]['up_iso_date']); |
128 | } | 135 | } |
@@ -136,13 +143,18 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase | |||
136 | 'searchtags' => 'stuff', | 143 | 'searchtags' => 'stuff', |
137 | 'searchterm' => 'beard', | 144 | 'searchterm' => 'beard', |
138 | ); | 145 | ); |
139 | $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, $criteria, false); | 146 | $feedBuilder = new FeedBuilder( |
147 | self::$bookmarkService, | ||
148 | self::$formatter, | ||
149 | static::$serverInfo, | ||
150 | false | ||
151 | ); | ||
140 | $feedBuilder->setLocale(self::$LOCALE); | 152 | $feedBuilder->setLocale(self::$LOCALE); |
141 | $data = $feedBuilder->buildData(); | 153 | $data = $feedBuilder->buildData(FeedBuilder::$FEED_ATOM, $criteria); |
142 | $this->assertEquals(1, count($data['links'])); | 154 | $this->assertEquals(1, count($data['links'])); |
143 | $link = array_shift($data['links']); | 155 | $link = array_shift($data['links']); |
144 | $this->assertEquals(41, $link['id']); | 156 | $this->assertEquals(41, $link['id']); |
145 | $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114651'), $link['created']); | 157 | $this->assertEquals(DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114651'), $link['created']); |
146 | } | 158 | } |
147 | 159 | ||
148 | /** | 160 | /** |
@@ -153,13 +165,18 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase | |||
153 | $criteria = array( | 165 | $criteria = array( |
154 | 'nb' => '3', | 166 | 'nb' => '3', |
155 | ); | 167 | ); |
156 | $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, $criteria, false); | 168 | $feedBuilder = new FeedBuilder( |
169 | self::$bookmarkService, | ||
170 | self::$formatter, | ||
171 | static::$serverInfo, | ||
172 | false | ||
173 | ); | ||
157 | $feedBuilder->setLocale(self::$LOCALE); | 174 | $feedBuilder->setLocale(self::$LOCALE); |
158 | $data = $feedBuilder->buildData(); | 175 | $data = $feedBuilder->buildData(FeedBuilder::$FEED_ATOM, $criteria); |
159 | $this->assertEquals(3, count($data['links'])); | 176 | $this->assertEquals(3, count($data['links'])); |
160 | $link = $data['links'][array_keys($data['links'])[2]]; | 177 | $link = $data['links'][array_keys($data['links'])[0]]; |
161 | $this->assertEquals(41, $link['id']); | 178 | $this->assertEquals(41, $link['id']); |
162 | $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114651'), $link['created']); | 179 | $this->assertEquals(DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114651'), $link['created']); |
163 | } | 180 | } |
164 | 181 | ||
165 | /** | 182 | /** |
@@ -167,28 +184,33 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase | |||
167 | */ | 184 | */ |
168 | public function testBuildDataPermalinks() | 185 | public function testBuildDataPermalinks() |
169 | { | 186 | { |
170 | $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, false); | 187 | $feedBuilder = new FeedBuilder( |
188 | self::$bookmarkService, | ||
189 | self::$formatter, | ||
190 | static::$serverInfo, | ||
191 | false | ||
192 | ); | ||
171 | $feedBuilder->setLocale(self::$LOCALE); | 193 | $feedBuilder->setLocale(self::$LOCALE); |
172 | $feedBuilder->setUsePermalinks(true); | 194 | $feedBuilder->setUsePermalinks(true); |
173 | $data = $feedBuilder->buildData(); | 195 | $data = $feedBuilder->buildData(FeedBuilder::$FEED_ATOM, null); |
174 | $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links'])); | 196 | $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links'])); |
175 | $this->assertTrue($data['usepermalinks']); | 197 | $this->assertTrue($data['usepermalinks']); |
176 | // First link is a permalink | 198 | // First link is a permalink |
177 | $link = $data['links'][array_keys($data['links'])[2]]; | 199 | $link = $data['links'][array_keys($data['links'])[0]]; |
178 | $this->assertEquals(41, $link['id']); | 200 | $this->assertEquals(41, $link['id']); |
179 | $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114651'), $link['created']); | 201 | $this->assertEquals(DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114651'), $link['created']); |
180 | $this->assertEquals('http://host.tld/?WDWyig', $link['guid']); | 202 | $this->assertEquals('http://host.tld/shaare/WDWyig', $link['guid']); |
181 | $this->assertEquals('http://host.tld/?WDWyig', $link['url']); | 203 | $this->assertEquals('http://host.tld/shaare/WDWyig', $link['url']); |
182 | $this->assertContains('Direct link', $link['description']); | 204 | $this->assertContainsPolyfill('Direct link', $link['description']); |
183 | $this->assertContains('http://host.tld/?WDWyig', $link['description']); | 205 | $this->assertContainsPolyfill('http://host.tld/shaare/WDWyig', $link['description']); |
184 | // Second link is a direct link | 206 | // Second link is a direct link |
185 | $link = $data['links'][array_keys($data['links'])[3]]; | 207 | $link = $data['links'][array_keys($data['links'])[1]]; |
186 | $this->assertEquals(8, $link['id']); | 208 | $this->assertEquals(8, $link['id']); |
187 | $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114633'), $link['created']); | 209 | $this->assertEquals(DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114633'), $link['created']); |
188 | $this->assertEquals('http://host.tld/?RttfEw', $link['guid']); | 210 | $this->assertEquals('http://host.tld/shaare/RttfEw', $link['guid']); |
189 | $this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['url']); | 211 | $this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['url']); |
190 | $this->assertContains('Direct link', $link['description']); | 212 | $this->assertContainsPolyfill('Direct link', $link['description']); |
191 | $this->assertContains('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['description']); | 213 | $this->assertContainsPolyfill('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['description']); |
192 | } | 214 | } |
193 | 215 | ||
194 | /** | 216 | /** |
@@ -196,18 +218,28 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase | |||
196 | */ | 218 | */ |
197 | public function testBuildDataHideDates() | 219 | public function testBuildDataHideDates() |
198 | { | 220 | { |
199 | $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, false); | 221 | $feedBuilder = new FeedBuilder( |
222 | self::$bookmarkService, | ||
223 | self::$formatter, | ||
224 | static::$serverInfo, | ||
225 | false | ||
226 | ); | ||
200 | $feedBuilder->setLocale(self::$LOCALE); | 227 | $feedBuilder->setLocale(self::$LOCALE); |
201 | $feedBuilder->setHideDates(true); | 228 | $feedBuilder->setHideDates(true); |
202 | $data = $feedBuilder->buildData(); | 229 | $data = $feedBuilder->buildData(FeedBuilder::$FEED_ATOM, null); |
203 | $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links'])); | 230 | $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links'])); |
204 | $this->assertFalse($data['show_dates']); | 231 | $this->assertFalse($data['show_dates']); |
205 | 232 | ||
206 | // Show dates while logged in | 233 | // Show dates while logged in |
207 | $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, true); | 234 | $feedBuilder = new FeedBuilder( |
235 | self::$bookmarkService, | ||
236 | self::$formatter, | ||
237 | static::$serverInfo, | ||
238 | true | ||
239 | ); | ||
208 | $feedBuilder->setLocale(self::$LOCALE); | 240 | $feedBuilder->setLocale(self::$LOCALE); |
209 | $feedBuilder->setHideDates(true); | 241 | $feedBuilder->setHideDates(true); |
210 | $data = $feedBuilder->buildData(); | 242 | $data = $feedBuilder->buildData(FeedBuilder::$FEED_ATOM, null); |
211 | $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links'])); | 243 | $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links'])); |
212 | $this->assertTrue($data['show_dates']); | 244 | $this->assertTrue($data['show_dates']); |
213 | } | 245 | } |
@@ -222,27 +254,26 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase | |||
222 | 'SERVER_NAME' => 'host.tld', | 254 | 'SERVER_NAME' => 'host.tld', |
223 | 'SERVER_PORT' => '8080', | 255 | 'SERVER_PORT' => '8080', |
224 | 'SCRIPT_NAME' => '/~user/shaarli/index.php', | 256 | 'SCRIPT_NAME' => '/~user/shaarli/index.php', |
225 | 'REQUEST_URI' => '/~user/shaarli/index.php?do=feed', | 257 | 'REQUEST_URI' => '/~user/shaarli/feed/atom', |
226 | ); | 258 | ); |
227 | $feedBuilder = new FeedBuilder( | 259 | $feedBuilder = new FeedBuilder( |
228 | self::$linkDB, | 260 | self::$bookmarkService, |
229 | FeedBuilder::$FEED_ATOM, | 261 | self::$formatter, |
230 | $serverInfo, | 262 | $serverInfo, |
231 | null, | ||
232 | false | 263 | false |
233 | ); | 264 | ); |
234 | $feedBuilder->setLocale(self::$LOCALE); | 265 | $feedBuilder->setLocale(self::$LOCALE); |
235 | $data = $feedBuilder->buildData(); | 266 | $data = $feedBuilder->buildData(FeedBuilder::$FEED_ATOM, null); |
236 | 267 | ||
237 | $this->assertEquals( | 268 | $this->assertEquals( |
238 | 'http://host.tld:8080/~user/shaarli/index.php?do=feed', | 269 | 'http://host.tld:8080/~user/shaarli/feed/atom', |
239 | $data['self_link'] | 270 | $data['self_link'] |
240 | ); | 271 | ); |
241 | 272 | ||
242 | // Test first link (note link) | 273 | // Test first link (note link) |
243 | $link = $data['links'][array_keys($data['links'])[2]]; | 274 | $link = $data['links'][array_keys($data['links'])[0]]; |
244 | $this->assertEquals('http://host.tld:8080/~user/shaarli/?WDWyig', $link['guid']); | 275 | $this->assertEquals('http://host.tld:8080/~user/shaarli/shaare/WDWyig', $link['guid']); |
245 | $this->assertEquals('http://host.tld:8080/~user/shaarli/?WDWyig', $link['url']); | 276 | $this->assertEquals('http://host.tld:8080/~user/shaarli/shaare/WDWyig', $link['url']); |
246 | $this->assertContains('http://host.tld:8080/~user/shaarli/?addtag=hashtag', $link['description']); | 277 | $this->assertContainsPolyfill('http://host.tld:8080/~user/shaarli/./add-tag/hashtag', $link['description']); |
247 | } | 278 | } |
248 | } | 279 | } |