Repository navigation
Fix fallback for unsupported CAPTCHA types #3690
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @covers FrmSettings | ||
| * @covers FrmFieldCaptcha | ||
| */ | ||
| class test_FrmCaptchaFallback extends FrmUnitTest { | ||
|
|
||
| /** | ||
| * @var FrmSettings | ||
| */ | ||
| private $original_settings; | ||
|
|
||
| public function setUp(): void { | ||
| parent::setUp(); | ||
| $this->original_settings = clone FrmAppHelper::get_settings(); | ||
| $this->set_front_end(); | ||
| } | ||
|
|
||
| public function tearDown(): void { | ||
| $GLOBALS['frm_settings'] = $this->original_settings; | ||
| parent::tearDown(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
|
|
||
| /** | ||
| * Load saved settings with test keys for the specified providers. | ||
| * | ||
| * @param string $active The saved provider selection. | ||
| * @param string[] $configured The providers with both keys configured. | ||
| * | ||
| * @return FrmSettings | ||
| */ | ||
| private function load_settings( $active, $configured ) { | ||
| $settings = clone $this->original_settings; | ||
| $settings->active_captcha = $active; | ||
| $settings->re_type = 'v2'; | ||
| $prefixes = array( | ||
| 'recaptcha' => '', | ||
| 'hcaptcha' => 'hcaptcha_', | ||
| 'turnstile' => 'turnstile_', | ||
| ); | ||
|
|
||
| foreach ( $prefixes as $type => $prefix ) { | ||
| $has_keys = in_array( $type, $configured, true ); | ||
| $settings->{ $prefix . 'pubkey' } = $has_keys ? 'test-site-key' : ''; | ||
| $settings->{ $prefix . 'privkey' } = $has_keys ? 'test-secret-key' : ''; | ||
| } | ||
|
|
||
| update_option( $settings->option_name, $settings ); | ||
| $GLOBALS['frm_settings'] = new FrmSettings(); | ||
| return FrmAppHelper::get_settings(); | ||
| } | ||
|
|
||
| public function test_unsupported_selection_uses_first_configured_provider() { | ||
| $providers = array( 'recaptcha', 'hcaptcha', 'turnstile' ); | ||
|
|
||
| foreach ( $providers as $expected ) { | ||
| $settings = $this->load_settings( 'altcha', $providers ); | ||
| $this->assertSame( $expected, $settings->active_captcha ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| $this->assertSame( $expected, FrmFieldCaptcha::get_captcha_image_name() ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| $this->assertSame( 'altcha', get_option( $settings->option_name )->active_captcha, 'Loading settings must preserve the saved selection.' ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| array_shift( $providers ); | ||
| } | ||
| } | ||
|
|
||
| public function test_supported_selection_is_preserved() { | ||
| foreach ( array( 'recaptcha', 'hcaptcha', 'turnstile' ) as $type ) { | ||
| $settings = $this->load_settings( $type, array( 'recaptcha', 'hcaptcha', 'turnstile' ) ); | ||
| $this->assertSame( $type, $settings->active_captcha ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
| } | ||
|
|
||
| public function test_fallback_requires_both_keys() { | ||
| $settings = $this->load_settings( 'altcha', array( 'turnstile' ) ); | ||
| $settings->pubkey = 'test-site-key'; | ||
| $settings->active_captcha = 'altcha'; | ||
| update_option( $settings->option_name, $settings ); | ||
| $this->assertSame( 'turnstile', ( new FrmSettings() )->active_captcha ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| $settings = $this->load_settings( 'altcha', array() ); | ||
| $this->assertSame( 'recaptcha', $settings->active_captcha ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| $this->assertFalse( FrmFieldCaptcha::should_show_captcha() ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
|
|
||
| public function test_fallback_renders_matching_captcha_and_loads_matching_script() { | ||
| $form_id = $this->factory->form->create(); | ||
| $this->factory->field->create( | ||
| array( | ||
| 'form_id' => $form_id, | ||
| 'type' => 'captcha', | ||
| ) | ||
| ); | ||
| $providers = array( | ||
| 'recaptcha' => array( 'g-recaptcha', 'https://www.google.com/recaptcha/api.js' ), | ||
| 'hcaptcha' => array( 'h-captcha', 'https://js.hcaptcha.com/1/api.js' ), | ||
| 'turnstile' => array( 'cf-turnstile', 'https://challenges.cloudflare.com/turnstile/v0/api.js' ), | ||
| ); | ||
|
|
||
| foreach ( $providers as $type => $expected ) { | ||
| $this->load_settings( 'altcha', array( $type ) ); | ||
| wp_deregister_script( 'captcha-api' ); | ||
| $html = do_shortcode( '[formidable id="' . $form_id . '"]' ); | ||
| $this->assertStringContainsString( $expected[0], $html ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| $this->assertStringContainsString( $expected[1], wp_scripts()->registered['captcha-api']->src ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| ob_start(); | ||
| FrmSettingsController::captcha_settings(); | ||
| $settings_html = ob_get_clean(); | ||
| $this->assertMatchesRegularExpression( '/id="frm-' . $type . '"[^>]+checked=/', $settings_html ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Preserve automatic single-widget reCAPTCHA.
When
re_multiis off,FrmFieldCaptcha::recaptcha_api_url()loads reCAPTCHA without explicit rendering, andFrmRecaptchaSettings::get_class_prefix()uses.g-recaptcha. That widget has nodata-rid. (raw.githubusercontent.com)When spam handling rejects failures, this guard now skips the challenge for that supported configuration.
validateRecaptcha()also skips.g-recaptcha, so the form submits without a token and server validation rejects it. Google’s API uses the first widget when no ID is supplied. Preserve that behavior for the automatically rendered widget. (developers.google.com)