]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/netscape/BookmarkExportTest.php
ad288f78ef802835f238e8d40f4c6c8d9bd2059d
[github/shaarli/Shaarli.git] / tests / netscape / BookmarkExportTest.php
1 <?php
2
3 namespace Shaarli\Netscape;
4
5 use malkusch\lock\mutex\NoMutex;
6 use Shaarli\Bookmark\BookmarkFileService;
7 use Shaarli\Config\ConfigManager;
8 use Shaarli\Formatter\BookmarkFormatter;
9 use Shaarli\Formatter\FormatterFactory;
10 use Shaarli\History;
11 use Shaarli\TestCase;
12
13 require_once 'tests/utils/ReferenceLinkDB.php';
14
15 /**
16 * Netscape bookmark export
17 */
18 class BookmarkExportTest extends TestCase
19 {
20 /**
21 * @var string datastore to test write operations
22 */
23 protected static $testDatastore = 'sandbox/datastore.php';
24
25 /**
26 * @var ConfigManager instance.
27 */
28 protected static $conf;
29
30 /**
31 * @var \ReferenceLinkDB instance.
32 */
33 protected static $refDb = null;
34
35 /**
36 * @var BookmarkFileService private instance.
37 */
38 protected static $bookmarkService = null;
39
40 /**
41 * @var BookmarkFormatter instance
42 */
43 protected static $formatter;
44
45 /**
46 * @var History instance
47 */
48 protected static $history;
49
50 /**
51 * @var NetscapeBookmarkUtils
52 */
53 protected $netscapeBookmarkUtils;
54
55 /**
56 * Instantiate reference data
57 */
58 public static function setUpBeforeClass(): void
59 {
60 $mutex = new NoMutex();
61 static::$conf = new ConfigManager('tests/utils/config/configJson');
62 static::$conf->set('resource.datastore', static::$testDatastore);
63 static::$refDb = new \ReferenceLinkDB();
64 static::$refDb->write(static::$testDatastore);
65 static::$history = new History('sandbox/history.php');
66 static::$bookmarkService = new BookmarkFileService(static::$conf, static::$history, $mutex, true);
67 $factory = new FormatterFactory(static::$conf, true);
68 static::$formatter = $factory->getFormatter('raw');
69 }
70
71 public function setUp(): void
72 {
73 $this->netscapeBookmarkUtils = new NetscapeBookmarkUtils(
74 static::$bookmarkService,
75 static::$conf,
76 static::$history
77 );
78 }
79
80 /**
81 * Attempt to export an invalid link selection
82 */
83 public function testFilterAndFormatInvalid()
84 {
85 $this->expectException(\Exception::class);
86 $this->expectExceptionMessageRegExp('/Invalid export selection/');
87
88 $this->netscapeBookmarkUtils->filterAndFormat(
89 self::$formatter,
90 'derp',
91 false,
92 ''
93 );
94 }
95
96 /**
97 * Prepare all bookmarks for export
98 */
99 public function testFilterAndFormatAll()
100 {
101 $links = $this->netscapeBookmarkUtils->filterAndFormat(
102 self::$formatter,
103 'all',
104 false,
105 ''
106 );
107 $this->assertEquals(self::$refDb->countLinks(), sizeof($links));
108 foreach ($links as $link) {
109 $date = $link['created'];
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 /**
122 * Prepare private bookmarks for export
123 */
124 public function testFilterAndFormatPrivate()
125 {
126 $links = $this->netscapeBookmarkUtils->filterAndFormat(
127 self::$formatter,
128 'private',
129 false,
130 ''
131 );
132 $this->assertEquals(self::$refDb->countPrivateLinks(), sizeof($links));
133 foreach ($links as $link) {
134 $date = $link['created'];
135 $this->assertEquals(
136 $date->getTimestamp(),
137 $link['timestamp']
138 );
139 $this->assertEquals(
140 str_replace(' ', ',', $link['tags']),
141 $link['taglist']
142 );
143 }
144 }
145
146 /**
147 * Prepare public bookmarks for export
148 */
149 public function testFilterAndFormatPublic()
150 {
151 $links = $this->netscapeBookmarkUtils->filterAndFormat(
152 self::$formatter,
153 'public',
154 false,
155 ''
156 );
157 $this->assertEquals(self::$refDb->countPublicLinks(), sizeof($links));
158 foreach ($links as $link) {
159 $date = $link['created'];
160 $this->assertEquals(
161 $date->getTimestamp(),
162 $link['timestamp']
163 );
164 $this->assertEquals(
165 str_replace(' ', ',', $link['tags']),
166 $link['taglist']
167 );
168 }
169 }
170
171 /**
172 * Do not prepend notes with the Shaarli index's URL
173 */
174 public function testFilterAndFormatDoNotPrependNoteUrl()
175 {
176 $links = $this->netscapeBookmarkUtils->filterAndFormat(
177 self::$formatter,
178 'public',
179 false,
180 ''
181 );
182 $this->assertEquals(
183 '/shaare/WDWyig',
184 $links[2]['url']
185 );
186 }
187
188 /**
189 * Prepend notes with the Shaarli index's URL
190 */
191 public function testFilterAndFormatPrependNoteUrl()
192 {
193 $indexUrl = 'http://localhost:7469/shaarli/';
194 $links = $this->netscapeBookmarkUtils->filterAndFormat(
195 self::$formatter,
196 'public',
197 true,
198 $indexUrl
199 );
200 $this->assertEquals(
201 $indexUrl . 'shaare/WDWyig',
202 $links[2]['url']
203 );
204 }
205 }