Skip to content

Commit

Permalink
tests: add tests for error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Waldstein authored and Jon Waldstein committed Oct 23, 2024
1 parent 7e459c6 commit 59ae796
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);

Expand Down
8 changes: 8 additions & 0 deletions src/Turnstile/ValueObjects/TurnstileVerifyResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
47 changes: 47 additions & 0 deletions tests/Unit/Turnstile/ValueObjects/TestTurnstileVerifyResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')],
];
}

}

0 comments on commit 59ae796

Please sign in to comment.