]>
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; |
a5a9cf23 | 11 | use Shaarli\TestCase; |
f24896b2 | 12 | |
349b0144 | 13 | require_once 'tests/utils/ReferenceLinkDB.php'; |
cd5327be V |
14 | |
15 | /** | |
a973afea | 16 | * Netscape bookmark export |
cd5327be | 17 | */ |
e8a10f31 | 18 | class BookmarkExportTest extends TestCase |
cd5327be V |
19 | { |
20 | /** | |
21 | * @var string datastore to test write operations | |
22 | */ | |
23 | protected static $testDatastore = 'sandbox/datastore.php'; | |
24 | ||
e8a10f31 A |
25 | /** |
26 | * @var ConfigManager instance. | |
27 | */ | |
28 | protected static $conf; | |
29 | ||
cd5327be | 30 | /** |
349b0144 | 31 | * @var \ReferenceLinkDB instance. |
cd5327be V |
32 | */ |
33 | protected static $refDb = null; | |
34 | ||
35 | /** | |
e26e2060 | 36 | * @var BookmarkFileService private instance. |
cd5327be | 37 | */ |
e26e2060 A |
38 | protected static $bookmarkService = null; |
39 | ||
40 | /** | |
41 | * @var BookmarkFormatter instance | |
42 | */ | |
43 | protected static $formatter; | |
cd5327be | 44 | |
e8a10f31 A |
45 | /** |
46 | * @var History instance | |
47 | */ | |
48 | protected static $history; | |
49 | ||
50 | /** | |
51 | * @var NetscapeBookmarkUtils | |
52 | */ | |
53 | protected $netscapeBookmarkUtils; | |
54 | ||
cd5327be V |
55 | /** |
56 | * Instantiate reference data | |
57 | */ | |
8f60e120 | 58 | public static function setUpBeforeClass(): void |
cd5327be | 59 | { |
fd1ddad9 | 60 | $mutex = new NoMutex(); |
e8a10f31 A |
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'); | |
fd1ddad9 | 66 | static::$bookmarkService = new BookmarkFileService(static::$conf, static::$history, $mutex, true); |
e8a10f31 A |
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 | ); | |
cd5327be V |
78 | } |
79 | ||
80 | /** | |
81 | * Attempt to export an invalid link selection | |
cd5327be V |
82 | */ |
83 | public function testFilterAndFormatInvalid() | |
84 | { | |
f447edb7 | 85 | $this->expectException(\Exception::class); |
b1baca99 A |
86 | $this->expectExceptionMessageRegExp('/Invalid export selection/'); |
87 | ||
e8a10f31 | 88 | $this->netscapeBookmarkUtils->filterAndFormat( |
e26e2060 A |
89 | self::$formatter, |
90 | 'derp', | |
91 | false, | |
92 | '' | |
93 | ); | |
cd5327be V |
94 | } |
95 | ||
96 | /** | |
e26e2060 | 97 | * Prepare all bookmarks for export |
cd5327be V |
98 | */ |
99 | public function testFilterAndFormatAll() | |
100 | { | |
e8a10f31 | 101 | $links = $this->netscapeBookmarkUtils->filterAndFormat( |
e26e2060 A |
102 | self::$formatter, |
103 | 'all', | |
104 | false, | |
105 | '' | |
106 | ); | |
cd5327be V |
107 | $this->assertEquals(self::$refDb->countLinks(), 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 private bookmarks for export |
cd5327be V |
123 | */ |
124 | public function testFilterAndFormatPrivate() | |
125 | { | |
e8a10f31 | 126 | $links = $this->netscapeBookmarkUtils->filterAndFormat( |
e26e2060 A |
127 | self::$formatter, |
128 | 'private', | |
129 | false, | |
130 | '' | |
131 | ); | |
cd5327be V |
132 | $this->assertEquals(self::$refDb->countPrivateLinks(), sizeof($links)); |
133 | foreach ($links as $link) { | |
c3dfd899 | 134 | $date = $link['created']; |
cd5327be V |
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 | /** | |
e26e2060 | 147 | * Prepare public bookmarks for export |
cd5327be V |
148 | */ |
149 | public function testFilterAndFormatPublic() | |
150 | { | |
e8a10f31 | 151 | $links = $this->netscapeBookmarkUtils->filterAndFormat( |
e26e2060 A |
152 | self::$formatter, |
153 | 'public', | |
154 | false, | |
155 | '' | |
156 | ); | |
cd5327be V |
157 | $this->assertEquals(self::$refDb->countPublicLinks(), sizeof($links)); |
158 | foreach ($links as $link) { | |
c3dfd899 | 159 | $date = $link['created']; |
cd5327be V |
160 | $this->assertEquals( |
161 | $date->getTimestamp(), | |
162 | $link['timestamp'] | |
163 | ); | |
164 | $this->assertEquals( | |
165 | str_replace(' ', ',', $link['tags']), | |
166 | $link['taglist'] | |
167 | ); | |
168 | } | |
169 | } | |
bb4a23aa V |
170 | |
171 | /** | |
172 | * Do not prepend notes with the Shaarli index's URL | |
173 | */ | |
174 | public function testFilterAndFormatDoNotPrependNoteUrl() | |
175 | { | |
e8a10f31 | 176 | $links = $this->netscapeBookmarkUtils->filterAndFormat( |
e26e2060 A |
177 | self::$formatter, |
178 | 'public', | |
179 | false, | |
180 | '' | |
181 | ); | |
bb4a23aa | 182 | $this->assertEquals( |
f7f08cee | 183 | '/shaare/WDWyig', |
4154c25b | 184 | $links[2]['url'] |
bb4a23aa V |
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/'; | |
e8a10f31 | 194 | $links = $this->netscapeBookmarkUtils->filterAndFormat( |
e26e2060 | 195 | self::$formatter, |
bb4a23aa V |
196 | 'public', |
197 | true, | |
198 | $indexUrl | |
199 | ); | |
200 | $this->assertEquals( | |
f7f08cee | 201 | $indexUrl . 'shaare/WDWyig', |
4154c25b | 202 | $links[2]['url'] |
bb4a23aa V |
203 | ); |
204 | } | |
cd5327be | 205 | } |