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