tests:add tests for setting owner_id on new portfolio create
This commit is contained in:
@@ -27,10 +27,6 @@ class Portfolio extends Model
|
|||||||
protected static function boot()
|
protected static function boot()
|
||||||
{
|
{
|
||||||
parent::boot();
|
parent::boot();
|
||||||
|
|
||||||
static::saving(function ($portfolio) {
|
|
||||||
unset($portfolio->owner_id);
|
|
||||||
});
|
|
||||||
|
|
||||||
static::saved(function ($portfolio) {
|
static::saved(function ($portfolio) {
|
||||||
|
|
||||||
@@ -95,7 +91,6 @@ class Portfolio extends Model
|
|||||||
{
|
{
|
||||||
// enable queued jobs to create portfolios with owners
|
// enable queued jobs to create portfolios with owners
|
||||||
if (!auth()->user()?->id && !$this->owner_id) {
|
if (!auth()->user()?->id && !$this->owner_id) {
|
||||||
$this->attributes['owner_id'] = $value;
|
|
||||||
static::$owner_id = $value;
|
static::$owner_id = $value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
namespace Database\Factories;
|
namespace Database\Factories;
|
||||||
|
|
||||||
use App\Models\User;
|
|
||||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -0,0 +1,54 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests;
|
||||||
|
|
||||||
|
use Tests\TestCase;
|
||||||
|
use App\Models\User;
|
||||||
|
use App\Models\Portfolio;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
|
||||||
|
class PortfoliosTest extends TestCase
|
||||||
|
{
|
||||||
|
use RefreshDatabase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*/
|
||||||
|
public function test_owner_is_assigned_to_portfolio_on_create(): void
|
||||||
|
{
|
||||||
|
$this->actingAs($user = User::factory()->create());
|
||||||
|
|
||||||
|
$portfolio = Portfolio::factory()->create();
|
||||||
|
|
||||||
|
$this->assertEquals($user->id, $portfolio->owner_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*/
|
||||||
|
public function test_owner_can_be_forced_on_create(): void
|
||||||
|
{
|
||||||
|
$this->actingAs($user = User::factory()->create());
|
||||||
|
|
||||||
|
$portfolio = Portfolio::factory()->make();
|
||||||
|
$portfolio->owner_id = $user->id;
|
||||||
|
$portfolio->save();
|
||||||
|
|
||||||
|
$this->assertEquals($user->id, $portfolio->owner_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*/
|
||||||
|
public function test_owner_cannot_be_changed_on_update(): void
|
||||||
|
{
|
||||||
|
$this->actingAs($owner = User::factory()->create());
|
||||||
|
|
||||||
|
$interloper = User::factory()->create();
|
||||||
|
|
||||||
|
$portfolio = Portfolio::factory()->create();
|
||||||
|
$portfolio->owner_id = $interloper->id;
|
||||||
|
$portfolio->save();
|
||||||
|
|
||||||
|
$this->assertEquals($owner->id, $portfolio->owner_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user