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