diff --git a/app/Entities/Partners/Vendor.php b/app/Entities/Partners/Vendor.php index 8188df1..2a6b1cc 100644 --- a/app/Entities/Partners/Vendor.php +++ b/app/Entities/Partners/Vendor.php @@ -6,5 +6,10 @@ use Illuminate\Database\Eloquent\Model; class Vendor extends Model { - protected $fillable = ['name', 'description']; + protected $fillable = ['name', 'description', 'owner_id']; + + public function owner() + { + return $this->belongsTo('App\Entities\Agencies\Agency', 'owner_id'); + } } diff --git a/app/Http/Controllers/Partners/VendorsController.php b/app/Http/Controllers/Partners/VendorsController.php index 5914688..7c799c9 100644 --- a/app/Http/Controllers/Partners/VendorsController.php +++ b/app/Http/Controllers/Partners/VendorsController.php @@ -35,12 +35,14 @@ class VendorsController extends Controller */ public function store(Request $request) { - $this->validate($request, [ + $newVendorData = $this->validate($request, [ 'name' => 'required|max:60', 'description' => 'nullable|max:255', ]); - Vendor::create($request->only('name', 'description')); + $newVendorData['owner_id'] = auth()->user()->agency->id; + + Vendor::create($newVendorData); flash(trans('vendor.created'), 'success'); return redirect()->route('vendors.index'); @@ -55,14 +57,14 @@ class VendorsController extends Controller */ public function update(Request $request, Vendor $vendor) { - $this->validate($request, [ + $vendorData = $this->validate($request, [ 'name' => 'required|max:60', 'description' => 'nullable|max:255', ]); $routeParam = request()->only('page', 'q'); - $vendor = $vendor->update($request->only('name', 'description')); + $vendor = $vendor->update($vendorData); flash(trans('vendor.updated'), 'success'); return redirect()->route('vendors.index', $routeParam); diff --git a/app/Policies/Partners/VendorPolicy.php b/app/Policies/Partners/VendorPolicy.php index 1df1785..efa2d4a 100644 --- a/app/Policies/Partners/VendorPolicy.php +++ b/app/Policies/Partners/VendorPolicy.php @@ -2,8 +2,8 @@ namespace App\Policies\Partners; -use App\Entities\Users\User; use App\Entities\Partners\Vendor; +use App\Entities\Users\User; use Illuminate\Auth\Access\HandlesAuthorization; class VendorPolicy @@ -19,8 +19,7 @@ class VendorPolicy */ public function view(User $user, Vendor $vendor) { - // Update $user authorization to view $vendor here. - return true; + return $user->agency->id == $vendor->owner_id; } /** @@ -32,8 +31,7 @@ class VendorPolicy */ public function create(User $user, Vendor $vendor) { - // Update $user authorization to create $vendor here. - return true; + return ! ! $user->agency; } /** @@ -45,8 +43,7 @@ class VendorPolicy */ public function update(User $user, Vendor $vendor) { - // Update $user authorization to update $vendor here. - return true; + return $this->view($user, $vendor); } /** @@ -58,7 +55,6 @@ class VendorPolicy */ public function delete(User $user, Vendor $vendor) { - // Update $user authorization to delete $vendor here. - return true; + return $this->view($user, $vendor); } } diff --git a/database/factories/VendorFactory.php b/database/factories/VendorFactory.php index f4c6db9..2ae3a3c 100644 --- a/database/factories/VendorFactory.php +++ b/database/factories/VendorFactory.php @@ -1,12 +1,16 @@ define(Vendor::class, function (Faker $faker) { return [ - 'name' => $faker->word, + 'name' => $faker->word, 'description' => $faker->sentence, + 'owner_id' => function () { + return factory(Agency::class)->create()->id; + }, ]; }); diff --git a/database/migrations/2017_11_01_185745_create_vendors_table.php b/database/migrations/2017_11_01_185745_create_vendors_table.php index 124e02c..f2d6371 100644 --- a/database/migrations/2017_11_01_185745_create_vendors_table.php +++ b/database/migrations/2017_11_01_185745_create_vendors_table.php @@ -1,8 +1,8 @@ increments('id'); $table->string('name', 60); $table->string('description')->nullable(); + $table->unsignedInteger('owner_id'); $table->timestamps(); }); } diff --git a/tests/Feature/ManageVendorsTest.php b/tests/Feature/ManageVendorsTest.php index 6bdc07d..d72a65a 100644 --- a/tests/Feature/ManageVendorsTest.php +++ b/tests/Feature/ManageVendorsTest.php @@ -35,6 +35,7 @@ class ManageVendorsTest extends TestCase $this->type('Vendor 1 description', 'description'); $this->press(trans('vendor.create')); + $this->see(trans('vendor.created')); $this->seePageIs(route('vendors.index')); $this->seeInDatabase('vendors', [ @@ -57,6 +58,7 @@ class ManageVendorsTest extends TestCase $this->type('Vendor 1 description', 'description'); $this->press(trans('vendor.update')); + $this->see(trans('vendor.updated')); $this->seePageIs(route('vendors.index', ['q' => '123'])); $this->seeInDatabase('vendors', [ @@ -81,6 +83,7 @@ class ManageVendorsTest extends TestCase $this->press(trans('app.delete_confirm_button')); + $this->see(trans('vendor.deleted')); $this->dontSeeInDatabase('vendors', [ 'id' => $vendor->id, ]); diff --git a/tests/Unit/Models/VendorTest.php b/tests/Unit/Models/VendorTest.php index 4f968bd..2d21236 100644 --- a/tests/Unit/Models/VendorTest.php +++ b/tests/Unit/Models/VendorTest.php @@ -2,6 +2,7 @@ namespace Tests\Unit\Models; +use App\Entities\Agencies\Agency; use App\Entities\Partners\Vendor; use Illuminate\Foundation\Testing\DatabaseMigrations; use Tests\TestCase as TestCase; @@ -11,9 +12,12 @@ class VendorTest extends TestCase use DatabaseMigrations; /** @test */ - public function it_has_name_attribute() + public function a_vendor_belongs_to_an_agency_as_its_owner() { - $vendor = factory(Vendor::class)->create(['name' => 'Vendor 1 name']); - $this->assertEquals('Vendor 1 name', $vendor->name); + $vendor = factory(Vendor::class)->create(); + $this->assertTrue( + $vendor->owner instanceof Agency, + 'A vendor must belongs to an App\Entities\Agencies\Agency model as its owner.' + ); } } diff --git a/tests/Unit/Policies/VendorPolicyTest.php b/tests/Unit/Policies/VendorPolicyTest.php index 427a98f..994feaa 100644 --- a/tests/Unit/Policies/VendorPolicyTest.php +++ b/tests/Unit/Policies/VendorPolicyTest.php @@ -21,7 +21,7 @@ class VendorPolicyTest extends TestCase public function user_can_view_vendor() { $user = $this->adminUserSigningIn(); - $vendor = factory(Vendor::class)->create(['name' => 'Vendor 1 name']); + $vendor = factory(Vendor::class)->create(['owner_id' => $user->agency->id]); $this->assertTrue($user->can('view', $vendor)); } @@ -29,7 +29,7 @@ class VendorPolicyTest extends TestCase public function user_can_update_vendor() { $user = $this->adminUserSigningIn(); - $vendor = factory(Vendor::class)->create(['name' => 'Vendor 1 name']); + $vendor = factory(Vendor::class)->create(['owner_id' => $user->agency->id]); $this->assertTrue($user->can('update', $vendor)); } @@ -37,7 +37,7 @@ class VendorPolicyTest extends TestCase public function user_can_delete_vendor() { $user = $this->adminUserSigningIn(); - $vendor = factory(Vendor::class)->create(['name' => 'Vendor 1 name']); + $vendor = factory(Vendor::class)->create(['owner_id' => $user->agency->id]); $this->assertTrue($user->can('delete', $vendor)); } }