]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/netscape/BookmarkExportTest.php
Add and update unit test for the new system (Bookmark + Service)
[github/shaarli/Shaarli.git] / tests / netscape / BookmarkExportTest.php
CommitLineData
cd5327be 1<?php
349b0144 2namespace Shaarli\Netscape;
cd5327be 3
e26e2060 4use Shaarli\Bookmark\BookmarkFileService;
f24896b2 5use Shaarli\Bookmark\LinkDB;
e26e2060
A
6use Shaarli\Config\ConfigManager;
7use Shaarli\Formatter\FormatterFactory;
8use Shaarli\Formatter\BookmarkFormatter;
9use Shaarli\History;
f24896b2 10
349b0144 11require_once 'tests/utils/ReferenceLinkDB.php';
cd5327be
V
12
13/**
a973afea 14 * Netscape bookmark export
cd5327be 15 */
349b0144 16class BookmarkExportTest extends \PHPUnit\Framework\TestCase
cd5327be
V
17{
18 /**
19 * @var string datastore to test write operations
20 */
21 protected static $testDatastore = 'sandbox/datastore.php';
22
23 /**
349b0144 24 * @var \ReferenceLinkDB instance.
cd5327be
V
25 */
26 protected static $refDb = null;
27
28 /**
e26e2060 29 * @var BookmarkFileService private instance.
cd5327be 30 */
e26e2060
A
31 protected static $bookmarkService = null;
32
33 /**
34 * @var BookmarkFormatter instance
35 */
36 protected static $formatter;
cd5327be
V
37
38 /**
39 * Instantiate reference data
40 */
41 public static function setUpBeforeClass()
42 {
e26e2060
A
43 $conf = new ConfigManager('tests/utils/config/configJson');
44 $conf->set('resource.datastore', self::$testDatastore);
349b0144 45 self::$refDb = new \ReferenceLinkDB();
cd5327be 46 self::$refDb->write(self::$testDatastore);
e26e2060
A
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');
cd5327be
V
51 }
52
53 /**
54 * Attempt to export an invalid link selection
55 * @expectedException Exception
56 * @expectedExceptionMessageRegExp /Invalid export selection/
57 */
58 public function testFilterAndFormatInvalid()
59 {
e26e2060
A
60 NetscapeBookmarkUtils::filterAndFormat(
61 self::$bookmarkService,
62 self::$formatter,
63 'derp',
64 false,
65 ''
66 );
cd5327be
V
67 }
68
69 /**
e26e2060 70 * Prepare all bookmarks for export
cd5327be
V
71 */
72 public function testFilterAndFormatAll()
73 {
e26e2060
A
74 $links = NetscapeBookmarkUtils::filterAndFormat(
75 self::$bookmarkService,
76 self::$formatter,
77 'all',
78 false,
79 ''
80 );
cd5327be
V
81 $this->assertEquals(self::$refDb->countLinks(), sizeof($links));
82 foreach ($links as $link) {
c3dfd899 83 $date = $link['created'];
cd5327be
V
84 $this->assertEquals(
85 $date->getTimestamp(),
86 $link['timestamp']
87 );
88 $this->assertEquals(
89 str_replace(' ', ',', $link['tags']),
90 $link['taglist']
91 );
92 }
93 }
94
95 /**
e26e2060 96 * Prepare private bookmarks for export
cd5327be
V
97 */
98 public function testFilterAndFormatPrivate()
99 {
e26e2060
A
100 $links = NetscapeBookmarkUtils::filterAndFormat(
101 self::$bookmarkService,
102 self::$formatter,
103 'private',
104 false,
105 ''
106 );
cd5327be
V
107 $this->assertEquals(self::$refDb->countPrivateLinks(), sizeof($links));
108 foreach ($links as $link) {
c3dfd899 109 $date = $link['created'];
cd5327be
V
110 $this->assertEquals(
111 $date->getTimestamp(),
112 $link['timestamp']
113 );
114 $this->assertEquals(
115 str_replace(' ', ',', $link['tags']),
116 $link['taglist']
117 );
118 }
119 }
120
121 /**
e26e2060 122 * Prepare public bookmarks for export
cd5327be
V
123 */
124 public function testFilterAndFormatPublic()
125 {
e26e2060
A
126 $links = NetscapeBookmarkUtils::filterAndFormat(
127 self::$bookmarkService,
128 self::$formatter,
129 'public',
130 false,
131 ''
132 );
cd5327be
V
133 $this->assertEquals(self::$refDb->countPublicLinks(), sizeof($links));
134 foreach ($links as $link) {
c3dfd899 135 $date = $link['created'];
cd5327be
V
136 $this->assertEquals(
137 $date->getTimestamp(),
138 $link['timestamp']
139 );
140 $this->assertEquals(
141 str_replace(' ', ',', $link['tags']),
142 $link['taglist']
143 );
144 }
145 }
bb4a23aa
V
146
147 /**
148 * Do not prepend notes with the Shaarli index's URL
149 */
150 public function testFilterAndFormatDoNotPrependNoteUrl()
151 {
e26e2060
A
152 $links = NetscapeBookmarkUtils::filterAndFormat(
153 self::$bookmarkService,
154 self::$formatter,
155 'public',
156 false,
157 ''
158 );
bb4a23aa
V
159 $this->assertEquals(
160 '?WDWyig',
4154c25b 161 $links[2]['url']
bb4a23aa
V
162 );
163 }
164
165 /**
166 * Prepend notes with the Shaarli index's URL
167 */
168 public function testFilterAndFormatPrependNoteUrl()
169 {
170 $indexUrl = 'http://localhost:7469/shaarli/';
171 $links = NetscapeBookmarkUtils::filterAndFormat(
e26e2060
A
172 self::$bookmarkService,
173 self::$formatter,
bb4a23aa
V
174 'public',
175 true,
176 $indexUrl
177 );
178 $this->assertEquals(
179 $indexUrl . '?WDWyig',
4154c25b 180 $links[2]['url']
bb4a23aa
V
181 );
182 }
cd5327be 183}