From ecc1a5d7f7559f8a4de614ad2f74ba243094751a Mon Sep 17 00:00:00 2001 From: a-komarev Date: Sun, 7 Oct 2018 13:53:22 +0300 Subject: [PATCH] Make Ban created_by_type & created_by_id fillable --- CHANGELOG.md | 7 +++++ src/Models/Ban.php | 2 ++ src/Observers/BanObserver.php | 6 ++-- tests/Unit/Models/BanTest.php | 55 +++++++++++++++++++++++++++++++++-- 4 files changed, 65 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4de2136..032295a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ All notable changes to `laravel-ban` will be documented in this file. +## [3.5.0] - 2018-10-07 + +### Added + +- ([#35](https://github.com/cybercog/laravel-ban/pull/35)) Ban `created_by_type` & `created_by_id` are fillable now + ## [3.4.0] - 2018-09-21 ### Added @@ -90,6 +96,7 @@ All notable changes to `laravel-ban` will be documented in this file. - Initial release +[3.5.0]: https://github.com/cybercog/laravel-ban/compare/3.4.0...3.5.0 [3.4.0]: https://github.com/cybercog/laravel-ban/compare/3.3.0...3.4.0 [3.3.0]: https://github.com/cybercog/laravel-ban/compare/3.2.0...3.3.0 [3.2.0]: https://github.com/cybercog/laravel-ban/compare/3.1.0...3.2.0 diff --git a/src/Models/Ban.php b/src/Models/Ban.php index 22ad487..ab267bd 100644 --- a/src/Models/Ban.php +++ b/src/Models/Ban.php @@ -42,6 +42,8 @@ class Ban extends Model implements BanContract protected $fillable = [ 'comment', 'expired_at', + 'created_by_type', + 'created_by_id', ]; /** diff --git a/src/Observers/BanObserver.php b/src/Observers/BanObserver.php index 3e624b3..a184392 100644 --- a/src/Observers/BanObserver.php +++ b/src/Observers/BanObserver.php @@ -31,10 +31,10 @@ class BanObserver public function creating(BanContract $ban) { $bannedBy = auth()->user(); - if ($bannedBy) { - $ban->forceFill([ - 'created_by_id' => $bannedBy->getKey(), + if ($bannedBy && is_null($ban->created_by_type) && is_null($ban->created_by_id)) { + $ban->fill([ 'created_by_type' => $bannedBy->getMorphClass(), + 'created_by_id' => $bannedBy->getKey(), ]); } } diff --git a/tests/Unit/Models/BanTest.php b/tests/Unit/Models/BanTest.php index 881b4be..c08fa59 100644 --- a/tests/Unit/Models/BanTest.php +++ b/tests/Unit/Models/BanTest.php @@ -30,7 +30,7 @@ public function it_can_fill_comment() 'comment' => 'Enjoy your ban!', ]); - $this->assertEquals('Enjoy your ban!', $ban->comment); + $this->assertSame('Enjoy your ban!', $ban->comment); } /** @test */ @@ -45,6 +45,26 @@ public function it_can_fill_expired_at() $this->assertEquals($expiredAt, $ban->expired_at); } + /** @test */ + public function it_can_fill_created_by_type() + { + $ban = new Ban([ + 'created_by_type' => 'TestType', + ]); + + $this->assertSame('TestType', $ban->created_by_type); + } + + /** @test */ + public function it_can_fill_created_by_id() + { + $ban = new Ban([ + 'created_by_id' => '4', + ]); + + $this->assertSame('4', $ban->created_by_id); + } + /** @test */ public function it_casts_expired_at() { @@ -81,13 +101,44 @@ public function it_can_has_ban_creator() $bannedBy = factory(User::class)->create(); $ban = factory(Ban::class)->create([ - 'created_by_id' => $bannedBy->getKey(), 'created_by_type' => $bannedBy->getMorphClass(), + 'created_by_id' => $bannedBy->getKey(), ]); $this->assertInstanceOf(User::class, $ban->createdBy); } + /** @test */ + public function it_can_set_custom_ban_creator() + { + $bannable = factory(User::class)->create(); + $bannedBy = factory(User::class)->create(); + + $ban = $bannable->bans()->create([ + 'created_by_type' => $bannedBy->getMorphClass(), + 'created_by_id' => $bannedBy->getKey(), + ]); + + $this->assertTrue($ban->createdBy->is($bannedBy)); + } + + /** @test */ + public function it_not_overwrite_ban_creator_with_auth_user_if_custom_value_is_provided() + { + $bannable = factory(User::class)->create(); + $bannedBy = factory(User::class)->create(); + $currentUser = factory(User::class)->create(); + + $this->actingAs($currentUser); + + $ban = $bannable->bans()->create([ + 'created_by_type' => $bannedBy->getMorphClass(), + 'created_by_id' => $bannedBy->getKey(), + ]); + + $this->assertTrue($ban->createdBy->is($bannedBy)); + } + /** @test */ public function it_can_make_model_with_expire_carbon_date() {