Skip to content
Open
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
7 changes: 7 additions & 0 deletions includes/class-recaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@ public static function get_script_url() {
* Register the reCAPTCHA script.
*/
public static function register_scripts() {
// The Events Calendar Community Events loads its own reCAPTCHA api.js on its
// submission page. Two api.js loads with different site keys break reCAPTCHA,
// and Newspack's client does not protect TEC forms, so bail early rather
// than breaking TEC's own reCAPTCHA.
if ( \function_exists( 'tribe_is_community_edit_event_page' ) && \tribe_is_community_edit_event_page() ) {
return;
}
if ( self::can_use_captcha() ) {
\wp_enqueue_style(
self::SCRIPT_HANDLE,
Expand Down
20 changes: 20 additions & 0 deletions tests/mocks/tec-community-events-mocks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
/**
* The Events Calendar Community Events mocks.
*
* @package Newspack\Tests
*/

if ( ! function_exists( 'tribe_is_community_edit_event_page' ) ) {
/**
* Stub for The Events Calendar Community Events plugin helper.
*
* Reads a global so tests can toggle the TEC community submission page state.
*
* @return bool
*/
function tribe_is_community_edit_event_page() {
global $newspack_test_is_tec_community_page;
return $newspack_test_is_tec_community_page ?? false;
}
}
98 changes: 98 additions & 0 deletions tests/unit-tests/recaptcha.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
/**
* Tests for the Recaptcha class.
*
* @package Newspack\Tests
*/

use Newspack\Recaptcha;

require_once NEWSPACK_ABSPATH . 'tests/mocks/tec-community-events-mocks.php';

/**
* Class Test_Recaptcha
*/
class Test_Recaptcha extends WP_UnitTestCase {
/**
* Reset options, enqueued scripts, and the TEC page global before each test.
*/
public function set_up() {
parent::set_up();
delete_option( 'newspack_recaptcha_use_captcha' );
delete_option( 'newspack_recaptcha_version' );
delete_option( 'newspack_recaptcha_credentials' );
wp_dequeue_script( 'newspack-recaptcha' );
wp_dequeue_script( 'newspack-recaptcha-api' );
wp_deregister_script( 'newspack-recaptcha' );
wp_deregister_script( 'newspack-recaptcha-api' );
Comment thread
wil-gerken marked this conversation as resolved.
wp_dequeue_style( 'newspack-recaptcha' );
wp_deregister_style( 'newspack-recaptcha' );
$GLOBALS['newspack_test_is_tec_community_page'] = false;
}

/**
* Configure valid reCAPTCHA v3 credentials so can_use_captcha() returns true.
*/
private function enable_recaptcha() {
update_option( 'newspack_recaptcha_use_captcha', true );
update_option( 'newspack_recaptcha_version', 'v3' );
update_option(
'newspack_recaptcha_credentials',
[
'v3' => [
'site_key' => 'test_key',
'site_secret' => 'test_secret',
],
'v2_invisible' => [
'site_key' => '',
'site_secret' => '',
],
]
);
}

/**
* Normal front-end pages should enqueue Google's reCAPTCHA api.js.
*/
public function test_enqueues_api_script_on_normal_page() {
$this->enable_recaptcha();
$GLOBALS['newspack_test_is_tec_community_page'] = false;

Recaptcha::register_scripts();

$this->assertTrue(
wp_script_is( 'newspack-recaptcha-api', 'enqueued' ),
'reCAPTCHA api.js should be enqueued on a normal page when reCAPTCHA is enabled.'
);
}

/**
* The TEC Community Events submission page should bail out and not enqueue api.js.
*/
public function test_skips_api_script_on_tec_community_page() {
$this->enable_recaptcha();
$GLOBALS['newspack_test_is_tec_community_page'] = true;

Recaptcha::register_scripts();

$this->assertFalse(
wp_script_is( 'newspack-recaptcha-api', 'enqueued' ),
'reCAPTCHA api.js should NOT be enqueued on TEC Community Events submission pages.'
);
Comment thread
wil-gerken marked this conversation as resolved.
}

/**
* When reCAPTCHA is disabled, api.js should never be enqueued regardless of page.
*/
public function test_does_not_enqueue_when_recaptcha_disabled() {
delete_option( 'newspack_recaptcha_use_captcha' );
$GLOBALS['newspack_test_is_tec_community_page'] = false;

Recaptcha::register_scripts();

$this->assertFalse(
wp_script_is( 'newspack-recaptcha-api', 'enqueued' ),
'reCAPTCHA api.js should not be enqueued when reCAPTCHA is disabled.'
);
}
}
Loading