diff options
Diffstat (limited to 'tests/bookmark/BookmarkFilterTest.php')
-rw-r--r-- | tests/bookmark/BookmarkFilterTest.php | 74 |
1 files changed, 63 insertions, 11 deletions
diff --git a/tests/bookmark/BookmarkFilterTest.php b/tests/bookmark/BookmarkFilterTest.php index d4c71cb9..574d8e3f 100644 --- a/tests/bookmark/BookmarkFilterTest.php +++ b/tests/bookmark/BookmarkFilterTest.php | |||
@@ -2,12 +2,11 @@ | |||
2 | 2 | ||
3 | namespace Shaarli\Bookmark; | 3 | namespace Shaarli\Bookmark; |
4 | 4 | ||
5 | use Exception; | 5 | use malkusch\lock\mutex\NoMutex; |
6 | use PHPUnit\Framework\TestCase; | ||
7 | use ReferenceLinkDB; | 6 | use ReferenceLinkDB; |
8 | use Shaarli\Config\ConfigManager; | 7 | use Shaarli\Config\ConfigManager; |
9 | use Shaarli\Formatter\FormatterFactory; | ||
10 | use Shaarli\History; | 8 | use Shaarli\History; |
9 | use Shaarli\TestCase; | ||
11 | 10 | ||
12 | /** | 11 | /** |
13 | * Class BookmarkFilterTest. | 12 | * Class BookmarkFilterTest. |
@@ -36,14 +35,15 @@ class BookmarkFilterTest extends TestCase | |||
36 | /** | 35 | /** |
37 | * Instantiate linkFilter with ReferenceLinkDB data. | 36 | * Instantiate linkFilter with ReferenceLinkDB data. |
38 | */ | 37 | */ |
39 | public static function setUpBeforeClass() | 38 | public static function setUpBeforeClass(): void |
40 | { | 39 | { |
40 | $mutex = new NoMutex(); | ||
41 | $conf = new ConfigManager('tests/utils/config/configJson'); | 41 | $conf = new ConfigManager('tests/utils/config/configJson'); |
42 | $conf->set('resource.datastore', self::$testDatastore); | 42 | $conf->set('resource.datastore', self::$testDatastore); |
43 | self::$refDB = new \ReferenceLinkDB(); | 43 | self::$refDB = new \ReferenceLinkDB(); |
44 | self::$refDB->write(self::$testDatastore); | 44 | self::$refDB->write(self::$testDatastore); |
45 | $history = new History('sandbox/history.php'); | 45 | $history = new History('sandbox/history.php'); |
46 | self::$bookmarkService = new \FakeBookmarkService($conf, $history, true); | 46 | self::$bookmarkService = new \FakeBookmarkService($conf, $history, $mutex, true); |
47 | self::$linkFilter = new BookmarkFilter(self::$bookmarkService->getBookmarks()); | 47 | self::$linkFilter = new BookmarkFilter(self::$bookmarkService->getBookmarks()); |
48 | } | 48 | } |
49 | 49 | ||
@@ -190,6 +190,17 @@ class BookmarkFilterTest extends TestCase | |||
190 | } | 190 | } |
191 | 191 | ||
192 | /** | 192 | /** |
193 | * Return bookmarks for a given day | ||
194 | */ | ||
195 | public function testFilterDayRestrictedVisibility(): void | ||
196 | { | ||
197 | $this->assertEquals( | ||
198 | 3, | ||
199 | count(self::$linkFilter->filter(BookmarkFilter::$FILTER_DAY, '20121206', false, BookmarkFilter::$PUBLIC)) | ||
200 | ); | ||
201 | } | ||
202 | |||
203 | /** | ||
193 | * 404 - day not found | 204 | * 404 - day not found |
194 | */ | 205 | */ |
195 | public function testFilterUnknownDay() | 206 | public function testFilterUnknownDay() |
@@ -202,21 +213,23 @@ class BookmarkFilterTest extends TestCase | |||
202 | 213 | ||
203 | /** | 214 | /** |
204 | * Use an invalid date format | 215 | * Use an invalid date format |
205 | * @expectedException Exception | ||
206 | * @expectedExceptionMessageRegExp /Invalid date format/ | ||
207 | */ | 216 | */ |
208 | public function testFilterInvalidDayWithChars() | 217 | public function testFilterInvalidDayWithChars() |
209 | { | 218 | { |
219 | $this->expectException(\Exception::class); | ||
220 | $this->expectExceptionMessageRegExp('/Invalid date format/'); | ||
221 | |||
210 | self::$linkFilter->filter(BookmarkFilter::$FILTER_DAY, 'Rainy day, dream away'); | 222 | self::$linkFilter->filter(BookmarkFilter::$FILTER_DAY, 'Rainy day, dream away'); |
211 | } | 223 | } |
212 | 224 | ||
213 | /** | 225 | /** |
214 | * Use an invalid date format | 226 | * Use an invalid date format |
215 | * @expectedException Exception | ||
216 | * @expectedExceptionMessageRegExp /Invalid date format/ | ||
217 | */ | 227 | */ |
218 | public function testFilterInvalidDayDigits() | 228 | public function testFilterInvalidDayDigits() |
219 | { | 229 | { |
230 | $this->expectException(\Exception::class); | ||
231 | $this->expectExceptionMessageRegExp('/Invalid date format/'); | ||
232 | |||
220 | self::$linkFilter->filter(BookmarkFilter::$FILTER_DAY, '20'); | 233 | self::$linkFilter->filter(BookmarkFilter::$FILTER_DAY, '20'); |
221 | } | 234 | } |
222 | 235 | ||
@@ -240,11 +253,11 @@ class BookmarkFilterTest extends TestCase | |||
240 | 253 | ||
241 | /** | 254 | /** |
242 | * No link for this hash | 255 | * No link for this hash |
243 | * | ||
244 | * @expectedException \Shaarli\Bookmark\Exception\BookmarkNotFoundException | ||
245 | */ | 256 | */ |
246 | public function testFilterUnknownSmallHash() | 257 | public function testFilterUnknownSmallHash() |
247 | { | 258 | { |
259 | $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException::class); | ||
260 | |||
248 | self::$linkFilter->filter(BookmarkFilter::$FILTER_HASH, 'Iblaah'); | 261 | self::$linkFilter->filter(BookmarkFilter::$FILTER_HASH, 'Iblaah'); |
249 | } | 262 | } |
250 | 263 | ||
@@ -511,4 +524,43 @@ class BookmarkFilterTest extends TestCase | |||
511 | )) | 524 | )) |
512 | ); | 525 | ); |
513 | } | 526 | } |
527 | |||
528 | /** | ||
529 | * Test search result highlights in every field of bookmark reference #9. | ||
530 | */ | ||
531 | public function testFullTextSearchHighlight(): void | ||
532 | { | ||
533 | $bookmarks = self::$linkFilter->filter( | ||
534 | BookmarkFilter::$FILTER_TEXT, | ||
535 | '"psr-2" coding guide http fig "psr-2/" "This guide" basic standard. coding-style quality assurance' | ||
536 | ); | ||
537 | |||
538 | static::assertCount(1, $bookmarks); | ||
539 | static::assertArrayHasKey(9, $bookmarks); | ||
540 | |||
541 | $bookmark = $bookmarks[9]; | ||
542 | $expectedHighlights = [ | ||
543 | 'title' => [ | ||
544 | ['start' => 0, 'end' => 5], // "psr-2" | ||
545 | ['start' => 7, 'end' => 13], // coding | ||
546 | ['start' => 20, 'end' => 25], // guide | ||
547 | ], | ||
548 | 'description' => [ | ||
549 | ['start' => 0, 'end' => 10], // "This guide" | ||
550 | ['start' => 45, 'end' => 50], // basic | ||
551 | ['start' => 58, 'end' => 67], // standard. | ||
552 | ], | ||
553 | 'url' => [ | ||
554 | ['start' => 0, 'end' => 4], // http | ||
555 | ['start' => 15, 'end' => 18], // fig | ||
556 | ['start' => 27, 'end' => 33], // "psr-2/" | ||
557 | ], | ||
558 | 'tags' => [ | ||
559 | ['start' => 0, 'end' => 12], // coding-style | ||
560 | ['start' => 23, 'end' => 30], // quality | ||
561 | ['start' => 31, 'end' => 40], // assurance | ||
562 | ], | ||
563 | ]; | ||
564 | static::assertSame($expectedHighlights, $bookmark->getAdditionalContentEntry('search_highlight')); | ||
565 | } | ||
514 | } | 566 | } |