From a4cd36fdf26f0b18818bba1b38dd1b23730b193e Mon Sep 17 00:00:00 2001 From: Radu Vasile Catalin Date: Tue, 3 Dec 2024 11:47:20 +0200 Subject: [PATCH 1/2] added failing test for proof of concept The value of foreign key is not passed down to user_id during approval as it should --- .../Feature/Scopes/ApprovalStateScopeTest.php | 45 ++++++++++--------- tests/Models/FakeUser.php | 12 +++++ tests/Pest.php | 6 +++ ...24_12_03_110050_create_fake_user_table.php | 18 ++++++++ 4 files changed, 59 insertions(+), 22 deletions(-) create mode 100644 tests/Models/FakeUser.php create mode 100644 tests/database/migrations/2024_12_03_110050_create_fake_user_table.php diff --git a/tests/Feature/Scopes/ApprovalStateScopeTest.php b/tests/Feature/Scopes/ApprovalStateScopeTest.php index 119c4fa..cdcaa7f 100644 --- a/tests/Feature/Scopes/ApprovalStateScopeTest.php +++ b/tests/Feature/Scopes/ApprovalStateScopeTest.php @@ -6,8 +6,8 @@ use Cjmellor\Approval\Events\ModelSetPending; use Cjmellor\Approval\Models\Approval; use Cjmellor\Approval\Tests\Models\FakeModel; +use Cjmellor\Approval\Tests\Models\FakeUser; use Illuminate\Support\Facades\Event; -use Illuminate\Support\Facades\Schema; test('Check if an Approval Model is approved', closure: function (): void { $this->approvalData = [ @@ -202,36 +202,37 @@ }); test(description: 'The model approver is listed correctly', closure: function () { - Schema::create('fake_users', callback: function (Illuminate\Database\Schema\Blueprint $table) { - $table->id(); - $table->string(column: 'name'); - $table->string(column: 'email')->unique(); - $table->string('password'); - }); + $user = FakeUser::create($this->fakeUserData); - class FakeUser extends \Illuminate\Foundation\Auth\User - { - protected $guarded = []; + $this->be($user); + + FakeModel::create($this->fakeModelData); - protected $table = 'fake_users'; + $approval = Approval::first(); + $approval->approve(); - public $timestamps = false; - } + expect($approval)->fresh()->audited_by->toBe(expected: $user->id); +}); - $user = FakeUser::create([ - 'name' => 'Chris Mellor', - 'email' => 'chris@mellor.pizza', - 'password' => 'password', - ]); +test(description: 'The model foreign key is set correctly', closure: function () { + $user = FakeUser::create($this->fakeUserData); $this->be($user); - FakeModel::create($this->fakeModelData); + $fakeModelData = [ + ...$this->fakeModelData, + 'user_id' => $user->id + ]; + + FakeModel::create($fakeModelData); $approval = Approval::first(); $approval->approve(); - expect($approval)->fresh()->audited_by->toBe(expected: $user->id); - - Schema::dropIfExists('fake_users'); + $this->assertDatabaseHas( + 'fake_models', + [ + 'user_id' => $user->id + ] + ); }); diff --git a/tests/Models/FakeUser.php b/tests/Models/FakeUser.php new file mode 100644 index 0000000..52a8c44 --- /dev/null +++ b/tests/Models/FakeUser.php @@ -0,0 +1,12 @@ + 'Chris', 'meta' => 'red', ]; + + $this->fakeUserData = [ + 'name' => 'Chris Mellor', + 'email' => 'chris@mellor.pizza', + 'password' => 'password', + ]; }) ->in(__DIR__); diff --git a/tests/database/migrations/2024_12_03_110050_create_fake_user_table.php b/tests/database/migrations/2024_12_03_110050_create_fake_user_table.php new file mode 100644 index 0000000..b558553 --- /dev/null +++ b/tests/database/migrations/2024_12_03_110050_create_fake_user_table.php @@ -0,0 +1,18 @@ +id(); + $table->string(column: 'name'); + $table->string(column: 'email')->unique(); + $table->string('password'); + }); + } +}; From f317d16714c0228ac9ebe4a5b42e16a84ff4a5e2 Mon Sep 17 00:00:00 2001 From: Radu Vasile Catalin Date: Tue, 3 Dec 2024 11:48:13 +0200 Subject: [PATCH 2/2] fixed null foreign key on approval --- src/Scopes/ApprovalStateScope.php | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/Scopes/ApprovalStateScope.php b/src/Scopes/ApprovalStateScope.php index 46b35e0..4dd07bf 100644 --- a/src/Scopes/ApprovalStateScope.php +++ b/src/Scopes/ApprovalStateScope.php @@ -54,7 +54,7 @@ public function extend(Builder $builder): void */ protected function addWithAnyState(Builder $builder): void { - $builder->macro('withAnyState', fn (Builder $builder): Builder => $builder->withoutGlobalScope($this)); + $builder->macro('withAnyState', fn(Builder $builder): Builder => $builder->withoutGlobalScope($this)); } /** @@ -62,7 +62,7 @@ protected function addWithAnyState(Builder $builder): void */ protected function addApproved(Builder $builder): void { - $builder->macro('approved', fn (Builder $builder): Builder => $builder + $builder->macro('approved', fn(Builder $builder): Builder => $builder ->withAnyState() ->where(column: 'state', operator: ApprovalStatus::Approved)); } @@ -72,7 +72,7 @@ protected function addApproved(Builder $builder): void */ protected function addPending(Builder $builder): void { - $builder->macro('pending', fn (Builder $builder): Builder => $builder + $builder->macro('pending', fn(Builder $builder): Builder => $builder ->withAnyState() ->where(column: 'state', operator: ApprovalStatus::Pending)); } @@ -82,7 +82,7 @@ protected function addPending(Builder $builder): void */ protected function addRejected(Builder $builder): void { - $builder->macro('rejected', fn (Builder $builder): Builder => $builder + $builder->macro('rejected', fn(Builder $builder): Builder => $builder ->withAnyState() ->where(column: 'state', operator: ApprovalStatus::Rejected)); } @@ -107,6 +107,11 @@ protected function addApprove(Builder $builder): void $newData = $builder->getModel()->new_data->toArray(); + $foreignKey = $builder->getModel()->foreign_key; + if ($foreignKey) { + $newData[$model->getApprovalForeignKeyName()] = $foreignKey; + } + // make sure we cast all attributes foreach ($newData as $key => $value) { $newData[$key] = $model->callCastAttribute($key, $value); @@ -148,7 +153,7 @@ protected function updateApprovalState(Builder $builder, ApprovalStatus $state): */ protected function addPostpone(Builder $builder): void { - $builder->macro('postpone', fn (Builder $builder): int => $this->updateApprovalState($builder, state: ApprovalStatus::Pending)); + $builder->macro('postpone', fn(Builder $builder): int => $this->updateApprovalState($builder, state: ApprovalStatus::Pending)); } /** @@ -156,6 +161,6 @@ protected function addPostpone(Builder $builder): void */ protected function addReject(Builder $builder): void { - $builder->macro('reject', fn (Builder $builder): int => $this->updateApprovalState($builder, state: ApprovalStatus::Rejected)); + $builder->macro('reject', fn(Builder $builder): int => $this->updateApprovalState($builder, state: ApprovalStatus::Rejected)); } }