Files
piratepoet/mailpoet/tests/unit/Subscription/Captcha/Validator/RecaptchaValidatorTest.php
Rodrigo Primo 1865fc8930 Replace expect()->isInstanceOf() with verify()->instanceOf()
codeception/verify 2.1 removed support for expect()->isInstanceOf() so we need
to replace it with verify()->instanceOf().

[MAILPOET-5664]
2023-10-24 08:58:22 +03:00

192 lines
5.7 KiB
PHP

<?php declare(strict_types = 1);
namespace MailPoet\Subscription\Captcha\Validator;
use Codeception\Stub;
use MailPoet\Settings\SettingsController;
use MailPoet\Subscription\Captcha\CaptchaConstants;
use MailPoet\WP\Functions as WPFunctions;
class RecaptchaValidatorTest extends \MailPoetUnitTest {
public function testSuccessfulInvisibleValidation() {
$captchaSettings = [
'type' => CaptchaConstants::TYPE_RECAPTCHA_INVISIBLE,
'recaptcha_invisible_secret_token' => 'recaptcha_invisible_secret_token',
'recaptcha_secret_token' => 'recaptcha_secret_token',
];
$recaptchaResponseToken = 'recaptchaResponseToken';
$response = json_encode(['success' => true]);
$settings = Stub::make(
SettingsController::class,
[
'get' => function($key) use ($captchaSettings) {
if ($key === 'captcha') {
return $captchaSettings;
}
},
],
$this
);
$wp = Stub::make(
WPFunctions::class,
[
'wpRemotePost' => function($url, $args) use ($recaptchaResponseToken, $captchaSettings, $response) {
verify($url)->equals('https://www.google.com/recaptcha/api/siteverify');
verify($args['body']['secret'])->equals($captchaSettings['recaptcha_invisible_secret_token']);
verify($args['body']['response'])->equals($recaptchaResponseToken);
return $response;
},
'isWpError' => false,
'wpRemoteRetrieveBody' => function($data) use ($response) {
verify($data)->equals($response);
return $response;
},
],
$this
);
$testee = new RecaptchaValidator($settings, $wp);
$data = [
'recaptchaResponseToken' => $recaptchaResponseToken,
];
verify($testee->validate($data))->true();
}
public function testSuccessfulValidation() {
$captchaSettings = [
'type' => CaptchaConstants::TYPE_RECAPTCHA,
'recaptcha_invisible_secret_token' => 'recaptcha_invisible_secret_token',
'recaptcha_secret_token' => 'recaptcha_secret_token',
];
$recaptchaResponseToken = 'recaptchaResponseToken';
$response = json_encode(['success' => true]);
$settings = Stub::make(
SettingsController::class,
[
'get' => function($key) use ($captchaSettings) {
if ($key === 'captcha') {
return $captchaSettings;
}
},
],
$this
);
$wp = Stub::make(
WPFunctions::class,
[
'wpRemotePost' => function($url, $args) use ($recaptchaResponseToken, $captchaSettings, $response) {
verify($url)->equals('https://www.google.com/recaptcha/api/siteverify');
verify($args['body']['secret'])->equals($captchaSettings['recaptcha_secret_token']);
verify($args['body']['response'])->equals($recaptchaResponseToken);
return $response;
},
'isWpError' => false,
'wpRemoteRetrieveBody' => function($data) use ($response) {
verify($data)->equals($response);
return $response;
},
],
$this
);
$testee = new RecaptchaValidator($settings, $wp);
$data = [
'recaptchaResponseToken' => $recaptchaResponseToken,
];
verify($testee->validate($data))->true();
}
public function testFailingValidation() {
$captchaSettings = [
'type' => CaptchaConstants::TYPE_RECAPTCHA_INVISIBLE,
'recaptcha_invisible_secret_token' => 'recaptcha_invisible_secret_token',
'recaptcha_secret_token' => 'recaptcha_secret_token',
];
$recaptchaResponseToken = 'recaptchaResponseToken';
$response = json_encode(['success' => false]);
$settings = Stub::make(
SettingsController::class,
[
'get' => function($key) use ($captchaSettings) {
if ($key === 'captcha') {
return $captchaSettings;
}
},
],
$this
);
$wp = Stub::make(
WPFunctions::class,
[
'wpRemotePost' => function() use ($response) {
return $response;
},
'isWpError' => false,
'wpRemoteRetrieveBody' => function() use ($response) {
return $response;
},
],
$this
);
$testee = new RecaptchaValidator($settings, $wp);
$data = [
'recaptchaResponseToken' => $recaptchaResponseToken,
];
$error = null;
try {
$testee->validate($data);
} catch (ValidationError $error) {
verify($error->getMessage())->equals('Error while validating the CAPTCHA.');
}
verify($error)->instanceOf(ValidationError::class);
}
public function testConnectionError() {
$captchaSettings = [
'type' => CaptchaConstants::TYPE_RECAPTCHA_INVISIBLE,
'recaptcha_invisible_secret_token' => 'recaptcha_invisible_secret_token',
'recaptcha_secret_token' => 'recaptcha_secret_token',
];
$recaptchaResponseToken = 'recaptchaResponseToken';
$response = (object)['wp-error'];
$settings = Stub::make(
SettingsController::class,
[
'get' => function($key) use ($captchaSettings) {
if ($key === 'captcha') {
return $captchaSettings;
}
},
],
$this
);
$wp = Stub::make(
WPFunctions::class,
[
'wpRemotePost' => function() use ($response) {
return $response;
},
'isWpError' => true,
],
$this
);
$testee = new RecaptchaValidator($settings, $wp);
$data = [
'recaptchaResponseToken' => $recaptchaResponseToken,
];
$error = null;
try {
$testee->validate($data);
} catch (ValidationError $error) {
verify($error->getMessage())->equals('Error while validating the CAPTCHA.');
}
verify($error)->instanceOf(ValidationError::class);
}
}