From 8de3a3559939cd5edfc342f6c0cfb480a71865d7 Mon Sep 17 00:00:00 2001 From: Mohammad Mortazavi Date: Sat, 21 Sep 2024 08:55:55 +0330 Subject: [PATCH 1/2] revert the revert :) The children of a parent will be removed through foreign key. meanwhile the associated file with each child will be removed with the parent removal action cause the children associated files stored in the same directory as the parent; --- src/Services/Actions/Delete.php | 7 +------ tests/Unit/ResourceModelTest.php | 18 +++++++++++------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/Services/Actions/Delete.php b/src/Services/Actions/Delete.php index 9c1ccc7..2f911e4 100644 --- a/src/Services/Actions/Delete.php +++ b/src/Services/Actions/Delete.php @@ -29,13 +29,8 @@ public function run(): Resource DB::beginTransaction(); try { - if ($this->model->children()->exists()) { - foreach ($this->model->children()->select('id', 'directory', 'external')->get() as $child) { - (new self($child))->run(); - } - } $this->model->delete(); - if (!$this->model->isExternal() and alicia_storage()->exists($this->model->directory)) { + if ($this->model->isNotExternal() and alicia_storage()->exists($this->model->directory)) { alicia_storage()->deleteDirectory($this->model->directory); } } catch (Throwable $e) { diff --git a/tests/Unit/ResourceModelTest.php b/tests/Unit/ResourceModelTest.php index bff3f2a..532a01d 100644 --- a/tests/Unit/ResourceModelTest.php +++ b/tests/Unit/ResourceModelTest.php @@ -10,7 +10,6 @@ use Illuminate\Support\Facades\URL; use Spatie\Image\Exceptions\InvalidManipulation; use Symfony\Component\HttpFoundation\BinaryFileResponse; -use function PHPUnit\Framework\assertDirectoryExists; class ResourceModelTest extends TestCase { @@ -294,22 +293,27 @@ public function isNotExternal(): void /** * @test * + * @return void * @throws InvalidManipulation * - * @return void */ public function childrenWIllBeDeleted(): void { $data = Alicia::upload( UploadedFile::fake()->image('g-eazy.png', 1080, 1080) ) - ->export([540 => 540, 480 => 480]) - ->getData(); + ->export([540 => 540, 480 => 480]) + ->getData(); + + $parentId = $data->get('parents')->pluck('id')->first(); + + self::assertDirectoryExists(alicia_storage()->path($data->get("$parentId-children")->first()->directory)); + self::assertDirectoryExists(alicia_storage()->path($data->get("$parentId-children")->last()->directory)); - Alicia::delete($data->get('parents')->pluck('id')->first()); + Alicia::delete($parentId); self::assertEmpty(DB::table('resources')->get()->pluck('id')); - self::assertDirectoryDoesNotExist($data->first()->first()->fullpath); - self::assertDirectoryDoesNotExist($data->first()->last()->fullpath); + self::assertDirectoryDoesNotExist(alicia_storage()->path($data->get("$parentId-children")->first()->directory)); + self::assertDirectoryDoesNotExist(alicia_storage()->path($data->get("$parentId-children")->last()->directory)); } } From cd76d05e165aa9332f9b5060ff6e677aac7a3b99 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Sat, 21 Sep 2024 05:26:07 +0000 Subject: [PATCH 2/2] Apply fixes from StyleCI --- tests/Unit/ResourceModelTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/Unit/ResourceModelTest.php b/tests/Unit/ResourceModelTest.php index 532a01d..7ff8719 100644 --- a/tests/Unit/ResourceModelTest.php +++ b/tests/Unit/ResourceModelTest.php @@ -289,13 +289,12 @@ public function isNotExternal(): void self::assertFalse($model->isExternal()); } - /** * @test * - * @return void * @throws InvalidManipulation * + * @return void */ public function childrenWIllBeDeleted(): void {