diff --git a/app/Actions/ImportHtmlBookmarks.php b/app/Actions/ImportHtmlBookmarks.php index 9006fabf..e7069f97 100644 --- a/app/Actions/ImportHtmlBookmarks.php +++ b/app/Actions/ImportHtmlBookmarks.php @@ -79,6 +79,7 @@ class ImportHtmlBookmarks $newTag = Tag::firstOrCreate([ 'user_id' => $userId, 'name' => $tag, + 'is_private' => usersettings('tags_private_default') === '1', ]); $newTags[] = $newTag->id; } diff --git a/app/Models/User.php b/app/Models/User.php index 21f5f3b1..a8b9b216 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -7,6 +7,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; +use Illuminate\Support\Collection; use Laravel\Fortify\TwoFactorAuthenticatable; /** @@ -52,7 +53,7 @@ class User extends Authenticatable return $this->hasMany(Setting::class, 'user_id', 'id'); } - public function settings(): \Illuminate\Support\Collection + public function settings(): Collection { if ($this->rawSettings->isEmpty()) { $this->load('rawSettings'); diff --git a/tests/Controller/App/ImportControllerTest.php b/tests/Controller/App/ImportControllerTest.php index 9e57e96c..9603cfa5 100644 --- a/tests/Controller/App/ImportControllerTest.php +++ b/tests/Controller/App/ImportControllerTest.php @@ -2,12 +2,12 @@ namespace Tests\Controller\App; -use App\Helper\HtmlMeta; -use App\Models\Link; +use App\Models\Setting; use App\Models\User; use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Http\UploadedFile; use Illuminate\Support\Facades\Http; +use Illuminate\Testing\TestResponse; use Tests\TestCase; class ImportControllerTest extends TestCase @@ -28,11 +28,48 @@ class ImportControllerTest extends TestCase { $response = $this->get('import'); - $response->assertOk() - ->assertSee('Import'); + $response->assertOk()->assertSee('Import'); } public function testValidImportActionResponse(): void + { + $response = $this->importBookmarks(); + + $response->assertOk() + ->assertJson([ + 'success' => true, + ]); + + $this->assertDatabaseCount('links', 5); + $this->assertDatabaseCount('tags', 18); + } + + public function testImportWithPrivateDefaults(): void + { + Setting::create(['user_id' => 1, 'key' => 'links_private_default', 'value' => '1']); + Setting::create(['user_id' => 1, 'key' => 'tags_private_default', 'value' => '1']); + + $response = $this->importBookmarks(); + + $response->assertOk() + ->assertJson([ + 'success' => true, + ]); + + $this->assertDatabaseCount('links', 5); + + $this->assertDatabaseHas('links', [ + 'url' => 'https://loader.io/', + 'is_private' => true, + ]); + + $this->assertDatabaseHas('tags', [ + 'name' => 'article', + 'is_private' => '1', + ]); + } + + protected function importBookmarks(): TestResponse { $testHtml = 'DuckDuckGo'; Http::fake(['*' => Http::response($testHtml)]); @@ -40,18 +77,10 @@ class ImportControllerTest extends TestCase $exampleData = file_get_contents(__DIR__ . '/data/import_example.html'); $file = UploadedFile::fake()->createWithContent('import_example.html', $exampleData); - $response = $this->post('import', [ + return $this->post('import', [ 'import-file' => $file, ], [ 'Accept' => 'application/json', ]); - - $response->assertOk() - ->assertJson([ - 'success' => true, - ]); - - $linkCount = Link::count(); - $this->assertEquals(5, $linkCount); } }