Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ export default [
},
},
{
files: [ 'tests/js/style-live-preview.test.mjs', 'tests/js/paypalBootstrap.test.mjs', 'tests/js/inlineSubmitAlignment.test.mjs', 'tests/js/squareVerifyBuyer.test.mjs' ],
files: [ 'tests/js/style-live-preview.test.mjs', 'tests/js/paypalBootstrap.test.mjs', 'tests/js/inlineSubmitAlignment.test.mjs', 'tests/js/squareVerifyBuyer.test.mjs', 'tests/js/invisibleRecaptcha.test.mjs' ],
rules: {
// The harnesses execute only checked-in scripts.
'sonarjs/code-eval': 'off',
Expand Down
3 changes: 2 additions & 1 deletion js/formidable.js
Original file line number Diff line number Diff line change
Expand Up @@ -1079,7 +1079,8 @@ function frmFrontFormJS() {
const recaptcha = form.querySelector( '.frm-g-recaptcha[data-size="invisible"], .g-recaptcha[data-size="invisible"]' );
if ( recaptcha ) {
const recaptchaID = recaptcha.dataset.rid;
if ( typeof grecaptcha === 'undefined' || recaptchaID === undefined ) {
// Google renders the default widget without assigning a data-rid.
if ( typeof grecaptcha === 'undefined' || ( recaptchaID === undefined && ! recaptcha.classList.contains( 'g-recaptcha' ) ) ) {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion js/formidable.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

82 changes: 82 additions & 0 deletions tests/js/invisibleRecaptcha.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';
import { test } from 'node:test';
import { runInNewContext } from 'node:vm';

const source = readFileSync( new URL( '../../js/formidable.js', import.meta.url ), 'utf8' );

/**
* Submit a form with an invisible reCAPTCHA and record what it does.
*
* @param {string} widgetClass The widget's rendering class.
* @param {string|void} widgetId The ID assigned by Formidable's explicit renderer.
* @param {Function} getResponse Returns the current response from reCAPTCHA.
* @param {Object} proForm Optional Pro form methods.
* @return {Array} Calls made to reCAPTCHA and the immediate submission handler.
*/
function submitForm( widgetClass, widgetId, getResponse, proForm ) {
const calls = [];
const widget = {
classList: { contains: name => name === widgetClass },
dataset: { rid: widgetId }
};
const form = {
classList: { contains: () => false },
querySelector: selector => selector.includes( `.${ widgetClass }[data-size="invisible"]` ) ? widget : null
};
function jQuery() {
return { ready() {}, hasClass: () => true, querySelectorAll: () => [] };

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unexpected empty method 'ready'


Having empty functions hurts readability, and is considered a code-smell. There's almost always a way to avoid using them. If you must use one, consider adding a comment to inform the reader of its purpose.

}
const context = {
window: {},
document: { body: { classList: { contains: () => false } } },
jQuery,
grecaptcha: getResponse && {
getResponse: id => {
calls.push( [ 'getResponse', id ] );
return getResponse( id );
},
reset: id => calls.push( [ 'reset', id ] ),
execute: id => calls.push( [ 'execute', id ] )
},
frmProForm: proForm
};

runInNewContext( source, context );
const { frmFrontForm } = context.window;
context.frmFrontForm = frmFrontForm;
frmFrontForm.submitFormNow = () => calls.push( [ 'submit' ] );
frmFrontForm.submitFormManual( { preventDefault() {} }, form );

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unexpected empty method 'preventDefault'


Having empty functions hurts readability, and is considered a code-smell. There's almost always a way to avoid using them. If you must use one, consider adding a comment to inform the reader of its purpose.


return calls;
}

test( 'Invisible reCAPTCHA executes the auto-rendered widget without an ID', () => {
const calls = submitForm( 'g-recaptcha', undefined, () => '' );

assert.deepEqual( calls, [ [ 'getResponse', undefined ], [ 'reset', undefined ], [ 'execute', undefined ] ] );
} );

test( 'Invisible reCAPTCHA executes the explicitly rendered widget by ID with Pro', () => {
const calls = submitForm( 'frm-g-recaptcha', '0', () => '', { goingToPreviousPage: () => false } );

assert.deepEqual( calls, [ [ 'getResponse', '0' ], [ 'reset', '0' ], [ 'execute', '0' ] ] );
} );

test( 'An explicit invisible widget without an ID cannot run the default widget', () => {
const calls = submitForm( 'frm-g-recaptcha', undefined, () => '' );

assert.deepEqual( calls, [ [ 'submit' ] ] );
} );

test( 'An unavailable reCAPTCHA API allows the submission to reach server validation', () => {
assert.deepEqual( submitForm( 'g-recaptcha' ), [ [ 'submit' ] ] );
} );

test( 'An auto-rendered widget that is not ready allows the submission to reach server validation', () => {
const calls = submitForm( 'g-recaptcha', undefined, () => {
throw new Error( 'No reCAPTCHA clients exist.' );
} );

assert.deepEqual( calls, [ [ 'getResponse', undefined ], [ 'submit' ] ] );
} );