aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/netscape/BookmarkImportTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'tests/netscape/BookmarkImportTest.php')
-rw-r--r--tests/netscape/BookmarkImportTest.php608
1 files changed, 283 insertions, 325 deletions
diff --git a/tests/netscape/BookmarkImportTest.php b/tests/netscape/BookmarkImportTest.php
index ccafc161..c1e49b5f 100644
--- a/tests/netscape/BookmarkImportTest.php
+++ b/tests/netscape/BookmarkImportTest.php
@@ -1,26 +1,31 @@
1<?php 1<?php
2
2namespace Shaarli\Netscape; 3namespace Shaarli\Netscape;
3 4
4use DateTime; 5use DateTime;
5use Shaarli\Bookmark\LinkDB; 6use Psr\Http\Message\UploadedFileInterface;
7use Shaarli\Bookmark\Bookmark;
8use Shaarli\Bookmark\BookmarkFileService;
9use Shaarli\Bookmark\BookmarkFilter;
6use Shaarli\Config\ConfigManager; 10use Shaarli\Config\ConfigManager;
7use Shaarli\History; 11use Shaarli\History;
12use Shaarli\TestCase;
13use Slim\Http\UploadedFile;
8 14
9/** 15/**
10 * Utility function to load a file's metadata in a $_FILES-like array 16 * Utility function to load a file's metadata in a $_FILES-like array
11 * 17 *
12 * @param string $filename Basename of the file 18 * @param string $filename Basename of the file
13 * 19 *
14 * @return array A $_FILES-like array 20 * @return UploadedFileInterface Upload file in PSR-7 compatible object
15 */ 21 */
16function file2array($filename) 22function file2array($filename)
17{ 23{
18 return array( 24 return new UploadedFile(
19 'filetoupload' => array( 25 __DIR__ . '/input/' . $filename,
20 'name' => $filename, 26 $filename,
21 'tmp_name' => __DIR__ . '/input/' . $filename, 27 null,
22 'size' => filesize(__DIR__ . '/input/' . $filename) 28 filesize(__DIR__ . '/input/' . $filename)
23 )
24 ); 29 );
25} 30}
26 31
@@ -28,7 +33,7 @@ function file2array($filename)
28/** 33/**
29 * Netscape bookmark import 34 * Netscape bookmark import
30 */ 35 */
31class BookmarkImportTest extends \PHPUnit\Framework\TestCase 36class BookmarkImportTest extends TestCase
32{ 37{
33 /** 38 /**
34 * @var string datastore to test write operations 39 * @var string datastore to test write operations
@@ -41,9 +46,9 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
41 protected static $historyFilePath = 'sandbox/history.php'; 46 protected static $historyFilePath = 'sandbox/history.php';
42 47
43 /** 48 /**
44 * @var LinkDB private LinkDB instance 49 * @var BookmarkFileService private LinkDB instance
45 */ 50 */
46 protected $linkDb = null; 51 protected $bookmarkService = null;
47 52
48 /** 53 /**
49 * @var string Dummy page cache 54 * @var string Dummy page cache
@@ -61,11 +66,16 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
61 protected $history; 66 protected $history;
62 67
63 /** 68 /**
69 * @var NetscapeBookmarkUtils
70 */
71 protected $netscapeBookmarkUtils;
72
73 /**
64 * @var string Save the current timezone. 74 * @var string Save the current timezone.
65 */ 75 */
66 protected static $defaultTimeZone; 76 protected static $defaultTimeZone;
67 77
68 public static function setUpBeforeClass() 78 public static function setUpBeforeClass(): void
69 { 79 {
70 self::$defaultTimeZone = date_default_timezone_get(); 80 self::$defaultTimeZone = date_default_timezone_get();
71 // Timezone without DST for test consistency 81 // Timezone without DST for test consistency
@@ -75,28 +85,31 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
75 /** 85 /**
76 * Resets test data before each test 86 * Resets test data before each test
77 */ 87 */
78 protected function setUp() 88 protected function setUp(): void
79 { 89 {
80 if (file_exists(self::$testDatastore)) { 90 if (file_exists(self::$testDatastore)) {
81 unlink(self::$testDatastore); 91 unlink(self::$testDatastore);
82 } 92 }
83 // start with an empty datastore 93 // start with an empty datastore
84 file_put_contents(self::$testDatastore, '<?php /* S7QysKquBQA= */ ?>'); 94 file_put_contents(self::$testDatastore, '<?php /* S7QysKquBQA= */ ?>');
85 $this->linkDb = new LinkDB(self::$testDatastore, true, false); 95
86 $this->conf = new ConfigManager('tests/utils/config/configJson'); 96 $this->conf = new ConfigManager('tests/utils/config/configJson');
87 $this->conf->set('resource.page_cache', $this->pagecache); 97 $this->conf->set('resource.page_cache', $this->pagecache);
98 $this->conf->set('resource.datastore', self::$testDatastore);
88 $this->history = new History(self::$historyFilePath); 99 $this->history = new History(self::$historyFilePath);
100 $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, true);
101 $this->netscapeBookmarkUtils = new NetscapeBookmarkUtils($this->bookmarkService, $this->conf, $this->history);
89 } 102 }
90 103
91 /** 104 /**
92 * Delete history file. 105 * Delete history file.
93 */ 106 */
94 public function tearDown() 107 protected function tearDown(): void
95 { 108 {
96 @unlink(self::$historyFilePath); 109 @unlink(self::$historyFilePath);
97 } 110 }
98 111
99 public static function tearDownAfterClass() 112 public static function tearDownAfterClass(): void
100 { 113 {
101 date_default_timezone_set(self::$defaultTimeZone); 114 date_default_timezone_set(self::$defaultTimeZone);
102 } 115 }
@@ -110,9 +123,9 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
110 $this->assertEquals( 123 $this->assertEquals(
111 'File empty.htm (0 bytes) has an unknown file format.' 124 'File empty.htm (0 bytes) has an unknown file format.'
112 .' Nothing was imported.', 125 .' Nothing was imported.',
113 NetscapeBookmarkUtils::import(null, $files, null, $this->conf, $this->history) 126 $this->netscapeBookmarkUtils->import(null, $files)
114 ); 127 );
115 $this->assertEquals(0, count($this->linkDb)); 128 $this->assertEquals(0, $this->bookmarkService->count());
116 } 129 }
117 130
118 /** 131 /**
@@ -123,9 +136,9 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
123 $files = file2array('no_doctype.htm'); 136 $files = file2array('no_doctype.htm');
124 $this->assertEquals( 137 $this->assertEquals(
125 'File no_doctype.htm (350 bytes) has an unknown file format. Nothing was imported.', 138 'File no_doctype.htm (350 bytes) has an unknown file format. Nothing was imported.',
126 NetscapeBookmarkUtils::import(null, $files, null, $this->conf, $this->history) 139 $this->netscapeBookmarkUtils->import(null, $files)
127 ); 140 );
128 $this->assertEquals(0, count($this->linkDb)); 141 $this->assertEquals(0, $this->bookmarkService->count());
129 } 142 }
130 143
131 /** 144 /**
@@ -136,10 +149,10 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
136 $files = file2array('lowercase_doctype.htm'); 149 $files = file2array('lowercase_doctype.htm');
137 $this->assertStringMatchesFormat( 150 $this->assertStringMatchesFormat(
138 'File lowercase_doctype.htm (386 bytes) was successfully processed in %d seconds:' 151 'File lowercase_doctype.htm (386 bytes) was successfully processed in %d seconds:'
139 .' 2 links imported, 0 links overwritten, 0 links skipped.', 152 .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
140 NetscapeBookmarkUtils::import(null, $files, $this->linkDb, $this->conf, $this->history) 153 $this->netscapeBookmarkUtils->import(null, $files)
141 ); 154 );
142 $this->assertEquals(2, count($this->linkDb)); 155 $this->assertEquals(2, $this->bookmarkService->count());
143 } 156 }
144 157
145 158
@@ -151,25 +164,24 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
151 $files = file2array('internet_explorer_encoding.htm'); 164 $files = file2array('internet_explorer_encoding.htm');
152 $this->assertStringMatchesFormat( 165 $this->assertStringMatchesFormat(
153 'File internet_explorer_encoding.htm (356 bytes) was successfully processed in %d seconds:' 166 'File internet_explorer_encoding.htm (356 bytes) was successfully processed in %d seconds:'
154 .' 1 links imported, 0 links overwritten, 0 links skipped.', 167 .' 1 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
155 NetscapeBookmarkUtils::import([], $files, $this->linkDb, $this->conf, $this->history) 168 $this->netscapeBookmarkUtils->import([], $files)
156 ); 169 );
157 $this->assertEquals(1, count($this->linkDb)); 170 $this->assertEquals(1, $this->bookmarkService->count());
158 $this->assertEquals(0, count_private($this->linkDb)); 171 $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
159 172
173 $bookmark = $this->bookmarkService->findByUrl('http://hginit.com/');
174 $this->assertEquals(0, $bookmark->getId());
160 $this->assertEquals( 175 $this->assertEquals(
161 array( 176 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20160618_203944'),
162 'id' => 0, 177 $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 ); 178 );
179 $this->assertEquals('Hg Init a Mercurial tutorial by Joel Spolsky', $bookmark->getTitle());
180 $this->assertEquals('http://hginit.com/', $bookmark->getUrl());
181 $this->assertEquals('', $bookmark->getDescription());
182 $this->assertFalse($bookmark->isPrivate());
183 $this->assertEquals('', $bookmark->getTagsString());
184 $this->assertEquals('La37cg', $bookmark->getShortUrl());
173 } 185 }
174 186
175 /** 187 /**
@@ -180,116 +192,115 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
180 $files = file2array('netscape_nested.htm'); 192 $files = file2array('netscape_nested.htm');
181 $this->assertStringMatchesFormat( 193 $this->assertStringMatchesFormat(
182 'File netscape_nested.htm (1337 bytes) was successfully processed in %d seconds:' 194 'File netscape_nested.htm (1337 bytes) was successfully processed in %d seconds:'
183 .' 8 links imported, 0 links overwritten, 0 links skipped.', 195 .' 8 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
184 NetscapeBookmarkUtils::import([], $files, $this->linkDb, $this->conf, $this->history) 196 $this->netscapeBookmarkUtils->import([], $files)
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 ); 197 );
198 $this->assertEquals(8, $this->bookmarkService->count());
199 $this->assertEquals(2, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
200
201 $bookmark = $this->bookmarkService->findByUrl('http://nest.ed/1');
202 $this->assertEquals(0, $bookmark->getId());
203 $this->assertEquals(
204 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20160225_235541'),
205 $bookmark->getCreated()
206 );
207 $this->assertEquals('Nested 1', $bookmark->getTitle());
208 $this->assertEquals('http://nest.ed/1', $bookmark->getUrl());
209 $this->assertEquals('', $bookmark->getDescription());
210 $this->assertFalse($bookmark->isPrivate());
211 $this->assertEquals('tag1 tag2', $bookmark->getTagsString());
212 $this->assertEquals('KyDNKA', $bookmark->getShortUrl());
213
214 $bookmark = $this->bookmarkService->findByUrl('http://nest.ed/1-1');
215 $this->assertEquals(1, $bookmark->getId());
216 $this->assertEquals(
217 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20160225_235542'),
218 $bookmark->getCreated()
219 );
220 $this->assertEquals('Nested 1-1', $bookmark->getTitle());
221 $this->assertEquals('http://nest.ed/1-1', $bookmark->getUrl());
222 $this->assertEquals('', $bookmark->getDescription());
223 $this->assertFalse($bookmark->isPrivate());
224 $this->assertEquals('folder1 tag1 tag2', $bookmark->getTagsString());
225 $this->assertEquals('T2LnXg', $bookmark->getShortUrl());
226
227 $bookmark = $this->bookmarkService->findByUrl('http://nest.ed/1-2');
228 $this->assertEquals(2, $bookmark->getId());
229 $this->assertEquals(
230 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20160225_235547'),
231 $bookmark->getCreated()
232 );
233 $this->assertEquals('Nested 1-2', $bookmark->getTitle());
234 $this->assertEquals('http://nest.ed/1-2', $bookmark->getUrl());
235 $this->assertEquals('', $bookmark->getDescription());
236 $this->assertFalse($bookmark->isPrivate());
237 $this->assertEquals('folder1 tag3 tag4', $bookmark->getTagsString());
238 $this->assertEquals('46SZxA', $bookmark->getShortUrl());
239
240 $bookmark = $this->bookmarkService->findByUrl('http://nest.ed/2-1');
241 $this->assertEquals(3, $bookmark->getId());
242 $this->assertEquals(
243 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20160202_202222'),
244 $bookmark->getCreated()
245 );
246 $this->assertEquals('Nested 2-1', $bookmark->getTitle());
247 $this->assertEquals('http://nest.ed/2-1', $bookmark->getUrl());
248 $this->assertEquals('First link of the second section', $bookmark->getDescription());
249 $this->assertTrue($bookmark->isPrivate());
250 $this->assertEquals('folder2', $bookmark->getTagsString());
251 $this->assertEquals('4UHOSw', $bookmark->getShortUrl());
252
253 $bookmark = $this->bookmarkService->findByUrl('http://nest.ed/2-2');
254 $this->assertEquals(4, $bookmark->getId());
255 $this->assertEquals(
256 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20160119_230227'),
257 $bookmark->getCreated()
258 );
259 $this->assertEquals('Nested 2-2', $bookmark->getTitle());
260 $this->assertEquals('http://nest.ed/2-2', $bookmark->getUrl());
261 $this->assertEquals('Second link of the second section', $bookmark->getDescription());
262 $this->assertTrue($bookmark->isPrivate());
263 $this->assertEquals('folder2', $bookmark->getTagsString());
264 $this->assertEquals('yfzwbw', $bookmark->getShortUrl());
265
266 $bookmark = $this->bookmarkService->findByUrl('http://nest.ed/3-1');
267 $this->assertEquals(5, $bookmark->getId());
268 $this->assertEquals(
269 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20160202_202222'),
270 $bookmark->getCreated()
271 );
272 $this->assertEquals('Nested 3-1', $bookmark->getTitle());
273 $this->assertEquals('http://nest.ed/3-1', $bookmark->getUrl());
274 $this->assertEquals('', $bookmark->getDescription());
275 $this->assertFalse($bookmark->isPrivate());
276 $this->assertEquals('folder3 folder3-1 tag3', $bookmark->getTagsString());
277 $this->assertEquals('UwxIUQ', $bookmark->getShortUrl());
278
279 $bookmark = $this->bookmarkService->findByUrl('http://nest.ed/3-2');
280 $this->assertEquals(6, $bookmark->getId());
281 $this->assertEquals(
282 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20160119_230227'),
283 $bookmark->getCreated()
284 );
285 $this->assertEquals('Nested 3-2', $bookmark->getTitle());
286 $this->assertEquals('http://nest.ed/3-2', $bookmark->getUrl());
287 $this->assertEquals('', $bookmark->getDescription());
288 $this->assertFalse($bookmark->isPrivate());
289 $this->assertEquals('folder3 folder3-1', $bookmark->getTagsString());
290 $this->assertEquals('p8dyZg', $bookmark->getShortUrl());
291
292 $bookmark = $this->bookmarkService->findByUrl('http://nest.ed/2');
293 $this->assertEquals(7, $bookmark->getId());
294 $this->assertEquals(
295 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20160229_111541'),
296 $bookmark->getCreated()
297 );
298 $this->assertEquals('Nested 2', $bookmark->getTitle());
299 $this->assertEquals('http://nest.ed/2', $bookmark->getUrl());
300 $this->assertEquals('', $bookmark->getDescription());
301 $this->assertFalse($bookmark->isPrivate());
302 $this->assertEquals('tag4', $bookmark->getTagsString());
303 $this->assertEquals('Gt3Uug', $bookmark->getShortUrl());
293 } 304 }
294 305
295 /** 306 /**
@@ -302,40 +313,38 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
302 $files = file2array('netscape_basic.htm'); 313 $files = file2array('netscape_basic.htm');
303 $this->assertStringMatchesFormat( 314 $this->assertStringMatchesFormat(
304 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' 315 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
305 .' 2 links imported, 0 links overwritten, 0 links skipped.', 316 .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
306 NetscapeBookmarkUtils::import([], $files, $this->linkDb, $this->conf, $this->history) 317 $this->netscapeBookmarkUtils->import([], $files)
307 ); 318 );
308 319
309 $this->assertEquals(2, count($this->linkDb)); 320 $this->assertEquals(2, $this->bookmarkService->count());
310 $this->assertEquals(1, count_private($this->linkDb)); 321 $this->assertEquals(1, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
311 322
323 $bookmark = $this->bookmarkService->findByUrl('https://private.tld');
324 $this->assertEquals(0, $bookmark->getId());
312 $this->assertEquals( 325 $this->assertEquals(
313 array( 326 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20001010_135536'),
314 'id' => 0, 327 $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 ); 328 );
329 $this->assertEquals('Secret stuff', $bookmark->getTitle());
330 $this->assertEquals('https://private.tld', $bookmark->getUrl());
331 $this->assertEquals('Super-secret stuff you\'re not supposed to know about', $bookmark->getDescription());
332 $this->assertTrue($bookmark->isPrivate());
333 $this->assertEquals('private secret', $bookmark->getTagsString());
334 $this->assertEquals('EokDtA', $bookmark->getShortUrl());
335
336 $bookmark = $this->bookmarkService->findByUrl('http://public.tld');
337 $this->assertEquals(1, $bookmark->getId());
326 $this->assertEquals( 338 $this->assertEquals(
327 array( 339 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20160225_235548'),
328 'id' => 1, 340 $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 ); 341 );
342 $this->assertEquals('Public stuff', $bookmark->getTitle());
343 $this->assertEquals('http://public.tld', $bookmark->getUrl());
344 $this->assertEquals('', $bookmark->getDescription());
345 $this->assertFalse($bookmark->isPrivate());
346 $this->assertEquals('public hello world', $bookmark->getTagsString());
347 $this->assertEquals('Er9ddA', $bookmark->getShortUrl());
339 } 348 }
340 349
341 /** 350 /**
@@ -347,43 +356,42 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
347 $files = file2array('netscape_basic.htm'); 356 $files = file2array('netscape_basic.htm');
348 $this->assertStringMatchesFormat( 357 $this->assertStringMatchesFormat(
349 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' 358 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
350 .' 2 links imported, 0 links overwritten, 0 links skipped.', 359 .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
351 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history) 360 $this->netscapeBookmarkUtils->import($post, $files)
352 ); 361 );
353 $this->assertEquals(2, count($this->linkDb));
354 $this->assertEquals(1, count_private($this->linkDb));
355 362
363 $this->assertEquals(2, $this->bookmarkService->count());
364 $this->assertEquals(1, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
365
366 $bookmark = $this->bookmarkService->findByUrl('https://private.tld');
367 $this->assertEquals(0, $bookmark->getId());
356 $this->assertEquals( 368 $this->assertEquals(
357 array( 369 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20001010_135536'),
358 'id' => 0, 370 $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 ); 371 );
372 $this->assertEquals('Secret stuff', $bookmark->getTitle());
373 $this->assertEquals('https://private.tld', $bookmark->getUrl());
374 $this->assertEquals('Super-secret stuff you\'re not supposed to know about', $bookmark->getDescription());
375 $this->assertTrue($bookmark->isPrivate());
376 $this->assertEquals('private secret', $bookmark->getTagsString());
377 $this->assertEquals('EokDtA', $bookmark->getShortUrl());
378
379 $bookmark = $this->bookmarkService->findByUrl('http://public.tld');
380 $this->assertEquals(1, $bookmark->getId());
370 $this->assertEquals( 381 $this->assertEquals(
371 array( 382 DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20160225_235548'),
372 'id' => 1, 383 $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 ); 384 );
385 $this->assertEquals('Public stuff', $bookmark->getTitle());
386 $this->assertEquals('http://public.tld', $bookmark->getUrl());
387 $this->assertEquals('', $bookmark->getDescription());
388 $this->assertFalse($bookmark->isPrivate());
389 $this->assertEquals('public hello world', $bookmark->getTagsString());
390 $this->assertEquals('Er9ddA', $bookmark->getShortUrl());
383 } 391 }
384 392
385 /** 393 /**
386 * Import links as public 394 * Import bookmarks as public
387 */ 395 */
388 public function testImportAsPublic() 396 public function testImportAsPublic()
389 { 397 {
@@ -391,23 +399,17 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
391 $files = file2array('netscape_basic.htm'); 399 $files = file2array('netscape_basic.htm');
392 $this->assertStringMatchesFormat( 400 $this->assertStringMatchesFormat(
393 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' 401 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
394 .' 2 links imported, 0 links overwritten, 0 links skipped.', 402 .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
395 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history) 403 $this->netscapeBookmarkUtils->import($post, $files)
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 ); 404 );
405 $this->assertEquals(2, $this->bookmarkService->count());
406 $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
407 $this->assertFalse($this->bookmarkService->get(0)->isPrivate());
408 $this->assertFalse($this->bookmarkService->get(1)->isPrivate());
407 } 409 }
408 410
409 /** 411 /**
410 * Import links as private 412 * Import bookmarks as private
411 */ 413 */
412 public function testImportAsPrivate() 414 public function testImportAsPrivate()
413 { 415 {
@@ -415,45 +417,34 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
415 $files = file2array('netscape_basic.htm'); 417 $files = file2array('netscape_basic.htm');
416 $this->assertStringMatchesFormat( 418 $this->assertStringMatchesFormat(
417 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' 419 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
418 .' 2 links imported, 0 links overwritten, 0 links skipped.', 420 .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
419 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history) 421 $this->netscapeBookmarkUtils->import($post, $files)
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 ); 422 );
423 $this->assertEquals(2, $this->bookmarkService->count());
424 $this->assertEquals(2, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
425 $this->assertTrue($this->bookmarkService->get(0)->isPrivate());
426 $this->assertTrue($this->bookmarkService->get(1)->isPrivate());
431 } 427 }
432 428
433 /** 429 /**
434 * Overwrite private links so they become public 430 * Overwrite private bookmarks so they become public
435 */ 431 */
436 public function testOverwriteAsPublic() 432 public function testOverwriteAsPublic()
437 { 433 {
438 $files = file2array('netscape_basic.htm'); 434 $files = file2array('netscape_basic.htm');
439 435
440 // import links as private 436 // import bookmarks as private
441 $post = array('privacy' => 'private'); 437 $post = array('privacy' => 'private');
442 $this->assertStringMatchesFormat( 438 $this->assertStringMatchesFormat(
443 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' 439 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
444 .' 2 links imported, 0 links overwritten, 0 links skipped.', 440 .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
445 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history) 441 $this->netscapeBookmarkUtils->import($post, $files)
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 ); 442 );
443 $this->assertEquals(2, $this->bookmarkService->count());
444 $this->assertEquals(2, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
445 $this->assertTrue($this->bookmarkService->get(0)->isPrivate());
446 $this->assertTrue($this->bookmarkService->get(1)->isPrivate());
447
457 // re-import as public, enable overwriting 448 // re-import as public, enable overwriting
458 $post = array( 449 $post = array(
459 'privacy' => 'public', 450 'privacy' => 'public',
@@ -461,45 +452,33 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
461 ); 452 );
462 $this->assertStringMatchesFormat( 453 $this->assertStringMatchesFormat(
463 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' 454 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
464 .' 2 links imported, 2 links overwritten, 0 links skipped.', 455 .' 2 bookmarks imported, 2 bookmarks overwritten, 0 bookmarks skipped.',
465 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history) 456 $this->netscapeBookmarkUtils->import($post, $files)
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 ); 457 );
458 $this->assertEquals(2, $this->bookmarkService->count());
459 $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
460 $this->assertFalse($this->bookmarkService->get(0)->isPrivate());
461 $this->assertFalse($this->bookmarkService->get(1)->isPrivate());
477 } 462 }
478 463
479 /** 464 /**
480 * Overwrite public links so they become private 465 * Overwrite public bookmarks so they become private
481 */ 466 */
482 public function testOverwriteAsPrivate() 467 public function testOverwriteAsPrivate()
483 { 468 {
484 $files = file2array('netscape_basic.htm'); 469 $files = file2array('netscape_basic.htm');
485 470
486 // import links as public 471 // import bookmarks as public
487 $post = array('privacy' => 'public'); 472 $post = array('privacy' => 'public');
488 $this->assertStringMatchesFormat( 473 $this->assertStringMatchesFormat(
489 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' 474 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
490 .' 2 links imported, 0 links overwritten, 0 links skipped.', 475 .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
491 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history) 476 $this->netscapeBookmarkUtils->import($post, $files)
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 ); 477 );
478 $this->assertEquals(2, $this->bookmarkService->count());
479 $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
480 $this->assertFalse($this->bookmarkService->get(0)->isPrivate());
481 $this->assertFalse($this->bookmarkService->get(1)->isPrivate());
503 482
504 // re-import as private, enable overwriting 483 // re-import as private, enable overwriting
505 $post = array( 484 $post = array(
@@ -508,23 +487,17 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
508 ); 487 );
509 $this->assertStringMatchesFormat( 488 $this->assertStringMatchesFormat(
510 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' 489 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
511 .' 2 links imported, 2 links overwritten, 0 links skipped.', 490 .' 2 bookmarks imported, 2 bookmarks overwritten, 0 bookmarks skipped.',
512 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history) 491 $this->netscapeBookmarkUtils->import($post, $files)
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 ); 492 );
493 $this->assertEquals(2, $this->bookmarkService->count());
494 $this->assertEquals(2, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
495 $this->assertTrue($this->bookmarkService->get(0)->isPrivate());
496 $this->assertTrue($this->bookmarkService->get(1)->isPrivate());
524 } 497 }
525 498
526 /** 499 /**
527 * Attept to import the same links twice without enabling overwriting 500 * Attept to import the same bookmarks twice without enabling overwriting
528 */ 501 */
529 public function testSkipOverwrite() 502 public function testSkipOverwrite()
530 { 503 {
@@ -532,21 +505,21 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
532 $files = file2array('netscape_basic.htm'); 505 $files = file2array('netscape_basic.htm');
533 $this->assertStringMatchesFormat( 506 $this->assertStringMatchesFormat(
534 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' 507 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
535 .' 2 links imported, 0 links overwritten, 0 links skipped.', 508 .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
536 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history) 509 $this->netscapeBookmarkUtils->import($post, $files)
537 ); 510 );
538 $this->assertEquals(2, count($this->linkDb)); 511 $this->assertEquals(2, $this->bookmarkService->count());
539 $this->assertEquals(0, count_private($this->linkDb)); 512 $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
540 513
541 // re-import as private, DO NOT enable overwriting 514 // re-import as private, DO NOT enable overwriting
542 $post = array('privacy' => 'private'); 515 $post = array('privacy' => 'private');
543 $this->assertStringMatchesFormat( 516 $this->assertStringMatchesFormat(
544 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' 517 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
545 .' 0 links imported, 0 links overwritten, 2 links skipped.', 518 .' 0 bookmarks imported, 0 bookmarks overwritten, 2 bookmarks skipped.',
546 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history) 519 $this->netscapeBookmarkUtils->import($post, $files)
547 ); 520 );
548 $this->assertEquals(2, count($this->linkDb)); 521 $this->assertEquals(2, $this->bookmarkService->count());
549 $this->assertEquals(0, count_private($this->linkDb)); 522 $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
550 } 523 }
551 524
552 /** 525 /**
@@ -561,19 +534,13 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
561 $files = file2array('netscape_basic.htm'); 534 $files = file2array('netscape_basic.htm');
562 $this->assertStringMatchesFormat( 535 $this->assertStringMatchesFormat(
563 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' 536 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
564 .' 2 links imported, 0 links overwritten, 0 links skipped.', 537 .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
565 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history) 538 $this->netscapeBookmarkUtils->import($post, $files)
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 ); 539 );
540 $this->assertEquals(2, $this->bookmarkService->count());
541 $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
542 $this->assertEquals('tag1 tag2 tag3 private secret', $this->bookmarkService->get(0)->getTagsString());
543 $this->assertEquals('tag1 tag2 tag3 public hello world', $this->bookmarkService->get(1)->getTagsString());
577 } 544 }
578 545
579 /** 546 /**
@@ -588,18 +555,18 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
588 $files = file2array('netscape_basic.htm'); 555 $files = file2array('netscape_basic.htm');
589 $this->assertStringMatchesFormat( 556 $this->assertStringMatchesFormat(
590 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:' 557 'File netscape_basic.htm (482 bytes) was successfully processed in %d seconds:'
591 .' 2 links imported, 0 links overwritten, 0 links skipped.', 558 .' 2 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
592 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history) 559 $this->netscapeBookmarkUtils->import($post, $files)
593 ); 560 );
594 $this->assertEquals(2, count($this->linkDb)); 561 $this->assertEquals(2, $this->bookmarkService->count());
595 $this->assertEquals(0, count_private($this->linkDb)); 562 $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
596 $this->assertEquals( 563 $this->assertEquals(
597 'tag1&amp; tag2 &quot;tag3&quot; private secret', 564 'tag1&amp; tag2 &quot;tag3&quot; private secret',
598 $this->linkDb['0']['tags'] 565 $this->bookmarkService->get(0)->getTagsString()
599 ); 566 );
600 $this->assertEquals( 567 $this->assertEquals(
601 'tag1&amp; tag2 &quot;tag3&quot; public hello world', 568 'tag1&amp; tag2 &quot;tag3&quot; public hello world',
602 $this->linkDb['1']['tags'] 569 $this->bookmarkService->get(1)->getTagsString()
603 ); 570 );
604 } 571 }
605 572
@@ -613,23 +580,14 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
613 $files = file2array('same_date.htm'); 580 $files = file2array('same_date.htm');
614 $this->assertStringMatchesFormat( 581 $this->assertStringMatchesFormat(
615 'File same_date.htm (453 bytes) was successfully processed in %d seconds:' 582 'File same_date.htm (453 bytes) was successfully processed in %d seconds:'
616 .' 3 links imported, 0 links overwritten, 0 links skipped.', 583 .' 3 bookmarks imported, 0 bookmarks overwritten, 0 bookmarks skipped.',
617 NetscapeBookmarkUtils::import(array(), $files, $this->linkDb, $this->conf, $this->history) 584 $this->netscapeBookmarkUtils->import(array(), $files)
618 ); 585 );
619 $this->assertEquals(3, count($this->linkDb)); 586 $this->assertEquals(3, $this->bookmarkService->count());
620 $this->assertEquals(0, count_private($this->linkDb)); 587 $this->assertEquals(0, $this->bookmarkService->count(BookmarkFilter::$PRIVATE));
621 $this->assertEquals( 588 $this->assertEquals(0, $this->bookmarkService->get(0)->getId());
622 0, 589 $this->assertEquals(1, $this->bookmarkService->get(1)->getId());
623 $this->linkDb[0]['id'] 590 $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 } 591 }
634 592
635 public function testImportCreateUpdateHistory() 593 public function testImportCreateUpdateHistory()
@@ -639,14 +597,14 @@ class BookmarkImportTest extends \PHPUnit\Framework\TestCase
639 'overwrite' => 'true', 597 'overwrite' => 'true',
640 ]; 598 ];
641 $files = file2array('netscape_basic.htm'); 599 $files = file2array('netscape_basic.htm');
642 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history); 600 $this->netscapeBookmarkUtils->import($post, $files);
643 $history = $this->history->getHistory(); 601 $history = $this->history->getHistory();
644 $this->assertEquals(1, count($history)); 602 $this->assertEquals(1, count($history));
645 $this->assertEquals(History::IMPORT, $history[0]['event']); 603 $this->assertEquals(History::IMPORT, $history[0]['event']);
646 $this->assertTrue(new DateTime('-5 seconds') < $history[0]['datetime']); 604 $this->assertTrue(new DateTime('-5 seconds') < $history[0]['datetime']);
647 605
648 // re-import as private, enable overwriting 606 // re-import as private, enable overwriting
649 NetscapeBookmarkUtils::import($post, $files, $this->linkDb, $this->conf, $this->history); 607 $this->netscapeBookmarkUtils->import($post, $files);
650 $history = $this->history->getHistory(); 608 $history = $this->history->getHistory();
651 $this->assertEquals(2, count($history)); 609 $this->assertEquals(2, count($history));
652 $this->assertEquals(History::IMPORT, $history[0]['event']); 610 $this->assertEquals(History::IMPORT, $history[0]['event']);