From 2d7c995738b4ff5106a19b787035a4f0f6e1745b Mon Sep 17 00:00:00 2001 From: fr0tt <33751346+fr0tt@users.noreply.github.com> Date: Fri, 11 Aug 2023 12:43:56 +0200 Subject: [PATCH] fix and test tag im/export --- app/Helpers/NetscapeBookmarkDecoder.php | 26 ++++++++++++++++++++----- app/Helpers/NetscapeBookmarkEncoder.php | 4 +++- tests/Feature/ImAndExportTest.php | 12 ++++++++++-- tests/bookmarks.html | 2 +- 4 files changed, 35 insertions(+), 9 deletions(-) diff --git a/app/Helpers/NetscapeBookmarkDecoder.php b/app/Helpers/NetscapeBookmarkDecoder.php index e829640..2e53b8f 100644 --- a/app/Helpers/NetscapeBookmarkDecoder.php +++ b/app/Helpers/NetscapeBookmarkDecoder.php @@ -4,6 +4,8 @@ use App\Models\Collection; use App\Services\PostService; +use App\Services\TagService; +use Illuminate\Support\Str; /* * This file is heavily inspired by: @@ -38,6 +40,7 @@ class NetscapeBookmarkDecoder { protected $items; private PostService $postService; + private TagService $tagService; private $userId; const TRUE_PATTERN = 'y|yes|on|checked|ok|1|true|array|\+|okay|yes|t|one'; @@ -50,6 +53,7 @@ class NetscapeBookmarkDecoder public function __construct(int $userId) { $this->postService = new PostService(); + $this->tagService = new TagService(); $this->userId = $userId; } @@ -119,10 +123,11 @@ public function parseString(string $bookmarkString, int $collectionId): array $tags = array(); if (preg_match('/(tags?|labels?|folders?)="(.*?)"/i', $line, $m7)) { - $tags = array_merge( - $tags, - explode(' ', strtr($m7[2], ',', ' ')) - ); + if (Str::contains($m7[2], ',')) { + $tags = preg_split('/\s*,\s*/', $m7[2]); + } else { + $tags = preg_split('/\s+/', $m7[2]); + } } $this->items[$i]['tags'] = $tags; @@ -268,9 +273,20 @@ private function createPost($title, $collectionId, $url, $description, $tags = n $url, $collectionId, $description, - count($tags) > 0 ? $tags : null, + $this->createTags($tags), $this->userId, ); } + private function createTags(array $tagNames): array|null + { + if (count($tagNames) === 0) { + return null; + } + return array_map(function ($tagName) { + $tag = $this->tagService->saveTag($tagName, $this->userId); + return $tag->id; + }, $tagNames); + } + } diff --git a/app/Helpers/NetscapeBookmarkEncoder.php b/app/Helpers/NetscapeBookmarkEncoder.php index 0c1a866..c057342 100644 --- a/app/Helpers/NetscapeBookmarkEncoder.php +++ b/app/Helpers/NetscapeBookmarkEncoder.php @@ -107,12 +107,14 @@ private function getPosts(int $collectionId = null, int $level): string $createdAt = strtotime($post->created_at); $updatedAt = strtotime($post->updated_at); $description = trim($post->description); + $tags = $post->tags->implode('name', ', '); + $content .= "{$this->tabs($level)}" . "
url}\" " . "ADD_DATE=\"{$createdAt}\" " . "LAST_MODIFIED=\"{$updatedAt}\" " . "ICON=\"{$this->icon($post->base_url)}\" " . - "TAGS=\"{$post->tags->join(', ')}\"" . + "TAGS=\"{$tags}\"" . ">{$post->title}\n" . "{$this->tabs($level)}" . "
{$description}\n"; diff --git a/tests/Feature/ImAndExportTest.php b/tests/Feature/ImAndExportTest.php index b71760d..9692d72 100644 --- a/tests/Feature/ImAndExportTest.php +++ b/tests/Feature/ImAndExportTest.php @@ -84,10 +84,11 @@ public function testImportBookmarks() 'content' => 'https://www.mozilla.org/en-US/', 'collection_id' => $personal->id ])->exists()); - $this->assertTrue(Post::where([ + $post = Post::where([ 'content' => 'https://www.bbc.com/', 'collection_id' => $collectionA->id - ])->exists()); + ]); + $this->assertTrue($post->exists()); $this->assertTrue(Post::where([ 'content' => 'https://www.theguardian.com/international', 'collection_id' => $collectionA1->id @@ -105,6 +106,11 @@ public function testImportBookmarks() 'collection_id' => $collectionB->id ])->exists()); + $post = $post->with('tags')->first(); + $this->assertEquals(2, count($post->tags)); + $this->assertEquals('british', $post->tags[0]->name); + $this->assertEquals('tea time', $post->tags[1]->name); + } public function testExportBookmark() @@ -181,6 +187,8 @@ public function testExportBookmarks() $exported = file_get_contents($response->getFile()); $original = file_get_contents(base_path('tests/bookmarks.html')); + $this->assertStringContainsString('TAGS="british, tea time">BBC', $exported); + $original = strip_tags($original, ['dl', 'p', 'dt', 'h3', 'a', 'dd']); // $original = preg_replace('/(^.*(?:\n.*))*?
/m', '
', $original, 1); // remove attributes diff --git a/tests/bookmarks.html b/tests/bookmarks.html index cdc473b..4c46073 100644 --- a/tests/bookmarks.html +++ b/tests/bookmarks.html @@ -18,7 +18,7 @@

Bookmarks

Internet for people, not profit — Mozilla (US)

CollectionA

-

BBC - Homepage +
BBC - Homepage

SubCollectionA1

News, sport and opinion from the Guardian's global edition | The Guardian