From fc12f9c95582c6ca37975492d087acf5aa875a52 Mon Sep 17 00:00:00 2001 From: thinkverse Date: Thu, 17 Jun 2021 12:30:49 +0200 Subject: [PATCH 1/3] Add tests for blade directives --- tests/BladeDirectivesTest.php | 92 +++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 tests/BladeDirectivesTest.php diff --git a/tests/BladeDirectivesTest.php b/tests/BladeDirectivesTest.php new file mode 100644 index 0000000..c614b44 --- /dev/null +++ b/tests/BladeDirectivesTest.php @@ -0,0 +1,92 @@ + 'test user', + 'email' => 'test@user.com', + 'password' => Hash::make('password') + ]); + + $feature = Feature::create([ + 'name' => 'test' + ]); + + $user->giveFeature($feature->name); + + $view = $this->actingAs($user)->blade("@feature('test') active feature @endfeature"); + + $view->assertSee('active feature'); + } + + /** + * @test + */ + public function it_shows_feature_to_user_in_feature_group() + { + $user = User::create([ + 'name' => 'test user', + 'email' => 'test@user.com', + 'password' => Hash::make('password') + ]); + + $group = FeatureGroup::create([ + 'name' => 'Test Group', + ]); + + $user->joinGroup($group->name); + + $view = $this->actingAs($user)->blade("@featuregroup('test group') active feature @endfeaturegroup"); + + $view->assertSee('active feature'); + } + + /** + * @test + */ + public function it_hides_feature_to_user_that_dont_have_feature() + { + $user = User::create([ + 'name' => 'test user', + 'email' => 'test@user.com', + 'password' => Hash::make('password') + ]); + + $view = $this->actingAs($user)->blade("@feature('test') active feature @endfeature"); + + $view->assertSee(''); + } + + /** + * @test + */ + public function it_hides_feature_to_user_not_in_feature_group() + { + $user = User::create([ + 'name' => 'test user', + 'email' => 'test@user.com', + 'password' => Hash::make('password') + ]); + + $view = $this->actingAs($user)->blade("@featuregroup('test group') active feature @endfeaturegroup"); + + $view->assertSee(''); + } +} From f856dd1faae3132464059fa67ae18ef5865cbb38 Mon Sep 17 00:00:00 2001 From: thinkverse Date: Fri, 18 Jun 2021 00:15:07 +0200 Subject: [PATCH 2/3] Add tests for groupfeature blade directive --- tests/BladeDirectivesTest.php | 50 +++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/tests/BladeDirectivesTest.php b/tests/BladeDirectivesTest.php index c614b44..4182fbe 100644 --- a/tests/BladeDirectivesTest.php +++ b/tests/BladeDirectivesTest.php @@ -58,6 +58,34 @@ public function it_shows_feature_to_user_in_feature_group() $view->assertSee('active feature'); } + /** + * @test + */ + public function it_shows_feature_to_user_when_feature_group_has_feature() + { + $user = User::create([ + 'name' => 'test user', + 'email' => 'test@user.com', + 'password' => Hash::make('password') + ]); + + $feature = Feature::create([ + 'name' => 'test' + ]); + + $group = FeatureGroup::create([ + 'name' => 'Test Group', + ]); + + $group->addFeature($feature); + + $user->joinGroup($group->name); + + $view = $this->actingAs($user)->blade("@groupfeature('test') active feature @endgroupfeature"); + + $view->assertSee('active feature'); + } + /** * @test */ @@ -89,4 +117,26 @@ public function it_hides_feature_to_user_not_in_feature_group() $view->assertSee(''); } + + /** + * @test + */ + public function it_hides_feature_to_user_when_feature_group_dont_have_feature() + { + $user = User::create([ + 'name' => 'test user', + 'email' => 'test@user.com', + 'password' => Hash::make('password') + ]); + + $group = FeatureGroup::create([ + 'name' => 'Test Group', + ]); + + $user->joinGroup($group->name); + + $view = $this->actingAs($user)->blade("@groupfeature('test') active feature @endgroupfeature"); + + $view->assertSee(''); + } } From 4397f9ea5ae97718c752fe64b48d845f1faa830f Mon Sep 17 00:00:00 2001 From: thinkverse Date: Fri, 18 Jun 2021 00:15:56 +0200 Subject: [PATCH 3/3] Fix groupfeature blade directive Defer group feature check to groupHasFeature method --- src/Concerns/HasFeatures.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Concerns/HasFeatures.php b/src/Concerns/HasFeatures.php index 7574705..09dff01 100644 --- a/src/Concerns/HasFeatures.php +++ b/src/Concerns/HasFeatures.php @@ -150,7 +150,9 @@ protected function getAllGroups(array $groups): Collection public function groupHasFeature(string $featureName): bool { - return $this->groups->features->contains('name', $featureName); + return $this->hasFeatureThroughGroup( + feature: $featureName, + ); } protected function featureExists(string $featureName): bool