From c02903617b148710441c562b83684d0c00203de4 Mon Sep 17 00:00:00 2001 From: "claude[bot]" <41898282+claude[bot]@users.noreply.github.com> Date: Sat, 20 Dec 2025 10:51:45 +0000 Subject: [PATCH] Add comprehensive unit tests for Blade directives - Test @aauth and @endaauth directive compilation - Test permission checking behavior - Test support for single/double quotes and variables - Test nested directives and multiple blocks - Test HTML content rendering with permissions Resolves #31 Co-authored-by: EA --- tests/Unit/BladeDirectiveTest.php | 142 ++++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 tests/Unit/BladeDirectiveTest.php diff --git a/tests/Unit/BladeDirectiveTest.php b/tests/Unit/BladeDirectiveTest.php new file mode 100644 index 0000000..800ed5d --- /dev/null +++ b/tests/Unit/BladeDirectiveTest.php @@ -0,0 +1,142 @@ +run(); + + $this->app->singleton('aauth', function ($app) { + return new \AuroraWebSoftware\AAuth\AAuth( + User::find(1), + 3 + ); + }); +}); + +test('aauth blade directive compiles correctly', function () { + $compiled = Blade::compileString("@aauth('test_permission')"); + + expect($compiled)->toContain(''); +}); + +test('endaauth blade directive compiles correctly', function () { + $compiled = Blade::compileString('@endaauth'); + + expect($compiled)->toBe(''); +}); + +test('aauth blade directive with permission renders content when user has permission', function () { + $blade = "@aauth('create_something_for_organization') +
User has permission
+@endaauth"; + + $compiled = Blade::compileString($blade); + ob_start(); + eval('?>'.$compiled); + $output = ob_get_clean(); + + expect(trim($output))->toContain('User has permission'); +}); + +test('aauth blade directive hides content when user lacks permission', function () { + $blade = "@aauth('non_existent_permission') +
User has permission
+@endaauth"; + + $compiled = Blade::compileString($blade); + ob_start(); + eval('?>'.$compiled); + $output = ob_get_clean(); + + expect(trim($output))->toBe(''); +}); + +test('aauth blade directive works with single quotes', function () { + $blade = "@aauth('create_something_for_organization') +

Content visible

+@endaauth"; + + $compiled = Blade::compileString($blade); + + expect($compiled)->toContain(""); + expect($compiled)->toContain(''); +}); + +test('aauth blade directive works with double quotes', function () { + $blade = '@aauth("create_something_for_organization") +

Content visible

+@endaauth'; + + $compiled = Blade::compileString($blade); + + expect($compiled)->toContain(''); + expect($compiled)->toContain(''); +}); + +test('aauth blade directive works with variable', function () { + $blade = '@aauth($permission) +

Content visible

+@endaauth'; + + $compiled = Blade::compileString($blade); + + expect($compiled)->toContain(''); + expect($compiled)->toContain(''); +}); + +test('nested aauth blade directives compile correctly', function () { + $blade = "@aauth('permission1') +
Outer content
+ @aauth('permission2') +
Inner content
+ @endaauth +@endaauth"; + + $compiled = Blade::compileString($blade); + + expect($compiled)->toContain(""); + expect($compiled)->toContain(""); +}); + +test('aauth blade directive with html content renders correctly when permitted', function () { + $blade = "@aauth('create_something_for_organization') + + Admin Panel +@endaauth"; + + $compiled = Blade::compileString($blade); + ob_start(); + eval('?>'.$compiled); + $output = ob_get_clean(); + + expect($output)->toContain('') + ->and($output)->toContain('Admin Panel'); +}); + +test('multiple aauth blocks in same template work independently', function () { + $blade = "@aauth('create_something_for_organization') +
First block
+@endaauth + +@aauth('non_existent_permission') +
Second block
+@endaauth + +@aauth('create_something_for_organization') +
Third block
+@endaauth"; + + $compiled = Blade::compileString($blade); + ob_start(); + eval('?>'.$compiled); + $output = ob_get_clean(); + + expect($output)->toContain('First block') + ->and($output)->toContain('Third block') + ->and($output)->not->toContain('Second block'); +});