|
| 1 | +<?php |
| 2 | + |
| 3 | +use Filament\Tests\TestCase; |
| 4 | +use Illuminate\Support\Facades\Blade; |
| 5 | + |
| 6 | +uses(TestCase::class); |
| 7 | + |
| 8 | +/* |
| 9 | +/ Default behavior tests |
| 10 | +*/ |
| 11 | +it('renders script tags without extra attributes', function (): void { |
| 12 | + $html = Blade::render('@filamentScripts'); |
| 13 | + |
| 14 | + preg_match_all('/<script[\s\S]*?>/m', $html, $matches); |
| 15 | + |
| 16 | + expect($matches[0])->not->toBeEmpty() |
| 17 | + ->and($html)->not->toContain('data-cfasync'); |
| 18 | +}); |
| 19 | + |
| 20 | +/* |
| 21 | +/ Inline attributes tests |
| 22 | +*/ |
| 23 | + |
| 24 | +it('applies inline attributes to every rendered script tag', function (): void { |
| 25 | + $html = Blade::render("@filamentScripts(attributes: ['data-cfasync' => 'false'])"); |
| 26 | + $htmlWithout = Blade::render('@filamentScripts'); |
| 27 | + |
| 28 | + preg_match_all('/<script[\s\S]*?src=[\s\S]*?>/m', $html, $withMatches); |
| 29 | + preg_match_all('/<script[\s\S]*?src=[\s\S]*?>/m', $htmlWithout, $withoutMatches); |
| 30 | + |
| 31 | + expect($withMatches[0])->toHaveCount(count($withoutMatches[0])); |
| 32 | + |
| 33 | + foreach ($withMatches[0] as $tag) { |
| 34 | + expect($tag)->toContain('data-cfasync="false"'); |
| 35 | + } |
| 36 | +}); |
| 37 | + |
| 38 | +/* |
| 39 | +/ Keyed vs non-keyed attributes tests |
| 40 | +*/ |
| 41 | + |
| 42 | +it('renders integer-keyed attributes as normal attributes, not integer keys like 0="async"', function (): void { |
| 43 | + $html = Blade::render("@filamentScripts(attributes: ['data-cfasync' => 'false', 'async'])"); |
| 44 | + |
| 45 | + expect($html) |
| 46 | + ->toContain('data-cfasync="false"') |
| 47 | + ->toContain(' async') |
| 48 | + ->not->toContain('0="async"') |
| 49 | + ->not->toContain('="async"'); |
| 50 | +}); |
| 51 | + |
| 52 | +/* |
| 53 | +/ FilamentData script block tests |
| 54 | +*/ |
| 55 | + |
| 56 | +it('applies inline attributes to the filamentData script block', function (): void { |
| 57 | + $html = Blade::render("@filamentScripts(attributes: ['data-cfasync' => 'false'])"); |
| 58 | + |
| 59 | + expect($html) |
| 60 | + ->toMatch('/<script[^>]*data-cfasync="false"[^>]*>\s*window\.filamentData/'); |
| 61 | +}); |
| 62 | + |
| 63 | +it('applies global config attributes to the filamentData script block', function (): void { |
| 64 | + config(['filament.scripts.attributes' => ['data-cfasync' => 'false']]); |
| 65 | + |
| 66 | + $html = Blade::render('@filamentScripts'); |
| 67 | + |
| 68 | + expect($html) |
| 69 | + ->toMatch('/<script[^>]*data-cfasync="false"[^>]*>\s*window\.filamentData/'); |
| 70 | +}); |
| 71 | + |
| 72 | +it('applies inline attributes to both the filamentData block and all src script tags', function (): void { |
| 73 | + $html = Blade::render("@filamentScripts(attributes: ['data-cfasync' => 'false'])"); |
| 74 | + |
| 75 | + preg_match_all('/<script[\s\S]*?data-cfasync="false"[\s\S]*?>/m', $html, $matches); |
| 76 | + |
| 77 | + expect(count($matches[0]))->toBeGreaterThanOrEqual(2); |
| 78 | +}); |
| 79 | + |
| 80 | +/* |
| 81 | +/ Global config attributes tests |
| 82 | +*/ |
| 83 | + |
| 84 | +it('global config attributes apply to every script tag', function (): void { |
| 85 | + config(['filament.scripts.attributes' => ['data-cfasync' => 'false']]); |
| 86 | + |
| 87 | + $html = Blade::render('@filamentScripts'); |
| 88 | + |
| 89 | + preg_match_all('/<script[\s\S]*?src=[\s\S]*?>/m', $html, $matches); |
| 90 | + |
| 91 | + expect($matches[0])->not->toBeEmpty(); |
| 92 | + |
| 93 | + foreach ($matches[0] as $tag) { |
| 94 | + expect($tag)->toContain('data-cfasync="false"'); |
| 95 | + } |
| 96 | +}); |
| 97 | + |
| 98 | +/* |
| 99 | +/ Overriding and merging attributes tests |
| 100 | +*/ |
| 101 | + |
| 102 | +it('inline attributes override global config attributes for the same key', function (): void { |
| 103 | + config(['filament.scripts.attributes' => ['data-cfasync' => 'false']]); |
| 104 | + |
| 105 | + $html = Blade::render("@filamentScripts(attributes: ['data-cfasync' => 'true'])"); |
| 106 | + |
| 107 | + expect($html) |
| 108 | + ->toContain('data-cfasync="true"') |
| 109 | + ->not->toContain('data-cfasync="false"'); |
| 110 | +}); |
| 111 | + |
| 112 | +it('merges global config attributes with inline attributes', function (): void { |
| 113 | + config(['filament.scripts.attributes' => ['data-cfasync' => 'false']]); |
| 114 | + |
| 115 | + $html = Blade::render("@filamentScripts(attributes: ['crossorigin' => 'anonymous'])"); |
| 116 | + |
| 117 | + expect($html) |
| 118 | + ->toContain('data-cfasync="false"') |
| 119 | + ->toContain('crossorigin="anonymous"'); |
| 120 | +}); |
0 commit comments