]>
Commit | Line | Data |
---|---|---|
cd5327be | 1 | <?php |
e8a10f31 | 2 | |
349b0144 | 3 | namespace Shaarli\Netscape; |
cd5327be | 4 | |
e8a10f31 | 5 | use PHPUnit\Framework\TestCase; |
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; |
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 | */ | |
57 | public static function setUpBeforeClass() | |
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 | |
80 | * @expectedException Exception | |
81 | * @expectedExceptionMessageRegExp /Invalid export selection/ | |
82 | */ | |
83 | public function testFilterAndFormatInvalid() | |
84 | { | |
e8a10f31 | 85 | $this->netscapeBookmarkUtils->filterAndFormat( |
e26e2060 A |
86 | self::$formatter, |
87 | 'derp', | |
88 | false, | |
89 | '' | |
90 | ); | |
cd5327be V |
91 | } |
92 | ||
93 | /** | |
e26e2060 | 94 | * Prepare all bookmarks for export |
cd5327be V |
95 | */ |
96 | public function testFilterAndFormatAll() | |
97 | { | |
e8a10f31 | 98 | $links = $this->netscapeBookmarkUtils->filterAndFormat( |
e26e2060 A |
99 | self::$formatter, |
100 | 'all', | |
101 | false, | |
102 | '' | |
103 | ); | |
cd5327be V |
104 | $this->assertEquals(self::$refDb->countLinks(), sizeof($links)); |
105 | foreach ($links as $link) { | |
c3dfd899 | 106 | $date = $link['created']; |
cd5327be V |
107 | $this->assertEquals( |
108 | $date->getTimestamp(), | |
109 | $link['timestamp'] | |
110 | ); | |
111 | $this->assertEquals( | |
112 | str_replace(' ', ',', $link['tags']), | |
113 | $link['taglist'] | |
114 | ); | |
115 | } | |
116 | } | |
117 | ||
118 | /** | |
e26e2060 | 119 | * Prepare private bookmarks for export |
cd5327be V |
120 | */ |
121 | public function testFilterAndFormatPrivate() | |
122 | { | |
e8a10f31 | 123 | $links = $this->netscapeBookmarkUtils->filterAndFormat( |
e26e2060 A |
124 | self::$formatter, |
125 | 'private', | |
126 | false, | |
127 | '' | |
128 | ); | |
cd5327be V |
129 | $this->assertEquals(self::$refDb->countPrivateLinks(), sizeof($links)); |
130 | foreach ($links as $link) { | |
c3dfd899 | 131 | $date = $link['created']; |
cd5327be V |
132 | $this->assertEquals( |
133 | $date->getTimestamp(), | |
134 | $link['timestamp'] | |
135 | ); | |
136 | $this->assertEquals( | |
137 | str_replace(' ', ',', $link['tags']), | |
138 | $link['taglist'] | |
139 | ); | |
140 | } | |
141 | } | |
142 | ||
143 | /** | |
e26e2060 | 144 | * Prepare public bookmarks for export |
cd5327be V |
145 | */ |
146 | public function testFilterAndFormatPublic() | |
147 | { | |
e8a10f31 | 148 | $links = $this->netscapeBookmarkUtils->filterAndFormat( |
e26e2060 A |
149 | self::$formatter, |
150 | 'public', | |
151 | false, | |
152 | '' | |
153 | ); | |
cd5327be V |
154 | $this->assertEquals(self::$refDb->countPublicLinks(), sizeof($links)); |
155 | foreach ($links as $link) { | |
c3dfd899 | 156 | $date = $link['created']; |
cd5327be V |
157 | $this->assertEquals( |
158 | $date->getTimestamp(), | |
159 | $link['timestamp'] | |
160 | ); | |
161 | $this->assertEquals( | |
162 | str_replace(' ', ',', $link['tags']), | |
163 | $link['taglist'] | |
164 | ); | |
165 | } | |
166 | } | |
bb4a23aa V |
167 | |
168 | /** | |
169 | * Do not prepend notes with the Shaarli index's URL | |
170 | */ | |
171 | public function testFilterAndFormatDoNotPrependNoteUrl() | |
172 | { | |
e8a10f31 | 173 | $links = $this->netscapeBookmarkUtils->filterAndFormat( |
e26e2060 A |
174 | self::$formatter, |
175 | 'public', | |
176 | false, | |
177 | '' | |
178 | ); | |
bb4a23aa | 179 | $this->assertEquals( |
f7f08cee | 180 | '/shaare/WDWyig', |
4154c25b | 181 | $links[2]['url'] |
bb4a23aa V |
182 | ); |
183 | } | |
184 | ||
185 | /** | |
186 | * Prepend notes with the Shaarli index's URL | |
187 | */ | |
188 | public function testFilterAndFormatPrependNoteUrl() | |
189 | { | |
190 | $indexUrl = 'http://localhost:7469/shaarli/'; | |
e8a10f31 | 191 | $links = $this->netscapeBookmarkUtils->filterAndFormat( |
e26e2060 | 192 | self::$formatter, |
bb4a23aa V |
193 | 'public', |
194 | true, | |
195 | $indexUrl | |
196 | ); | |
197 | $this->assertEquals( | |
f7f08cee | 198 | $indexUrl . 'shaare/WDWyig', |
4154c25b | 199 | $links[2]['url'] |
bb4a23aa V |
200 | ); |
201 | } | |
cd5327be | 202 | } |