diff --git a/app/Http/Livewire/HasLivewireAuth.php b/app/Http/Livewire/HasLivewireAuth.php index be1a7a1..316d7f6 100644 --- a/app/Http/Livewire/HasLivewireAuth.php +++ b/app/Http/Livewire/HasLivewireAuth.php @@ -29,10 +29,7 @@ public function hydrate() throw new AuthenticationException(); } - if (! isset($this->permissionName)) { - $splitted = explode('-', self::getName()); - $this->permissionName = Str::plural($splitted[1]).'.'.$splitted[0]; - } + $this->getPermissionName(); $this->authorize('for-route', [$this->permissionName, $this->getModel()]); } @@ -48,10 +45,24 @@ public function getModel() return $this->setModel(); } - foreach (get_object_vars($this) as $var) { - if ($var instanceof Model) { - return $var; - } + return collect(get_object_vars($this)) + ->filter(function ($variable) { + return $variable instanceof Model; + })->first(); + } + + /** + * Get permission name. + * + * @return void + */ + public function getPermissionName() + { + if (isset($this->permissionName)) { + return; } + + $splitted = explode('-', self::getName()); + $this->permissionName = Str::plural($splitted[1]).'.'.$splitted[0]; } } diff --git a/app/Scopes/VisibleToScope.php b/app/Scopes/VisibleToScope.php index 4870b3f..4848cb8 100644 --- a/app/Scopes/VisibleToScope.php +++ b/app/Scopes/VisibleToScope.php @@ -37,18 +37,21 @@ public function apply(Builder $builder, Model $model) } /** - * Should we return early form global scope. + * Should we return early form global scope base on permission and owner field. * * @param \App\Models\User $user + * @param \Illuminate\Database\Eloquent\Model $model * @return bool */ - private function returnEarly($user) + public function returnEarlyPermission($user, $model) { - if ($user === null) { + $permission = $user->getPermission($model->getTable().'.index'); + + if (! $permission->pivot->owner_restricted === true) { return true; } - if ($user->isAdmin()) { + if (! Schema::hasColumn($model->getTable(), AppServiceProvider::OWNER_FIELD)) { return true; } @@ -56,21 +59,18 @@ private function returnEarly($user) } /** - * Should we return early form global scope base on permission and owner field. + * Should we return early form global scope. * * @param \App\Models\User $user - * @param \Illuminate\Database\Eloquent\Model $model * @return bool */ - public function returnEarlyPermission($user, $model) + private function returnEarly($user) { - $permission = $user->getPermission($model->getTable().'.index'); - - if (! $permission->pivot->owner_restricted === true) { + if ($user === null) { return true; } - if (! Schema::hasColumn($model->getTable(), AppServiceProvider::OWNER_FIELD)) { + if ($user->isAdmin()) { return true; } diff --git a/stubs/component.create.test.stub b/stubs/component.create.test.stub new file mode 100644 index 0000000..c570017 --- /dev/null +++ b/stubs/component.create.test.stub @@ -0,0 +1,79 @@ +admin = create_admin(); + } + + /** @test */ + public function assert_create_{{ dummyText }}_component_uses_livewire_auth_trait() + { + $this->assertContains(HasLivewireAuth::class, class_uses(Create{{ DummyText }}Component::class)); + } + + /** @test */ + public function render() + { + Livewire::actingAs($this->admin) + ->test(Create{{ DummyText }}Component::class) + ->assertSee('Save') + ->assertStatus(Response::HTTP_OK); + } + + /** @test */ + public function store() + { + Livewire::actingAs($this->admin) + ->test(Create{{ DummyText }}Component::class) + // + ->call('store') + ->assertRedirect('{{ dummyTextPlu }}'); + + $this->assertTrue(session()->has('flash')); + + ${{ dummyTextPlu }} = {{ DummyText }}::where('name', 'manager') + ->where('label', 'Manager') + ->get(); + + $this->assertCount(1, ${{ dummyTextPlu }}); + } + + /** + * @test + * @dataProvider clientFormValidationProvider + */ + public function test_store_validation_rules($clientFormInput, $clientFormValue, $rule) + { + Livewire::actingAs($this->admin) + ->test(Create{{ DummyText }}Component::class) + ->set($clientFormInput, $clientFormValue) + ->call('store') + ->assertHasErrors([$clientFormInput => $rule]); + } + + public function clientFormValidationProvider() + { + return [ + ]; + } +}