mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2026-07-29 02:07:27 +08:00
128 lines
4.6 KiB
PHP
128 lines
4.6 KiB
PHP
<?php
|
|
declare( strict_types = 1 );
|
|
|
|
namespace WooCommerce\PayPalCommerce\StoreSync\CartValidation;
|
|
|
|
use Mockery;
|
|
use WooCommerce\PayPalCommerce\StoreSync\Config\StoreCurrencyValue;
|
|
use WooCommerce\PayPalCommerce\StoreSync\Helper\ProductManager;
|
|
use WooCommerce\PayPalCommerce\StoreSync\Validation\StoreValidation;
|
|
use WooCommerce\PayPalCommerce\StoreSync\Validation\ValidationIssue;
|
|
use WooCommerce\PayPalCommerce\StoreSync\CartValidation\CouponValidator\CouponValidator;
|
|
use WooCommerce\PayPalCommerce\StoreSync\CartValidation\CouponValidator\CouponContextBuilder;
|
|
use WooCommerce\PayPalCommerce\StoreSync\CartValidation\CouponValidator\DiscountCalculator;
|
|
use WooCommerce\PayPalCommerce\StoreSync\CartValidation\CouponValidator\CouponResolutionBuilder;
|
|
|
|
/**
|
|
* @covers \WooCommerce\PayPalCommerce\StoreSync\CartValidation\CouponValidator\CouponValidator
|
|
*/
|
|
class CouponValidatorTest extends ValidationTest {
|
|
|
|
private CouponValidator $validator;
|
|
private $product_manager;
|
|
private $context_builder;
|
|
private $discount_calculator;
|
|
private $resolution_builder;
|
|
|
|
public function setUp(): void {
|
|
parent::setUp();
|
|
\Brain\Monkey\Functions\when( 'get_woocommerce_currency' )->justReturn( 'USD' );
|
|
// Note: wc_coupons_enabled() is stubbed per-test as needed
|
|
$this->product_manager = Mockery::mock( ProductManager::class );
|
|
$this->discount_calculator = new DiscountCalculator( $this->product_manager );
|
|
$store_currency = Mockery::mock( StoreCurrencyValue::class );
|
|
$store_currency->allows( 'value' )->andReturn( 'USD' );
|
|
$this->context_builder =
|
|
new CouponContextBuilder( $this->product_manager, $this->discount_calculator, $store_currency );
|
|
$this->resolution_builder = new CouponResolutionBuilder();
|
|
$this->validator = new CouponValidator(
|
|
$this->context_builder,
|
|
$this->discount_calculator,
|
|
$this->resolution_builder
|
|
);
|
|
}
|
|
|
|
public function test_validate_returns_null_for_cart_without_coupons(): void {
|
|
$cart = $this->create_cart_with_coupons( array() );
|
|
|
|
$result = $this->validator->validate( $this->wrap_in_store_cart( $cart ) );
|
|
|
|
$this->assertNull( $result );
|
|
}
|
|
|
|
public function test_validate_skips_remove_action_coupons(): void {
|
|
$cart = $this->create_cart_with_coupons(
|
|
array(
|
|
array( 'code' => 'REMOVE_ME', 'action' => 'REMOVE' ),
|
|
)
|
|
);
|
|
|
|
$result = $this->validator->validate( $this->wrap_in_store_cart( $cart ) );
|
|
|
|
// REMOVE actions are skipped entirely - no WC_Coupon instantiation needed.
|
|
$this->assertNull( $result );
|
|
}
|
|
|
|
public function test_validate_returns_error_when_coupons_disabled(): void {
|
|
// This test requires actual WooCommerce classes, so skip in unit test environment.
|
|
// TODO: This test does not run in a unit-test environment. How is it executed?
|
|
if ( ! class_exists( 'WC_Coupon' ) || ! class_exists( 'WC_Discounts' ) ) {
|
|
$this->markTestSkipped( 'WooCommerce classes not available in unit test environment' );
|
|
}
|
|
|
|
// Stub wc_coupons_enabled to return false for this test.
|
|
\Brain\Monkey\Functions\when( 'wc_coupons_enabled' )->justReturn( false );
|
|
|
|
$cart = $this->create_cart_with_coupons(
|
|
array(
|
|
array( 'code' => 'TESTCODE', 'action' => 'APPLY' ),
|
|
)
|
|
);
|
|
|
|
$result = $this->validator->validate( $this->wrap_in_store_cart( $cart ) );
|
|
|
|
$this->assertIsArray( $result );
|
|
$this->assertCount( 1, $result );
|
|
|
|
$data = $result[0]->to_array();
|
|
$this->assertValidationIssue( $data, 'PRICING_ERROR', 'BUSINESS_RULE', 'coupons', 'Coupons are not enabled' );
|
|
}
|
|
|
|
public function test_validate_filters_only_apply_actions(): void {
|
|
$cart = $this->create_cart_with_coupons(
|
|
array(
|
|
array( 'code' => 'KEEP_THIS', 'action' => 'REMOVE' ),
|
|
array( 'code' => 'ALSO_REMOVE', 'action' => 'REMOVE' ),
|
|
)
|
|
);
|
|
|
|
$result = $this->validator->validate( $this->wrap_in_store_cart( $cart ) );
|
|
|
|
// All coupons are REMOVE actions, so nothing to validate.
|
|
$this->assertNull( $result );
|
|
}
|
|
|
|
public function test_coupon_invalid_issue_has_correct_error_code(): void {
|
|
$issue = ValidationIssue::create_coupon_invalid( 'Test message' )
|
|
->user_message( 'Test user message' )
|
|
->for_field( 'coupons[0]' );
|
|
|
|
$data = $issue->to_array();
|
|
$this->assertValidationIssue( $data, 'PRICING_ERROR', 'BUSINESS_RULE' );
|
|
}
|
|
|
|
public function test_coupon_invalid_truncates_long_messages(): void {
|
|
$long_message = str_repeat( 'a', 300 );
|
|
$long_user_message = str_repeat( 'b', 600 );
|
|
|
|
$issue = ValidationIssue::create_coupon_invalid( $long_message )
|
|
->user_message( $long_user_message );
|
|
|
|
$data = $issue->to_array();
|
|
$this->assertValidationIssue( $data, 'PRICING_ERROR', 'BUSINESS_RULE' );
|
|
|
|
$this->assertSame( 255, strlen( $data['message'] ) );
|
|
$this->assertSame( 500, strlen( $data['user_message'] ) );
|
|
}
|
|
|
|
}
|