aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-01-18 10:01:06 +0100
committerGitHub <noreply@github.com>2020-01-18 10:01:06 +0100
commit3fb29fdda04ca86e04422d49b86cf646d53c4f9d (patch)
treeadf8512f93f5559ba87d0c9931969ae4ebea7133 /tests
parent796c4c57d085ae4589b53dfe8369ae9ba30ffdaf (diff)
parente26e2060f5470ce8bf4c5973284bae07b8af170a (diff)
downloadShaarli-3fb29fdda04ca86e04422d49b86cf646d53c4f9d.tar.gz
Shaarli-3fb29fdda04ca86e04422d49b86cf646d53c4f9d.tar.zst
Shaarli-3fb29fdda04ca86e04422d49b86cf646d53c4f9d.zip
Store bookmarks as PHP objects and add a service layer to retriā€¦ (#1307)
Store bookmarks as PHP objects and add a service layer to retrieve them
Diffstat (limited to 'tests')
-rw-r--r--tests/HistoryTest.php240
-rw-r--r--tests/api/ApiMiddlewareTest.php8
-rw-r--r--tests/api/ApiUtilsTest.php67
-rw-r--r--tests/api/controllers/history/HistoryTest.php4
-rw-r--r--tests/api/controllers/info/InfoTest.php20
-rw-r--r--tests/api/controllers/links/DeleteLinkTest.php22
-rw-r--r--tests/api/controllers/links/GetLinkIdTest.php11
-rw-r--r--tests/api/controllers/links/GetLinksTest.php15
-rw-r--r--tests/api/controllers/links/PostLinkTest.php30
-rw-r--r--tests/api/controllers/links/PutLinkTest.php24
-rw-r--r--tests/api/controllers/tags/DeleteTagTest.php29
-rw-r--r--tests/api/controllers/tags/GetTagNameTest.php8
-rw-r--r--tests/api/controllers/tags/GetTagsTest.php23
-rw-r--r--tests/api/controllers/tags/PutTagTest.php25
-rw-r--r--tests/bookmark/BookmarkArrayTest.php239
-rw-r--r--tests/bookmark/BookmarkFileServiceTest.php1042
-rw-r--r--tests/bookmark/BookmarkFilterTest.php514
-rw-r--r--tests/bookmark/BookmarkInitializerTest.php120
-rw-r--r--tests/bookmark/BookmarkTest.php388
-rw-r--r--tests/bookmark/LinkUtilsTest.php9
-rw-r--r--tests/bootstrap.php18
-rw-r--r--tests/config/ConfigJsonTest.php2
-rw-r--r--tests/feed/FeedBuilderTest.php103
-rw-r--r--tests/formatter/BookmarkDefaultFormatterTest.php156
-rw-r--r--tests/formatter/BookmarkMarkdownFormatterTest.php160
-rw-r--r--tests/formatter/BookmarkRawFormatterTest.php97
-rw-r--r--tests/formatter/FormatterFactoryTest.php101
-rw-r--r--tests/legacy/LegacyDummyUpdater.php74
-rw-r--r--tests/legacy/LegacyLinkDBTest.php (renamed from tests/bookmark/LinkDBTest.php)72
-rw-r--r--tests/legacy/LegacyLinkFilterTest.php (renamed from tests/bookmark/LinkFilterTest.php)128
-rw-r--r--tests/legacy/LegacyUpdaterTest.php886
-rw-r--r--tests/netscape/BookmarkExportTest.php70
-rw-r--r--tests/netscape/BookmarkImportTest.php570
-rw-r--r--tests/plugins/PluginArchiveorgTest.php4
-rw-r--r--tests/plugins/PluginIssoTest.php12
-rw-r--r--tests/plugins/PluginMarkdownTest.php316
-rw-r--r--tests/updater/DummyUpdater.php13
-rw-r--r--tests/updater/UpdaterTest.php684
-rw-r--r--tests/utils/FakeBookmarkService.php18
-rw-r--r--tests/utils/ReferenceHistory.php2
-rw-r--r--tests/utils/ReferenceLinkDB.php111
-rw-r--r--tests/utils/config/configJson.json.php6
42 files changed, 4705 insertions, 1736 deletions
diff --git a/tests/HistoryTest.php b/tests/HistoryTest.php
index 8303e53a..7189c3a9 100644
--- a/tests/HistoryTest.php
+++ b/tests/HistoryTest.php
@@ -4,6 +4,7 @@ namespace Shaarli;
4 4
5use DateTime; 5use DateTime;
6use Exception; 6use Exception;
7use Shaarli\Bookmark\Bookmark;
7 8
8class HistoryTest extends \PHPUnit\Framework\TestCase 9class HistoryTest extends \PHPUnit\Framework\TestCase
9{ 10{
@@ -15,9 +16,11 @@ class HistoryTest extends \PHPUnit\Framework\TestCase
15 /** 16 /**
16 * Delete history file. 17 * Delete history file.
17 */ 18 */
18 public function tearDown() 19 public function setUp()
19 { 20 {
20 @unlink(self::$historyFilePath); 21 if (file_exists(self::$historyFilePath)) {
22 unlink(self::$historyFilePath);
23 }
21 } 24 }
22 25
23 /** 26 /**
@@ -73,137 +76,140 @@ class HistoryTest extends \PHPUnit\Framework\TestCase
73 public function testAddLink() 76 public function testAddLink()
74 { 77 {
75 $history = new History(self::$historyFilePath); 78 $history = new History(self::$historyFilePath);
76 $history->addLink(['id' => 0]); 79 $bookmark = (new Bookmark())->setId(0);
80 $history->addLink($bookmark);
77 $actual = $history->getHistory()[0]; 81 $actual = $history->getHistory()[0];
78 $this->assertEquals(History::CREATED, $actual['event']); 82 $this->assertEquals(History::CREATED, $actual['event']);
79 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']); 83 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
80 $this->assertEquals(0, $actual['id']); 84 $this->assertEquals(0, $actual['id']);
81 85
82 $history = new History(self::$historyFilePath); 86 $history = new History(self::$historyFilePath);
83 $history->addLink(['id' => 1]); 87 $bookmark = (new Bookmark())->setId(1);
88 $history->addLink($bookmark);
84 $actual = $history->getHistory()[0]; 89 $actual = $history->getHistory()[0];
85 $this->assertEquals(History::CREATED, $actual['event']); 90 $this->assertEquals(History::CREATED, $actual['event']);
86 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']); 91 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
87 $this->assertEquals(1, $actual['id']); 92 $this->assertEquals(1, $actual['id']);
88 93
89 $history = new History(self::$historyFilePath); 94 $history = new History(self::$historyFilePath);
90 $history->addLink(['id' => 'str']); 95 $bookmark = (new Bookmark())->setId('str');
96 $history->addLink($bookmark);
91 $actual = $history->getHistory()[0]; 97 $actual = $history->getHistory()[0];
92 $this->assertEquals(History::CREATED, $actual['event']); 98 $this->assertEquals(History::CREATED, $actual['event']);
93 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']); 99 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
94 $this->assertEquals('str', $actual['id']); 100 $this->assertEquals('str', $actual['id']);
95 } 101 }
96 102
97 /** 103// /**
98 * Test updated link event 104// * Test updated link event
99 */ 105// */
100 public function testUpdateLink() 106// public function testUpdateLink()
101 { 107// {
102 $history = new History(self::$historyFilePath); 108// $history = new History(self::$historyFilePath);
103 $history->updateLink(['id' => 1]); 109// $history->updateLink(['id' => 1]);
104 $actual = $history->getHistory()[0]; 110// $actual = $history->getHistory()[0];
105 $this->assertEquals(History::UPDATED, $actual['event']); 111// $this->assertEquals(History::UPDATED, $actual['event']);
106 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']); 112// $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
107 $this->assertEquals(1, $actual['id']); 113// $this->assertEquals(1, $actual['id']);
108 } 114// }
109 115//
110 /** 116// /**
111 * Test delete link event 117// * Test delete link event
112 */ 118// */
113 public function testDeleteLink() 119// public function testDeleteLink()
114 { 120// {
115 $history = new History(self::$historyFilePath); 121// $history = new History(self::$historyFilePath);
116 $history->deleteLink(['id' => 1]); 122// $history->deleteLink(['id' => 1]);
117 $actual = $history->getHistory()[0]; 123// $actual = $history->getHistory()[0];
118 $this->assertEquals(History::DELETED, $actual['event']); 124// $this->assertEquals(History::DELETED, $actual['event']);
119 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']); 125// $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
120 $this->assertEquals(1, $actual['id']); 126// $this->assertEquals(1, $actual['id']);
121 } 127// }
122 128//
123 /** 129// /**
124 * Test updated settings event 130// * Test updated settings event
125 */ 131// */
126 public function testUpdateSettings() 132// public function testUpdateSettings()
127 { 133// {
128 $history = new History(self::$historyFilePath); 134// $history = new History(self::$historyFilePath);
129 $history->updateSettings(); 135// $history->updateSettings();
130 $actual = $history->getHistory()[0]; 136// $actual = $history->getHistory()[0];
131 $this->assertEquals(History::SETTINGS, $actual['event']); 137// $this->assertEquals(History::SETTINGS, $actual['event']);
132 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']); 138// $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
133 $this->assertEmpty($actual['id']); 139// $this->assertEmpty($actual['id']);
134 } 140// }
135 141//
136 /** 142// /**
137 * Make sure that new items are stored at the beginning 143// * Make sure that new items are stored at the beginning
138 */ 144// */
139 public function testHistoryOrder() 145// public function testHistoryOrder()
140 { 146// {
141 $history = new History(self::$historyFilePath); 147// $history = new History(self::$historyFilePath);
142 $history->updateLink(['id' => 1]); 148// $history->updateLink(['id' => 1]);
143 $actual = $history->getHistory()[0]; 149// $actual = $history->getHistory()[0];
144 $this->assertEquals(History::UPDATED, $actual['event']); 150// $this->assertEquals(History::UPDATED, $actual['event']);
145 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']); 151// $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
146 $this->assertEquals(1, $actual['id']); 152// $this->assertEquals(1, $actual['id']);
147 153//
148 $history->addLink(['id' => 1]); 154// $history->addLink(['id' => 1]);
149 $actual = $history->getHistory()[0]; 155// $actual = $history->getHistory()[0];
150 $this->assertEquals(History::CREATED, $actual['event']); 156// $this->assertEquals(History::CREATED, $actual['event']);
151 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']); 157// $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
152 $this->assertEquals(1, $actual['id']); 158// $this->assertEquals(1, $actual['id']);
153 } 159// }
154 160//
155 /** 161// /**
156 * Re-read history from file after writing an event 162// * Re-read history from file after writing an event
157 */ 163// */
158 public function testHistoryRead() 164// public function testHistoryRead()
159 { 165// {
160 $history = new History(self::$historyFilePath); 166// $history = new History(self::$historyFilePath);
161 $history->updateLink(['id' => 1]); 167// $history->updateLink(['id' => 1]);
162 $history = new History(self::$historyFilePath); 168// $history = new History(self::$historyFilePath);
163 $actual = $history->getHistory()[0]; 169// $actual = $history->getHistory()[0];
164 $this->assertEquals(History::UPDATED, $actual['event']); 170// $this->assertEquals(History::UPDATED, $actual['event']);
165 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']); 171// $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
166 $this->assertEquals(1, $actual['id']); 172// $this->assertEquals(1, $actual['id']);
167 } 173// }
168 174//
169 /** 175// /**
170 * Re-read history from file after writing an event and make sure that the order is correct 176// * Re-read history from file after writing an event and make sure that the order is correct
171 */ 177// */
172 public function testHistoryOrderRead() 178// public function testHistoryOrderRead()
173 { 179// {
174 $history = new History(self::$historyFilePath); 180// $history = new History(self::$historyFilePath);
175 $history->updateLink(['id' => 1]); 181// $history->updateLink(['id' => 1]);
176 $history->addLink(['id' => 1]); 182// $history->addLink(['id' => 1]);
177 183//
178 $history = new History(self::$historyFilePath); 184// $history = new History(self::$historyFilePath);
179 $actual = $history->getHistory()[0]; 185// $actual = $history->getHistory()[0];
180 $this->assertEquals(History::CREATED, $actual['event']); 186// $this->assertEquals(History::CREATED, $actual['event']);
181 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']); 187// $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
182 $this->assertEquals(1, $actual['id']); 188// $this->assertEquals(1, $actual['id']);
183 189//
184 $actual = $history->getHistory()[1]; 190// $actual = $history->getHistory()[1];
185 $this->assertEquals(History::UPDATED, $actual['event']); 191// $this->assertEquals(History::UPDATED, $actual['event']);
186 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']); 192// $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
187 $this->assertEquals(1, $actual['id']); 193// $this->assertEquals(1, $actual['id']);
188 } 194// }
189 195//
190 /** 196// /**
191 * Test retention time: delete old entries. 197// * Test retention time: delete old entries.
192 */ 198// */
193 public function testHistoryRententionTime() 199// public function testHistoryRententionTime()
194 { 200// {
195 $history = new History(self::$historyFilePath, 5); 201// $history = new History(self::$historyFilePath, 5);
196 $history->updateLink(['id' => 1]); 202// $history->updateLink(['id' => 1]);
197 $this->assertEquals(1, count($history->getHistory())); 203// $this->assertEquals(1, count($history->getHistory()));
198 $arr = $history->getHistory(); 204// $arr = $history->getHistory();
199 $arr[0]['datetime'] = new DateTime('-1 hour'); 205// $arr[0]['datetime'] = new DateTime('-1 hour');
200 FileUtils::writeFlatDB(self::$historyFilePath, $arr); 206// FileUtils::writeFlatDB(self::$historyFilePath, $arr);
201 207//
202 $history = new History(self::$historyFilePath, 60); 208// $history = new History(self::$historyFilePath, 60);
203 $this->assertEquals(1, count($history->getHistory())); 209// $this->assertEquals(1, count($history->getHistory()));
204 $this->assertEquals(1, $history->getHistory()[0]['id']); 210// $this->assertEquals(1, $history->getHistory()[0]['id']);
205 $history->updateLink(['id' => 2]); 211// $history->updateLink(['id' => 2]);
206 $this->assertEquals(1, count($history->getHistory())); 212// $this->assertEquals(1, count($history->getHistory()));
207 $this->assertEquals(2, $history->getHistory()[0]['id']); 213// $this->assertEquals(2, $history->getHistory()[0]['id']);
208 } 214// }
209} 215}
diff --git a/tests/api/ApiMiddlewareTest.php b/tests/api/ApiMiddlewareTest.php
index 0b9b03f2..df2fb33a 100644
--- a/tests/api/ApiMiddlewareTest.php
+++ b/tests/api/ApiMiddlewareTest.php
@@ -2,6 +2,7 @@
2namespace Shaarli\Api; 2namespace Shaarli\Api;
3 3
4use Shaarli\Config\ConfigManager; 4use Shaarli\Config\ConfigManager;
5use Shaarli\History;
5use Slim\Container; 6use Slim\Container;
6use Slim\Http\Environment; 7use Slim\Http\Environment;
7use Slim\Http\Request; 8use Slim\Http\Request;
@@ -40,18 +41,21 @@ class ApiMiddlewareTest extends \PHPUnit\Framework\TestCase
40 protected $container; 41 protected $container;
41 42
42 /** 43 /**
43 * Before every test, instantiate a new Api with its config, plugins and links. 44 * Before every test, instantiate a new Api with its config, plugins and bookmarks.
44 */ 45 */
45 public function setUp() 46 public function setUp()
46 { 47 {
47 $this->conf = new ConfigManager('tests/utils/config/configJson.json.php'); 48 $this->conf = new ConfigManager('tests/utils/config/configJson');
48 $this->conf->set('api.secret', 'NapoleonWasALizard'); 49 $this->conf->set('api.secret', 'NapoleonWasALizard');
49 50
50 $this->refDB = new \ReferenceLinkDB(); 51 $this->refDB = new \ReferenceLinkDB();
51 $this->refDB->write(self::$testDatastore); 52 $this->refDB->write(self::$testDatastore);
52 53
54 $history = new History('sandbox/history.php');
55
53 $this->container = new Container(); 56 $this->container = new Container();
54 $this->container['conf'] = $this->conf; 57 $this->container['conf'] = $this->conf;
58 $this->container['history'] = $history;
55 } 59 }
56 60
57 /** 61 /**
diff --git a/tests/api/ApiUtilsTest.php b/tests/api/ApiUtilsTest.php
index 7499dd71..7efec9bb 100644
--- a/tests/api/ApiUtilsTest.php
+++ b/tests/api/ApiUtilsTest.php
@@ -2,6 +2,7 @@
2 2
3namespace Shaarli\Api; 3namespace Shaarli\Api;
4 4
5use Shaarli\Bookmark\Bookmark;
5use Shaarli\Http\Base64Url; 6use Shaarli\Http\Base64Url;
6 7
7/** 8/**
@@ -212,7 +213,7 @@ class ApiUtilsTest extends \PHPUnit\Framework\TestCase
212 public function testFormatLinkComplete() 213 public function testFormatLinkComplete()
213 { 214 {
214 $indexUrl = 'https://domain.tld/sub/'; 215 $indexUrl = 'https://domain.tld/sub/';
215 $link = [ 216 $data = [
216 'id' => 12, 217 'id' => 12,
217 'url' => 'http://lol.lol', 218 'url' => 'http://lol.lol',
218 'shorturl' => 'abc', 219 'shorturl' => 'abc',
@@ -223,6 +224,8 @@ class ApiUtilsTest extends \PHPUnit\Framework\TestCase
223 'created' => \DateTime::createFromFormat('Ymd_His', '20170107_160102'), 224 'created' => \DateTime::createFromFormat('Ymd_His', '20170107_160102'),
224 'updated' => \DateTime::createFromFormat('Ymd_His', '20170107_160612'), 225 'updated' => \DateTime::createFromFormat('Ymd_His', '20170107_160612'),
225 ]; 226 ];
227 $bookmark = new Bookmark();
228 $bookmark->fromArray($data);
226 229
227 $expected = [ 230 $expected = [
228 'id' => 12, 231 'id' => 12,
@@ -236,7 +239,7 @@ class ApiUtilsTest extends \PHPUnit\Framework\TestCase
236 'updated' => '2017-01-07T16:06:12+00:00', 239 'updated' => '2017-01-07T16:06:12+00:00',
237 ]; 240 ];
238 241
239 $this->assertEquals($expected, ApiUtils::formatLink($link, $indexUrl)); 242 $this->assertEquals($expected, ApiUtils::formatLink($bookmark, $indexUrl));
240 } 243 }
241 244
242 /** 245 /**
@@ -245,7 +248,7 @@ class ApiUtilsTest extends \PHPUnit\Framework\TestCase
245 public function testFormatLinkMinimalNote() 248 public function testFormatLinkMinimalNote()
246 { 249 {
247 $indexUrl = 'https://domain.tld/sub/'; 250 $indexUrl = 'https://domain.tld/sub/';
248 $link = [ 251 $data = [
249 'id' => 12, 252 'id' => 12,
250 'url' => '?abc', 253 'url' => '?abc',
251 'shorturl' => 'abc', 254 'shorturl' => 'abc',
@@ -255,6 +258,8 @@ class ApiUtilsTest extends \PHPUnit\Framework\TestCase
255 'private' => '', 258 'private' => '',
256 'created' => \DateTime::createFromFormat('Ymd_His', '20170107_160102'), 259 'created' => \DateTime::createFromFormat('Ymd_His', '20170107_160102'),
257 ]; 260 ];
261 $bookmark = new Bookmark();
262 $bookmark->fromArray($data);
258 263
259 $expected = [ 264 $expected = [
260 'id' => 12, 265 'id' => 12,
@@ -268,7 +273,7 @@ class ApiUtilsTest extends \PHPUnit\Framework\TestCase
268 'updated' => '', 273 'updated' => '',
269 ]; 274 ];
270 275
271 $this->assertEquals($expected, ApiUtils::formatLink($link, $indexUrl)); 276 $this->assertEquals($expected, ApiUtils::formatLink($bookmark, $indexUrl));
272 } 277 }
273 278
274 /** 279 /**
@@ -277,7 +282,7 @@ class ApiUtilsTest extends \PHPUnit\Framework\TestCase
277 public function testUpdateLink() 282 public function testUpdateLink()
278 { 283 {
279 $created = \DateTime::createFromFormat('Ymd_His', '20170107_160102'); 284 $created = \DateTime::createFromFormat('Ymd_His', '20170107_160102');
280 $old = [ 285 $data = [
281 'id' => 12, 286 'id' => 12,
282 'url' => '?abc', 287 'url' => '?abc',
283 'shorturl' => 'abc', 288 'shorturl' => 'abc',
@@ -287,8 +292,10 @@ class ApiUtilsTest extends \PHPUnit\Framework\TestCase
287 'private' => '', 292 'private' => '',
288 'created' => $created, 293 'created' => $created,
289 ]; 294 ];
295 $old = new Bookmark();
296 $old->fromArray($data);
290 297
291 $new = [ 298 $data = [
292 'id' => 13, 299 'id' => 13,
293 'shorturl' => 'nope', 300 'shorturl' => 'nope',
294 'url' => 'http://somewhere.else', 301 'url' => 'http://somewhere.else',
@@ -299,17 +306,18 @@ class ApiUtilsTest extends \PHPUnit\Framework\TestCase
299 'created' => 'creation', 306 'created' => 'creation',
300 'updated' => 'updation', 307 'updated' => 'updation',
301 ]; 308 ];
309 $new = new Bookmark();
310 $new->fromArray($data);
302 311
303 $result = ApiUtils::updateLink($old, $new); 312 $result = ApiUtils::updateLink($old, $new);
304 $this->assertEquals(12, $result['id']); 313 $this->assertEquals(12, $result->getId());
305 $this->assertEquals('http://somewhere.else', $result['url']); 314 $this->assertEquals('http://somewhere.else', $result->getUrl());
306 $this->assertEquals('abc', $result['shorturl']); 315 $this->assertEquals('abc', $result->getShortUrl());
307 $this->assertEquals('Le Cid', $result['title']); 316 $this->assertEquals('Le Cid', $result->getTitle());
308 $this->assertEquals('PercĆ© jusques au fond du cœur [...]', $result['description']); 317 $this->assertEquals('PercĆ© jusques au fond du cœur [...]', $result->getDescription());
309 $this->assertEquals('corneille rodrigue', $result['tags']); 318 $this->assertEquals('corneille rodrigue', $result->getTagsString());
310 $this->assertEquals(true, $result['private']); 319 $this->assertEquals(true, $result->isPrivate());
311 $this->assertEquals($created, $result['created']); 320 $this->assertEquals($created, $result->getCreated());
312 $this->assertTrue(new \DateTime('5 seconds ago') < $result['updated']);
313 } 321 }
314 322
315 /** 323 /**
@@ -318,7 +326,7 @@ class ApiUtilsTest extends \PHPUnit\Framework\TestCase
318 public function testUpdateLinkMinimal() 326 public function testUpdateLinkMinimal()
319 { 327 {
320 $created = \DateTime::createFromFormat('Ymd_His', '20170107_160102'); 328 $created = \DateTime::createFromFormat('Ymd_His', '20170107_160102');
321 $old = [ 329 $data = [
322 'id' => 12, 330 'id' => 12,
323 'url' => '?abc', 331 'url' => '?abc',
324 'shorturl' => 'abc', 332 'shorturl' => 'abc',
@@ -328,24 +336,19 @@ class ApiUtilsTest extends \PHPUnit\Framework\TestCase
328 'private' => true, 336 'private' => true,
329 'created' => $created, 337 'created' => $created,
330 ]; 338 ];
339 $old = new Bookmark();
340 $old->fromArray($data);
331 341
332 $new = [ 342 $new = new Bookmark();
333 'url' => '',
334 'title' => '',
335 'description' => '',
336 'tags' => '',
337 'private' => false,
338 ];
339 343
340 $result = ApiUtils::updateLink($old, $new); 344 $result = ApiUtils::updateLink($old, $new);
341 $this->assertEquals(12, $result['id']); 345 $this->assertEquals(12, $result->getId());
342 $this->assertEquals('?abc', $result['url']); 346 $this->assertEquals('', $result->getUrl());
343 $this->assertEquals('abc', $result['shorturl']); 347 $this->assertEquals('abc', $result->getShortUrl());
344 $this->assertEquals('?abc', $result['title']); 348 $this->assertEquals('', $result->getTitle());
345 $this->assertEquals('', $result['description']); 349 $this->assertEquals('', $result->getDescription());
346 $this->assertEquals('', $result['tags']); 350 $this->assertEquals('', $result->getTagsString());
347 $this->assertEquals(false, $result['private']); 351 $this->assertEquals(false, $result->isPrivate());
348 $this->assertEquals($created, $result['created']); 352 $this->assertEquals($created, $result->getCreated());
349 $this->assertTrue(new \DateTime('5 seconds ago') < $result['updated']);
350 } 353 }
351} 354}
diff --git a/tests/api/controllers/history/HistoryTest.php b/tests/api/controllers/history/HistoryTest.php
index e287f239..f4d3b646 100644
--- a/tests/api/controllers/history/HistoryTest.php
+++ b/tests/api/controllers/history/HistoryTest.php
@@ -39,11 +39,11 @@ class HistoryTest extends \PHPUnit\Framework\TestCase
39 protected $controller; 39 protected $controller;
40 40
41 /** 41 /**
42 * Before every test, instantiate a new Api with its config, plugins and links. 42 * Before every test, instantiate a new Api with its config, plugins and bookmarks.
43 */ 43 */
44 public function setUp() 44 public function setUp()
45 { 45 {
46 $this->conf = new ConfigManager('tests/utils/config/configJson.json.php'); 46 $this->conf = new ConfigManager('tests/utils/config/configJson');
47 $this->refHistory = new \ReferenceHistory(); 47 $this->refHistory = new \ReferenceHistory();
48 $this->refHistory->write(self::$testHistory); 48 $this->refHistory->write(self::$testHistory);
49 $this->container = new Container(); 49 $this->container = new Container();
diff --git a/tests/api/controllers/info/InfoTest.php b/tests/api/controllers/info/InfoTest.php
index e70d371b..b5c938e1 100644
--- a/tests/api/controllers/info/InfoTest.php
+++ b/tests/api/controllers/info/InfoTest.php
@@ -1,7 +1,10 @@
1<?php 1<?php
2namespace Shaarli\Api\Controllers; 2namespace Shaarli\Api\Controllers;
3 3
4use PHPUnit\Framework\TestCase;
5use Shaarli\Bookmark\BookmarkFileService;
4use Shaarli\Config\ConfigManager; 6use Shaarli\Config\ConfigManager;
7use Shaarli\History;
5use Slim\Container; 8use Slim\Container;
6use Slim\Http\Environment; 9use Slim\Http\Environment;
7use Slim\Http\Request; 10use Slim\Http\Request;
@@ -14,7 +17,7 @@ use Slim\Http\Response;
14 * 17 *
15 * @package Api\Controllers 18 * @package Api\Controllers
16 */ 19 */
17class InfoTest extends \PHPUnit\Framework\TestCase 20class InfoTest extends TestCase
18{ 21{
19 /** 22 /**
20 * @var string datastore to test write operations 23 * @var string datastore to test write operations
@@ -42,17 +45,20 @@ class InfoTest extends \PHPUnit\Framework\TestCase
42 protected $controller; 45 protected $controller;
43 46
44 /** 47 /**
45 * Before every test, instantiate a new Api with its config, plugins and links. 48 * Before every test, instantiate a new Api with its config, plugins and bookmarks.
46 */ 49 */
47 public function setUp() 50 public function setUp()
48 { 51 {
49 $this->conf = new ConfigManager('tests/utils/config/configJson.json.php'); 52 $this->conf = new ConfigManager('tests/utils/config/configJson');
53 $this->conf->set('resource.datastore', self::$testDatastore);
50 $this->refDB = new \ReferenceLinkDB(); 54 $this->refDB = new \ReferenceLinkDB();
51 $this->refDB->write(self::$testDatastore); 55 $this->refDB->write(self::$testDatastore);
52 56
57 $history = new History('sandbox/history.php');
58
53 $this->container = new Container(); 59 $this->container = new Container();
54 $this->container['conf'] = $this->conf; 60 $this->container['conf'] = $this->conf;
55 $this->container['db'] = new \Shaarli\Bookmark\LinkDB(self::$testDatastore, true, false); 61 $this->container['db'] = new BookmarkFileService($this->conf, $history, true);
56 $this->container['history'] = null; 62 $this->container['history'] = null;
57 63
58 $this->controller = new Info($this->container); 64 $this->controller = new Info($this->container);
@@ -84,11 +90,11 @@ class InfoTest extends \PHPUnit\Framework\TestCase
84 $this->assertEquals(2, $data['private_counter']); 90 $this->assertEquals(2, $data['private_counter']);
85 $this->assertEquals('Shaarli', $data['settings']['title']); 91 $this->assertEquals('Shaarli', $data['settings']['title']);
86 $this->assertEquals('?', $data['settings']['header_link']); 92 $this->assertEquals('?', $data['settings']['header_link']);
87 $this->assertEquals('UTC', $data['settings']['timezone']); 93 $this->assertEquals('Europe/Paris', $data['settings']['timezone']);
88 $this->assertEquals(ConfigManager::$DEFAULT_PLUGINS, $data['settings']['enabled_plugins']); 94 $this->assertEquals(ConfigManager::$DEFAULT_PLUGINS, $data['settings']['enabled_plugins']);
89 $this->assertEquals(false, $data['settings']['default_private_links']); 95 $this->assertEquals(true, $data['settings']['default_private_links']);
90 96
91 $title = 'My links'; 97 $title = 'My bookmarks';
92 $headerLink = 'http://shaarli.tld'; 98 $headerLink = 'http://shaarli.tld';
93 $timezone = 'Europe/Paris'; 99 $timezone = 'Europe/Paris';
94 $enabledPlugins = array('foo', 'bar'); 100 $enabledPlugins = array('foo', 'bar');
diff --git a/tests/api/controllers/links/DeleteLinkTest.php b/tests/api/controllers/links/DeleteLinkTest.php
index 90193e28..6c2b3698 100644
--- a/tests/api/controllers/links/DeleteLinkTest.php
+++ b/tests/api/controllers/links/DeleteLinkTest.php
@@ -3,7 +3,7 @@
3 3
4namespace Shaarli\Api\Controllers; 4namespace Shaarli\Api\Controllers;
5 5
6use Shaarli\Bookmark\LinkDB; 6use Shaarli\Bookmark\BookmarkFileService;
7use Shaarli\Config\ConfigManager; 7use Shaarli\Config\ConfigManager;
8use Shaarli\History; 8use Shaarli\History;
9use Slim\Container; 9use Slim\Container;
@@ -34,9 +34,9 @@ class DeleteLinkTest extends \PHPUnit\Framework\TestCase
34 protected $refDB = null; 34 protected $refDB = null;
35 35
36 /** 36 /**
37 * @var LinkDB instance. 37 * @var BookmarkFileService instance.
38 */ 38 */
39 protected $linkDB; 39 protected $bookmarkService;
40 40
41 /** 41 /**
42 * @var HistoryController instance. 42 * @var HistoryController instance.
@@ -54,20 +54,22 @@ class DeleteLinkTest extends \PHPUnit\Framework\TestCase
54 protected $controller; 54 protected $controller;
55 55
56 /** 56 /**
57 * Before each test, instantiate a new Api with its config, plugins and links. 57 * Before each test, instantiate a new Api with its config, plugins and bookmarks.
58 */ 58 */
59 public function setUp() 59 public function setUp()
60 { 60 {
61 $this->conf = new ConfigManager('tests/utils/config/configJson'); 61 $this->conf = new ConfigManager('tests/utils/config/configJson');
62 $this->conf->set('resource.datastore', self::$testDatastore);
62 $this->refDB = new \ReferenceLinkDB(); 63 $this->refDB = new \ReferenceLinkDB();
63 $this->refDB->write(self::$testDatastore); 64 $this->refDB->write(self::$testDatastore);
64 $this->linkDB = new LinkDB(self::$testDatastore, true, false);
65 $refHistory = new \ReferenceHistory(); 65 $refHistory = new \ReferenceHistory();
66 $refHistory->write(self::$testHistory); 66 $refHistory->write(self::$testHistory);
67 $this->history = new History(self::$testHistory); 67 $this->history = new History(self::$testHistory);
68 $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, true);
69
68 $this->container = new Container(); 70 $this->container = new Container();
69 $this->container['conf'] = $this->conf; 71 $this->container['conf'] = $this->conf;
70 $this->container['db'] = $this->linkDB; 72 $this->container['db'] = $this->bookmarkService;
71 $this->container['history'] = $this->history; 73 $this->container['history'] = $this->history;
72 74
73 $this->controller = new Links($this->container); 75 $this->controller = new Links($this->container);
@@ -88,7 +90,7 @@ class DeleteLinkTest extends \PHPUnit\Framework\TestCase
88 public function testDeleteLinkValid() 90 public function testDeleteLinkValid()
89 { 91 {
90 $id = '41'; 92 $id = '41';
91 $this->assertTrue(isset($this->linkDB[$id])); 93 $this->assertTrue($this->bookmarkService->exists($id));
92 $env = Environment::mock([ 94 $env = Environment::mock([
93 'REQUEST_METHOD' => 'DELETE', 95 'REQUEST_METHOD' => 'DELETE',
94 ]); 96 ]);
@@ -98,8 +100,8 @@ class DeleteLinkTest extends \PHPUnit\Framework\TestCase
98 $this->assertEquals(204, $response->getStatusCode()); 100 $this->assertEquals(204, $response->getStatusCode());
99 $this->assertEmpty((string) $response->getBody()); 101 $this->assertEmpty((string) $response->getBody());
100 102
101 $this->linkDB = new LinkDB(self::$testDatastore, true, false); 103 $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, true);
102 $this->assertFalse(isset($this->linkDB[$id])); 104 $this->assertFalse($this->bookmarkService->exists($id));
103 105
104 $historyEntry = $this->history->getHistory()[0]; 106 $historyEntry = $this->history->getHistory()[0];
105 $this->assertEquals(History::DELETED, $historyEntry['event']); 107 $this->assertEquals(History::DELETED, $historyEntry['event']);
@@ -117,7 +119,7 @@ class DeleteLinkTest extends \PHPUnit\Framework\TestCase
117 public function testDeleteLink404() 119 public function testDeleteLink404()
118 { 120 {
119 $id = -1; 121 $id = -1;
120 $this->assertFalse(isset($this->linkDB[$id])); 122 $this->assertFalse($this->bookmarkService->exists($id));
121 $env = Environment::mock([ 123 $env = Environment::mock([
122 'REQUEST_METHOD' => 'DELETE', 124 'REQUEST_METHOD' => 'DELETE',
123 ]); 125 ]);
diff --git a/tests/api/controllers/links/GetLinkIdTest.php b/tests/api/controllers/links/GetLinkIdTest.php
index cb9b7f6a..c26411ac 100644
--- a/tests/api/controllers/links/GetLinkIdTest.php
+++ b/tests/api/controllers/links/GetLinkIdTest.php
@@ -2,7 +2,10 @@
2 2
3namespace Shaarli\Api\Controllers; 3namespace Shaarli\Api\Controllers;
4 4
5use Shaarli\Bookmark\Bookmark;
6use Shaarli\Bookmark\BookmarkFileService;
5use Shaarli\Config\ConfigManager; 7use Shaarli\Config\ConfigManager;
8use Shaarli\History;
6use Slim\Container; 9use Slim\Container;
7use Slim\Http\Environment; 10use Slim\Http\Environment;
8use Slim\Http\Request; 11use Slim\Http\Request;
@@ -50,17 +53,19 @@ class GetLinkIdTest extends \PHPUnit\Framework\TestCase
50 const NB_FIELDS_LINK = 9; 53 const NB_FIELDS_LINK = 9;
51 54
52 /** 55 /**
53 * Before each test, instantiate a new Api with its config, plugins and links. 56 * Before each test, instantiate a new Api with its config, plugins and bookmarks.
54 */ 57 */
55 public function setUp() 58 public function setUp()
56 { 59 {
57 $this->conf = new ConfigManager('tests/utils/config/configJson'); 60 $this->conf = new ConfigManager('tests/utils/config/configJson');
61 $this->conf->set('resource.datastore', self::$testDatastore);
58 $this->refDB = new \ReferenceLinkDB(); 62 $this->refDB = new \ReferenceLinkDB();
59 $this->refDB->write(self::$testDatastore); 63 $this->refDB->write(self::$testDatastore);
64 $history = new History('sandbox/history.php');
60 65
61 $this->container = new Container(); 66 $this->container = new Container();
62 $this->container['conf'] = $this->conf; 67 $this->container['conf'] = $this->conf;
63 $this->container['db'] = new \Shaarli\Bookmark\LinkDB(self::$testDatastore, true, false); 68 $this->container['db'] = new BookmarkFileService($this->conf, $history, true);
64 $this->container['history'] = null; 69 $this->container['history'] = null;
65 70
66 $this->controller = new Links($this->container); 71 $this->controller = new Links($this->container);
@@ -107,7 +112,7 @@ class GetLinkIdTest extends \PHPUnit\Framework\TestCase
107 $this->assertEquals('sTuff', $data['tags'][0]); 112 $this->assertEquals('sTuff', $data['tags'][0]);
108 $this->assertEquals(false, $data['private']); 113 $this->assertEquals(false, $data['private']);
109 $this->assertEquals( 114 $this->assertEquals(
110 \DateTime::createFromFormat(\Shaarli\Bookmark\LinkDB::LINK_DATE_FORMAT, '20150310_114651')->format(\DateTime::ATOM), 115 \DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114651')->format(\DateTime::ATOM),
111 $data['created'] 116 $data['created']
112 ); 117 );
113 $this->assertEmpty($data['updated']); 118 $this->assertEmpty($data['updated']);
diff --git a/tests/api/controllers/links/GetLinksTest.php b/tests/api/controllers/links/GetLinksTest.php
index 711a3152..4e2d55ac 100644
--- a/tests/api/controllers/links/GetLinksTest.php
+++ b/tests/api/controllers/links/GetLinksTest.php
@@ -1,8 +1,11 @@
1<?php 1<?php
2namespace Shaarli\Api\Controllers; 2namespace Shaarli\Api\Controllers;
3 3
4use Shaarli\Bookmark\Bookmark;
5use Shaarli\Bookmark\BookmarkFileService;
4use Shaarli\Bookmark\LinkDB; 6use Shaarli\Bookmark\LinkDB;
5use Shaarli\Config\ConfigManager; 7use Shaarli\Config\ConfigManager;
8use Shaarli\History;
6use Slim\Container; 9use Slim\Container;
7use Slim\Http\Environment; 10use Slim\Http\Environment;
8use Slim\Http\Request; 11use Slim\Http\Request;
@@ -50,17 +53,19 @@ class GetLinksTest extends \PHPUnit\Framework\TestCase
50 const NB_FIELDS_LINK = 9; 53 const NB_FIELDS_LINK = 9;
51 54
52 /** 55 /**
53 * Before every test, instantiate a new Api with its config, plugins and links. 56 * Before every test, instantiate a new Api with its config, plugins and bookmarks.
54 */ 57 */
55 public function setUp() 58 public function setUp()
56 { 59 {
57 $this->conf = new ConfigManager('tests/utils/config/configJson'); 60 $this->conf = new ConfigManager('tests/utils/config/configJson');
61 $this->conf->set('resource.datastore', self::$testDatastore);
58 $this->refDB = new \ReferenceLinkDB(); 62 $this->refDB = new \ReferenceLinkDB();
59 $this->refDB->write(self::$testDatastore); 63 $this->refDB->write(self::$testDatastore);
64 $history = new History('sandbox/history.php');
60 65
61 $this->container = new Container(); 66 $this->container = new Container();
62 $this->container['conf'] = $this->conf; 67 $this->container['conf'] = $this->conf;
63 $this->container['db'] = new LinkDB(self::$testDatastore, true, false); 68 $this->container['db'] = new BookmarkFileService($this->conf, $history, true);
64 $this->container['history'] = null; 69 $this->container['history'] = null;
65 70
66 $this->controller = new Links($this->container); 71 $this->controller = new Links($this->container);
@@ -75,7 +80,7 @@ class GetLinksTest extends \PHPUnit\Framework\TestCase
75 } 80 }
76 81
77 /** 82 /**
78 * Test basic getLinks service: returns all links. 83 * Test basic getLinks service: returns all bookmarks.
79 */ 84 */
80 public function testGetLinks() 85 public function testGetLinks()
81 { 86 {
@@ -114,7 +119,7 @@ class GetLinksTest extends \PHPUnit\Framework\TestCase
114 $this->assertEquals('sTuff', $first['tags'][0]); 119 $this->assertEquals('sTuff', $first['tags'][0]);
115 $this->assertEquals(false, $first['private']); 120 $this->assertEquals(false, $first['private']);
116 $this->assertEquals( 121 $this->assertEquals(
117 \DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114651')->format(\DateTime::ATOM), 122 \DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114651')->format(\DateTime::ATOM),
118 $first['created'] 123 $first['created']
119 ); 124 );
120 $this->assertEmpty($first['updated']); 125 $this->assertEmpty($first['updated']);
@@ -125,7 +130,7 @@ class GetLinksTest extends \PHPUnit\Framework\TestCase
125 130
126 // Update date 131 // Update date
127 $this->assertEquals( 132 $this->assertEquals(
128 \DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160803_093033')->format(\DateTime::ATOM), 133 \DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20160803_093033')->format(\DateTime::ATOM),
129 $link['updated'] 134 $link['updated']
130 ); 135 );
131 } 136 }
diff --git a/tests/api/controllers/links/PostLinkTest.php b/tests/api/controllers/links/PostLinkTest.php
index d683a984..b2dd09eb 100644
--- a/tests/api/controllers/links/PostLinkTest.php
+++ b/tests/api/controllers/links/PostLinkTest.php
@@ -3,6 +3,8 @@
3namespace Shaarli\Api\Controllers; 3namespace Shaarli\Api\Controllers;
4 4
5use PHPUnit\Framework\TestCase; 5use PHPUnit\Framework\TestCase;
6use Shaarli\Bookmark\Bookmark;
7use Shaarli\Bookmark\BookmarkFileService;
6use Shaarli\Config\ConfigManager; 8use Shaarli\Config\ConfigManager;
7use Shaarli\History; 9use Shaarli\History;
8use Slim\Container; 10use Slim\Container;
@@ -41,6 +43,11 @@ class PostLinkTest extends TestCase
41 protected $refDB = null; 43 protected $refDB = null;
42 44
43 /** 45 /**
46 * @var BookmarkFileService instance.
47 */
48 protected $bookmarkService;
49
50 /**
44 * @var HistoryController instance. 51 * @var HistoryController instance.
45 */ 52 */
46 protected $history; 53 protected $history;
@@ -61,29 +68,30 @@ class PostLinkTest extends TestCase
61 const NB_FIELDS_LINK = 9; 68 const NB_FIELDS_LINK = 9;
62 69
63 /** 70 /**
64 * Before every test, instantiate a new Api with its config, plugins and links. 71 * Before every test, instantiate a new Api with its config, plugins and bookmarks.
65 */ 72 */
66 public function setUp() 73 public function setUp()
67 { 74 {
68 $this->conf = new ConfigManager('tests/utils/config/configJson.json.php'); 75 $this->conf = new ConfigManager('tests/utils/config/configJson');
76 $this->conf->set('resource.datastore', self::$testDatastore);
69 $this->refDB = new \ReferenceLinkDB(); 77 $this->refDB = new \ReferenceLinkDB();
70 $this->refDB->write(self::$testDatastore); 78 $this->refDB->write(self::$testDatastore);
71
72 $refHistory = new \ReferenceHistory(); 79 $refHistory = new \ReferenceHistory();
73 $refHistory->write(self::$testHistory); 80 $refHistory->write(self::$testHistory);
74 $this->history = new History(self::$testHistory); 81 $this->history = new History(self::$testHistory);
82 $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, true);
75 83
76 $this->container = new Container(); 84 $this->container = new Container();
77 $this->container['conf'] = $this->conf; 85 $this->container['conf'] = $this->conf;
78 $this->container['db'] = new \Shaarli\Bookmark\LinkDB(self::$testDatastore, true, false); 86 $this->container['db'] = $this->bookmarkService;
79 $this->container['history'] = new History(self::$testHistory); 87 $this->container['history'] = $this->history;
80 88
81 $this->controller = new Links($this->container); 89 $this->controller = new Links($this->container);
82 90
83 $mock = $this->createMock(Router::class); 91 $mock = $this->createMock(Router::class);
84 $mock->expects($this->any()) 92 $mock->expects($this->any())
85 ->method('relativePathFor') 93 ->method('relativePathFor')
86 ->willReturn('api/v1/links/1'); 94 ->willReturn('api/v1/bookmarks/1');
87 95
88 // affect @property-read... seems to work 96 // affect @property-read... seems to work
89 $this->controller->getCi()->router = $mock; 97 $this->controller->getCi()->router = $mock;
@@ -118,7 +126,7 @@ class PostLinkTest extends TestCase
118 126
119 $response = $this->controller->postLink($request, new Response()); 127 $response = $this->controller->postLink($request, new Response());
120 $this->assertEquals(201, $response->getStatusCode()); 128 $this->assertEquals(201, $response->getStatusCode());
121 $this->assertEquals('api/v1/links/1', $response->getHeader('Location')[0]); 129 $this->assertEquals('api/v1/bookmarks/1', $response->getHeader('Location')[0]);
122 $data = json_decode((string) $response->getBody(), true); 130 $data = json_decode((string) $response->getBody(), true);
123 $this->assertEquals(self::NB_FIELDS_LINK, count($data)); 131 $this->assertEquals(self::NB_FIELDS_LINK, count($data));
124 $this->assertEquals(43, $data['id']); 132 $this->assertEquals(43, $data['id']);
@@ -127,7 +135,7 @@ class PostLinkTest extends TestCase
127 $this->assertEquals('?' . $data['shorturl'], $data['title']); 135 $this->assertEquals('?' . $data['shorturl'], $data['title']);
128 $this->assertEquals('', $data['description']); 136 $this->assertEquals('', $data['description']);
129 $this->assertEquals([], $data['tags']); 137 $this->assertEquals([], $data['tags']);
130 $this->assertEquals(false, $data['private']); 138 $this->assertEquals(true, $data['private']);
131 $this->assertTrue( 139 $this->assertTrue(
132 new \DateTime('5 seconds ago') < \DateTime::createFromFormat(\DateTime::ATOM, $data['created']) 140 new \DateTime('5 seconds ago') < \DateTime::createFromFormat(\DateTime::ATOM, $data['created'])
133 ); 141 );
@@ -163,7 +171,7 @@ class PostLinkTest extends TestCase
163 $response = $this->controller->postLink($request, new Response()); 171 $response = $this->controller->postLink($request, new Response());
164 172
165 $this->assertEquals(201, $response->getStatusCode()); 173 $this->assertEquals(201, $response->getStatusCode());
166 $this->assertEquals('api/v1/links/1', $response->getHeader('Location')[0]); 174 $this->assertEquals('api/v1/bookmarks/1', $response->getHeader('Location')[0]);
167 $data = json_decode((string) $response->getBody(), true); 175 $data = json_decode((string) $response->getBody(), true);
168 $this->assertEquals(self::NB_FIELDS_LINK, count($data)); 176 $this->assertEquals(self::NB_FIELDS_LINK, count($data));
169 $this->assertEquals(43, $data['id']); 177 $this->assertEquals(43, $data['id']);
@@ -211,11 +219,11 @@ class PostLinkTest extends TestCase
211 $this->assertEquals(['gnu', 'media', 'web', '.hidden', 'hashtag'], $data['tags']); 219 $this->assertEquals(['gnu', 'media', 'web', '.hidden', 'hashtag'], $data['tags']);
212 $this->assertEquals(false, $data['private']); 220 $this->assertEquals(false, $data['private']);
213 $this->assertEquals( 221 $this->assertEquals(
214 \DateTime::createFromFormat(\Shaarli\Bookmark\LinkDB::LINK_DATE_FORMAT, '20130614_184135'), 222 \DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20130614_184135'),
215 \DateTime::createFromFormat(\DateTime::ATOM, $data['created']) 223 \DateTime::createFromFormat(\DateTime::ATOM, $data['created'])
216 ); 224 );
217 $this->assertEquals( 225 $this->assertEquals(
218 \DateTime::createFromFormat(\Shaarli\Bookmark\LinkDB::LINK_DATE_FORMAT, '20130615_184230'), 226 \DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20130615_184230'),
219 \DateTime::createFromFormat(\DateTime::ATOM, $data['updated']) 227 \DateTime::createFromFormat(\DateTime::ATOM, $data['updated'])
220 ); 228 );
221 } 229 }
diff --git a/tests/api/controllers/links/PutLinkTest.php b/tests/api/controllers/links/PutLinkTest.php
index cd815b66..cb63742e 100644
--- a/tests/api/controllers/links/PutLinkTest.php
+++ b/tests/api/controllers/links/PutLinkTest.php
@@ -3,6 +3,8 @@
3 3
4namespace Shaarli\Api\Controllers; 4namespace Shaarli\Api\Controllers;
5 5
6use Shaarli\Bookmark\Bookmark;
7use Shaarli\Bookmark\BookmarkFileService;
6use Shaarli\Config\ConfigManager; 8use Shaarli\Config\ConfigManager;
7use Shaarli\History; 9use Shaarli\History;
8use Slim\Container; 10use Slim\Container;
@@ -33,6 +35,11 @@ class PutLinkTest extends \PHPUnit\Framework\TestCase
33 protected $refDB = null; 35 protected $refDB = null;
34 36
35 /** 37 /**
38 * @var BookmarkFileService instance.
39 */
40 protected $bookmarkService;
41
42 /**
36 * @var HistoryController instance. 43 * @var HistoryController instance.
37 */ 44 */
38 protected $history; 45 protected $history;
@@ -53,22 +60,23 @@ class PutLinkTest extends \PHPUnit\Framework\TestCase
53 const NB_FIELDS_LINK = 9; 60 const NB_FIELDS_LINK = 9;
54 61
55 /** 62 /**
56 * Before every test, instantiate a new Api with its config, plugins and links. 63 * Before every test, instantiate a new Api with its config, plugins and bookmarks.
57 */ 64 */
58 public function setUp() 65 public function setUp()
59 { 66 {
60 $this->conf = new ConfigManager('tests/utils/config/configJson.json.php'); 67 $this->conf = new ConfigManager('tests/utils/config/configJson');
68 $this->conf->set('resource.datastore', self::$testDatastore);
61 $this->refDB = new \ReferenceLinkDB(); 69 $this->refDB = new \ReferenceLinkDB();
62 $this->refDB->write(self::$testDatastore); 70 $this->refDB->write(self::$testDatastore);
63
64 $refHistory = new \ReferenceHistory(); 71 $refHistory = new \ReferenceHistory();
65 $refHistory->write(self::$testHistory); 72 $refHistory->write(self::$testHistory);
66 $this->history = new History(self::$testHistory); 73 $this->history = new History(self::$testHistory);
74 $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, true);
67 75
68 $this->container = new Container(); 76 $this->container = new Container();
69 $this->container['conf'] = $this->conf; 77 $this->container['conf'] = $this->conf;
70 $this->container['db'] = new \Shaarli\Bookmark\LinkDB(self::$testDatastore, true, false); 78 $this->container['db'] = $this->bookmarkService;
71 $this->container['history'] = new History(self::$testHistory); 79 $this->container['history'] = $this->history;
72 80
73 $this->controller = new Links($this->container); 81 $this->controller = new Links($this->container);
74 82
@@ -110,7 +118,7 @@ class PutLinkTest extends \PHPUnit\Framework\TestCase
110 $this->assertEquals('?WDWyig', $data['title']); 118 $this->assertEquals('?WDWyig', $data['title']);
111 $this->assertEquals('', $data['description']); 119 $this->assertEquals('', $data['description']);
112 $this->assertEquals([], $data['tags']); 120 $this->assertEquals([], $data['tags']);
113 $this->assertEquals(false, $data['private']); 121 $this->assertEquals(true, $data['private']);
114 $this->assertEquals( 122 $this->assertEquals(
115 \DateTime::createFromFormat('Ymd_His', '20150310_114651'), 123 \DateTime::createFromFormat('Ymd_His', '20150310_114651'),
116 \DateTime::createFromFormat(\DateTime::ATOM, $data['created']) 124 \DateTime::createFromFormat(\DateTime::ATOM, $data['created'])
@@ -199,11 +207,11 @@ class PutLinkTest extends \PHPUnit\Framework\TestCase
199 $this->assertEquals(['gnu', 'media', 'web', '.hidden', 'hashtag'], $data['tags']); 207 $this->assertEquals(['gnu', 'media', 'web', '.hidden', 'hashtag'], $data['tags']);
200 $this->assertEquals(false, $data['private']); 208 $this->assertEquals(false, $data['private']);
201 $this->assertEquals( 209 $this->assertEquals(
202 \DateTime::createFromFormat(\Shaarli\Bookmark\LinkDB::LINK_DATE_FORMAT, '20130614_184135'), 210 \DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20130614_184135'),
203 \DateTime::createFromFormat(\DateTime::ATOM, $data['created']) 211 \DateTime::createFromFormat(\DateTime::ATOM, $data['created'])
204 ); 212 );
205 $this->assertEquals( 213 $this->assertEquals(
206 \DateTime::createFromFormat(\Shaarli\Bookmark\LinkDB::LINK_DATE_FORMAT, '20130615_184230'), 214 \DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20130615_184230'),
207 \DateTime::createFromFormat(\DateTime::ATOM, $data['updated']) 215 \DateTime::createFromFormat(\DateTime::ATOM, $data['updated'])
208 ); 216 );
209 } 217 }
diff --git a/tests/api/controllers/tags/DeleteTagTest.php b/tests/api/controllers/tags/DeleteTagTest.php
index 84e1d56e..c6748872 100644
--- a/tests/api/controllers/tags/DeleteTagTest.php
+++ b/tests/api/controllers/tags/DeleteTagTest.php
@@ -3,6 +3,7 @@
3 3
4namespace Shaarli\Api\Controllers; 4namespace Shaarli\Api\Controllers;
5 5
6use Shaarli\Bookmark\BookmarkFileService;
6use Shaarli\Bookmark\LinkDB; 7use Shaarli\Bookmark\LinkDB;
7use Shaarli\Config\ConfigManager; 8use Shaarli\Config\ConfigManager;
8use Shaarli\History; 9use Shaarli\History;
@@ -34,9 +35,9 @@ class DeleteTagTest extends \PHPUnit\Framework\TestCase
34 protected $refDB = null; 35 protected $refDB = null;
35 36
36 /** 37 /**
37 * @var LinkDB instance. 38 * @var BookmarkFileService instance.
38 */ 39 */
39 protected $linkDB; 40 protected $bookmarkService;
40 41
41 /** 42 /**
42 * @var HistoryController instance. 43 * @var HistoryController instance.
@@ -54,20 +55,22 @@ class DeleteTagTest extends \PHPUnit\Framework\TestCase
54 protected $controller; 55 protected $controller;
55 56
56 /** 57 /**
57 * Before each test, instantiate a new Api with its config, plugins and links. 58 * Before each test, instantiate a new Api with its config, plugins and bookmarks.
58 */ 59 */
59 public function setUp() 60 public function setUp()
60 { 61 {
61 $this->conf = new ConfigManager('tests/utils/config/configJson'); 62 $this->conf = new ConfigManager('tests/utils/config/configJson');
63 $this->conf->set('resource.datastore', self::$testDatastore);
62 $this->refDB = new \ReferenceLinkDB(); 64 $this->refDB = new \ReferenceLinkDB();
63 $this->refDB->write(self::$testDatastore); 65 $this->refDB->write(self::$testDatastore);
64 $this->linkDB = new LinkDB(self::$testDatastore, true, false);
65 $refHistory = new \ReferenceHistory(); 66 $refHistory = new \ReferenceHistory();
66 $refHistory->write(self::$testHistory); 67 $refHistory->write(self::$testHistory);
67 $this->history = new History(self::$testHistory); 68 $this->history = new History(self::$testHistory);
69 $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, true);
70
68 $this->container = new Container(); 71 $this->container = new Container();
69 $this->container['conf'] = $this->conf; 72 $this->container['conf'] = $this->conf;
70 $this->container['db'] = $this->linkDB; 73 $this->container['db'] = $this->bookmarkService;
71 $this->container['history'] = $this->history; 74 $this->container['history'] = $this->history;
72 75
73 $this->controller = new Tags($this->container); 76 $this->controller = new Tags($this->container);
@@ -88,7 +91,7 @@ class DeleteTagTest extends \PHPUnit\Framework\TestCase
88 public function testDeleteTagValid() 91 public function testDeleteTagValid()
89 { 92 {
90 $tagName = 'gnu'; 93 $tagName = 'gnu';
91 $tags = $this->linkDB->linksCountPerTag(); 94 $tags = $this->bookmarkService->bookmarksCountPerTag();
92 $this->assertTrue($tags[$tagName] > 0); 95 $this->assertTrue($tags[$tagName] > 0);
93 $env = Environment::mock([ 96 $env = Environment::mock([
94 'REQUEST_METHOD' => 'DELETE', 97 'REQUEST_METHOD' => 'DELETE',
@@ -99,11 +102,11 @@ class DeleteTagTest extends \PHPUnit\Framework\TestCase
99 $this->assertEquals(204, $response->getStatusCode()); 102 $this->assertEquals(204, $response->getStatusCode());
100 $this->assertEmpty((string) $response->getBody()); 103 $this->assertEmpty((string) $response->getBody());
101 104
102 $this->linkDB = new LinkDB(self::$testDatastore, true, false); 105 $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, true);
103 $tags = $this->linkDB->linksCountPerTag(); 106 $tags = $this->bookmarkService->bookmarksCountPerTag();
104 $this->assertFalse(isset($tags[$tagName])); 107 $this->assertFalse(isset($tags[$tagName]));
105 108
106 // 2 links affected 109 // 2 bookmarks affected
107 $historyEntry = $this->history->getHistory()[0]; 110 $historyEntry = $this->history->getHistory()[0];
108 $this->assertEquals(History::UPDATED, $historyEntry['event']); 111 $this->assertEquals(History::UPDATED, $historyEntry['event']);
109 $this->assertTrue( 112 $this->assertTrue(
@@ -122,7 +125,7 @@ class DeleteTagTest extends \PHPUnit\Framework\TestCase
122 public function testDeleteTagCaseSensitivity() 125 public function testDeleteTagCaseSensitivity()
123 { 126 {
124 $tagName = 'sTuff'; 127 $tagName = 'sTuff';
125 $tags = $this->linkDB->linksCountPerTag(); 128 $tags = $this->bookmarkService->bookmarksCountPerTag();
126 $this->assertTrue($tags[$tagName] > 0); 129 $this->assertTrue($tags[$tagName] > 0);
127 $env = Environment::mock([ 130 $env = Environment::mock([
128 'REQUEST_METHOD' => 'DELETE', 131 'REQUEST_METHOD' => 'DELETE',
@@ -133,8 +136,8 @@ class DeleteTagTest extends \PHPUnit\Framework\TestCase
133 $this->assertEquals(204, $response->getStatusCode()); 136 $this->assertEquals(204, $response->getStatusCode());
134 $this->assertEmpty((string) $response->getBody()); 137 $this->assertEmpty((string) $response->getBody());
135 138
136 $this->linkDB = new LinkDB(self::$testDatastore, true, false); 139 $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, true);
137 $tags = $this->linkDB->linksCountPerTag(); 140 $tags = $this->bookmarkService->bookmarksCountPerTag();
138 $this->assertFalse(isset($tags[$tagName])); 141 $this->assertFalse(isset($tags[$tagName]));
139 $this->assertTrue($tags[strtolower($tagName)] > 0); 142 $this->assertTrue($tags[strtolower($tagName)] > 0);
140 143
@@ -154,7 +157,7 @@ class DeleteTagTest extends \PHPUnit\Framework\TestCase
154 public function testDeleteLink404() 157 public function testDeleteLink404()
155 { 158 {
156 $tagName = 'nopenope'; 159 $tagName = 'nopenope';
157 $tags = $this->linkDB->linksCountPerTag(); 160 $tags = $this->bookmarkService->bookmarksCountPerTag();
158 $this->assertFalse(isset($tags[$tagName])); 161 $this->assertFalse(isset($tags[$tagName]));
159 $env = Environment::mock([ 162 $env = Environment::mock([
160 'REQUEST_METHOD' => 'DELETE', 163 'REQUEST_METHOD' => 'DELETE',
diff --git a/tests/api/controllers/tags/GetTagNameTest.php b/tests/api/controllers/tags/GetTagNameTest.php
index a2525c17..b9a81f9b 100644
--- a/tests/api/controllers/tags/GetTagNameTest.php
+++ b/tests/api/controllers/tags/GetTagNameTest.php
@@ -2,8 +2,10 @@
2 2
3namespace Shaarli\Api\Controllers; 3namespace Shaarli\Api\Controllers;
4 4
5use Shaarli\Bookmark\BookmarkFileService;
5use Shaarli\Bookmark\LinkDB; 6use Shaarli\Bookmark\LinkDB;
6use Shaarli\Config\ConfigManager; 7use Shaarli\Config\ConfigManager;
8use Shaarli\History;
7use Slim\Container; 9use Slim\Container;
8use Slim\Http\Environment; 10use Slim\Http\Environment;
9use Slim\Http\Request; 11use Slim\Http\Request;
@@ -49,17 +51,19 @@ class GetTagNameTest extends \PHPUnit\Framework\TestCase
49 const NB_FIELDS_TAG = 2; 51 const NB_FIELDS_TAG = 2;
50 52
51 /** 53 /**
52 * Before each test, instantiate a new Api with its config, plugins and links. 54 * Before each test, instantiate a new Api with its config, plugins and bookmarks.
53 */ 55 */
54 public function setUp() 56 public function setUp()
55 { 57 {
56 $this->conf = new ConfigManager('tests/utils/config/configJson'); 58 $this->conf = new ConfigManager('tests/utils/config/configJson');
59 $this->conf->set('resource.datastore', self::$testDatastore);
57 $this->refDB = new \ReferenceLinkDB(); 60 $this->refDB = new \ReferenceLinkDB();
58 $this->refDB->write(self::$testDatastore); 61 $this->refDB->write(self::$testDatastore);
62 $history = new History('sandbox/history.php');
59 63
60 $this->container = new Container(); 64 $this->container = new Container();
61 $this->container['conf'] = $this->conf; 65 $this->container['conf'] = $this->conf;
62 $this->container['db'] = new LinkDB(self::$testDatastore, true, false); 66 $this->container['db'] = new BookmarkFileService($this->conf, $history, true);
63 $this->container['history'] = null; 67 $this->container['history'] = null;
64 68
65 $this->controller = new Tags($this->container); 69 $this->controller = new Tags($this->container);
diff --git a/tests/api/controllers/tags/GetTagsTest.php b/tests/api/controllers/tags/GetTagsTest.php
index 98628c98..53a3326d 100644
--- a/tests/api/controllers/tags/GetTagsTest.php
+++ b/tests/api/controllers/tags/GetTagsTest.php
@@ -1,8 +1,10 @@
1<?php 1<?php
2namespace Shaarli\Api\Controllers; 2namespace Shaarli\Api\Controllers;
3 3
4use Shaarli\Bookmark\BookmarkFileService;
4use Shaarli\Bookmark\LinkDB; 5use Shaarli\Bookmark\LinkDB;
5use Shaarli\Config\ConfigManager; 6use Shaarli\Config\ConfigManager;
7use Shaarli\History;
6use Slim\Container; 8use Slim\Container;
7use Slim\Http\Environment; 9use Slim\Http\Environment;
8use Slim\Http\Request; 10use Slim\Http\Request;
@@ -38,9 +40,9 @@ class GetTagsTest extends \PHPUnit\Framework\TestCase
38 protected $container; 40 protected $container;
39 41
40 /** 42 /**
41 * @var LinkDB instance. 43 * @var BookmarkFileService instance.
42 */ 44 */
43 protected $linkDB; 45 protected $bookmarkService;
44 46
45 /** 47 /**
46 * @var Tags controller instance. 48 * @var Tags controller instance.
@@ -53,18 +55,21 @@ class GetTagsTest extends \PHPUnit\Framework\TestCase
53 const NB_FIELDS_TAG = 2; 55 const NB_FIELDS_TAG = 2;
54 56
55 /** 57 /**
56 * Before every test, instantiate a new Api with its config, plugins and links. 58 * Before every test, instantiate a new Api with its config, plugins and bookmarks.
57 */ 59 */
58 public function setUp() 60 public function setUp()
59 { 61 {
60 $this->conf = new ConfigManager('tests/utils/config/configJson'); 62 $this->conf = new ConfigManager('tests/utils/config/configJson');
63 $this->conf->set('resource.datastore', self::$testDatastore);
61 $this->refDB = new \ReferenceLinkDB(); 64 $this->refDB = new \ReferenceLinkDB();
62 $this->refDB->write(self::$testDatastore); 65 $this->refDB->write(self::$testDatastore);
66 $history = new History('sandbox/history.php');
67
68 $this->bookmarkService = new BookmarkFileService($this->conf, $history, true);
63 69
64 $this->container = new Container(); 70 $this->container = new Container();
65 $this->container['conf'] = $this->conf; 71 $this->container['conf'] = $this->conf;
66 $this->linkDB = new LinkDB(self::$testDatastore, true, false); 72 $this->container['db'] = $this->bookmarkService;
67 $this->container['db'] = $this->linkDB;
68 $this->container['history'] = null; 73 $this->container['history'] = null;
69 74
70 $this->controller = new Tags($this->container); 75 $this->controller = new Tags($this->container);
@@ -83,7 +88,7 @@ class GetTagsTest extends \PHPUnit\Framework\TestCase
83 */ 88 */
84 public function testGetTagsAll() 89 public function testGetTagsAll()
85 { 90 {
86 $tags = $this->linkDB->linksCountPerTag(); 91 $tags = $this->bookmarkService->bookmarksCountPerTag();
87 $env = Environment::mock([ 92 $env = Environment::mock([
88 'REQUEST_METHOD' => 'GET', 93 'REQUEST_METHOD' => 'GET',
89 ]); 94 ]);
@@ -136,7 +141,7 @@ class GetTagsTest extends \PHPUnit\Framework\TestCase
136 */ 141 */
137 public function testGetTagsLimitAll() 142 public function testGetTagsLimitAll()
138 { 143 {
139 $tags = $this->linkDB->linksCountPerTag(); 144 $tags = $this->bookmarkService->bookmarksCountPerTag();
140 $env = Environment::mock([ 145 $env = Environment::mock([
141 'REQUEST_METHOD' => 'GET', 146 'REQUEST_METHOD' => 'GET',
142 'QUERY_STRING' => 'limit=all' 147 'QUERY_STRING' => 'limit=all'
@@ -170,7 +175,7 @@ class GetTagsTest extends \PHPUnit\Framework\TestCase
170 */ 175 */
171 public function testGetTagsVisibilityPrivate() 176 public function testGetTagsVisibilityPrivate()
172 { 177 {
173 $tags = $this->linkDB->linksCountPerTag([], 'private'); 178 $tags = $this->bookmarkService->bookmarksCountPerTag([], 'private');
174 $env = Environment::mock([ 179 $env = Environment::mock([
175 'REQUEST_METHOD' => 'GET', 180 'REQUEST_METHOD' => 'GET',
176 'QUERY_STRING' => 'visibility=private' 181 'QUERY_STRING' => 'visibility=private'
@@ -190,7 +195,7 @@ class GetTagsTest extends \PHPUnit\Framework\TestCase
190 */ 195 */
191 public function testGetTagsVisibilityPublic() 196 public function testGetTagsVisibilityPublic()
192 { 197 {
193 $tags = $this->linkDB->linksCountPerTag([], 'public'); 198 $tags = $this->bookmarkService->bookmarksCountPerTag([], 'public');
194 $env = Environment::mock( 199 $env = Environment::mock(
195 [ 200 [
196 'REQUEST_METHOD' => 'GET', 201 'REQUEST_METHOD' => 'GET',
diff --git a/tests/api/controllers/tags/PutTagTest.php b/tests/api/controllers/tags/PutTagTest.php
index 86106fc7..2a3cc15a 100644
--- a/tests/api/controllers/tags/PutTagTest.php
+++ b/tests/api/controllers/tags/PutTagTest.php
@@ -3,6 +3,7 @@
3namespace Shaarli\Api\Controllers; 3namespace Shaarli\Api\Controllers;
4 4
5use Shaarli\Api\Exceptions\ApiBadParametersException; 5use Shaarli\Api\Exceptions\ApiBadParametersException;
6use Shaarli\Bookmark\BookmarkFileService;
6use Shaarli\Bookmark\LinkDB; 7use Shaarli\Bookmark\LinkDB;
7use Shaarli\Config\ConfigManager; 8use Shaarli\Config\ConfigManager;
8use Shaarli\History; 9use Shaarli\History;
@@ -44,9 +45,9 @@ class PutTagTest extends \PHPUnit\Framework\TestCase
44 protected $container; 45 protected $container;
45 46
46 /** 47 /**
47 * @var LinkDB instance. 48 * @var BookmarkFileService instance.
48 */ 49 */
49 protected $linkDB; 50 protected $bookmarkService;
50 51
51 /** 52 /**
52 * @var Tags controller instance. 53 * @var Tags controller instance.
@@ -59,22 +60,22 @@ class PutTagTest extends \PHPUnit\Framework\TestCase
59 const NB_FIELDS_TAG = 2; 60 const NB_FIELDS_TAG = 2;
60 61
61 /** 62 /**
62 * Before every test, instantiate a new Api with its config, plugins and links. 63 * Before every test, instantiate a new Api with its config, plugins and bookmarks.
63 */ 64 */
64 public function setUp() 65 public function setUp()
65 { 66 {
66 $this->conf = new ConfigManager('tests/utils/config/configJson.json.php'); 67 $this->conf = new ConfigManager('tests/utils/config/configJson');
68 $this->conf->set('resource.datastore', self::$testDatastore);
67 $this->refDB = new \ReferenceLinkDB(); 69 $this->refDB = new \ReferenceLinkDB();
68 $this->refDB->write(self::$testDatastore); 70 $this->refDB->write(self::$testDatastore);
69
70 $refHistory = new \ReferenceHistory(); 71 $refHistory = new \ReferenceHistory();
71 $refHistory->write(self::$testHistory); 72 $refHistory->write(self::$testHistory);
72 $this->history = new History(self::$testHistory); 73 $this->history = new History(self::$testHistory);
74 $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, true);
73 75
74 $this->container = new Container(); 76 $this->container = new Container();
75 $this->container['conf'] = $this->conf; 77 $this->container['conf'] = $this->conf;
76 $this->linkDB = new LinkDB(self::$testDatastore, true, false); 78 $this->container['db'] = $this->bookmarkService;
77 $this->container['db'] = $this->linkDB;
78 $this->container['history'] = $this->history; 79 $this->container['history'] = $this->history;
79 80
80 $this->controller = new Tags($this->container); 81 $this->controller = new Tags($this->container);
@@ -109,7 +110,7 @@ class PutTagTest extends \PHPUnit\Framework\TestCase
109 $this->assertEquals($newName, $data['name']); 110 $this->assertEquals($newName, $data['name']);
110 $this->assertEquals(2, $data['occurrences']); 111 $this->assertEquals(2, $data['occurrences']);
111 112
112 $tags = $this->linkDB->linksCountPerTag(); 113 $tags = $this->bookmarkService->bookmarksCountPerTag();
113 $this->assertNotTrue(isset($tags[$tagName])); 114 $this->assertNotTrue(isset($tags[$tagName]));
114 $this->assertEquals(2, $tags[$newName]); 115 $this->assertEquals(2, $tags[$newName]);
115 116
@@ -133,7 +134,7 @@ class PutTagTest extends \PHPUnit\Framework\TestCase
133 $tagName = 'gnu'; 134 $tagName = 'gnu';
134 $newName = 'w3c'; 135 $newName = 'w3c';
135 136
136 $tags = $this->linkDB->linksCountPerTag(); 137 $tags = $this->bookmarkService->bookmarksCountPerTag();
137 $this->assertEquals(1, $tags[$newName]); 138 $this->assertEquals(1, $tags[$newName]);
138 $this->assertEquals(2, $tags[$tagName]); 139 $this->assertEquals(2, $tags[$tagName]);
139 140
@@ -151,7 +152,7 @@ class PutTagTest extends \PHPUnit\Framework\TestCase
151 $this->assertEquals($newName, $data['name']); 152 $this->assertEquals($newName, $data['name']);
152 $this->assertEquals(3, $data['occurrences']); 153 $this->assertEquals(3, $data['occurrences']);
153 154
154 $tags = $this->linkDB->linksCountPerTag(); 155 $tags = $this->bookmarkService->bookmarksCountPerTag();
155 $this->assertNotTrue(isset($tags[$tagName])); 156 $this->assertNotTrue(isset($tags[$tagName]));
156 $this->assertEquals(3, $tags[$newName]); 157 $this->assertEquals(3, $tags[$newName]);
157 } 158 }
@@ -167,7 +168,7 @@ class PutTagTest extends \PHPUnit\Framework\TestCase
167 $tagName = 'gnu'; 168 $tagName = 'gnu';
168 $newName = ''; 169 $newName = '';
169 170
170 $tags = $this->linkDB->linksCountPerTag(); 171 $tags = $this->bookmarkService->bookmarksCountPerTag();
171 $this->assertEquals(2, $tags[$tagName]); 172 $this->assertEquals(2, $tags[$tagName]);
172 173
173 $env = Environment::mock([ 174 $env = Environment::mock([
@@ -185,7 +186,7 @@ class PutTagTest extends \PHPUnit\Framework\TestCase
185 try { 186 try {
186 $this->controller->putTag($request, new Response(), ['tagName' => $tagName]); 187 $this->controller->putTag($request, new Response(), ['tagName' => $tagName]);
187 } catch (ApiBadParametersException $e) { 188 } catch (ApiBadParametersException $e) {
188 $tags = $this->linkDB->linksCountPerTag(); 189 $tags = $this->bookmarkService->bookmarksCountPerTag();
189 $this->assertEquals(2, $tags[$tagName]); 190 $this->assertEquals(2, $tags[$tagName]);
190 throw $e; 191 throw $e;
191 } 192 }
diff --git a/tests/bookmark/BookmarkArrayTest.php b/tests/bookmark/BookmarkArrayTest.php
new file mode 100644
index 00000000..0f8f04c5
--- /dev/null
+++ b/tests/bookmark/BookmarkArrayTest.php
@@ -0,0 +1,239 @@
1<?php
2
3namespace Shaarli\Bookmark;
4
5use PHPUnit\Framework\TestCase;
6use Shaarli\Bookmark\Exception\InvalidBookmarkException;
7use Shaarli\Config\ConfigManager;
8use Shaarli\History;
9
10/**
11 * Class BookmarkArrayTest
12 */
13class BookmarkArrayTest extends TestCase
14{
15 /**
16 * Test the constructor and make sure that the instance is properly initialized
17 */
18 public function testArrayConstructorEmpty()
19 {
20 $array = new BookmarkArray();
21 $this->assertTrue(is_iterable($array));
22 $this->assertEmpty($array);
23 }
24
25 /**
26 * Test adding entries to the array, specifying the key offset or not.
27 */
28 public function testArrayAccessAddEntries()
29 {
30 $array = new BookmarkArray();
31 $bookmark = new Bookmark();
32 $bookmark->setId(11)->validate();
33 $array[] = $bookmark;
34 $this->assertCount(1, $array);
35 $this->assertTrue(isset($array[11]));
36 $this->assertNull($array[0]);
37 $this->assertEquals($bookmark, $array[11]);
38
39 $bookmark = new Bookmark();
40 $bookmark->setId(14)->validate();
41 $array[14] = $bookmark;
42 $this->assertCount(2, $array);
43 $this->assertTrue(isset($array[14]));
44 $this->assertNull($array[0]);
45 $this->assertEquals($bookmark, $array[14]);
46 }
47
48 /**
49 * Test adding a bad entry: wrong type
50 *
51 * @expectedException Shaarli\Bookmark\Exception\InvalidBookmarkException
52 */
53 public function testArrayAccessAddBadEntryInstance()
54 {
55 $array = new BookmarkArray();
56 $array[] = 'nope';
57 }
58
59 /**
60 * Test adding a bad entry: no id
61 *
62 * @expectedException Shaarli\Bookmark\Exception\InvalidBookmarkException
63 */
64 public function testArrayAccessAddBadEntryNoId()
65 {
66 $array = new BookmarkArray();
67 $bookmark = new Bookmark();
68 $array[] = $bookmark;
69 }
70
71 /**
72 * Test adding a bad entry: no url
73 *
74 * @expectedException Shaarli\Bookmark\Exception\InvalidBookmarkException
75 */
76 public function testArrayAccessAddBadEntryNoUrl()
77 {
78 $array = new BookmarkArray();
79 $bookmark = (new Bookmark())->setId(11);
80 $array[] = $bookmark;
81 }
82
83 /**
84 * Test adding a bad entry: invalid offset
85 *
86 * @expectedException Shaarli\Bookmark\Exception\InvalidBookmarkException
87 */
88 public function testArrayAccessAddBadEntryOffset()
89 {
90 $array = new BookmarkArray();
91 $bookmark = (new Bookmark())->setId(11);
92 $bookmark->validate();
93 $array['nope'] = $bookmark;
94 }
95
96 /**
97 * Test adding a bad entry: invalid ID type
98 *
99 * @expectedException Shaarli\Bookmark\Exception\InvalidBookmarkException
100 */
101 public function testArrayAccessAddBadEntryIdType()
102 {
103 $array = new BookmarkArray();
104 $bookmark = (new Bookmark())->setId('nope');
105 $bookmark->validate();
106 $array[] = $bookmark;
107 }
108
109 /**
110 * Test adding a bad entry: ID/offset not consistent
111 *
112 * @expectedException Shaarli\Bookmark\Exception\InvalidBookmarkException
113 */
114 public function testArrayAccessAddBadEntryIdOffset()
115 {
116 $array = new BookmarkArray();
117 $bookmark = (new Bookmark())->setId(11);
118 $bookmark->validate();
119 $array[14] = $bookmark;
120 }
121
122 /**
123 * Test update entries through array access.
124 */
125 public function testArrayAccessUpdateEntries()
126 {
127 $array = new BookmarkArray();
128 $bookmark = new Bookmark();
129 $bookmark->setId(11)->validate();
130 $bookmark->setTitle('old');
131 $array[] = $bookmark;
132 $bookmark = new Bookmark();
133 $bookmark->setId(11)->validate();
134 $bookmark->setTitle('test');
135 $array[] = $bookmark;
136 $this->assertCount(1, $array);
137 $this->assertEquals('test', $array[11]->getTitle());
138
139 $bookmark = new Bookmark();
140 $bookmark->setId(11)->validate();
141 $bookmark->setTitle('test2');
142 $array[11] = $bookmark;
143 $this->assertCount(1, $array);
144 $this->assertEquals('test2', $array[11]->getTitle());
145 }
146
147 /**
148 * Test delete entries through array access.
149 */
150 public function testArrayAccessDeleteEntries()
151 {
152 $array = new BookmarkArray();
153 $bookmark11 = new Bookmark();
154 $bookmark11->setId(11)->validate();
155 $array[] = $bookmark11;
156 $bookmark14 = new Bookmark();
157 $bookmark14->setId(14)->validate();
158 $array[] = $bookmark14;
159 $bookmark23 = new Bookmark();
160 $bookmark23->setId(23)->validate();
161 $array[] = $bookmark23;
162 $bookmark0 = new Bookmark();
163 $bookmark0->setId(0)->validate();
164 $array[] = $bookmark0;
165 $this->assertCount(4, $array);
166
167 unset($array[14]);
168 $this->assertCount(3, $array);
169 $this->assertEquals($bookmark11, $array[11]);
170 $this->assertEquals($bookmark23, $array[23]);
171 $this->assertEquals($bookmark0, $array[0]);
172
173 unset($array[23]);
174 $this->assertCount(2, $array);
175 $this->assertEquals($bookmark11, $array[11]);
176 $this->assertEquals($bookmark0, $array[0]);
177
178 unset($array[11]);
179 $this->assertCount(1, $array);
180 $this->assertEquals($bookmark0, $array[0]);
181
182 unset($array[0]);
183 $this->assertCount(0, $array);
184 }
185
186 /**
187 * Test iterating through array access.
188 */
189 public function testArrayAccessIterate()
190 {
191 $array = new BookmarkArray();
192 $bookmark11 = new Bookmark();
193 $bookmark11->setId(11)->validate();
194 $array[] = $bookmark11;
195 $bookmark14 = new Bookmark();
196 $bookmark14->setId(14)->validate();
197 $array[] = $bookmark14;
198 $bookmark23 = new Bookmark();
199 $bookmark23->setId(23)->validate();
200 $array[] = $bookmark23;
201 $this->assertCount(3, $array);
202
203 foreach ($array as $id => $bookmark) {
204 $this->assertEquals(${'bookmark'. $id}, $bookmark);
205 }
206 }
207
208 /**
209 * Test reordering the array.
210 */
211 public function testReorder()
212 {
213 $refDB = new \ReferenceLinkDB();
214 $refDB->write('sandbox/datastore.php');
215
216
217 $bookmarks = $refDB->getLinks();
218 $bookmarks->reorder('ASC');
219 $this->assertInstanceOf(BookmarkArray::class, $bookmarks);
220
221 $stickyIds = [11, 10];
222 $standardIds = [42, 4, 9, 1, 0, 7, 6, 8, 41];
223 $linkIds = array_merge($stickyIds, $standardIds);
224 $cpt = 0;
225 foreach ($bookmarks as $key => $value) {
226 $this->assertEquals($linkIds[$cpt++], $key);
227 }
228
229 $bookmarks = $refDB->getLinks();
230 $bookmarks->reorder('DESC');
231 $this->assertInstanceOf(BookmarkArray::class, $bookmarks);
232
233 $linkIds = array_merge(array_reverse($stickyIds), array_reverse($standardIds));
234 $cpt = 0;
235 foreach ($bookmarks as $key => $value) {
236 $this->assertEquals($linkIds[$cpt++], $key);
237 }
238 }
239}
diff --git a/tests/bookmark/BookmarkFileServiceTest.php b/tests/bookmark/BookmarkFileServiceTest.php
new file mode 100644
index 00000000..1b438a7f
--- /dev/null
+++ b/tests/bookmark/BookmarkFileServiceTest.php
@@ -0,0 +1,1042 @@
1<?php
2/**
3 * Link datastore tests
4 */
5
6namespace Shaarli\Bookmark;
7
8use DateTime;
9use PHPUnit\Framework\TestCase;
10use ReferenceLinkDB;
11use ReflectionClass;
12use Shaarli;
13use Shaarli\Bookmark\Exception\BookmarkNotFoundException;
14use Shaarli\Config\ConfigManager;
15use Shaarli\History;
16
17/**
18 * Unitary tests for LegacyLinkDBTest
19 */
20class BookmarkFileServiceTest extends TestCase
21{
22 // datastore to test write operations
23 protected static $testDatastore = 'sandbox/datastore.php';
24
25 protected static $testConf = 'sandbox/config';
26
27 protected static $testUpdates = 'sandbox/updates.txt';
28
29 /**
30 * @var ConfigManager instance.
31 */
32 protected $conf;
33
34 /**
35 * @var History instance.
36 */
37 protected $history;
38
39 /**
40 * @var ReferenceLinkDB instance.
41 */
42 protected $refDB = null;
43
44 /**
45 * @var BookmarkFileService public LinkDB instance.
46 */
47 protected $publicLinkDB = null;
48
49 /**
50 * @var BookmarkFileService private LinkDB instance.
51 */
52 protected $privateLinkDB = null;
53
54 /**
55 * Instantiates public and private LinkDBs with test data
56 *
57 * The reference datastore contains public and private bookmarks that
58 * will be used to test LinkDB's methods:
59 * - access filtering (public/private),
60 * - link searches:
61 * - by day,
62 * - by tag,
63 * - by text,
64 * - etc.
65 *
66 * Resets test data for each test
67 */
68 protected function setUp()
69 {
70 if (file_exists(self::$testDatastore)) {
71 unlink(self::$testDatastore);
72 }
73
74 if (file_exists(self::$testConf .'.json.php')) {
75 unlink(self::$testConf .'.json.php');
76 }
77
78 if (file_exists(self::$testUpdates)) {
79 unlink(self::$testUpdates);
80 }
81
82 copy('tests/utils/config/configJson.json.php', self::$testConf .'.json.php');
83 $this->conf = new ConfigManager(self::$testConf);
84 $this->conf->set('resource.datastore', self::$testDatastore);
85 $this->conf->set('resource.updates', self::$testUpdates);
86 $this->refDB = new \ReferenceLinkDB();
87 $this->refDB->write(self::$testDatastore);
88 $this->history = new History('sandbox/history.php');
89 $this->publicLinkDB = new BookmarkFileService($this->conf, $this->history, false);
90 $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true);
91 }
92
93 /**
94 * Test migrate() method with a legacy datastore.
95 */
96 public function testDatabaseMigration()
97 {
98 if (!defined('SHAARLI_VERSION')) {
99 define('SHAARLI_VERSION', 'dev');
100 }
101
102 $this->refDB = new \ReferenceLinkDB(true);
103 $this->refDB->write(self::$testDatastore);
104 $db = self::getMethod('migrate');
105 $db->invokeArgs($this->privateLinkDB, []);
106
107 $db = new \FakeBookmarkService($this->conf, $this->history, true);
108 $this->assertInstanceOf(BookmarkArray::class, $db->getBookmarks());
109 $this->assertEquals($this->refDB->countLinks(), $db->count());
110 }
111
112 /**
113 * Test get() method for a defined and saved bookmark
114 */
115 public function testGetDefinedSaved()
116 {
117 $bookmark = $this->privateLinkDB->get(42);
118 $this->assertEquals(42, $bookmark->getId());
119 $this->assertEquals('Note: I have a big ID but an old date', $bookmark->getTitle());
120 }
121
122 /**
123 * Test get() method for a defined and not saved bookmark
124 */
125 public function testGetDefinedNotSaved()
126 {
127 $bookmark = new Bookmark();
128 $this->privateLinkDB->add($bookmark);
129 $createdBookmark = $this->privateLinkDB->get(43);
130 $this->assertEquals(43, $createdBookmark->getId());
131 $this->assertEmpty($createdBookmark->getDescription());
132 }
133
134 /**
135 * Test get() method for an undefined bookmark
136 *
137 * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException
138 */
139 public function testGetUndefined()
140 {
141 $this->privateLinkDB->get(666);
142 }
143
144 /**
145 * Test add() method for a bookmark fully built
146 */
147 public function testAddFull()
148 {
149 $bookmark = new Bookmark();
150 $bookmark->setUrl($url = 'https://domain.tld/index.php');
151 $bookmark->setShortUrl('abc');
152 $bookmark->setTitle($title = 'This a brand new bookmark');
153 $bookmark->setDescription($desc = 'It should be created and written');
154 $bookmark->setTags($tags = ['tag1', 'tagssss']);
155 $bookmark->setThumbnail($thumb = 'http://thumb.tld/dle.png');
156 $bookmark->setPrivate(true);
157 $bookmark->setSticky(true);
158 $bookmark->setCreated($created = DateTime::createFromFormat('Ymd_His', '20190518_140354'));
159 $bookmark->setUpdated($updated = DateTime::createFromFormat('Ymd_His', '20190518_150354'));
160
161 $this->privateLinkDB->add($bookmark);
162 $bookmark = $this->privateLinkDB->get(43);
163 $this->assertEquals(43, $bookmark->getId());
164 $this->assertEquals($url, $bookmark->getUrl());
165 $this->assertEquals('abc', $bookmark->getShortUrl());
166 $this->assertEquals($title, $bookmark->getTitle());
167 $this->assertEquals($desc, $bookmark->getDescription());
168 $this->assertEquals($tags, $bookmark->getTags());
169 $this->assertEquals($thumb, $bookmark->getThumbnail());
170 $this->assertTrue($bookmark->isPrivate());
171 $this->assertTrue($bookmark->isSticky());
172 $this->assertEquals($created, $bookmark->getCreated());
173 $this->assertEquals($updated, $bookmark->getUpdated());
174
175 // reload from file
176 $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true);
177
178 $bookmark = $this->privateLinkDB->get(43);
179 $this->assertEquals(43, $bookmark->getId());
180 $this->assertEquals($url, $bookmark->getUrl());
181 $this->assertEquals('abc', $bookmark->getShortUrl());
182 $this->assertEquals($title, $bookmark->getTitle());
183 $this->assertEquals($desc, $bookmark->getDescription());
184 $this->assertEquals($tags, $bookmark->getTags());
185 $this->assertEquals($thumb, $bookmark->getThumbnail());
186 $this->assertTrue($bookmark->isPrivate());
187 $this->assertTrue($bookmark->isSticky());
188 $this->assertEquals($created, $bookmark->getCreated());
189 $this->assertEquals($updated, $bookmark->getUpdated());
190 }
191
192 /**
193 * Test add() method for a bookmark without any field set
194 */
195 public function testAddMinimal()
196 {
197 $bookmark = new Bookmark();
198 $this->privateLinkDB->add($bookmark);
199
200 $bookmark = $this->privateLinkDB->get(43);
201 $this->assertEquals(43, $bookmark->getId());
202 $this->assertRegExp('/\?[\w\-]{6}/', $bookmark->getUrl());
203 $this->assertRegExp('/[\w\-]{6}/', $bookmark->getShortUrl());
204 $this->assertEquals($bookmark->getUrl(), $bookmark->getTitle());
205 $this->assertEmpty($bookmark->getDescription());
206 $this->assertEmpty($bookmark->getTags());
207 $this->assertEmpty($bookmark->getThumbnail());
208 $this->assertFalse($bookmark->isPrivate());
209 $this->assertFalse($bookmark->isSticky());
210 $this->assertTrue(new \DateTime('5 seconds ago') < $bookmark->getCreated());
211 $this->assertNull($bookmark->getUpdated());
212
213 // reload from file
214 $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true);
215
216 $bookmark = $this->privateLinkDB->get(43);
217 $this->assertEquals(43, $bookmark->getId());
218 $this->assertRegExp('/\?[\w\-]{6}/', $bookmark->getUrl());
219 $this->assertRegExp('/[\w\-]{6}/', $bookmark->getShortUrl());
220 $this->assertEquals($bookmark->getUrl(), $bookmark->getTitle());
221 $this->assertEmpty($bookmark->getDescription());
222 $this->assertEmpty($bookmark->getTags());
223 $this->assertEmpty($bookmark->getThumbnail());
224 $this->assertFalse($bookmark->isPrivate());
225 $this->assertFalse($bookmark->isSticky());
226 $this->assertTrue(new \DateTime('5 seconds ago') < $bookmark->getCreated());
227 $this->assertNull($bookmark->getUpdated());
228 }
229
230 /**
231 * Test add() method for a bookmark without any field set and without writing the data store
232 *
233 * @expectedExceptionMessage Shaarli\Bookmark\Exception\BookmarkNotFoundException
234 */
235 public function testAddMinimalNoWrite()
236 {
237 $bookmark = new Bookmark();
238 $this->privateLinkDB->add($bookmark);
239
240 $bookmark = $this->privateLinkDB->get(43);
241 $this->assertEquals(43, $bookmark->getId());
242
243 // reload from file
244 $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true);
245
246 $this->privateLinkDB->get(43);
247 }
248
249 /**
250 * Test add() method while logged out
251 *
252 * @expectedException \Exception
253 * @expectedExceptionMessage You're not authorized to alter the datastore
254 */
255 public function testAddLoggedOut()
256 {
257 $this->publicLinkDB->add(new Bookmark());
258 }
259
260 /**
261 * Test add() method with an entry which is not a bookmark instance
262 *
263 * @expectedException \Exception
264 * @expectedExceptionMessage Provided data is invalid
265 */
266 public function testAddNotABookmark()
267 {
268 $this->privateLinkDB->add(['title' => 'hi!']);
269 }
270
271 /**
272 * Test add() method with a Bookmark already containing an ID
273 *
274 * @expectedException \Exception
275 * @expectedExceptionMessage This bookmarks already exists
276 */
277 public function testAddWithId()
278 {
279 $bookmark = new Bookmark();
280 $bookmark->setId(43);
281 $this->privateLinkDB->add($bookmark);
282 }
283
284 /**
285 * Test set() method for a bookmark fully built
286 */
287 public function testSetFull()
288 {
289 $bookmark = $this->privateLinkDB->get(42);
290 $bookmark->setUrl($url = 'https://domain.tld/index.php');
291 $bookmark->setShortUrl('abc');
292 $bookmark->setTitle($title = 'This a brand new bookmark');
293 $bookmark->setDescription($desc = 'It should be created and written');
294 $bookmark->setTags($tags = ['tag1', 'tagssss']);
295 $bookmark->setThumbnail($thumb = 'http://thumb.tld/dle.png');
296 $bookmark->setPrivate(true);
297 $bookmark->setSticky(true);
298 $bookmark->setCreated($created = DateTime::createFromFormat('Ymd_His', '20190518_140354'));
299 $bookmark->setUpdated($updated = DateTime::createFromFormat('Ymd_His', '20190518_150354'));
300
301 $this->privateLinkDB->set($bookmark);
302 $bookmark = $this->privateLinkDB->get(42);
303 $this->assertEquals(42, $bookmark->getId());
304 $this->assertEquals($url, $bookmark->getUrl());
305 $this->assertEquals('abc', $bookmark->getShortUrl());
306 $this->assertEquals($title, $bookmark->getTitle());
307 $this->assertEquals($desc, $bookmark->getDescription());
308 $this->assertEquals($tags, $bookmark->getTags());
309 $this->assertEquals($thumb, $bookmark->getThumbnail());
310 $this->assertTrue($bookmark->isPrivate());
311 $this->assertTrue($bookmark->isSticky());
312 $this->assertEquals($created, $bookmark->getCreated());
313 $this->assertTrue(new \DateTime('5 seconds ago') < $bookmark->getUpdated());
314
315 // reload from file
316 $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true);
317
318 $bookmark = $this->privateLinkDB->get(42);
319 $this->assertEquals(42, $bookmark->getId());
320 $this->assertEquals($url, $bookmark->getUrl());
321 $this->assertEquals('abc', $bookmark->getShortUrl());
322 $this->assertEquals($title, $bookmark->getTitle());
323 $this->assertEquals($desc, $bookmark->getDescription());
324 $this->assertEquals($tags, $bookmark->getTags());
325 $this->assertEquals($thumb, $bookmark->getThumbnail());
326 $this->assertTrue($bookmark->isPrivate());
327 $this->assertTrue($bookmark->isSticky());
328 $this->assertEquals($created, $bookmark->getCreated());
329 $this->assertTrue(new \DateTime('5 seconds ago') < $bookmark->getUpdated());
330 }
331
332 /**
333 * Test set() method for a bookmark without any field set
334 */
335 public function testSetMinimal()
336 {
337 $bookmark = $this->privateLinkDB->get(42);
338 $this->privateLinkDB->set($bookmark);
339
340 $bookmark = $this->privateLinkDB->get(42);
341 $this->assertEquals(42, $bookmark->getId());
342 $this->assertEquals('?WDWyig', $bookmark->getUrl());
343 $this->assertEquals('1eYJ1Q', $bookmark->getShortUrl());
344 $this->assertEquals('Note: I have a big ID but an old date', $bookmark->getTitle());
345 $this->assertEquals('Used to test bookmarks reordering.', $bookmark->getDescription());
346 $this->assertEquals(['ut'], $bookmark->getTags());
347 $this->assertFalse($bookmark->getThumbnail());
348 $this->assertFalse($bookmark->isPrivate());
349 $this->assertFalse($bookmark->isSticky());
350 $this->assertEquals(
351 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20100310_101010'),
352 $bookmark->getCreated()
353 );
354 $this->assertTrue(new \DateTime('5 seconds ago') < $bookmark->getUpdated());
355
356 // reload from file
357 $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true);
358
359 $bookmark = $this->privateLinkDB->get(42);
360 $this->assertEquals(42, $bookmark->getId());
361 $this->assertEquals('?WDWyig', $bookmark->getUrl());
362 $this->assertEquals('1eYJ1Q', $bookmark->getShortUrl());
363 $this->assertEquals('Note: I have a big ID but an old date', $bookmark->getTitle());
364 $this->assertEquals('Used to test bookmarks reordering.', $bookmark->getDescription());
365 $this->assertEquals(['ut'], $bookmark->getTags());
366 $this->assertFalse($bookmark->getThumbnail());
367 $this->assertFalse($bookmark->isPrivate());
368 $this->assertFalse($bookmark->isSticky());
369 $this->assertEquals(
370 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20100310_101010'),
371 $bookmark->getCreated()
372 );
373 $this->assertTrue(new \DateTime('5 seconds ago') < $bookmark->getUpdated());
374 }
375
376 /**
377 * Test set() method for a bookmark without any field set and without writing the data store
378 */
379 public function testSetMinimalNoWrite()
380 {
381 $bookmark = $this->privateLinkDB->get(42);
382 $bookmark->setTitle($title = 'hi!');
383 $this->privateLinkDB->set($bookmark, false);
384
385 $bookmark = $this->privateLinkDB->get(42);
386 $this->assertEquals(42, $bookmark->getId());
387 $this->assertEquals($title, $bookmark->getTitle());
388
389 // reload from file
390 $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true);
391
392 $bookmark = $this->privateLinkDB->get(42);
393 $this->assertEquals(42, $bookmark->getId());
394 $this->assertEquals('Note: I have a big ID but an old date', $bookmark->getTitle());
395 }
396
397 /**
398 * Test set() method while logged out
399 *
400 * @expectedException \Exception
401 * @expectedExceptionMessage You're not authorized to alter the datastore
402 */
403 public function testSetLoggedOut()
404 {
405 $this->publicLinkDB->set(new Bookmark());
406 }
407
408 /**
409 * Test set() method with an entry which is not a bookmark instance
410 *
411 * @expectedException \Exception
412 * @expectedExceptionMessage Provided data is invalid
413 */
414 public function testSetNotABookmark()
415 {
416 $this->privateLinkDB->set(['title' => 'hi!']);
417 }
418
419 /**
420 * Test set() method with a Bookmark without an ID defined.
421 *
422 * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException
423 */
424 public function testSetWithoutId()
425 {
426 $bookmark = new Bookmark();
427 $this->privateLinkDB->set($bookmark);
428 }
429
430 /**
431 * Test set() method with a Bookmark with an unknow ID
432 *
433 * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException
434 */
435 public function testSetWithUnknownId()
436 {
437 $bookmark = new Bookmark();
438 $bookmark->setId(666);
439 $this->privateLinkDB->set($bookmark);
440 }
441
442 /**
443 * Test addOrSet() method with a new ID
444 */
445 public function testAddOrSetNew()
446 {
447 $bookmark = new Bookmark();
448 $this->privateLinkDB->addOrSet($bookmark);
449
450 $bookmark = $this->privateLinkDB->get(43);
451 $this->assertEquals(43, $bookmark->getId());
452
453 // reload from file
454 $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true);
455
456 $bookmark = $this->privateLinkDB->get(43);
457 $this->assertEquals(43, $bookmark->getId());
458 }
459
460 /**
461 * Test addOrSet() method with an existing ID
462 */
463 public function testAddOrSetExisting()
464 {
465 $bookmark = $this->privateLinkDB->get(42);
466 $bookmark->setTitle($title = 'hi!');
467 $this->privateLinkDB->addOrSet($bookmark);
468
469 $bookmark = $this->privateLinkDB->get(42);
470 $this->assertEquals(42, $bookmark->getId());
471 $this->assertEquals($title, $bookmark->getTitle());
472
473 // reload from file
474 $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true);
475
476 $bookmark = $this->privateLinkDB->get(42);
477 $this->assertEquals(42, $bookmark->getId());
478 $this->assertEquals($title, $bookmark->getTitle());
479 }
480
481 /**
482 * Test addOrSet() method while logged out
483 *
484 * @expectedException \Exception
485 * @expectedExceptionMessage You're not authorized to alter the datastore
486 */
487 public function testAddOrSetLoggedOut()
488 {
489 $this->publicLinkDB->addOrSet(new Bookmark());
490 }
491
492 /**
493 * Test addOrSet() method with an entry which is not a bookmark instance
494 *
495 * @expectedException \Exception
496 * @expectedExceptionMessage Provided data is invalid
497 */
498 public function testAddOrSetNotABookmark()
499 {
500 $this->privateLinkDB->addOrSet(['title' => 'hi!']);
501 }
502
503 /**
504 * Test addOrSet() method for a bookmark without any field set and without writing the data store
505 */
506 public function testAddOrSetMinimalNoWrite()
507 {
508 $bookmark = $this->privateLinkDB->get(42);
509 $bookmark->setTitle($title = 'hi!');
510 $this->privateLinkDB->addOrSet($bookmark, false);
511
512 $bookmark = $this->privateLinkDB->get(42);
513 $this->assertEquals(42, $bookmark->getId());
514 $this->assertEquals($title, $bookmark->getTitle());
515
516 // reload from file
517 $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true);
518
519 $bookmark = $this->privateLinkDB->get(42);
520 $this->assertEquals(42, $bookmark->getId());
521 $this->assertEquals('Note: I have a big ID but an old date', $bookmark->getTitle());
522 }
523
524 /**
525 * Test remove() method with an existing Bookmark
526 *
527 * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException
528 */
529 public function testRemoveExisting()
530 {
531 $bookmark = $this->privateLinkDB->get(42);
532 $this->privateLinkDB->remove($bookmark);
533
534 $exception = null;
535 try {
536 $this->privateLinkDB->get(42);
537 } catch (BookmarkNotFoundException $e) {
538 $exception = $e;
539 }
540 $this->assertInstanceOf(BookmarkNotFoundException::class, $exception);
541
542 // reload from file
543 $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true);
544
545 $this->privateLinkDB->get(42);
546 }
547
548 /**
549 * Test remove() method while logged out
550 *
551 * @expectedException \Exception
552 * @expectedExceptionMessage You're not authorized to alter the datastore
553 */
554 public function testRemoveLoggedOut()
555 {
556 $bookmark = $this->privateLinkDB->get(42);
557 $this->publicLinkDB->remove($bookmark);
558 }
559
560 /**
561 * Test remove() method with an entry which is not a bookmark instance
562 *
563 * @expectedException \Exception
564 * @expectedExceptionMessage Provided data is invalid
565 */
566 public function testRemoveNotABookmark()
567 {
568 $this->privateLinkDB->remove(['title' => 'hi!']);
569 }
570
571 /**
572 * Test remove() method with a Bookmark with an unknown ID
573 *
574 * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException
575 */
576 public function testRemoveWithUnknownId()
577 {
578 $bookmark = new Bookmark();
579 $bookmark->setId(666);
580 $this->privateLinkDB->remove($bookmark);
581 }
582
583 /**
584 * Test exists() method
585 */
586 public function testExists()
587 {
588 $this->assertTrue($this->privateLinkDB->exists(42)); // public
589 $this->assertTrue($this->privateLinkDB->exists(6)); // private
590
591 $this->assertTrue($this->privateLinkDB->exists(42, BookmarkFilter::$ALL));
592 $this->assertTrue($this->privateLinkDB->exists(6, BookmarkFilter::$ALL));
593
594 $this->assertTrue($this->privateLinkDB->exists(42, BookmarkFilter::$PUBLIC));
595 $this->assertFalse($this->privateLinkDB->exists(6, BookmarkFilter::$PUBLIC));
596
597 $this->assertFalse($this->privateLinkDB->exists(42, BookmarkFilter::$PRIVATE));
598 $this->assertTrue($this->privateLinkDB->exists(6, BookmarkFilter::$PRIVATE));
599
600 $this->assertTrue($this->publicLinkDB->exists(42));
601 $this->assertFalse($this->publicLinkDB->exists(6));
602
603 $this->assertTrue($this->publicLinkDB->exists(42, BookmarkFilter::$PUBLIC));
604 $this->assertFalse($this->publicLinkDB->exists(6, BookmarkFilter::$PUBLIC));
605
606 $this->assertFalse($this->publicLinkDB->exists(42, BookmarkFilter::$PRIVATE));
607 $this->assertTrue($this->publicLinkDB->exists(6, BookmarkFilter::$PRIVATE));
608 }
609
610 /**
611 * Test initialize() method
612 */
613 public function testInitialize()
614 {
615 $dbSize = $this->privateLinkDB->count();
616 $this->privateLinkDB->initialize();
617 $this->assertEquals($dbSize + 2, $this->privateLinkDB->count());
618 $this->assertEquals(
619 'My secret stuff... - Pastebin.com',
620 $this->privateLinkDB->get(43)->getTitle()
621 );
622 $this->assertEquals(
623 'The personal, minimalist, super-fast, database free, bookmarking service',
624 $this->privateLinkDB->get(44)->getTitle()
625 );
626 }
627
628 /*
629 * The following tests have been taken from the legacy LinkDB test and adapted
630 * to make sure that nothing have been broken in the migration process.
631 * They mostly cover search/filters. Some of them might be redundant with the previous ones.
632 */
633
634 /**
635 * Attempt to instantiate a LinkDB whereas the datastore is not writable
636 *
637 * @expectedException Shaarli\Bookmark\Exception\NotWritableDataStoreException
638 * @expectedExceptionMessageRegExp #Couldn't load data from the data store file "null".*#
639 */
640 public function testConstructDatastoreNotWriteable()
641 {
642 $conf = new ConfigManager('tests/utils/config/configJson');
643 $conf->set('resource.datastore', 'null/store.db');
644 new BookmarkFileService($conf, $this->history, true);
645 }
646
647 /**
648 * The DB doesn't exist, ensure it is created with an empty datastore
649 */
650 public function testCheckDBNewLoggedIn()
651 {
652 unlink(self::$testDatastore);
653 $this->assertFileNotExists(self::$testDatastore);
654 new BookmarkFileService($this->conf, $this->history, true);
655 $this->assertFileExists(self::$testDatastore);
656
657 // ensure the correct data has been written
658 $this->assertGreaterThan(0, filesize(self::$testDatastore));
659 }
660
661 /**
662 * The DB doesn't exist, but not logged in, ensure it initialized, but the file is not written
663 */
664 public function testCheckDBNewLoggedOut()
665 {
666 unlink(self::$testDatastore);
667 $this->assertFileNotExists(self::$testDatastore);
668 $db = new \FakeBookmarkService($this->conf, $this->history, false);
669 $this->assertFileNotExists(self::$testDatastore);
670 $this->assertInstanceOf(BookmarkArray::class, $db->getBookmarks());
671 $this->assertCount(0, $db->getBookmarks());
672 }
673
674 /**
675 * Load public bookmarks from the DB
676 */
677 public function testReadPublicDB()
678 {
679 $this->assertEquals(
680 $this->refDB->countPublicLinks(),
681 $this->publicLinkDB->count()
682 );
683 }
684
685 /**
686 * Load public and private bookmarks from the DB
687 */
688 public function testReadPrivateDB()
689 {
690 $this->assertEquals(
691 $this->refDB->countLinks(),
692 $this->privateLinkDB->count()
693 );
694 }
695
696 /**
697 * Save the bookmarks to the DB
698 */
699 public function testSave()
700 {
701 $testDB = new BookmarkFileService($this->conf, $this->history, true);
702 $dbSize = $testDB->count();
703
704 $bookmark = new Bookmark();
705 $testDB->add($bookmark);
706
707 $testDB = new BookmarkFileService($this->conf, $this->history, true);
708 $this->assertEquals($dbSize + 1, $testDB->count());
709 }
710
711 /**
712 * Count existing bookmarks - public bookmarks hidden
713 */
714 public function testCountHiddenPublic()
715 {
716 $this->conf->set('privacy.hide_public_links', true);
717 $linkDB = new BookmarkFileService($this->conf, $this->history, false);
718
719 $this->assertEquals(0, $linkDB->count());
720 }
721
722 /**
723 * List the days for which bookmarks have been posted
724 */
725 public function testDays()
726 {
727 $this->assertEquals(
728 ['20100309', '20100310', '20121206', '20121207', '20130614', '20150310'],
729 $this->publicLinkDB->days()
730 );
731
732 $this->assertEquals(
733 ['20100309', '20100310', '20121206', '20121207', '20130614', '20141125', '20150310'],
734 $this->privateLinkDB->days()
735 );
736 }
737
738 /**
739 * The URL corresponds to an existing entry in the DB
740 */
741 public function testGetKnownLinkFromURL()
742 {
743 $link = $this->publicLinkDB->findByUrl('http://mediagoblin.org/');
744
745 $this->assertNotEquals(false, $link);
746 $this->assertContains(
747 'A free software media publishing platform',
748 $link->getDescription()
749 );
750 }
751
752 /**
753 * The URL is not in the DB
754 */
755 public function testGetUnknownLinkFromURL()
756 {
757 $this->assertEquals(
758 false,
759 $this->publicLinkDB->findByUrl('http://dev.null')
760 );
761 }
762
763 /**
764 * Lists all tags
765 */
766 public function testAllTags()
767 {
768 $this->assertEquals(
769 [
770 'web' => 3,
771 'cartoon' => 2,
772 'gnu' => 2,
773 'dev' => 1,
774 'samba' => 1,
775 'media' => 1,
776 'software' => 1,
777 'stallman' => 1,
778 'free' => 1,
779 '-exclude' => 1,
780 'hashtag' => 2,
781 // The DB contains a link with `sTuff` and another one with `stuff` tag.
782 // They need to be grouped with the first case found - order by date DESC: `sTuff`.
783 'sTuff' => 2,
784 'ut' => 1,
785 ],
786 $this->publicLinkDB->bookmarksCountPerTag()
787 );
788
789 $this->assertEquals(
790 [
791 'web' => 4,
792 'cartoon' => 3,
793 'gnu' => 2,
794 'dev' => 2,
795 'samba' => 1,
796 'media' => 1,
797 'software' => 1,
798 'stallman' => 1,
799 'free' => 1,
800 'html' => 1,
801 'w3c' => 1,
802 'css' => 1,
803 'Mercurial' => 1,
804 'sTuff' => 2,
805 '-exclude' => 1,
806 '.hidden' => 1,
807 'hashtag' => 2,
808 'tag1' => 1,
809 'tag2' => 1,
810 'tag3' => 1,
811 'tag4' => 1,
812 'ut' => 1,
813 ],
814 $this->privateLinkDB->bookmarksCountPerTag()
815 );
816 $this->assertEquals(
817 [
818 'web' => 4,
819 'cartoon' => 2,
820 'gnu' => 1,
821 'dev' => 1,
822 'samba' => 1,
823 'media' => 1,
824 'html' => 1,
825 'w3c' => 1,
826 'css' => 1,
827 'Mercurial' => 1,
828 '.hidden' => 1,
829 'hashtag' => 1,
830 ],
831 $this->privateLinkDB->bookmarksCountPerTag(['web'])
832 );
833 $this->assertEquals(
834 [
835 'web' => 1,
836 'html' => 1,
837 'w3c' => 1,
838 'css' => 1,
839 'Mercurial' => 1,
840 ],
841 $this->privateLinkDB->bookmarksCountPerTag(['web'], 'private')
842 );
843 }
844
845 /**
846 * Test filter with string.
847 */
848 public function testFilterString()
849 {
850 $tags = 'dev cartoon';
851 $request = ['searchtags' => $tags];
852 $this->assertEquals(
853 2,
854 count($this->privateLinkDB->search($request, null, true))
855 );
856 }
857
858 /**
859 * Test filter with array.
860 */
861 public function testFilterArray()
862 {
863 $tags = ['dev', 'cartoon'];
864 $request = ['searchtags' => $tags];
865 $this->assertEquals(
866 2,
867 count($this->privateLinkDB->search($request, null, true))
868 );
869 }
870
871 /**
872 * Test hidden tags feature:
873 * tags starting with a dot '.' are only visible when logged in.
874 */
875 public function testHiddenTags()
876 {
877 $tags = '.hidden';
878 $request = ['searchtags' => $tags];
879 $this->assertEquals(
880 1,
881 count($this->privateLinkDB->search($request, 'all', true))
882 );
883
884 $this->assertEquals(
885 0,
886 count($this->publicLinkDB->search($request, 'public', true))
887 );
888 }
889
890 /**
891 * Test filterHash() with a valid smallhash.
892 */
893 public function testFilterHashValid()
894 {
895 $request = smallHash('20150310_114651');
896 $this->assertEquals(
897 1,
898 count($this->publicLinkDB->findByHash($request))
899 );
900 $request = smallHash('20150310_114633' . 8);
901 $this->assertEquals(
902 1,
903 count($this->publicLinkDB->findByHash($request))
904 );
905 }
906
907 /**
908 * Test filterHash() with an invalid smallhash.
909 *
910 * @expectedException \Shaarli\Bookmark\Exception\BookmarkNotFoundException
911 */
912 public function testFilterHashInValid1()
913 {
914 $request = 'blabla';
915 $this->publicLinkDB->findByHash($request);
916 }
917
918 /**
919 * Test filterHash() with an empty smallhash.
920 *
921 * @expectedException \Shaarli\Bookmark\Exception\BookmarkNotFoundException
922 */
923 public function testFilterHashInValid()
924 {
925 $this->publicLinkDB->findByHash('');
926 }
927
928 /**
929 * Test linksCountPerTag all tags without filter.
930 * Equal occurrences should be sorted alphabetically.
931 */
932 public function testCountLinkPerTagAllNoFilter()
933 {
934 $expected = [
935 'web' => 4,
936 'cartoon' => 3,
937 'dev' => 2,
938 'gnu' => 2,
939 'hashtag' => 2,
940 'sTuff' => 2,
941 '-exclude' => 1,
942 '.hidden' => 1,
943 'Mercurial' => 1,
944 'css' => 1,
945 'free' => 1,
946 'html' => 1,
947 'media' => 1,
948 'samba' => 1,
949 'software' => 1,
950 'stallman' => 1,
951 'tag1' => 1,
952 'tag2' => 1,
953 'tag3' => 1,
954 'tag4' => 1,
955 'ut' => 1,
956 'w3c' => 1,
957 ];
958 $tags = $this->privateLinkDB->bookmarksCountPerTag();
959
960 $this->assertEquals($expected, $tags, var_export($tags, true));
961 }
962
963 /**
964 * Test linksCountPerTag all tags with filter.
965 * Equal occurrences should be sorted alphabetically.
966 */
967 public function testCountLinkPerTagAllWithFilter()
968 {
969 $expected = [
970 'gnu' => 2,
971 'hashtag' => 2,
972 '-exclude' => 1,
973 '.hidden' => 1,
974 'free' => 1,
975 'media' => 1,
976 'software' => 1,
977 'stallman' => 1,
978 'stuff' => 1,
979 'web' => 1,
980 ];
981 $tags = $this->privateLinkDB->bookmarksCountPerTag(['gnu']);
982
983 $this->assertEquals($expected, $tags, var_export($tags, true));
984 }
985
986 /**
987 * Test linksCountPerTag public tags with filter.
988 * Equal occurrences should be sorted alphabetically.
989 */
990 public function testCountLinkPerTagPublicWithFilter()
991 {
992 $expected = [
993 'gnu' => 2,
994 'hashtag' => 2,
995 '-exclude' => 1,
996 '.hidden' => 1,
997 'free' => 1,
998 'media' => 1,
999 'software' => 1,
1000 'stallman' => 1,
1001 'stuff' => 1,
1002 'web' => 1,
1003 ];
1004 $tags = $this->privateLinkDB->bookmarksCountPerTag(['gnu'], 'public');
1005
1006 $this->assertEquals($expected, $tags, var_export($tags, true));
1007 }
1008
1009 /**
1010 * Test linksCountPerTag public tags with filter.
1011 * Equal occurrences should be sorted alphabetically.
1012 */
1013 public function testCountLinkPerTagPrivateWithFilter()
1014 {
1015 $expected = [
1016 'cartoon' => 1,
1017 'dev' => 1,
1018 'tag1' => 1,
1019 'tag2' => 1,
1020 'tag3' => 1,
1021 'tag4' => 1,
1022 ];
1023 $tags = $this->privateLinkDB->bookmarksCountPerTag(['dev'], 'private');
1024
1025 $this->assertEquals($expected, $tags, var_export($tags, true));
1026 }
1027
1028 /**
1029 * Allows to test LinkDB's private methods
1030 *
1031 * @see
1032 * https://sebastian-bergmann.de/archives/881-Testing-Your-Privates.html
1033 * http://stackoverflow.com/a/2798203
1034 */
1035 protected static function getMethod($name)
1036 {
1037 $class = new ReflectionClass('Shaarli\Bookmark\BookmarkFileService');
1038 $method = $class->getMethod($name);
1039 $method->setAccessible(true);
1040 return $method;
1041 }
1042}
diff --git a/tests/bookmark/BookmarkFilterTest.php b/tests/bookmark/BookmarkFilterTest.php
new file mode 100644
index 00000000..d4c71cb9
--- /dev/null
+++ b/tests/bookmark/BookmarkFilterTest.php
@@ -0,0 +1,514 @@
1<?php
2
3namespace Shaarli\Bookmark;
4
5use Exception;
6use PHPUnit\Framework\TestCase;
7use ReferenceLinkDB;
8use Shaarli\Config\ConfigManager;
9use Shaarli\Formatter\FormatterFactory;
10use Shaarli\History;
11
12/**
13 * Class BookmarkFilterTest.
14 */
15class BookmarkFilterTest extends TestCase
16{
17 /**
18 * @var string Test datastore path.
19 */
20 protected static $testDatastore = 'sandbox/datastore.php';
21 /**
22 * @var BookmarkFilter instance.
23 */
24 protected static $linkFilter;
25
26 /**
27 * @var ReferenceLinkDB instance
28 */
29 protected static $refDB;
30
31 /**
32 * @var BookmarkFileService instance
33 */
34 protected static $bookmarkService;
35
36 /**
37 * Instantiate linkFilter with ReferenceLinkDB data.
38 */
39 public static function setUpBeforeClass()
40 {
41 $conf = new ConfigManager('tests/utils/config/configJson');
42 $conf->set('resource.datastore', self::$testDatastore);
43 self::$refDB = new \ReferenceLinkDB();
44 self::$refDB->write(self::$testDatastore);
45 $history = new History('sandbox/history.php');
46 self::$bookmarkService = new \FakeBookmarkService($conf, $history, true);
47 self::$linkFilter = new BookmarkFilter(self::$bookmarkService->getBookmarks());
48 }
49
50 /**
51 * Blank filter.
52 */
53 public function testFilter()
54 {
55 $this->assertEquals(
56 self::$refDB->countLinks(),
57 count(self::$linkFilter->filter('', ''))
58 );
59
60 $this->assertEquals(
61 self::$refDB->countLinks(),
62 count(self::$linkFilter->filter('', '', 'all'))
63 );
64
65 $this->assertEquals(
66 self::$refDB->countLinks(),
67 count(self::$linkFilter->filter('', '', 'randomstr'))
68 );
69
70 // Private only.
71 $this->assertEquals(
72 self::$refDB->countPrivateLinks(),
73 count(self::$linkFilter->filter('', '', false, 'private'))
74 );
75
76 // Public only.
77 $this->assertEquals(
78 self::$refDB->countPublicLinks(),
79 count(self::$linkFilter->filter('', '', false, 'public'))
80 );
81
82 $this->assertEquals(
83 ReferenceLinkDB::$NB_LINKS_TOTAL,
84 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TAG, ''))
85 );
86
87 $this->assertEquals(
88 self::$refDB->countUntaggedLinks(),
89 count(
90 self::$linkFilter->filter(
91 BookmarkFilter::$FILTER_TAG,
92 /*$request=*/
93 '',
94 /*$casesensitive=*/
95 false,
96 /*$visibility=*/
97 'all',
98 /*$untaggedonly=*/
99 true
100 )
101 )
102 );
103
104 $this->assertEquals(
105 ReferenceLinkDB::$NB_LINKS_TOTAL,
106 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, ''))
107 );
108 }
109
110 /**
111 * Filter bookmarks using a tag
112 */
113 public function testFilterOneTag()
114 {
115 $this->assertEquals(
116 4,
117 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TAG, 'web', false))
118 );
119
120 $this->assertEquals(
121 4,
122 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TAG, 'web', false, 'all'))
123 );
124
125 $this->assertEquals(
126 4,
127 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TAG, 'web', false, 'default-blabla'))
128 );
129
130 // Private only.
131 $this->assertEquals(
132 1,
133 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TAG, 'web', false, 'private'))
134 );
135
136 // Public only.
137 $this->assertEquals(
138 3,
139 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TAG, 'web', false, 'public'))
140 );
141 }
142
143 /**
144 * Filter bookmarks using a tag - case-sensitive
145 */
146 public function testFilterCaseSensitiveTag()
147 {
148 $this->assertEquals(
149 0,
150 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TAG, 'mercurial', true))
151 );
152
153 $this->assertEquals(
154 1,
155 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TAG, 'Mercurial', true))
156 );
157 }
158
159 /**
160 * Filter bookmarks using a tag combination
161 */
162 public function testFilterMultipleTags()
163 {
164 $this->assertEquals(
165 2,
166 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TAG, 'dev cartoon', false))
167 );
168 }
169
170 /**
171 * Filter bookmarks using a non-existent tag
172 */
173 public function testFilterUnknownTag()
174 {
175 $this->assertEquals(
176 0,
177 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TAG, 'null', false))
178 );
179 }
180
181 /**
182 * Return bookmarks for a given day
183 */
184 public function testFilterDay()
185 {
186 $this->assertEquals(
187 4,
188 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_DAY, '20121206'))
189 );
190 }
191
192 /**
193 * 404 - day not found
194 */
195 public function testFilterUnknownDay()
196 {
197 $this->assertEquals(
198 0,
199 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_DAY, '19700101'))
200 );
201 }
202
203 /**
204 * Use an invalid date format
205 * @expectedException Exception
206 * @expectedExceptionMessageRegExp /Invalid date format/
207 */
208 public function testFilterInvalidDayWithChars()
209 {
210 self::$linkFilter->filter(BookmarkFilter::$FILTER_DAY, 'Rainy day, dream away');
211 }
212
213 /**
214 * Use an invalid date format
215 * @expectedException Exception
216 * @expectedExceptionMessageRegExp /Invalid date format/
217 */
218 public function testFilterInvalidDayDigits()
219 {
220 self::$linkFilter->filter(BookmarkFilter::$FILTER_DAY, '20');
221 }
222
223 /**
224 * Retrieve a link entry with its hash
225 */
226 public function testFilterSmallHash()
227 {
228 $links = self::$linkFilter->filter(BookmarkFilter::$FILTER_HASH, 'IuWvgA');
229
230 $this->assertEquals(
231 1,
232 count($links)
233 );
234
235 $this->assertEquals(
236 'MediaGoblin',
237 $links[7]->getTitle()
238 );
239 }
240
241 /**
242 * No link for this hash
243 *
244 * @expectedException \Shaarli\Bookmark\Exception\BookmarkNotFoundException
245 */
246 public function testFilterUnknownSmallHash()
247 {
248 self::$linkFilter->filter(BookmarkFilter::$FILTER_HASH, 'Iblaah');
249 }
250
251 /**
252 * Full-text search - no result found.
253 */
254 public function testFilterFullTextNoResult()
255 {
256 $this->assertEquals(
257 0,
258 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'azertyuiop'))
259 );
260 }
261
262 /**
263 * Full-text search - result from a link's URL
264 */
265 public function testFilterFullTextURL()
266 {
267 $this->assertEquals(
268 2,
269 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'ars.userfriendly.org'))
270 );
271
272 $this->assertEquals(
273 2,
274 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'ars org'))
275 );
276 }
277
278 /**
279 * Full-text search - result from a link's title only
280 */
281 public function testFilterFullTextTitle()
282 {
283 // use miscellaneous cases
284 $this->assertEquals(
285 2,
286 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'userfriendly -'))
287 );
288 $this->assertEquals(
289 2,
290 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'UserFriendly -'))
291 );
292 $this->assertEquals(
293 2,
294 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'uSeRFrIendlY -'))
295 );
296
297 // use miscellaneous case and offset
298 $this->assertEquals(
299 2,
300 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'RFrIendL'))
301 );
302 }
303
304 /**
305 * Full-text search - result from the link's description only
306 */
307 public function testFilterFullTextDescription()
308 {
309 $this->assertEquals(
310 1,
311 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'publishing media'))
312 );
313
314 $this->assertEquals(
315 1,
316 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'mercurial w3c'))
317 );
318
319 $this->assertEquals(
320 3,
321 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, '"free software"'))
322 );
323 }
324
325 /**
326 * Full-text search - result from the link's tags only
327 */
328 public function testFilterFullTextTags()
329 {
330 $this->assertEquals(
331 6,
332 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'web'))
333 );
334
335 $this->assertEquals(
336 6,
337 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'web', 'all'))
338 );
339
340 $this->assertEquals(
341 6,
342 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'web', 'bla'))
343 );
344
345 // Private only.
346 $this->assertEquals(
347 1,
348 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'web', false, 'private'))
349 );
350
351 // Public only.
352 $this->assertEquals(
353 5,
354 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'web', false, 'public'))
355 );
356 }
357
358 /**
359 * Full-text search - result set from mixed sources
360 */
361 public function testFilterFullTextMixed()
362 {
363 $this->assertEquals(
364 3,
365 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'free software'))
366 );
367 }
368
369 /**
370 * Full-text search - test exclusion with '-'.
371 */
372 public function testExcludeSearch()
373 {
374 $this->assertEquals(
375 1,
376 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'free -gnu'))
377 );
378
379 $this->assertEquals(
380 ReferenceLinkDB::$NB_LINKS_TOTAL - 1,
381 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, '-revolution'))
382 );
383 }
384
385 /**
386 * Full-text search - test AND, exact terms and exclusion combined, across fields.
387 */
388 public function testMultiSearch()
389 {
390 $this->assertEquals(
391 2,
392 count(self::$linkFilter->filter(
393 BookmarkFilter::$FILTER_TEXT,
394 '"Free Software " stallman "read this" @website stuff'
395 ))
396 );
397
398 $this->assertEquals(
399 1,
400 count(self::$linkFilter->filter(
401 BookmarkFilter::$FILTER_TEXT,
402 '"free software " stallman "read this" -beard @website stuff'
403 ))
404 );
405 }
406
407 /**
408 * Full-text search - make sure that exact search won't work across fields.
409 */
410 public function testSearchExactTermMultiFieldsKo()
411 {
412 $this->assertEquals(
413 0,
414 count(self::$linkFilter->filter(
415 BookmarkFilter::$FILTER_TEXT,
416 '"designer naming"'
417 ))
418 );
419
420 $this->assertEquals(
421 0,
422 count(self::$linkFilter->filter(
423 BookmarkFilter::$FILTER_TEXT,
424 '"designernaming"'
425 ))
426 );
427 }
428
429 /**
430 * Tag search with exclusion.
431 */
432 public function testTagFilterWithExclusion()
433 {
434 $this->assertEquals(
435 1,
436 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TAG, 'gnu -free'))
437 );
438
439 $this->assertEquals(
440 ReferenceLinkDB::$NB_LINKS_TOTAL - 1,
441 count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TAG, '-free'))
442 );
443 }
444
445 /**
446 * Test crossed search (terms + tags).
447 */
448 public function testFilterCrossedSearch()
449 {
450 $terms = '"Free Software " stallman "read this" @website stuff';
451 $tags = 'free';
452 $this->assertEquals(
453 1,
454 count(self::$linkFilter->filter(
455 BookmarkFilter::$FILTER_TAG | BookmarkFilter::$FILTER_TEXT,
456 array($tags, $terms)
457 ))
458 );
459 $this->assertEquals(
460 2,
461 count(self::$linkFilter->filter(
462 BookmarkFilter::$FILTER_TAG | BookmarkFilter::$FILTER_TEXT,
463 array('', $terms)
464 ))
465 );
466 $this->assertEquals(
467 1,
468 count(self::$linkFilter->filter(
469 BookmarkFilter::$FILTER_TAG | BookmarkFilter::$FILTER_TEXT,
470 array(false, 'PSR-2')
471 ))
472 );
473 $this->assertEquals(
474 1,
475 count(self::$linkFilter->filter(
476 BookmarkFilter::$FILTER_TAG | BookmarkFilter::$FILTER_TEXT,
477 array($tags, '')
478 ))
479 );
480 $this->assertEquals(
481 ReferenceLinkDB::$NB_LINKS_TOTAL,
482 count(self::$linkFilter->filter(
483 BookmarkFilter::$FILTER_TAG | BookmarkFilter::$FILTER_TEXT,
484 ''
485 ))
486 );
487 }
488
489 /**
490 * Filter bookmarks by #hashtag.
491 */
492 public function testFilterByHashtag()
493 {
494 $hashtag = 'hashtag';
495 $this->assertEquals(
496 3,
497 count(self::$linkFilter->filter(
498 BookmarkFilter::$FILTER_TAG,
499 $hashtag
500 ))
501 );
502
503 $hashtag = 'private';
504 $this->assertEquals(
505 1,
506 count(self::$linkFilter->filter(
507 BookmarkFilter::$FILTER_TAG,
508 $hashtag,
509 false,
510 'private'
511 ))
512 );
513 }
514}
diff --git a/tests/bookmark/BookmarkInitializerTest.php b/tests/bookmark/BookmarkInitializerTest.php
new file mode 100644
index 00000000..d23eb069
--- /dev/null
+++ b/tests/bookmark/BookmarkInitializerTest.php
@@ -0,0 +1,120 @@
1<?php
2
3namespace Shaarli\Bookmark;
4
5use PHPUnit\Framework\TestCase;
6use ReferenceLinkDB;
7use Shaarli\Config\ConfigManager;
8use Shaarli\History;
9
10/**
11 * Class BookmarkInitializerTest
12 * @package Shaarli\Bookmark
13 */
14class BookmarkInitializerTest extends TestCase
15{
16 /** @var string Path of test data store */
17 protected static $testDatastore = 'sandbox/datastore.php';
18
19 /** @var string Path of test config file */
20 protected static $testConf = 'sandbox/config';
21
22 /**
23 * @var ConfigManager instance.
24 */
25 protected $conf;
26
27 /**
28 * @var History instance.
29 */
30 protected $history;
31
32 /** @var BookmarkServiceInterface instance */
33 protected $bookmarkService;
34
35 /** @var BookmarkInitializer instance */
36 protected $initializer;
37
38 /**
39 * Initialize an empty BookmarkFileService
40 */
41 public function setUp()
42 {
43 if (file_exists(self::$testDatastore)) {
44 unlink(self::$testDatastore);
45 }
46
47 copy('tests/utils/config/configJson.json.php', self::$testConf .'.json.php');
48 $this->conf = new ConfigManager(self::$testConf);
49 $this->conf->set('resource.datastore', self::$testDatastore);
50 $this->history = new History('sandbox/history.php');
51 $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, true);
52
53 $this->initializer = new BookmarkInitializer($this->bookmarkService);
54 }
55
56 /**
57 * Test initialize() with an empty data store.
58 */
59 public function testInitializeEmptyDataStore()
60 {
61 $refDB = new \ReferenceLinkDB();
62 $refDB->write(self::$testDatastore);
63 $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, true);
64 $this->initializer = new BookmarkInitializer($this->bookmarkService);
65
66 $this->initializer->initialize();
67
68 $this->assertEquals($refDB->countLinks() + 2, $this->bookmarkService->count());
69 $bookmark = $this->bookmarkService->get(43);
70 $this->assertEquals(43, $bookmark->getId());
71 $this->assertEquals('My secret stuff... - Pastebin.com', $bookmark->getTitle());
72 $this->assertTrue($bookmark->isPrivate());
73
74 $bookmark = $this->bookmarkService->get(44);
75 $this->assertEquals(44, $bookmark->getId());
76 $this->assertEquals(
77 'The personal, minimalist, super-fast, database free, bookmarking service',
78 $bookmark->getTitle()
79 );
80 $this->assertFalse($bookmark->isPrivate());
81
82 // Reload from file
83 $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, true);
84 $this->assertEquals($refDB->countLinks() + 2, $this->bookmarkService->count());
85 $bookmark = $this->bookmarkService->get(43);
86 $this->assertEquals(43, $bookmark->getId());
87 $this->assertEquals('My secret stuff... - Pastebin.com', $bookmark->getTitle());
88 $this->assertTrue($bookmark->isPrivate());
89
90 $bookmark = $this->bookmarkService->get(44);
91 $this->assertEquals(44, $bookmark->getId());
92 $this->assertEquals(
93 'The personal, minimalist, super-fast, database free, bookmarking service',
94 $bookmark->getTitle()
95 );
96 $this->assertFalse($bookmark->isPrivate());
97 }
98
99 /**
100 * Test initialize() with a data store containing bookmarks.
101 */
102 public function testInitializeNotEmptyDataStore()
103 {
104 $this->initializer->initialize();
105
106 $this->assertEquals(2, $this->bookmarkService->count());
107 $bookmark = $this->bookmarkService->get(0);
108 $this->assertEquals(0, $bookmark->getId());
109 $this->assertEquals('My secret stuff... - Pastebin.com', $bookmark->getTitle());
110 $this->assertTrue($bookmark->isPrivate());
111
112 $bookmark = $this->bookmarkService->get(1);
113 $this->assertEquals(1, $bookmark->getId());
114 $this->assertEquals(
115 'The personal, minimalist, super-fast, database free, bookmarking service',
116 $bookmark->getTitle()
117 );
118 $this->assertFalse($bookmark->isPrivate());
119 }
120}
diff --git a/tests/bookmark/BookmarkTest.php b/tests/bookmark/BookmarkTest.php
new file mode 100644
index 00000000..9a3bbbfc
--- /dev/null
+++ b/tests/bookmark/BookmarkTest.php
@@ -0,0 +1,388 @@
1<?php
2
3namespace Shaarli\Bookmark;
4
5use PHPUnit\Framework\TestCase;
6use Shaarli\Bookmark\Exception\InvalidBookmarkException;
7
8/**
9 * Class BookmarkTest
10 */
11class BookmarkTest extends TestCase
12{
13 /**
14 * Test fromArray() with a link with full data
15 */
16 public function testFromArrayFull()
17 {
18 $data = [
19 'id' => 1,
20 'shorturl' => 'abc',
21 'url' => 'https://domain.tld/oof.html?param=value#anchor',
22 'title' => 'This is an array link',
23 'description' => 'HTML desc<br><p>hi!</p>',
24 'thumbnail' => 'https://domain.tld/pic.png',
25 'sticky' => true,
26 'created' => new \DateTime('-1 minute'),
27 'tags' => ['tag1', 'tag2', 'chair'],
28 'updated' => new \DateTime(),
29 'private' => true,
30 ];
31
32 $bookmark = (new Bookmark())->fromArray($data);
33 $this->assertEquals($data['id'], $bookmark->getId());
34 $this->assertEquals($data['shorturl'], $bookmark->getShortUrl());
35 $this->assertEquals($data['url'], $bookmark->getUrl());
36 $this->assertEquals($data['title'], $bookmark->getTitle());
37 $this->assertEquals($data['description'], $bookmark->getDescription());
38 $this->assertEquals($data['thumbnail'], $bookmark->getThumbnail());
39 $this->assertEquals($data['sticky'], $bookmark->isSticky());
40 $this->assertEquals($data['created'], $bookmark->getCreated());
41 $this->assertEquals($data['tags'], $bookmark->getTags());
42 $this->assertEquals('tag1 tag2 chair', $bookmark->getTagsString());
43 $this->assertEquals($data['updated'], $bookmark->getUpdated());
44 $this->assertEquals($data['private'], $bookmark->isPrivate());
45 $this->assertFalse($bookmark->isNote());
46 }
47
48 /**
49 * Test fromArray() with a link with minimal data.
50 * Note that I use null values everywhere but this should not happen in the real world.
51 */
52 public function testFromArrayMinimal()
53 {
54 $data = [
55 'id' => null,
56 'shorturl' => null,
57 'url' => null,
58 'title' => null,
59 'description' => null,
60 'created' => null,
61 'tags' => null,
62 'private' => null,
63 ];
64
65 $bookmark = (new Bookmark())->fromArray($data);
66 $this->assertNull($bookmark->getId());
67 $this->assertNull($bookmark->getShortUrl());
68 $this->assertNull($bookmark->getUrl());
69 $this->assertNull($bookmark->getTitle());
70 $this->assertEquals('', $bookmark->getDescription());
71 $this->assertNull($bookmark->getCreated());
72 $this->assertEquals([], $bookmark->getTags());
73 $this->assertEquals('', $bookmark->getTagsString());
74 $this->assertNull($bookmark->getUpdated());
75 $this->assertFalse($bookmark->getThumbnail());
76 $this->assertFalse($bookmark->isSticky());
77 $this->assertFalse($bookmark->isPrivate());
78 $this->assertTrue($bookmark->isNote());
79 }
80
81 /**
82 * Test validate() with a valid minimal bookmark
83 */
84 public function testValidateValidFullBookmark()
85 {
86 $bookmark = new Bookmark();
87 $bookmark->setId(2);
88 $bookmark->setShortUrl('abc');
89 $bookmark->setCreated($date = \DateTime::createFromFormat('Ymd_His', '20190514_200102'));
90 $bookmark->setUpdated($dateUp = \DateTime::createFromFormat('Ymd_His', '20190514_210203'));
91 $bookmark->setUrl($url = 'https://domain.tld/oof.html?param=value#anchor');
92 $bookmark->setTitle($title = 'This is an array link');
93 $bookmark->setDescription($desc = 'HTML desc<br><p>hi!</p>');
94 $bookmark->setTags($tags = ['tag1', 'tag2', 'chair']);
95 $bookmark->setThumbnail($thumb = 'https://domain.tld/pic.png');
96 $bookmark->setPrivate(true);
97 $bookmark->validate();
98
99 $this->assertEquals(2, $bookmark->getId());
100 $this->assertEquals('abc', $bookmark->getShortUrl());
101 $this->assertEquals($date, $bookmark->getCreated());
102 $this->assertEquals($dateUp, $bookmark->getUpdated());
103 $this->assertEquals($url, $bookmark->getUrl());
104 $this->assertEquals($title, $bookmark->getTitle());
105 $this->assertEquals($desc, $bookmark->getDescription());
106 $this->assertEquals($tags, $bookmark->getTags());
107 $this->assertEquals(implode(' ', $tags), $bookmark->getTagsString());
108 $this->assertEquals($thumb, $bookmark->getThumbnail());
109 $this->assertTrue($bookmark->isPrivate());
110 $this->assertFalse($bookmark->isNote());
111 }
112
113 /**
114 * Test validate() with a valid minimal bookmark
115 */
116 public function testValidateValidMinimalBookmark()
117 {
118 $bookmark = new Bookmark();
119 $bookmark->setId(1);
120 $bookmark->setShortUrl('abc');
121 $bookmark->setCreated($date = \DateTime::createFromFormat('Ymd_His', '20190514_200102'));
122 $bookmark->validate();
123
124 $this->assertEquals(1, $bookmark->getId());
125 $this->assertEquals('abc', $bookmark->getShortUrl());
126 $this->assertEquals($date, $bookmark->getCreated());
127 $this->assertEquals('?abc', $bookmark->getUrl());
128 $this->assertEquals('?abc', $bookmark->getTitle());
129 $this->assertEquals('', $bookmark->getDescription());
130 $this->assertEquals([], $bookmark->getTags());
131 $this->assertEquals('', $bookmark->getTagsString());
132 $this->assertFalse($bookmark->getThumbnail());
133 $this->assertFalse($bookmark->isPrivate());
134 $this->assertTrue($bookmark->isNote());
135 $this->assertNull($bookmark->getUpdated());
136 }
137
138 /**
139 * Test validate() with a a bookmark without ID.
140 */
141 public function testValidateNotValidNoId()
142 {
143 $bookmark = new Bookmark();
144 $bookmark->setShortUrl('abc');
145 $bookmark->setCreated(\DateTime::createFromFormat('Ymd_His', '20190514_200102'));
146 $exception = null;
147 try {
148 $bookmark->validate();
149 } catch (InvalidBookmarkException $e) {
150 $exception = $e;
151 }
152 $this->assertNotNull($exception);
153 $this->assertContains('- ID: '. PHP_EOL, $exception->getMessage());
154 }
155
156 /**
157 * Test validate() with a a bookmark with a non integer ID.
158 */
159 public function testValidateNotValidStringId()
160 {
161 $bookmark = new Bookmark();
162 $bookmark->setId('str');
163 $bookmark->setShortUrl('abc');
164 $bookmark->setCreated(\DateTime::createFromFormat('Ymd_His', '20190514_200102'));
165 $exception = null;
166 try {
167 $bookmark->validate();
168 } catch (InvalidBookmarkException $e) {
169 $exception = $e;
170 }
171 $this->assertNotNull($exception);
172 $this->assertContains('- ID: str'. PHP_EOL, $exception->getMessage());
173 }
174
175 /**
176 * Test validate() with a a bookmark without short url.
177 */
178 public function testValidateNotValidNoShortUrl()
179 {
180 $bookmark = new Bookmark();
181 $bookmark->setId(1);
182 $bookmark->setCreated(\DateTime::createFromFormat('Ymd_His', '20190514_200102'));
183 $bookmark->setShortUrl(null);
184 $exception = null;
185 try {
186 $bookmark->validate();
187 } catch (InvalidBookmarkException $e) {
188 $exception = $e;
189 }
190 $this->assertNotNull($exception);
191 $this->assertContains('- ShortUrl: '. PHP_EOL, $exception->getMessage());
192 }
193
194 /**
195 * Test validate() with a a bookmark without created datetime.
196 */
197 public function testValidateNotValidNoCreated()
198 {
199 $bookmark = new Bookmark();
200 $bookmark->setId(1);
201 $bookmark->setShortUrl('abc');
202 $bookmark->setCreated(null);
203 $exception = null;
204 try {
205 $bookmark->validate();
206 } catch (InvalidBookmarkException $e) {
207 $exception = $e;
208 }
209 $this->assertNotNull($exception);
210 $this->assertContains('- Created: '. PHP_EOL, $exception->getMessage());
211 }
212
213 /**
214 * Test validate() with a a bookmark with a bad created datetime.
215 */
216 public function testValidateNotValidBadCreated()
217 {
218 $bookmark = new Bookmark();
219 $bookmark->setId(1);
220 $bookmark->setShortUrl('abc');
221 $bookmark->setCreated('hi!');
222 $exception = null;
223 try {
224 $bookmark->validate();
225 } catch (InvalidBookmarkException $e) {
226 $exception = $e;
227 }
228 $this->assertNotNull($exception);
229 $this->assertContains('- Created: Not a DateTime object'. PHP_EOL, $exception->getMessage());
230 }
231
232 /**
233 * Test setId() and make sure that default fields are generated.
234 */
235 public function testSetIdEmptyGeneratedFields()
236 {
237 $bookmark = new Bookmark();
238 $bookmark->setId(2);
239
240 $this->assertEquals(2, $bookmark->getId());
241 $this->assertRegExp('/[\w\-]{6}/', $bookmark->getShortUrl());
242 $this->assertTrue(new \DateTime('5 seconds ago') < $bookmark->getCreated());
243 }
244
245 /**
246 * Test setId() and with generated fields already set.
247 */
248 public function testSetIdSetGeneratedFields()
249 {
250 $bookmark = new Bookmark();
251 $bookmark->setShortUrl('abc');
252 $bookmark->setCreated($date = \DateTime::createFromFormat('Ymd_His', '20190514_200102'));
253 $bookmark->setId(2);
254
255 $this->assertEquals(2, $bookmark->getId());
256 $this->assertEquals('abc', $bookmark->getShortUrl());
257 $this->assertEquals($date, $bookmark->getCreated());
258 }
259
260 /**
261 * Test setUrl() and make sure it accepts custom protocols
262 */
263 public function testGetUrlWithValidProtocols()
264 {
265 $bookmark = new Bookmark();
266 $bookmark->setUrl($url = 'myprotocol://helloworld', ['myprotocol']);
267 $this->assertEquals($url, $bookmark->getUrl());
268
269 $bookmark->setUrl($url = 'https://helloworld.tld', ['myprotocol']);
270 $this->assertEquals($url, $bookmark->getUrl());
271 }
272
273 /**
274 * Test setUrl() and make sure it accepts custom protocols
275 */
276 public function testGetUrlWithNotValidProtocols()
277 {
278 $bookmark = new Bookmark();
279 $bookmark->setUrl('myprotocol://helloworld', []);
280 $this->assertEquals('http://helloworld', $bookmark->getUrl());
281
282 $bookmark->setUrl($url = 'https://helloworld.tld', []);
283 $this->assertEquals($url, $bookmark->getUrl());
284 }
285
286 /**
287 * Test setTagsString() with exotic data
288 */
289 public function testSetTagsString()
290 {
291 $bookmark = new Bookmark();
292
293 $str = 'tag1 tag2 tag3.tag3-2, tag4 , -tag5 ';
294 $bookmark->setTagsString($str);
295 $this->assertEquals(
296 [
297 'tag1',
298 'tag2',
299 'tag3.tag3-2',
300 'tag4',
301 'tag5',
302 ],
303 $bookmark->getTags()
304 );
305 }
306
307 /**
308 * Test setTags() with exotic data
309 */
310 public function testSetTags()
311 {
312 $bookmark = new Bookmark();
313
314 $array = [
315 'tag1 ',
316 ' tag2',
317 'tag3.tag3-2,',
318 ', tag4',
319 ', ',
320 '-tag5 ',
321 ];
322 $bookmark->setTags($array);
323 $this->assertEquals(
324 [
325 'tag1',
326 'tag2',
327 'tag3.tag3-2',
328 'tag4',
329 'tag5',
330 ],
331 $bookmark->getTags()
332 );
333 }
334
335 /**
336 * Test renameTag()
337 */
338 public function testRenameTag()
339 {
340 $bookmark = new Bookmark();
341 $bookmark->setTags(['tag1', 'tag2', 'chair']);
342 $bookmark->renameTag('chair', 'table');
343 $this->assertEquals(['tag1', 'tag2', 'table'], $bookmark->getTags());
344 $bookmark->renameTag('tag1', 'tag42');
345 $this->assertEquals(['tag42', 'tag2', 'table'], $bookmark->getTags());
346 $bookmark->renameTag('tag42', 'tag43');
347 $this->assertEquals(['tag43', 'tag2', 'table'], $bookmark->getTags());
348 $bookmark->renameTag('table', 'desk');
349 $this->assertEquals(['tag43', 'tag2', 'desk'], $bookmark->getTags());
350 }
351
352 /**
353 * Test renameTag() with a tag that is not present in the bookmark
354 */
355 public function testRenameTagNotExists()
356 {
357 $bookmark = new Bookmark();
358 $bookmark->setTags(['tag1', 'tag2', 'chair']);
359 $bookmark->renameTag('nope', 'table');
360 $this->assertEquals(['tag1', 'tag2', 'chair'], $bookmark->getTags());
361 }
362
363 /**
364 * Test deleteTag()
365 */
366 public function testDeleteTag()
367 {
368 $bookmark = new Bookmark();
369 $bookmark->setTags(['tag1', 'tag2', 'chair']);
370 $bookmark->deleteTag('chair');
371 $this->assertEquals(['tag1', 'tag2'], $bookmark->getTags());
372 $bookmark->deleteTag('tag1');
373 $this->assertEquals(['tag2'], $bookmark->getTags());
374 $bookmark->deleteTag('tag2');
375 $this->assertEquals([], $bookmark->getTags());
376 }
377
378 /**
379 * Test deleteTag() with a tag that is not present in the bookmark
380 */
381 public function testDeleteTagNotExists()
382 {
383 $bookmark = new Bookmark();
384 $bookmark->setTags(['tag1', 'tag2', 'chair']);
385 $bookmark->deleteTag('nope');
386 $this->assertEquals(['tag1', 'tag2', 'chair'], $bookmark->getTags());
387 }
388}
diff --git a/tests/bookmark/LinkUtilsTest.php b/tests/bookmark/LinkUtilsTest.php
index 78cb8f2a..591976f2 100644
--- a/tests/bookmark/LinkUtilsTest.php
+++ b/tests/bookmark/LinkUtilsTest.php
@@ -389,15 +389,6 @@ class LinkUtilsTest extends TestCase
389 } 389 }
390 390
391 /** 391 /**
392 * Test count_private.
393 */
394 public function testCountPrivateLinks()
395 {
396 $refDB = new ReferenceLinkDB();
397 $this->assertEquals($refDB->countPrivateLinks(), count_private($refDB->getLinks()));
398 }
399
400 /**
401 * Test text2clickable. 392 * Test text2clickable.
402 */ 393 */
403 public function testText2clickable() 394 public function testText2clickable()
diff --git a/tests/bootstrap.php b/tests/bootstrap.php
index d36d73cd..0afbcba6 100644
--- a/tests/bootstrap.php
+++ b/tests/bootstrap.php
@@ -4,3 +4,21 @@ require_once 'vendor/autoload.php';
4 4
5$conf = new \Shaarli\Config\ConfigManager('tests/utils/config/configJson'); 5$conf = new \Shaarli\Config\ConfigManager('tests/utils/config/configJson');
6new \Shaarli\Languages('en', $conf); 6new \Shaarli\Languages('en', $conf);
7
8// is_iterable is only compatible with PHP 7.1+
9if (!function_exists('is_iterable')) {
10 function is_iterable($var)
11 {
12 return is_array($var) || $var instanceof \Traversable;
13 }
14}
15
16// TODO: remove this after fixing UT
17require_once 'application/bookmark/LinkUtils.php';
18require_once 'application/Utils.php';
19require_once 'application/http/UrlUtils.php';
20require_once 'application/http/HttpUtils.php';
21require_once 'application/feed/Cache.php';
22require_once 'tests/utils/ReferenceLinkDB.php';
23require_once 'tests/utils/ReferenceHistory.php';
24require_once 'tests/utils/FakeBookmarkService.php';
diff --git a/tests/config/ConfigJsonTest.php b/tests/config/ConfigJsonTest.php
index 95ad060b..33160eb0 100644
--- a/tests/config/ConfigJsonTest.php
+++ b/tests/config/ConfigJsonTest.php
@@ -24,7 +24,7 @@ class ConfigJsonTest extends \PHPUnit\Framework\TestCase
24 $conf = $this->configIO->read('tests/utils/config/configJson.json.php'); 24 $conf = $this->configIO->read('tests/utils/config/configJson.json.php');
25 $this->assertEquals('root', $conf['credentials']['login']); 25 $this->assertEquals('root', $conf['credentials']['login']);
26 $this->assertEquals('lala', $conf['redirector']['url']); 26 $this->assertEquals('lala', $conf['redirector']['url']);
27 $this->assertEquals('tests/utils/config/datastore.php', $conf['resource']['datastore']); 27 $this->assertEquals('sandbox/datastore.php', $conf['resource']['datastore']);
28 $this->assertEquals('1', $conf['plugins']['WALLABAG_VERSION']); 28 $this->assertEquals('1', $conf['plugins']['WALLABAG_VERSION']);
29 } 29 }
30 30
diff --git a/tests/feed/FeedBuilderTest.php b/tests/feed/FeedBuilderTest.php
index b496cb4c..a43ff672 100644
--- a/tests/feed/FeedBuilderTest.php
+++ b/tests/feed/FeedBuilderTest.php
@@ -4,7 +4,12 @@ namespace Shaarli\Feed;
4 4
5use DateTime; 5use DateTime;
6use ReferenceLinkDB; 6use ReferenceLinkDB;
7use Shaarli\Bookmark\Bookmark;
8use Shaarli\Bookmark\BookmarkFileService;
7use Shaarli\Bookmark\LinkDB; 9use Shaarli\Bookmark\LinkDB;
10use Shaarli\Config\ConfigManager;
11use Shaarli\Formatter\FormatterFactory;
12use Shaarli\History;
8 13
9/** 14/**
10 * FeedBuilderTest class. 15 * FeedBuilderTest class.
@@ -30,7 +35,9 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase
30 35
31 protected static $testDatastore = 'sandbox/datastore.php'; 36 protected static $testDatastore = 'sandbox/datastore.php';
32 37
33 public static $linkDB; 38 public static $bookmarkService;
39
40 public static $formatter;
34 41
35 public static $serverInfo; 42 public static $serverInfo;
36 43
@@ -39,9 +46,15 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase
39 */ 46 */
40 public static function setUpBeforeClass() 47 public static function setUpBeforeClass()
41 { 48 {
42 $refLinkDB = new ReferenceLinkDB(); 49 $conf = new ConfigManager('tests/utils/config/configJson');
50 $conf->set('resource.datastore', self::$testDatastore);
51 $refLinkDB = new \ReferenceLinkDB();
43 $refLinkDB->write(self::$testDatastore); 52 $refLinkDB->write(self::$testDatastore);
44 self::$linkDB = new LinkDB(self::$testDatastore, true, false); 53 $history = new History('sandbox/history.php');
54 $factory = new FormatterFactory($conf);
55 self::$formatter = $factory->getFormatter();
56 self::$bookmarkService = new BookmarkFileService($conf, $history, true);
57
45 self::$serverInfo = array( 58 self::$serverInfo = array(
46 'HTTPS' => 'Off', 59 'HTTPS' => 'Off',
47 'SERVER_NAME' => 'host.tld', 60 'SERVER_NAME' => 'host.tld',
@@ -56,15 +69,15 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase
56 */ 69 */
57 public function testGetTypeLanguage() 70 public function testGetTypeLanguage()
58 { 71 {
59 $feedBuilder = new FeedBuilder(null, FeedBuilder::$FEED_ATOM, null, null, false); 72 $feedBuilder = new FeedBuilder(null, self::$formatter, FeedBuilder::$FEED_ATOM, null, null, false);
60 $feedBuilder->setLocale(self::$LOCALE); 73 $feedBuilder->setLocale(self::$LOCALE);
61 $this->assertEquals(self::$ATOM_LANGUAGUE, $feedBuilder->getTypeLanguage()); 74 $this->assertEquals(self::$ATOM_LANGUAGUE, $feedBuilder->getTypeLanguage());
62 $feedBuilder = new FeedBuilder(null, FeedBuilder::$FEED_RSS, null, null, false); 75 $feedBuilder = new FeedBuilder(null, self::$formatter, FeedBuilder::$FEED_RSS, null, null, false);
63 $feedBuilder->setLocale(self::$LOCALE); 76 $feedBuilder->setLocale(self::$LOCALE);
64 $this->assertEquals(self::$RSS_LANGUAGE, $feedBuilder->getTypeLanguage()); 77 $this->assertEquals(self::$RSS_LANGUAGE, $feedBuilder->getTypeLanguage());
65 $feedBuilder = new FeedBuilder(null, FeedBuilder::$FEED_ATOM, null, null, false); 78 $feedBuilder = new FeedBuilder(null, self::$formatter, FeedBuilder::$FEED_ATOM, null, null, false);
66 $this->assertEquals('en', $feedBuilder->getTypeLanguage()); 79 $this->assertEquals('en', $feedBuilder->getTypeLanguage());
67 $feedBuilder = new FeedBuilder(null, FeedBuilder::$FEED_RSS, null, null, false); 80 $feedBuilder = new FeedBuilder(null, self::$formatter, FeedBuilder::$FEED_RSS, null, null, false);
68 $this->assertEquals('en-en', $feedBuilder->getTypeLanguage()); 81 $this->assertEquals('en-en', $feedBuilder->getTypeLanguage());
69 } 82 }
70 83
@@ -73,7 +86,14 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase
73 */ 86 */
74 public function testRSSBuildData() 87 public function testRSSBuildData()
75 { 88 {
76 $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_RSS, self::$serverInfo, null, false); 89 $feedBuilder = new FeedBuilder(
90 self::$bookmarkService,
91 self::$formatter,
92 FeedBuilder::$FEED_RSS,
93 self::$serverInfo,
94 null,
95 false
96 );
77 $feedBuilder->setLocale(self::$LOCALE); 97 $feedBuilder->setLocale(self::$LOCALE);
78 $data = $feedBuilder->buildData(); 98 $data = $feedBuilder->buildData();
79 // Test headers (RSS) 99 // Test headers (RSS)
@@ -88,7 +108,7 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase
88 // Test first not pinned link (note link) 108 // Test first not pinned link (note link)
89 $link = $data['links'][array_keys($data['links'])[2]]; 109 $link = $data['links'][array_keys($data['links'])[2]];
90 $this->assertEquals(41, $link['id']); 110 $this->assertEquals(41, $link['id']);
91 $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114651'), $link['created']); 111 $this->assertEquals(DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114651'), $link['created']);
92 $this->assertEquals('http://host.tld/?WDWyig', $link['guid']); 112 $this->assertEquals('http://host.tld/?WDWyig', $link['guid']);
93 $this->assertEquals('http://host.tld/?WDWyig', $link['url']); 113 $this->assertEquals('http://host.tld/?WDWyig', $link['url']);
94 $this->assertRegExp('/Tue, 10 Mar 2015 11:46:51 \+\d{4}/', $link['pub_iso_date']); 114 $this->assertRegExp('/Tue, 10 Mar 2015 11:46:51 \+\d{4}/', $link['pub_iso_date']);
@@ -117,7 +137,14 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase
117 */ 137 */
118 public function testAtomBuildData() 138 public function testAtomBuildData()
119 { 139 {
120 $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, false); 140 $feedBuilder = new FeedBuilder(
141 self::$bookmarkService,
142 self::$formatter,
143 FeedBuilder::$FEED_ATOM,
144 self::$serverInfo,
145 null,
146 false
147 );
121 $feedBuilder->setLocale(self::$LOCALE); 148 $feedBuilder->setLocale(self::$LOCALE);
122 $data = $feedBuilder->buildData(); 149 $data = $feedBuilder->buildData();
123 $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links'])); 150 $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
@@ -136,13 +163,20 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase
136 'searchtags' => 'stuff', 163 'searchtags' => 'stuff',
137 'searchterm' => 'beard', 164 'searchterm' => 'beard',
138 ); 165 );
139 $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, $criteria, false); 166 $feedBuilder = new FeedBuilder(
167 self::$bookmarkService,
168 self::$formatter,
169 FeedBuilder::$FEED_ATOM,
170 self::$serverInfo,
171 $criteria,
172 false
173 );
140 $feedBuilder->setLocale(self::$LOCALE); 174 $feedBuilder->setLocale(self::$LOCALE);
141 $data = $feedBuilder->buildData(); 175 $data = $feedBuilder->buildData();
142 $this->assertEquals(1, count($data['links'])); 176 $this->assertEquals(1, count($data['links']));
143 $link = array_shift($data['links']); 177 $link = array_shift($data['links']);
144 $this->assertEquals(41, $link['id']); 178 $this->assertEquals(41, $link['id']);
145 $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']);
146 } 180 }
147 181
148 /** 182 /**
@@ -153,13 +187,20 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase
153 $criteria = array( 187 $criteria = array(
154 'nb' => '3', 188 'nb' => '3',
155 ); 189 );
156 $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, $criteria, false); 190 $feedBuilder = new FeedBuilder(
191 self::$bookmarkService,
192 self::$formatter,
193 FeedBuilder::$FEED_ATOM,
194 self::$serverInfo,
195 $criteria,
196 false
197 );
157 $feedBuilder->setLocale(self::$LOCALE); 198 $feedBuilder->setLocale(self::$LOCALE);
158 $data = $feedBuilder->buildData(); 199 $data = $feedBuilder->buildData();
159 $this->assertEquals(3, count($data['links'])); 200 $this->assertEquals(3, count($data['links']));
160 $link = $data['links'][array_keys($data['links'])[2]]; 201 $link = $data['links'][array_keys($data['links'])[2]];
161 $this->assertEquals(41, $link['id']); 202 $this->assertEquals(41, $link['id']);
162 $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114651'), $link['created']); 203 $this->assertEquals(DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114651'), $link['created']);
163 } 204 }
164 205
165 /** 206 /**
@@ -167,7 +208,14 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase
167 */ 208 */
168 public function testBuildDataPermalinks() 209 public function testBuildDataPermalinks()
169 { 210 {
170 $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, false); 211 $feedBuilder = new FeedBuilder(
212 self::$bookmarkService,
213 self::$formatter,
214 FeedBuilder::$FEED_ATOM,
215 self::$serverInfo,
216 null,
217 false
218 );
171 $feedBuilder->setLocale(self::$LOCALE); 219 $feedBuilder->setLocale(self::$LOCALE);
172 $feedBuilder->setUsePermalinks(true); 220 $feedBuilder->setUsePermalinks(true);
173 $data = $feedBuilder->buildData(); 221 $data = $feedBuilder->buildData();
@@ -176,7 +224,7 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase
176 // First link is a permalink 224 // First link is a permalink
177 $link = $data['links'][array_keys($data['links'])[2]]; 225 $link = $data['links'][array_keys($data['links'])[2]];
178 $this->assertEquals(41, $link['id']); 226 $this->assertEquals(41, $link['id']);
179 $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114651'), $link['created']); 227 $this->assertEquals(DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114651'), $link['created']);
180 $this->assertEquals('http://host.tld/?WDWyig', $link['guid']); 228 $this->assertEquals('http://host.tld/?WDWyig', $link['guid']);
181 $this->assertEquals('http://host.tld/?WDWyig', $link['url']); 229 $this->assertEquals('http://host.tld/?WDWyig', $link['url']);
182 $this->assertContains('Direct link', $link['description']); 230 $this->assertContains('Direct link', $link['description']);
@@ -184,7 +232,7 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase
184 // Second link is a direct link 232 // Second link is a direct link
185 $link = $data['links'][array_keys($data['links'])[3]]; 233 $link = $data['links'][array_keys($data['links'])[3]];
186 $this->assertEquals(8, $link['id']); 234 $this->assertEquals(8, $link['id']);
187 $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114633'), $link['created']); 235 $this->assertEquals(DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114633'), $link['created']);
188 $this->assertEquals('http://host.tld/?RttfEw', $link['guid']); 236 $this->assertEquals('http://host.tld/?RttfEw', $link['guid']);
189 $this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['url']); 237 $this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['url']);
190 $this->assertContains('Direct link', $link['description']); 238 $this->assertContains('Direct link', $link['description']);
@@ -196,7 +244,14 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase
196 */ 244 */
197 public function testBuildDataHideDates() 245 public function testBuildDataHideDates()
198 { 246 {
199 $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, false); 247 $feedBuilder = new FeedBuilder(
248 self::$bookmarkService,
249 self::$formatter,
250 FeedBuilder::$FEED_ATOM,
251 self::$serverInfo,
252 null,
253 false
254 );
200 $feedBuilder->setLocale(self::$LOCALE); 255 $feedBuilder->setLocale(self::$LOCALE);
201 $feedBuilder->setHideDates(true); 256 $feedBuilder->setHideDates(true);
202 $data = $feedBuilder->buildData(); 257 $data = $feedBuilder->buildData();
@@ -204,7 +259,14 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase
204 $this->assertFalse($data['show_dates']); 259 $this->assertFalse($data['show_dates']);
205 260
206 // Show dates while logged in 261 // Show dates while logged in
207 $feedBuilder = new FeedBuilder(self::$linkDB, FeedBuilder::$FEED_ATOM, self::$serverInfo, null, true); 262 $feedBuilder = new FeedBuilder(
263 self::$bookmarkService,
264 self::$formatter,
265 FeedBuilder::$FEED_ATOM,
266 self::$serverInfo,
267 null,
268 true
269 );
208 $feedBuilder->setLocale(self::$LOCALE); 270 $feedBuilder->setLocale(self::$LOCALE);
209 $feedBuilder->setHideDates(true); 271 $feedBuilder->setHideDates(true);
210 $data = $feedBuilder->buildData(); 272 $data = $feedBuilder->buildData();
@@ -225,7 +287,8 @@ class FeedBuilderTest extends \PHPUnit\Framework\TestCase
225 'REQUEST_URI' => '/~user/shaarli/index.php?do=feed', 287 'REQUEST_URI' => '/~user/shaarli/index.php?do=feed',
226 ); 288 );
227 $feedBuilder = new FeedBuilder( 289 $feedBuilder = new FeedBuilder(
228 self::$linkDB, 290 self::$bookmarkService,
291 self::$formatter,
229 FeedBuilder::$FEED_ATOM, 292 FeedBuilder::$FEED_ATOM,
230 $serverInfo, 293 $serverInfo,
231 null, 294 null,
diff --git a/tests/formatter/BookmarkDefaultFormatterTest.php b/tests/formatter/BookmarkDefaultFormatterTest.php
new file mode 100644
index 00000000..fe42a208
--- /dev/null
+++ b/tests/formatter/BookmarkDefaultFormatterTest.php
@@ -0,0 +1,156 @@
1<?php
2
3namespace Shaarli\Formatter;
4
5use DateTime;
6use PHPUnit\Framework\TestCase;
7use Shaarli\Bookmark\Bookmark;
8use Shaarli\Config\ConfigManager;
9
10/**
11 * Class BookmarkDefaultFormatterTest
12 * @package Shaarli\Formatter
13 */
14class BookmarkDefaultFormatterTest extends TestCase
15{
16 /** @var string Path of test config file */
17 protected static $testConf = 'sandbox/config';
18
19 /** @var BookmarkFormatter */
20 protected $formatter;
21
22 /** @var ConfigManager instance */
23 protected $conf;
24
25 /**
26 * Initialize formatter instance.
27 */
28 public function setUp()
29 {
30 copy('tests/utils/config/configJson.json.php', self::$testConf .'.json.php');
31 $this->conf = new ConfigManager(self::$testConf);
32 $this->formatter = new BookmarkDefaultFormatter($this->conf);
33 }
34
35 /**
36 * Test formatting a bookmark with all its attribute filled.
37 */
38 public function testFormatFull()
39 {
40 $bookmark = new Bookmark();
41 $bookmark->setId($id = 11);
42 $bookmark->setShortUrl($short = 'abcdef');
43 $bookmark->setUrl('https://sub.domain.tld?query=here&for=real#hash');
44 $bookmark->setTitle($title = 'This is a <strong>bookmark</strong>');
45 $bookmark->setDescription($desc = '<h2>Content</h2><p>`Here is some content</p>');
46 $bookmark->setTags($tags = ['tag1', 'bookmark', 'other', '<script>alert("xss");</script>']);
47 $bookmark->setThumbnail('http://domain2.tdl2/?type=img&name=file.png');
48 $bookmark->setSticky(true);
49 $bookmark->setCreated($created = DateTime::createFromFormat('Ymd_His', '20190521_190412'));
50 $bookmark->setUpdated($updated = DateTime::createFromFormat('Ymd_His', '20190521_191213'));
51 $bookmark->setPrivate(true);
52
53 $link = $this->formatter->format($bookmark);
54 $this->assertEquals($id, $link['id']);
55 $this->assertEquals($short, $link['shorturl']);
56 $this->assertEquals('https://sub.domain.tld?query=here&amp;for=real#hash', $link['url']);
57 $this->assertEquals(
58 'https://sub.domain.tld?query=here&amp;for=real#hash',
59 $link['real_url']
60 );
61 $this->assertEquals('This is a &lt;strong&gt;bookmark&lt;/strong&gt;', $link['title']);
62 $this->assertEquals(
63 '&lt;h2&gt;Content&lt;/h2&gt;&lt;p&gt;`Here is some content&lt;/p&gt;',
64 $link['description']
65 );
66 $tags[3] = '&lt;script&gt;alert(&quot;xss&quot;);&lt;/script&gt;';
67 $this->assertEquals($tags, $link['taglist']);
68 $this->assertEquals(implode(' ', $tags), $link['tags']);
69 $this->assertEquals(
70 'http://domain2.tdl2/?type=img&amp;name=file.png',
71 $link['thumbnail']
72 );
73 $this->assertEquals($created, $link['created']);
74 $this->assertEquals($created->getTimestamp(), $link['timestamp']);
75 $this->assertEquals($updated, $link['updated']);
76 $this->assertEquals($updated->getTimestamp(), $link['updated_timestamp']);
77 $this->assertTrue($link['private']);
78 $this->assertTrue($link['sticky']);
79 $this->assertEquals('private', $link['class']);
80 }
81
82 /**
83 * Test formatting a bookmark with all its attribute filled.
84 */
85 public function testFormatMinimal()
86 {
87 $bookmark = new Bookmark();
88
89 $link = $this->formatter->format($bookmark);
90 $this->assertEmpty($link['id']);
91 $this->assertEmpty($link['shorturl']);
92 $this->assertEmpty($link['url']);
93 $this->assertEmpty($link['real_url']);
94 $this->assertEmpty($link['title']);
95 $this->assertEmpty($link['description']);
96 $this->assertEmpty($link['taglist']);
97 $this->assertEmpty($link['tags']);
98 $this->assertEmpty($link['thumbnail']);
99 $this->assertEmpty($link['created']);
100 $this->assertEmpty($link['timestamp']);
101 $this->assertEmpty($link['updated']);
102 $this->assertEmpty($link['updated_timestamp']);
103 $this->assertFalse($link['private']);
104 $this->assertFalse($link['sticky']);
105 $this->assertEmpty($link['class']);
106 }
107
108 /**
109 * Make sure that the description is properly formatted by the default formatter.
110 */
111 public function testFormatDescription()
112 {
113 $description = [];
114 $description[] = 'This a <strong>description</strong>' . PHP_EOL;
115 $description[] = 'text https://sub.domain.tld?query=here&for=real#hash more text'. PHP_EOL;
116 $description[] = 'Also, there is an #hashtag added'. PHP_EOL;
117 $description[] = ' A N D KEEP SPACES ! '. PHP_EOL;
118
119 $bookmark = new Bookmark();
120 $bookmark->setDescription(implode('', $description));
121 $link = $this->formatter->format($bookmark);
122
123 $description[0] = 'This a &lt;strong&gt;description&lt;/strong&gt;<br />';
124 $url = 'https://sub.domain.tld?query=here&amp;for=real#hash';
125 $description[1] = 'text <a href="'. $url .'">'. $url .'</a> more text<br />';
126 $description[2] = 'Also, there is an <a href="?addtag=hashtag" '.
127 'title="Hashtag hashtag">#hashtag</a> added<br />';
128 $description[3] = '&nbsp; &nbsp; A &nbsp;N &nbsp;D KEEP &nbsp; &nbsp; '.
129 'SPACES &nbsp; &nbsp;! &nbsp; <br />';
130
131 $this->assertEquals(implode(PHP_EOL, $description) . PHP_EOL, $link['description']);
132 }
133
134 /**
135 * Test formatting URL with an index_url set
136 * It should prepend relative links.
137 */
138 public function testFormatNoteWithIndexUrl()
139 {
140 $bookmark = new Bookmark();
141 $bookmark->setUrl($short = '?abcdef');
142 $description = 'Text #hashtag more text';
143 $bookmark->setDescription($description);
144
145 $this->formatter->addContextData('index_url', $root = 'https://domain.tld/hithere/');
146
147 $link = $this->formatter->format($bookmark);
148 $this->assertEquals($root . $short, $link['url']);
149 $this->assertEquals($root . $short, $link['real_url']);
150 $this->assertEquals(
151 'Text <a href="'. $root .'?addtag=hashtag" title="Hashtag hashtag">'.
152 '#hashtag</a> more text',
153 $link['description']
154 );
155 }
156}
diff --git a/tests/formatter/BookmarkMarkdownFormatterTest.php b/tests/formatter/BookmarkMarkdownFormatterTest.php
new file mode 100644
index 00000000..0ca7f802
--- /dev/null
+++ b/tests/formatter/BookmarkMarkdownFormatterTest.php
@@ -0,0 +1,160 @@
1<?php
2
3namespace Shaarli\Formatter;
4
5use DateTime;
6use PHPUnit\Framework\TestCase;
7use Shaarli\Bookmark\Bookmark;
8use Shaarli\Config\ConfigManager;
9
10/**
11 * Class BookmarkMarkdownFormatterTest
12 * @package Shaarli\Formatter
13 */
14class BookmarkMarkdownFormatterTest extends TestCase
15{
16 /** @var string Path of test config file */
17 protected static $testConf = 'sandbox/config';
18
19 /** @var BookmarkFormatter */
20 protected $formatter;
21
22 /** @var ConfigManager instance */
23 protected $conf;
24
25 /**
26 * Initialize formatter instance.
27 */
28 public function setUp()
29 {
30 copy('tests/utils/config/configJson.json.php', self::$testConf .'.json.php');
31 $this->conf = new ConfigManager(self::$testConf);
32 $this->formatter = new BookmarkMarkdownFormatter($this->conf);
33 }
34
35 /**
36 * Test formatting a bookmark with all its attribute filled.
37 */
38 public function testFormatFull()
39 {
40 $bookmark = new Bookmark();
41 $bookmark->setId($id = 11);
42 $bookmark->setShortUrl($short = 'abcdef');
43 $bookmark->setUrl('https://sub.domain.tld?query=here&for=real#hash');
44 $bookmark->setTitle($title = 'This is a <strong>bookmark</strong>');
45 $bookmark->setDescription('<h2>Content</h2><p>`Here is some content</p>');
46 $bookmark->setTags($tags = ['tag1', 'bookmark', 'other', '<script>alert("xss");</script>']);
47 $bookmark->setThumbnail('http://domain2.tdl2/?type=img&name=file.png');
48 $bookmark->setSticky(true);
49 $bookmark->setCreated($created = DateTime::createFromFormat('Ymd_His', '20190521_190412'));
50 $bookmark->setUpdated($updated = DateTime::createFromFormat('Ymd_His', '20190521_191213'));
51 $bookmark->setPrivate(true);
52
53 $link = $this->formatter->format($bookmark);
54 $this->assertEquals($id, $link['id']);
55 $this->assertEquals($short, $link['shorturl']);
56 $this->assertEquals('https://sub.domain.tld?query=here&amp;for=real#hash', $link['url']);
57 $this->assertEquals(
58 'https://sub.domain.tld?query=here&amp;for=real#hash',
59 $link['real_url']
60 );
61 $this->assertEquals('This is a &lt;strong&gt;bookmark&lt;/strong&gt;', $link['title']);
62 $this->assertEquals(
63 '<div class="markdown"><p>'.
64 '&lt;h2&gt;Content&lt;/h2&gt;&lt;p&gt;`Here is some content&lt;/p&gt;'.
65 '</p></div>',
66 $link['description']
67 );
68 $tags[3] = '&lt;script&gt;alert(&quot;xss&quot;);&lt;/script&gt;';
69 $this->assertEquals($tags, $link['taglist']);
70 $this->assertEquals(implode(' ', $tags), $link['tags']);
71 $this->assertEquals(
72 'http://domain2.tdl2/?type=img&amp;name=file.png',
73 $link['thumbnail']
74 );
75 $this->assertEquals($created, $link['created']);
76 $this->assertEquals($created->getTimestamp(), $link['timestamp']);
77 $this->assertEquals($updated, $link['updated']);
78 $this->assertEquals($updated->getTimestamp(), $link['updated_timestamp']);
79 $this->assertTrue($link['private']);
80 $this->assertTrue($link['sticky']);
81 $this->assertEquals('private', $link['class']);
82 }
83
84 /**
85 * Test formatting a bookmark with all its attribute filled.
86 */
87 public function testFormatMinimal()
88 {
89 $bookmark = new Bookmark();
90
91 $link = $this->formatter->format($bookmark);
92 $this->assertEmpty($link['id']);
93 $this->assertEmpty($link['shorturl']);
94 $this->assertEmpty($link['url']);
95 $this->assertEmpty($link['real_url']);
96 $this->assertEmpty($link['title']);
97 $this->assertEmpty($link['description']);
98 $this->assertEmpty($link['taglist']);
99 $this->assertEmpty($link['tags']);
100 $this->assertEmpty($link['thumbnail']);
101 $this->assertEmpty($link['created']);
102 $this->assertEmpty($link['timestamp']);
103 $this->assertEmpty($link['updated']);
104 $this->assertEmpty($link['updated_timestamp']);
105 $this->assertFalse($link['private']);
106 $this->assertFalse($link['sticky']);
107 $this->assertEmpty($link['class']);
108 }
109
110 /**
111 * Make sure that the description is properly formatted by the default formatter.
112 */
113 public function testFormatDescription()
114 {
115 $description = 'This a <strong>description</strong>'. PHP_EOL;
116 $description .= 'text https://sub.domain.tld?query=here&for=real#hash more text'. PHP_EOL;
117 $description .= 'Also, there is an #hashtag added'. PHP_EOL;
118 $description .= ' A N D KEEP SPACES ! '. PHP_EOL;
119
120 $bookmark = new Bookmark();
121 $bookmark->setDescription($description);
122 $link = $this->formatter->format($bookmark);
123
124 $description = '<div class="markdown"><p>';
125 $description .= 'This a &lt;strong&gt;description&lt;/strong&gt;<br />'. PHP_EOL;
126 $url = 'https://sub.domain.tld?query=here&amp;for=real#hash';
127 $description .= 'text <a href="'. $url .'">'. $url .'</a> more text<br />'. PHP_EOL;
128 $description .= 'Also, there is an <a href="?addtag=hashtag">#hashtag</a> added<br />'. PHP_EOL;
129 $description .= 'A N D KEEP SPACES ! ';
130 $description .= '</p></div>';
131
132 $this->assertEquals($description, $link['description']);
133 }
134
135 /**
136 * Test formatting URL with an index_url set
137 * It should prepend relative links.
138 */
139 public function testFormatNoteWithIndexUrl()
140 {
141 $bookmark = new Bookmark();
142 $bookmark->setUrl($short = '?abcdef');
143 $description = 'Text #hashtag more text';
144 $bookmark->setDescription($description);
145
146 $this->formatter->addContextData('index_url', $root = 'https://domain.tld/hithere/');
147
148 $description = '<div class="markdown"><p>';
149 $description .= 'Text <a href="'. $root .'?addtag=hashtag">#hashtag</a> more text';
150 $description .= '</p></div>';
151
152 $link = $this->formatter->format($bookmark);
153 $this->assertEquals($root . $short, $link['url']);
154 $this->assertEquals($root . $short, $link['real_url']);
155 $this->assertEquals(
156 $description,
157 $link['description']
158 );
159 }
160}
diff --git a/tests/formatter/BookmarkRawFormatterTest.php b/tests/formatter/BookmarkRawFormatterTest.php
new file mode 100644
index 00000000..ceb6fb73
--- /dev/null
+++ b/tests/formatter/BookmarkRawFormatterTest.php
@@ -0,0 +1,97 @@
1<?php
2
3namespace Shaarli\Formatter;
4
5use DateTime;
6use PHPUnit\Framework\TestCase;
7use Shaarli\Bookmark\Bookmark;
8use Shaarli\Config\ConfigManager;
9
10/**
11 * Class BookmarkRawFormatterTest
12 * @package Shaarli\Formatter
13 */
14class BookmarkRawFormatterTest extends TestCase
15{
16 /** @var string Path of test config file */
17 protected static $testConf = 'sandbox/config';
18
19 /** @var BookmarkFormatter */
20 protected $formatter;
21
22 /** @var ConfigManager instance */
23 protected $conf;
24
25 /**
26 * Initialize formatter instance.
27 */
28 public function setUp()
29 {
30 copy('tests/utils/config/configJson.json.php', self::$testConf .'.json.php');
31 $this->conf = new ConfigManager(self::$testConf);
32 $this->formatter = new BookmarkRawFormatter($this->conf);
33 }
34
35 /**
36 * Test formatting a bookmark with all its attribute filled.
37 */
38 public function testFormatFull()
39 {
40 $bookmark = new Bookmark();
41 $bookmark->setId($id = 11);
42 $bookmark->setShortUrl($short = 'abcdef');
43 $bookmark->setUrl($url = 'https://sub.domain.tld?query=here&for=real#hash');
44 $bookmark->setTitle($title = 'This is a <strong>bookmark</strong>');
45 $bookmark->setDescription($desc = '<h2>Content</h2><p>`Here is some content</p>');
46 $bookmark->setTags($tags = ['tag1', 'bookmark', 'other', '<script>alert("xss");</script>']);
47 $bookmark->setThumbnail($thumb = 'http://domain2.tdl2/file.png');
48 $bookmark->setSticky(true);
49 $bookmark->setCreated($created = DateTime::createFromFormat('Ymd_His', '20190521_190412'));
50 $bookmark->setUpdated($updated = DateTime::createFromFormat('Ymd_His', '20190521_191213'));
51 $bookmark->setPrivate(true);
52
53 $link = $this->formatter->format($bookmark);
54 $this->assertEquals($id, $link['id']);
55 $this->assertEquals($short, $link['shorturl']);
56 $this->assertEquals($url, $link['url']);
57 $this->assertEquals($url, $link['real_url']);
58 $this->assertEquals($title, $link['title']);
59 $this->assertEquals($desc, $link['description']);
60 $this->assertEquals($tags, $link['taglist']);
61 $this->assertEquals(implode(' ', $tags), $link['tags']);
62 $this->assertEquals($thumb, $link['thumbnail']);
63 $this->assertEquals($created, $link['created']);
64 $this->assertEquals($created->getTimestamp(), $link['timestamp']);
65 $this->assertEquals($updated, $link['updated']);
66 $this->assertEquals($updated->getTimestamp(), $link['updated_timestamp']);
67 $this->assertTrue($link['private']);
68 $this->assertTrue($link['sticky']);
69 $this->assertEquals('private', $link['class']);
70 }
71
72 /**
73 * Test formatting a bookmark with all its attribute filled.
74 */
75 public function testFormatMinimal()
76 {
77 $bookmark = new Bookmark();
78
79 $link = $this->formatter->format($bookmark);
80 $this->assertEmpty($link['id']);
81 $this->assertEmpty($link['shorturl']);
82 $this->assertEmpty($link['url']);
83 $this->assertEmpty($link['real_url']);
84 $this->assertEmpty($link['title']);
85 $this->assertEmpty($link['description']);
86 $this->assertEmpty($link['taglist']);
87 $this->assertEmpty($link['tags']);
88 $this->assertEmpty($link['thumbnail']);
89 $this->assertEmpty($link['created']);
90 $this->assertEmpty($link['timestamp']);
91 $this->assertEmpty($link['updated']);
92 $this->assertEmpty($link['updated_timestamp']);
93 $this->assertFalse($link['private']);
94 $this->assertFalse($link['sticky']);
95 $this->assertEmpty($link['class']);
96 }
97}
diff --git a/tests/formatter/FormatterFactoryTest.php b/tests/formatter/FormatterFactoryTest.php
new file mode 100644
index 00000000..317c0b2d
--- /dev/null
+++ b/tests/formatter/FormatterFactoryTest.php
@@ -0,0 +1,101 @@
1<?php
2
3namespace Shaarli\Formatter;
4
5use PHPUnit\Framework\TestCase;
6use Shaarli\Config\ConfigManager;
7
8/**
9 * Class FormatterFactoryTest
10 *
11 * @package Shaarli\Formatter
12 */
13class FormatterFactoryTest extends TestCase
14{
15 /** @var string Path of test config file */
16 protected static $testConf = 'sandbox/config';
17
18 /** @var FormatterFactory instance */
19 protected $factory;
20
21 /** @var ConfigManager instance */
22 protected $conf;
23
24 /**
25 * Initialize FormatterFactory instance
26 */
27 public function setUp()
28 {
29 copy('tests/utils/config/configJson.json.php', self::$testConf .'.json.php');
30 $this->conf = new ConfigManager(self::$testConf);
31 $this->factory = new FormatterFactory($this->conf);
32 }
33
34 /**
35 * Test creating an instance of BookmarkFormatter without any setting -> default formatter
36 */
37 public function testCreateInstanceDefault()
38 {
39 $this->assertInstanceOf(BookmarkDefaultFormatter::class, $this->factory->getFormatter());
40 }
41
42 /**
43 * Test creating an instance of BookmarkDefaultFormatter from settings
44 */
45 public function testCreateInstanceDefaultSetting()
46 {
47 $this->conf->set('formatter', 'default');
48 $this->assertInstanceOf(BookmarkDefaultFormatter::class, $this->factory->getFormatter());
49 }
50
51 /**
52 * Test creating an instance of BookmarkDefaultFormatter from parameter
53 */
54 public function testCreateInstanceDefaultParameter()
55 {
56 $this->assertInstanceOf(
57 BookmarkDefaultFormatter::class,
58 $this->factory->getFormatter('default')
59 );
60 }
61
62 /**
63 * Test creating an instance of BookmarkRawFormatter from settings
64 */
65 public function testCreateInstanceRawSetting()
66 {
67 $this->conf->set('formatter', 'raw');
68 $this->assertInstanceOf(BookmarkRawFormatter::class, $this->factory->getFormatter());
69 }
70
71 /**
72 * Test creating an instance of BookmarkRawFormatter from parameter
73 */
74 public function testCreateInstanceRawParameter()
75 {
76 $this->assertInstanceOf(
77 BookmarkRawFormatter::class,
78 $this->factory->getFormatter('raw')
79 );
80 }
81
82 /**
83 * Test creating an instance of BookmarkMarkdownFormatter from settings
84 */
85 public function testCreateInstanceMarkdownSetting()
86 {
87 $this->conf->set('formatter', 'markdown');
88 $this->assertInstanceOf(BookmarkMarkdownFormatter::class, $this->factory->getFormatter());
89 }
90
91 /**
92 * Test creating an instance of BookmarkMarkdownFormatter from parameter
93 */
94 public function testCreateInstanceMarkdownParameter()
95 {
96 $this->assertInstanceOf(
97 BookmarkMarkdownFormatter::class,
98 $this->factory->getFormatter('markdown')
99 );
100 }
101}
diff --git a/tests/legacy/LegacyDummyUpdater.php b/tests/legacy/LegacyDummyUpdater.php
new file mode 100644
index 00000000..10e0a5b7
--- /dev/null
+++ b/tests/legacy/LegacyDummyUpdater.php
@@ -0,0 +1,74 @@
1<?php
2namespace Shaarli\Updater;
3
4use Exception;
5use ReflectionClass;
6use ReflectionMethod;
7use Shaarli\Config\ConfigManager;
8use Shaarli\Legacy\LegacyLinkDB;
9use Shaarli\Legacy\LegacyUpdater;
10
11/**
12 * Class LegacyDummyUpdater.
13 * Extends updater to add update method designed for unit tests.
14 */
15class LegacyDummyUpdater extends LegacyUpdater
16{
17 /**
18 * Object constructor.
19 *
20 * @param array $doneUpdates Updates which are already done.
21 * @param LegacyLinkDB $linkDB LinkDB instance.
22 * @param ConfigManager $conf Configuration Manager instance.
23 * @param boolean $isLoggedIn True if the user is logged in.
24 */
25 public function __construct($doneUpdates, $linkDB, $conf, $isLoggedIn)
26 {
27 parent::__construct($doneUpdates, $linkDB, $conf, $isLoggedIn);
28
29 // Retrieve all update methods.
30 // For unit test, only retrieve final methods,
31 $class = new ReflectionClass($this);
32 $this->methods = $class->getMethods(ReflectionMethod::IS_FINAL);
33 }
34
35 /**
36 * Update method 1.
37 *
38 * @return bool true.
39 */
40 final private function updateMethodDummy1()
41 {
42 return true;
43 }
44
45 /**
46 * Update method 2.
47 *
48 * @return bool true.
49 */
50 final private function updateMethodDummy2()
51 {
52 return true;
53 }
54
55 /**
56 * Update method 3.
57 *
58 * @return bool true.
59 */
60 final private function updateMethodDummy3()
61 {
62 return true;
63 }
64
65 /**
66 * Update method 4, raise an exception.
67 *
68 * @throws Exception error.
69 */
70 final private function updateMethodException()
71 {
72 throw new Exception('whatever');
73 }
74}
diff --git a/tests/bookmark/LinkDBTest.php b/tests/legacy/LegacyLinkDBTest.php
index ffe03cc5..17b2b0e6 100644
--- a/tests/bookmark/LinkDBTest.php
+++ b/tests/legacy/LegacyLinkDBTest.php
@@ -3,12 +3,13 @@
3 * Link datastore tests 3 * Link datastore tests
4 */ 4 */
5 5
6namespace Shaarli\Bookmark; 6namespace Shaarli\Legacy;
7 7
8use DateTime; 8use DateTime;
9use ReferenceLinkDB; 9use ReferenceLinkDB;
10use ReflectionClass; 10use ReflectionClass;
11use Shaarli; 11use Shaarli;
12use Shaarli\Bookmark\Bookmark;
12 13
13require_once 'application/feed/Cache.php'; 14require_once 'application/feed/Cache.php';
14require_once 'application/Utils.php'; 15require_once 'application/Utils.php';
@@ -16,9 +17,9 @@ require_once 'tests/utils/ReferenceLinkDB.php';
16 17
17 18
18/** 19/**
19 * Unitary tests for LinkDB 20 * Unitary tests for LegacyLinkDBTest
20 */ 21 */
21class LinkDBTest extends \PHPUnit\Framework\TestCase 22class LegacyLinkDBTest extends \PHPUnit\Framework\TestCase
22{ 23{
23 // datastore to test write operations 24 // datastore to test write operations
24 protected static $testDatastore = 'sandbox/datastore.php'; 25 protected static $testDatastore = 'sandbox/datastore.php';
@@ -29,19 +30,19 @@ class LinkDBTest extends \PHPUnit\Framework\TestCase
29 protected static $refDB = null; 30 protected static $refDB = null;
30 31
31 /** 32 /**
32 * @var LinkDB public LinkDB instance. 33 * @var LegacyLinkDB public LinkDB instance.
33 */ 34 */
34 protected static $publicLinkDB = null; 35 protected static $publicLinkDB = null;
35 36
36 /** 37 /**
37 * @var LinkDB private LinkDB instance. 38 * @var LegacyLinkDB private LinkDB instance.
38 */ 39 */
39 protected static $privateLinkDB = null; 40 protected static $privateLinkDB = null;
40 41
41 /** 42 /**
42 * Instantiates public and private LinkDBs with test data 43 * Instantiates public and private LinkDBs with test data
43 * 44 *
44 * The reference datastore contains public and private links that 45 * The reference datastore contains public and private bookmarks that
45 * will be used to test LinkDB's methods: 46 * will be used to test LinkDB's methods:
46 * - access filtering (public/private), 47 * - access filtering (public/private),
47 * - link searches: 48 * - link searches:
@@ -58,11 +59,10 @@ class LinkDBTest extends \PHPUnit\Framework\TestCase
58 unlink(self::$testDatastore); 59 unlink(self::$testDatastore);
59 } 60 }
60 61
61 self::$refDB = new ReferenceLinkDB(); 62 self::$refDB = new ReferenceLinkDB(true);
62 self::$refDB->write(self::$testDatastore); 63 self::$refDB->write(self::$testDatastore);
63 64 self::$publicLinkDB = new LegacyLinkDB(self::$testDatastore, false, false);
64 self::$publicLinkDB = new LinkDB(self::$testDatastore, false, false); 65 self::$privateLinkDB = new LegacyLinkDB(self::$testDatastore, true, false);
65 self::$privateLinkDB = new LinkDB(self::$testDatastore, true, false);
66 } 66 }
67 67
68 /** 68 /**
@@ -74,7 +74,7 @@ class LinkDBTest extends \PHPUnit\Framework\TestCase
74 */ 74 */
75 protected static function getMethod($name) 75 protected static function getMethod($name)
76 { 76 {
77 $class = new ReflectionClass('Shaarli\Bookmark\LinkDB'); 77 $class = new ReflectionClass('Shaarli\Legacy\LegacyLinkDB');
78 $method = $class->getMethod($name); 78 $method = $class->getMethod($name);
79 $method->setAccessible(true); 79 $method->setAccessible(true);
80 return $method; 80 return $method;
@@ -85,7 +85,7 @@ class LinkDBTest extends \PHPUnit\Framework\TestCase
85 */ 85 */
86 public function testConstructLoggedIn() 86 public function testConstructLoggedIn()
87 { 87 {
88 new LinkDB(self::$testDatastore, true, false); 88 new LegacyLinkDB(self::$testDatastore, true, false);
89 $this->assertFileExists(self::$testDatastore); 89 $this->assertFileExists(self::$testDatastore);
90 } 90 }
91 91
@@ -94,7 +94,7 @@ class LinkDBTest extends \PHPUnit\Framework\TestCase
94 */ 94 */
95 public function testConstructLoggedOut() 95 public function testConstructLoggedOut()
96 { 96 {
97 new LinkDB(self::$testDatastore, false, false); 97 new LegacyLinkDB(self::$testDatastore, false, false);
98 $this->assertFileExists(self::$testDatastore); 98 $this->assertFileExists(self::$testDatastore);
99 } 99 }
100 100
@@ -106,7 +106,7 @@ class LinkDBTest extends \PHPUnit\Framework\TestCase
106 */ 106 */
107 public function testConstructDatastoreNotWriteable() 107 public function testConstructDatastoreNotWriteable()
108 { 108 {
109 new LinkDB('null/store.db', false, false); 109 new LegacyLinkDB('null/store.db', false, false);
110 } 110 }
111 111
112 /** 112 /**
@@ -114,7 +114,7 @@ class LinkDBTest extends \PHPUnit\Framework\TestCase
114 */ 114 */
115 public function testCheckDBNew() 115 public function testCheckDBNew()
116 { 116 {
117 $linkDB = new LinkDB(self::$testDatastore, false, false); 117 $linkDB = new LegacyLinkDB(self::$testDatastore, false, false);
118 unlink(self::$testDatastore); 118 unlink(self::$testDatastore);
119 $this->assertFileNotExists(self::$testDatastore); 119 $this->assertFileNotExists(self::$testDatastore);
120 120
@@ -131,7 +131,7 @@ class LinkDBTest extends \PHPUnit\Framework\TestCase
131 */ 131 */
132 public function testCheckDBLoad() 132 public function testCheckDBLoad()
133 { 133 {
134 $linkDB = new LinkDB(self::$testDatastore, false, false); 134 $linkDB = new LegacyLinkDB(self::$testDatastore, false, false);
135 $datastoreSize = filesize(self::$testDatastore); 135 $datastoreSize = filesize(self::$testDatastore);
136 $this->assertGreaterThan(0, $datastoreSize); 136 $this->assertGreaterThan(0, $datastoreSize);
137 137
@@ -151,13 +151,13 @@ class LinkDBTest extends \PHPUnit\Framework\TestCase
151 public function testReadEmptyDB() 151 public function testReadEmptyDB()
152 { 152 {
153 file_put_contents(self::$testDatastore, '<?php /* S7QysKquBQA= */ ?>'); 153 file_put_contents(self::$testDatastore, '<?php /* S7QysKquBQA= */ ?>');
154 $emptyDB = new LinkDB(self::$testDatastore, false, false); 154 $emptyDB = new LegacyLinkDB(self::$testDatastore, false, false);
155 $this->assertEquals(0, sizeof($emptyDB)); 155 $this->assertEquals(0, sizeof($emptyDB));
156 $this->assertEquals(0, count($emptyDB)); 156 $this->assertEquals(0, count($emptyDB));
157 } 157 }
158 158
159 /** 159 /**
160 * Load public links from the DB 160 * Load public bookmarks from the DB
161 */ 161 */
162 public function testReadPublicDB() 162 public function testReadPublicDB()
163 { 163 {
@@ -168,7 +168,7 @@ class LinkDBTest extends \PHPUnit\Framework\TestCase
168 } 168 }
169 169
170 /** 170 /**
171 * Load public and private links from the DB 171 * Load public and private bookmarks from the DB
172 */ 172 */
173 public function testReadPrivateDB() 173 public function testReadPrivateDB()
174 { 174 {
@@ -179,11 +179,11 @@ class LinkDBTest extends \PHPUnit\Framework\TestCase
179 } 179 }
180 180
181 /** 181 /**
182 * Save the links to the DB 182 * Save the bookmarks to the DB
183 */ 183 */
184 public function testSave() 184 public function testSave()
185 { 185 {
186 $testDB = new LinkDB(self::$testDatastore, true, false); 186 $testDB = new LegacyLinkDB(self::$testDatastore, true, false);
187 $dbSize = sizeof($testDB); 187 $dbSize = sizeof($testDB);
188 188
189 $link = array( 189 $link = array(
@@ -192,18 +192,18 @@ class LinkDBTest extends \PHPUnit\Framework\TestCase
192 'url' => 'http://dum.my', 192 'url' => 'http://dum.my',
193 'description' => 'One more', 193 'description' => 'One more',
194 'private' => 0, 194 'private' => 0,
195 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150518_190000'), 195 'created' => DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150518_190000'),
196 'tags' => 'unit test' 196 'tags' => 'unit test'
197 ); 197 );
198 $testDB[$link['id']] = $link; 198 $testDB[$link['id']] = $link;
199 $testDB->save('tests'); 199 $testDB->save('tests');
200 200
201 $testDB = new LinkDB(self::$testDatastore, true, false); 201 $testDB = new LegacyLinkDB(self::$testDatastore, true, false);
202 $this->assertEquals($dbSize + 1, sizeof($testDB)); 202 $this->assertEquals($dbSize + 1, sizeof($testDB));
203 } 203 }
204 204
205 /** 205 /**
206 * Count existing links 206 * Count existing bookmarks
207 */ 207 */
208 public function testCount() 208 public function testCount()
209 { 209 {
@@ -218,11 +218,11 @@ class LinkDBTest extends \PHPUnit\Framework\TestCase
218 } 218 }
219 219
220 /** 220 /**
221 * Count existing links - public links hidden 221 * Count existing bookmarks - public bookmarks hidden
222 */ 222 */
223 public function testCountHiddenPublic() 223 public function testCountHiddenPublic()
224 { 224 {
225 $linkDB = new LinkDB(self::$testDatastore, false, true); 225 $linkDB = new LegacyLinkDB(self::$testDatastore, false, true);
226 226
227 $this->assertEquals( 227 $this->assertEquals(
228 0, 228 0,
@@ -235,7 +235,7 @@ class LinkDBTest extends \PHPUnit\Framework\TestCase
235 } 235 }
236 236
237 /** 237 /**
238 * List the days for which links have been posted 238 * List the days for which bookmarks have been posted
239 */ 239 */
240 public function testDays() 240 public function testDays()
241 { 241 {
@@ -422,7 +422,7 @@ class LinkDBTest extends \PHPUnit\Framework\TestCase
422 /** 422 /**
423 * Test filterHash() with an invalid smallhash. 423 * Test filterHash() with an invalid smallhash.
424 * 424 *
425 * @expectedException \Shaarli\Bookmark\Exception\LinkNotFoundException 425 * @expectedException \Shaarli\Bookmark\Exception\BookmarkNotFoundException
426 */ 426 */
427 public function testFilterHashInValid1() 427 public function testFilterHashInValid1()
428 { 428 {
@@ -433,7 +433,7 @@ class LinkDBTest extends \PHPUnit\Framework\TestCase
433 /** 433 /**
434 * Test filterHash() with an empty smallhash. 434 * Test filterHash() with an empty smallhash.
435 * 435 *
436 * @expectedException \Shaarli\Bookmark\Exception\LinkNotFoundException 436 * @expectedException \Shaarli\Bookmark\Exception\BookmarkNotFoundException
437 */ 437 */
438 public function testFilterHashInValid() 438 public function testFilterHashInValid()
439 { 439 {
@@ -462,12 +462,12 @@ class LinkDBTest extends \PHPUnit\Framework\TestCase
462 } 462 }
463 463
464 /** 464 /**
465 * Test rename tag with a valid value present in multiple links 465 * Test rename tag with a valid value present in multiple bookmarks
466 */ 466 */
467 public function testRenameTagMultiple() 467 public function testRenameTagMultiple()
468 { 468 {
469 self::$refDB->write(self::$testDatastore); 469 self::$refDB->write(self::$testDatastore);
470 $linkDB = new LinkDB(self::$testDatastore, true, false); 470 $linkDB = new LegacyLinkDB(self::$testDatastore, true, false);
471 471
472 $res = $linkDB->renameTag('cartoon', 'Taz'); 472 $res = $linkDB->renameTag('cartoon', 'Taz');
473 $this->assertEquals(3, count($res)); 473 $this->assertEquals(3, count($res));
@@ -482,7 +482,7 @@ class LinkDBTest extends \PHPUnit\Framework\TestCase
482 public function testRenameTagCaseSensitive() 482 public function testRenameTagCaseSensitive()
483 { 483 {
484 self::$refDB->write(self::$testDatastore); 484 self::$refDB->write(self::$testDatastore);
485 $linkDB = new LinkDB(self::$testDatastore, true, false); 485 $linkDB = new LegacyLinkDB(self::$testDatastore, true, false);
486 486
487 $res = $linkDB->renameTag('sTuff', 'Taz'); 487 $res = $linkDB->renameTag('sTuff', 'Taz');
488 $this->assertEquals(1, count($res)); 488 $this->assertEquals(1, count($res));
@@ -494,7 +494,7 @@ class LinkDBTest extends \PHPUnit\Framework\TestCase
494 */ 494 */
495 public function testRenameTagInvalid() 495 public function testRenameTagInvalid()
496 { 496 {
497 $linkDB = new LinkDB(self::$testDatastore, false, false); 497 $linkDB = new LegacyLinkDB(self::$testDatastore, false, false);
498 498
499 $this->assertFalse($linkDB->renameTag('', 'test')); 499 $this->assertFalse($linkDB->renameTag('', 'test'));
500 $this->assertFalse($linkDB->renameTag('', '')); 500 $this->assertFalse($linkDB->renameTag('', ''));
@@ -509,7 +509,7 @@ class LinkDBTest extends \PHPUnit\Framework\TestCase
509 public function testDeleteTag() 509 public function testDeleteTag()
510 { 510 {
511 self::$refDB->write(self::$testDatastore); 511 self::$refDB->write(self::$testDatastore);
512 $linkDB = new LinkDB(self::$testDatastore, true, false); 512 $linkDB = new LegacyLinkDB(self::$testDatastore, true, false);
513 513
514 $res = $linkDB->renameTag('cartoon', null); 514 $res = $linkDB->renameTag('cartoon', null);
515 $this->assertEquals(3, count($res)); 515 $this->assertEquals(3, count($res));
@@ -624,7 +624,7 @@ class LinkDBTest extends \PHPUnit\Framework\TestCase
624 { 624 {
625 $nextId = 43; 625 $nextId = 43;
626 $creation = DateTime::createFromFormat('Ymd_His', '20190807_130444'); 626 $creation = DateTime::createFromFormat('Ymd_His', '20190807_130444');
627 $linkDB = new LinkDB(self::$testDatastore, true, false); 627 $linkDB = new LegacyLinkDB(self::$testDatastore, true, false);
628 for ($i = 0; $i < 4; ++$i) { 628 for ($i = 0; $i < 4; ++$i) {
629 $linkDB[$nextId + $i] = [ 629 $linkDB[$nextId + $i] = [
630 'id' => $nextId + $i, 630 'id' => $nextId + $i,
@@ -639,7 +639,7 @@ class LinkDBTest extends \PHPUnit\Framework\TestCase
639 // Check 4 new links 4 times 639 // Check 4 new links 4 times
640 for ($i = 0; $i < 4; ++$i) { 640 for ($i = 0; $i < 4; ++$i) {
641 $linkDB->save('tests'); 641 $linkDB->save('tests');
642 $linkDB = new LinkDB(self::$testDatastore, true, false); 642 $linkDB = new LegacyLinkDB(self::$testDatastore, true, false);
643 $count = 3; 643 $count = 3;
644 foreach ($linkDB as $link) { 644 foreach ($linkDB as $link) {
645 if ($link['sticky'] === true) { 645 if ($link['sticky'] === true) {
diff --git a/tests/bookmark/LinkFilterTest.php b/tests/legacy/LegacyLinkFilterTest.php
index 808f8122..ba9ec529 100644
--- a/tests/bookmark/LinkFilterTest.php
+++ b/tests/legacy/LegacyLinkFilterTest.php
@@ -4,18 +4,20 @@ namespace Shaarli\Bookmark;
4 4
5use Exception; 5use Exception;
6use ReferenceLinkDB; 6use ReferenceLinkDB;
7use Shaarli\Legacy\LegacyLinkDB;
8use Shaarli\Legacy\LegacyLinkFilter;
7 9
8/** 10/**
9 * Class LinkFilterTest. 11 * Class LegacyLinkFilterTest.
10 */ 12 */
11class LinkFilterTest extends \PHPUnit\Framework\TestCase 13class LegacyLinkFilterTest extends \PHPUnit\Framework\TestCase
12{ 14{
13 /** 15 /**
14 * @var string Test datastore path. 16 * @var string Test datastore path.
15 */ 17 */
16 protected static $testDatastore = 'sandbox/datastore.php'; 18 protected static $testDatastore = 'sandbox/datastore.php';
17 /** 19 /**
18 * @var LinkFilter instance. 20 * @var BookmarkFilter instance.
19 */ 21 */
20 protected static $linkFilter; 22 protected static $linkFilter;
21 23
@@ -25,7 +27,7 @@ class LinkFilterTest extends \PHPUnit\Framework\TestCase
25 protected static $refDB; 27 protected static $refDB;
26 28
27 /** 29 /**
28 * @var LinkDB instance 30 * @var LegacyLinkDB instance
29 */ 31 */
30 protected static $linkDB; 32 protected static $linkDB;
31 33
@@ -34,10 +36,10 @@ class LinkFilterTest extends \PHPUnit\Framework\TestCase
34 */ 36 */
35 public static function setUpBeforeClass() 37 public static function setUpBeforeClass()
36 { 38 {
37 self::$refDB = new ReferenceLinkDB(); 39 self::$refDB = new ReferenceLinkDB(true);
38 self::$refDB->write(self::$testDatastore); 40 self::$refDB->write(self::$testDatastore);
39 self::$linkDB = new LinkDB(self::$testDatastore, true, false); 41 self::$linkDB = new LegacyLinkDB(self::$testDatastore, true, false);
40 self::$linkFilter = new LinkFilter(self::$linkDB); 42 self::$linkFilter = new LegacyLinkFilter(self::$linkDB);
41 } 43 }
42 44
43 /** 45 /**
@@ -74,14 +76,14 @@ class LinkFilterTest extends \PHPUnit\Framework\TestCase
74 76
75 $this->assertEquals( 77 $this->assertEquals(
76 ReferenceLinkDB::$NB_LINKS_TOTAL, 78 ReferenceLinkDB::$NB_LINKS_TOTAL,
77 count(self::$linkFilter->filter(LinkFilter::$FILTER_TAG, '')) 79 count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TAG, ''))
78 ); 80 );
79 81
80 $this->assertEquals( 82 $this->assertEquals(
81 self::$refDB->countUntaggedLinks(), 83 self::$refDB->countUntaggedLinks(),
82 count( 84 count(
83 self::$linkFilter->filter( 85 self::$linkFilter->filter(
84 LinkFilter::$FILTER_TAG, 86 LegacyLinkFilter::$FILTER_TAG,
85 /*$request=*/ 87 /*$request=*/
86 '', 88 '',
87 /*$casesensitive=*/ 89 /*$casesensitive=*/
@@ -96,89 +98,89 @@ class LinkFilterTest extends \PHPUnit\Framework\TestCase
96 98
97 $this->assertEquals( 99 $this->assertEquals(
98 ReferenceLinkDB::$NB_LINKS_TOTAL, 100 ReferenceLinkDB::$NB_LINKS_TOTAL,
99 count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, '')) 101 count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, ''))
100 ); 102 );
101 } 103 }
102 104
103 /** 105 /**
104 * Filter links using a tag 106 * Filter bookmarks using a tag
105 */ 107 */
106 public function testFilterOneTag() 108 public function testFilterOneTag()
107 { 109 {
108 $this->assertEquals( 110 $this->assertEquals(
109 4, 111 4,
110 count(self::$linkFilter->filter(LinkFilter::$FILTER_TAG, 'web', false)) 112 count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TAG, 'web', false))
111 ); 113 );
112 114
113 $this->assertEquals( 115 $this->assertEquals(
114 4, 116 4,
115 count(self::$linkFilter->filter(LinkFilter::$FILTER_TAG, 'web', false, 'all')) 117 count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TAG, 'web', false, 'all'))
116 ); 118 );
117 119
118 $this->assertEquals( 120 $this->assertEquals(
119 4, 121 4,
120 count(self::$linkFilter->filter(LinkFilter::$FILTER_TAG, 'web', false, 'default-blabla')) 122 count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TAG, 'web', false, 'default-blabla'))
121 ); 123 );
122 124
123 // Private only. 125 // Private only.
124 $this->assertEquals( 126 $this->assertEquals(
125 1, 127 1,
126 count(self::$linkFilter->filter(LinkFilter::$FILTER_TAG, 'web', false, 'private')) 128 count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TAG, 'web', false, 'private'))
127 ); 129 );
128 130
129 // Public only. 131 // Public only.
130 $this->assertEquals( 132 $this->assertEquals(
131 3, 133 3,
132 count(self::$linkFilter->filter(LinkFilter::$FILTER_TAG, 'web', false, 'public')) 134 count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TAG, 'web', false, 'public'))
133 ); 135 );
134 } 136 }
135 137
136 /** 138 /**
137 * Filter links using a tag - case-sensitive 139 * Filter bookmarks using a tag - case-sensitive
138 */ 140 */
139 public function testFilterCaseSensitiveTag() 141 public function testFilterCaseSensitiveTag()
140 { 142 {
141 $this->assertEquals( 143 $this->assertEquals(
142 0, 144 0,
143 count(self::$linkFilter->filter(LinkFilter::$FILTER_TAG, 'mercurial', true)) 145 count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TAG, 'mercurial', true))
144 ); 146 );
145 147
146 $this->assertEquals( 148 $this->assertEquals(
147 1, 149 1,
148 count(self::$linkFilter->filter(LinkFilter::$FILTER_TAG, 'Mercurial', true)) 150 count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TAG, 'Mercurial', true))
149 ); 151 );
150 } 152 }
151 153
152 /** 154 /**
153 * Filter links using a tag combination 155 * Filter bookmarks using a tag combination
154 */ 156 */
155 public function testFilterMultipleTags() 157 public function testFilterMultipleTags()
156 { 158 {
157 $this->assertEquals( 159 $this->assertEquals(
158 2, 160 2,
159 count(self::$linkFilter->filter(LinkFilter::$FILTER_TAG, 'dev cartoon', false)) 161 count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TAG, 'dev cartoon', false))
160 ); 162 );
161 } 163 }
162 164
163 /** 165 /**
164 * Filter links using a non-existent tag 166 * Filter bookmarks using a non-existent tag
165 */ 167 */
166 public function testFilterUnknownTag() 168 public function testFilterUnknownTag()
167 { 169 {
168 $this->assertEquals( 170 $this->assertEquals(
169 0, 171 0,
170 count(self::$linkFilter->filter(LinkFilter::$FILTER_TAG, 'null', false)) 172 count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TAG, 'null', false))
171 ); 173 );
172 } 174 }
173 175
174 /** 176 /**
175 * Return links for a given day 177 * Return bookmarks for a given day
176 */ 178 */
177 public function testFilterDay() 179 public function testFilterDay()
178 { 180 {
179 $this->assertEquals( 181 $this->assertEquals(
180 4, 182 4,
181 count(self::$linkFilter->filter(LinkFilter::$FILTER_DAY, '20121206')) 183 count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_DAY, '20121206'))
182 ); 184 );
183 } 185 }
184 186
@@ -189,7 +191,7 @@ class LinkFilterTest extends \PHPUnit\Framework\TestCase
189 { 191 {
190 $this->assertEquals( 192 $this->assertEquals(
191 0, 193 0,
192 count(self::$linkFilter->filter(LinkFilter::$FILTER_DAY, '19700101')) 194 count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_DAY, '19700101'))
193 ); 195 );
194 } 196 }
195 197
@@ -200,7 +202,7 @@ class LinkFilterTest extends \PHPUnit\Framework\TestCase
200 */ 202 */
201 public function testFilterInvalidDayWithChars() 203 public function testFilterInvalidDayWithChars()
202 { 204 {
203 self::$linkFilter->filter(LinkFilter::$FILTER_DAY, 'Rainy day, dream away'); 205 self::$linkFilter->filter(LegacyLinkFilter::$FILTER_DAY, 'Rainy day, dream away');
204 } 206 }
205 207
206 /** 208 /**
@@ -210,7 +212,7 @@ class LinkFilterTest extends \PHPUnit\Framework\TestCase
210 */ 212 */
211 public function testFilterInvalidDayDigits() 213 public function testFilterInvalidDayDigits()
212 { 214 {
213 self::$linkFilter->filter(LinkFilter::$FILTER_DAY, '20'); 215 self::$linkFilter->filter(LegacyLinkFilter::$FILTER_DAY, '20');
214 } 216 }
215 217
216 /** 218 /**
@@ -218,7 +220,7 @@ class LinkFilterTest extends \PHPUnit\Framework\TestCase
218 */ 220 */
219 public function testFilterSmallHash() 221 public function testFilterSmallHash()
220 { 222 {
221 $links = self::$linkFilter->filter(LinkFilter::$FILTER_HASH, 'IuWvgA'); 223 $links = self::$linkFilter->filter(LegacyLinkFilter::$FILTER_HASH, 'IuWvgA');
222 224
223 $this->assertEquals( 225 $this->assertEquals(
224 1, 226 1,
@@ -234,11 +236,11 @@ class LinkFilterTest extends \PHPUnit\Framework\TestCase
234 /** 236 /**
235 * No link for this hash 237 * No link for this hash
236 * 238 *
237 * @expectedException \Shaarli\Bookmark\Exception\LinkNotFoundException 239 * @expectedException \Shaarli\Bookmark\Exception\BookmarkNotFoundException
238 */ 240 */
239 public function testFilterUnknownSmallHash() 241 public function testFilterUnknownSmallHash()
240 { 242 {
241 self::$linkFilter->filter(LinkFilter::$FILTER_HASH, 'Iblaah'); 243 self::$linkFilter->filter(LegacyLinkFilter::$FILTER_HASH, 'Iblaah');
242 } 244 }
243 245
244 /** 246 /**
@@ -248,7 +250,7 @@ class LinkFilterTest extends \PHPUnit\Framework\TestCase
248 { 250 {
249 $this->assertEquals( 251 $this->assertEquals(
250 0, 252 0,
251 count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'azertyuiop')) 253 count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'azertyuiop'))
252 ); 254 );
253 } 255 }
254 256
@@ -259,12 +261,12 @@ class LinkFilterTest extends \PHPUnit\Framework\TestCase
259 { 261 {
260 $this->assertEquals( 262 $this->assertEquals(
261 2, 263 2,
262 count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'ars.userfriendly.org')) 264 count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'ars.userfriendly.org'))
263 ); 265 );
264 266
265 $this->assertEquals( 267 $this->assertEquals(
266 2, 268 2,
267 count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'ars org')) 269 count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'ars org'))
268 ); 270 );
269 } 271 }
270 272
@@ -276,21 +278,21 @@ class LinkFilterTest extends \PHPUnit\Framework\TestCase
276 // use miscellaneous cases 278 // use miscellaneous cases
277 $this->assertEquals( 279 $this->assertEquals(
278 2, 280 2,
279 count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'userfriendly -')) 281 count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'userfriendly -'))
280 ); 282 );
281 $this->assertEquals( 283 $this->assertEquals(
282 2, 284 2,
283 count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'UserFriendly -')) 285 count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'UserFriendly -'))
284 ); 286 );
285 $this->assertEquals( 287 $this->assertEquals(
286 2, 288 2,
287 count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'uSeRFrIendlY -')) 289 count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'uSeRFrIendlY -'))
288 ); 290 );
289 291
290 // use miscellaneous case and offset 292 // use miscellaneous case and offset
291 $this->assertEquals( 293 $this->assertEquals(
292 2, 294 2,
293 count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'RFrIendL')) 295 count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'RFrIendL'))
294 ); 296 );
295 } 297 }
296 298
@@ -301,17 +303,17 @@ class LinkFilterTest extends \PHPUnit\Framework\TestCase
301 { 303 {
302 $this->assertEquals( 304 $this->assertEquals(
303 1, 305 1,
304 count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'publishing media')) 306 count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'publishing media'))
305 ); 307 );
306 308
307 $this->assertEquals( 309 $this->assertEquals(
308 1, 310 1,
309 count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'mercurial w3c')) 311 count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'mercurial w3c'))
310 ); 312 );
311 313
312 $this->assertEquals( 314 $this->assertEquals(
313 3, 315 3,
314 count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, '"free software"')) 316 count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, '"free software"'))
315 ); 317 );
316 } 318 }
317 319
@@ -322,29 +324,29 @@ class LinkFilterTest extends \PHPUnit\Framework\TestCase
322 { 324 {
323 $this->assertEquals( 325 $this->assertEquals(
324 6, 326 6,
325 count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'web')) 327 count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'web'))
326 ); 328 );
327 329
328 $this->assertEquals( 330 $this->assertEquals(
329 6, 331 6,
330 count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'web', 'all')) 332 count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'web', 'all'))
331 ); 333 );
332 334
333 $this->assertEquals( 335 $this->assertEquals(
334 6, 336 6,
335 count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'web', 'bla')) 337 count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'web', 'bla'))
336 ); 338 );
337 339
338 // Private only. 340 // Private only.
339 $this->assertEquals( 341 $this->assertEquals(
340 1, 342 1,
341 count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'web', false, 'private')) 343 count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'web', false, 'private'))
342 ); 344 );
343 345
344 // Public only. 346 // Public only.
345 $this->assertEquals( 347 $this->assertEquals(
346 5, 348 5,
347 count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'web', false, 'public')) 349 count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'web', false, 'public'))
348 ); 350 );
349 } 351 }
350 352
@@ -355,7 +357,7 @@ class LinkFilterTest extends \PHPUnit\Framework\TestCase
355 { 357 {
356 $this->assertEquals( 358 $this->assertEquals(
357 3, 359 3,
358 count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'free software')) 360 count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'free software'))
359 ); 361 );
360 } 362 }
361 363
@@ -366,12 +368,12 @@ class LinkFilterTest extends \PHPUnit\Framework\TestCase
366 { 368 {
367 $this->assertEquals( 369 $this->assertEquals(
368 1, 370 1,
369 count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'free -gnu')) 371 count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'free -gnu'))
370 ); 372 );
371 373
372 $this->assertEquals( 374 $this->assertEquals(
373 ReferenceLinkDB::$NB_LINKS_TOTAL - 1, 375 ReferenceLinkDB::$NB_LINKS_TOTAL - 1,
374 count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, '-revolution')) 376 count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, '-revolution'))
375 ); 377 );
376 } 378 }
377 379
@@ -383,7 +385,7 @@ class LinkFilterTest extends \PHPUnit\Framework\TestCase
383 $this->assertEquals( 385 $this->assertEquals(
384 2, 386 2,
385 count(self::$linkFilter->filter( 387 count(self::$linkFilter->filter(
386 LinkFilter::$FILTER_TEXT, 388 LegacyLinkFilter::$FILTER_TEXT,
387 '"Free Software " stallman "read this" @website stuff' 389 '"Free Software " stallman "read this" @website stuff'
388 )) 390 ))
389 ); 391 );
@@ -391,7 +393,7 @@ class LinkFilterTest extends \PHPUnit\Framework\TestCase
391 $this->assertEquals( 393 $this->assertEquals(
392 1, 394 1,
393 count(self::$linkFilter->filter( 395 count(self::$linkFilter->filter(
394 LinkFilter::$FILTER_TEXT, 396 LegacyLinkFilter::$FILTER_TEXT,
395 '"free software " stallman "read this" -beard @website stuff' 397 '"free software " stallman "read this" -beard @website stuff'
396 )) 398 ))
397 ); 399 );
@@ -405,7 +407,7 @@ class LinkFilterTest extends \PHPUnit\Framework\TestCase
405 $this->assertEquals( 407 $this->assertEquals(
406 0, 408 0,
407 count(self::$linkFilter->filter( 409 count(self::$linkFilter->filter(
408 LinkFilter::$FILTER_TEXT, 410 LegacyLinkFilter::$FILTER_TEXT,
409 '"designer naming"' 411 '"designer naming"'
410 )) 412 ))
411 ); 413 );
@@ -413,7 +415,7 @@ class LinkFilterTest extends \PHPUnit\Framework\TestCase
413 $this->assertEquals( 415 $this->assertEquals(
414 0, 416 0,
415 count(self::$linkFilter->filter( 417 count(self::$linkFilter->filter(
416 LinkFilter::$FILTER_TEXT, 418 LegacyLinkFilter::$FILTER_TEXT,
417 '"designernaming"' 419 '"designernaming"'
418 )) 420 ))
419 ); 421 );
@@ -426,12 +428,12 @@ class LinkFilterTest extends \PHPUnit\Framework\TestCase
426 { 428 {
427 $this->assertEquals( 429 $this->assertEquals(
428 1, 430 1,
429 count(self::$linkFilter->filter(LinkFilter::$FILTER_TAG, 'gnu -free')) 431 count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TAG, 'gnu -free'))
430 ); 432 );
431 433
432 $this->assertEquals( 434 $this->assertEquals(
433 ReferenceLinkDB::$NB_LINKS_TOTAL - 1, 435 ReferenceLinkDB::$NB_LINKS_TOTAL - 1,
434 count(self::$linkFilter->filter(LinkFilter::$FILTER_TAG, '-free')) 436 count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TAG, '-free'))
435 ); 437 );
436 } 438 }
437 439
@@ -445,42 +447,42 @@ class LinkFilterTest extends \PHPUnit\Framework\TestCase
445 $this->assertEquals( 447 $this->assertEquals(
446 1, 448 1,
447 count(self::$linkFilter->filter( 449 count(self::$linkFilter->filter(
448 LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT, 450 LegacyLinkFilter::$FILTER_TAG | LegacyLinkFilter::$FILTER_TEXT,
449 array($tags, $terms) 451 array($tags, $terms)
450 )) 452 ))
451 ); 453 );
452 $this->assertEquals( 454 $this->assertEquals(
453 2, 455 2,
454 count(self::$linkFilter->filter( 456 count(self::$linkFilter->filter(
455 LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT, 457 LegacyLinkFilter::$FILTER_TAG | LegacyLinkFilter::$FILTER_TEXT,
456 array('', $terms) 458 array('', $terms)
457 )) 459 ))
458 ); 460 );
459 $this->assertEquals( 461 $this->assertEquals(
460 1, 462 1,
461 count(self::$linkFilter->filter( 463 count(self::$linkFilter->filter(
462 LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT, 464 LegacyLinkFilter::$FILTER_TAG | LegacyLinkFilter::$FILTER_TEXT,
463 array(false, 'PSR-2') 465 array(false, 'PSR-2')
464 )) 466 ))
465 ); 467 );
466 $this->assertEquals( 468 $this->assertEquals(
467 1, 469 1,
468 count(self::$linkFilter->filter( 470 count(self::$linkFilter->filter(
469 LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT, 471 LegacyLinkFilter::$FILTER_TAG | LegacyLinkFilter::$FILTER_TEXT,
470 array($tags, '') 472 array($tags, '')
471 )) 473 ))
472 ); 474 );
473 $this->assertEquals( 475 $this->assertEquals(
474 ReferenceLinkDB::$NB_LINKS_TOTAL, 476 ReferenceLinkDB::$NB_LINKS_TOTAL,
475 count(self::$linkFilter->filter( 477 count(self::$linkFilter->filter(
476 LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT, 478 LegacyLinkFilter::$FILTER_TAG | LegacyLinkFilter::$FILTER_TEXT,
477 '' 479 ''
478 )) 480 ))
479 ); 481 );
480 } 482 }
481 483
482 /** 484 /**
483 * Filter links by #hashtag. 485 * Filter bookmarks by #hashtag.
484 */ 486 */
485 public function testFilterByHashtag() 487 public function testFilterByHashtag()
486 { 488 {
@@ -488,7 +490,7 @@ class LinkFilterTest extends \PHPUnit\Framework\TestCase
488 $this->assertEquals( 490 $this->assertEquals(
489 3, 491 3,
490 count(self::$linkFilter->filter( 492 count(self::$linkFilter->filter(
491 LinkFilter::$FILTER_TAG, 493 LegacyLinkFilter::$FILTER_TAG,
492 $hashtag 494 $hashtag
493 )) 495 ))
494 ); 496 );
@@ -497,7 +499,7 @@ class LinkFilterTest extends \PHPUnit\Framework\TestCase
497 $this->assertEquals( 499 $this->assertEquals(
498 1, 500 1,
499 count(self::$linkFilter->filter( 501 count(self::$linkFilter->filter(
500 LinkFilter::$FILTER_TAG, 502 LegacyLinkFilter::$FILTER_TAG,
501 $hashtag, 503 $hashtag,
502 false, 504 false,
503 'private' 505 'private'
diff --git a/tests/legacy/LegacyUpdaterTest.php b/tests/legacy/LegacyUpdaterTest.php
new file mode 100644
index 00000000..7c429811
--- /dev/null
+++ b/tests/legacy/LegacyUpdaterTest.php
@@ -0,0 +1,886 @@
1<?php
2namespace Shaarli\Updater;
3
4use DateTime;
5use Exception;
6use Shaarli\Bookmark\Bookmark;
7use Shaarli\Config\ConfigJson;
8use Shaarli\Config\ConfigManager;
9use Shaarli\Config\ConfigPhp;
10use Shaarli\Legacy\LegacyLinkDB;
11use Shaarli\Legacy\LegacyUpdater;
12use Shaarli\Thumbnailer;
13
14require_once 'application/updater/UpdaterUtils.php';
15require_once 'tests/updater/DummyUpdater.php';
16require_once 'tests/utils/ReferenceLinkDB.php';
17require_once 'inc/rain.tpl.class.php';
18
19/**
20 * Class UpdaterTest.
21 * Runs unit tests against the updater class.
22 */
23class LegacyUpdaterTest extends \PHPUnit\Framework\TestCase
24{
25 /**
26 * @var string Path to test datastore.
27 */
28 protected static $testDatastore = 'sandbox/datastore.php';
29
30 /**
31 * @var string Config file path (without extension).
32 */
33 protected static $configFile = 'sandbox/config';
34
35 /**
36 * @var ConfigManager
37 */
38 protected $conf;
39
40 /**
41 * Executed before each test.
42 */
43 public function setUp()
44 {
45 copy('tests/utils/config/configJson.json.php', self::$configFile .'.json.php');
46 $this->conf = new ConfigManager(self::$configFile);
47 }
48
49 /**
50 * Test UpdaterUtils::read_updates_file with an empty/missing file.
51 */
52 public function testReadEmptyUpdatesFile()
53 {
54 $this->assertEquals(array(), UpdaterUtils::read_updates_file(''));
55 $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt';
56 touch($updatesFile);
57 $this->assertEquals(array(), UpdaterUtils::read_updates_file($updatesFile));
58 unlink($updatesFile);
59 }
60
61 /**
62 * Test read/write updates file.
63 */
64 public function testReadWriteUpdatesFile()
65 {
66 $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt';
67 $updatesMethods = array('m1', 'm2', 'm3');
68
69 UpdaterUtils::write_updates_file($updatesFile, $updatesMethods);
70 $readMethods = UpdaterUtils::read_updates_file($updatesFile);
71 $this->assertEquals($readMethods, $updatesMethods);
72
73 // Update
74 $updatesMethods[] = 'm4';
75 UpdaterUtils::write_updates_file($updatesFile, $updatesMethods);
76 $readMethods = UpdaterUtils::read_updates_file($updatesFile);
77 $this->assertEquals($readMethods, $updatesMethods);
78 unlink($updatesFile);
79 }
80
81 /**
82 * Test errors in UpdaterUtils::write_updates_file(): empty updates file.
83 *
84 * @expectedException Exception
85 * @expectedExceptionMessageRegExp /Updates file path is not set(.*)/
86 */
87 public function testWriteEmptyUpdatesFile()
88 {
89 UpdaterUtils::write_updates_file('', array('test'));
90 }
91
92 /**
93 * Test errors in UpdaterUtils::write_updates_file(): not writable updates file.
94 *
95 * @expectedException Exception
96 * @expectedExceptionMessageRegExp /Unable to write(.*)/
97 */
98 public function testWriteUpdatesFileNotWritable()
99 {
100 $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt';
101 touch($updatesFile);
102 chmod($updatesFile, 0444);
103 try {
104 @UpdaterUtils::write_updates_file($updatesFile, array('test'));
105 } catch (Exception $e) {
106 unlink($updatesFile);
107 throw $e;
108 }
109 }
110
111 /**
112 * Test the update() method, with no update to run.
113 * 1. Everything already run.
114 * 2. User is logged out.
115 */
116 public function testNoUpdates()
117 {
118 $updates = array(
119 'updateMethodDummy1',
120 'updateMethodDummy2',
121 'updateMethodDummy3',
122 'updateMethodException',
123 );
124 $updater = new DummyUpdater($updates, array(), $this->conf, true);
125 $this->assertEquals(array(), $updater->update());
126
127 $updater = new DummyUpdater(array(), array(), $this->conf, false);
128 $this->assertEquals(array(), $updater->update());
129 }
130
131 /**
132 * Test the update() method, with all updates to run (except the failing one).
133 */
134 public function testUpdatesFirstTime()
135 {
136 $updates = array('updateMethodException',);
137 $expectedUpdates = array(
138 'updateMethodDummy1',
139 'updateMethodDummy2',
140 'updateMethodDummy3',
141 );
142 $updater = new DummyUpdater($updates, array(), $this->conf, true);
143 $this->assertEquals($expectedUpdates, $updater->update());
144 }
145
146 /**
147 * Test the update() method, only one update to run.
148 */
149 public function testOneUpdate()
150 {
151 $updates = array(
152 'updateMethodDummy1',
153 'updateMethodDummy3',
154 'updateMethodException',
155 );
156 $expectedUpdate = array('updateMethodDummy2');
157
158 $updater = new DummyUpdater($updates, array(), $this->conf, true);
159 $this->assertEquals($expectedUpdate, $updater->update());
160 }
161
162 /**
163 * Test Update failed.
164 *
165 * @expectedException \Exception
166 */
167 public function testUpdateFailed()
168 {
169 $updates = array(
170 'updateMethodDummy1',
171 'updateMethodDummy2',
172 'updateMethodDummy3',
173 );
174
175 $updater = new DummyUpdater($updates, array(), $this->conf, true);
176 $updater->update();
177 }
178
179 /**
180 * Test update mergeDeprecatedConfig:
181 * 1. init a config file.
182 * 2. init a options.php file with update value.
183 * 3. merge.
184 * 4. check updated value in config file.
185 */
186 public function testUpdateMergeDeprecatedConfig()
187 {
188 $this->conf->setConfigFile('tests/utils/config/configPhp');
189 $this->conf->reset();
190
191 $optionsFile = 'tests/updater/options.php';
192 $options = '<?php
193$GLOBALS[\'privateLinkByDefault\'] = true;';
194 file_put_contents($optionsFile, $options);
195
196 // tmp config file.
197 $this->conf->setConfigFile('tests/updater/config');
198
199 // merge configs
200 $updater = new LegacyUpdater(array(), array(), $this->conf, true);
201 // This writes a new config file in tests/updater/config.php
202 $updater->updateMethodMergeDeprecatedConfigFile();
203
204 // make sure updated field is changed
205 $this->conf->reload();
206 $this->assertTrue($this->conf->get('privacy.default_private_links'));
207 $this->assertFalse(is_file($optionsFile));
208 // Delete the generated file.
209 unlink($this->conf->getConfigFileExt());
210 }
211
212 /**
213 * Test mergeDeprecatedConfig in without options file.
214 */
215 public function testMergeDeprecatedConfigNoFile()
216 {
217 $updater = new LegacyUpdater(array(), array(), $this->conf, true);
218 $updater->updateMethodMergeDeprecatedConfigFile();
219
220 $this->assertEquals('root', $this->conf->get('credentials.login'));
221 }
222
223 /**
224 * Test renameDashTags update method.
225 */
226 public function testRenameDashTags()
227 {
228 $refDB = new \ReferenceLinkDB(true);
229 $refDB->write(self::$testDatastore);
230 $linkDB = new LegacyLinkDB(self::$testDatastore, true, false);
231
232 $this->assertEmpty($linkDB->filterSearch(array('searchtags' => 'exclude')));
233 $updater = new LegacyUpdater(array(), $linkDB, $this->conf, true);
234 $updater->updateMethodRenameDashTags();
235 $this->assertNotEmpty($linkDB->filterSearch(array('searchtags' => 'exclude')));
236 }
237
238 /**
239 * Convert old PHP config file to JSON config.
240 */
241 public function testConfigToJson()
242 {
243 $configFile = 'tests/utils/config/configPhp';
244 $this->conf->setConfigFile($configFile);
245 $this->conf->reset();
246
247 // The ConfigIO is initialized with ConfigPhp.
248 $this->assertTrue($this->conf->getConfigIO() instanceof ConfigPhp);
249
250 $updater = new LegacyUpdater(array(), array(), $this->conf, false);
251 $done = $updater->updateMethodConfigToJson();
252 $this->assertTrue($done);
253
254 // The ConfigIO has been updated to ConfigJson.
255 $this->assertTrue($this->conf->getConfigIO() instanceof ConfigJson);
256 $this->assertTrue(file_exists($this->conf->getConfigFileExt()));
257
258 // Check JSON config data.
259 $this->conf->reload();
260 $this->assertEquals('root', $this->conf->get('credentials.login'));
261 $this->assertEquals('lala', $this->conf->get('redirector.url'));
262 $this->assertEquals('data/datastore.php', $this->conf->get('resource.datastore'));
263 $this->assertEquals('1', $this->conf->get('plugins.WALLABAG_VERSION'));
264
265 rename($configFile . '.save.php', $configFile . '.php');
266 unlink($this->conf->getConfigFileExt());
267 }
268
269 /**
270 * Launch config conversion update with an existing JSON file => nothing to do.
271 */
272 public function testConfigToJsonNothingToDo()
273 {
274 $filetime = filemtime($this->conf->getConfigFileExt());
275 $updater = new LegacyUpdater(array(), array(), $this->conf, false);
276 $done = $updater->updateMethodConfigToJson();
277 $this->assertTrue($done);
278 $expected = filemtime($this->conf->getConfigFileExt());
279 $this->assertEquals($expected, $filetime);
280 }
281
282 /**
283 * Test escapeUnescapedConfig with valid data.
284 */
285 public function testEscapeConfig()
286 {
287 $sandbox = 'sandbox/config';
288 copy(self::$configFile . '.json.php', $sandbox . '.json.php');
289 $this->conf = new ConfigManager($sandbox);
290 $title = '<script>alert("title");</script>';
291 $headerLink = '<script>alert("header_link");</script>';
292 $this->conf->set('general.title', $title);
293 $this->conf->set('general.header_link', $headerLink);
294 $updater = new LegacyUpdater(array(), array(), $this->conf, true);
295 $done = $updater->updateMethodEscapeUnescapedConfig();
296 $this->assertTrue($done);
297 $this->conf->reload();
298 $this->assertEquals(escape($title), $this->conf->get('general.title'));
299 $this->assertEquals(escape($headerLink), $this->conf->get('general.header_link'));
300 unlink($sandbox . '.json.php');
301 }
302
303 /**
304 * Test updateMethodApiSettings(): create default settings for the API (enabled + secret).
305 */
306 public function testUpdateApiSettings()
307 {
308 $confFile = 'sandbox/config';
309 copy(self::$configFile .'.json.php', $confFile .'.json.php');
310 $conf = new ConfigManager($confFile);
311 $updater = new LegacyUpdater(array(), array(), $conf, true);
312
313 $this->assertFalse($conf->exists('api.enabled'));
314 $this->assertFalse($conf->exists('api.secret'));
315 $updater->updateMethodApiSettings();
316 $conf->reload();
317 $this->assertTrue($conf->get('api.enabled'));
318 $this->assertTrue($conf->exists('api.secret'));
319 unlink($confFile .'.json.php');
320 }
321
322 /**
323 * Test updateMethodApiSettings(): already set, do nothing.
324 */
325 public function testUpdateApiSettingsNothingToDo()
326 {
327 $confFile = 'sandbox/config';
328 copy(self::$configFile .'.json.php', $confFile .'.json.php');
329 $conf = new ConfigManager($confFile);
330 $conf->set('api.enabled', false);
331 $conf->set('api.secret', '');
332 $updater = new LegacyUpdater(array(), array(), $conf, true);
333 $updater->updateMethodApiSettings();
334 $this->assertFalse($conf->get('api.enabled'));
335 $this->assertEmpty($conf->get('api.secret'));
336 unlink($confFile .'.json.php');
337 }
338
339 /**
340 * Test updateMethodDatastoreIds().
341 */
342 public function testDatastoreIds()
343 {
344 $links = array(
345 '20121206_182539' => array(
346 'linkdate' => '20121206_182539',
347 'title' => 'Geek and Poke',
348 'url' => 'http://geek-and-poke.com/',
349 'description' => 'desc',
350 'tags' => 'dev cartoon tag1 tag2 tag3 tag4 ',
351 'updated' => '20121206_190301',
352 'private' => false,
353 ),
354 '20121206_172539' => array(
355 'linkdate' => '20121206_172539',
356 'title' => 'UserFriendly - Samba',
357 'url' => 'http://ars.userfriendly.org/cartoons/?id=20010306',
358 'description' => '',
359 'tags' => 'samba cartoon web',
360 'private' => false,
361 ),
362 '20121206_142300' => array(
363 'linkdate' => '20121206_142300',
364 'title' => 'UserFriendly - Web Designer',
365 'url' => 'http://ars.userfriendly.org/cartoons/?id=20121206',
366 'description' => 'Naming conventions... #private',
367 'tags' => 'samba cartoon web',
368 'private' => true,
369 ),
370 );
371 $refDB = new \ReferenceLinkDB(true);
372 $refDB->setLinks($links);
373 $refDB->write(self::$testDatastore);
374 $linkDB = new LegacyLinkDB(self::$testDatastore, true, false);
375
376 $checksum = hash_file('sha1', self::$testDatastore);
377
378 $this->conf->set('resource.data_dir', 'sandbox');
379 $this->conf->set('resource.datastore', self::$testDatastore);
380
381 $updater = new LegacyUpdater(array(), $linkDB, $this->conf, true);
382 $this->assertTrue($updater->updateMethodDatastoreIds());
383
384 $linkDB = new LegacyLinkDB(self::$testDatastore, true, false);
385
386 $backupFiles = glob($this->conf->get('resource.data_dir') . '/datastore.'. date('YmdH') .'*.php');
387 $backup = null;
388 foreach ($backupFiles as $backupFile) {
389 if (strpos($backupFile, '_1') === false) {
390 $backup = $backupFile;
391 }
392 }
393 $this->assertNotNull($backup);
394 $this->assertFileExists($backup);
395 $this->assertEquals($checksum, hash_file('sha1', $backup));
396 unlink($backup);
397
398 $this->assertEquals(3, count($linkDB));
399 $this->assertTrue(isset($linkDB[0]));
400 $this->assertFalse(isset($linkDB[0]['linkdate']));
401 $this->assertEquals(0, $linkDB[0]['id']);
402 $this->assertEquals('UserFriendly - Web Designer', $linkDB[0]['title']);
403 $this->assertEquals('http://ars.userfriendly.org/cartoons/?id=20121206', $linkDB[0]['url']);
404 $this->assertEquals('Naming conventions... #private', $linkDB[0]['description']);
405 $this->assertEquals('samba cartoon web', $linkDB[0]['tags']);
406 $this->assertTrue($linkDB[0]['private']);
407 $this->assertEquals(
408 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20121206_142300'),
409 $linkDB[0]['created']
410 );
411
412 $this->assertTrue(isset($linkDB[1]));
413 $this->assertFalse(isset($linkDB[1]['linkdate']));
414 $this->assertEquals(1, $linkDB[1]['id']);
415 $this->assertEquals('UserFriendly - Samba', $linkDB[1]['title']);
416 $this->assertEquals(
417 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20121206_172539'),
418 $linkDB[1]['created']
419 );
420
421 $this->assertTrue(isset($linkDB[2]));
422 $this->assertFalse(isset($linkDB[2]['linkdate']));
423 $this->assertEquals(2, $linkDB[2]['id']);
424 $this->assertEquals('Geek and Poke', $linkDB[2]['title']);
425 $this->assertEquals(
426 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20121206_182539'),
427 $linkDB[2]['created']
428 );
429 $this->assertEquals(
430 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20121206_190301'),
431 $linkDB[2]['updated']
432 );
433 }
434
435 /**
436 * Test updateMethodDatastoreIds() with the update already applied: nothing to do.
437 */
438 public function testDatastoreIdsNothingToDo()
439 {
440 $refDB = new \ReferenceLinkDB(true);
441 $refDB->write(self::$testDatastore);
442 $linkDB = new LegacyLinkDB(self::$testDatastore, true, false);
443
444 $this->conf->set('resource.data_dir', 'sandbox');
445 $this->conf->set('resource.datastore', self::$testDatastore);
446
447 $checksum = hash_file('sha1', self::$testDatastore);
448 $updater = new LegacyUpdater(array(), $linkDB, $this->conf, true);
449 $this->assertTrue($updater->updateMethodDatastoreIds());
450 $this->assertEquals($checksum, hash_file('sha1', self::$testDatastore));
451 }
452
453 /**
454 * Test defaultTheme update with default settings: nothing to do.
455 */
456 public function testDefaultThemeWithDefaultSettings()
457 {
458 $sandbox = 'sandbox/config';
459 copy(self::$configFile . '.json.php', $sandbox . '.json.php');
460 $this->conf = new ConfigManager($sandbox);
461 $updater = new LegacyUpdater([], [], $this->conf, true);
462 $this->assertTrue($updater->updateMethodDefaultTheme());
463
464 $this->assertEquals('tpl/', $this->conf->get('resource.raintpl_tpl'));
465 $this->assertEquals('default', $this->conf->get('resource.theme'));
466 $this->conf = new ConfigManager($sandbox);
467 $this->assertEquals('tpl/', $this->conf->get('resource.raintpl_tpl'));
468 $this->assertEquals('default', $this->conf->get('resource.theme'));
469 unlink($sandbox . '.json.php');
470 }
471
472 /**
473 * Test defaultTheme update with a custom theme in a subfolder
474 */
475 public function testDefaultThemeWithCustomTheme()
476 {
477 $theme = 'iamanartist';
478 $sandbox = 'sandbox/config';
479 copy(self::$configFile . '.json.php', $sandbox . '.json.php');
480 $this->conf = new ConfigManager($sandbox);
481 mkdir('sandbox/'. $theme);
482 touch('sandbox/'. $theme .'/linklist.html');
483 $this->conf->set('resource.raintpl_tpl', 'sandbox/'. $theme .'/');
484 $updater = new LegacyUpdater([], [], $this->conf, true);
485 $this->assertTrue($updater->updateMethodDefaultTheme());
486
487 $this->assertEquals('sandbox', $this->conf->get('resource.raintpl_tpl'));
488 $this->assertEquals($theme, $this->conf->get('resource.theme'));
489 $this->conf = new ConfigManager($sandbox);
490 $this->assertEquals('sandbox', $this->conf->get('resource.raintpl_tpl'));
491 $this->assertEquals($theme, $this->conf->get('resource.theme'));
492 unlink($sandbox . '.json.php');
493 unlink('sandbox/'. $theme .'/linklist.html');
494 rmdir('sandbox/'. $theme);
495 }
496
497 /**
498 * Test updateMethodEscapeMarkdown with markdown plugin enabled
499 * => setting markdown_escape set to false.
500 */
501 public function testEscapeMarkdownSettingToFalse()
502 {
503 $sandboxConf = 'sandbox/config';
504 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
505 $this->conf = new ConfigManager($sandboxConf);
506
507 $this->conf->set('general.enabled_plugins', ['markdown']);
508 $updater = new LegacyUpdater([], [], $this->conf, true);
509 $this->assertTrue($updater->updateMethodEscapeMarkdown());
510 $this->assertFalse($this->conf->get('security.markdown_escape'));
511
512 // reload from file
513 $this->conf = new ConfigManager($sandboxConf);
514 $this->assertFalse($this->conf->get('security.markdown_escape'));
515 }
516
517
518 /**
519 * Test updateMethodEscapeMarkdown with markdown plugin disabled
520 * => setting markdown_escape set to true.
521 */
522 public function testEscapeMarkdownSettingToTrue()
523 {
524 $sandboxConf = 'sandbox/config';
525 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
526 $this->conf = new ConfigManager($sandboxConf);
527
528 $this->conf->set('general.enabled_plugins', []);
529 $updater = new LegacyUpdater([], [], $this->conf, true);
530 $this->assertTrue($updater->updateMethodEscapeMarkdown());
531 $this->assertTrue($this->conf->get('security.markdown_escape'));
532
533 // reload from file
534 $this->conf = new ConfigManager($sandboxConf);
535 $this->assertTrue($this->conf->get('security.markdown_escape'));
536 }
537
538 /**
539 * Test updateMethodEscapeMarkdown with nothing to do (setting already enabled)
540 */
541 public function testEscapeMarkdownSettingNothingToDoEnabled()
542 {
543 $sandboxConf = 'sandbox/config';
544 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
545 $this->conf = new ConfigManager($sandboxConf);
546 $this->conf->set('security.markdown_escape', true);
547 $updater = new LegacyUpdater([], [], $this->conf, true);
548 $this->assertTrue($updater->updateMethodEscapeMarkdown());
549 $this->assertTrue($this->conf->get('security.markdown_escape'));
550 }
551
552 /**
553 * Test updateMethodEscapeMarkdown with nothing to do (setting already disabled)
554 */
555 public function testEscapeMarkdownSettingNothingToDoDisabled()
556 {
557 $this->conf->set('security.markdown_escape', false);
558 $updater = new LegacyUpdater([], [], $this->conf, true);
559 $this->assertTrue($updater->updateMethodEscapeMarkdown());
560 $this->assertFalse($this->conf->get('security.markdown_escape'));
561 }
562
563 /**
564 * Test updateMethodPiwikUrl with valid data
565 */
566 public function testUpdatePiwikUrlValid()
567 {
568 $sandboxConf = 'sandbox/config';
569 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
570 $this->conf = new ConfigManager($sandboxConf);
571 $url = 'mypiwik.tld';
572 $this->conf->set('plugins.PIWIK_URL', $url);
573 $updater = new LegacyUpdater([], [], $this->conf, true);
574 $this->assertTrue($updater->updateMethodPiwikUrl());
575 $this->assertEquals('http://'. $url, $this->conf->get('plugins.PIWIK_URL'));
576
577 // reload from file
578 $this->conf = new ConfigManager($sandboxConf);
579 $this->assertEquals('http://'. $url, $this->conf->get('plugins.PIWIK_URL'));
580 }
581
582 /**
583 * Test updateMethodPiwikUrl without setting
584 */
585 public function testUpdatePiwikUrlEmpty()
586 {
587 $updater = new LegacyUpdater([], [], $this->conf, true);
588 $this->assertTrue($updater->updateMethodPiwikUrl());
589 $this->assertEmpty($this->conf->get('plugins.PIWIK_URL'));
590 }
591
592 /**
593 * Test updateMethodPiwikUrl: valid URL, nothing to do
594 */
595 public function testUpdatePiwikUrlNothingToDo()
596 {
597 $url = 'https://mypiwik.tld';
598 $this->conf->set('plugins.PIWIK_URL', $url);
599 $updater = new LegacyUpdater([], [], $this->conf, true);
600 $this->assertTrue($updater->updateMethodPiwikUrl());
601 $this->assertEquals($url, $this->conf->get('plugins.PIWIK_URL'));
602 }
603
604 /**
605 * Test updateMethodAtomDefault with show_atom set to false
606 * => update to true.
607 */
608 public function testUpdateMethodAtomDefault()
609 {
610 $sandboxConf = 'sandbox/config';
611 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
612 $this->conf = new ConfigManager($sandboxConf);
613 $this->conf->set('feed.show_atom', false);
614 $updater = new LegacyUpdater([], [], $this->conf, true);
615 $this->assertTrue($updater->updateMethodAtomDefault());
616 $this->assertTrue($this->conf->get('feed.show_atom'));
617 // reload from file
618 $this->conf = new ConfigManager($sandboxConf);
619 $this->assertTrue($this->conf->get('feed.show_atom'));
620 }
621 /**
622 * Test updateMethodAtomDefault with show_atom not set.
623 * => nothing to do
624 */
625 public function testUpdateMethodAtomDefaultNoExist()
626 {
627 $sandboxConf = 'sandbox/config';
628 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
629 $this->conf = new ConfigManager($sandboxConf);
630 $updater = new LegacyUpdater([], [], $this->conf, true);
631 $this->assertTrue($updater->updateMethodAtomDefault());
632 $this->assertTrue($this->conf->get('feed.show_atom'));
633 }
634 /**
635 * Test updateMethodAtomDefault with show_atom set to true.
636 * => nothing to do
637 */
638 public function testUpdateMethodAtomDefaultAlreadyTrue()
639 {
640 $sandboxConf = 'sandbox/config';
641 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
642 $this->conf = new ConfigManager($sandboxConf);
643 $this->conf->set('feed.show_atom', true);
644 $updater = new LegacyUpdater([], [], $this->conf, true);
645 $this->assertTrue($updater->updateMethodAtomDefault());
646 $this->assertTrue($this->conf->get('feed.show_atom'));
647 }
648
649 /**
650 * Test updateMethodDownloadSizeAndTimeoutConf, it should be set if none is already defined.
651 */
652 public function testUpdateMethodDownloadSizeAndTimeoutConf()
653 {
654 $sandboxConf = 'sandbox/config';
655 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
656 $this->conf = new ConfigManager($sandboxConf);
657 $updater = new LegacyUpdater([], [], $this->conf, true);
658 $this->assertTrue($updater->updateMethodDownloadSizeAndTimeoutConf());
659 $this->assertEquals(4194304, $this->conf->get('general.download_max_size'));
660 $this->assertEquals(30, $this->conf->get('general.download_timeout'));
661
662 $this->conf = new ConfigManager($sandboxConf);
663 $this->assertEquals(4194304, $this->conf->get('general.download_max_size'));
664 $this->assertEquals(30, $this->conf->get('general.download_timeout'));
665 }
666
667 /**
668 * Test updateMethodDownloadSizeAndTimeoutConf, it shouldn't be set if it is already defined.
669 */
670 public function testUpdateMethodDownloadSizeAndTimeoutConfIgnore()
671 {
672 $sandboxConf = 'sandbox/config';
673 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
674 $this->conf = new ConfigManager($sandboxConf);
675 $this->conf->set('general.download_max_size', 38);
676 $this->conf->set('general.download_timeout', 70);
677 $updater = new LegacyUpdater([], [], $this->conf, true);
678 $this->assertTrue($updater->updateMethodDownloadSizeAndTimeoutConf());
679 $this->assertEquals(38, $this->conf->get('general.download_max_size'));
680 $this->assertEquals(70, $this->conf->get('general.download_timeout'));
681 }
682
683 /**
684 * Test updateMethodDownloadSizeAndTimeoutConf, only the maz size should be set here.
685 */
686 public function testUpdateMethodDownloadSizeAndTimeoutConfOnlySize()
687 {
688 $sandboxConf = 'sandbox/config';
689 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
690 $this->conf = new ConfigManager($sandboxConf);
691 $this->conf->set('general.download_max_size', 38);
692 $updater = new LegacyUpdater([], [], $this->conf, true);
693 $this->assertTrue($updater->updateMethodDownloadSizeAndTimeoutConf());
694 $this->assertEquals(38, $this->conf->get('general.download_max_size'));
695 $this->assertEquals(30, $this->conf->get('general.download_timeout'));
696 }
697
698 /**
699 * Test updateMethodDownloadSizeAndTimeoutConf, only the time out should be set here.
700 */
701 public function testUpdateMethodDownloadSizeAndTimeoutConfOnlyTimeout()
702 {
703 $sandboxConf = 'sandbox/config';
704 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
705 $this->conf = new ConfigManager($sandboxConf);
706 $this->conf->set('general.download_timeout', 3);
707 $updater = new LegacyUpdater([], [], $this->conf, true);
708 $this->assertTrue($updater->updateMethodDownloadSizeAndTimeoutConf());
709 $this->assertEquals(4194304, $this->conf->get('general.download_max_size'));
710 $this->assertEquals(3, $this->conf->get('general.download_timeout'));
711 }
712
713 /**
714 * Test updateMethodWebThumbnailer with thumbnails enabled.
715 */
716 public function testUpdateMethodWebThumbnailerEnabled()
717 {
718 $this->conf->remove('thumbnails');
719 $this->conf->set('thumbnail.enable_thumbnails', true);
720 $updater = new LegacyUpdater([], [], $this->conf, true, $_SESSION);
721 $this->assertTrue($updater->updateMethodWebThumbnailer());
722 $this->assertFalse($this->conf->exists('thumbnail'));
723 $this->assertEquals(\Shaarli\Thumbnailer::MODE_ALL, $this->conf->get('thumbnails.mode'));
724 $this->assertEquals(125, $this->conf->get('thumbnails.width'));
725 $this->assertEquals(90, $this->conf->get('thumbnails.height'));
726 $this->assertContains('You have enabled or changed thumbnails', $_SESSION['warnings'][0]);
727 }
728
729 /**
730 * Test updateMethodWebThumbnailer with thumbnails disabled.
731 */
732 public function testUpdateMethodWebThumbnailerDisabled()
733 {
734 if (isset($_SESSION['warnings'])) {
735 unset($_SESSION['warnings']);
736 }
737
738 $this->conf->remove('thumbnails');
739 $this->conf->set('thumbnail.enable_thumbnails', false);
740 $updater = new LegacyUpdater([], [], $this->conf, true, $_SESSION);
741 $this->assertTrue($updater->updateMethodWebThumbnailer());
742 $this->assertFalse($this->conf->exists('thumbnail'));
743 $this->assertEquals(Thumbnailer::MODE_NONE, $this->conf->get('thumbnails.mode'));
744 $this->assertEquals(125, $this->conf->get('thumbnails.width'));
745 $this->assertEquals(90, $this->conf->get('thumbnails.height'));
746 $this->assertTrue(empty($_SESSION['warnings']));
747 }
748
749 /**
750 * Test updateMethodWebThumbnailer with thumbnails disabled.
751 */
752 public function testUpdateMethodWebThumbnailerNothingToDo()
753 {
754 if (isset($_SESSION['warnings'])) {
755 unset($_SESSION['warnings']);
756 }
757
758 $updater = new LegacyUpdater([], [], $this->conf, true, $_SESSION);
759 $this->assertTrue($updater->updateMethodWebThumbnailer());
760 $this->assertFalse($this->conf->exists('thumbnail'));
761 $this->assertEquals(Thumbnailer::MODE_COMMON, $this->conf->get('thumbnails.mode'));
762 $this->assertEquals(90, $this->conf->get('thumbnails.width'));
763 $this->assertEquals(53, $this->conf->get('thumbnails.height'));
764 $this->assertTrue(empty($_SESSION['warnings']));
765 }
766
767 /**
768 * Test updateMethodSetSticky().
769 */
770 public function testUpdateStickyValid()
771 {
772 $blank = [
773 'id' => 1,
774 'url' => 'z',
775 'title' => '',
776 'description' => '',
777 'tags' => '',
778 'created' => new DateTime(),
779 ];
780 $links = [
781 1 => ['id' => 1] + $blank,
782 2 => ['id' => 2] + $blank,
783 ];
784 $refDB = new \ReferenceLinkDB(true);
785 $refDB->setLinks($links);
786 $refDB->write(self::$testDatastore);
787 $linkDB = new LegacyLinkDB(self::$testDatastore, true, false);
788
789 $updater = new LegacyUpdater(array(), $linkDB, $this->conf, true);
790 $this->assertTrue($updater->updateMethodSetSticky());
791
792 $linkDB = new LegacyLinkDB(self::$testDatastore, true, false);
793 foreach ($linkDB as $link) {
794 $this->assertFalse($link['sticky']);
795 }
796 }
797
798 /**
799 * Test updateMethodSetSticky().
800 */
801 public function testUpdateStickyNothingToDo()
802 {
803 $blank = [
804 'id' => 1,
805 'url' => 'z',
806 'title' => '',
807 'description' => '',
808 'tags' => '',
809 'created' => new DateTime(),
810 ];
811 $links = [
812 1 => ['id' => 1, 'sticky' => true] + $blank,
813 2 => ['id' => 2] + $blank,
814 ];
815 $refDB = new \ReferenceLinkDB(true);
816 $refDB->setLinks($links);
817 $refDB->write(self::$testDatastore);
818 $linkDB = new LegacyLinkDB(self::$testDatastore, true, false);
819
820 $updater = new LegacyUpdater(array(), $linkDB, $this->conf, true);
821 $this->assertTrue($updater->updateMethodSetSticky());
822
823 $linkDB = new LegacyLinkDB(self::$testDatastore, true, false);
824 $this->assertTrue($linkDB[1]['sticky']);
825 }
826
827 /**
828 * Test updateMethodRemoveRedirector().
829 */
830 public function testUpdateRemoveRedirector()
831 {
832 $sandboxConf = 'sandbox/config';
833 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
834 $this->conf = new ConfigManager($sandboxConf);
835 $updater = new LegacyUpdater([], null, $this->conf, true);
836 $this->assertTrue($updater->updateMethodRemoveRedirector());
837 $this->assertFalse($this->conf->exists('redirector'));
838 $this->conf = new ConfigManager($sandboxConf);
839 $this->assertFalse($this->conf->exists('redirector'));
840 }
841
842 /**
843 * Test updateMethodFormatterSetting()
844 */
845 public function testUpdateMethodFormatterSettingDefault()
846 {
847 $sandboxConf = 'sandbox/config';
848 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
849 $this->conf = new ConfigManager($sandboxConf);
850 $this->conf->set('formatter', 'default');
851 $updater = new LegacyUpdater([], null, $this->conf, true);
852 $enabledPlugins = $this->conf->get('general.enabled_plugins');
853 $this->assertFalse(in_array('markdown', $enabledPlugins));
854 $this->assertTrue($updater->updateMethodFormatterSetting());
855 $this->assertEquals('default', $this->conf->get('formatter'));
856 $this->assertEquals($enabledPlugins, $this->conf->get('general.enabled_plugins'));
857
858 $this->conf = new ConfigManager($sandboxConf);
859 $this->assertEquals('default', $this->conf->get('formatter'));
860 $this->assertEquals($enabledPlugins, $this->conf->get('general.enabled_plugins'));
861 }
862
863 /**
864 * Test updateMethodFormatterSetting()
865 */
866 public function testUpdateMethodFormatterSettingMarkdown()
867 {
868 $sandboxConf = 'sandbox/config';
869 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
870 $this->conf = new ConfigManager($sandboxConf);
871 $this->conf->set('formatter', 'default');
872 $updater = new LegacyUpdater([], null, $this->conf, true);
873 $enabledPlugins = $this->conf->get('general.enabled_plugins');
874 $enabledPlugins[] = 'markdown';
875 $this->conf->set('general.enabled_plugins', $enabledPlugins);
876
877 $this->assertTrue(in_array('markdown', $this->conf->get('general.enabled_plugins')));
878 $this->assertTrue($updater->updateMethodFormatterSetting());
879 $this->assertEquals('markdown', $this->conf->get('formatter'));
880 $this->assertFalse(in_array('markdown', $this->conf->get('general.enabled_plugins')));
881
882 $this->conf = new ConfigManager($sandboxConf);
883 $this->assertEquals('markdown', $this->conf->get('formatter'));
884 $this->assertFalse(in_array('markdown', $this->conf->get('general.enabled_plugins')));
885 }
886}
diff --git a/tests/netscape/BookmarkExportTest.php b/tests/netscape/BookmarkExportTest.php
index 6de9876d..011d19ac 100644
--- a/tests/netscape/BookmarkExportTest.php
+++ b/tests/netscape/BookmarkExportTest.php
@@ -1,7 +1,12 @@
1<?php 1<?php
2namespace Shaarli\Netscape; 2namespace Shaarli\Netscape;
3 3
4use Shaarli\Bookmark\BookmarkFileService;
4use Shaarli\Bookmark\LinkDB; 5use Shaarli\Bookmark\LinkDB;
6use Shaarli\Config\ConfigManager;
7use Shaarli\Formatter\FormatterFactory;
8use Shaarli\Formatter\BookmarkFormatter;
9use Shaarli\History;
5 10
6require_once 'tests/utils/ReferenceLinkDB.php'; 11require_once 'tests/utils/ReferenceLinkDB.php';
7 12
@@ -21,18 +26,28 @@ class BookmarkExportTest extends \PHPUnit\Framework\TestCase
21 protected static $refDb = null; 26 protected static $refDb = null;
22 27
23 /** 28 /**
24 * @var LinkDB private LinkDB instance. 29 * @var BookmarkFileService private instance.
25 */ 30 */
26 protected static $linkDb = null; 31 protected static $bookmarkService = null;
32
33 /**
34 * @var BookmarkFormatter instance
35 */
36 protected static $formatter;
27 37
28 /** 38 /**
29 * Instantiate reference data 39 * Instantiate reference data
30 */ 40 */
31 public static function setUpBeforeClass() 41 public static function setUpBeforeClass()
32 { 42 {
43 $conf = new ConfigManager('tests/utils/config/configJson');
44 $conf->set('resource.datastore', self::$testDatastore);
33 self::$refDb = new \ReferenceLinkDB(); 45 self::$refDb = new \ReferenceLinkDB();
34 self::$refDb->write(self::$testDatastore); 46 self::$refDb->write(self::$testDatastore);
35 self::$linkDb = new LinkDB(self::$testDatastore, true, false); 47 $history = new History('sandbox/history.php');
48 self::$bookmarkService = new BookmarkFileService($conf, $history, true);
49 $factory = new FormatterFactory($conf);
50 self::$formatter = $factory->getFormatter('raw');
36 } 51 }
37 52
38 /** 53 /**
@@ -42,15 +57,27 @@ class BookmarkExportTest extends \PHPUnit\Framework\TestCase
42 */ 57 */
43 public function testFilterAndFormatInvalid() 58 public function testFilterAndFormatInvalid()
44 { 59 {
45 NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'derp', false, ''); 60 NetscapeBookmarkUtils::filterAndFormat(
61 self::$bookmarkService,
62 self::$formatter,
63 'derp',
64 false,
65 ''
66 );
46 } 67 }
47 68
48 /** 69 /**
49 * Prepare all links for export 70 * Prepare all bookmarks for export
50 */ 71 */
51 public function testFilterAndFormatAll() 72 public function testFilterAndFormatAll()
52 { 73 {
53 $links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'all', false, ''); 74 $links = NetscapeBookmarkUtils::filterAndFormat(
75 self::$bookmarkService,
76 self::$formatter,
77 'all',
78 false,
79 ''
80 );
54 $this->assertEquals(self::$refDb->countLinks(), sizeof($links)); 81 $this->assertEquals(self::$refDb->countLinks(), sizeof($links));
55 foreach ($links as $link) { 82 foreach ($links as $link) {
56 $date = $link['created']; 83 $date = $link['created'];
@@ -66,11 +93,17 @@ class BookmarkExportTest extends \PHPUnit\Framework\TestCase
66 } 93 }
67 94
68 /** 95 /**
69 * Prepare private links for export 96 * Prepare private bookmarks for export
70 */ 97 */
71 public function testFilterAndFormatPrivate() 98 public function testFilterAndFormatPrivate()
72 { 99 {
73 $links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'private', false, ''); 100 $links = NetscapeBookmarkUtils::filterAndFormat(
101 self::$bookmarkService,
102 self::$formatter,
103 'private',
104 false,
105 ''
106 );
74 $this->assertEquals(self::$refDb->countPrivateLinks(), sizeof($links)); 107 $this->assertEquals(self::$refDb->countPrivateLinks(), sizeof($links));
75 foreach ($links as $link) { 108 foreach ($links as $link) {
76 $date = $link['created']; 109 $date = $link['created'];
@@ -86,11 +119,17 @@ class BookmarkExportTest extends \PHPUnit\Framework\TestCase
86 } 119 }
87 120
88 /** 121 /**
89 * Prepare public links for export 122 * Prepare public bookmarks for export
90 */ 123 */
91 public function testFilterAndFormatPublic() 124 public function testFilterAndFormatPublic()
92 { 125 {
93 $links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'public', false, ''); 126 $links = NetscapeBookmarkUtils::filterAndFormat(
127 self::$bookmarkService,
128 self::$formatter,
129 'public',
130 false,
131 ''
132 );
94 $this->assertEquals(self::$refDb->countPublicLinks(), sizeof($links)); 133 $this->assertEquals(self::$refDb->countPublicLinks(), sizeof($links));
95 foreach ($links as $link) { 134 foreach ($links as $link) {
96 $date = $link['created']; 135 $date = $link['created'];
@@ -110,7 +149,13 @@ class BookmarkExportTest extends \PHPUnit\Framework\TestCase
110 */ 149 */
111 public function testFilterAndFormatDoNotPrependNoteUrl() 150 public function testFilterAndFormatDoNotPrependNoteUrl()
112 { 151 {
113 $links = NetscapeBookmarkUtils::filterAndFormat(self::$linkDb, 'public', false, ''); 152 $links = NetscapeBookmarkUtils::filterAndFormat(
153 self::$bookmarkService,
154 self::$formatter,
155 'public',
156 false,
157 ''
158 );
114 $this->assertEquals( 159 $this->assertEquals(
115 '?WDWyig', 160 '?WDWyig',
116 $links[2]['url'] 161 $links[2]['url']
@@ -124,7 +169,8 @@ class BookmarkExportTest extends \PHPUnit\Framework\TestCase
124 { 169 {
125 $indexUrl = 'http://localhost:7469/shaarli/'; 170 $indexUrl = 'http://localhost:7469/shaarli/';
126 $links = NetscapeBookmarkUtils::filterAndFormat( 171 $links = NetscapeBookmarkUtils::filterAndFormat(
127 self::$linkDb, 172 self::$bookmarkService,
173 self::$formatter,
128 'public', 174 'public',
129 true, 175 true,
130 $indexUrl 176 $indexUrl
diff --git a/tests/netscape/BookmarkImportTest.php b/tests/netscape/BookmarkImportTest.php
index ccafc161..fef7f6d1 100644
--- a/tests/netscape/BookmarkImportTest.php
+++ b/tests/netscape/BookmarkImportTest.php
@@ -2,6 +2,9 @@
2namespace Shaarli\Netscape; 2namespace Shaarli\Netscape;
3 3
4use DateTime; 4use DateTime;
5use Shaarli\Bookmark\Bookmark;
6use Shaarli\Bookmark\BookmarkFilter;
7use Shaarli\Bookmark\BookmarkFileService;
5use Shaarli\Bookmark\LinkDB; 8use Shaarli\Bookmark\LinkDB;
6use Shaarli\Config\ConfigManager; 9use Shaarli\Config\ConfigManager;
7use Shaarli\History; 10use Shaarli\History;
@@ -41,9 +44,9 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
41 protected static $historyFilePath = 'sandbox/history.php'; 44 protected static $historyFilePath = 'sandbox/history.php';
42 45
43 /** 46 /**
44 * @var LinkDB private LinkDB instance 47 * @var BookmarkFileService private LinkDB instance
45 */ 48 */
46 protected $linkDb = null; 49 protected $bookmarkService = null;
47 50
48 /** 51 /**
49 * @var string Dummy page cache 52 * @var string Dummy page cache
@@ -82,10 +85,12 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
82 } 85 }
83 // start with an empty datastore 86 // start with an empty datastore
84 file_put_contents(self::$testDatastore, '<?php /* S7QysKquBQA= */ ?>'); 87 file_put_contents(self::$testDatastore, '<?php /* S7QysKquBQA= */ ?>');
85 $this->linkDb = new LinkDB(self::$testDatastore, true, false); 88
86 $this->conf = new ConfigManager('tests/utils/config/configJson'); 89 $this->conf = new ConfigManager('tests/utils/config/configJson');
87 $this->conf->set('resource.page_cache', $this->pagecache); 90 $this->conf->set('resource.page_cache', $this->pagecache);
91 $this->conf->set('resource.datastore', self::$testDatastore);
88 $this->history = new History(self::$historyFilePath); 92 $this->history = new History(self::$historyFilePath);
93 $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, true);
89 } 94 }
90 95
91 /** 96 /**
@@ -112,7 +117,7 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
112 .' Nothing was imported.', 117 .' Nothing was imported.',
113 NetscapeBookmarkUtils::import(null, $files, null, $this->conf, $this->history) 118 NetscapeBookmarkUtils::import(null, $files, null, $this->conf, $this->history)
114 ); 119 );
115 $this->assertEquals(0, count($this->linkDb)); 120 $this->assertEquals(0, $this->bookmarkService->count());
116 } 121 }
117 122
118 /** 123 /**
@@ -125,7 +130,7 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
125 'File no_doctype.htm (350 bytes) has an unknown file format. Nothing was imported.', 130 'File no_doctype.htm (350 bytes) has an unknown file format. Nothing was imported.',
126 NetscapeBookmarkUtils::import(null, $files, null, $this->conf, $this->history) 131 NetscapeBookmarkUtils::import(null, $files, null, $this->conf, $this->history)
127 ); 132 );
128 $this->assertEquals(0, count($this->linkDb)); 133 $this->assertEquals(0, $this->bookmarkService->count());
129 } 134 }
130 135
131 /** 136 /**
@@ -136,10 +141,10 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
136 $files = file2array('lowercase_doctype.htm'); 141 $files = file2array('lowercase_doctype.htm');
137 $this->assertStringMatchesFormat( 142 $this->assertStringMatchesFormat(
138 'File lowercase_doctype.htm (386 bytes) was successfully processed in %d seconds:' 143 'File lowercase_doctype.htm (386 bytes) was successfully processed in %d seconds:'
139 .' 2 links imported, 0 links overwritten, 0 links skipped.', 144 .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
140 NetscapeBookmarkUtils::import(null, $files, $this->linkDb, $this->conf, $this->history) 145 NetscapeBookmarkUtils::import(null, $files, $this->bookmarkService, $this->conf, $this->history)
141 ); 146 );
142 $this->assertEquals(2, count($this->linkDb)); 147 $this->assertEquals(2, $this->bookmarkService->count());
143 } 148 }
144 149
145 150
@@ -151,25 +156,24 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
151 $files = file2array('internet_explorer_encoding.htm'); 156 $files = file2array('internet_explorer_encoding.htm');
152 $this->assertStringMatchesFormat( 157 $this->assertStringMatchesFormat(
153 'File internet_explorer_encoding.htm (356 bytes) was successfully processed in %d seconds:' 158 'File internet_explorer_encoding.htm (356 bytes) was successfully processed in %d seconds:'
154 .' 1 links imported, 0 links overwritten, 0 links skipped.', 159 .' 1 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
155 NetscapeBookmarkUtils::import([], $files, $this->linkDb, $this->conf, $this->history) 160 NetscapeBookmarkUtils::import([], $files, $this->bookmarkService, $this->conf, $this->history)
156 ); 161 );
157 $this->assertEquals(1, count($this->linkDb)); 162 $this->assertEquals(1, $this->bookmarkService->count());
158 $this->assertEquals(0, count_private($this->linkDb)); 163 $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
159 164
165 $bookmark = $this->bookmarkService->findByUrl('http://hginit.com/');
166 $this->assertEquals(0, $bookmark->getId());
160 $this->assertEquals( 167 $this->assertEquals(
161 array( 168 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20160618_203944'),
162 'id' => 0, 169 $bookmark->getCreated()
163 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160618_203944'),
164 'title' => 'Hg Init a Mercurial tutorial by Joel Spolsky',
165 'url' => 'http://hginit.com/',
166 'description' => '',
167 'private' => 0,
168 'tags' => '',
169 'shorturl' => 'La37cg',
170 ),
171 $this->linkDb->getLinkFromUrl('http://hginit.com/')
172 ); 170 );
171 $this->assertEquals('Hg Init a Mercurial tutorial by Joel Spolsky', $bookmark->getTitle());
172 $this->assertEquals('http://hginit.com/', $bookmark->getUrl());
173 $this->assertEquals('', $bookmark->getDescription());
174 $this->assertFalse($bookmark->isPrivate());
175 $this->assertEquals('', $bookmark->getTagsString());
176 $this->assertEquals('La37cg', $bookmark->getShortUrl());
173 } 177 }
174 178
175 /** 179 /**
@@ -180,116 +184,115 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
180 $files = file2array('netscape_nested.htm'); 184 $files = file2array('netscape_nested.htm');
181 $this->assertStringMatchesFormat( 185 $this->assertStringMatchesFormat(
182 'File netscape_nested.htm (1337 bytes) was successfully processed in %d seconds:' 186 'File netscape_nested.htm (1337 bytes) was successfully processed in %d seconds:'
183 .' 8 links imported, 0 links overwritten, 0 links skipped.', 187 .' 8 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
184 NetscapeBookmarkUtils::import([], $files, $this->linkDb, $this->conf, $this->history) 188 NetscapeBookmarkUtils::import([], $files, $this->bookmarkService, $this->conf, $this->history)
185 );
186 $this->assertEquals(8, count($this->linkDb));
187 $this->assertEquals(2, count_private($this->linkDb));
188
189 $this->assertEquals(
190 array(
191 'id' => 0,
192 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160225_235541'),
193 'title' => 'Nested 1',
194 'url' => 'http://nest.ed/1',
195 'description' => '',
196 'private' => 0,
197 'tags' => 'tag1 tag2',
198 'shorturl' => 'KyDNKA',
199 ),
200 $this->linkDb->getLinkFromUrl('http://nest.ed/1')
201 );
202 $this->assertEquals(
203 array(
204 'id' => 1,
205 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160225_235542'),
206 'title' => 'Nested 1-1',
207 'url' => 'http://nest.ed/1-1',
208 'description' => '',
209 'private' => 0,
210 'tags' => 'folder1 tag1 tag2',
211 'shorturl' => 'T2LnXg',
212 ),
213 $this->linkDb->getLinkFromUrl('http://nest.ed/1-1')
214 );
215 $this->assertEquals(
216 array(
217 'id' => 2,
218 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160225_235547'),
219 'title' => 'Nested 1-2',
220 'url' => 'http://nest.ed/1-2',
221 'description' => '',
222 'private' => 0,
223 'tags' => 'folder1 tag3 tag4',
224 'shorturl' => '46SZxA',
225 ),
226 $this->linkDb->getLinkFromUrl('http://nest.ed/1-2')
227 );
228 $this->assertEquals(
229 array(
230 'id' => 3,
231 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160202_202222'),
232 'title' => 'Nested 2-1',
233 'url' => 'http://nest.ed/2-1',
234 'description' => 'First link of the second section',
235 'private' => 1,
236 'tags' => 'folder2',
237 'shorturl' => '4UHOSw',
238 ),
239 $this->linkDb->getLinkFromUrl('http://nest.ed/2-1')
240 );
241 $this->assertEquals(
242 array(
243 'id' => 4,
244 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160119_230227'),
245 'title' => 'Nested 2-2',
246 'url' => 'http://nest.ed/2-2',
247 'description' => 'Second link of the second section',
248 'private' => 1,
249 'tags' => 'folder2',
250 'shorturl' => 'yfzwbw',
251 ),
252 $this->linkDb->getLinkFromUrl('http://nest.ed/2-2')
253 );
254 $this->assertEquals(
255 array(
256 'id' => 5,
257 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160202_202222'),
258 'title' => 'Nested 3-1',
259 'url' => 'http://nest.ed/3-1',
260 'description' => '',
261 'private' => 0,
262 'tags' => 'folder3 folder3-1 tag3',
263 'shorturl' => 'UwxIUQ',
264 ),
265 $this->linkDb->getLinkFromUrl('http://nest.ed/3-1')
266 );
267 $this->assertEquals(
268 array(
269 'id' => 6,
270 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160119_230227'),
271 'title' => 'Nested 3-2',
272 'url' => 'http://nest.ed/3-2',
273 'description' => '',
274 'private' => 0,
275 'tags' => 'folder3 folder3-1',
276 'shorturl' => 'p8dyZg',
277 ),
278 $this->linkDb->getLinkFromUrl('http://nest.ed/3-2')
279 );
280 $this->assertEquals(
281 array(
282 'id' => 7,
283 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160229_111541'),
284 'title' => 'Nested 2',
285 'url' => 'http://nest.ed/2',
286 'description' => '',
287 'private' => 0,
288 'tags' => 'tag4',
289 'shorturl' => 'Gt3Uug',
290 ),
291 $this->linkDb->getLinkFromUrl('http://nest.ed/2')
292 ); 189 );
190 $this->assertEquals(8, $this->bookmarkService->count());
191 $this->assertEquals(2, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
192
193 $bookmark = $this->bookmarkService->findByUrl('http://nest.ed/1');
194 $this->assertEquals(0, $bookmark->getId());
195 $this->assertEquals(
196 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20160225_235541'),
197 $bookmark->getCreated()
198 );
199 $this->assertEquals('Nested 1', $bookmark->getTitle());
200 $this->assertEquals('http://nest.ed/1', $bookmark->getUrl());
201 $this->assertEquals('', $bookmark->getDescription());
202 $this->assertFalse($bookmark->isPrivate());
203 $this->assertEquals('tag1 tag2', $bookmark->getTagsString());
204 $this->assertEquals('KyDNKA', $bookmark->getShortUrl());
205
206 $bookmark = $this->bookmarkService->findByUrl('http://nest.ed/1-1');
207 $this->assertEquals(1, $bookmark->getId());
208 $this->assertEquals(
209 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20160225_235542'),
210 $bookmark->getCreated()
211 );
212 $this->assertEquals('Nested 1-1', $bookmark->getTitle());
213 $this->assertEquals('http://nest.ed/1-1', $bookmark->getUrl());
214 $this->assertEquals('', $bookmark->getDescription());
215 $this->assertFalse($bookmark->isPrivate());
216 $this->assertEquals('folder1 tag1 tag2', $bookmark->getTagsString());
217 $this->assertEquals('T2LnXg', $bookmark->getShortUrl());
218
219 $bookmark = $this->bookmarkService->findByUrl('http://nest.ed/1-2');
220 $this->assertEquals(2, $bookmark->getId());
221 $this->assertEquals(
222 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20160225_235547'),
223 $bookmark->getCreated()
224 );
225 $this->assertEquals('Nested 1-2', $bookmark->getTitle());
226 $this->assertEquals('http://nest.ed/1-2', $bookmark->getUrl());
227 $this->assertEquals('', $bookmark->getDescription());
228 $this->assertFalse($bookmark->isPrivate());
229 $this->assertEquals('folder1 tag3 tag4', $bookmark->getTagsString());
230 $this->assertEquals('46SZxA', $bookmark->getShortUrl());
231
232 $bookmark = $this->bookmarkService->findByUrl('http://nest.ed/2-1');
233 $this->assertEquals(3, $bookmark->getId());
234 $this->assertEquals(
235 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20160202_202222'),
236 $bookmark->getCreated()
237 );
238 $this->assertEquals('Nested 2-1', $bookmark->getTitle());
239 $this->assertEquals('http://nest.ed/2-1', $bookmark->getUrl());
240 $this->assertEquals('First link of the second section', $bookmark->getDescription());
241 $this->assertTrue($bookmark->isPrivate());
242 $this->assertEquals('folder2', $bookmark->getTagsString());
243 $this->assertEquals('4UHOSw', $bookmark->getShortUrl());
244
245 $bookmark = $this->bookmarkService->findByUrl('http://nest.ed/2-2');
246 $this->assertEquals(4, $bookmark->getId());
247 $this->assertEquals(
248 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20160119_230227'),
249 $bookmark->getCreated()
250 );
251 $this->assertEquals('Nested 2-2', $bookmark->getTitle());
252 $this->assertEquals('http://nest.ed/2-2', $bookmark->getUrl());
253 $this->assertEquals('Second link of the second section', $bookmark->getDescription());
254 $this->assertTrue($bookmark->isPrivate());
255 $this->assertEquals('folder2', $bookmark->getTagsString());
256 $this->assertEquals('yfzwbw', $bookmark->getShortUrl());
257
258 $bookmark = $this->bookmarkService->findByUrl('http://nest.ed/3-1');
259 $this->assertEquals(5, $bookmark->getId());
260 $this->assertEquals(
261 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20160202_202222'),
262 $bookmark->getCreated()
263 );
264 $this->assertEquals('Nested 3-1', $bookmark->getTitle());
265 $this->assertEquals('http://nest.ed/3-1', $bookmark->getUrl());
266 $this->assertEquals('', $bookmark->getDescription());
267 $this->assertFalse($bookmark->isPrivate());
268 $this->assertEquals('folder3 folder3-1 tag3', $bookmark->getTagsString());
269 $this->assertEquals('UwxIUQ', $bookmark->getShortUrl());
270
271 $bookmark = $this->bookmarkService->findByUrl('http://nest.ed/3-2');
272 $this->assertEquals(6, $bookmark->getId());
273 $this->assertEquals(
274 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20160119_230227'),
275 $bookmark->getCreated()
276 );
277 $this->assertEquals('Nested 3-2', $bookmark->getTitle());
278 $this->assertEquals('http://nest.ed/3-2', $bookmark->getUrl());
279 $this->assertEquals('', $bookmark->getDescription());
280 $this->assertFalse($bookmark->isPrivate());
281 $this->assertEquals('folder3 folder3-1', $bookmark->getTagsString());
282 $this->assertEquals('p8dyZg', $bookmark->getShortUrl());
283
284 $bookmark = $this->bookmarkService->findByUrl('http://nest.ed/2');
285 $this->assertEquals(7, $bookmark->getId());
286 $this->assertEquals(
287 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20160229_111541'),
288 $bookmark->getCreated()
289 );
290 $this->assertEquals('Nested 2', $bookmark->getTitle());
291 $this->assertEquals('http://nest.ed/2', $bookmark->getUrl());
292 $this->assertEquals('', $bookmark->getDescription());
293 $this->assertFalse($bookmark->isPrivate());
294 $this->assertEquals('tag4', $bookmark->getTagsString());
295 $this->assertEquals('Gt3Uug', $bookmark->getShortUrl());
293 } 296 }
294 297
295 /** 298 /**
@@ -302,40 +305,38 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
302 $files = file2array('netscape_basic.htm'); 305 $files = file2array('netscape_basic.htm');
303 $this->assertStringMatchesFormat( 306 $this->assertStringMatchesFormat(
304 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' 307 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
305 .' 2 links imported, 0 links overwritten, 0 links skipped.', 308 .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
306 NetscapeBookmarkUtils::import([], $files, $this->linkDb, $this->conf, $this->history) 309 NetscapeBookmarkUtils::import([], $files, $this->bookmarkService, $this->conf, $this->history)
307 ); 310 );
308 311
309 $this->assertEquals(2, count($this->linkDb)); 312 $this->assertEquals(2, $this->bookmarkService->count());
310 $this->assertEquals(1, count_private($this->linkDb)); 313 $this->assertEquals(1, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
311 314
315 $bookmark = $this->bookmarkService->findByUrl('https://private.tld');
316 $this->assertEquals(0, $bookmark->getId());
312 $this->assertEquals( 317 $this->assertEquals(
313 array( 318 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20001010_135536'),
314 'id' => 0, 319 $bookmark->getCreated()
315 // Old link - UTC+4 (note that TZ in the import file is ignored).
316 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20001010_135536'),
317 'title' => 'Secret stuff',
318 'url' => 'https://private.tld',
319 'description' => "Super-secret stuff you're not supposed to know about",
320 'private' => 1,
321 'tags' => 'private secret',
322 'shorturl' => 'EokDtA',
323 ),
324 $this->linkDb->getLinkFromUrl('https://private.tld')
325 ); 320 );
321 $this->assertEquals('Secret stuff', $bookmark->getTitle());
322 $this->assertEquals('https://private.tld', $bookmark->getUrl());
323 $this->assertEquals('Super-secret stuff you\'re not supposed to know about', $bookmark->getDescription());
324 $this->assertTrue($bookmark->isPrivate());
325 $this->assertEquals('private secret', $bookmark->getTagsString());
326 $this->assertEquals('EokDtA', $bookmark->getShortUrl());
327
328 $bookmark = $this->bookmarkService->findByUrl('http://public.tld');
329 $this->assertEquals(1, $bookmark->getId());
326 $this->assertEquals( 330 $this->assertEquals(
327 array( 331 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20160225_235548'),
328 'id' => 1, 332 $bookmark->getCreated()
329 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160225_235548'),
330 'title' => 'Public stuff',
331 'url' => 'http://public.tld',
332 'description' => '',
333 'private' => 0,
334 'tags' => 'public hello world',
335 'shorturl' => 'Er9ddA',
336 ),
337 $this->linkDb->getLinkFromUrl('http://public.tld')
338 ); 333 );
334 $this->assertEquals('Public stuff', $bookmark->getTitle());
335 $this->assertEquals('http://public.tld', $bookmark->getUrl());
336 $this->assertEquals('', $bookmark->getDescription());
337 $this->assertFalse($bookmark->isPrivate());
338 $this->assertEquals('public hello world', $bookmark->getTagsString());
339 $this->assertEquals('Er9ddA', $bookmark->getShortUrl());
339 } 340 }
340 341
341 /** 342 /**
@@ -347,43 +348,42 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
347 $files = file2array('netscape_basic.htm'); 348 $files = file2array('netscape_basic.htm');
348 $this->assertStringMatchesFormat( 349 $this->assertStringMatchesFormat(
349 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' 350 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
350 .' 2 links imported, 0 links overwritten, 0 links skipped.', 351 .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
351 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history) 352 NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history)
352 ); 353 );
353 $this->assertEquals(2, count($this->linkDb));
354 $this->assertEquals(1, count_private($this->linkDb));
355 354
355 $this->assertEquals(2, $this->bookmarkService->count());
356 $this->assertEquals(1, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
357
358 $bookmark = $this->bookmarkService->findByUrl('https://private.tld');
359 $this->assertEquals(0, $bookmark->getId());
356 $this->assertEquals( 360 $this->assertEquals(
357 array( 361 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20001010_135536'),
358 'id' => 0, 362 $bookmark->getCreated()
359 // Note that TZ in the import file is ignored.
360 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20001010_135536'),
361 'title' => 'Secret stuff',
362 'url' => 'https://private.tld',
363 'description' => "Super-secret stuff you're not supposed to know about",
364 'private' => 1,
365 'tags' => 'private secret',
366 'shorturl' => 'EokDtA',
367 ),
368 $this->linkDb->getLinkFromUrl('https://private.tld')
369 ); 363 );
364 $this->assertEquals('Secret stuff', $bookmark->getTitle());
365 $this->assertEquals('https://private.tld', $bookmark->getUrl());
366 $this->assertEquals('Super-secret stuff you\'re not supposed to know about', $bookmark->getDescription());
367 $this->assertTrue($bookmark->isPrivate());
368 $this->assertEquals('private secret', $bookmark->getTagsString());
369 $this->assertEquals('EokDtA', $bookmark->getShortUrl());
370
371 $bookmark = $this->bookmarkService->findByUrl('http://public.tld');
372 $this->assertEquals(1, $bookmark->getId());
370 $this->assertEquals( 373 $this->assertEquals(
371 array( 374 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20160225_235548'),
372 'id' => 1, 375 $bookmark->getCreated()
373 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160225_235548'),
374 'title' => 'Public stuff',
375 'url' => 'http://public.tld',
376 'description' => '',
377 'private' => 0,
378 'tags' => 'public hello world',
379 'shorturl' => 'Er9ddA',
380 ),
381 $this->linkDb->getLinkFromUrl('http://public.tld')
382 ); 376 );
377 $this->assertEquals('Public stuff', $bookmark->getTitle());
378 $this->assertEquals('http://public.tld', $bookmark->getUrl());
379 $this->assertEquals('', $bookmark->getDescription());
380 $this->assertFalse($bookmark->isPrivate());
381 $this->assertEquals('public hello world', $bookmark->getTagsString());
382 $this->assertEquals('Er9ddA', $bookmark->getShortUrl());
383 } 383 }
384 384
385 /** 385 /**
386 * Import links as public 386 * Import bookmarks as public
387 */ 387 */
388 public function testImportAsPublic() 388 public function testImportAsPublic()
389 { 389 {
@@ -391,23 +391,17 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
391 $files = file2array('netscape_basic.htm'); 391 $files = file2array('netscape_basic.htm');
392 $this->assertStringMatchesFormat( 392 $this->assertStringMatchesFormat(
393 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' 393 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
394 .' 2 links imported, 0 links overwritten, 0 links skipped.', 394 .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
395 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history) 395 NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history)
396 );
397 $this->assertEquals(2, count($this->linkDb));
398 $this->assertEquals(0, count_private($this->linkDb));
399 $this->assertEquals(
400 0,
401 $this->linkDb[0]['private']
402 );
403 $this->assertEquals(
404 0,
405 $this->linkDb[1]['private']
406 ); 396 );
397 $this->assertEquals(2, $this->bookmarkService->count());
398 $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
399 $this->assertFalse($this->bookmarkService->get(0)->isPrivate());
400 $this->assertFalse($this->bookmarkService->get(1)->isPrivate());
407 } 401 }
408 402
409 /** 403 /**
410 * Import links as private 404 * Import bookmarks as private
411 */ 405 */
412 public function testImportAsPrivate() 406 public function testImportAsPrivate()
413 { 407 {
@@ -415,45 +409,34 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
415 $files = file2array('netscape_basic.htm'); 409 $files = file2array('netscape_basic.htm');
416 $this->assertStringMatchesFormat( 410 $this->assertStringMatchesFormat(
417 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' 411 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
418 .' 2 links imported, 0 links overwritten, 0 links skipped.', 412 .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
419 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history) 413 NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history)
420 );
421 $this->assertEquals(2, count($this->linkDb));
422 $this->assertEquals(2, count_private($this->linkDb));
423 $this->assertEquals(
424 1,
425 $this->linkDb['0']['private']
426 );
427 $this->assertEquals(
428 1,
429 $this->linkDb['1']['private']
430 ); 414 );
415 $this->assertEquals(2, $this->bookmarkService->count());
416 $this->assertEquals(2, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
417 $this->assertTrue($this->bookmarkService->get(0)->isPrivate());
418 $this->assertTrue($this->bookmarkService->get(1)->isPrivate());
431 } 419 }
432 420
433 /** 421 /**
434 * Overwrite private links so they become public 422 * Overwrite private bookmarks so they become public
435 */ 423 */
436 public function testOverwriteAsPublic() 424 public function testOverwriteAsPublic()
437 { 425 {
438 $files = file2array('netscape_basic.htm'); 426 $files = file2array('netscape_basic.htm');
439 427
440 // import links as private 428 // import bookmarks as private
441 $post = array('privacy' => 'private'); 429 $post = array('privacy' => 'private');
442 $this->assertStringMatchesFormat( 430 $this->assertStringMatchesFormat(
443 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' 431 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
444 .' 2 links imported, 0 links overwritten, 0 links skipped.', 432 .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
445 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history) 433 NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history)
446 );
447 $this->assertEquals(2, count($this->linkDb));
448 $this->assertEquals(2, count_private($this->linkDb));
449 $this->assertEquals(
450 1,
451 $this->linkDb[0]['private']
452 );
453 $this->assertEquals(
454 1,
455 $this->linkDb[1]['private']
456 ); 434 );
435 $this->assertEquals(2, $this->bookmarkService->count());
436 $this->assertEquals(2, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
437 $this->assertTrue($this->bookmarkService->get(0)->isPrivate());
438 $this->assertTrue($this->bookmarkService->get(1)->isPrivate());
439
457 // re-import as public, enable overwriting 440 // re-import as public, enable overwriting
458 $post = array( 441 $post = array(
459 'privacy' => 'public', 442 'privacy' => 'public',
@@ -461,45 +444,33 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
461 ); 444 );
462 $this->assertStringMatchesFormat( 445 $this->assertStringMatchesFormat(
463 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' 446 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
464 .' 2 links imported, 2 links overwritten, 0 links skipped.', 447 .' 2 bookmarks imported, 2 bookmarks overwritten, 0 bookmarks skipped.',
465 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history) 448 NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history)
466 );
467 $this->assertEquals(2, count($this->linkDb));
468 $this->assertEquals(0, count_private($this->linkDb));
469 $this->assertEquals(
470 0,
471 $this->linkDb[0]['private']
472 );
473 $this->assertEquals(
474 0,
475 $this->linkDb[1]['private']
476 ); 449 );
450 $this->assertEquals(2, $this->bookmarkService->count());
451 $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
452 $this->assertFalse($this->bookmarkService->get(0)->isPrivate());
453 $this->assertFalse($this->bookmarkService->get(1)->isPrivate());
477 } 454 }
478 455
479 /** 456 /**
480 * Overwrite public links so they become private 457 * Overwrite public bookmarks so they become private
481 */ 458 */
482 public function testOverwriteAsPrivate() 459 public function testOverwriteAsPrivate()
483 { 460 {
484 $files = file2array('netscape_basic.htm'); 461 $files = file2array('netscape_basic.htm');
485 462
486 // import links as public 463 // import bookmarks as public
487 $post = array('privacy' => 'public'); 464 $post = array('privacy' => 'public');
488 $this->assertStringMatchesFormat( 465 $this->assertStringMatchesFormat(
489 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' 466 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
490 .' 2 links imported, 0 links overwritten, 0 links skipped.', 467 .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
491 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history) 468 NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history)
492 );
493 $this->assertEquals(2, count($this->linkDb));
494 $this->assertEquals(0, count_private($this->linkDb));
495 $this->assertEquals(
496 0,
497 $this->linkDb['0']['private']
498 );
499 $this->assertEquals(
500 0,
501 $this->linkDb['1']['private']
502 ); 469 );
470 $this->assertEquals(2, $this->bookmarkService->count());
471 $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
472 $this->assertFalse($this->bookmarkService->get(0)->isPrivate());
473 $this->assertFalse($this->bookmarkService->get(1)->isPrivate());
503 474
504 // re-import as private, enable overwriting 475 // re-import as private, enable overwriting
505 $post = array( 476 $post = array(
@@ -508,23 +479,17 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
508 ); 479 );
509 $this->assertStringMatchesFormat( 480 $this->assertStringMatchesFormat(
510 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' 481 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
511 .' 2 links imported, 2 links overwritten, 0 links skipped.', 482 .' 2 bookmarks imported, 2 bookmarks overwritten, 0 bookmarks skipped.',
512 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history) 483 NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history)
513 );
514 $this->assertEquals(2, count($this->linkDb));
515 $this->assertEquals(2, count_private($this->linkDb));
516 $this->assertEquals(
517 1,
518 $this->linkDb['0']['private']
519 );
520 $this->assertEquals(
521 1,
522 $this->linkDb['1']['private']
523 ); 484 );
485 $this->assertEquals(2, $this->bookmarkService->count());
486 $this->assertEquals(2, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
487 $this->assertTrue($this->bookmarkService->get(0)->isPrivate());
488 $this->assertTrue($this->bookmarkService->get(1)->isPrivate());
524 } 489 }
525 490
526 /** 491 /**
527 * Attept to import the same links twice without enabling overwriting 492 * Attept to import the same bookmarks twice without enabling overwriting
528 */ 493 */
529 public function testSkipOverwrite() 494 public function testSkipOverwrite()
530 { 495 {
@@ -532,21 +497,21 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
532 $files = file2array('netscape_basic.htm'); 497 $files = file2array('netscape_basic.htm');
533 $this->assertStringMatchesFormat( 498 $this->assertStringMatchesFormat(
534 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' 499 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
535 .' 2 links imported, 0 links overwritten, 0 links skipped.', 500 .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
536 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history) 501 NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history)
537 ); 502 );
538 $this->assertEquals(2, count($this->linkDb)); 503 $this->assertEquals(2, $this->bookmarkService->count());
539 $this->assertEquals(0, count_private($this->linkDb)); 504 $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
540 505
541 // re-import as private, DO NOT enable overwriting 506 // re-import as private, DO NOT enable overwriting
542 $post = array('privacy' => 'private'); 507 $post = array('privacy' => 'private');
543 $this->assertStringMatchesFormat( 508 $this->assertStringMatchesFormat(
544 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' 509 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
545 .' 0 links imported, 0 links overwritten, 2 links skipped.', 510 .' 0 bookmarks imported, 0 bookmarks overwritten, 2 bookmarks skipped.',
546 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history) 511 NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history)
547 ); 512 );
548 $this->assertEquals(2, count($this->linkDb)); 513 $this->assertEquals(2, $this->bookmarkService->count());
549 $this->assertEquals(0, count_private($this->linkDb)); 514 $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
550 } 515 }
551 516
552 /** 517 /**
@@ -561,19 +526,13 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
561 $files = file2array('netscape_basic.htm'); 526 $files = file2array('netscape_basic.htm');
562 $this->assertStringMatchesFormat( 527 $this->assertStringMatchesFormat(
563 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' 528 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
564 .' 2 links imported, 0 links overwritten, 0 links skipped.', 529 .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
565 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history) 530 NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history)
566 );
567 $this->assertEquals(2, count($this->linkDb));
568 $this->assertEquals(0, count_private($this->linkDb));
569 $this->assertEquals(
570 'tag1 tag2 tag3 private secret',
571 $this->linkDb['0']['tags']
572 );
573 $this->assertEquals(
574 'tag1 tag2 tag3 public hello world',
575 $this->linkDb['1']['tags']
576 ); 531 );
532 $this->assertEquals(2, $this->bookmarkService->count());
533 $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
534 $this->assertEquals('tag1 tag2 tag3 private secret', $this->bookmarkService->get(0)->getTagsString());
535 $this->assertEquals('tag1 tag2 tag3 public hello world', $this->bookmarkService->get(1)->getTagsString());
577 } 536 }
578 537
579 /** 538 /**
@@ -588,18 +547,18 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
588 $files = file2array('netscape_basic.htm'); 547 $files = file2array('netscape_basic.htm');
589 $this->assertStringMatchesFormat( 548 $this->assertStringMatchesFormat(
590 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' 549 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
591 .' 2 links imported, 0 links overwritten, 0 links skipped.', 550 .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
592 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history) 551 NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history)
593 ); 552 );
594 $this->assertEquals(2, count($this->linkDb)); 553 $this->assertEquals(2, $this->bookmarkService->count());
595 $this->assertEquals(0, count_private($this->linkDb)); 554 $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
596 $this->assertEquals( 555 $this->assertEquals(
597 'tag1&amp; tag2 &quot;tag3&quot; private secret', 556 'tag1&amp; tag2 &quot;tag3&quot; private secret',
598 $this->linkDb['0']['tags'] 557 $this->bookmarkService->get(0)->getTagsString()
599 ); 558 );
600 $this->assertEquals( 559 $this->assertEquals(
601 'tag1&amp; tag2 &quot;tag3&quot; public hello world', 560 'tag1&amp; tag2 &quot;tag3&quot; public hello world',
602 $this->linkDb['1']['tags'] 561 $this->bookmarkService->get(1)->getTagsString()
603 ); 562 );
604 } 563 }
605 564
@@ -613,23 +572,14 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
613 $files = file2array('same_date.htm'); 572 $files = file2array('same_date.htm');
614 $this->assertStringMatchesFormat( 573 $this->assertStringMatchesFormat(
615 'File same_date.htm (453 bytes) was successfully processed in %d seconds:' 574 'File same_date.htm (453 bytes) was successfully processed in %d seconds:'
616 .' 3 links imported, 0 links overwritten, 0 links skipped.', 575 .' 3 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
617 NetscapeBookmarkUtils::import(array(), $files, $this->linkDb, $this->conf, $this->history) 576 NetscapeBookmarkUtils::import(array(), $files, $this->bookmarkService, $this->conf, $this->history)
618 ); 577 );
619 $this->assertEquals(3, count($this->linkDb)); 578 $this->assertEquals(3, $this->bookmarkService->count());
620 $this->assertEquals(0, count_private($this->linkDb)); 579 $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
621 $this->assertEquals( 580 $this->assertEquals(0, $this->bookmarkService->get(0)->getId());
622 0, 581 $this->assertEquals(1, $this->bookmarkService->get(1)->getId());
623 $this->linkDb[0]['id'] 582 $this->assertEquals(2, $this->bookmarkService->get(2)->getId());
624 );
625 $this->assertEquals(
626 1,
627 $this->linkDb[1]['id']
628 );
629 $this->assertEquals(
630 2,
631 $this->linkDb[2]['id']
632 );
633 } 583 }
634 584
635 public function testImportCreateUpdateHistory() 585 public function testImportCreateUpdateHistory()
@@ -639,14 +589,14 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
639 'overwrite' => 'true', 589 'overwrite' => 'true',
640 ]; 590 ];
641 $files = file2array('netscape_basic.htm'); 591 $files = file2array('netscape_basic.htm');
642 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history); 592 NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history);
643 $history = $this->history->getHistory(); 593 $history = $this->history->getHistory();
644 $this->assertEquals(1, count($history)); 594 $this->assertEquals(1, count($history));
645 $this->assertEquals(History::IMPORT, $history[0]['event']); 595 $this->assertEquals(History::IMPORT, $history[0]['event']);
646 $this->assertTrue(new DateTime('-5 seconds') < $history[0]['datetime']); 596 $this->assertTrue(new DateTime('-5 seconds') < $history[0]['datetime']);
647 597
648 // re-import as private, enable overwriting 598 // re-import as private, enable overwriting
649 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history); 599 NetscapeBookmarkUtils::import($post, $files, $this->bookmarkService, $this->conf, $this->history);
650 $history = $this->history->getHistory(); 600 $history = $this->history->getHistory();
651 $this->assertEquals(2, count($history)); 601 $this->assertEquals(2, count($history));
652 $this->assertEquals(History::IMPORT, $history[0]['event']); 602 $this->assertEquals(History::IMPORT, $history[0]['event']);
diff --git a/tests/plugins/PluginArchiveorgTest.php b/tests/plugins/PluginArchiveorgTest.php
index 510288bb..b9a67adb 100644
--- a/tests/plugins/PluginArchiveorgTest.php
+++ b/tests/plugins/PluginArchiveorgTest.php
@@ -24,7 +24,7 @@ class PluginArchiveorgTest extends \PHPUnit\Framework\TestCase
24 } 24 }
25 25
26 /** 26 /**
27 * Test render_linklist hook on external links. 27 * Test render_linklist hook on external bookmarks.
28 */ 28 */
29 public function testArchiveorgLinklistOnExternalLinks() 29 public function testArchiveorgLinklistOnExternalLinks()
30 { 30 {
@@ -54,7 +54,7 @@ class PluginArchiveorgTest extends \PHPUnit\Framework\TestCase
54 } 54 }
55 55
56 /** 56 /**
57 * Test render_linklist hook on internal links. 57 * Test render_linklist hook on internal bookmarks.
58 */ 58 */
59 public function testArchiveorgLinklistOnInternalLinks() 59 public function testArchiveorgLinklistOnInternalLinks()
60 { 60 {
diff --git a/tests/plugins/PluginIssoTest.php b/tests/plugins/PluginIssoTest.php
index bdfab439..99477205 100644
--- a/tests/plugins/PluginIssoTest.php
+++ b/tests/plugins/PluginIssoTest.php
@@ -2,7 +2,7 @@
2namespace Shaarli\Plugin\Isso; 2namespace Shaarli\Plugin\Isso;
3 3
4use DateTime; 4use DateTime;
5use Shaarli\Bookmark\LinkDB; 5use Shaarli\Bookmark\Bookmark;
6use Shaarli\Config\ConfigManager; 6use Shaarli\Config\ConfigManager;
7use Shaarli\Plugin\PluginManager; 7use Shaarli\Plugin\PluginManager;
8 8
@@ -60,7 +60,7 @@ class PluginIssoTest extends \PHPUnit\Framework\TestCase
60 array( 60 array(
61 'id' => 12, 61 'id' => 12,
62 'url' => $str, 62 'url' => $str,
63 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $date), 63 'created' => DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, $date),
64 ) 64 )
65 ) 65 )
66 ); 66 );
@@ -85,7 +85,7 @@ class PluginIssoTest extends \PHPUnit\Framework\TestCase
85 } 85 }
86 86
87 /** 87 /**
88 * Test isso plugin when multiple links are displayed (shouldn't be displayed). 88 * Test isso plugin when multiple bookmarks are displayed (shouldn't be displayed).
89 */ 89 */
90 public function testIssoMultipleLinks() 90 public function testIssoMultipleLinks()
91 { 91 {
@@ -102,13 +102,13 @@ class PluginIssoTest extends \PHPUnit\Framework\TestCase
102 'id' => 12, 102 'id' => 12,
103 'url' => $str, 103 'url' => $str,
104 'shorturl' => $short1 = 'abcd', 104 'shorturl' => $short1 = 'abcd',
105 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $date1), 105 'created' => DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, $date1),
106 ), 106 ),
107 array( 107 array(
108 'id' => 13, 108 'id' => 13,
109 'url' => $str . '2', 109 'url' => $str . '2',
110 'shorturl' => $short2 = 'efgh', 110 'shorturl' => $short2 = 'efgh',
111 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $date2), 111 'created' => DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, $date2),
112 ), 112 ),
113 ) 113 )
114 ); 114 );
@@ -136,7 +136,7 @@ class PluginIssoTest extends \PHPUnit\Framework\TestCase
136 'id' => 12, 136 'id' => 12,
137 'url' => $str, 137 'url' => $str,
138 'shorturl' => $short1 = 'abcd', 138 'shorturl' => $short1 = 'abcd',
139 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $date), 139 'created' => DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, $date),
140 ) 140 )
141 ), 141 ),
142 'search_term' => $str 142 'search_term' => $str
diff --git a/tests/plugins/PluginMarkdownTest.php b/tests/plugins/PluginMarkdownTest.php
deleted file mode 100644
index 15fa9ba5..00000000
--- a/tests/plugins/PluginMarkdownTest.php
+++ /dev/null
@@ -1,316 +0,0 @@
1<?php
2namespace Shaarli\Plugin\Markdown;
3
4use Shaarli\Config\ConfigManager;
5use Shaarli\Plugin\PluginManager;
6
7/**
8 * PluginMarkdownTest.php
9 */
10
11require_once 'application/bookmark/LinkUtils.php';
12require_once 'application/Utils.php';
13require_once 'plugins/markdown/markdown.php';
14
15/**
16 * Class PluginMarkdownTest
17 * Unit test for the Markdown plugin
18 */
19class PluginMarkdownTest extends \PHPUnit\Framework\TestCase
20{
21 /**
22 * @var ConfigManager instance.
23 */
24 protected $conf;
25
26 /**
27 * Reset plugin path
28 */
29 public function setUp()
30 {
31 PluginManager::$PLUGINS_PATH = 'plugins';
32 $this->conf = new ConfigManager('tests/utils/config/configJson');
33 $this->conf->set('security.allowed_protocols', ['ftp', 'magnet']);
34 }
35
36 /**
37 * Test render_linklist hook.
38 * Only check that there is basic markdown rendering.
39 */
40 public function testMarkdownLinklist()
41 {
42 $markdown = '# My title' . PHP_EOL . 'Very interesting content.';
43 $data = array(
44 'links' => array(
45 0 => array(
46 'description' => $markdown,
47 ),
48 ),
49 );
50
51 $data = hook_markdown_render_linklist($data, $this->conf);
52 $this->assertNotFalse(strpos($data['links'][0]['description'], '<h1>'));
53 $this->assertNotFalse(strpos($data['links'][0]['description'], '<p>'));
54
55 $this->assertEquals($markdown, $data['links'][0]['description_src']);
56 }
57
58 /**
59 * Test render_feed hook.
60 */
61 public function testMarkdownFeed()
62 {
63 $markdown = '# My title' . PHP_EOL . 'Very interesting content.';
64 $markdown .= '&#8212; <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>';
65 $data = array(
66 'links' => array(
67 0 => array(
68 'description' => $markdown,
69 ),
70 ),
71 );
72
73 $data = hook_markdown_render_feed($data, $this->conf);
74 $this->assertNotFalse(strpos($data['links'][0]['description'], '<h1>'));
75 $this->assertNotFalse(strpos($data['links'][0]['description'], '<p>'));
76 $this->assertStringEndsWith(
77 '&#8212; <a href="http://domain.tld/?0oc_VQ">Permalien</a></p></div>',
78 $data['links'][0]['description']
79 );
80 }
81
82 /**
83 * Test render_daily hook.
84 * Only check that there is basic markdown rendering.
85 */
86 public function testMarkdownDaily()
87 {
88 $markdown = '# My title' . PHP_EOL . 'Very interesting content.';
89 $data = array(
90 // Columns data
91 'linksToDisplay' => array(
92 // nth link
93 0 => array(
94 'formatedDescription' => $markdown,
95 ),
96 ),
97 );
98
99 $data = hook_markdown_render_daily($data, $this->conf);
100 $this->assertNotFalse(strpos($data['linksToDisplay'][0]['formatedDescription'], '<h1>'));
101 $this->assertNotFalse(strpos($data['linksToDisplay'][0]['formatedDescription'], '<p>'));
102 }
103
104 /**
105 * Test reverse_text2clickable().
106 */
107 public function testReverseText2clickable()
108 {
109 $text = 'stuff http://hello.there/is=someone#here otherstuff';
110 $clickableText = text2clickable($text);
111 $reversedText = reverse_text2clickable($clickableText);
112 $this->assertEquals($text, $reversedText);
113 }
114
115 /**
116 * Test reverse_text2clickable().
117 */
118 public function testReverseText2clickableHashtags()
119 {
120 $text = file_get_contents('tests/plugins/resources/hashtags.raw');
121 $md = file_get_contents('tests/plugins/resources/hashtags.md');
122 $clickableText = hashtag_autolink($text);
123 $reversedText = reverse_text2clickable($clickableText);
124 $this->assertEquals($md, $reversedText);
125 }
126
127 /**
128 * Test reverse_nl2br().
129 */
130 public function testReverseNl2br()
131 {
132 $text = 'stuff' . PHP_EOL . 'otherstuff';
133 $processedText = nl2br($text);
134 $reversedText = reverse_nl2br($processedText);
135 $this->assertEquals($text, $reversedText);
136 }
137
138 /**
139 * Test reverse_space2nbsp().
140 */
141 public function testReverseSpace2nbsp()
142 {
143 $text = ' stuff' . PHP_EOL . ' otherstuff and another';
144 $processedText = space2nbsp($text);
145 $reversedText = reverse_space2nbsp($processedText);
146 $this->assertEquals($text, $reversedText);
147 }
148
149 public function testReverseFeedPermalink()
150 {
151 $text = 'Description... ';
152 $text .= '&#8212; <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>';
153 $expected = 'Description... &#8212; [Permalien](http://domain.tld/?0oc_VQ)';
154 $processedText = reverse_feed_permalink($text);
155
156 $this->assertEquals($expected, $processedText);
157 }
158
159 public function testReverseFeedDirectLink()
160 {
161 $text = 'Description... ';
162 $text .= '&#8212; <a href="http://domain.tld/?0oc_VQ" title="Direct link">Direct link</a>';
163 $expected = 'Description... &#8212; [Direct link](http://domain.tld/?0oc_VQ)';
164 $processedText = reverse_feed_permalink($text);
165
166 $this->assertEquals($expected, $processedText);
167 }
168
169 public function testReverseLastFeedPermalink()
170 {
171 $text = 'Description... ';
172 $text .= '<br>&#8212; <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>';
173 $expected = $text;
174 $text .= '<br>&#8212; <a href="http://domain.tld/?0oc_VQ" title="Permalien">Permalien</a>';
175 $expected .= '<br>&#8212; [Permalien](http://domain.tld/?0oc_VQ)';
176 $processedText = reverse_feed_permalink($text);
177
178 $this->assertEquals($expected, $processedText);
179 }
180
181 public function testReverseNoFeedPermalink()
182 {
183 $text = 'Hello! Where are you from?';
184 $expected = $text;
185 $processedText = reverse_feed_permalink($text);
186
187 $this->assertEquals($expected, $processedText);
188 }
189
190 /**
191 * Test sanitize_html().
192 */
193 public function testSanitizeHtml()
194 {
195 $input = '< script src="js.js"/>';
196 $input .= '< script attr>alert(\'xss\');</script>';
197 $input .= '<style> * { display: none }</style>';
198 $output = escape($input);
199 $input .= '<a href="#" onmouseHover="alert(\'xss\');" attr="tt">link</a>';
200 $output .= '<a href="#" attr="tt">link</a>';
201 $input .= '<a href="#" onmouseHover=alert(\'xss\'); attr="tt">link</a>';
202 $output .= '<a href="#" attr="tt">link</a>';
203 $this->assertEquals($output, sanitize_html($input));
204 // Do not touch escaped HTML.
205 $input = escape($input);
206 $this->assertEquals($input, sanitize_html($input));
207 }
208
209 /**
210 * Test the no markdown tag.
211 */
212 public function testNoMarkdownTag()
213 {
214 $str = 'All _work_ and `no play` makes Jack a *dull* boy.';
215 $data = array(
216 'links' => array(array(
217 'description' => $str,
218 'tags' => NO_MD_TAG,
219 'taglist' => array(NO_MD_TAG),
220 ))
221 );
222
223 $processed = hook_markdown_render_linklist($data, $this->conf);
224 $this->assertEquals($str, $processed['links'][0]['description']);
225
226 $processed = hook_markdown_render_feed($data, $this->conf);
227 $this->assertEquals($str, $processed['links'][0]['description']);
228
229 $data = array(
230 // Columns data
231 'linksToDisplay' => array(
232 // nth link
233 0 => array(
234 'formatedDescription' => $str,
235 'tags' => NO_MD_TAG,
236 'taglist' => array(),
237 ),
238 ),
239 );
240
241 $data = hook_markdown_render_daily($data, $this->conf);
242 $this->assertEquals($str, $data['linksToDisplay'][0]['formatedDescription']);
243 }
244
245 /**
246 * Test that a close value to nomarkdown is not understand as nomarkdown (previous value `.nomarkdown`).
247 */
248 public function testNoMarkdownNotExcactlyMatching()
249 {
250 $str = 'All _work_ and `no play` makes Jack a *dull* boy.';
251 $data = array(
252 'links' => array(array(
253 'description' => $str,
254 'tags' => '.' . NO_MD_TAG,
255 'taglist' => array('.'. NO_MD_TAG),
256 ))
257 );
258
259 $data = hook_markdown_render_feed($data, $this->conf);
260 $this->assertContains('<em>', $data['links'][0]['description']);
261 }
262
263 /**
264 * Make sure that the generated HTML match the reference HTML file.
265 */
266 public function testMarkdownGlobalProcessDescription()
267 {
268 $md = file_get_contents('tests/plugins/resources/markdown.md');
269 $md = format_description($md);
270 $html = file_get_contents('tests/plugins/resources/markdown.html');
271
272 $data = process_markdown(
273 $md,
274 $this->conf->get('security.markdown_escape', true),
275 $this->conf->get('security.allowed_protocols')
276 );
277 $this->assertEquals($html, $data . PHP_EOL);
278 }
279
280 /**
281 * Make sure that the HTML tags are escaped.
282 */
283 public function testMarkdownWithHtmlEscape()
284 {
285 $md = '**strong** <strong>strong</strong>';
286 $html = '<div class="markdown"><p><strong>strong</strong> &lt;strong&gt;strong&lt;/strong&gt;</p></div>';
287 $data = array(
288 'links' => array(
289 0 => array(
290 'description' => $md,
291 ),
292 ),
293 );
294 $data = hook_markdown_render_linklist($data, $this->conf);
295 $this->assertEquals($html, $data['links'][0]['description']);
296 }
297
298 /**
299 * Make sure that the HTML tags aren't escaped with the setting set to false.
300 */
301 public function testMarkdownWithHtmlNoEscape()
302 {
303 $this->conf->set('security.markdown_escape', false);
304 $md = '**strong** <strong>strong</strong>';
305 $html = '<div class="markdown"><p><strong>strong</strong> <strong>strong</strong></p></div>';
306 $data = array(
307 'links' => array(
308 0 => array(
309 'description' => $md,
310 ),
311 ),
312 );
313 $data = hook_markdown_render_linklist($data, $this->conf);
314 $this->assertEquals($html, $data['links'][0]['description']);
315 }
316}
diff --git a/tests/updater/DummyUpdater.php b/tests/updater/DummyUpdater.php
index 9e866f1f..07c7f5c4 100644
--- a/tests/updater/DummyUpdater.php
+++ b/tests/updater/DummyUpdater.php
@@ -4,6 +4,7 @@ namespace Shaarli\Updater;
4use Exception; 4use Exception;
5use ReflectionClass; 5use ReflectionClass;
6use ReflectionMethod; 6use ReflectionMethod;
7use Shaarli\Bookmark\BookmarkFileService;
7use Shaarli\Bookmark\LinkDB; 8use Shaarli\Bookmark\LinkDB;
8use Shaarli\Config\ConfigManager; 9use Shaarli\Config\ConfigManager;
9 10
@@ -16,14 +17,14 @@ class DummyUpdater extends Updater
16 /** 17 /**
17 * Object constructor. 18 * Object constructor.
18 * 19 *
19 * @param array $doneUpdates Updates which are already done. 20 * @param array $doneUpdates Updates which are already done.
20 * @param LinkDB $linkDB LinkDB instance. 21 * @param BookmarkFileService $bookmarkService LinkDB instance.
21 * @param ConfigManager $conf Configuration Manager instance. 22 * @param ConfigManager $conf Configuration Manager instance.
22 * @param boolean $isLoggedIn True if the user is logged in. 23 * @param boolean $isLoggedIn True if the user is logged in.
23 */ 24 */
24 public function __construct($doneUpdates, $linkDB, $conf, $isLoggedIn) 25 public function __construct($doneUpdates, $bookmarkService, $conf, $isLoggedIn)
25 { 26 {
26 parent::__construct($doneUpdates, $linkDB, $conf, $isLoggedIn); 27 parent::__construct($doneUpdates, $bookmarkService, $conf, $isLoggedIn);
27 28
28 // Retrieve all update methods. 29 // Retrieve all update methods.
29 // For unit test, only retrieve final methods, 30 // For unit test, only retrieve final methods,
diff --git a/tests/updater/UpdaterTest.php b/tests/updater/UpdaterTest.php
index ac87e33c..c689982b 100644
--- a/tests/updater/UpdaterTest.php
+++ b/tests/updater/UpdaterTest.php
@@ -1,15 +1,9 @@
1<?php 1<?php
2namespace Shaarli\Updater; 2namespace Shaarli\Updater;
3 3
4use DateTime;
5use Exception; 4use Exception;
6use Shaarli\Bookmark\LinkDB;
7use Shaarli\Config\ConfigJson;
8use Shaarli\Config\ConfigManager; 5use Shaarli\Config\ConfigManager;
9use Shaarli\Config\ConfigPhp;
10use Shaarli\Thumbnailer;
11 6
12require_once 'application/updater/UpdaterUtils.php';
13require_once 'tests/updater/DummyUpdater.php'; 7require_once 'tests/updater/DummyUpdater.php';
14require_once 'tests/utils/ReferenceLinkDB.php'; 8require_once 'tests/utils/ReferenceLinkDB.php';
15require_once 'inc/rain.tpl.class.php'; 9require_once 'inc/rain.tpl.class.php';
@@ -45,14 +39,14 @@ class UpdaterTest extends \PHPUnit\Framework\TestCase
45 } 39 }
46 40
47 /** 41 /**
48 * Test read_updates_file with an empty/missing file. 42 * Test UpdaterUtils::read_updates_file with an empty/missing file.
49 */ 43 */
50 public function testReadEmptyUpdatesFile() 44 public function testReadEmptyUpdatesFile()
51 { 45 {
52 $this->assertEquals(array(), read_updates_file('')); 46 $this->assertEquals(array(), UpdaterUtils::read_updates_file(''));
53 $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt'; 47 $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt';
54 touch($updatesFile); 48 touch($updatesFile);
55 $this->assertEquals(array(), read_updates_file($updatesFile)); 49 $this->assertEquals(array(), UpdaterUtils::read_updates_file($updatesFile));
56 unlink($updatesFile); 50 unlink($updatesFile);
57 } 51 }
58 52
@@ -64,31 +58,31 @@ class UpdaterTest extends \PHPUnit\Framework\TestCase
64 $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt'; 58 $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt';
65 $updatesMethods = array('m1', 'm2', 'm3'); 59 $updatesMethods = array('m1', 'm2', 'm3');
66 60
67 write_updates_file($updatesFile, $updatesMethods); 61 UpdaterUtils::write_updates_file($updatesFile, $updatesMethods);
68 $readMethods = read_updates_file($updatesFile); 62 $readMethods = UpdaterUtils::read_updates_file($updatesFile);
69 $this->assertEquals($readMethods, $updatesMethods); 63 $this->assertEquals($readMethods, $updatesMethods);
70 64
71 // Update 65 // Update
72 $updatesMethods[] = 'm4'; 66 $updatesMethods[] = 'm4';
73 write_updates_file($updatesFile, $updatesMethods); 67 UpdaterUtils::write_updates_file($updatesFile, $updatesMethods);
74 $readMethods = read_updates_file($updatesFile); 68 $readMethods = UpdaterUtils::read_updates_file($updatesFile);
75 $this->assertEquals($readMethods, $updatesMethods); 69 $this->assertEquals($readMethods, $updatesMethods);
76 unlink($updatesFile); 70 unlink($updatesFile);
77 } 71 }
78 72
79 /** 73 /**
80 * Test errors in write_updates_file(): empty updates file. 74 * Test errors in UpdaterUtils::write_updates_file(): empty updates file.
81 * 75 *
82 * @expectedException Exception 76 * @expectedException Exception
83 * @expectedExceptionMessageRegExp /Updates file path is not set(.*)/ 77 * @expectedExceptionMessageRegExp /Updates file path is not set(.*)/
84 */ 78 */
85 public function testWriteEmptyUpdatesFile() 79 public function testWriteEmptyUpdatesFile()
86 { 80 {
87 write_updates_file('', array('test')); 81 UpdaterUtils::write_updates_file('', array('test'));
88 } 82 }
89 83
90 /** 84 /**
91 * Test errors in write_updates_file(): not writable updates file. 85 * Test errors in UpdaterUtils::write_updates_file(): not writable updates file.
92 * 86 *
93 * @expectedException Exception 87 * @expectedException Exception
94 * @expectedExceptionMessageRegExp /Unable to write(.*)/ 88 * @expectedExceptionMessageRegExp /Unable to write(.*)/
@@ -99,7 +93,7 @@ class UpdaterTest extends \PHPUnit\Framework\TestCase
99 touch($updatesFile); 93 touch($updatesFile);
100 chmod($updatesFile, 0444); 94 chmod($updatesFile, 0444);
101 try { 95 try {
102 @write_updates_file($updatesFile, array('test')); 96 @UpdaterUtils::write_updates_file($updatesFile, array('test'));
103 } catch (Exception $e) { 97 } catch (Exception $e) {
104 unlink($updatesFile); 98 unlink($updatesFile);
105 throw $e; 99 throw $e;
@@ -173,660 +167,4 @@ class UpdaterTest extends \PHPUnit\Framework\TestCase
173 $updater = new DummyUpdater($updates, array(), $this->conf, true); 167 $updater = new DummyUpdater($updates, array(), $this->conf, true);
174 $updater->update(); 168 $updater->update();
175 } 169 }
176
177 /**
178 * Test update mergeDeprecatedConfig:
179 * 1. init a config file.
180 * 2. init a options.php file with update value.
181 * 3. merge.
182 * 4. check updated value in config file.
183 */
184 public function testUpdateMergeDeprecatedConfig()
185 {
186 $this->conf->setConfigFile('tests/utils/config/configPhp');
187 $this->conf->reset();
188
189 $optionsFile = 'tests/updater/options.php';
190 $options = '<?php
191$GLOBALS[\'privateLinkByDefault\'] = true;';
192 file_put_contents($optionsFile, $options);
193
194 // tmp config file.
195 $this->conf->setConfigFile('tests/updater/config');
196
197 // merge configs
198 $updater = new Updater(array(), array(), $this->conf, true);
199 // This writes a new config file in tests/updater/config.php
200 $updater->updateMethodMergeDeprecatedConfigFile();
201
202 // make sure updated field is changed
203 $this->conf->reload();
204 $this->assertTrue($this->conf->get('privacy.default_private_links'));
205 $this->assertFalse(is_file($optionsFile));
206 // Delete the generated file.
207 unlink($this->conf->getConfigFileExt());
208 }
209
210 /**
211 * Test mergeDeprecatedConfig in without options file.
212 */
213 public function testMergeDeprecatedConfigNoFile()
214 {
215 $updater = new Updater(array(), array(), $this->conf, true);
216 $updater->updateMethodMergeDeprecatedConfigFile();
217
218 $this->assertEquals('root', $this->conf->get('credentials.login'));
219 }
220
221 /**
222 * Test renameDashTags update method.
223 */
224 public function testRenameDashTags()
225 {
226 $refDB = new \ReferenceLinkDB();
227 $refDB->write(self::$testDatastore);
228 $linkDB = new LinkDB(self::$testDatastore, true, false);
229
230 $this->assertEmpty($linkDB->filterSearch(array('searchtags' => 'exclude')));
231 $updater = new Updater(array(), $linkDB, $this->conf, true);
232 $updater->updateMethodRenameDashTags();
233 $this->assertNotEmpty($linkDB->filterSearch(array('searchtags' => 'exclude')));
234 }
235
236 /**
237 * Convert old PHP config file to JSON config.
238 */
239 public function testConfigToJson()
240 {
241 $configFile = 'tests/utils/config/configPhp';
242 $this->conf->setConfigFile($configFile);
243 $this->conf->reset();
244
245 // The ConfigIO is initialized with ConfigPhp.
246 $this->assertTrue($this->conf->getConfigIO() instanceof ConfigPhp);
247
248 $updater = new Updater(array(), array(), $this->conf, false);
249 $done = $updater->updateMethodConfigToJson();
250 $this->assertTrue($done);
251
252 // The ConfigIO has been updated to ConfigJson.
253 $this->assertTrue($this->conf->getConfigIO() instanceof ConfigJson);
254 $this->assertTrue(file_exists($this->conf->getConfigFileExt()));
255
256 // Check JSON config data.
257 $this->conf->reload();
258 $this->assertEquals('root', $this->conf->get('credentials.login'));
259 $this->assertEquals('lala', $this->conf->get('redirector.url'));
260 $this->assertEquals('data/datastore.php', $this->conf->get('resource.datastore'));
261 $this->assertEquals('1', $this->conf->get('plugins.WALLABAG_VERSION'));
262
263 rename($configFile . '.save.php', $configFile . '.php');
264 unlink($this->conf->getConfigFileExt());
265 }
266
267 /**
268 * Launch config conversion update with an existing JSON file => nothing to do.
269 */
270 public function testConfigToJsonNothingToDo()
271 {
272 $filetime = filemtime($this->conf->getConfigFileExt());
273 $updater = new Updater(array(), array(), $this->conf, false);
274 $done = $updater->updateMethodConfigToJson();
275 $this->assertTrue($done);
276 $expected = filemtime($this->conf->getConfigFileExt());
277 $this->assertEquals($expected, $filetime);
278 }
279
280 /**
281 * Test escapeUnescapedConfig with valid data.
282 */
283 public function testEscapeConfig()
284 {
285 $sandbox = 'sandbox/config';
286 copy(self::$configFile . '.json.php', $sandbox . '.json.php');
287 $this->conf = new ConfigManager($sandbox);
288 $title = '<script>alert("title");</script>';
289 $headerLink = '<script>alert("header_link");</script>';
290 $this->conf->set('general.title', $title);
291 $this->conf->set('general.header_link', $headerLink);
292 $updater = new Updater(array(), array(), $this->conf, true);
293 $done = $updater->updateMethodEscapeUnescapedConfig();
294 $this->assertTrue($done);
295 $this->conf->reload();
296 $this->assertEquals(escape($title), $this->conf->get('general.title'));
297 $this->assertEquals(escape($headerLink), $this->conf->get('general.header_link'));
298 unlink($sandbox . '.json.php');
299 }
300
301 /**
302 * Test updateMethodApiSettings(): create default settings for the API (enabled + secret).
303 */
304 public function testUpdateApiSettings()
305 {
306 $confFile = 'sandbox/config';
307 copy(self::$configFile .'.json.php', $confFile .'.json.php');
308 $conf = new ConfigManager($confFile);
309 $updater = new Updater(array(), array(), $conf, true);
310
311 $this->assertFalse($conf->exists('api.enabled'));
312 $this->assertFalse($conf->exists('api.secret'));
313 $updater->updateMethodApiSettings();
314 $conf->reload();
315 $this->assertTrue($conf->get('api.enabled'));
316 $this->assertTrue($conf->exists('api.secret'));
317 unlink($confFile .'.json.php');
318 }
319
320 /**
321 * Test updateMethodApiSettings(): already set, do nothing.
322 */
323 public function testUpdateApiSettingsNothingToDo()
324 {
325 $confFile = 'sandbox/config';
326 copy(self::$configFile .'.json.php', $confFile .'.json.php');
327 $conf = new ConfigManager($confFile);
328 $conf->set('api.enabled', false);
329 $conf->set('api.secret', '');
330 $updater = new Updater(array(), array(), $conf, true);
331 $updater->updateMethodApiSettings();
332 $this->assertFalse($conf->get('api.enabled'));
333 $this->assertEmpty($conf->get('api.secret'));
334 unlink($confFile .'.json.php');
335 }
336
337 /**
338 * Test updateMethodDatastoreIds().
339 */
340 public function testDatastoreIds()
341 {
342 $links = array(
343 '20121206_182539' => array(
344 'linkdate' => '20121206_182539',
345 'title' => 'Geek and Poke',
346 'url' => 'http://geek-and-poke.com/',
347 'description' => 'desc',
348 'tags' => 'dev cartoon tag1 tag2 tag3 tag4 ',
349 'updated' => '20121206_190301',
350 'private' => false,
351 ),
352 '20121206_172539' => array(
353 'linkdate' => '20121206_172539',
354 'title' => 'UserFriendly - Samba',
355 'url' => 'http://ars.userfriendly.org/cartoons/?id=20010306',
356 'description' => '',
357 'tags' => 'samba cartoon web',
358 'private' => false,
359 ),
360 '20121206_142300' => array(
361 'linkdate' => '20121206_142300',
362 'title' => 'UserFriendly - Web Designer',
363 'url' => 'http://ars.userfriendly.org/cartoons/?id=20121206',
364 'description' => 'Naming conventions... #private',
365 'tags' => 'samba cartoon web',
366 'private' => true,
367 ),
368 );
369 $refDB = new \ReferenceLinkDB();
370 $refDB->setLinks($links);
371 $refDB->write(self::$testDatastore);
372 $linkDB = new LinkDB(self::$testDatastore, true, false);
373
374 $checksum = hash_file('sha1', self::$testDatastore);
375
376 $this->conf->set('resource.data_dir', 'sandbox');
377 $this->conf->set('resource.datastore', self::$testDatastore);
378
379 $updater = new Updater(array(), $linkDB, $this->conf, true);
380 $this->assertTrue($updater->updateMethodDatastoreIds());
381
382 $linkDB = new LinkDB(self::$testDatastore, true, false);
383
384 $backup = glob($this->conf->get('resource.data_dir') . '/datastore.'. date('YmdH') .'*.php');
385 $backup = $backup[0];
386
387 $this->assertFileExists($backup);
388 $this->assertEquals($checksum, hash_file('sha1', $backup));
389 unlink($backup);
390
391 $this->assertEquals(3, count($linkDB));
392 $this->assertTrue(isset($linkDB[0]));
393 $this->assertFalse(isset($linkDB[0]['linkdate']));
394 $this->assertEquals(0, $linkDB[0]['id']);
395 $this->assertEquals('UserFriendly - Web Designer', $linkDB[0]['title']);
396 $this->assertEquals('http://ars.userfriendly.org/cartoons/?id=20121206', $linkDB[0]['url']);
397 $this->assertEquals('Naming conventions... #private', $linkDB[0]['description']);
398 $this->assertEquals('samba cartoon web', $linkDB[0]['tags']);
399 $this->assertTrue($linkDB[0]['private']);
400 $this->assertEquals(
401 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_142300'),
402 $linkDB[0]['created']
403 );
404
405 $this->assertTrue(isset($linkDB[1]));
406 $this->assertFalse(isset($linkDB[1]['linkdate']));
407 $this->assertEquals(1, $linkDB[1]['id']);
408 $this->assertEquals('UserFriendly - Samba', $linkDB[1]['title']);
409 $this->assertEquals(
410 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_172539'),
411 $linkDB[1]['created']
412 );
413
414 $this->assertTrue(isset($linkDB[2]));
415 $this->assertFalse(isset($linkDB[2]['linkdate']));
416 $this->assertEquals(2, $linkDB[2]['id']);
417 $this->assertEquals('Geek and Poke', $linkDB[2]['title']);
418 $this->assertEquals(
419 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_182539'),
420 $linkDB[2]['created']
421 );
422 $this->assertEquals(
423 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_190301'),
424 $linkDB[2]['updated']
425 );
426 }
427
428 /**
429 * Test updateMethodDatastoreIds() with the update already applied: nothing to do.
430 */
431 public function testDatastoreIdsNothingToDo()
432 {
433 $refDB = new \ReferenceLinkDB();
434 $refDB->write(self::$testDatastore);
435 $linkDB = new LinkDB(self::$testDatastore, true, false);
436
437 $this->conf->set('resource.data_dir', 'sandbox');
438 $this->conf->set('resource.datastore', self::$testDatastore);
439
440 $checksum = hash_file('sha1', self::$testDatastore);
441 $updater = new Updater(array(), $linkDB, $this->conf, true);
442 $this->assertTrue($updater->updateMethodDatastoreIds());
443 $this->assertEquals($checksum, hash_file('sha1', self::$testDatastore));
444 }
445
446 /**
447 * Test defaultTheme update with default settings: nothing to do.
448 */
449 public function testDefaultThemeWithDefaultSettings()
450 {
451 $sandbox = 'sandbox/config';
452 copy(self::$configFile . '.json.php', $sandbox . '.json.php');
453 $this->conf = new ConfigManager($sandbox);
454 $updater = new Updater([], [], $this->conf, true);
455 $this->assertTrue($updater->updateMethodDefaultTheme());
456
457 $this->assertEquals('tpl/', $this->conf->get('resource.raintpl_tpl'));
458 $this->assertEquals('default', $this->conf->get('resource.theme'));
459 $this->conf = new ConfigManager($sandbox);
460 $this->assertEquals('tpl/', $this->conf->get('resource.raintpl_tpl'));
461 $this->assertEquals('default', $this->conf->get('resource.theme'));
462 unlink($sandbox . '.json.php');
463 }
464
465 /**
466 * Test defaultTheme update with a custom theme in a subfolder
467 */
468 public function testDefaultThemeWithCustomTheme()
469 {
470 $theme = 'iamanartist';
471 $sandbox = 'sandbox/config';
472 copy(self::$configFile . '.json.php', $sandbox . '.json.php');
473 $this->conf = new ConfigManager($sandbox);
474 mkdir('sandbox/'. $theme);
475 touch('sandbox/'. $theme .'/linklist.html');
476 $this->conf->set('resource.raintpl_tpl', 'sandbox/'. $theme .'/');
477 $updater = new Updater([], [], $this->conf, true);
478 $this->assertTrue($updater->updateMethodDefaultTheme());
479
480 $this->assertEquals('sandbox', $this->conf->get('resource.raintpl_tpl'));
481 $this->assertEquals($theme, $this->conf->get('resource.theme'));
482 $this->conf = new ConfigManager($sandbox);
483 $this->assertEquals('sandbox', $this->conf->get('resource.raintpl_tpl'));
484 $this->assertEquals($theme, $this->conf->get('resource.theme'));
485 unlink($sandbox . '.json.php');
486 unlink('sandbox/'. $theme .'/linklist.html');
487 rmdir('sandbox/'. $theme);
488 }
489
490 /**
491 * Test updateMethodEscapeMarkdown with markdown plugin enabled
492 * => setting markdown_escape set to false.
493 */
494 public function testEscapeMarkdownSettingToFalse()
495 {
496 $sandboxConf = 'sandbox/config';
497 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
498 $this->conf = new ConfigManager($sandboxConf);
499
500 $this->conf->set('general.enabled_plugins', ['markdown']);
501 $updater = new Updater([], [], $this->conf, true);
502 $this->assertTrue($updater->updateMethodEscapeMarkdown());
503 $this->assertFalse($this->conf->get('security.markdown_escape'));
504
505 // reload from file
506 $this->conf = new ConfigManager($sandboxConf);
507 $this->assertFalse($this->conf->get('security.markdown_escape'));
508 }
509
510
511 /**
512 * Test updateMethodEscapeMarkdown with markdown plugin disabled
513 * => setting markdown_escape set to true.
514 */
515 public function testEscapeMarkdownSettingToTrue()
516 {
517 $sandboxConf = 'sandbox/config';
518 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
519 $this->conf = new ConfigManager($sandboxConf);
520
521 $this->conf->set('general.enabled_plugins', []);
522 $updater = new Updater([], [], $this->conf, true);
523 $this->assertTrue($updater->updateMethodEscapeMarkdown());
524 $this->assertTrue($this->conf->get('security.markdown_escape'));
525
526 // reload from file
527 $this->conf = new ConfigManager($sandboxConf);
528 $this->assertTrue($this->conf->get('security.markdown_escape'));
529 }
530
531 /**
532 * Test updateMethodEscapeMarkdown with nothing to do (setting already enabled)
533 */
534 public function testEscapeMarkdownSettingNothingToDoEnabled()
535 {
536 $sandboxConf = 'sandbox/config';
537 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
538 $this->conf = new ConfigManager($sandboxConf);
539 $this->conf->set('security.markdown_escape', true);
540 $updater = new Updater([], [], $this->conf, true);
541 $this->assertTrue($updater->updateMethodEscapeMarkdown());
542 $this->assertTrue($this->conf->get('security.markdown_escape'));
543 }
544
545 /**
546 * Test updateMethodEscapeMarkdown with nothing to do (setting already disabled)
547 */
548 public function testEscapeMarkdownSettingNothingToDoDisabled()
549 {
550 $this->conf->set('security.markdown_escape', false);
551 $updater = new Updater([], [], $this->conf, true);
552 $this->assertTrue($updater->updateMethodEscapeMarkdown());
553 $this->assertFalse($this->conf->get('security.markdown_escape'));
554 }
555
556 /**
557 * Test updateMethodPiwikUrl with valid data
558 */
559 public function testUpdatePiwikUrlValid()
560 {
561 $sandboxConf = 'sandbox/config';
562 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
563 $this->conf = new ConfigManager($sandboxConf);
564 $url = 'mypiwik.tld';
565 $this->conf->set('plugins.PIWIK_URL', $url);
566 $updater = new Updater([], [], $this->conf, true);
567 $this->assertTrue($updater->updateMethodPiwikUrl());
568 $this->assertEquals('http://'. $url, $this->conf->get('plugins.PIWIK_URL'));
569
570 // reload from file
571 $this->conf = new ConfigManager($sandboxConf);
572 $this->assertEquals('http://'. $url, $this->conf->get('plugins.PIWIK_URL'));
573 }
574
575 /**
576 * Test updateMethodPiwikUrl without setting
577 */
578 public function testUpdatePiwikUrlEmpty()
579 {
580 $updater = new Updater([], [], $this->conf, true);
581 $this->assertTrue($updater->updateMethodPiwikUrl());
582 $this->assertEmpty($this->conf->get('plugins.PIWIK_URL'));
583 }
584
585 /**
586 * Test updateMethodPiwikUrl: valid URL, nothing to do
587 */
588 public function testUpdatePiwikUrlNothingToDo()
589 {
590 $url = 'https://mypiwik.tld';
591 $this->conf->set('plugins.PIWIK_URL', $url);
592 $updater = new Updater([], [], $this->conf, true);
593 $this->assertTrue($updater->updateMethodPiwikUrl());
594 $this->assertEquals($url, $this->conf->get('plugins.PIWIK_URL'));
595 }
596
597 /**
598 * Test updateMethodAtomDefault with show_atom set to false
599 * => update to true.
600 */
601 public function testUpdateMethodAtomDefault()
602 {
603 $sandboxConf = 'sandbox/config';
604 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
605 $this->conf = new ConfigManager($sandboxConf);
606 $this->conf->set('feed.show_atom', false);
607 $updater = new Updater([], [], $this->conf, true);
608 $this->assertTrue($updater->updateMethodAtomDefault());
609 $this->assertTrue($this->conf->get('feed.show_atom'));
610 // reload from file
611 $this->conf = new ConfigManager($sandboxConf);
612 $this->assertTrue($this->conf->get('feed.show_atom'));
613 }
614 /**
615 * Test updateMethodAtomDefault with show_atom not set.
616 * => nothing to do
617 */
618 public function testUpdateMethodAtomDefaultNoExist()
619 {
620 $sandboxConf = 'sandbox/config';
621 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
622 $this->conf = new ConfigManager($sandboxConf);
623 $updater = new Updater([], [], $this->conf, true);
624 $this->assertTrue($updater->updateMethodAtomDefault());
625 $this->assertTrue($this->conf->get('feed.show_atom'));
626 }
627 /**
628 * Test updateMethodAtomDefault with show_atom set to true.
629 * => nothing to do
630 */
631 public function testUpdateMethodAtomDefaultAlreadyTrue()
632 {
633 $sandboxConf = 'sandbox/config';
634 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
635 $this->conf = new ConfigManager($sandboxConf);
636 $this->conf->set('feed.show_atom', true);
637 $updater = new Updater([], [], $this->conf, true);
638 $this->assertTrue($updater->updateMethodAtomDefault());
639 $this->assertTrue($this->conf->get('feed.show_atom'));
640 }
641
642 /**
643 * Test updateMethodDownloadSizeAndTimeoutConf, it should be set if none is already defined.
644 */
645 public function testUpdateMethodDownloadSizeAndTimeoutConf()
646 {
647 $sandboxConf = 'sandbox/config';
648 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
649 $this->conf = new ConfigManager($sandboxConf);
650 $updater = new Updater([], [], $this->conf, true);
651 $this->assertTrue($updater->updateMethodDownloadSizeAndTimeoutConf());
652 $this->assertEquals(4194304, $this->conf->get('general.download_max_size'));
653 $this->assertEquals(30, $this->conf->get('general.download_timeout'));
654
655 $this->conf = new ConfigManager($sandboxConf);
656 $this->assertEquals(4194304, $this->conf->get('general.download_max_size'));
657 $this->assertEquals(30, $this->conf->get('general.download_timeout'));
658 }
659
660 /**
661 * Test updateMethodDownloadSizeAndTimeoutConf, it shouldn't be set if it is already defined.
662 */
663 public function testUpdateMethodDownloadSizeAndTimeoutConfIgnore()
664 {
665 $sandboxConf = 'sandbox/config';
666 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
667 $this->conf = new ConfigManager($sandboxConf);
668 $this->conf->set('general.download_max_size', 38);
669 $this->conf->set('general.download_timeout', 70);
670 $updater = new Updater([], [], $this->conf, true);
671 $this->assertTrue($updater->updateMethodDownloadSizeAndTimeoutConf());
672 $this->assertEquals(38, $this->conf->get('general.download_max_size'));
673 $this->assertEquals(70, $this->conf->get('general.download_timeout'));
674 }
675
676 /**
677 * Test updateMethodDownloadSizeAndTimeoutConf, only the maz size should be set here.
678 */
679 public function testUpdateMethodDownloadSizeAndTimeoutConfOnlySize()
680 {
681 $sandboxConf = 'sandbox/config';
682 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
683 $this->conf = new ConfigManager($sandboxConf);
684 $this->conf->set('general.download_max_size', 38);
685 $updater = new Updater([], [], $this->conf, true);
686 $this->assertTrue($updater->updateMethodDownloadSizeAndTimeoutConf());
687 $this->assertEquals(38, $this->conf->get('general.download_max_size'));
688 $this->assertEquals(30, $this->conf->get('general.download_timeout'));
689 }
690
691 /**
692 * Test updateMethodDownloadSizeAndTimeoutConf, only the time out should be set here.
693 */
694 public function testUpdateMethodDownloadSizeAndTimeoutConfOnlyTimeout()
695 {
696 $sandboxConf = 'sandbox/config';
697 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
698 $this->conf = new ConfigManager($sandboxConf);
699 $this->conf->set('general.download_timeout', 3);
700 $updater = new Updater([], [], $this->conf, true);
701 $this->assertTrue($updater->updateMethodDownloadSizeAndTimeoutConf());
702 $this->assertEquals(4194304, $this->conf->get('general.download_max_size'));
703 $this->assertEquals(3, $this->conf->get('general.download_timeout'));
704 }
705
706 /**
707 * Test updateMethodWebThumbnailer with thumbnails enabled.
708 */
709 public function testUpdateMethodWebThumbnailerEnabled()
710 {
711 $this->conf->remove('thumbnails');
712 $this->conf->set('thumbnail.enable_thumbnails', true);
713 $updater = new Updater([], [], $this->conf, true, $_SESSION);
714 $this->assertTrue($updater->updateMethodWebThumbnailer());
715 $this->assertFalse($this->conf->exists('thumbnail'));
716 $this->assertEquals(\Shaarli\Thumbnailer::MODE_ALL, $this->conf->get('thumbnails.mode'));
717 $this->assertEquals(125, $this->conf->get('thumbnails.width'));
718 $this->assertEquals(90, $this->conf->get('thumbnails.height'));
719 $this->assertContains('You have enabled or changed thumbnails', $_SESSION['warnings'][0]);
720 }
721
722 /**
723 * Test updateMethodWebThumbnailer with thumbnails disabled.
724 */
725 public function testUpdateMethodWebThumbnailerDisabled()
726 {
727 if (isset($_SESSION['warnings'])) {
728 unset($_SESSION['warnings']);
729 }
730 $this->conf->remove('thumbnails');
731 $this->conf->set('thumbnail.enable_thumbnails', false);
732 $updater = new Updater([], [], $this->conf, true, $_SESSION);
733 $this->assertTrue($updater->updateMethodWebThumbnailer());
734 $this->assertFalse($this->conf->exists('thumbnail'));
735 $this->assertEquals(Thumbnailer::MODE_NONE, $this->conf->get('thumbnails.mode'));
736 $this->assertEquals(125, $this->conf->get('thumbnails.width'));
737 $this->assertEquals(90, $this->conf->get('thumbnails.height'));
738 $this->assertTrue(empty($_SESSION['warnings']));
739 }
740
741 /**
742 * Test updateMethodWebThumbnailer with thumbnails disabled.
743 */
744 public function testUpdateMethodWebThumbnailerNothingToDo()
745 {
746 if (isset($_SESSION['warnings'])) {
747 unset($_SESSION['warnings']);
748 }
749 $updater = new Updater([], [], $this->conf, true, $_SESSION);
750 $this->assertTrue($updater->updateMethodWebThumbnailer());
751 $this->assertFalse($this->conf->exists('thumbnail'));
752 $this->assertEquals(Thumbnailer::MODE_COMMON, $this->conf->get('thumbnails.mode'));
753 $this->assertEquals(90, $this->conf->get('thumbnails.width'));
754 $this->assertEquals(53, $this->conf->get('thumbnails.height'));
755 $this->assertTrue(empty($_SESSION['warnings']));
756 }
757
758 /**
759 * Test updateMethodSetSticky().
760 */
761 public function testUpdateStickyValid()
762 {
763 $blank = [
764 'id' => 1,
765 'url' => 'z',
766 'title' => '',
767 'description' => '',
768 'tags' => '',
769 'created' => new DateTime(),
770 ];
771 $links = [
772 1 => ['id' => 1] + $blank,
773 2 => ['id' => 2] + $blank,
774 ];
775 $refDB = new \ReferenceLinkDB();
776 $refDB->setLinks($links);
777 $refDB->write(self::$testDatastore);
778 $linkDB = new LinkDB(self::$testDatastore, true, false);
779
780 $updater = new Updater(array(), $linkDB, $this->conf, true);
781 $this->assertTrue($updater->updateMethodSetSticky());
782
783 $linkDB = new LinkDB(self::$testDatastore, true, false);
784 foreach ($linkDB as $link) {
785 $this->assertFalse($link['sticky']);
786 }
787 }
788
789 /**
790 * Test updateMethodSetSticky().
791 */
792 public function testUpdateStickyNothingToDo()
793 {
794 $blank = [
795 'id' => 1,
796 'url' => 'z',
797 'title' => '',
798 'description' => '',
799 'tags' => '',
800 'created' => new DateTime(),
801 ];
802 $links = [
803 1 => ['id' => 1, 'sticky' => true] + $blank,
804 2 => ['id' => 2] + $blank,
805 ];
806 $refDB = new \ReferenceLinkDB();
807 $refDB->setLinks($links);
808 $refDB->write(self::$testDatastore);
809 $linkDB = new LinkDB(self::$testDatastore, true, false);
810
811 $updater = new Updater(array(), $linkDB, $this->conf, true);
812 $this->assertTrue($updater->updateMethodSetSticky());
813
814 $linkDB = new LinkDB(self::$testDatastore, true, false);
815 $this->assertTrue($linkDB[1]['sticky']);
816 }
817
818 /**
819 * Test updateMethodRemoveRedirector().
820 */
821 public function testUpdateRemoveRedirector()
822 {
823 $sandboxConf = 'sandbox/config';
824 copy(self::$configFile . '.json.php', $sandboxConf . '.json.php');
825 $this->conf = new ConfigManager($sandboxConf);
826 $updater = new Updater([], null, $this->conf, true);
827 $this->assertTrue($updater->updateMethodRemoveRedirector());
828 $this->assertFalse($this->conf->exists('redirector'));
829 $this->conf = new ConfigManager($sandboxConf);
830 $this->assertFalse($this->conf->exists('redirector'));
831 }
832} 170}
diff --git a/tests/utils/FakeBookmarkService.php b/tests/utils/FakeBookmarkService.php
new file mode 100644
index 00000000..1ec5bc3d
--- /dev/null
+++ b/tests/utils/FakeBookmarkService.php
@@ -0,0 +1,18 @@
1<?php
2
3
4use Shaarli\Bookmark\BookmarkArray;
5use Shaarli\Bookmark\BookmarkFilter;
6use Shaarli\Bookmark\BookmarkIO;
7use Shaarli\Bookmark\BookmarkFileService;
8use Shaarli\Bookmark\Exception\EmptyDataStoreException;
9use Shaarli\Config\ConfigManager;
10use Shaarli\History;
11
12class FakeBookmarkService extends BookmarkFileService
13{
14 public function getBookmarks()
15 {
16 return $this->bookmarks;
17 }
18}
diff --git a/tests/utils/ReferenceHistory.php b/tests/utils/ReferenceHistory.php
index e411c417..516c9f51 100644
--- a/tests/utils/ReferenceHistory.php
+++ b/tests/utils/ReferenceHistory.php
@@ -76,7 +76,7 @@ class ReferenceHistory
76 } 76 }
77 77
78 /** 78 /**
79 * Returns the number of links in the reference data 79 * Returns the number of bookmarks in the reference data
80 */ 80 */
81 public function count() 81 public function count()
82 { 82 {
diff --git a/tests/utils/ReferenceLinkDB.php b/tests/utils/ReferenceLinkDB.php
index c12bcb67..0095f5a1 100644
--- a/tests/utils/ReferenceLinkDB.php
+++ b/tests/utils/ReferenceLinkDB.php
@@ -1,30 +1,39 @@
1<?php 1<?php
2 2
3use Shaarli\Bookmark\LinkDB; 3use Shaarli\Bookmark\Bookmark;
4use Shaarli\Bookmark\BookmarkArray;
4 5
5/** 6/**
6 * Populates a reference datastore to test LinkDB 7 * Populates a reference datastore to test Bookmark
7 */ 8 */
8class ReferenceLinkDB 9class ReferenceLinkDB
9{ 10{
10 public static $NB_LINKS_TOTAL = 11; 11 public static $NB_LINKS_TOTAL = 11;
11 12
12 private $_links = array(); 13 private $bookmarks = array();
13 private $_publicCount = 0; 14 private $_publicCount = 0;
14 private $_privateCount = 0; 15 private $_privateCount = 0;
15 16
17 private $isLegacy;
18
16 /** 19 /**
17 * Populates the test DB with reference data 20 * Populates the test DB with reference data
21 *
22 * @param bool $isLegacy Use links as array instead of Bookmark object
18 */ 23 */
19 public function __construct() 24 public function __construct($isLegacy = false)
20 { 25 {
26 $this->isLegacy = $isLegacy;
27 if (! $this->isLegacy) {
28 $this->bookmarks = new BookmarkArray();
29 }
21 $this->addLink( 30 $this->addLink(
22 11, 31 11,
23 'Pined older', 32 'Pined older',
24 '?PCRizQ', 33 '?PCRizQ',
25 'This is an older pinned link', 34 'This is an older pinned link',
26 0, 35 0,
27 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20100309_101010'), 36 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20100309_101010'),
28 '', 37 '',
29 null, 38 null,
30 'PCRizQ', 39 'PCRizQ',
@@ -37,7 +46,7 @@ class ReferenceLinkDB
37 '?0gCTjQ', 46 '?0gCTjQ',
38 'This is a pinned link', 47 'This is a pinned link',
39 0, 48 0,
40 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121207_152312'), 49 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20121207_152312'),
41 '', 50 '',
42 null, 51 null,
43 '0gCTjQ', 52 '0gCTjQ',
@@ -50,7 +59,7 @@ class ReferenceLinkDB
50 '?WDWyig', 59 '?WDWyig',
51 'Stallman has a beard and is part of the Free Software Foundation (or not). Seriously, read this. #hashtag', 60 'Stallman has a beard and is part of the Free Software Foundation (or not). Seriously, read this. #hashtag',
52 0, 61 0,
53 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114651'), 62 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114651'),
54 'sTuff', 63 'sTuff',
55 null, 64 null,
56 'WDWyig' 65 'WDWyig'
@@ -60,9 +69,9 @@ class ReferenceLinkDB
60 42, 69 42,
61 'Note: I have a big ID but an old date', 70 'Note: I have a big ID but an old date',
62 '?WDWyig', 71 '?WDWyig',
63 'Used to test links reordering.', 72 'Used to test bookmarks reordering.',
64 0, 73 0,
65 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20100310_101010'), 74 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20100310_101010'),
66 'ut' 75 'ut'
67 ); 76 );
68 77
@@ -72,7 +81,7 @@ class ReferenceLinkDB
72 'http://www.php-fig.org/psr/psr-2/', 81 'http://www.php-fig.org/psr/psr-2/',
73 'This guide extends and expands on PSR-1, the basic coding standard.', 82 'This guide extends and expands on PSR-1, the basic coding standard.',
74 0, 83 0,
75 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_152312'), 84 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20121206_152312'),
76 '' 85 ''
77 ); 86 );
78 87
@@ -82,9 +91,9 @@ class ReferenceLinkDB
82 'https://static.fsf.org/nosvn/faif-2.0.pdf', 91 'https://static.fsf.org/nosvn/faif-2.0.pdf',
83 'Richard Stallman and the Free Software Revolution. Read this. #hashtag', 92 'Richard Stallman and the Free Software Revolution. Read this. #hashtag',
84 0, 93 0,
85 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114633'), 94 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150310_114633'),
86 'free gnu software stallman -exclude stuff hashtag', 95 'free gnu software stallman -exclude stuff hashtag',
87 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160803_093033') 96 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20160803_093033')
88 ); 97 );
89 98
90 $this->addLink( 99 $this->addLink(
@@ -93,9 +102,9 @@ class ReferenceLinkDB
93 'http://mediagoblin.org/', 102 'http://mediagoblin.org/',
94 'A free software media publishing platform #hashtagOther', 103 'A free software media publishing platform #hashtagOther',
95 0, 104 0,
96 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20130614_184135'), 105 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20130614_184135'),
97 'gnu media web .hidden hashtag', 106 'gnu media web .hidden hashtag',
98 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20130615_184230'), 107 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20130615_184230'),
99 'IuWvgA' 108 'IuWvgA'
100 ); 109 );
101 110
@@ -105,7 +114,7 @@ class ReferenceLinkDB
105 'https://dvcs.w3.org/hg/markup-validator/summary', 114 'https://dvcs.w3.org/hg/markup-validator/summary',
106 'Mercurial repository for the W3C Validator #private', 115 'Mercurial repository for the W3C Validator #private',
107 1, 116 1,
108 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20141125_084734'), 117 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20141125_084734'),
109 'css html w3c web Mercurial' 118 'css html w3c web Mercurial'
110 ); 119 );
111 120
@@ -115,7 +124,7 @@ class ReferenceLinkDB
115 'http://ars.userfriendly.org/cartoons/?id=20121206', 124 'http://ars.userfriendly.org/cartoons/?id=20121206',
116 'Naming conventions... #private', 125 'Naming conventions... #private',
117 0, 126 0,
118 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_142300'), 127 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20121206_142300'),
119 'dev cartoon web' 128 'dev cartoon web'
120 ); 129 );
121 130
@@ -125,7 +134,7 @@ class ReferenceLinkDB
125 'http://ars.userfriendly.org/cartoons/?id=20010306', 134 'http://ars.userfriendly.org/cartoons/?id=20010306',
126 'Tropical printing', 135 'Tropical printing',
127 0, 136 0,
128 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_172539'), 137 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20121206_172539'),
129 'samba cartoon web' 138 'samba cartoon web'
130 ); 139 );
131 140
@@ -135,7 +144,7 @@ class ReferenceLinkDB
135 'http://geek-and-poke.com/', 144 'http://geek-and-poke.com/',
136 '', 145 '',
137 1, 146 1,
138 DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_182539'), 147 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20121206_182539'),
139 'dev cartoon tag1 tag2 tag3 tag4 ' 148 'dev cartoon tag1 tag2 tag3 tag4 '
140 ); 149 );
141 } 150 }
@@ -164,10 +173,15 @@ class ReferenceLinkDB
164 'tags' => $tags, 173 'tags' => $tags,
165 'created' => $date, 174 'created' => $date,
166 'updated' => $updated, 175 'updated' => $updated,
167 'shorturl' => $shorturl ? $shorturl : smallHash($date->format(LinkDB::LINK_DATE_FORMAT) . $id), 176 'shorturl' => $shorturl ? $shorturl : smallHash($date->format(Bookmark::LINK_DATE_FORMAT) . $id),
168 'sticky' => $pinned 177 'sticky' => $pinned
169 ); 178 );
170 $this->_links[$id] = $link; 179 if (! $this->isLegacy) {
180 $bookmark = new Bookmark();
181 $this->bookmarks[$id] = $bookmark->fromArray($link);
182 } else {
183 $this->bookmarks[$id] = $link;
184 }
171 185
172 if ($private) { 186 if ($private) {
173 $this->_privateCount++; 187 $this->_privateCount++;
@@ -184,37 +198,38 @@ class ReferenceLinkDB
184 $this->reorder(); 198 $this->reorder();
185 file_put_contents( 199 file_put_contents(
186 $filename, 200 $filename,
187 '<?php /* '.base64_encode(gzdeflate(serialize($this->_links))).' */ ?>' 201 '<?php /* '.base64_encode(gzdeflate(serialize($this->bookmarks))).' */ ?>'
188 ); 202 );
189 } 203 }
190 204
191 /** 205 /**
192 * Reorder links by creation date (newest first). 206 * Reorder links by creation date (newest first).
193 * 207 *
194 * Also update the urls and ids mapping arrays.
195 *
196 * @param string $order ASC|DESC 208 * @param string $order ASC|DESC
197 */ 209 */
198 public function reorder($order = 'DESC') 210 public function reorder($order = 'DESC')
199 { 211 {
200 // backward compatibility: ignore reorder if the the `created` field doesn't exist 212 if (! $this->isLegacy) {
201 if (! isset(array_values($this->_links)[0]['created'])) { 213 $this->bookmarks->reorder($order);
202 return; 214 } else {
203 } 215 $order = $order === 'ASC' ? -1 : 1;
204 216 // backward compatibility: ignore reorder if the the `created` field doesn't exist
205 $order = $order === 'ASC' ? -1 : 1; 217 if (! isset(array_values($this->bookmarks)[0]['created'])) {
206 // Reorder array by dates. 218 return;
207 usort($this->_links, function ($a, $b) use ($order) {
208 if (isset($a['sticky']) && isset($b['sticky']) && $a['sticky'] !== $b['sticky']) {
209 return $a['sticky'] ? -1 : 1;
210 } 219 }
211 220
212 return $a['created'] < $b['created'] ? 1 * $order : -1 * $order; 221 usort($this->bookmarks, function ($a, $b) use ($order) {
213 }); 222 if (isset($a['sticky']) && isset($b['sticky']) && $a['sticky'] !== $b['sticky']) {
223 return $a['sticky'] ? -1 : 1;
224 }
225
226 return $a['created'] < $b['created'] ? 1 * $order : -1 * $order;
227 });
228 }
214 } 229 }
215 230
216 /** 231 /**
217 * Returns the number of links in the reference data 232 * Returns the number of bookmarks in the reference data
218 */ 233 */
219 public function countLinks() 234 public function countLinks()
220 { 235 {
@@ -222,7 +237,7 @@ class ReferenceLinkDB
222 } 237 }
223 238
224 /** 239 /**
225 * Returns the number of public links in the reference data 240 * Returns the number of public bookmarks in the reference data
226 */ 241 */
227 public function countPublicLinks() 242 public function countPublicLinks()
228 { 243 {
@@ -230,7 +245,7 @@ class ReferenceLinkDB
230 } 245 }
231 246
232 /** 247 /**
233 * Returns the number of private links in the reference data 248 * Returns the number of private bookmarks in the reference data
234 */ 249 */
235 public function countPrivateLinks() 250 public function countPrivateLinks()
236 { 251 {
@@ -238,14 +253,20 @@ class ReferenceLinkDB
238 } 253 }
239 254
240 /** 255 /**
241 * Returns the number of links without tag 256 * Returns the number of bookmarks without tag
242 */ 257 */
243 public function countUntaggedLinks() 258 public function countUntaggedLinks()
244 { 259 {
245 $cpt = 0; 260 $cpt = 0;
246 foreach ($this->_links as $link) { 261 foreach ($this->bookmarks as $link) {
247 if (empty($link['tags'])) { 262 if (! $this->isLegacy) {
248 ++$cpt; 263 if (empty($link->getTags())) {
264 ++$cpt;
265 }
266 } else {
267 if (empty($link['tags'])) {
268 ++$cpt;
269 }
249 } 270 }
250 } 271 }
251 return $cpt; 272 return $cpt;
@@ -254,16 +275,16 @@ class ReferenceLinkDB
254 public function getLinks() 275 public function getLinks()
255 { 276 {
256 $this->reorder(); 277 $this->reorder();
257 return $this->_links; 278 return $this->bookmarks;
258 } 279 }
259 280
260 /** 281 /**
261 * Setter to override link creation. 282 * Setter to override link creation.
262 * 283 *
263 * @param array $links List of links. 284 * @param array $links List of bookmarks.
264 */ 285 */
265 public function setLinks($links) 286 public function setLinks($links)
266 { 287 {
267 $this->_links = $links; 288 $this->bookmarks = $links;
268 } 289 }
269} 290}
diff --git a/tests/utils/config/configJson.json.php b/tests/utils/config/configJson.json.php
index 1549ddfc..b04dc303 100644
--- a/tests/utils/config/configJson.json.php
+++ b/tests/utils/config/configJson.json.php
@@ -41,12 +41,12 @@
41 "foo": "bar" 41 "foo": "bar"
42 }, 42 },
43 "resource": { 43 "resource": {
44 "datastore": "tests\/utils\/config\/datastore.php", 44 "datastore": "sandbox/datastore.php",
45 "data_dir": "sandbox\/", 45 "data_dir": "sandbox\/",
46 "raintpl_tpl": "tpl\/", 46 "raintpl_tpl": "tpl\/",
47 "config": "data\/config.php", 47 "config": "data\/config.php",
48 "ban_file": "data\/ipbans.php", 48 "ban_file": "data\/ipbans.php",
49 "updates": "data\/updates.txt", 49 "updates": "sandbox/updates.txt",
50 "log": "data\/log.txt", 50 "log": "data\/log.txt",
51 "update_check": "data\/lastupdatecheck.txt", 51 "update_check": "data\/lastupdatecheck.txt",
52 "history": "data\/history.php", 52 "history": "data\/history.php",
@@ -59,7 +59,7 @@
59 "WALLABAG_VERSION": 1 59 "WALLABAG_VERSION": 1
60 }, 60 },
61 "dev": { 61 "dev": {
62 "debug": true 62 "debug": false
63 }, 63 },
64 "updates": { 64 "updates": {
65 "check_updates": false, 65 "check_updates": false,