From 594a4acc94fd8a26c2e36ed34d50b6557b8998c6 Mon Sep 17 00:00:00 2001 From: Peter Papp Date: Tue, 2 Mar 2021 15:42:04 +0100 Subject: [PATCH] added it_delete_folder_with_their_content_within_softly and it_delete_folder_with_their_content_within_hardly test --- app/Http/Tools/Editor.php | 8 +- tests/Feature/FolderTest.php | 154 ++++++++++++++++++++++++++++++++--- 2 files changed, 147 insertions(+), 15 deletions(-) diff --git a/app/Http/Tools/Editor.php b/app/Http/Tools/Editor.php index 4c978019..99dd2e82 100644 --- a/app/Http/Tools/Editor.php +++ b/app/Http/Tools/Editor.php @@ -302,10 +302,14 @@ class Editor foreach ($files as $file) { // Delete file - Storage::delete('/files/' . $file->basename); + Storage::delete("/files/$file->user_id/$file->basename"); // Delete thumbnail if exist - if (!is_null($file->thumbnail)) Storage::delete('/files/' . $file->getRawOriginal('thumbnail')); + if ($file->thumbnail) { + Storage::delete( + "/files/$file->user_id/{$file->getRawOriginal('thumbnail')}" + ); + } // Delete file permanently $file->forceDelete(); diff --git a/tests/Feature/FolderTest.php b/tests/Feature/FolderTest.php index 2bffc159..3633c3fa 100644 --- a/tests/Feature/FolderTest.php +++ b/tests/Feature/FolderTest.php @@ -2,10 +2,14 @@ namespace Tests\Feature; +use App\Models\File; use App\Models\Folder; use App\Models\User; +use App\Services\SetupService; use Illuminate\Foundation\Testing\DatabaseMigrations; +use Illuminate\Http\UploadedFile; use Laravel\Sanctum\Sanctum; +use Storage; use Tests\TestCase; // TODO: pridat foldre do api skupiny @@ -14,6 +18,12 @@ class FolderTest extends TestCase { use DatabaseMigrations; + public function __construct() + { + parent::__construct(); + $this->setup = app()->make(SetupService::class); + } + /** * @test */ @@ -261,21 +271,17 @@ class FolderTest extends TestCase ], ])->assertStatus(204); - $this->assertSoftDeleted('folders', [ - 'id' => $folder_1->id, - ]); + collect([$folder_1, $folder_2]) + ->each(function ($folder) { - $this->assertSoftDeleted('folders', [ - 'id' => $folder_2->id, - ]); + $this->assertSoftDeleted('folders', [ + 'id' => $folder->id, + ]); - $this->assertDatabaseMissing('favourite_folder', [ - 'folder_id' => $folder_1->id, - ]); - - $this->assertDatabaseMissing('favourite_folder', [ - 'folder_id' => $folder_2->id, - ]); + $this->assertDatabaseMissing('favourite_folder', [ + 'folder_id' => $folder->id, + ]); + }); } /** @@ -318,14 +324,136 @@ class FolderTest extends TestCase ]); } + /** + * @test + */ public function it_delete_folder_with_their_content_within_softly() { + $user = User::factory(User::class) + ->create(); + Sanctum::actingAs($user); + + $folder_root = Folder::factory(Folder::class) + ->create([ + 'user_id' => $user->id + ]); + + $folder_children = Folder::factory(Folder::class) + ->create([ + 'user_id' => $user->id, + 'parent_id' => $folder_root->id, + ]); + + $file_1 = File::factory(File::class) + ->create([ + 'folder_id' => $folder_root->id, + 'user_id' => $user->id, + ]); + + $file_2 = File::factory(File::class) + ->create([ + 'folder_id' => $folder_children->id, + 'user_id' => $user->id, + ]); + + $this->postJson("/api/remove", [ + 'items' => [ + [ + 'id' => $folder_root->id, + 'type' => 'folder', + 'force_delete' => false, + ], + ], + ])->assertStatus(204); + + collect([$file_1, $file_2]) + ->each(function ($file) { + $this->assertSoftDeleted('files', [ + 'id' => $file->id, + ]); + }); + + collect([$folder_root, $folder_children]) + ->each(function ($file) { + $this->assertSoftDeleted('folders', [ + 'id' => $file->id, + ]); + }); } + /** + * @test + */ public function it_delete_folder_with_their_content_within_hardly() { + Storage::fake('local'); + $this->setup->create_directories(); + + $user = User::factory(User::class) + ->create(); + + Sanctum::actingAs($user); + + $folder_root = Folder::factory(Folder::class) + ->create([ + 'user_id' => $user->id + ]); + + $folder_children = Folder::factory(Folder::class) + ->create([ + 'user_id' => $user->id, + 'parent_id' => $folder_root->id, + ]); + + collect([$folder_root, $folder_children]) + ->each(function ($folder, $index) { + + $file = UploadedFile::fake() + ->create("fake-file-$index.pdf", 1200, 'application/pdf'); + + $this->postJson('/api/upload', [ + 'file' => $file, + 'folder_id' => $folder->id, + 'is_last' => true, + ])->assertStatus(201); + }); + + $uploaded_files = File::all(); + + collect([0, 1]) + ->each(function ($index) use ($folder_root) { + $this->postJson("/api/remove", [ + 'items' => [ + [ + 'id' => $folder_root->id, + 'type' => 'folder', + 'force_delete' => $index, + ], + ], + ])->assertStatus(204); + }); + + $uploaded_files + ->each(function ($file, $index) use ($user) { + + $this->assertDatabaseMissing('files', [ + 'id' => $file->id, + ]); + + Storage::disk('local') + ->assertMissing( + "files/$user->id/fake-file-$index.pdf" + ); + }); + + collect([$folder_root, $folder_children]) + ->each(function ($id) { + $this->assertDatabaseMissing('folders', [ + 'id' => $id, + ]); + }); } public function it_zip_and_download_folder_with_content_within()