|
| 1 | +<?php |
| 2 | + |
| 3 | +use Maize\GoogleRecaptchaV3\Enums\Badge; |
| 4 | +use Maize\GoogleRecaptchaV3\GoogleRecaptchaV3; |
| 5 | + |
| 6 | +beforeEach(function () { |
| 7 | + config()->set('google-recaptcha-v3.enabled', true); |
| 8 | + config()->set('google-recaptcha-v3.site_key', 'test-site-key'); |
| 9 | + config()->set('google-recaptcha-v3.secret_key', 'test-secret-key'); |
| 10 | +}); |
| 11 | + |
| 12 | +it('renders recaptcha HTML with badge parameter', function (Badge $badge, bool $shouldContainBadgeParam) { |
| 13 | + $recaptcha = new GoogleRecaptchaV3; |
| 14 | + |
| 15 | + $html = (fn () => $this->toHtml($badge))->call($recaptcha); |
| 16 | + |
| 17 | + expect($html) |
| 18 | + ->toContain('https://www.google.com/recaptcha/api.js') |
| 19 | + ->toContain('render=test-site-key') |
| 20 | + ->toContain('window.recaptcha') |
| 21 | + ->toContain('grecaptcha.execute'); |
| 22 | + |
| 23 | + if ($shouldContainBadgeParam) { |
| 24 | + expect($html)->toContain("badge={$badge->value}"); |
| 25 | + } else { |
| 26 | + expect($html)->not->toContain('badge='); |
| 27 | + } |
| 28 | +})->with([ |
| 29 | + 'bottomright badge' => [Badge::BOTTOMRIGHT, true], |
| 30 | + 'bottomleft badge' => [Badge::BOTTOMLEFT, true], |
| 31 | + 'inline badge' => [Badge::INLINE, true], |
| 32 | + 'hidden badge should not have badge param' => [Badge::HIDDEN, false], |
| 33 | +]); |
| 34 | + |
| 35 | +it('renders hidden badge style for hidden badge', function () { |
| 36 | + $recaptcha = new GoogleRecaptchaV3; |
| 37 | + |
| 38 | + $html = (fn () => $this->toHtml(Badge::HIDDEN))->call($recaptcha); |
| 39 | + |
| 40 | + expect($html) |
| 41 | + ->toContain('.grecaptcha-badge') |
| 42 | + ->toContain('visibility: hidden') |
| 43 | + ->toContain('<style>') |
| 44 | + ->toContain('</style>'); |
| 45 | +}); |
| 46 | + |
| 47 | +it('does not render hidden badge style for non-hidden badges', function (Badge $badge) { |
| 48 | + $recaptcha = new GoogleRecaptchaV3; |
| 49 | + |
| 50 | + $html = (fn () => $this->toHtml($badge))->call($recaptcha); |
| 51 | + |
| 52 | + expect($html) |
| 53 | + ->not->toContain('.grecaptcha-badge') |
| 54 | + ->not->toContain('visibility: hidden'); |
| 55 | +})->with([ |
| 56 | + Badge::BOTTOMRIGHT, |
| 57 | + Badge::BOTTOMLEFT, |
| 58 | + Badge::INLINE, |
| 59 | +]); |
| 60 | + |
| 61 | +it('returns empty string when recaptcha is disabled', function () { |
| 62 | + config()->set('google-recaptcha-v3.enabled', false); |
| 63 | + |
| 64 | + $recaptcha = new GoogleRecaptchaV3; |
| 65 | + $html = (fn () => $this->toHtml(Badge::BOTTOMRIGHT))->call($recaptcha); |
| 66 | + |
| 67 | + expect($html)->toBe(''); |
| 68 | +}); |
| 69 | + |
| 70 | +it('returns empty string when site key is missing', function () { |
| 71 | + config()->set('google-recaptcha-v3.site_key', null); |
| 72 | + |
| 73 | + $recaptcha = new GoogleRecaptchaV3; |
| 74 | + $html = (fn () => $this->toHtml(Badge::BOTTOMRIGHT))->call($recaptcha); |
| 75 | + |
| 76 | + expect($html)->toBe(''); |
| 77 | +}); |
| 78 | + |
| 79 | +it('returns empty string when secret key is missing', function () { |
| 80 | + config()->set('google-recaptcha-v3.secret_key', null); |
| 81 | + |
| 82 | + $recaptcha = new GoogleRecaptchaV3; |
| 83 | + $html = (fn () => $this->toHtml(Badge::BOTTOMRIGHT))->call($recaptcha); |
| 84 | + |
| 85 | + expect($html)->toBe(''); |
| 86 | +}); |
| 87 | + |
| 88 | +it('includes window.recaptcha function in output', function () { |
| 89 | + $recaptcha = new GoogleRecaptchaV3; |
| 90 | + |
| 91 | + $html = (fn () => $this->toHtml(Badge::BOTTOMRIGHT))->call($recaptcha); |
| 92 | + |
| 93 | + expect($html) |
| 94 | + ->toContain('window.recaptcha = function') |
| 95 | + ->toContain('action = \'submit\'') |
| 96 | + ->toContain('grecaptcha.ready') |
| 97 | + ->toContain('grecaptcha.execute'); |
| 98 | +}); |
| 99 | + |
| 100 | +it('includes site key in grecaptcha execute call', function () { |
| 101 | + $recaptcha = new GoogleRecaptchaV3; |
| 102 | + |
| 103 | + $html = (fn () => $this->toHtml(Badge::BOTTOMRIGHT))->call($recaptcha); |
| 104 | + |
| 105 | + expect($html)->toContain("grecaptcha.execute('test-site-key'"); |
| 106 | +}); |
| 107 | + |
| 108 | +it('wraps scripts in script tags', function () { |
| 109 | + $recaptcha = new GoogleRecaptchaV3; |
| 110 | + |
| 111 | + $html = (fn () => $this->toHtml(Badge::BOTTOMRIGHT))->call($recaptcha); |
| 112 | + |
| 113 | + expect($html) |
| 114 | + ->toMatch('/<script>.*?<\/script>/s') |
| 115 | + ->toContain('<script>') |
| 116 | + ->toContain('</script>'); |
| 117 | +}); |
| 118 | + |
| 119 | +it('generates correct js script url for each badge', function (Badge $badge, bool $shouldHaveBadgeQuery) { |
| 120 | + $recaptcha = new GoogleRecaptchaV3; |
| 121 | + |
| 122 | + $url = (fn () => $this->getJsScriptUrl($badge))->call($recaptcha); |
| 123 | + |
| 124 | + expect($url) |
| 125 | + ->toContain('https://www.google.com/recaptcha/api.js') |
| 126 | + ->toContain('render=test-site-key'); |
| 127 | + |
| 128 | + if ($shouldHaveBadgeQuery) { |
| 129 | + expect($url)->toContain("badge={$badge->value}"); |
| 130 | + } else { |
| 131 | + expect($url)->not->toContain('badge='); |
| 132 | + } |
| 133 | +})->with([ |
| 134 | + 'bottomright has badge query' => [Badge::BOTTOMRIGHT, true], |
| 135 | + 'bottomleft has badge query' => [Badge::BOTTOMLEFT, true], |
| 136 | + 'inline has badge query' => [Badge::INLINE, true], |
| 137 | + 'hidden does not have badge query' => [Badge::HIDDEN, false], |
| 138 | +]); |
| 139 | + |
| 140 | +it('generates js token script with site key', function () { |
| 141 | + $recaptcha = new GoogleRecaptchaV3; |
| 142 | + |
| 143 | + $script = (fn () => $this->getJsTokenScript())->call($recaptcha); |
| 144 | + |
| 145 | + expect($script) |
| 146 | + ->toContain('window.recaptcha = function') |
| 147 | + ->toContain("grecaptcha.execute('test-site-key'") |
| 148 | + ->toContain('action = \'submit\'') |
| 149 | + ->toContain('new Promise') |
| 150 | + ->toContain('grecaptcha.ready'); |
| 151 | +}); |
| 152 | + |
| 153 | +it('generates hidden badge style css', function () { |
| 154 | + $recaptcha = new GoogleRecaptchaV3; |
| 155 | + |
| 156 | + $style = (fn () => $this->getHiddenBadgeStyle())->call($recaptcha); |
| 157 | + |
| 158 | + expect($style) |
| 159 | + ->toContain('.grecaptcha-badge') |
| 160 | + ->toContain('visibility: hidden') |
| 161 | + ->not->toContain('<style>') |
| 162 | + ->not->toContain('</style>'); |
| 163 | +}); |
0 commit comments