Skip to content

Commit 641a67c

Browse files
committed
test(fraud-protection): cover protected-gateway-available reCAPTCHA enqueue cases
Adds regression coverage for the previous commit: reCAPTCHA still enqueues when a protected gateway (ACDC) is available at checkout despite the smart-button location being disabled, still skips when no protected gateway is available either, and still enqueues on the My Account "Add payment method" page when one is available there.
1 parent fd9eb78 commit 641a67c

1 file changed

Lines changed: 85 additions & 0 deletions

File tree

tests/PHPUnit/FraudProtection/Recaptcha/RecaptchaTest.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use WooCommerce\PayPalCommerce\Assets\AssetGetter;
1010
use WooCommerce\PayPalCommerce\FraudProtection\PersistentCounter;
1111
use WooCommerce\PayPalCommerce\TestCase;
12+
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
1213
use WooCommerce\PayPalCommerce\WcGateway\Helper\SettingsStatus;
1314
use function Brain\Monkey\Functions\expect;
1415
use function Brain\Monkey\Functions\when;
@@ -163,6 +164,90 @@ public function test_enqueue_scripts_checks_block_express_location_on_block_chec
163164
$this->make_testee( $settings_status )->enqueue_scripts();
164165
}
165166

167+
/**
168+
* GIVEN the smart-button location is disabled for checkout (no wallet button)
169+
* WHEN a reCAPTCHA-protected gateway (e.g. ACDC) is still available as a
170+
* selectable WC payment gateway at checkout
171+
* THEN reCAPTCHA must still enqueue, since ACDC's availability is independent
172+
* of the smart-button location setting.
173+
*/
174+
public function test_enqueue_scripts_runs_when_protected_gateway_available_despite_disabled_location(): void {
175+
when( 'is_product' )->justReturn( false );
176+
when( 'is_cart' )->justReturn( false );
177+
when( 'is_checkout' )->justReturn( true );
178+
when( 'has_block' )->justReturn( false );
179+
180+
$this->stub_available_gateways( array( CreditCardGateway::ID ) );
181+
182+
$settings_status = Mockery::mock( SettingsStatus::class );
183+
$settings_status->shouldReceive( 'is_smart_button_enabled_for_location' )
184+
->with( 'checkout' )
185+
->andReturn( false );
186+
187+
expect( 'apply_filters' )
188+
->once()
189+
->with( 'woocommerce_paypal_payments_recaptcha_should_enqueue', true, 'checkout' )
190+
->andReturn( true );
191+
expect( 'wp_enqueue_script' )->twice();
192+
expect( 'wp_localize_script' )->once();
193+
194+
$this->make_testee( $settings_status, array( CreditCardGateway::ID ) )->enqueue_scripts();
195+
}
196+
197+
/**
198+
* GIVEN the smart-button location is disabled for checkout
199+
* WHEN no reCAPTCHA-protected gateway is available there either (e.g. only BACS)
200+
* THEN reCAPTCHA must not enqueue.
201+
*/
202+
public function test_enqueue_scripts_skips_at_checkout_when_no_protected_gateway_available(): void {
203+
when( 'is_product' )->justReturn( false );
204+
when( 'is_cart' )->justReturn( false );
205+
when( 'is_checkout' )->justReturn( true );
206+
when( 'has_block' )->justReturn( false );
207+
208+
$this->stub_available_gateways( array( 'bacs' ) );
209+
210+
$settings_status = Mockery::mock( SettingsStatus::class );
211+
$settings_status->shouldReceive( 'is_smart_button_enabled_for_location' )
212+
->with( 'checkout' )
213+
->andReturn( false );
214+
215+
expect( 'apply_filters' )
216+
->once()
217+
->with( 'woocommerce_paypal_payments_recaptcha_should_enqueue', false, 'checkout' )
218+
->andReturn( false );
219+
expect( 'wp_enqueue_script' )->never();
220+
221+
$this->make_testee( $settings_status, array( CreditCardGateway::ID ) )->enqueue_scripts();
222+
}
223+
224+
/**
225+
* GIVEN the current page is the My Account "Add payment method" page (no smart-button
226+
* location applies there at all)
227+
* WHEN a reCAPTCHA-protected gateway is available there (e.g. AXO rendering can_render_dcc())
228+
* THEN reCAPTCHA must still enqueue.
229+
*/
230+
public function test_enqueue_scripts_runs_on_add_payment_method_page_when_protected_gateway_available(): void {
231+
when( 'is_product' )->justReturn( false );
232+
when( 'is_cart' )->justReturn( false );
233+
when( 'is_checkout' )->justReturn( false );
234+
when( 'is_add_payment_method_page' )->justReturn( true );
235+
236+
$this->stub_available_gateways( array( 'ppcp-axo-gateway' ) );
237+
238+
$settings_status = Mockery::mock( SettingsStatus::class );
239+
$settings_status->shouldNotReceive( 'is_smart_button_enabled_for_location' );
240+
241+
expect( 'apply_filters' )
242+
->once()
243+
->with( 'woocommerce_paypal_payments_recaptcha_should_enqueue', true, null )
244+
->andReturn( true );
245+
expect( 'wp_enqueue_script' )->twice();
246+
expect( 'wp_localize_script' )->once();
247+
248+
$this->make_testee( $settings_status, array( 'ppcp-axo-gateway' ) )->enqueue_scripts();
249+
}
250+
166251
public function test_enqueue_scripts_checks_classic_checkout_location(): void {
167252
when( 'is_product' )->justReturn( false );
168253
when( 'is_cart' )->justReturn( false );

0 commit comments

Comments
 (0)