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