From 59ae79641bc5ba6a96f86692d96453a8854d74cd Mon Sep 17 00:00:00 2001 From: Jon Waldstein Date: Wed, 23 Oct 2024 10:22:21 -0400 Subject: [PATCH] tests: add tests for error messages --- .../DonationForm/Rules/TurnstileFieldRule.php | 1 + .../ValueObjects/TurnstileVerifyResponse.php | 8 ++++ .../TestTurnstileVerifyResponse.php | 47 +++++++++++++++++++ 3 files changed, 56 insertions(+) diff --git a/src/FormExtension/DonationForm/Rules/TurnstileFieldRule.php b/src/FormExtension/DonationForm/Rules/TurnstileFieldRule.php index ab47d1f..90425a8 100644 --- a/src/FormExtension/DonationForm/Rules/TurnstileFieldRule.php +++ b/src/FormExtension/DonationForm/Rules/TurnstileFieldRule.php @@ -56,6 +56,7 @@ public function __invoke($value, Closure $fail, string $key, array $values) if (!$response->isSuccess()) { Log::spam(__('Turnstile verification failed.', 'givewp-cloudflare-turnstile'), [ 'response' => $response, + 'errorMessages' => $response->getErrorMessages() ?? [], 'formId' => $values['formId'] ?? null, ]); diff --git a/src/Turnstile/ValueObjects/TurnstileVerifyResponse.php b/src/Turnstile/ValueObjects/TurnstileVerifyResponse.php index 6f5e54e..8c5a843 100644 --- a/src/Turnstile/ValueObjects/TurnstileVerifyResponse.php +++ b/src/Turnstile/ValueObjects/TurnstileVerifyResponse.php @@ -94,4 +94,12 @@ public function getErrorMessage(string $code): ?string return 'Invalid response'; } } + + /** + * @return array + */ + public function getErrorMessages(): array + { + return array_map([$this, 'getErrorMessage'], $this->errorCodes); + } } diff --git a/tests/Unit/Turnstile/ValueObjects/TestTurnstileVerifyResponse.php b/tests/Unit/Turnstile/ValueObjects/TestTurnstileVerifyResponse.php index acfc601..abe7384 100644 --- a/tests/Unit/Turnstile/ValueObjects/TestTurnstileVerifyResponse.php +++ b/tests/Unit/Turnstile/ValueObjects/TestTurnstileVerifyResponse.php @@ -29,4 +29,51 @@ public function testShouldReturnSelf(): void $this->assertTrue($turnstileVerifyResponse->success); } + /** + * @since 1.0.0 + * @dataProvider errorMessagesProvider + */ + public function testShouldGetErrorMessage(string $code, string $message): void + { + $turnstileVerifyResponse = new TurnstileVerifyResponse((object)[ + 'success' => false, + 'error_codes' => [$code], + ]); + + $this->assertEquals($message, $turnstileVerifyResponse->getErrorMessage($code)); + } + + /** + * @since 1.0.0 + */ + public function testShouldGetErrorMessages(): void + { + $turnstileVerifyResponse = new TurnstileVerifyResponse((object)[ + 'success' => false, + 'error_codes' => ['missing-input-secret', 'invalid-input-secret'], + ]); + + $this->assertEquals([ + __("The secret parameter was not passed.", 'givewp-cloudflare-turnstile'), + __("The secret parameter was invalid or did not exist.", 'givewp-cloudflare-turnstile'), + ], $turnstileVerifyResponse->getErrorMessages()); + } + + /** + * @since 1.0.0 + */ + public function errorMessagesProvider(): array + { + return [ + ['missing-input-secret', __("The secret parameter was not passed.", 'givewp-cloudflare-turnstile')], + ['invalid-input-secret', __("The secret parameter was invalid or did not exist.", 'givewp-cloudflare-turnstile')], + ['missing-input-response', __("The response parameter (token) was not passed.", 'givewp-cloudflare-turnstile')], + ['invalid-input-response', __("The response parameter (token) is invalid or has expired. Most of the time, this means a fake token has been used. If the error persists, contact customer support.", 'givewp-cloudflare-turnstile')], + ['bad-request', __("The request was rejected because it was malformed.", 'givewp-cloudflare-turnstile')], + ['timeout-or-duplicate', __("The response parameter (token) has already been validated before. This means that the token was issued five minutes ago and is no longer valid, or it was already redeemed.", 'givewp-cloudflare-turnstile')], + ['internal-error', __("An internal error happened while validating the response. The request can be retried.", 'givewp-cloudflare-turnstile')], + ['', __("Invalid response", 'givewp-cloudflare-turnstile')], + ]; + } + }