]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/netscape/BookmarkExportTest.php
13b85f3d832dfab4d4a7428d2c3d8fc755dea6f6
[github/shaarli/Shaarli.git] / tests / netscape / BookmarkExportTest.php
1 <?php
2
3 namespace Shaarli\Netscape;
4
5 use PHPUnit\Framework\TestCase;
6 use Shaarli\Bookmark\BookmarkFileService;
7 use Shaarli\Config\ConfigManager;
8 use Shaarli\Formatter\BookmarkFormatter;
9 use Shaarli\Formatter\FormatterFactory;
10 use Shaarli\History;
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 * @expectedExceptionMessageRegExp /Invalid export selection/
82 */
83 public function testFilterAndFormatInvalid()
84 {
85 $this->netscapeBookmarkUtils->filterAndFormat(
86 self::$formatter,
87 'derp',
88 false,
89 ''
90 );
91 }
92
93 /**
94 * Prepare all bookmarks for export
95 */
96 public function testFilterAndFormatAll()
97 {
98 $links = $this->netscapeBookmarkUtils->filterAndFormat(
99 self::$formatter,
100 'all',
101 false,
102 ''
103 );
104 $this->assertEquals(self::$refDb->countLinks(), sizeof($links));
105 foreach ($links as $link) {
106 $date = $link['created'];
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 /**
119 * Prepare private bookmarks for export
120 */
121 public function testFilterAndFormatPrivate()
122 {
123 $links = $this->netscapeBookmarkUtils->filterAndFormat(
124 self::$formatter,
125 'private',
126 false,
127 ''
128 );
129 $this->assertEquals(self::$refDb->countPrivateLinks(), sizeof($links));
130 foreach ($links as $link) {
131 $date = $link['created'];
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 /**
144 * Prepare public bookmarks for export
145 */
146 public function testFilterAndFormatPublic()
147 {
148 $links = $this->netscapeBookmarkUtils->filterAndFormat(
149 self::$formatter,
150 'public',
151 false,
152 ''
153 );
154 $this->assertEquals(self::$refDb->countPublicLinks(), sizeof($links));
155 foreach ($links as $link) {
156 $date = $link['created'];
157 $this->assertEquals(
158 $date->getTimestamp(),
159 $link['timestamp']
160 );
161 $this->assertEquals(
162 str_replace(' ', ',', $link['tags']),
163 $link['taglist']
164 );
165 }
166 }
167
168 /**
169 * Do not prepend notes with the Shaarli index's URL
170 */
171 public function testFilterAndFormatDoNotPrependNoteUrl()
172 {
173 $links = $this->netscapeBookmarkUtils->filterAndFormat(
174 self::$formatter,
175 'public',
176 false,
177 ''
178 );
179 $this->assertEquals(
180 '/shaare/WDWyig',
181 $links[2]['url']
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/';
191 $links = $this->netscapeBookmarkUtils->filterAndFormat(
192 self::$formatter,
193 'public',
194 true,
195 $indexUrl
196 );
197 $this->assertEquals(
198 $indexUrl . 'shaare/WDWyig',
199 $links[2]['url']
200 );
201 }
202 }