From ada6d5ed3f812cf890de50db8d59888774376995 Mon Sep 17 00:00:00 2001 From: Kovah Date: Tue, 20 Feb 2024 10:45:19 +0100 Subject: [PATCH] Add default date for imported bookmarks if there's no date in the export (#752) --- app/Actions/ImportHtmlBookmarks.php | 8 +++-- tests/Controller/App/ImportControllerTest.php | 24 ++++++++++++-- .../App/data/import_example_dateless.html | 33 +++++++++++++++++++ 3 files changed, 60 insertions(+), 5 deletions(-) create mode 100644 tests/Controller/App/data/import_example_dateless.html diff --git a/app/Actions/ImportHtmlBookmarks.php b/app/Actions/ImportHtmlBookmarks.php index 8e654776..47fb70ef 100644 --- a/app/Actions/ImportHtmlBookmarks.php +++ b/app/Actions/ImportHtmlBookmarks.php @@ -6,7 +6,7 @@ use App\Helper\HtmlMeta; use App\Helper\LinkIconMapper; use App\Models\Link; use App\Models\Tag; -use Carbon\Carbon; +use Illuminate\Support\Carbon; use Illuminate\Support\Facades\Log; use Shaarli\NetscapeBookmarkParser\NetscapeBookmarkParser; @@ -20,7 +20,7 @@ class ImportHtmlBookmarks * * @param string $data * @param string $userId - * @param bool $generateMeta + * @param bool $generateMeta * @return bool */ public function run(string $data, string $userId, bool $generateMeta = true): bool @@ -64,7 +64,9 @@ class ImportHtmlBookmarks 'icon' => LinkIconMapper::mapLink($link['url']), 'is_private' => usersettings('tags_private_default') === '1' ? true : $isPublic, ]); - $newLink->created_at = Carbon::createFromTimestamp($link['dateCreated']); + $newLink->created_at = $link['dateCreated'] + ? Carbon::createFromTimestamp($link['dateCreated']) + : Carbon::now(); $newLink->updated_at = Carbon::now(); $newLink->timestamps = false; $newLink->save(); diff --git a/tests/Controller/App/ImportControllerTest.php b/tests/Controller/App/ImportControllerTest.php index 9603cfa5..40e489c7 100644 --- a/tests/Controller/App/ImportControllerTest.php +++ b/tests/Controller/App/ImportControllerTest.php @@ -6,6 +6,7 @@ use App\Models\Setting; use App\Models\User; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Http\UploadedFile; +use Illuminate\Support\Carbon; use Illuminate\Support\Facades\Http; use Illuminate\Testing\TestResponse; use Tests\TestCase; @@ -44,6 +45,25 @@ class ImportControllerTest extends TestCase $this->assertDatabaseCount('tags', 18); } + public function testDatelessImportActionResponse(): void + { + $this->travelTo(Carbon::create(2024, 2, 20)); + + $response = $this->importBookmarks('/data/import_example_dateless.html'); + + $response->assertOk() + ->assertJson([ + 'success' => true, + ]); + + $this->assertDatabaseCount('links', 5); + $this->assertDatabaseCount('tags', 18); + $this->assertDatabaseHas('links', [ + 'url' => 'https://loader.io/', + 'created_at' => '2024-02-20 00:00:00' + ]); + } + public function testImportWithPrivateDefaults(): void { Setting::create(['user_id' => 1, 'key' => 'links_private_default', 'value' => '1']); @@ -69,12 +89,12 @@ class ImportControllerTest extends TestCase ]); } - protected function importBookmarks(): TestResponse + protected function importBookmarks(string $importFile = '/data/import_example.html'): TestResponse { $testHtml = 'DuckDuckGo'; Http::fake(['*' => Http::response($testHtml)]); - $exampleData = file_get_contents(__DIR__ . '/data/import_example.html'); + $exampleData = file_get_contents(__DIR__ . $importFile); $file = UploadedFile::fake()->createWithContent('import_example.html', $exampleData); return $this->post('import', [ diff --git a/tests/Controller/App/data/import_example_dateless.html b/tests/Controller/App/data/import_example_dateless.html new file mode 100644 index 00000000..e2020056 --- /dev/null +++ b/tests/Controller/App/data/import_example_dateless.html @@ -0,0 +1,33 @@ + + + +LinkAce +
+

+

+ 5-Hour Rule: If you’re not spending 5 hours per + week learning, you’re being irresponsible +
Why did the busiest person in the world, former president Barack Obama, read an hour a day while in office? Why + has the best investor in history, Warren Buffett, invested 80% of his time in reading… +
+ Adele – Design Systems and Pattern Libraries + Repository +
+ Adobe Color CC +
+ Application Load Testing Tools for API Endpoints with + loader.io +
Free tool for web application load testing that allows for the simulation of concurrent connections to your web + application's APIs +
+ Astral — + Organize Your GitHub Stars With Ease +
Astral is the best way to manage your starred repositories on GitHub using tags, notes and a powerful search + feature. +