From b3bd8c3e8d367975980043e772f7cd78b7f96bc6 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Thu, 22 Oct 2020 16:21:03 +0200 Subject: Feature: support any tag separator So it allows to have multiple words tags. Breaking change: commas ',' are no longer a default separator. Fixes #594 --- tests/bookmark/BookmarkFilterTest.php | 2 +- tests/bookmark/BookmarkTest.php | 25 +++++-- tests/bookmark/LinkUtilsTest.php | 124 ++++++++++++++++++++++++++++++++-- 3 files changed, 141 insertions(+), 10 deletions(-) (limited to 'tests/bookmark') diff --git a/tests/bookmark/BookmarkFilterTest.php b/tests/bookmark/BookmarkFilterTest.php index 574d8e3f..835674f2 100644 --- a/tests/bookmark/BookmarkFilterTest.php +++ b/tests/bookmark/BookmarkFilterTest.php @@ -44,7 +44,7 @@ class BookmarkFilterTest extends TestCase self::$refDB->write(self::$testDatastore); $history = new History('sandbox/history.php'); self::$bookmarkService = new \FakeBookmarkService($conf, $history, $mutex, true); - self::$linkFilter = new BookmarkFilter(self::$bookmarkService->getBookmarks()); + self::$linkFilter = new BookmarkFilter(self::$bookmarkService->getBookmarks(), $conf); } /** diff --git a/tests/bookmark/BookmarkTest.php b/tests/bookmark/BookmarkTest.php index 4c1ae25d..cb91b26b 100644 --- a/tests/bookmark/BookmarkTest.php +++ b/tests/bookmark/BookmarkTest.php @@ -78,6 +78,23 @@ class BookmarkTest extends TestCase $this->assertTrue($bookmark->isNote()); } + /** + * Test fromArray() with a link with a custom tags separator + */ + public function testFromArrayCustomTagsSeparator() + { + $data = [ + 'id' => 1, + 'tags' => ['tag1', 'tag2', 'chair'], + ]; + + $bookmark = (new Bookmark())->fromArray($data, '@'); + $this->assertEquals($data['id'], $bookmark->getId()); + $this->assertEquals($data['tags'], $bookmark->getTags()); + $this->assertEquals('tag1@tag2@chair', $bookmark->getTagsString('@')); + } + + /** * Test validate() with a valid minimal bookmark */ @@ -252,7 +269,7 @@ class BookmarkTest extends TestCase { $bookmark = new Bookmark(); - $str = 'tag1 tag2 tag3.tag3-2, tag4 , -tag5 '; + $str = 'tag1 tag2 tag3.tag3-2 tag4 -tag5 '; $bookmark->setTagsString($str); $this->assertEquals( [ @@ -276,9 +293,9 @@ class BookmarkTest extends TestCase $array = [ 'tag1 ', ' tag2', - 'tag3.tag3-2,', - ', tag4', - ', ', + 'tag3.tag3-2', + ' tag4', + ' ', '-tag5 ', ]; $bookmark->setTags($array); diff --git a/tests/bookmark/LinkUtilsTest.php b/tests/bookmark/LinkUtilsTest.php index 3321242f..c2f9f930 100644 --- a/tests/bookmark/LinkUtilsTest.php +++ b/tests/bookmark/LinkUtilsTest.php @@ -247,7 +247,8 @@ class LinkUtilsTest extends TestCase $title, $desc, $keywords, - false + false, + ' ' ); $data = [ @@ -297,7 +298,8 @@ class LinkUtilsTest extends TestCase $title, $desc, $keywords, - false + false, + ' ' ); $data = [ @@ -330,7 +332,8 @@ class LinkUtilsTest extends TestCase $title, $desc, $keywords, - false + false, + ' ' ); $data = [ @@ -363,7 +366,8 @@ class LinkUtilsTest extends TestCase $title, $desc, $keywords, - false + false, + ' ' ); $data = [ @@ -428,7 +432,8 @@ class LinkUtilsTest extends TestCase $title, $desc, $keywords, - true + true, + ' ' ); $data = [ 'th=device-width">' @@ -574,6 +579,115 @@ class LinkUtilsTest extends TestCase $this->assertFalse(is_note('https://github.com/shaarli/Shaarli/?hi')); } + /** + * Test tags_str2array with whitespace separator. + */ + public function testTagsStr2ArrayWithSpaceSeparator(): void + { + $separator = ' '; + + static::assertSame(['tag1', 'tag2', 'tag3'], tags_str2array('tag1 tag2 tag3', $separator)); + static::assertSame(['tag1', 'tag2', 'tag3'], tags_str2array('tag1 tag2 tag3', $separator)); + static::assertSame(['tag1', 'tag2', 'tag3'], tags_str2array(' tag1 tag2 tag3 ', $separator)); + static::assertSame(['tag1@', 'tag2,', '.tag3'], tags_str2array(' tag1@ tag2, .tag3 ', $separator)); + static::assertSame([], tags_str2array('', $separator)); + static::assertSame([], tags_str2array(' ', $separator)); + static::assertSame([], tags_str2array(null, $separator)); + } + + /** + * Test tags_str2array with @ separator. + */ + public function testTagsStr2ArrayWithCharSeparator(): void + { + $separator = '@'; + + static::assertSame(['tag1', 'tag2', 'tag3'], tags_str2array('tag1@tag2@tag3', $separator)); + static::assertSame(['tag1', 'tag2', 'tag3'], tags_str2array('tag1@@@@tag2@@@@tag3', $separator)); + static::assertSame(['tag1', 'tag2', 'tag3'], tags_str2array('@@@tag1@@@tag2@@@@tag3@@', $separator)); + static::assertSame( + ['tag1#', 'tag2, and other', '.tag3'], + tags_str2array('@@@ tag1# @@@ tag2, and other @@@@.tag3@@', $separator) + ); + static::assertSame([], tags_str2array('', $separator)); + static::assertSame([], tags_str2array(' ', $separator)); + static::assertSame([], tags_str2array(null, $separator)); + } + + /** + * Test tags_array2str with ' ' separator. + */ + public function testTagsArray2StrWithSpaceSeparator(): void + { + $separator = ' '; + + static::assertSame('tag1 tag2 tag3', tags_array2str(['tag1', 'tag2', 'tag3'], $separator)); + static::assertSame('tag1, tag2@ tag3', tags_array2str(['tag1,', 'tag2@', 'tag3'], $separator)); + static::assertSame('tag1 tag2 tag3', tags_array2str([' tag1 ', 'tag2', 'tag3 '], $separator)); + static::assertSame('tag1 tag2 tag3', tags_array2str([' tag1 ', ' ', 'tag2', ' ', 'tag3 '], $separator)); + static::assertSame('tag1', tags_array2str([' tag1 '], $separator)); + static::assertSame('', tags_array2str([' '], $separator)); + static::assertSame('', tags_array2str([], $separator)); + static::assertSame('', tags_array2str(null, $separator)); + } + + /** + * Test tags_array2str with @ separator. + */ + public function testTagsArray2StrWithCharSeparator(): void + { + $separator = '@'; + + static::assertSame('tag1@tag2@tag3', tags_array2str(['tag1', 'tag2', 'tag3'], $separator)); + static::assertSame('tag1,@tag2@tag3', tags_array2str(['tag1,', 'tag2@', 'tag3'], $separator)); + static::assertSame( + 'tag1@tag2, and other@tag3', + tags_array2str(['@@@@ tag1@@@', ' @tag2, and other @', 'tag3@@@@'], $separator) + ); + static::assertSame('tag1@tag2@tag3', tags_array2str(['@@@tag1@@@', '@', 'tag2', '@@@', 'tag3@@@'], $separator)); + static::assertSame('tag1', tags_array2str(['@@@@tag1@@@@'], $separator)); + static::assertSame('', tags_array2str(['@@@'], $separator)); + static::assertSame('', tags_array2str([], $separator)); + static::assertSame('', tags_array2str(null, $separator)); + } + + /** + * Test tags_array2str with @ separator. + */ + public function testTagsFilterWithSpaceSeparator(): void + { + $separator = ' '; + + static::assertSame(['tag1', 'tag2', 'tag3'], tags_filter(['tag1', 'tag2', 'tag3'], $separator)); + static::assertSame(['tag1,', 'tag2@', 'tag3'], tags_filter(['tag1,', 'tag2@', 'tag3'], $separator)); + static::assertSame(['tag1', 'tag2', 'tag3'], tags_filter([' tag1 ', 'tag2', 'tag3 '], $separator)); + static::assertSame(['tag1', 'tag2', 'tag3'], tags_filter([' tag1 ', ' ', 'tag2', ' ', 'tag3 '], $separator)); + static::assertSame(['tag1'], tags_filter([' tag1 '], $separator)); + static::assertSame([], tags_filter([' '], $separator)); + static::assertSame([], tags_filter([], $separator)); + static::assertSame([], tags_filter(null, $separator)); + } + + /** + * Test tags_array2str with @ separator. + */ + public function testTagsArrayFilterWithSpaceSeparator(): void + { + $separator = '@'; + + static::assertSame(['tag1', 'tag2', 'tag3'], tags_filter(['tag1', 'tag2', 'tag3'], $separator)); + static::assertSame(['tag1,', 'tag2#', 'tag3'], tags_filter(['tag1,', 'tag2#', 'tag3'], $separator)); + static::assertSame( + ['tag1', 'tag2, and other', 'tag3'], + tags_filter(['@@@@ tag1@@@', ' @tag2, and other @', 'tag3@@@@'], $separator) + ); + static::assertSame(['tag1', 'tag2', 'tag3'], tags_filter(['@@@tag1@@@', '@', 'tag2', '@@@', 'tag3@@@'], $separator)); + static::assertSame(['tag1'], tags_filter(['@@@@tag1@@@@'], $separator)); + static::assertSame([], tags_filter(['@@@'], $separator)); + static::assertSame([], tags_filter([], $separator)); + static::assertSame([], tags_filter(null, $separator)); + } + /** * Util function to build an hashtag link. * -- cgit v1.2.3