|
| 1 | +<?php |
| 2 | +declare( strict_types=1 ); |
| 3 | + |
| 4 | +namespace WooCommerce\PayPalCommerce\FraudProtection\Recaptcha; |
| 5 | + |
| 6 | +use Mockery; |
| 7 | +use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; |
| 8 | +use Psr\Log\LoggerInterface; |
| 9 | +use WooCommerce\PayPalCommerce\Assets\AssetGetter; |
| 10 | +use WooCommerce\PayPalCommerce\FraudProtection\PersistentCounter; |
| 11 | +use WooCommerce\PayPalCommerce\TestCase; |
| 12 | +use WooCommerce\PayPalCommerce\WcGateway\Helper\SettingsStatus; |
| 13 | +use function Brain\Monkey\Functions\expect; |
| 14 | +use function Brain\Monkey\Functions\when; |
| 15 | + |
| 16 | +class RecaptchaTest extends TestCase { |
| 17 | + use MockeryPHPUnitIntegration; |
| 18 | + |
| 19 | + private $recaptcha_options = array( |
| 20 | + 'guest_only' => '', |
| 21 | + 'site_key_v3' => 'v3-site-key', |
| 22 | + 'secret_key_v3' => 'v3-secret-key', |
| 23 | + 'site_key_v2' => 'v2-site-key', |
| 24 | + 'secret_key_v2' => 'v2-secret-key', |
| 25 | + 'v2_theme' => 'light', |
| 26 | + ); |
| 27 | + |
| 28 | + public function setUp(): void { |
| 29 | + parent::setUp(); |
| 30 | + |
| 31 | + when( 'is_user_logged_in' )->justReturn( false ); |
| 32 | + when( 'has_block' )->justReturn( false ); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Builds a Recaptcha instance configured so should_use_recaptcha() is true |
| 37 | + * (valid v2/v3 keys, integration enabled), so tests only need to control |
| 38 | + * the location-gating logic under test. |
| 39 | + */ |
| 40 | + private function make_testee( SettingsStatus $settings_status ): Recaptcha { |
| 41 | + $integration = Mockery::mock( RecaptchaIntegration::class ); |
| 42 | + $integration->enabled = 'yes'; |
| 43 | + $integration->shouldReceive( 'get_option' )->andReturnUsing( |
| 44 | + function ( string $key, $default = false ) { |
| 45 | + return $this->recaptcha_options[ $key ] ?? $default; |
| 46 | + } |
| 47 | + ); |
| 48 | + |
| 49 | + $asset_getter = Mockery::mock( AssetGetter::class ); |
| 50 | + $asset_getter->shouldReceive( 'get_asset_url' )->andReturn( 'https://example.com/recaptcha-handler.js' ); |
| 51 | + |
| 52 | + $logger = Mockery::mock( LoggerInterface::class ); |
| 53 | + $rejection_counter = Mockery::mock( PersistentCounter::class ); |
| 54 | + |
| 55 | + return new Recaptcha( |
| 56 | + $integration, |
| 57 | + array(), |
| 58 | + $asset_getter, |
| 59 | + '1.0.0', |
| 60 | + $logger, |
| 61 | + $rejection_counter, |
| 62 | + $settings_status |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + public function test_enqueue_scripts_skips_on_unsupported_page(): void { |
| 67 | + when( 'is_product' )->justReturn( false ); |
| 68 | + when( 'is_cart' )->justReturn( false ); |
| 69 | + when( 'is_checkout' )->justReturn( false ); |
| 70 | + |
| 71 | + $settings_status = Mockery::mock( SettingsStatus::class ); |
| 72 | + $settings_status->shouldNotReceive( 'is_smart_button_enabled_for_location' ); |
| 73 | + |
| 74 | + expect( 'apply_filters' ) |
| 75 | + ->once() |
| 76 | + ->with( 'woocommerce_paypal_payments_recaptcha_should_enqueue', false, null ) |
| 77 | + ->andReturn( false ); |
| 78 | + expect( 'wp_enqueue_script' )->never(); |
| 79 | + |
| 80 | + $this->make_testee( $settings_status )->enqueue_scripts(); |
| 81 | + } |
| 82 | + |
| 83 | + public function test_enqueue_scripts_skips_when_location_disabled(): void { |
| 84 | + when( 'is_product' )->justReturn( true ); |
| 85 | + when( 'is_cart' )->justReturn( false ); |
| 86 | + when( 'is_checkout' )->justReturn( false ); |
| 87 | + |
| 88 | + $settings_status = Mockery::mock( SettingsStatus::class ); |
| 89 | + $settings_status->shouldReceive( 'is_smart_button_enabled_for_location' ) |
| 90 | + ->with( 'product' ) |
| 91 | + ->andReturn( false ); |
| 92 | + |
| 93 | + expect( 'apply_filters' ) |
| 94 | + ->once() |
| 95 | + ->with( 'woocommerce_paypal_payments_recaptcha_should_enqueue', false, 'product' ) |
| 96 | + ->andReturn( false ); |
| 97 | + expect( 'wp_enqueue_script' )->never(); |
| 98 | + |
| 99 | + $this->make_testee( $settings_status )->enqueue_scripts(); |
| 100 | + } |
| 101 | + |
| 102 | + public function test_enqueue_scripts_runs_when_location_enabled(): void { |
| 103 | + when( 'is_product' )->justReturn( true ); |
| 104 | + when( 'is_cart' )->justReturn( false ); |
| 105 | + when( 'is_checkout' )->justReturn( false ); |
| 106 | + |
| 107 | + $settings_status = Mockery::mock( SettingsStatus::class ); |
| 108 | + $settings_status->shouldReceive( 'is_smart_button_enabled_for_location' ) |
| 109 | + ->with( 'product' ) |
| 110 | + ->andReturn( true ); |
| 111 | + |
| 112 | + expect( 'apply_filters' ) |
| 113 | + ->once() |
| 114 | + ->with( 'woocommerce_paypal_payments_recaptcha_should_enqueue', true, 'product' ) |
| 115 | + ->andReturn( true ); |
| 116 | + expect( 'wp_enqueue_script' )->twice(); |
| 117 | + expect( 'wp_localize_script' )->once(); |
| 118 | + |
| 119 | + $this->make_testee( $settings_status )->enqueue_scripts(); |
| 120 | + } |
| 121 | + |
| 122 | + public function test_enqueue_scripts_checks_block_express_location_on_block_checkout(): void { |
| 123 | + when( 'is_product' )->justReturn( false ); |
| 124 | + when( 'is_cart' )->justReturn( false ); |
| 125 | + when( 'is_checkout' )->justReturn( true ); |
| 126 | + when( 'has_block' )->justReturn( true ); |
| 127 | + |
| 128 | + $settings_status = Mockery::mock( SettingsStatus::class ); |
| 129 | + $settings_status->shouldReceive( 'is_smart_button_enabled_for_location' ) |
| 130 | + ->with( 'checkout-block-express' ) |
| 131 | + ->andReturn( false ); |
| 132 | + |
| 133 | + expect( 'apply_filters' ) |
| 134 | + ->once() |
| 135 | + ->with( 'woocommerce_paypal_payments_recaptcha_should_enqueue', false, 'checkout-block-express' ) |
| 136 | + ->andReturn( false ); |
| 137 | + expect( 'wp_enqueue_script' )->never(); |
| 138 | + |
| 139 | + $this->make_testee( $settings_status )->enqueue_scripts(); |
| 140 | + } |
| 141 | + |
| 142 | + public function test_enqueue_scripts_checks_classic_checkout_location(): void { |
| 143 | + when( 'is_product' )->justReturn( false ); |
| 144 | + when( 'is_cart' )->justReturn( false ); |
| 145 | + when( 'is_checkout' )->justReturn( true ); |
| 146 | + when( 'has_block' )->justReturn( false ); |
| 147 | + |
| 148 | + $settings_status = Mockery::mock( SettingsStatus::class ); |
| 149 | + $settings_status->shouldReceive( 'is_smart_button_enabled_for_location' ) |
| 150 | + ->with( 'checkout' ) |
| 151 | + ->andReturn( true ); |
| 152 | + |
| 153 | + expect( 'apply_filters' ) |
| 154 | + ->once() |
| 155 | + ->with( 'woocommerce_paypal_payments_recaptcha_should_enqueue', true, 'checkout' ) |
| 156 | + ->andReturn( true ); |
| 157 | + expect( 'wp_enqueue_script' )->twice(); |
| 158 | + expect( 'wp_localize_script' )->once(); |
| 159 | + |
| 160 | + $this->make_testee( $settings_status )->enqueue_scripts(); |
| 161 | + } |
| 162 | + |
| 163 | + public function test_filter_can_force_enqueue_on_a_disabled_location(): void { |
| 164 | + when( 'is_product' )->justReturn( true ); |
| 165 | + when( 'is_cart' )->justReturn( false ); |
| 166 | + when( 'is_checkout' )->justReturn( false ); |
| 167 | + |
| 168 | + $settings_status = Mockery::mock( SettingsStatus::class ); |
| 169 | + $settings_status->shouldReceive( 'is_smart_button_enabled_for_location' ) |
| 170 | + ->with( 'product' ) |
| 171 | + ->andReturn( false ); |
| 172 | + |
| 173 | + expect( 'apply_filters' ) |
| 174 | + ->once() |
| 175 | + ->with( 'woocommerce_paypal_payments_recaptcha_should_enqueue', false, 'product' ) |
| 176 | + ->andReturn( true ); |
| 177 | + expect( 'wp_enqueue_script' )->twice(); |
| 178 | + expect( 'wp_localize_script' )->once(); |
| 179 | + |
| 180 | + $this->make_testee( $settings_status )->enqueue_scripts(); |
| 181 | + } |
| 182 | + |
| 183 | + public function test_render_v2_container_returns_empty_when_location_disabled(): void { |
| 184 | + when( 'is_product' )->justReturn( true ); |
| 185 | + when( 'is_cart' )->justReturn( false ); |
| 186 | + when( 'is_checkout' )->justReturn( false ); |
| 187 | + |
| 188 | + $settings_status = Mockery::mock( SettingsStatus::class ); |
| 189 | + $settings_status->shouldReceive( 'is_smart_button_enabled_for_location' ) |
| 190 | + ->with( 'product' ) |
| 191 | + ->andReturn( false ); |
| 192 | + |
| 193 | + expect( 'apply_filters' ) |
| 194 | + ->once() |
| 195 | + ->with( 'woocommerce_paypal_payments_recaptcha_should_enqueue', false, 'product' ) |
| 196 | + ->andReturn( false ); |
| 197 | + |
| 198 | + $this->assertSame( '', $this->make_testee( $settings_status )->render_v2_container() ); |
| 199 | + } |
| 200 | + |
| 201 | + public function test_render_v2_container_returns_markup_when_location_enabled(): void { |
| 202 | + when( 'is_product' )->justReturn( true ); |
| 203 | + when( 'is_cart' )->justReturn( false ); |
| 204 | + when( 'is_checkout' )->justReturn( false ); |
| 205 | + |
| 206 | + $settings_status = Mockery::mock( SettingsStatus::class ); |
| 207 | + $settings_status->shouldReceive( 'is_smart_button_enabled_for_location' ) |
| 208 | + ->with( 'product' ) |
| 209 | + ->andReturn( true ); |
| 210 | + |
| 211 | + expect( 'apply_filters' ) |
| 212 | + ->once() |
| 213 | + ->with( 'woocommerce_paypal_payments_recaptcha_should_enqueue', true, 'product' ) |
| 214 | + ->andReturn( true ); |
| 215 | + |
| 216 | + $this->assertStringContainsString( |
| 217 | + 'ppcp-recaptcha-v2-container', |
| 218 | + $this->make_testee( $settings_status )->render_v2_container() |
| 219 | + ); |
| 220 | + } |
| 221 | +} |
0 commit comments