Skip to content

Commit

Permalink
Merge pull request #64 from phpbb/develop
Browse files Browse the repository at this point in the history
Merge Develop into master
  • Loading branch information
Crizz0 authored May 5, 2019
2 parents 4aa9421 + e4ed353 commit 4fefa3d
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
97 changes: 97 additions & 0 deletions src/Phpbb/TranslationValidator/Validator/FileValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,81 @@ class FileValidator
/** @var \Phpbb\TranslationValidator\Output\OutputInterface */
protected $output;

/** @var array List from https://developers.google.com/recaptcha/docs/language */
private $reCaptchaLanguages = [
'ar',
'af',
'am',
'hy',
'az',
'eu',
'bn',
'bg',
'ca',
'zh-HK',
'zh-CN',
'zh-TW',
'hr',
'cs',
'da',
'nl',
'en-GB',
'en',
'et',
'fil',
'fi',
'fr',
'fr-CA',
'gl',
'ka',
'de',
'de-AT',
'de-CH',
'el',
'gu',
'iw',
'hi',
'hu',
'is',
'id',
'it',
'ja',
'kn',
'ko',
'lo',
'lv',
'lt',
'ms',
'ml',
'mr',
'mn',
'no',
'fa',
'pl',
'pt',
'pt-BR',
'pt-PT',
'ro',
'ru',
'sr',
'si',
'sk',
'sl',
'es',
'es-419',
'sw',
'sv',
'ta',
'te',
'th',
'tr',
'uk',
'ur',
'vi',
'zu',
'', // Allow empty strings
];

/**
* @param InputInterface $input
* @param OutputInterface $output
Expand Down Expand Up @@ -281,6 +356,28 @@ public function validateLangFile($sourceFile, $originFile)
$this->output->addMessage(Output::FATAL, 'Must not contain key: ' . $validateLangKey, $originFile);
}
}

// Check reCaptcha file
if ($originFile === $this->originLanguagePath . 'captcha_recaptcha.php')
{
$this->validateReCaptchaValue($originFile, $validate);
}
}

/**
* Check that the reCaptcha key provided is allowed
* @param $originFile
* @param array $validate
*/
public function validateReCaptchaValue($originFile, $validate)
{
// The key 'RECAPTCHA_LANG' must match the list provided by Google, or be left empty
// If any other key is used, we will show an error
if (array_key_exists('RECAPTCHA_LANG', $validate) && !in_array($validate['RECAPTCHA_LANG'], $this->reCaptchaLanguages))
{
// The supplied value doesn't match the allowed values
$this->output->addMessage(Output::ERROR, 'reCaptcha must match a language/country code on https://developers.google.com/recaptcha/docs/language - if no code exists for your language you can use "en" or leave the string empty', $originFile, 'RECAPTCHA_LANG');
}
}

/**
Expand Down
22 changes: 22 additions & 0 deletions tests/FileValidator/ValidateLangTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,26 @@ public function testValidateLangFile($file, $expected)
$this->validator->validateLangFile($file, $file);
$this->assertOutputMessages($expected);
}

/**
* Test the reCaptcha checks
*/
public function testValidateLangReCaptcha()
{
// Failure - as we supply a key that isn't valid
$reCaptchaLanguage = ['RECAPTCHA_LANG' => 'incorrect'];
$this->validator->validateReCaptchaValue('', $reCaptchaLanguage);

$output = $this->output->getMessages();
$expected = Output::ERROR . '-reCaptcha must match a language/country code on https://developers.google.com/recaptcha/docs/language - if no code exists for your language you can use "en" or leave the string empty--RECAPTCHA_LANG';

$this->assertEquals($this->output->getMessageCount(Output::ERROR), 1);
$this->assertEquals($output[0], $expected);

// Pass - as 'en' is valid
$reCaptchaLanguage['RECAPTCHA_LANG'] = 'en';
$this->validator->validateReCaptchaValue('', $reCaptchaLanguage);

$this->assertEquals($this->output->getMessageCount(Output::ERROR), 1); // Shouldn't change in size as no error added
}
}

0 comments on commit 4fefa3d

Please sign in to comment.