]>
Commit | Line | Data |
---|---|---|
cd5327be | 1 | <?php |
e8a10f31 | 2 | |
349b0144 | 3 | namespace Shaarli\Netscape; |
cd5327be | 4 | |
fd1ddad9 | 5 | use malkusch\lock\mutex\NoMutex; |
e26e2060 | 6 | use Shaarli\Bookmark\BookmarkFileService; |
e26e2060 | 7 | use Shaarli\Config\ConfigManager; |
e26e2060 | 8 | use Shaarli\Formatter\BookmarkFormatter; |
e8a10f31 | 9 | use Shaarli\Formatter\FormatterFactory; |
e26e2060 | 10 | use Shaarli\History; |
bcba6bd3 | 11 | use Shaarli\Plugin\PluginManager; |
a5a9cf23 | 12 | use Shaarli\TestCase; |
f24896b2 | 13 | |
349b0144 | 14 | require_once 'tests/utils/ReferenceLinkDB.php'; |
cd5327be V |
15 | |
16 | /** | |
a973afea | 17 | * Netscape bookmark export |
cd5327be | 18 | */ |
e8a10f31 | 19 | class BookmarkExportTest extends TestCase |
cd5327be V |
20 | { |
21 | /** | |
22 | * @var string datastore to test write operations | |
23 | */ | |
24 | protected static $testDatastore = 'sandbox/datastore.php'; | |
25 | ||
e8a10f31 A |
26 | /** |
27 | * @var ConfigManager instance. | |
28 | */ | |
29 | protected static $conf; | |
30 | ||
cd5327be | 31 | /** |
349b0144 | 32 | * @var \ReferenceLinkDB instance. |
cd5327be V |
33 | */ |
34 | protected static $refDb = null; | |
35 | ||
36 | /** | |
e26e2060 | 37 | * @var BookmarkFileService private instance. |
cd5327be | 38 | */ |
e26e2060 A |
39 | protected static $bookmarkService = null; |
40 | ||
41 | /** | |
42 | * @var BookmarkFormatter instance | |
43 | */ | |
44 | protected static $formatter; | |
cd5327be | 45 | |
e8a10f31 A |
46 | /** |
47 | * @var History instance | |
48 | */ | |
49 | protected static $history; | |
50 | ||
bcba6bd3 A |
51 | /** @var PluginManager */ |
52 | protected static $pluginManager; | |
53 | ||
e8a10f31 A |
54 | /** |
55 | * @var NetscapeBookmarkUtils | |
56 | */ | |
57 | protected $netscapeBookmarkUtils; | |
58 | ||
cd5327be V |
59 | /** |
60 | * Instantiate reference data | |
61 | */ | |
8f60e120 | 62 | public static function setUpBeforeClass(): void |
cd5327be | 63 | { |
fd1ddad9 | 64 | $mutex = new NoMutex(); |
e8a10f31 A |
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'); | |
bcba6bd3 A |
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 | ); | |
e8a10f31 A |
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 | ); | |
cd5327be V |
89 | } |
90 | ||
91 | /** | |
92 | * Attempt to export an invalid link selection | |
cd5327be V |
93 | */ |
94 | public function testFilterAndFormatInvalid() | |
95 | { | |
f447edb7 | 96 | $this->expectException(\Exception::class); |
b1baca99 A |
97 | $this->expectExceptionMessageRegExp('/Invalid export selection/'); |
98 | ||
e8a10f31 | 99 | $this->netscapeBookmarkUtils->filterAndFormat( |
e26e2060 A |
100 | self::$formatter, |
101 | 'derp', | |
102 | false, | |
103 | '' | |
104 | ); | |
cd5327be V |
105 | } |
106 | ||
107 | /** | |
e26e2060 | 108 | * Prepare all bookmarks for export |
cd5327be V |
109 | */ |
110 | public function testFilterAndFormatAll() | |
111 | { | |
e8a10f31 | 112 | $links = $this->netscapeBookmarkUtils->filterAndFormat( |
e26e2060 A |
113 | self::$formatter, |
114 | 'all', | |
115 | false, | |
116 | '' | |
117 | ); | |
cd5327be V |
118 | $this->assertEquals(self::$refDb->countLinks(), sizeof($links)); |
119 | foreach ($links as $link) { | |
c3dfd899 | 120 | $date = $link['created']; |
cd5327be V |
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 | /** | |
e26e2060 | 133 | * Prepare private bookmarks for export |
cd5327be V |
134 | */ |
135 | public function testFilterAndFormatPrivate() | |
136 | { | |
e8a10f31 | 137 | $links = $this->netscapeBookmarkUtils->filterAndFormat( |
e26e2060 A |
138 | self::$formatter, |
139 | 'private', | |
140 | false, | |
141 | '' | |
142 | ); | |
cd5327be V |
143 | $this->assertEquals(self::$refDb->countPrivateLinks(), sizeof($links)); |
144 | foreach ($links as $link) { | |
c3dfd899 | 145 | $date = $link['created']; |
cd5327be V |
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 | /** | |
e26e2060 | 158 | * Prepare public bookmarks for export |
cd5327be V |
159 | */ |
160 | public function testFilterAndFormatPublic() | |
161 | { | |
e8a10f31 | 162 | $links = $this->netscapeBookmarkUtils->filterAndFormat( |
e26e2060 A |
163 | self::$formatter, |
164 | 'public', | |
165 | false, | |
166 | '' | |
167 | ); | |
cd5327be V |
168 | $this->assertEquals(self::$refDb->countPublicLinks(), sizeof($links)); |
169 | foreach ($links as $link) { | |
c3dfd899 | 170 | $date = $link['created']; |
cd5327be V |
171 | $this->assertEquals( |
172 | $date->getTimestamp(), | |
173 | $link['timestamp'] | |
174 | ); | |
175 | $this->assertEquals( | |
176 | str_replace(' ', ',', $link['tags']), | |
177 | $link['taglist'] | |
178 | ); | |
179 | } | |
180 | } | |
bb4a23aa V |
181 | |
182 | /** | |
183 | * Do not prepend notes with the Shaarli index's URL | |
184 | */ | |
185 | public function testFilterAndFormatDoNotPrependNoteUrl() | |
186 | { | |
e8a10f31 | 187 | $links = $this->netscapeBookmarkUtils->filterAndFormat( |
e26e2060 A |
188 | self::$formatter, |
189 | 'public', | |
190 | false, | |
191 | '' | |
192 | ); | |
bb4a23aa | 193 | $this->assertEquals( |
f7f08cee | 194 | '/shaare/WDWyig', |
4154c25b | 195 | $links[2]['url'] |
bb4a23aa V |
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/'; | |
e8a10f31 | 205 | $links = $this->netscapeBookmarkUtils->filterAndFormat( |
e26e2060 | 206 | self::$formatter, |
bb4a23aa V |
207 | 'public', |
208 | true, | |
209 | $indexUrl | |
210 | ); | |
211 | $this->assertEquals( | |
f7f08cee | 212 | $indexUrl . 'shaare/WDWyig', |
4154c25b | 213 | $links[2]['url'] |
bb4a23aa V |
214 | ); |
215 | } | |
cd5327be | 216 | } |