]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/LinkDBTest.php
Improved search: combine AND, exact terms and exclude search.
[github/shaarli/Shaarli.git] / tests / LinkDBTest.php
CommitLineData
ca74886f
V
1<?php
2/**
3 * Link datastore tests
4 */
5
01e48f26 6require_once 'application/Cache.php';
2e28269b 7require_once 'application/FileUtils.php';
ca74886f
V
8require_once 'application/LinkDB.php';
9require_once 'application/Utils.php';
10require_once 'tests/utils/ReferenceLinkDB.php';
11
ca74886f
V
12
13/**
14 * Unitary tests for LinkDB
15 */
16class LinkDBTest extends PHPUnit_Framework_TestCase
17{
18 // datastore to test write operations
4bf35ba5 19 protected static $testDatastore = 'sandbox/datastore.php';
ca74886f
V
20 protected static $refDB = null;
21 protected static $publicLinkDB = null;
22 protected static $privateLinkDB = null;
23
24 /**
25 * Instantiates public and private LinkDBs with test data
26 *
27 * The reference datastore contains public and private links that
28 * will be used to test LinkDB's methods:
29 * - access filtering (public/private),
30 * - link searches:
31 * - by day,
32 * - by tag,
33 * - by text,
34 * - etc.
35 */
36 public static function setUpBeforeClass()
37 {
38 self::$refDB = new ReferenceLinkDB();
9c8752a2 39 self::$refDB->write(self::$testDatastore);
ca74886f 40
9c8752a2
V
41 self::$publicLinkDB = new LinkDB(self::$testDatastore, false, false);
42 self::$privateLinkDB = new LinkDB(self::$testDatastore, true, false);
ca74886f
V
43 }
44
45 /**
46 * Resets test data for each test
47 */
48 protected function setUp()
49 {
ca74886f
V
50 if (file_exists(self::$testDatastore)) {
51 unlink(self::$testDatastore);
52 }
53 }
54
55 /**
56 * Allows to test LinkDB's private methods
57 *
58 * @see
59 * https://sebastian-bergmann.de/archives/881-Testing-Your-Privates.html
60 * http://stackoverflow.com/a/2798203
61 */
62 protected static function getMethod($name)
63 {
64 $class = new ReflectionClass('LinkDB');
65 $method = $class->getMethod($name);
66 $method->setAccessible(true);
67 return $method;
68 }
69
70 /**
71 * Instantiate LinkDB objects - logged in user
72 */
73 public function testConstructLoggedIn()
74 {
9c8752a2 75 new LinkDB(self::$testDatastore, true, false);
ca74886f
V
76 $this->assertFileExists(self::$testDatastore);
77 }
78
79 /**
80 * Instantiate LinkDB objects - logged out or public instance
81 */
82 public function testConstructLoggedOut()
83 {
9c8752a2 84 new LinkDB(self::$testDatastore, false, false);
ca74886f
V
85 $this->assertFileExists(self::$testDatastore);
86 }
87
88 /**
89 * Attempt to instantiate a LinkDB whereas the datastore is not writable
90 *
2e28269b
V
91 * @expectedException IOException
92 * @expectedExceptionMessageRegExp /Error accessing null/
ca74886f
V
93 */
94 public function testConstructDatastoreNotWriteable()
95 {
9c8752a2 96 new LinkDB('null/store.db', false, false);
ca74886f
V
97 }
98
99 /**
100 * The DB doesn't exist, ensure it is created with dummy content
101 */
102 public function testCheckDBNew()
103 {
9c8752a2 104 $linkDB = new LinkDB(self::$testDatastore, false, false);
ca74886f
V
105 unlink(self::$testDatastore);
106 $this->assertFileNotExists(self::$testDatastore);
107
07b6fa75 108 $checkDB = self::getMethod('_checkDB');
ca74886f
V
109 $checkDB->invokeArgs($linkDB, array());
110 $this->assertFileExists(self::$testDatastore);
111
112 // ensure the correct data has been written
0037fbe1 113 $this->assertGreaterThan(0, filesize(self::$testDatastore));
ca74886f
V
114 }
115
116 /**
117 * The DB exists, don't do anything
118 */
119 public function testCheckDBLoad()
120 {
9c8752a2 121 $linkDB = new LinkDB(self::$testDatastore, false, false);
0037fbe1
V
122 $datastoreSize = filesize(self::$testDatastore);
123 $this->assertGreaterThan(0, $datastoreSize);
ca74886f 124
07b6fa75 125 $checkDB = self::getMethod('_checkDB');
ca74886f
V
126 $checkDB->invokeArgs($linkDB, array());
127
128 // ensure the datastore is left unmodified
129 $this->assertEquals(
0037fbe1 130 $datastoreSize,
30e6f1ca 131 filesize(self::$testDatastore)
ca74886f
V
132 );
133 }
134
135 /**
136 * Load an empty DB
137 */
138 public function testReadEmptyDB()
139 {
9c8752a2
V
140 file_put_contents(self::$testDatastore, '<?php /* S7QysKquBQA= */ ?>');
141 $emptyDB = new LinkDB(self::$testDatastore, false, false);
ca74886f
V
142 $this->assertEquals(0, sizeof($emptyDB));
143 $this->assertEquals(0, count($emptyDB));
144 }
145
146 /**
147 * Load public links from the DB
148 */
149 public function testReadPublicDB()
150 {
151 $this->assertEquals(
152 self::$refDB->countPublicLinks(),
153 sizeof(self::$publicLinkDB)
154 );
155 }
156
157 /**
158 * Load public and private links from the DB
159 */
160 public function testReadPrivateDB()
161 {
162 $this->assertEquals(
163 self::$refDB->countLinks(),
164 sizeof(self::$privateLinkDB)
165 );
166 }
167
168 /**
169 * Save the links to the DB
170 */
171 public function testSaveDB()
172 {
9c8752a2 173 $testDB = new LinkDB(self::$testDatastore, true, false);
ca74886f
V
174 $dbSize = sizeof($testDB);
175
176 $link = array(
177 'title'=>'an additional link',
178 'url'=>'http://dum.my',
179 'description'=>'One more',
180 'private'=>0,
181 'linkdate'=>'20150518_190000',
182 'tags'=>'unit test'
183 );
184 $testDB[$link['linkdate']] = $link;
01e48f26 185 $testDB->savedb('tests');
ca74886f 186
9c8752a2 187 $testDB = new LinkDB(self::$testDatastore, true, false);
ca74886f
V
188 $this->assertEquals($dbSize + 1, sizeof($testDB));
189 }
190
191 /**
192 * Count existing links
193 */
194 public function testCount()
195 {
196 $this->assertEquals(
197 self::$refDB->countPublicLinks(),
198 self::$publicLinkDB->count()
199 );
200 $this->assertEquals(
201 self::$refDB->countLinks(),
202 self::$privateLinkDB->count()
203 );
204 }
205
9f15ca9e
V
206 /**
207 * Count existing links - public links hidden
208 */
209 public function testCountHiddenPublic()
210 {
9c8752a2 211 $linkDB = new LinkDB(self::$testDatastore, false, true);
9f15ca9e
V
212
213 $this->assertEquals(
214 0,
215 $linkDB->count()
216 );
217 $this->assertEquals(
218 0,
219 $linkDB->count()
220 );
221 }
222
ca74886f
V
223 /**
224 * List the days for which links have been posted
225 */
226 public function testDays()
227 {
228 $this->assertEquals(
d1e2f8e5 229 array('20121206', '20130614', '20150310'),
ca74886f
V
230 self::$publicLinkDB->days()
231 );
232
233 $this->assertEquals(
d1e2f8e5 234 array('20121206', '20130614', '20141125', '20150310'),
ca74886f
V
235 self::$privateLinkDB->days()
236 );
237 }
238
239 /**
240 * The URL corresponds to an existing entry in the DB
241 */
242 public function testGetKnownLinkFromURL()
243 {
244 $link = self::$publicLinkDB->getLinkFromUrl('http://mediagoblin.org/');
245
246 $this->assertNotEquals(false, $link);
247 $this->assertEquals(
248 'A free software media publishing platform',
249 $link['description']
250 );
251 }
252
253 /**
254 * The URL is not in the DB
255 */
256 public function testGetUnknownLinkFromURL()
257 {
258 $this->assertEquals(
259 false,
260 self::$publicLinkDB->getLinkFromUrl('http://dev.null')
261 );
262 }
263
264 /**
265 * Lists all tags
266 */
267 public function testAllTags()
268 {
269 $this->assertEquals(
d1e2f8e5 270 array(
ca74886f
V
271 'web' => 3,
272 'cartoon' => 2,
273 'gnu' => 2,
274 'dev' => 1,
275 'samba' => 1,
276 'media' => 1,
277 'software' => 1,
278 'stallman' => 1,
21979ff1
A
279 'free' => 1,
280 '-exclude' => 1,
bedd176a 281 'stuff' => 2,
d1e2f8e5 282 ),
ca74886f
V
283 self::$publicLinkDB->allTags()
284 );
285
286 $this->assertEquals(
d1e2f8e5 287 array(
ca74886f
V
288 'web' => 4,
289 'cartoon' => 3,
290 'gnu' => 2,
291 'dev' => 2,
292 'samba' => 1,
293 'media' => 1,
294 'software' => 1,
295 'stallman' => 1,
296 'free' => 1,
297 'html' => 1,
298 'w3c' => 1,
299 'css' => 1,
21979ff1
A
300 'Mercurial' => 1,
301 '-exclude' => 1,
195acf9f 302 '.hidden' => 1,
d1e2f8e5 303 ),
ca74886f
V
304 self::$privateLinkDB->allTags()
305 );
306 }
307
308 /**
822bffce 309 * Test real_url without redirector.
ca74886f 310 */
822bffce 311 public function testLinkRealUrlWithoutRedirector()
ca74886f 312 {
822bffce
A
313 $db = new LinkDB(self::$testDatastore, false, false);
314 foreach($db as $link) {
315 $this->assertEquals($link['url'], $link['real_url']);
316 }
ca74886f
V
317 }
318
319 /**
822bffce 320 * Test real_url with redirector.
ca74886f 321 */
822bffce 322 public function testLinkRealUrlWithRedirector()
ca74886f 323 {
822bffce
A
324 $redirector = 'http://redirector.to?';
325 $db = new LinkDB(self::$testDatastore, false, false, $redirector);
326 foreach($db as $link) {
327 $this->assertStringStartsWith($redirector, $link['real_url']);
328 }
ca74886f
V
329 }
330
331 /**
822bffce 332 * Test filter with string.
ca74886f 333 */
822bffce 334 public function testFilterString()
ca74886f 335 {
822bffce 336 $tags = 'dev cartoon';
ca74886f
V
337 $this->assertEquals(
338 2,
822bffce 339 count(self::$privateLinkDB->filter(LinkFilter::$FILTER_TAG, $tags, true, false))
ca74886f
V
340 );
341 }
342
343 /**
822bffce 344 * Test filter with string.
ca74886f 345 */
822bffce 346 public function testFilterArray()
ca74886f 347 {
822bffce 348 $tags = array('dev', 'cartoon');
ca74886f
V
349 $this->assertEquals(
350 2,
822bffce 351 count(self::$privateLinkDB->filter(LinkFilter::$FILTER_TAG, $tags, true, false))
ca74886f
V
352 );
353 }
195acf9f
A
354
355 /**
356 * Test hidden tags feature:
357 * tags starting with a dot '.' are only visible when logged in.
358 */
359 public function testHiddenTags()
360 {
361 $tags = '.hidden';
362 $this->assertEquals(
363 1,
364 count(self::$privateLinkDB->filter(LinkFilter::$FILTER_TAG, $tags, true, false))
365 );
366
367 $this->assertEquals(
368 0,
369 count(self::$publicLinkDB->filter(LinkFilter::$FILTER_TAG, $tags, true, false))
370 );
371 }
ca74886f 372}