From 09e516daa2dba78cc97b9c5c1f4c445a5fe4e3d3 Mon Sep 17 00:00:00 2001 From: Claire Date: Thu, 25 Apr 2024 11:47:18 +0200 Subject: [PATCH 01/68] fix cs --- src/includes/AlmaSettings.php | 5 +- src/includes/Blocks/AlmaBlock.php | 5 +- src/includes/Gateways/AlmaPaymentGateway.php | 4 +- src/includes/Handlers/CartHandler.php | 5 +- src/includes/Helpers/CartHelper.php | 67 ++++++++-- src/includes/Helpers/PaymentHelper.php | 4 +- src/includes/Helpers/SessionHelper.php | 32 +++++ src/includes/Helpers/VersionHelper.php | 31 +++++ .../templates/alma-checkout-plan-details.php | 4 +- src/tests/Helpers/CartHelperTest.php | 124 ++++++++++++++++++ src/tests/{ => Helpers}/ToolsHelperTest.php | 7 +- 11 files changed, 264 insertions(+), 24 deletions(-) create mode 100644 src/includes/Helpers/SessionHelper.php create mode 100644 src/includes/Helpers/VersionHelper.php create mode 100644 src/tests/Helpers/CartHelperTest.php rename src/tests/{ => Helpers}/ToolsHelperTest.php (97%) diff --git a/src/includes/AlmaSettings.php b/src/includes/AlmaSettings.php index d6012def..b152e515 100644 --- a/src/includes/AlmaSettings.php +++ b/src/includes/AlmaSettings.php @@ -42,8 +42,11 @@ use Alma\Woocommerce\Helpers\GeneralHelper; use Alma\Woocommerce\Helpers\InternationalizationHelper; use Alma\Woocommerce\Helpers\PaymentHelper; +use Alma\Woocommerce\Helpers\SessionHelper; use Alma\Woocommerce\Helpers\SettingsHelper as AlmaHelperSettings; use Alma\Woocommerce\Helpers\PlanBuilderHelper; +use Alma\Woocommerce\Helpers\ToolsHelper; +use Alma\Woocommerce\Helpers\VersionHelper; /** * Handles settings retrieval from the settings API. @@ -145,7 +148,7 @@ public function __construct() { $this->logger = new AlmaLogger(); $this->encryptor_helper = new EncryptorHelper(); $this->fee_plan_helper = new FeePlanHelper(); - $this->cart_helper = new CartHelper(); + $this->cart_helper = new CartHelper( new ToolsHelper(), new SessionHelper(), new VersionHelper() ); $this->load_settings(); } diff --git a/src/includes/Blocks/AlmaBlock.php b/src/includes/Blocks/AlmaBlock.php index f93140e2..0b0aff4c 100644 --- a/src/includes/Blocks/AlmaBlock.php +++ b/src/includes/Blocks/AlmaBlock.php @@ -15,6 +15,9 @@ use Alma\Woocommerce\Helpers\CartHelper; use Alma\Woocommerce\Helpers\CheckoutHelper; use Alma\Woocommerce\Helpers\GatewayHelper; +use Alma\Woocommerce\Helpers\SessionHelper; +use Alma\Woocommerce\Helpers\ToolsHelper; +use Alma\Woocommerce\Helpers\VersionHelper; use Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType; use Alma\Woocommerce\Helpers\AssetsHelper; use Alma\Woocommerce\Helpers\ConstantsHelper; @@ -74,7 +77,7 @@ public function initialize() { $this->gateway_helper = new GatewayHelper(); $this->alma_settings = new AlmaSettings(); $this->checkout_helper = new CheckoutHelper(); - $this->cart_helper = new CartHelper(); + $this->cart_helper = new CartHelper( new ToolsHelper(), new SessionHelper(), new VersionHelper() ); $this->alma_plan_builder = new PlanBuilderHelper(); } diff --git a/src/includes/Gateways/AlmaPaymentGateway.php b/src/includes/Gateways/AlmaPaymentGateway.php index d8dde15d..272bd539 100644 --- a/src/includes/Gateways/AlmaPaymentGateway.php +++ b/src/includes/Gateways/AlmaPaymentGateway.php @@ -31,12 +31,14 @@ use Alma\Woocommerce\Helpers\OrderHelper; use Alma\Woocommerce\Helpers\PaymentHelper; use Alma\Woocommerce\Helpers\PluginHelper; +use Alma\Woocommerce\Helpers\SessionHelper; use Alma\Woocommerce\Helpers\SettingsHelper; use Alma\Woocommerce\Helpers\CartHelper; use Alma\Woocommerce\Helpers\ToolsHelper; use Alma\Woocommerce\Helpers\PlanBuilderHelper; use Alma\Woocommerce\Helpers\TemplateLoaderHelper; use Alma\Woocommerce\AlmaSettings; +use Alma\Woocommerce\Helpers\VersionHelper; /** * AlmaPaymentGateway @@ -185,7 +187,7 @@ public function __construct( $check_basics = true ) { $this->template_loader = new TemplateLoaderHelper(); $this->soc_helper = new ShareOfCheckoutHelper(); $this->plugin_helper = new PluginHelper(); - $this->cart_helper = new CartHelper(); + $this->cart_helper = new CartHelper( $this->tool_helper, new SessionHelper(), new VersionHelper() ); $this->id = $this->get_gateway_id(); $this->method_title = __( 'Payment in instalments and deferred with Alma - 2x 3x 4x', 'alma-gateway-for-woocommerce' ); $this->method_description = __( 'Install Alma and boost your sales! It\'s simple and guaranteed, your cash flow is secured. 0 commitment, 0 subscription, 0 risk.', 'alma-gateway-for-woocommerce' ); diff --git a/src/includes/Handlers/CartHandler.php b/src/includes/Handlers/CartHandler.php index a97cc338..73f25c56 100644 --- a/src/includes/Handlers/CartHandler.php +++ b/src/includes/Handlers/CartHandler.php @@ -15,6 +15,9 @@ use Alma\Woocommerce\Helpers\CartHelper; use Alma\Woocommerce\Helpers\ConstantsHelper; +use Alma\Woocommerce\Helpers\SessionHelper; +use Alma\Woocommerce\Helpers\ToolsHelper; +use Alma\Woocommerce\Helpers\VersionHelper; /** * CartHandler @@ -57,7 +60,7 @@ public function display_cart_eligibility() { } } - $cart_helper = new CartHelper(); + $cart_helper = new CartHelper( new ToolsHelper(), new SessionHelper(), new VersionHelper() ); $amount = $cart_helper->get_total_in_cents(); $this->inject_payment_plan_widget( $has_excluded_products, $amount, ConstantsHelper::JQUERY_CART_UPDATE_EVENT ); diff --git a/src/includes/Helpers/CartHelper.php b/src/includes/Helpers/CartHelper.php index 5bdd5869..f71ddf69 100644 --- a/src/includes/Helpers/CartHelper.php +++ b/src/includes/Helpers/CartHelper.php @@ -25,15 +25,36 @@ class CartHelper { * * @var ToolsHelper */ - protected $tool_helper; + protected $tools_helper; /** - * __construct + * Helper Session. * - * @return void + * @var SessionHelper */ - public function __construct() { - $this->tool_helper = new ToolsHelper(); + protected $session_helper; + + + /** + * Helper Version. + * + * @var VersionHelper + */ + protected $version_helper; + + + /** + * Constructor. + * + * @codeCoverageIgnore + * @param ToolsHelper $tools_helper The tool Helper. + * @param SessionHelper $session_helper The session Helper. + * @param VersionHelper $version_helper The version Helper. + */ + public function __construct( $tools_helper, $session_helper, $version_helper ) { + $this->tools_helper = $tools_helper; + $this->session_helper = $session_helper; + $this->version_helper = $version_helper; } /** @@ -44,7 +65,7 @@ public function __construct() { * @see get_total_from_cart */ public function get_total_in_cents() { - return $this->tool_helper->alma_price_to_cents( $this->get_total_from_cart() ); + return $this->tools_helper->alma_price_to_cents( $this->get_total_from_cart() ); } /** @@ -52,24 +73,42 @@ public function get_total_in_cents() { * * @return float */ - protected function get_total_from_cart() { - if ( ! wc()->cart ) { + public function get_total_from_cart() { + $cart = $this->get_cart(); + + if ( ! $cart ) { return 0; } - if ( version_compare( WC()->version, '3.2.0', '<' ) ) { - return wc()->cart->total; + if ( version_compare( $this->version_helper->get_version(), '3.2.0', '<' ) ) { + return $cart->total; } - $total = wc()->cart->get_total( null ); + $total = $cart->get_total( null ); + + $session = $this->session_helper->get_session(); + $session_total = $session->get( 'cart_totals', null ); if ( - 0 === $total - && ! empty( WC()->session->get( 'cart_totals', null )['total'] ) + ( + 0 === $total + || '0' === $total + ) + && ! empty( $session_total['total'] ) ) { - $total = WC()->session->get( 'cart_totals', null )['total']; + $total = $session_total['total']; } return $total; } + + /** + * Get Wc cart + * + * @codeCoverageIgnore + * @return \WC_Cart|null + */ + public function get_cart() { + return wc()->cart; + } } diff --git a/src/includes/Helpers/PaymentHelper.php b/src/includes/Helpers/PaymentHelper.php index 24bf6207..6bee8856 100644 --- a/src/includes/Helpers/PaymentHelper.php +++ b/src/includes/Helpers/PaymentHelper.php @@ -89,7 +89,7 @@ public function __construct() { $this->payment_upon_trigger = new PaymentUponTriggerService(); $this->alma_settings = new AlmaSettings(); $this->tool_helper = new ToolsHelper(); - $this->cart_helper = new CartHelper(); + $this->cart_helper = new CartHelper( $this->tool_helper, new SessionHelper(), new VersionHelper() ); $this->order_helper = new OrderHelper(); } @@ -347,7 +347,7 @@ public function get_description( $payment_method ) { * @return array Payload to request eligibility v2 endpoint. */ public static function get_eligibility_payload_from_cart() { - $cart_helper = new CartHelper(); + $cart_helper = new CartHelper( new ToolsHelper(), new SessionHelper(), new VersionHelper() ); $customer_helper = new CustomerHelper(); $settings = new AlmaSettings(); diff --git a/src/includes/Helpers/SessionHelper.php b/src/includes/Helpers/SessionHelper.php new file mode 100644 index 00000000..f1bf5ed9 --- /dev/null +++ b/src/includes/Helpers/SessionHelper.php @@ -0,0 +1,32 @@ +session; + } +} diff --git a/src/includes/Helpers/VersionHelper.php b/src/includes/Helpers/VersionHelper.php new file mode 100644 index 00000000..8d37656f --- /dev/null +++ b/src/includes/Helpers/VersionHelper.php @@ -0,0 +1,31 @@ +version; + } +} diff --git a/src/public/templates/alma-checkout-plan-details.php b/src/public/templates/alma-checkout-plan-details.php index 157acf80..4a267459 100644 --- a/src/public/templates/alma-checkout-plan-details.php +++ b/src/public/templates/alma-checkout-plan-details.php @@ -11,6 +11,8 @@ use Alma\Woocommerce\Helpers\ConstantsHelper; use Alma\Woocommerce\Helpers\ToolsHelper; use Alma\Woocommerce\Helpers\CartHelper; +use Alma\Woocommerce\Helpers\SessionHelper; +use Alma\Woocommerce\Helpers\VersionHelper; ?> @@ -99,7 +101,7 @@ class="getInstallmentsCount() > 4 ) { - $alma_cart_helper = new CartHelper(); + $alma_cart_helper = new CartHelper( new ToolsHelper(), new SessionHelper(), new VersionHelper() ); ?>

%', $bps ); - - return '' . $formatted_bps . ''; + public function alma_format_percent_from_bps( $bps ) { + $bps = number_format( + $this->alma_price_from_cents( $bps ), + $this->price_helper->get_woo_decimals(), + $this->price_helper->get_woo_decimal_separator(), + $this->price_helper->get_woo_thousand_separator() + ); + + $formatted_bps = sprintf( + $this->price_helper->get_woo_format(), + '%', + $bps + ); + + return sprintf( '%s', $formatted_bps ); } /** @@ -88,7 +116,7 @@ public static function alma_format_percent_from_bps( $bps ) { * * @return float */ - public static function alma_price_from_cents( $price ) { + public function alma_price_from_cents( $price ) { return (float) ( $price / 100 ); } @@ -102,8 +130,8 @@ public static function alma_price_from_cents( $price ) { * * @see wc_price() */ - public static function alma_format_price_from_cents( $price, $args = array() ) { - return wc_price( self::alma_price_from_cents( $price ), array_merge( array( 'currency' => 'EUR' ), $args ) ); + public function alma_format_price_from_cents( $price, $args = array() ) { + return wc_price( $this->alma_price_from_cents( $price ), array_merge( array( 'currency' => 'EUR' ), $args ) ); } /** @@ -149,7 +177,8 @@ public static function action_for_webhook( $webhook ) { * @return bool */ public function check_currency() { - $currency = get_woocommerce_currency(); + $currency = $this->currency_helper->get_currency(); + if ( 'EUR' !== $currency ) { $this->logger->warning( 'Currency not supported - Not displaying by Alma.', diff --git a/src/public/templates/alma-checkout-plan-details.php b/src/public/templates/alma-checkout-plan-details.php index 4a267459..2fc835f6 100644 --- a/src/public/templates/alma-checkout-plan-details.php +++ b/src/public/templates/alma-checkout-plan-details.php @@ -13,7 +13,16 @@ use Alma\Woocommerce\Helpers\CartHelper; use Alma\Woocommerce\Helpers\SessionHelper; use Alma\Woocommerce\Helpers\VersionHelper; +use Alma\Woocommerce\Helpers\PriceHelper; +use Alma\Woocommerce\AlmaLogger; +use Alma\Woocommerce\Helpers\CurrencyHelper; +/** + * The tools helper + * + * @var ToolsHelper $tools_helper + */ +$tools_helper = new ToolsHelper( new AlmaLogger(), new PriceHelper(), new CurrencyHelper() ); // phpcs:ignore ?>

@@ -68,8 +77,8 @@ class=" today_amount (0), %2$s => total_amount, %3$s => i18n formatted due_date. __( '%1$s today then %2$s on %3$s', 'alma-gateway-for-woocommerce' ), - ToolsHelper::alma_format_price_from_cents( 0 ), - ToolsHelper::alma_format_price_from_cents( $alma_step['total_amount'] ), + $tools_helper->alma_format_price_from_cents( 0 ), + $tools_helper->alma_format_price_from_cents( $alma_step['total_amount'] ), date_i18n( get_option( 'date_format' ), $alma_step['due_date'] ) ) ); @@ -81,7 +90,7 @@ class="' . esc_html( date_i18n( get_option( 'date_format' ), $alma_step['due_date'] ) ) . ''; } - echo wp_kses_post( ToolsHelper::alma_format_price_from_cents( $alma_step['total_amount'] ) ); + echo wp_kses_post( $tools_helper->alma_format_price_from_cents( $alma_step['total_amount'] ) ); } ?>

@@ -93,7 +102,7 @@ class=" - + alma_format_price_from_cents( $alma_step['customer_fee'] ) ); ?>

getInstallmentsCount() > 4 ) { - $alma_cart_helper = new CartHelper( new ToolsHelper(), new SessionHelper(), new VersionHelper() ); + $alma_cart_helper = new CartHelper( $tools_helper, new SessionHelper(), new VersionHelper() ); ?>

- get_total_in_cents() ) ); ?> + alma_format_price_from_cents( $alma_cart_helper->get_total_in_cents() ) ); ?>

- customerTotalCostAmount ) ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName ?> + alma_format_price_from_cents( $alma_eligibility->customerTotalCostAmount ) ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName ?>

getAnnualInterestRate(); @@ -146,7 +155,7 @@ class=" - + tools_helper->alma_format_percent_from_bps( $alma_annual_interest_rate ) ); ?>

- getCustomerTotalCostAmount() + $alma_cart_helper->get_total_in_cents() ) ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName ?> + alma_format_price_from_cents( $alma_eligibility->getCustomerTotalCostAmount() + $alma_cart_helper->get_total_in_cents() ) ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName ?>

session_helper = new SessionHelper(); + $this->version_helper = new VersionHelper(); + $this->tools_helper = new ToolsHelper(new AlmaLogger(), new PriceHelper(), new CurrencyHelper()); + } /** * @covers \Alma\Woocommerce\Helpers\CartHelper::get_total_from_cart * @@ -27,7 +56,11 @@ class CartHelperTest extends WP_UnitTestCase { */ public function test_get_total_from_cart() { // Test Empty Cart - $cart_helper = \Mockery::mock(CartHelper::class, [new ToolsHelper(), new SessionHelper(), new VersionHelper()])->makePartial(); + $cart_helper = \Mockery::mock(CartHelper::class, [ + $this->tools_helper, + $this->session_helper, + $this->version_helper + ])->makePartial(); $cart_helper->shouldReceive('get_cart') ->andReturn(null); @@ -39,7 +72,7 @@ public function test_get_total_from_cart() { $version_helper->shouldReceive('get_version') ->andReturn('2.0.0'); - $cart_helper = \Mockery::mock(CartHelper::class, [new ToolsHelper(), new SessionHelper(), $version_helper])->makePartial(); + $cart_helper = \Mockery::mock(CartHelper::class, [$this->tools_helper, $this->session_helper, $version_helper])->makePartial(); $cart = new \stdClass(); $cart->total = '1.0000'; @@ -50,7 +83,7 @@ public function test_get_total_from_cart() { // Test Cart version >3.2.0 and cart total not null - $cart_helper = \Mockery::mock(CartHelper::class, [new ToolsHelper(), new SessionHelper(), new VersionHelper()])->makePartial(); + $cart_helper = \Mockery::mock(CartHelper::class, [$this->tools_helper, $this->session_helper, $this->version_helper])->makePartial(); $wc_cart = \Mockery::mock(\WC_Cart::class); $wc_cart->shouldReceive('get_total') @@ -71,7 +104,7 @@ public function test_get_total_from_cart() { $session_helper->shouldReceive('get_session') ->andReturn($session); - $cart_helper = \Mockery::mock(CartHelper::class, [new ToolsHelper(), $session_helper, new VersionHelper()])->makePartial(); + $cart_helper = \Mockery::mock(CartHelper::class, [$this->tools_helper, $session_helper, $this->version_helper])->makePartial(); $wc_cart = \Mockery::mock(\WC_Cart::class); @@ -92,7 +125,7 @@ public function test_get_total_from_cart() { $session_helper->shouldReceive('get_session') ->andReturn($session); - $cart_helper = \Mockery::mock(CartHelper::class, [new ToolsHelper(),$session_helper, new VersionHelper()])->makePartial(); + $cart_helper = \Mockery::mock(CartHelper::class, [$this->tools_helper,$session_helper, $this->version_helper])->makePartial(); $wc_cart = \Mockery::mock(\WC_Cart::class); $wc_cart->shouldReceive('get_total') @@ -112,7 +145,7 @@ public function test_get_total_from_cart() { * @return void */ public function test_get_total_in_cents() { - $cart_helper = \Mockery::mock(CartHelper::class, [new ToolsHelper(), new SessionHelper(), new VersionHelper()])->makePartial(); + $cart_helper = \Mockery::mock(CartHelper::class, [$this->tools_helper, $this->session_helper, $this->version_helper])->makePartial(); $cart_helper->shouldReceive('get_total_from_cart') ->andReturn('4.000'); diff --git a/src/tests/Helpers/ToolsHelperTest.php b/src/tests/Helpers/ToolsHelperTest.php index cf938c24..8e5d8b5e 100644 --- a/src/tests/Helpers/ToolsHelperTest.php +++ b/src/tests/Helpers/ToolsHelperTest.php @@ -9,8 +9,12 @@ namespace Alma\Woocommerce\Tests\Helpers; +use Alma\Woocommerce\AlmaLogger; use Alma\Woocommerce\Helpers\ConstantsHelper; +use Alma\Woocommerce\Helpers\CurrencyHelper; +use Alma\Woocommerce\Helpers\PriceHelper; use Alma\Woocommerce\Helpers\ToolsHelper; +use Mockery\Mock; use WP_UnitTestCase; /** @@ -24,7 +28,7 @@ class ToolsHelperTest extends WP_UnitTestCase { protected $tools_helper; public function setup() { - $this->tools_helper = new ToolsHelper(); + $this->tools_helper = new ToolsHelper(new AlmaLogger(), new PriceHelper(), new CurrencyHelper()); } /** @@ -34,10 +38,10 @@ public function setup() { */ public function test_is_amount_plan_key() { - $result = ToolsHelper::is_amount_plan_key( 'min_amount_general_15_1_0' ); + $result = $this->tools_helper->is_amount_plan_key( 'min_amount_general_15_1_0' ); $this->assertTrue( $result ); - $result = ToolsHelper::is_amount_plan_key( 'min_amount_pos_15_1_0' ); + $result = $this->tools_helper->is_amount_plan_key( 'min_amount_pos_15_1_0' ); $this->assertFalse( $result ); } @@ -49,7 +53,7 @@ public function test_is_amount_plan_key() { public function test_alma_price_to_cents() { $result = $this->tools_helper->alma_price_to_cents( '1.0999' ); - $this->assertEquals( $result, 110 ); + $this->assertEquals( 110, $result ); } /** @@ -58,36 +62,52 @@ public function test_alma_price_to_cents() { * @return void */ public function test_alma_format_percent_from_bps() { - - $result = ToolsHelper::alma_format_percent_from_bps( '10000' ); - $this->assertEquals( $result, '%100.00' ); + // BPS positive + $priceHelper = \Mockery::mock(PriceHelper::class); + $priceHelper->shouldReceive('get_woo_decimals')->andReturn('2'); + $priceHelper->shouldReceive('get_woo_decimal_separator')->andReturn(','); + $priceHelper->shouldReceive('get_woo_thousand_separator')->andReturn('.'); + $priceHelper->shouldReceive('get_woo_format')->andReturn('%2$s %1$s'); + + $toolsHelper = new ToolsHelper(new AlmaLogger(), $priceHelper, new CurrencyHelper()); + $result = $toolsHelper->alma_format_percent_from_bps( '100000' ); + $this->assertEquals( '1.000,00 %', $result ); + + // BPS negative + $priceHelper = \Mockery::mock(PriceHelper::class); + $priceHelper->shouldReceive('get_woo_decimals')->andReturn('3'); + $priceHelper->shouldReceive('get_woo_decimal_separator')->andReturn(' '); + $priceHelper->shouldReceive('get_woo_thousand_separator')->andReturn(' '); + $priceHelper->shouldReceive('get_woo_format')->andReturn('%2$s %1$s'); + + $toolsHelper = new ToolsHelper(new AlmaLogger(), $priceHelper, new CurrencyHelper()); + $result = $toolsHelper->alma_format_percent_from_bps( '-200000' ); + $this->assertEquals( '-2 000 000 %', $result ); } /** * - * + * @covers \Alma\Woocommerce\Helpers\ToolsHelper::alma_price_from_cents * @return void */ public function test_alma_price_from_cents() { - $result = ToolsHelper::alma_price_from_cents( 10000 ); - $this->assertEquals( $result, 100 ); + $result = $this->tools_helper->alma_price_from_cents( 10000 ); + $this->assertEquals( 100, $result ); } /** - * - * + * @covers \Alma\Woocommerce\Helpers\ToolsHelper::alma_format_price_from_cents * @return void */ public function test_alma_format_price_from_cents() { - $result = ToolsHelper::alma_format_price_from_cents( 10000 ); - $this->assertEquals( $result, '100.00' ); + $result = $this->tools_helper->alma_format_price_from_cents( 10000 ); + $this->assertEquals( '100.00', $result ); } /** - * - * + * @covers \Alma\Woocommerce\Helpers\ToolsHelper::alma_string_to_bool * @return void */ public function test_alma_string_to_bool() { @@ -119,24 +139,47 @@ public function test_alma_string_to_bool() { } /** - * - * + * @covers \Alma\Woocommerce\Helpers\ToolsHelper::url_for_webhook * @return void */ public function test_url_for_webhook() { $result = $this->tools_helper->url_for_webhook( ConstantsHelper::CUSTOMER_RETURN ); - $this->assertEquals( $result, 'http://example.org/?wc-api=alma_customer_return' ); + $this->assertEquals( 'http://example.org/?wc-api=alma_customer_return', $result ); } /** - * - * + * @covers \Alma\Woocommerce\Helpers\ToolsHelper::action_for_webhook * @return void */ public function test_action_for_webhook() { $result = ToolsHelper::action_for_webhook( ConstantsHelper::CUSTOMER_RETURN ); - $this->assertEquals( $result, 'woocommerce_api_alma_customer_return' ); + $this->assertEquals( 'woocommerce_api_alma_customer_return', $result ); } + /** + * @covers \Alma\Woocommerce\Helpers\ToolsHelper::check_currency + * @return void + */ + public function test_check_currency() { + // Test Euros + $currencyHelper = \Mockery::mock(CurrencyHelper::class); + $currencyHelper->shouldReceive('get_currency')->andReturn('EUR'); + $toolsHelper = new ToolsHelper(new AlmaLogger(), new PriceHelper(), $currencyHelper); + + $this->assertTrue($toolsHelper->check_currency()); + + // Test not Euros + $currencyHelper = \Mockery::mock(CurrencyHelper::class); + $currencyHelper->shouldReceive('get_currency')->andReturn('DOL'); + + $logger = \Mockery::mock(AlmaLogger::class); + $logger->shouldReceive('warning') + ->with('Currency not supported - Not displaying by Alma.', array('Currency' => 'DOL')); + + $toolsHelper = new ToolsHelper($logger, new PriceHelper(), $currencyHelper); + + $this->assertFalse($toolsHelper->check_currency()); + + } } From 66a628fc7ca9eb3e0dd8eed5ed4fea31c2b43ba9 Mon Sep 17 00:00:00 2001 From: Claire Date: Thu, 25 Apr 2024 17:15:40 +0200 Subject: [PATCH 05/68] start unit tests on settings helper --- .../Admin/Helpers/FormFieldsHelper.php | 34 +- src/includes/Admin/Helpers/FormHelper.php | 24 +- src/includes/Admin/Helpers/GeneralHelper.php | 21 +- src/includes/Admin/Helpers/RefundHelper.php | 10 +- .../Admin/Helpers/ShareOfCheckoutHelper.php | 6 +- src/includes/AlmaSettings.php | 55 +-- src/includes/Blocks/AlmaBlock.php | 22 +- .../CurrencyFactory.php} | 12 +- .../PriceFactory.php} | 12 +- .../SessionFactory.php} | 12 +- .../VersionFactory.php} | 12 +- src/includes/Gateways/AlmaPaymentGateway.php | 41 +- src/includes/Handlers/CartHandler.php | 16 +- src/includes/Handlers/GenericHandler.php | 6 +- src/includes/Helpers/CartHelper.php | 14 +- .../Helpers/InternationalizationHelper.php | 40 +- src/includes/Helpers/PaymentHelper.php | 20 +- src/includes/Helpers/SettingsHelper.php | 143 ++++--- src/includes/Helpers/ToolsHelper.php | 12 +- .../templates/alma-checkout-plan-details.php | 16 +- src/tests/Helpers/CartHelperTest.php | 24 +- src/tests/Helpers/SettingsHelperTest.php | 374 ++++++++++++++++++ src/tests/Helpers/ToolsHelperTest.php | 23 +- 23 files changed, 715 insertions(+), 234 deletions(-) rename src/includes/{Helpers/CurrencyHelper.php => Factories/CurrencyFactory.php} (60%) rename src/includes/{Helpers/PriceHelper.php => Factories/PriceFactory.php} (80%) rename src/includes/{Helpers/SessionHelper.php => Factories/SessionFactory.php} (62%) rename src/includes/{Helpers/VersionHelper.php => Factories/VersionFactory.php} (59%) create mode 100644 src/tests/Helpers/SettingsHelperTest.php diff --git a/src/includes/Admin/Helpers/FormFieldsHelper.php b/src/includes/Admin/Helpers/FormFieldsHelper.php index 250ee3ec..bb988f99 100644 --- a/src/includes/Admin/Helpers/FormFieldsHelper.php +++ b/src/includes/Admin/Helpers/FormFieldsHelper.php @@ -18,18 +18,18 @@ use Alma\API\Entities\FeePlan; use Alma\Woocommerce\Admin\Builders\FormHtmlBuilder; use Alma\Woocommerce\AlmaLogger; +use Alma\Woocommerce\AlmaSettings; +use Alma\Woocommerce\Factories\CurrencyFactory; +use Alma\Woocommerce\Factories\PriceFactory; use Alma\Woocommerce\Helpers\AssetsHelper; use Alma\Woocommerce\Helpers\BlockHelper; use Alma\Woocommerce\Helpers\ConstantsHelper; -use Alma\Woocommerce\Helpers\CurrencyHelper; use Alma\Woocommerce\Helpers\FeePlanHelper; use Alma\Woocommerce\Helpers\GeneralHelper; use Alma\Woocommerce\Helpers\InternationalizationHelper; -use Alma\Woocommerce\Helpers\PriceHelper; +use Alma\Woocommerce\Helpers\PluginHelper; use Alma\Woocommerce\Helpers\ToolsHelper; use Alma\Woocommerce\Services\PaymentUponTriggerService; -use Alma\Woocommerce\AlmaSettings; -use Alma\Woocommerce\Helpers\PluginHelper; /** @@ -80,16 +80,24 @@ class FormFieldsHelper { */ protected $tools_helper; + /** + * Internationalization Helper. + * + * @var InternationalizationHelper + */ + protected $internalionalization_helper; + /** * Constructor. */ public function __construct() { - $this->settings_helper = new AlmaSettings(); - $this->payment_upon_trigger = new PaymentUponTriggerService(); - $this->fee_plan_helper = new FeePlanHelper(); - $this->plugin_helper = new PluginHelper(); - $this->block_helper = new BlockHelper(); - $this->tools_helper = new ToolsHelper( new AlmaLogger(), new PriceHelper(), new CurrencyHelper() ); + $this->settings_helper = new AlmaSettings(); + $this->payment_upon_trigger = new PaymentUponTriggerService(); + $this->fee_plan_helper = new FeePlanHelper(); + $this->plugin_helper = new PluginHelper(); + $this->block_helper = new BlockHelper(); + $this->tools_helper = new ToolsHelper( new AlmaLogger(), new PriceFactory(), new CurrencyFactory() ); + $this->internalionalization_helper = new InternationalizationHelper(); } @@ -578,7 +586,7 @@ public function init_general_settings_fields( array $default_settings ) { ), ); - $field_cart_not_eligible_message_gift_cards = InternationalizationHelper::generate_i18n_field( + $field_cart_not_eligible_message_gift_cards = $this->internalionalization_helper->generate_i18n_field( 'cart_not_eligible_message_gift_cards', array( 'title' => __( 'Non-eligibility message for excluded products', 'alma-gateway-for-woocommerce' ), @@ -629,7 +637,7 @@ protected function get_custom_fields_payment_method( $payment_method_name, $titl ), ); - $field_payment_method_title = InternationalizationHelper::generate_i18n_field( + $field_payment_method_title = $this->internalionalization_helper->generate_i18n_field( 'title_' . $blocks . $payment_method_name, array( 'title' => __( 'Title', 'alma-gateway-for-woocommerce' ), @@ -639,7 +647,7 @@ protected function get_custom_fields_payment_method( $payment_method_name, $titl $default_settings[ 'title_' . $blocks . $payment_method_name ] ); - $field_payment_method_description = InternationalizationHelper::generate_i18n_field( + $field_payment_method_description = $this->internalionalization_helper->generate_i18n_field( 'description_' . $blocks . $payment_method_name, array( 'title' => __( 'Description', 'alma-gateway-for-woocommerce' ), diff --git a/src/includes/Admin/Helpers/FormHelper.php b/src/includes/Admin/Helpers/FormHelper.php index c074dfaf..96115d66 100644 --- a/src/includes/Admin/Helpers/FormHelper.php +++ b/src/includes/Admin/Helpers/FormHelper.php @@ -15,7 +15,13 @@ die( 'Not allowed' ); // Exit if accessed directly. } -use Alma\Woocommerce\Helpers\SettingsHelper as AlmaHelperSettings; +use Alma\Woocommerce\AlmaLogger; +use Alma\Woocommerce\Factories\CurrencyFactory; +use Alma\Woocommerce\Factories\PriceFactory; +use Alma\Woocommerce\Factories\VersionFactory; +use Alma\Woocommerce\Helpers\InternationalizationHelper; +use Alma\Woocommerce\Helpers\SettingsHelper; +use Alma\Woocommerce\Helpers\ToolsHelper; /** * FormHelper. @@ -29,11 +35,25 @@ class FormHelper { */ protected $form_fields_helper; + + /** + * The Settings Helper. + * + * @var SettingsHelper + */ + protected $settings_helper; + + /** * Constructor. */ public function __construct() { $this->form_fields_helper = new FormFieldsHelper(); + $this->settings_helper = new SettingsHelper( + new InternationalizationHelper(), + new VersionFactory(), + new ToolsHelper( new AlmaLogger(), new PriceFactory(), new CurrencyFactory() ) + ); } /** @@ -44,7 +64,7 @@ public function __construct() { * @return array[] */ public function init_form_fields( $show_alma_fee_plans ) { - $default_settings = AlmaHelperSettings::default_settings(); + $default_settings = $this->settings_helper->default_settings(); if ( ! $show_alma_fee_plans ) { return array_merge( diff --git a/src/includes/Admin/Helpers/GeneralHelper.php b/src/includes/Admin/Helpers/GeneralHelper.php index 75243dcb..36d492de 100644 --- a/src/includes/Admin/Helpers/GeneralHelper.php +++ b/src/includes/Admin/Helpers/GeneralHelper.php @@ -22,6 +22,19 @@ */ class GeneralHelper { + /** + * Internalionalization Helper. + * + * @var InternationalizationHelper + */ + protected $internalization_helper; + + /** + * Constructor + */ + public function __construct() { + $this->internalization_helper = new InternationalizationHelper(); + } /** * Returns if the language code in parameter matches the current admin page language. * @@ -29,8 +42,8 @@ class GeneralHelper { * * @return bool */ - public static function is_lang_selected( $code_lang ) { - if ( self::get_current_admin_page_language() === $code_lang ) { + public function is_lang_selected( $code_lang ) { + if ( $this->get_current_admin_page_language() === $code_lang ) { return true; } @@ -42,14 +55,14 @@ public static function is_lang_selected( $code_lang ) { * * @return string */ - public static function get_current_admin_page_language() { + public function get_current_admin_page_language() { $current_admin_page_language = get_locale(); if ( defined( 'ICL_LANGUAGE_CODE' ) ) { $current_admin_page_language = \ICL_LANGUAGE_CODE; } - return InternationalizationHelper::map_locale( $current_admin_page_language ); + return $this->internalization_helper->map_locale( $current_admin_page_language ); } } diff --git a/src/includes/Admin/Helpers/RefundHelper.php b/src/includes/Admin/Helpers/RefundHelper.php index 4b38efe2..a35f7108 100644 --- a/src/includes/Admin/Helpers/RefundHelper.php +++ b/src/includes/Admin/Helpers/RefundHelper.php @@ -15,12 +15,12 @@ die( 'Not allowed' ); // Exit if accessed directly. } -use Alma\Woocommerce\Helpers\CurrencyHelper; -use Alma\Woocommerce\Helpers\PriceHelper; -use Alma\Woocommerce\Helpers\ToolsHelper; -use Alma\Woocommerce\AlmaSettings; use Alma\Woocommerce\AlmaLogger; +use Alma\Woocommerce\AlmaSettings; +use Alma\Woocommerce\Factories\CurrencyFactory; +use Alma\Woocommerce\Factories\PriceFactory; use Alma\Woocommerce\Helpers\ConstantsHelper; +use Alma\Woocommerce\Helpers\ToolsHelper; /** * RefundHelper @@ -62,7 +62,7 @@ class RefundHelper { public function __construct() { $this->logger = new AlmaLogger(); $this->alma_settings = new AlmaSettings(); - $this->tool_helper = new ToolsHelper( $this->logger, new PriceHelper(), new CurrencyHelper() ); + $this->tool_helper = new ToolsHelper( $this->logger, new PriceFactory(), new CurrencyFactory() ); } /** diff --git a/src/includes/Admin/Helpers/ShareOfCheckoutHelper.php b/src/includes/Admin/Helpers/ShareOfCheckoutHelper.php index fb5efcb5..47cca4cb 100644 --- a/src/includes/Admin/Helpers/ShareOfCheckoutHelper.php +++ b/src/includes/Admin/Helpers/ShareOfCheckoutHelper.php @@ -14,9 +14,9 @@ use Alma\Woocommerce\AlmaLogger; use Alma\Woocommerce\AlmaSettings; use Alma\Woocommerce\Exceptions\ApiSocLastUpdateDatesException; -use Alma\Woocommerce\Helpers\CurrencyHelper; +use Alma\Woocommerce\Factories\CurrencyFactory; +use Alma\Woocommerce\Factories\PriceFactory; use Alma\Woocommerce\Helpers\OrderHelper; -use Alma\Woocommerce\Helpers\PriceHelper; use Alma\Woocommerce\Helpers\ToolsHelper; if ( ! defined( 'ABSPATH' ) ) { @@ -62,7 +62,7 @@ class ShareOfCheckoutHelper { public function __construct() { $this->alma_settings = new AlmaSettings(); $this->order_helper = new OrderHelper(); - $this->tool_helper = new ToolsHelper( new AlmaLogger(), new PriceHelper(), new CurrencyHelper() ); + $this->tool_helper = new ToolsHelper( new AlmaLogger(), new PriceFactory(), new CurrencyFactory() ); $this->check_legal_helper = new CheckLegalHelper(); } diff --git a/src/includes/AlmaSettings.php b/src/includes/AlmaSettings.php index b97059fb..f8a5ef8d 100644 --- a/src/includes/AlmaSettings.php +++ b/src/includes/AlmaSettings.php @@ -21,6 +21,7 @@ use Alma\API\ParamsError; use Alma\API\RequestError; use Alma\Woocommerce\Exceptions\ActivationException; +use Alma\Woocommerce\Exceptions\AlmaException; use Alma\Woocommerce\Exceptions\ApiCreatePaymentsException; use Alma\Woocommerce\Exceptions\ApiFetchPaymentsException; use Alma\Woocommerce\Exceptions\ApiFullRefundException; @@ -32,23 +33,22 @@ use Alma\Woocommerce\Exceptions\ApiShareOfCheckoutException; use Alma\Woocommerce\Exceptions\ApiSocLastUpdateDatesException; use Alma\Woocommerce\Exceptions\ApiTriggerPaymentsException; -use Alma\Woocommerce\Exceptions\AlmaException; use Alma\Woocommerce\Exceptions\PlansDefinitionException; use Alma\Woocommerce\Exceptions\WrongCredentialsException; +use Alma\Woocommerce\Factories\CurrencyFactory; +use Alma\Woocommerce\Factories\PriceFactory; +use Alma\Woocommerce\Factories\SessionFactory; +use Alma\Woocommerce\Factories\VersionFactory; use Alma\Woocommerce\Helpers\CartHelper; use Alma\Woocommerce\Helpers\ConstantsHelper; -use Alma\Woocommerce\Helpers\CurrencyHelper; use Alma\Woocommerce\Helpers\EncryptorHelper; use Alma\Woocommerce\Helpers\FeePlanHelper; use Alma\Woocommerce\Helpers\GeneralHelper; use Alma\Woocommerce\Helpers\InternationalizationHelper; use Alma\Woocommerce\Helpers\PaymentHelper; -use Alma\Woocommerce\Helpers\PriceHelper; -use Alma\Woocommerce\Helpers\SessionHelper; -use Alma\Woocommerce\Helpers\SettingsHelper as AlmaHelperSettings; use Alma\Woocommerce\Helpers\PlanBuilderHelper; +use Alma\Woocommerce\Helpers\SettingsHelper; use Alma\Woocommerce\Helpers\ToolsHelper; -use Alma\Woocommerce\Helpers\VersionHelper; /** * Handles settings retrieval from the settings API. @@ -142,23 +142,34 @@ class AlmaSettings { */ public $cart_helper; + /** + * Internalionalization Helper. + * + * @var InternationalizationHelper + */ + protected $internationalization_helper; + + /** + * Settings Helper. + * + * @var SettingsHelper + */ + protected $settings_helper; /** * Constructor. */ public function __construct() { - $this->logger = new AlmaLogger(); - $this->encryptor_helper = new EncryptorHelper(); - $this->fee_plan_helper = new FeePlanHelper(); - $this->cart_helper = new CartHelper( - new ToolsHelper( - $this->logger, - new PriceHelper(), - new CurrencyHelper() - ), - new SessionHelper(), - new VersionHelper() - ); + $this->logger = new AlmaLogger(); + $this->encryptor_helper = new EncryptorHelper(); + $this->fee_plan_helper = new FeePlanHelper(); + $this->internationalization_helper = new InternationalizationHelper(); + + $version_helper = new VersionFactory(); + $tools_helper = new ToolsHelper( $this->logger, new PriceFactory(), new CurrencyFactory() ); + + $this->settings_helper = new SettingsHelper( $this->internationalization_helper, $version_helper, $tools_helper ); + $this->cart_helper = new CartHelper( $tools_helper, new SessionFactory(), $version_helper ); $this->load_settings(); } @@ -199,7 +210,7 @@ public function get_settings() { $settings['allowed_fee_plans'] = unserialize( $settings['allowed_fee_plans'] ); // phpcs:ignore } - return array_merge( AlmaHelperSettings::default_settings(), $settings ); + return array_merge( $this->settings_helper->default_settings(), $settings ); } /** @@ -433,12 +444,12 @@ public function get_description( $payment_method, $is_blocks = false ) { * @return string */ public function get_i18n( $key ) { - if ( InternationalizationHelper::is_site_multilingual() ) { + if ( $this->internationalization_helper->is_site_multilingual() ) { if ( $this->{$key . '_' . get_locale()} ) { return $this->{$key . '_' . get_locale()}; } - return InternationalizationHelper::get_translated_text( - AlmaHelperSettings::default_settings()[ $key ], + return $this->internationalization_helper->get_translated_text( + $this->settings_helper->default_settings()[ $key ], get_locale() ); } diff --git a/src/includes/Blocks/AlmaBlock.php b/src/includes/Blocks/AlmaBlock.php index 588965a6..05cd9838 100644 --- a/src/includes/Blocks/AlmaBlock.php +++ b/src/includes/Blocks/AlmaBlock.php @@ -13,18 +13,18 @@ use Alma\Woocommerce\AlmaLogger; use Alma\Woocommerce\AlmaSettings; +use Alma\Woocommerce\Factories\CurrencyFactory; +use Alma\Woocommerce\Factories\PriceFactory; +use Alma\Woocommerce\Factories\SessionFactory; +use Alma\Woocommerce\Factories\VersionFactory; +use Alma\Woocommerce\Helpers\AssetsHelper; use Alma\Woocommerce\Helpers\CartHelper; use Alma\Woocommerce\Helpers\CheckoutHelper; -use Alma\Woocommerce\Helpers\CurrencyHelper; +use Alma\Woocommerce\Helpers\ConstantsHelper; use Alma\Woocommerce\Helpers\GatewayHelper; -use Alma\Woocommerce\Helpers\PriceHelper; -use Alma\Woocommerce\Helpers\SessionHelper; +use Alma\Woocommerce\Helpers\PlanBuilderHelper; use Alma\Woocommerce\Helpers\ToolsHelper; -use Alma\Woocommerce\Helpers\VersionHelper; use Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType; -use Alma\Woocommerce\Helpers\AssetsHelper; -use Alma\Woocommerce\Helpers\ConstantsHelper; -use Alma\Woocommerce\Helpers\PlanBuilderHelper; if ( ! defined( 'ABSPATH' ) ) { die( 'Not allowed' ); // Exit if accessed directly. @@ -83,11 +83,11 @@ public function initialize() { $this->cart_helper = new CartHelper( new ToolsHelper( new AlmaLogger(), - new PriceHelper(), - new CurrencyHelper() + new PriceFactory(), + new CurrencyFactory() ), - new SessionHelper(), - new VersionHelper() + new SessionFactory(), + new VersionFactory() ); $this->alma_plan_builder = new PlanBuilderHelper(); } diff --git a/src/includes/Helpers/CurrencyHelper.php b/src/includes/Factories/CurrencyFactory.php similarity index 60% rename from src/includes/Helpers/CurrencyHelper.php rename to src/includes/Factories/CurrencyFactory.php index cd368109..e45039ee 100644 --- a/src/includes/Helpers/CurrencyHelper.php +++ b/src/includes/Factories/CurrencyFactory.php @@ -1,24 +1,24 @@ scripts_helper = new AssetsHelper(); $this->plan_builder = new PlanBuilderHelper(); $this->encryption_helper = new EncryptorHelper(); - $this->tool_helper = new ToolsHelper( $this->logger, new PriceHelper(), new CurrencyHelper() ); + $this->tool_helper = new ToolsHelper( $this->logger, new PriceFactory(), new CurrencyFactory() ); $this->alma_payment_helper = new PaymentHelper(); $this->order_helper = new OrderHelper(); $this->template_loader = new TemplateLoaderHelper(); $this->soc_helper = new ShareOfCheckoutHelper(); $this->plugin_helper = new PluginHelper(); - $this->cart_helper = new CartHelper( $this->tool_helper, new SessionHelper(), new VersionHelper() ); + $version_helper = new VersionFactory(); + $this->settings_helper = new SettingsHelper( new InternationalizationHelper(), $version_helper, $this->tool_helper ); + $this->cart_helper = new CartHelper( $this->tool_helper, new SessionFactory(), $version_helper ); $this->id = $this->get_gateway_id(); $this->method_title = __( 'Payment in instalments and deferred with Alma - 2x 3x 4x', 'alma-gateway-for-woocommerce' ); $this->method_description = __( 'Install Alma and boost your sales! It\'s simple and guaranteed, your cash flow is secured. 0 commitment, 0 subscription, 0 risk.', 'alma-gateway-for-woocommerce' ); @@ -197,7 +207,7 @@ public function __construct( $check_basics = true ) { if ( $check_basics ) { $this->check_activation(); - SettingsHelper::check_alma_keys( $this->alma_settings->has_keys(), false ); + $this->settings_helper->check_alma_keys( $this->alma_settings->has_keys(), false ); $this->add_filters(); $this->gateway_helper->add_actions(); $this->init_admin_form(); @@ -365,7 +375,7 @@ public function init_admin_form() { * @throws NoCredentialsException No credentials Api excetption. */ public function manage_credentials() { - SettingsHelper::check_alma_keys( $this->alma_settings->has_keys() ); + $this->settings_helper->check_alma_keys( $this->alma_settings->has_keys() ); $this->init_alma_client(); @@ -544,8 +554,7 @@ public function clean_credentials( $post_data ) { ) || $this->alma_settings->settings['environment'] !== $post_data['woocommerce_alma_environment'] ) { - $settings_helper = new SettingsHelper(); - $this->alma_settings->settings = $settings_helper->reset_plans( $this->alma_settings->settings ); + $this->alma_settings->settings = $this->settings_helper->reset_plans( $this->alma_settings->settings ); } } @@ -579,6 +588,8 @@ protected function convert_amounts_to_cents() { * @noinspection PhpUnused */ public function generate_text_alma_i18n_html( $key, $data ) { + $admin_general_helper = new AdminGeneralHelper(); + $field_key = $this->get_field_key( $key ); $defaults = array( 'title' => '', @@ -619,7 +630,7 @@ public function generate_text_alma_i18n_html( $key, $data ) {