Skip to content

Commit

Permalink
Merge pull request #101 from ARCANEDEV/fixing-captcha-lang
Browse files Browse the repository at this point in the history
Fixing the captcha's lang
  • Loading branch information
arcanedev-maroc committed Oct 2, 2020
2 parents 537348f + 95f326b commit bc6b639
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 19 deletions.
3 changes: 1 addition & 2 deletions composer.json
Expand Up @@ -38,8 +38,7 @@
}
},
"scripts": {
"test": "phpunit",
"coverage": "phpunit --coverage-html build/coverage/html"
"test": "phpunit --testdox"
},
"extra": {
"branch-alias": {
Expand Down
12 changes: 11 additions & 1 deletion src/AbstractNoCaptcha.php
Expand Up @@ -152,6 +152,16 @@ public function setLang($lang)
return $this;
}

/**
* Get language code.
*
* @return string|null
*/
public function getLang()
{
return $this->lang;
}

/**
* Set HTTP Request Client.
*
Expand Down Expand Up @@ -279,7 +289,7 @@ abstract protected function parseResponse($json);
*/
protected function hasLang()
{
return ! empty($this->lang);
return ! empty($this->getLang());
}

/**
Expand Down
11 changes: 9 additions & 2 deletions src/Contracts/NoCaptcha.php
Expand Up @@ -23,7 +23,7 @@ interface NoCaptcha
*
* @param \Arcanedev\NoCaptcha\Contracts\Utilities\Request $request
*
* @return self
* @return $this
*/
public function setRequestClient(Request $request);

Expand All @@ -32,10 +32,17 @@ public function setRequestClient(Request $request);
*
* @param string $lang
*
* @return self
* @return $this
*/
public function setLang($lang);

/**
* Get language code.
*
* @return string|null
*/
public function getLang();

/* -----------------------------------------------------------------
| Main Methods
| -----------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion src/NoCaptchaManager.php
Expand Up @@ -84,7 +84,7 @@ protected function buildDriver(string $driver): NoCaptchaContract
return $this->container->make($driver, [
'secret' => $this->config('secret'),
'siteKey' => $this->config('sitekey'),
'locale' => $this->config('lang') ?: $this->container->getLocale(),
'lang' => $this->config('lang') ?: $this->container->getLocale(),
]);
}

Expand Down
2 changes: 1 addition & 1 deletion src/NoCaptchaV2.php
Expand Up @@ -45,7 +45,7 @@ private function getScriptSrc($callbackName = null)
$queries = [];

if ($this->hasLang())
Arr::set($queries, 'hl', $this->lang);
Arr::set($queries, 'hl', $this->getLang());

if ($this->hasCallbackName($callbackName)) {
Arr::set($queries, 'onload', $callbackName);
Expand Down
7 changes: 3 additions & 4 deletions src/NoCaptchaV3.php
Expand Up @@ -6,7 +6,6 @@

use Arcanedev\Html\Elements\Input;
use Arcanedev\NoCaptcha\Utilities\ResponseV3;
use Illuminate\Support\Arr;

/**
* Class NoCaptchaV3
Expand Down Expand Up @@ -128,12 +127,12 @@ private function getScriptSrc($callbackName = null)
$queries = [];

if ($this->hasLang())
Arr::set($queries, 'hl', $this->lang);
$queries['hl'] = $this->getLang();

Arr::set($queries, 'render', $this->getSiteKey());
$queries['render'] = $this->getSiteKey();

if ($this->hasCallbackName($callbackName))
Arr::set($queries, 'onload', $callbackName);
$queries['onload'] = $callbackName;

return $this->getClientUrl() . (count($queries) ? '?' . http_build_query($queries) : '');
}
Expand Down
35 changes: 27 additions & 8 deletions tests/Laravel/NoCaptchaManagerTest.php
Expand Up @@ -81,15 +81,16 @@ public function it_can_get_default_driver_via_helper(): void
/** @test */
public function it_can_get_driver_by_given_version(): void
{
static::assertInstanceOf(
NoCaptchaV3::class,
$this->manager->version('v3')
);
$versions = [
'v3' => NoCaptchaV3::class,
'v2' => NoCaptchaV2::class,
];

static::assertInstanceOf(
NoCaptchaV2::class,
$this->manager->version('v2')
);
foreach ($versions as $version => $class) {
$captcha = $this->manager->version();

static::assertInstanceOf(NoCaptchaV3::class, $captcha);
}
}

/** @test */
Expand All @@ -100,4 +101,22 @@ public function it_must_throw_exception_on_unsupported_version(): void

$this->manager->version('v1');
}

/** @test */
public function it_can_set_lang_from_locale()
{
$versions = [
'v3' => NoCaptchaV3::class,
'v2' => NoCaptchaV2::class,
];

$this->app->setLocale('fr');

foreach ($versions as $version => $class) {
$captcha = $this->manager->version($version);

static::assertInstanceOf($class, $captcha);
static::assertSame('fr', $captcha->getLang());
}
}
}

0 comments on commit bc6b639

Please sign in to comment.