Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Ban created_by_type & created_by_id fillable #35

Merged
merged 1 commit into from
Oct 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/Models/Ban.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class Ban extends Model implements BanContract
protected $fillable = [
'comment',
'expired_at',
'created_by_type',
'created_by_id',
];

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Observers/BanObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
]);
}
}
Expand Down
55 changes: 53 additions & 2 deletions tests/Unit/Models/BanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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()
{
Expand Down Expand Up @@ -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()
{
Expand Down