]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/netscape/BookmarkExportTest.php
Compatibility with PHPUnit 9
[github/shaarli/Shaarli.git] / tests / netscape / BookmarkExportTest.php
1 <?php
2
3 namespace Shaarli\Netscape;
4
5 use Shaarli\Bookmark\BookmarkFileService;
6 use Shaarli\Config\ConfigManager;
7 use Shaarli\Formatter\BookmarkFormatter;
8 use Shaarli\Formatter\FormatterFactory;
9 use Shaarli\History;
10 use Shaarli\TestCase;
11
12 require_once 'tests/utils/ReferenceLinkDB.php';
13
14 /**
15 * Netscape bookmark export
16 */
17 class BookmarkExportTest extends TestCase
18 {
19 /**
20 * @var string datastore to test write operations
21 */
22 protected static $testDatastore = 'sandbox/datastore.php';
23
24 /**
25 * @var ConfigManager instance.
26 */
27 protected static $conf;
28
29 /**
30 * @var \ReferenceLinkDB instance.
31 */
32 protected static $refDb = null;
33
34 /**
35 * @var BookmarkFileService private instance.
36 */
37 protected static $bookmarkService = null;
38
39 /**
40 * @var BookmarkFormatter instance
41 */
42 protected static $formatter;
43
44 /**
45 * @var History instance
46 */
47 protected static $history;
48
49 /**
50 * @var NetscapeBookmarkUtils
51 */
52 protected $netscapeBookmarkUtils;
53
54 /**
55 * Instantiate reference data
56 */
57 public static function setUpBeforeClass(): void
58 {
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 );
76 }
77
78 /**
79 * Attempt to export an invalid link selection
80 * @expectedException Exception
81 */
82 public function testFilterAndFormatInvalid()
83 {
84 $this->expectExceptionMessageRegExp('/Invalid export selection/');
85
86 $this->netscapeBookmarkUtils->filterAndFormat(
87 self::$formatter,
88 'derp',
89 false,
90 ''
91 );
92 }
93
94 /**
95 * Prepare all bookmarks for export
96 */
97 public function testFilterAndFormatAll()
98 {
99 $links = $this->netscapeBookmarkUtils->filterAndFormat(
100 self::$formatter,
101 'all',
102 false,
103 ''
104 );
105 $this->assertEquals(self::$refDb->countLinks(), sizeof($links));
106 foreach ($links as $link) {
107 $date = $link['created'];
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 /**
120 * Prepare private bookmarks for export
121 */
122 public function testFilterAndFormatPrivate()
123 {
124 $links = $this->netscapeBookmarkUtils->filterAndFormat(
125 self::$formatter,
126 'private',
127 false,
128 ''
129 );
130 $this->assertEquals(self::$refDb->countPrivateLinks(), sizeof($links));
131 foreach ($links as $link) {
132 $date = $link['created'];
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 /**
145 * Prepare public bookmarks for export
146 */
147 public function testFilterAndFormatPublic()
148 {
149 $links = $this->netscapeBookmarkUtils->filterAndFormat(
150 self::$formatter,
151 'public',
152 false,
153 ''
154 );
155 $this->assertEquals(self::$refDb->countPublicLinks(), sizeof($links));
156 foreach ($links as $link) {
157 $date = $link['created'];
158 $this->assertEquals(
159 $date->getTimestamp(),
160 $link['timestamp']
161 );
162 $this->assertEquals(
163 str_replace(' ', ',', $link['tags']),
164 $link['taglist']
165 );
166 }
167 }
168
169 /**
170 * Do not prepend notes with the Shaarli index's URL
171 */
172 public function testFilterAndFormatDoNotPrependNoteUrl()
173 {
174 $links = $this->netscapeBookmarkUtils->filterAndFormat(
175 self::$formatter,
176 'public',
177 false,
178 ''
179 );
180 $this->assertEquals(
181 '/shaare/WDWyig',
182 $links[2]['url']
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/';
192 $links = $this->netscapeBookmarkUtils->filterAndFormat(
193 self::$formatter,
194 'public',
195 true,
196 $indexUrl
197 );
198 $this->assertEquals(
199 $indexUrl . 'shaare/WDWyig',
200 $links[2]['url']
201 );
202 }
203 }