Skip to content

Commit fafa0fd

Browse files
authored
Merge pull request #315 from hCaptcha/v3.9.0
V3.9.0
2 parents 4369880 + 7fb7d58 commit fafa0fd

File tree

129 files changed

+6000
-1274
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

129 files changed

+6000
-1274
lines changed

.tests/js/assets-js-files/hcaptcha-wc-checkout.test.js

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,28 @@ require( '../../../assets/js/hcaptcha-wc-checkout' );
1212
window.hCaptchaWC( $ );
1313

1414
describe( 'hCaptcha WooCommerce', () => {
15-
let hCaptchaReset;
1615
let hCaptchaBindEvents;
1716

1817
beforeEach( () => {
19-
hCaptchaReset = jest.fn();
20-
global.hCaptchaReset = hCaptchaReset;
21-
2218
hCaptchaBindEvents = jest.fn();
23-
global.hCaptchaBindEvents = hCaptchaBindEvents;
19+
window.hCaptchaBindEvents = hCaptchaBindEvents;
2420
} );
2521

2622
afterEach( () => {
27-
global.hCaptchaReset.mockRestore();
28-
global.hCaptchaBindEvents.mockRestore();
23+
window.hCaptchaBindEvents.mockRestore();
2924
} );
3025

31-
test( 'checkout_error event triggers hCaptchaReset', () => {
26+
test( 'checkout_error event triggers hCaptchaBindEvents', () => {
3227
const event = new CustomEvent( 'checkout_error' );
3328
document.body.dispatchEvent( event );
3429

35-
expect( hCaptchaReset ).toHaveBeenCalledTimes( 1 );
30+
expect( hCaptchaBindEvents ).toHaveBeenCalledTimes( 1 );
3631
} );
3732

38-
test( 'updated_checkout event triggers hCaptchaBindEvents and hCaptchaReset', () => {
33+
test( 'updated_checkout event triggers hCaptchaBindEvents', () => {
3934
const event = new CustomEvent( 'updated_checkout' );
4035
document.body.dispatchEvent( event );
4136

4237
expect( hCaptchaBindEvents ).toHaveBeenCalledTimes( 1 );
43-
expect( hCaptchaReset ).toHaveBeenCalledTimes( 1 );
4438
} );
4539
} );

.tests/php/integration/AAAMainTest.php

Lines changed: 169 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
namespace HCaptcha\Tests\Integration;
1515

16-
use HCaptcha\Admin\Notifications;
1716
use HCaptcha\AutoVerify\AutoVerify;
1817
use HCaptcha\BBPress\NewTopic;
1918
use HCaptcha\BBPress\Reply;
@@ -582,6 +581,152 @@ static function ( $name ) {
582581
self::assertSame( $expected, ob_get_clean() );
583582
}
584583

584+
/**
585+
* Test get_api_url().
586+
*
587+
* @param string $api_host API host.
588+
* @param string $expected Expected value.
589+
*
590+
* @return void
591+
* @dataProvider dp_test_get_api_url
592+
*/
593+
public function test_get_api_url( string $api_host, string $expected ) {
594+
update_option(
595+
'hcaptcha_settings',
596+
[
597+
'api_host' => $api_host,
598+
]
599+
);
600+
601+
$subject = new Main();
602+
603+
$subject->init_hooks();
604+
605+
self::assertSame( $expected, $subject->get_api_url() );
606+
}
607+
608+
/**
609+
* Data provider for test_get_api_url().
610+
*
611+
* @return array
612+
*/
613+
public function dp_test_get_api_url(): array {
614+
return [
615+
[ '', 'https://js.hcaptcha.com/1/api.js' ],
616+
[ ' ', 'https://js.hcaptcha.com/1/api.js' ],
617+
[ 'cn1.hcaptcha.com', 'https://cn1.hcaptcha.com/1/api.js' ],
618+
[ 'http://cn1.hcaptcha.com', 'https://cn1.hcaptcha.com/1/api.js' ],
619+
[ 'https://cn1.hcaptcha.com', 'https://cn1.hcaptcha.com/1/api.js' ],
620+
];
621+
}
622+
623+
/**
624+
* Test get_api_src().
625+
*
626+
* @return void
627+
*/
628+
public function test_get_api_src() {
629+
update_option(
630+
'hcaptcha_settings',
631+
[
632+
'recaptcha_compat_off' => [ 'on' ],
633+
'custom_themes' => [ 'on' ],
634+
'asset_host' => 'assets-cn1.hcaptcha.com',
635+
'endpoint' => 'cn1.hcaptcha.com',
636+
'host' => 'cn1.hcaptcha.com',
637+
'image_host' => 'imgs-cn1.hcaptcha.com',
638+
'report_api' => 'reportapi-cn1.hcaptcha.com',
639+
'sentry' => 'cn1.hcaptcha.com',
640+
]
641+
);
642+
643+
$expected = 'https://js.hcaptcha.com/1/api.js?onload=hCaptchaOnLoad&render=explicit&recaptchacompat=off&custom=true&assethost=https%3A%2F%2Fassets-cn1.hcaptcha.com&endpoint=https%3A%2F%2Fcn1.hcaptcha.com&host=https%3A%2F%2Fcn1.hcaptcha.com&imghost=https%3A%2F%2Fimgs-cn1.hcaptcha.com&reportapi=https%3A%2F%2Freportapi-cn1.hcaptcha.com&sentry=https%3A%2F%2Fcn1.hcaptcha.com';
644+
645+
$subject = new Main();
646+
647+
$subject->init_hooks();
648+
649+
self::assertSame( $expected, $subject->get_api_src() );
650+
}
651+
652+
/**
653+
* Test get_verify_url().
654+
*
655+
* @param string $backend Backend.
656+
* @param string $expected Expected value.
657+
*
658+
* @return void
659+
* @dataProvider dp_test_get_verify_url
660+
*/
661+
public function test_get_verify_url( string $backend, string $expected ) {
662+
update_option(
663+
'hcaptcha_settings',
664+
[
665+
'backend' => $backend,
666+
]
667+
);
668+
669+
$subject = new Main();
670+
671+
$subject->init_hooks();
672+
673+
self::assertSame( $expected, $subject->get_verify_url() );
674+
}
675+
676+
/**
677+
* Data provider for test_get_verify_url().
678+
*
679+
* @return array
680+
*/
681+
public function dp_test_get_verify_url(): array {
682+
return [
683+
[ '', 'https://api.hcaptcha.com/siteverify' ],
684+
[ ' ', 'https://api.hcaptcha.com/siteverify' ],
685+
[ 'cn1.hcaptcha.com', 'https://cn1.hcaptcha.com/siteverify' ],
686+
[ 'http://cn1.hcaptcha.com', 'https://cn1.hcaptcha.com/siteverify' ],
687+
[ 'https://cn1.hcaptcha.com', 'https://cn1.hcaptcha.com/siteverify' ],
688+
];
689+
}
690+
691+
/**
692+
* Test get_check_site_config_url().
693+
*
694+
* @param string $backend Backend.
695+
* @param string $expected Expected value.
696+
*
697+
* @return void
698+
* @dataProvider dp_test_get_check_site_config_url
699+
*/
700+
public function test_get_check_site_config_url( string $backend, string $expected ) {
701+
update_option(
702+
'hcaptcha_settings',
703+
[
704+
'backend' => $backend,
705+
]
706+
);
707+
708+
$subject = new Main();
709+
710+
$subject->init_hooks();
711+
712+
self::assertSame( $expected, $subject->get_check_site_config_url() );
713+
}
714+
715+
/**
716+
* Data provider for test_get_check_site_config_url().
717+
*
718+
* @return array
719+
*/
720+
public function dp_test_get_check_site_config_url(): array {
721+
return [
722+
[ '', 'https://api.hcaptcha.com/checksiteconfig' ],
723+
[ ' ', 'https://api.hcaptcha.com/checksiteconfig' ],
724+
[ 'cn1.hcaptcha.com', 'https://cn1.hcaptcha.com/checksiteconfig' ],
725+
[ 'http://cn1.hcaptcha.com', 'https://cn1.hcaptcha.com/checksiteconfig' ],
726+
[ 'https://cn1.hcaptcha.com', 'https://cn1.hcaptcha.com/checksiteconfig' ],
727+
];
728+
}
729+
585730
/**
586731
* Test print_footer_scripts().
587732
*
@@ -758,6 +903,29 @@ function delayedLoad() {
758903
self::assertNotFalse( strpos( $scripts, $expected_scripts ) );
759904
}
760905

906+
/**
907+
* Test print_footer_scripts when blocked by filter.
908+
*
909+
* @return void
910+
*/
911+
public function test_print_footer_script_when_blocked_by_filter() {
912+
add_filter( 'hcap_activate', '__return_true' );
913+
add_filter( 'hcap_print_hcaptcha_scripts', '__return_false' );
914+
915+
$subject = new Main();
916+
917+
$subject->init_hooks();
918+
919+
self::assertFalse( wp_script_is( 'hcaptcha' ) );
920+
921+
ob_start();
922+
do_action( 'wp_print_footer_scripts' );
923+
$scripts = ob_get_clean();
924+
925+
self::assertFalse( wp_script_is( 'hcaptcha' ) );
926+
self::assertSame( '', $scripts );
927+
}
928+
761929
/**
762930
* Data provider for test_print_footer_scripts().
763931
*

.tests/php/integration/Admin/NotificationsTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ public function test_init( bool $empty_keys ) {
7070
'text' => 'Read post',
7171
],
7272
],
73+
'please-rate' => [
74+
'title' => 'Rate hCaptcha plugin',
75+
'message' => 'Please rate <strong>hCaptcha for WordPress</strong> <a href="https://wordpress.org/support/plugin/hcaptcha-for-forms-and-more/reviews/?filter=5#new-post" target="_blank" rel="noopener noreferrer">★★★★★</a> on <a href="https://wordpress.org/support/plugin/hcaptcha-for-forms-and-more/reviews/?filter=5#new-post" target="_blank" rel="noopener noreferrer">WordPress.org</a>. Thank you!',
76+
'button' => [
77+
'url' => 'https://wordpress.org/support/plugin/hcaptcha-for-forms-and-more/reviews/?filter=5#new-post',
78+
'text' => 'Rate',
79+
],
80+
],
7381
'search-integrations' => [
7482
'title' => 'Search on Integrations page',
7583
'message' => 'Now you can search for plugin an themes on the Integrations page.',
@@ -78,6 +86,14 @@ public function test_init( bool $empty_keys ) {
7886
'text' => 'Start search',
7987
],
8088
],
89+
'enterprise-support' => [
90+
'title' => 'Support for Enterprise features',
91+
'message' => 'The hCaptcha plugin commenced support for Enterprise features. Solve your fraud and abuse problem today.',
92+
'button' => [
93+
'url' => 'https://www.hcaptcha.com/#enterprise-features?r=wp&utm_source=wordpress&utm_medium=wpplugin&utm_campaign=not',
94+
'text' => 'Get started',
95+
],
96+
],
8197
];
8298

8399
if ( ! $empty_keys ) {

.tests/php/integration/AutoVerify/AutoVerifyTest.php

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
use HCaptcha\AutoVerify\AutoVerify;
1616
use HCaptcha\Tests\Integration\HCaptchaWPTestCase;
17+
use Mockery;
1718

1819
/**
1920
* Test AutoVerify class.
@@ -39,15 +40,13 @@ public function test_init_and_init_hooks() {
3940
$subject = new AutoVerify();
4041
$subject->init();
4142

42-
self::assertSame(
43-
- PHP_INT_MAX,
44-
has_action( 'init', [ $subject, 'verify_form' ] )
45-
);
46-
43+
self::assertSame( -PHP_INT_MAX, has_action( 'init', [ $subject, 'verify_form' ] ) );
44+
self::assertSame( PHP_INT_MAX, has_filter( 'the_content', [ $subject, 'content_filter' ] ) );
4745
self::assertSame(
4846
PHP_INT_MAX,
49-
has_filter( 'the_content', [ $subject, 'content_filter' ] )
47+
has_filter( 'widget_block_content', [ $subject, 'widget_block_content_filter' ] )
5048
);
49+
self::assertSame( 10, has_action( 'hcap_auto_verify_register', [ $subject, 'content_filter' ] ) );
5150
}
5251

5352
/**
@@ -68,6 +67,26 @@ public function test_content_filter() {
6867
self::assertSame( $expected, get_transient( $subject::TRANSIENT ) );
6968
}
7069

70+
/**
71+
* Test widget_block_content_filter().
72+
*/
73+
public function test_widget_block_content_filter() {
74+
$wp_widget_block = Mockery::mock( 'WP_Widget_Block' );
75+
76+
$request_uri = $this->get_test_request_uri();
77+
$content = $this->get_test_content();
78+
79+
$_SERVER['REQUEST_URI'] = $request_uri;
80+
81+
$expected = $this->get_test_registered_forms();
82+
83+
$subject = new AutoVerify();
84+
85+
self::assertFalse( get_transient( $subject::TRANSIENT ) );
86+
self::assertSame( $content, $subject->widget_block_content_filter( $content, [], $wp_widget_block ) );
87+
self::assertSame( $expected, get_transient( $subject::TRANSIENT ) );
88+
}
89+
7190
/**
7291
* Test content_filter() with action containing host.
7392
*/

0 commit comments

Comments
 (0)