diff --git a/app/Entities/Payments/Payment.php b/app/Entities/Payments/Payment.php index 0c4a0eb..e5ee315 100755 --- a/app/Entities/Payments/Payment.php +++ b/app/Entities/Payments/Payment.php @@ -5,38 +5,16 @@ namespace App\Entities\Payments; use App\Entities\Partners\Partner; use App\Entities\Payments\PaymentPresenter; use App\Entities\Projects\Project; -use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; use Laracasts\Presenter\PresentableTrait; class Payment extends Model { - use PresentableTrait; protected $presenter = PaymentPresenter::class; protected $guarded = ['id', 'created_at', 'updated_at']; - /** - * The "booting" method of the model. - * - * @return void - */ - protected static function boot() - { - parent::boot(); - - if (auth()->user()) { - static::addGlobalScope('by_owner', function (Builder $builder) { - if ( ! is_null(auth()->user()->agency)) { - $builder->where('owner_id', auth()->user()->agency->id); - } else { - $builder->where('owner_id', 0); - } - }); - } - } - public function project() { return $this->belongsTo(Project::class); diff --git a/app/Entities/Projects/Project.php b/app/Entities/Projects/Project.php index 7637d17..5ef96ab 100755 --- a/app/Entities/Projects/Project.php +++ b/app/Entities/Projects/Project.php @@ -9,37 +9,18 @@ use App\Entities\Payments\Payment; use App\Entities\Projects\ProjectPresenter; use App\Entities\Projects\Task; use App\Entities\Subscriptions\Subscription; -use Illuminate\Database\Eloquent\Builder; +use App\Traits\OwnedByAgency; use Illuminate\Database\Eloquent\Model; use Laracasts\Presenter\PresentableTrait; class Project extends Model { - - use PresentableTrait; + use PresentableTrait, OwnedByAgency; protected $presenter = ProjectPresenter::class; protected $guarded = ['id', 'created_at', 'updated_at']; // protected $dates = ['start_date','end_date']; - /** - * The "booting" method of the model. - * - * @return void - */ - protected static function boot() - { - parent::boot(); - - static::addGlobalScope('by_owner', function (Builder $builder) { - if ( ! is_null(auth()->user()->agency)) { - $builder->where('owner_id', auth()->user()->agency->id); - } else { - $builder->where('owner_id', 0); - } - }); - } - public function nameLink() { return link_to_route('projects.show', $this->name, [$this->id]); diff --git a/app/Traits/OwnedByAgency.php b/app/Traits/OwnedByAgency.php new file mode 100644 index 0000000..610c96c --- /dev/null +++ b/app/Traits/OwnedByAgency.php @@ -0,0 +1,19 @@ +user()->agency)) { + $builder->where('owner_id', auth()->user()->agency->id); + } else { + $builder->where('owner_id', 0); + } + }); + } +} diff --git a/tests/Feature/Api/ApiEventsTest.php b/tests/Feature/Api/ApiEventsTest.php index bffa916..8db31f9 100644 --- a/tests/Feature/Api/ApiEventsTest.php +++ b/tests/Feature/Api/ApiEventsTest.php @@ -12,11 +12,11 @@ class ApiEventsTest extends TestCase /** @test */ public function it_can_get_all_existing_events() { - $user = factory(User::class)->create(); + $user = factory(User::class)->create(); $events = factory(Event::class, 2)->create(['user_id' => $user->id]); $this->getJson(route('api.events.index'), [ - 'Authorization' => 'Bearer '.$user->api_token + 'Authorization' => 'Bearer '.$user->api_token, ]); $this->seeStatusCode(200); @@ -30,94 +30,92 @@ class ApiEventsTest extends TestCase 'end', 'allDay', 'project_id', - ] + ], ]); } /** @test */ public function user_can_create_new_event() { - $user = factory(User::class)->create(); + $user = factory(User::class)->create(); $project = factory(Project::class)->create(); $this->postJson(route('api.events.store'), [ - 'title' => 'New Event Title', - 'body' => 'New Event Body', - 'start' => '2016-07-21 12:20:00', + 'title' => 'New Event Title', + 'body' => 'New Event Body', + 'start' => '2016-07-21 12:20:00', 'project_id' => $project->id, ], [ - 'Authorization' => 'Bearer '.$user->api_token + 'Authorization' => 'Bearer '.$user->api_token, ]); - // $this->dump(); - $this->seeStatusCode(201); $this->seeJson([ - 'message' => trans('event.created'), + 'message' => trans('event.created'), 'project_id' => $project->id, - 'user' => $user->name, - 'title' => 'New Event Title', - 'body' => 'New Event Body', - 'start' => '2016-07-21 12:20:00', - 'end' => null, - 'allDay' => false, + 'user' => $user->name, + 'title' => 'New Event Title', + 'body' => 'New Event Body', + 'start' => '2016-07-21 12:20:00', + 'end' => null, + 'allDay' => false, ]); $this->seeInDatabase('user_events', [ 'project_id' => $project->id, - 'user_id' => $user->id, - 'title' => 'New Event Title', - 'body' => 'New Event Body', - 'start' => '2016-07-21 12:20:00', - 'end' => null, - 'is_allday' => 0, + 'user_id' => $user->id, + 'title' => 'New Event Title', + 'body' => 'New Event Body', + 'start' => '2016-07-21 12:20:00', + 'end' => null, + 'is_allday' => 0, ]); } /** @test */ public function user_can_update_their_event() { - $user = factory(User::class)->create(); + $user = factory(User::class)->create(); $project = factory(Project::class)->create(); - $event = factory(Event::class)->create(['user_id' => $user->id]); + $event = factory(Event::class)->create(['user_id' => $user->id]); // dump($event->toArray()); $this->patchJson(route('api.events.update'), [ - 'id' => $event->id, + 'id' => $event->id, 'project_id' => $project->id, - 'title' => 'New Event Title', - 'body' => 'New Event Body', - 'is_allday' => 'true', + 'title' => 'New Event Title', + 'body' => 'New Event Body', + 'is_allday' => 'true', ], [ - 'Authorization' => 'Bearer '.$user->api_token + 'Authorization' => 'Bearer '.$user->api_token, ]); $this->seeStatusCode(200); $this->seeJson([ - 'message' => trans('event.updated'), + 'message' => trans('event.updated'), 'project_id' => $project->id, - 'user' => $user->name, - 'title' => 'New Event Title', - 'body' => 'New Event Body', + 'user' => $user->name, + 'title' => 'New Event Title', + 'body' => 'New Event Body', ]); $this->seeInDatabase('user_events', [ - 'user_id' => $user->id, + 'user_id' => $user->id, 'project_id' => $project->id, - 'title' => 'New Event Title', - 'body' => 'New Event Body', + 'title' => 'New Event Title', + 'body' => 'New Event Body', ]); } /** @test */ public function user_can_delete_their_event() { - $user = factory(User::class)->create(); + $user = factory(User::class)->create(); $event = factory(Event::class)->create(['user_id' => $user->id]); $this->deleteJson(route('api.events.destroy'), ['id' => $event->id], [ - 'Authorization' => 'Bearer '.$user->api_token + 'Authorization' => 'Bearer '.$user->api_token, ]); $this->seeStatusCode(200); @@ -130,15 +128,15 @@ class ApiEventsTest extends TestCase /** @test */ public function user_can_reschedule_their_event() { - $user = factory(User::class)->create(); + $user = factory(User::class)->create(); $event = factory(Event::class)->create(['user_id' => $user->id, 'start' => '2016-11-17 12:00:00']); $this->patchJson(route('api.events.reschedule'), [ - 'id' => $event->id, + 'id' => $event->id, 'start' => '2016-11-07 13:00:00', - 'end' => '2016-11-07 15:00:00', + 'end' => '2016-11-07 15:00:00', ], [ - 'Authorization' => 'Bearer '.$user->api_token + 'Authorization' => 'Bearer '.$user->api_token, ]); // $this->dump(); @@ -149,10 +147,10 @@ class ApiEventsTest extends TestCase ]); $this->seeInDatabase('user_events', [ - 'id' => $event->id, + 'id' => $event->id, 'user_id' => $user->id, - 'start' => '2016-11-07 13:00:00', - 'end' => '2016-11-07 15:00:00', + 'start' => '2016-11-07 13:00:00', + 'end' => '2016-11-07 15:00:00', ]); } } diff --git a/tests/Feature/Api/ApiManageProjectsTest.php b/tests/Feature/Api/ApiManageProjectsTest.php index 932af4c..a581935 100644 --- a/tests/Feature/Api/ApiManageProjectsTest.php +++ b/tests/Feature/Api/ApiManageProjectsTest.php @@ -3,7 +3,6 @@ namespace Tests\Feature\Api; use App\Entities\Projects\Project; -use App\Entities\Users\User; use Tests\TestCase; class ApiManageProjectsTest extends TestCase @@ -11,11 +10,11 @@ class ApiManageProjectsTest extends TestCase /** @test */ public function user_can_get_project_lists() { - $user = factory(User::class)->create(); - $project = factory(Project::class, 5)->create(['owner_id' => $user->id]); + $user = $this->adminUserSigningIn(); + $project = factory(Project::class, 1)->create(['owner_id' => $user->agency->id]); $this->getJson(route('api.projects.index'), [ - 'Authorization' => 'Bearer '.$user->api_token + 'Authorization' => 'Bearer '.$user->api_token, ]); $this->seeStatusCode(200); diff --git a/tests/Feature/Auth/MemberLoginTest.php b/tests/Feature/Auth/MemberLoginTest.php index af8edb6..ff9aed2 100644 --- a/tests/Feature/Auth/MemberLoginTest.php +++ b/tests/Feature/Auth/MemberLoginTest.php @@ -2,6 +2,7 @@ namespace Tests\Feature\Auth; +use App\Entities\Agencies\Agency; use App\Entities\Users\User; use Tests\TestCase; @@ -11,6 +12,7 @@ class MemberLoginTest extends TestCase public function user_can_login_and_logout() { $user = factory(User::class)->create(['name' => 'Nama Member', 'email' => 'email@mail.com']); + factory(Agency::class)->create(['owner_id' => $user->id]); $this->visit(route('auth.login'));