added it_delete_folder_with_their_content_within_softly and it_delete_folder_with_their_content_within_hardly test

This commit is contained in:
Peter Papp
2021-03-02 15:42:04 +01:00
parent 42d243e2fe
commit 594a4acc94
2 changed files with 147 additions and 15 deletions
+6 -2
View File
@@ -302,10 +302,14 @@ class Editor
foreach ($files as $file) { foreach ($files as $file) {
// Delete file // Delete file
Storage::delete('/files/' . $file->basename); Storage::delete("/files/$file->user_id/$file->basename");
// Delete thumbnail if exist // 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 // Delete file permanently
$file->forceDelete(); $file->forceDelete();
+141 -13
View File
@@ -2,10 +2,14 @@
namespace Tests\Feature; namespace Tests\Feature;
use App\Models\File;
use App\Models\Folder; use App\Models\Folder;
use App\Models\User; use App\Models\User;
use App\Services\SetupService;
use Illuminate\Foundation\Testing\DatabaseMigrations; use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Http\UploadedFile;
use Laravel\Sanctum\Sanctum; use Laravel\Sanctum\Sanctum;
use Storage;
use Tests\TestCase; use Tests\TestCase;
// TODO: pridat foldre do api skupiny // TODO: pridat foldre do api skupiny
@@ -14,6 +18,12 @@ class FolderTest extends TestCase
{ {
use DatabaseMigrations; use DatabaseMigrations;
public function __construct()
{
parent::__construct();
$this->setup = app()->make(SetupService::class);
}
/** /**
* @test * @test
*/ */
@@ -261,21 +271,17 @@ class FolderTest extends TestCase
], ],
])->assertStatus(204); ])->assertStatus(204);
$this->assertSoftDeleted('folders', [ collect([$folder_1, $folder_2])
'id' => $folder_1->id, ->each(function ($folder) {
]);
$this->assertSoftDeleted('folders', [ $this->assertSoftDeleted('folders', [
'id' => $folder_2->id, 'id' => $folder->id,
]); ]);
$this->assertDatabaseMissing('favourite_folder', [ $this->assertDatabaseMissing('favourite_folder', [
'folder_id' => $folder_1->id, 'folder_id' => $folder->id,
]); ]);
});
$this->assertDatabaseMissing('favourite_folder', [
'folder_id' => $folder_2->id,
]);
} }
/** /**
@@ -318,14 +324,136 @@ class FolderTest extends TestCase
]); ]);
} }
/**
* @test
*/
public function it_delete_folder_with_their_content_within_softly() 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() 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() public function it_zip_and_download_folder_with_content_within()