File: ParseNetscapeBookmarksTest.php

package info (click to toggle)
php-netscape-bookmark-parser 4.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 636 kB
  • sloc: php: 3,592; makefile: 49; sh: 3
file content (368 lines) | stat: -rw-r--r-- 10,757 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
<?php

declare(strict_types=1);

namespace Shaarli\NetscapeBookmarkParser\Tests\Unit\Encoder;

use Shaarli\NetscapeBookmarkParser\Encoder\NetscapeBookmarkDecoder;
use PHPUnit\Framework\TestCase;

/**
 * Ensure basic Netscape bookmarks are properly parsed.
 *
 * @see https://msdn.microsoft.com/en-us/library/aa753582%28v=vs.85%29.aspx
 * @see http://www.w3schools.com/tags/tag_dl.asp
 */
class ParseNetscapeBookmarksTest extends TestCase
{
    public const FIXTURE_DIRECTORY = __DIR__ . '/../../Fixtures/Encoder/';

    public const EXPECTED_1 = [
        [
            'name' => 'Secret stuff',
            'image'  => null,
            'url'   => 'https://private.tld',
            'tags'  => [
                'private',
                'secret',
            ],
            'description'  => "Super-secret stuff you're not supposed to know about",
            'dateCreated'  => 971175336,
            'public'   => false,
        ],
        [
            'name' => 'Public stuff',
            'image'  => null,
            'url'   => 'http://public.tld',
            'tags'  => [
                'public',
                'hello',
                'world',
            ],
            'description'  => null,
            'dateCreated'  => 1456433748,
            'public'   => true,
        ],
    ];

    public const EXPECTED_2 = [
        [
            'name' => 'Multiline desc',
            'image'  => null,
            'url'   => 'http://multi.li.ne/1',
            'tags'  => [
                'multi',
            ],
            'description'  => "List:\n- item1\n- item2\n- item3",
            'dateCreated'  => 1456433741,
            'public'   => true,
        ],
        [
            'name' => 'Multiline desc',
            'image'  => null,
            'url'   => 'http://multi.li.ne/2',
            'tags'  => [
                'multi',
            ],
            'description'  => "Nested lists:\n- list1\n  - item1.1\n  - item1.2\n  - item1.3\n- list2\n  - item2.1",
            'dateCreated'  => 1456433742,
            'public'   => true,
        ],
        [
            'name' => 'Multiline desc',
            'image'  => null,
            'url'   => 'http://multi.li.ne/3',
            'tags'  => [
                'multi',
            ],
            'description'  => "List:\n- item1\n- item2\n\nParagraph number one.\n\nParagraph\nnumber\ntwo.",
            'dateCreated'  => 1456433747,
            'public'   => true,
        ],
    ];

    public const EXPECTED_3 = [
        [
            'name' => 'Nested 1',
            'image'  => null,
            'url'   => 'http://nest.ed/1',
            'tags'  => [
                'tag1',
                'tag2',
                'multi word',
            ],
            'description'  => null,
            'dateCreated'  => 1456433741,
            'public'   => true,
        ],
        [
            'name' => 'Nested 1-1',
            'image'  => null,
            'url'   => 'http://nest.ed/1-1',
            'tags'  => [
                'folder1',
                'the first',
                'folder to encounter',
                'tag1',
                'tag2',
                'multi word',
            ],
            'description'  => null,
            'dateCreated'  => 1456433742,
            'public'   => true,
        ],
        [
            'name' => 'Nested 1-2',
            'image'  => null,
            'url'   => 'http://nest.ed/1-2',
            'tags'  => [
                'folder1',
                'the first',
                'folder to encounter',
                'tag3',
                'tag4',
                'leaf multi word',
            ],
            'description'  => null,
            'dateCreated'  => 1456433747,
            'public'   => true,
        ],
        [
            'name' => 'Nested 2-1',
            'image'  => null,
            'url'   => 'http://nest.ed/2-1',
            'tags'  => [
                'folder2',
            ],
            'description'  => 'First link of the second section',
            'dateCreated'  => 1454433742,
            'public'   => true,
        ],
        [
            'name' => 'Nested 2-2',
            'image'  => null,
            'url'   => 'http://nest.ed/2-2',
            'tags'  => [
                'folder2',
            ],
            'description'  => 'Second link of the second section',
            'dateCreated'  => 1453233747,
            'public'   => true,
        ],
        [
            'name' => 'Nested 3-1',
            'image'  => null,
            'url'   => 'http://nest.ed/3-1',
            'tags'  => [
                'folder3',
                'folder3-1',
                'tag3',
            ],
            'description'  => null,
            'dateCreated'  => 1454433742,
            'public'   => true,
        ],
        [
            'name' => 'Nested 3-2',
            'image'  => null,
            'url'   => 'http://nest.ed/3-2',
            'tags'  => [
                'folder3',
                'folder3-1',
            ],
            'description'  => null,
            'dateCreated'  => 1453233747,
            'public'   => true,
        ],
        [
            'name' => 'Nested 2',
            'image'  => null,
            'url'   => 'http://nest.ed/2',
            'tags'  => [
                'tag4',
            ],
            'description'  => null,
            'dateCreated'  => 1456733741,
            'public'   => true,
        ],
    ];

    // phpcs:disable Generic.Files.LineLength
    public const EXPECTED_4 = [
        [
            'name' => 'The hunt for the fish pirates who exploit the sea - BBC Future',
            'image'  => null,
            'url'   => 'https://www.bbc.com/future/article/20190213-the-dramatic-hunt-for-the-fish-pirates-exploiting-our-seas',
            'tags'  => [
                'story',
                'oceans',
            ],
            'description'  => "For 10 years, a rogue fishing vessel and its crew plundered the world’s oceans, escaping repeated attempts of capture. Then a dramatic pursuit finally netted the one that got away.\n<A href=\"http://localhost.localdomain:8083/Shaarli/?JVvqCA\">\n<img src=\"http://localhost.localdomain:8083/Shaarli/cache/thumb/290ccda0deea6083ee613d358446103e/c975558ad43acdbd982ffafd8c01163d6c9ec5ca125901.jpg\"/>\n</A>",
            'dateCreated'  => 1591451445,
            'public'   => false,
        ],
    ];
    // phpcs:enable

    /**
     * @var NetscapeBookmarkDecoder|null
     */
    private $decoder;

    /**
     * Parse a basic Netscape file.
     */
    public function testParseBasic(): void
    {
        $content = \file_get_contents(self::FIXTURE_DIRECTORY . 'input/netscape_basic.htm');
        $result = $this->decoder->decode($content);

        $this->assertSame(self::EXPECTED_1, $result);
    }

    /**
     * Parse a Netscape file containing multiline descriptions.
     */
    public function testParseMultilineDescriptions(): void
    {
        $content = \file_get_contents(self::FIXTURE_DIRECTORY . 'input/netscape_multiline.htm');
        $result = $this->decoder->decode($content);

        $this->assertSame(self::EXPECTED_2, $result);
    }

    /**
     * Parse bookmarks nested in folders.
     */
    public function testParseNested(): void
    {
        $content = \file_get_contents(self::FIXTURE_DIRECTORY . 'input/netscape_nested.htm');
        $result = $this->decoder->decode($content);

        $this->assertSame(self::EXPECTED_3, $result);
    }

    /**
     * Leave links with no/empty tag as-is.
     */
    public function testEmptyDefaultTag(): void
    {
        $content = '<A HREF="http://no.tag">NoTag</A>';
        $result = $this->decoder->decode($content);

        $expected = [
            [
                'name' => 'NoTag',
                'image'  => null,
                'url'   => 'http://no.tag',
                'tags'  => [],
                'description'  => null,
                'dateCreated'  => null,
                'public'   => null,
            ],
        ];

        $this->assertSame($expected, $result);
    }

    /**
     * Keep empty tags.
     */
    public function testParseEmptyTags(): void
    {
        $content = '<A HREF="http://empty.tag" TAGS="">EmptyTag</A>';
        $result = $this->decoder->decode($content);

        $expected = [
            [
                'name' => 'EmptyTag',
                'image'  => null,
                'url'   => 'http://empty.tag',
                'tags'  => [],
                'description'  => null,
                'dateCreated'  => null,
                'public'   => null,
            ],
        ];

        $this->assertSame($expected, $result);
    }

    /**
     * Parse space-separated tags.
     */
    public function testParseSpaceTags(): void
    {
        $content = '<A HREF="http://space.tag" TAGS="t1 t2">SpaceTag</A>';
        $result = $this->decoder->decode($content);

        $expected = [
            [
                'name' => 'SpaceTag',
                'image'  => null,
                'url'   => 'http://space.tag',
                'tags'  => [
                    't1',
                    't2',
                ],
                'description'  => null,
                'dateCreated'  => null,
                'public'   => null,
            ],
        ];

        $this->assertSame($expected, $result);
    }

    /**
     * Parse comma-separated tags.
     */
    public function testParseCommaTags(): void
    {
        $content = '<A HREF="http://comma.tag" TAGS="t1,t2,t3">CommaTag</A>';
        $result = $this->decoder->decode($content);

        $expected = [
            [
                'name' => 'CommaTag',
                'image'  => null,
                'url'   => 'http://comma.tag',
                'tags'  => [
                    't1',
                    't2',
                    't3',
                ],
                'description'  => null,
                'dateCreated'  => null,
                'public'   => null,
            ],
        ];

        $this->assertSame($expected, $result);
    }

    /**
     * Parse a extended Netscape file from Shaarli.
     */
    public function testParseExtended(): void
    {
        $content = \file_get_contents(self::FIXTURE_DIRECTORY . 'input/netscape_extended.htm');
        $result = $this->decoder->decode($content);

        $this->assertSame(self::EXPECTED_4, $result);
    }

    // --------------------------------------------------
    // Setup / Tear Down
    // --------------------------------------------------

    protected function setUp(): void
    {
        $this->decoder = new NetscapeBookmarkDecoder();
    }

    protected function tearDown(): void
    {
        $this->decoder = null;
    }
}