Skip to content

Commit

Permalink
Merge pull request #144 from mikemand/fix-blade-directives
Browse files Browse the repository at this point in the history
Fix blade directives
  • Loading branch information
albertcht authored Apr 10, 2021
2 parents 36e3d1c + e359b55 commit edb97f9
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 9 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ language: php
php:
- '7.2'
- '7.3'
- '7.4'

install:
- travis_retry composer install --no-interaction --prefer-dist --no-suggest
Expand Down
18 changes: 16 additions & 2 deletions src/InvisibleReCaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace AlbertCht\InvisibleReCaptcha;

use Illuminate\Support\Arr;
use Symfony\Component\HttpFoundation\Request;
use GuzzleHttp\Client;

Expand Down Expand Up @@ -92,10 +93,20 @@ public function render($lang = null, $nonce = null)
{
$html = $this->renderPolyfill();
$html .= $this->renderCaptchaHTML();
$html .= $this->renderFooterJS($lang, $nonce);
$html .= $this->renderFooterJS([$lang, $nonce]);
return $html;
}

/**
* Render HTML reCaptcha from directive.
*
* @return string
*/
public function renderCaptcha(...$arguments)
{
return $this->render(...$arguments);
}

/**
* Render the polyfill JS components only.
*
Expand Down Expand Up @@ -128,8 +139,11 @@ public function renderCaptchaHTML()
*
* @return string
*/
public function renderFooterJS($lang = null, $nonce = null)
public function renderFooterJS(...$arguments)
{
$lang = Arr::get($arguments, 0);
$nonce = Arr::get($arguments, 1);

$html = '<script src="' . $this->getCaptchaJs($lang) . '" async defer';
if (isset($nonce) && ! empty($nonce)) {
$html .= ' nonce="' . $nonce . '"';
Expand Down
8 changes: 4 additions & 4 deletions src/InvisibleReCaptchaServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,17 @@ public function provides()
*/
public function addBladeDirective(BladeCompiler $blade)
{
$blade->directive('captcha', function ($lang) {
return "<?php echo app('captcha')->render({$lang}); ?>";
$blade->directive('captcha', function ($arguments) {
return "<?php echo app('captcha')->renderCaptcha({$arguments}); ?>";
});
$blade->directive('captchaPolyfill', function () {
return "<?php echo app('captcha')->renderPolyfill(); ?>";
});
$blade->directive('captchaHTML', function () {
return "<?php echo app('captcha')->renderCaptchaHTML(); ?>";
});
$blade->directive('captchaScripts', function ($lang, $nonce) {
return "<?php echo app('captcha')->renderFooterJS({$lang}, {$nonce}); ?>";
$blade->directive('captchaScripts', function ($arguments) {
return "<?php echo app('captcha')->renderFooterJS({$arguments}); ?>";
});
}
}
45 changes: 42 additions & 3 deletions tests/CaptchaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ public function testGetCaptchaJs()
$this->assertEquals($js . '?hl=us', $this->captcha->getCaptchaJs('us'));
}

public function testJavascriptHasNonce()
{
$this->assertStringContainsString('nonce="nonce-ASDFGHJKL"', $this->captcha->renderFooterJS('us', 'nonce-ASDFGHJKL'));
}

public function testGetPolyfillJs()
{
$js = 'https://cdn.polyfill.io/v2/polyfill.min.js';
Expand All @@ -79,10 +84,44 @@ public function testBladeDirective()
$serviceProvider = new InvisibleReCaptchaServiceProvider($app);
$serviceProvider->addBladeDirective($blade);

$result = $blade->compileString('@captcha()');
$this->assertEquals(
"<?php echo app('captcha')->render(); ?>",
$result
"<?php echo app('captcha')->renderCaptcha(); ?>",
$blade->compileString('@captcha()')
);

$this->assertEquals(
"<?php echo app('captcha')->renderCaptcha('us'); ?>",
$blade->compileString("@captcha('us')")
);

$this->assertEquals(
"<?php echo app('captcha')->renderCaptcha('us', 'nonce-ASDFGHJKL'); ?>",
$blade->compileString("@captcha('us', 'nonce-ASDFGHJKL')")
);

$this->assertEquals(
"<?php echo app('captcha')->renderPolyfill(); ?>",
$blade->compileString('@captchaPolyfill()')
);

$this->assertEquals(
"<?php echo app('captcha')->renderCaptchaHTML(); ?>",
$blade->compileString('@captchaHTML()')
);

$this->assertEquals(
"<?php echo app('captcha')->renderFooterJS(); ?>",
$blade->compileString('@captchaScripts()')
);

$this->assertEquals(
"<?php echo app('captcha')->renderFooterJS('us'); ?>",
$blade->compileString("@captchaScripts('us')")
);

$this->assertEquals(
"<?php echo app('captcha')->renderFooterJS('us', 'nonce-ASDFGHJKL'); ?>",
$blade->compileString("@captchaScripts('us', 'nonce-ASDFGHJKL')")
);
}
}

0 comments on commit edb97f9

Please sign in to comment.