-
Notifications
You must be signed in to change notification settings - Fork 58
fix(recaptcha): skip script registration on TEC Community Events pages #4666
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
Open
wil-gerken
wants to merge
4
commits into
trunk
Choose a base branch
from
fix/prevent-two-captchas-tec-community-events
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+125
−0
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
484782d
fix(recaptcha): skip script registration on TEC Community Events pages
wil-gerken dc8e010
style(recaptcha): prefix globals and trim trailing whitespace
wil-gerken 2fa69ad
test(recaptcha): extract TEC stub to mocks file for PHPCS
wil-gerken 9fa745e
test(recaptcha): address Copilot feedback on assertions and cleanup
wil-gerken File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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' ); | ||
| 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.' | ||
| ); | ||
|
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.' | ||
| ); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.