2016-04-10 17:34:07 +02:00
|
|
|
<?php
|
2020-06-17 15:55:31 +02:00
|
|
|
|
2018-12-04 00:13:42 +01:00
|
|
|
namespace Shaarli\Netscape;
|
2016-04-10 17:34:07 +02:00
|
|
|
|
2020-01-17 21:34:12 +01:00
|
|
|
use Shaarli\Bookmark\BookmarkFileService;
|
|
|
|
use Shaarli\Config\ConfigManager;
|
|
|
|
use Shaarli\Formatter\BookmarkFormatter;
|
2020-06-17 15:55:31 +02:00
|
|
|
use Shaarli\Formatter\FormatterFactory;
|
2020-01-17 21:34:12 +01:00
|
|
|
use Shaarli\History;
|
2020-09-29 14:41:40 +02:00
|
|
|
use Shaarli\TestCase;
|
2018-12-03 01:10:39 +01:00
|
|
|
|
2018-12-04 00:13:42 +01:00
|
|
|
require_once 'tests/utils/ReferenceLinkDB.php';
|
2016-04-10 17:34:07 +02:00
|
|
|
|
|
|
|
/**
|
2016-07-28 22:54:33 +02:00
|
|
|
* Netscape bookmark export
|
2016-04-10 17:34:07 +02:00
|
|
|
*/
|
2020-06-17 15:55:31 +02:00
|
|
|
class BookmarkExportTest extends TestCase
|
2016-04-10 17:34:07 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string datastore to test write operations
|
|
|
|
*/
|
|
|
|
protected static $testDatastore = 'sandbox/datastore.php';
|
|
|
|
|
2020-06-17 15:55:31 +02:00
|
|
|
/**
|
|
|
|
* @var ConfigManager instance.
|
|
|
|
*/
|
|
|
|
protected static $conf;
|
|
|
|
|
2016-04-10 17:34:07 +02:00
|
|
|
/**
|
2018-12-04 00:13:42 +01:00
|
|
|
* @var \ReferenceLinkDB instance.
|
2016-04-10 17:34:07 +02:00
|
|
|
*/
|
|
|
|
protected static $refDb = null;
|
|
|
|
|
|
|
|
/**
|
2020-01-17 21:34:12 +01:00
|
|
|
* @var BookmarkFileService private instance.
|
2016-04-10 17:34:07 +02:00
|
|
|
*/
|
2020-01-17 21:34:12 +01:00
|
|
|
protected static $bookmarkService = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var BookmarkFormatter instance
|
|
|
|
*/
|
|
|
|
protected static $formatter;
|
2016-04-10 17:34:07 +02:00
|
|
|
|
2020-06-17 15:55:31 +02:00
|
|
|
/**
|
|
|
|
* @var History instance
|
|
|
|
*/
|
|
|
|
protected static $history;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var NetscapeBookmarkUtils
|
|
|
|
*/
|
|
|
|
protected $netscapeBookmarkUtils;
|
|
|
|
|
2016-04-10 17:34:07 +02:00
|
|
|
/**
|
|
|
|
* Instantiate reference data
|
|
|
|
*/
|
2020-09-26 15:08:39 +02:00
|
|
|
public static function setUpBeforeClass(): void
|
2016-04-10 17:34:07 +02:00
|
|
|
{
|
2020-06-17 15:55:31 +02:00
|
|
|
static::$conf = new ConfigManager('tests/utils/config/configJson');
|
|
|
|
static::$conf->set('resource.datastore', static::$testDatastore);
|
|
|
|
static::$refDb = new \ReferenceLinkDB();
|
|
|
|
static::$refDb->write(static::$testDatastore);
|
|
|
|
static::$history = new History('sandbox/history.php');
|
|
|
|
static::$bookmarkService = new BookmarkFileService(static::$conf, static::$history, true);
|
|
|
|
$factory = new FormatterFactory(static::$conf, true);
|
|
|
|
static::$formatter = $factory->getFormatter('raw');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
$this->netscapeBookmarkUtils = new NetscapeBookmarkUtils(
|
|
|
|
static::$bookmarkService,
|
|
|
|
static::$conf,
|
|
|
|
static::$history
|
|
|
|
);
|
2016-04-10 17:34:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attempt to export an invalid link selection
|
|
|
|
*/
|
|
|
|
public function testFilterAndFormatInvalid()
|
|
|
|
{
|
2020-09-29 18:41:21 +02:00
|
|
|
$this->expectException(\Exception::class);
|
2020-09-27 14:07:08 +02:00
|
|
|
$this->expectExceptionMessageRegExp('/Invalid export selection/');
|
|
|
|
|
2020-06-17 15:55:31 +02:00
|
|
|
$this->netscapeBookmarkUtils->filterAndFormat(
|
2020-01-17 21:34:12 +01:00
|
|
|
self::$formatter,
|
|
|
|
'derp',
|
|
|
|
false,
|
|
|
|
''
|
|
|
|
);
|
2016-04-10 17:34:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-17 21:34:12 +01:00
|
|
|
* Prepare all bookmarks for export
|
2016-04-10 17:34:07 +02:00
|
|
|
*/
|
|
|
|
public function testFilterAndFormatAll()
|
|
|
|
{
|
2020-06-17 15:55:31 +02:00
|
|
|
$links = $this->netscapeBookmarkUtils->filterAndFormat(
|
2020-01-17 21:34:12 +01:00
|
|
|
self::$formatter,
|
|
|
|
'all',
|
|
|
|
false,
|
|
|
|
''
|
|
|
|
);
|
2016-04-10 17:34:07 +02:00
|
|
|
$this->assertEquals(self::$refDb->countLinks(), sizeof($links));
|
|
|
|
foreach ($links as $link) {
|
2016-11-28 16:17:25 +01:00
|
|
|
$date = $link['created'];
|
2016-04-10 17:34:07 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
$date->getTimestamp(),
|
|
|
|
$link['timestamp']
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
str_replace(' ', ',', $link['tags']),
|
|
|
|
$link['taglist']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-17 21:34:12 +01:00
|
|
|
* Prepare private bookmarks for export
|
2016-04-10 17:34:07 +02:00
|
|
|
*/
|
|
|
|
public function testFilterAndFormatPrivate()
|
|
|
|
{
|
2020-06-17 15:55:31 +02:00
|
|
|
$links = $this->netscapeBookmarkUtils->filterAndFormat(
|
2020-01-17 21:34:12 +01:00
|
|
|
self::$formatter,
|
|
|
|
'private',
|
|
|
|
false,
|
|
|
|
''
|
|
|
|
);
|
2016-04-10 17:34:07 +02:00
|
|
|
$this->assertEquals(self::$refDb->countPrivateLinks(), sizeof($links));
|
|
|
|
foreach ($links as $link) {
|
2016-11-28 16:17:25 +01:00
|
|
|
$date = $link['created'];
|
2016-04-10 17:34:07 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
$date->getTimestamp(),
|
|
|
|
$link['timestamp']
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
str_replace(' ', ',', $link['tags']),
|
|
|
|
$link['taglist']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-17 21:34:12 +01:00
|
|
|
* Prepare public bookmarks for export
|
2016-04-10 17:34:07 +02:00
|
|
|
*/
|
|
|
|
public function testFilterAndFormatPublic()
|
|
|
|
{
|
2020-06-17 15:55:31 +02:00
|
|
|
$links = $this->netscapeBookmarkUtils->filterAndFormat(
|
2020-01-17 21:34:12 +01:00
|
|
|
self::$formatter,
|
|
|
|
'public',
|
|
|
|
false,
|
|
|
|
''
|
|
|
|
);
|
2016-04-10 17:34:07 +02:00
|
|
|
$this->assertEquals(self::$refDb->countPublicLinks(), sizeof($links));
|
|
|
|
foreach ($links as $link) {
|
2016-11-28 16:17:25 +01:00
|
|
|
$date = $link['created'];
|
2016-04-10 17:34:07 +02:00
|
|
|
$this->assertEquals(
|
|
|
|
$date->getTimestamp(),
|
|
|
|
$link['timestamp']
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
str_replace(' ', ',', $link['tags']),
|
|
|
|
$link['taglist']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2016-05-05 19:22:06 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Do not prepend notes with the Shaarli index's URL
|
|
|
|
*/
|
|
|
|
public function testFilterAndFormatDoNotPrependNoteUrl()
|
|
|
|
{
|
2020-06-17 15:55:31 +02:00
|
|
|
$links = $this->netscapeBookmarkUtils->filterAndFormat(
|
2020-01-17 21:34:12 +01:00
|
|
|
self::$formatter,
|
|
|
|
'public',
|
|
|
|
false,
|
|
|
|
''
|
|
|
|
);
|
2016-05-05 19:22:06 +02:00
|
|
|
$this->assertEquals(
|
2020-07-28 22:24:41 +02:00
|
|
|
'/shaare/WDWyig',
|
2018-05-22 22:44:38 +02:00
|
|
|
$links[2]['url']
|
2016-05-05 19:22:06 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepend notes with the Shaarli index's URL
|
|
|
|
*/
|
|
|
|
public function testFilterAndFormatPrependNoteUrl()
|
|
|
|
{
|
|
|
|
$indexUrl = 'http://localhost:7469/shaarli/';
|
2020-06-17 15:55:31 +02:00
|
|
|
$links = $this->netscapeBookmarkUtils->filterAndFormat(
|
2020-01-17 21:34:12 +01:00
|
|
|
self::$formatter,
|
2016-05-05 19:22:06 +02:00
|
|
|
'public',
|
|
|
|
true,
|
|
|
|
$indexUrl
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2020-07-28 22:24:41 +02:00
|
|
|
$indexUrl . 'shaare/WDWyig',
|
2018-05-22 22:44:38 +02:00
|
|
|
$links[2]['url']
|
2016-05-05 19:22:06 +02:00
|
|
|
);
|
|
|
|
}
|
2016-04-10 17:34:07 +02:00
|
|
|
}
|