Commit 88363f3884 for woocommerce
commit 88363f3884a9e70c6a7765b6ffb1887f64c78c31
Author: Wesley Rosa <wesleyjrosa@gmail.com>
Date: Wed Jan 7 09:37:08 2026 -0300
PayPal Standard Refactor 7: Moving the PayPal Buttons class to the src folder (#62633)
* Moving the PayPal buttons class to the src folder
* Add changefile(s) from automation for the following project(s): woocommerce
* Fix namespace
* Fix PHPStan issue
* Add missing return types + deprecated warning method calls
* Fix require path
* Apply suggestion from @daledupreez
Co-authored-by: daledupreez <dale@automattic.com>
* Fix doc block return type
* Update plugins/woocommerce/src/Gateways/PayPal/Buttons.php
Co-authored-by: Malith Senaweera <6216000+malithsen@users.noreply.github.com>
* Fix test return type
* Fix tests
* Address review comment
* PHPStan baseline update
* Fix tests
* Fix tests
* Fix tests
* Fix tests
* Using the new Request class
* Baseline file update
* Moving import above the ABSPATH check
* Revert class loading removal
---------
Co-authored-by: github-actions <github-actions@github.com>
Co-authored-by: daledupreez <dale@automattic.com>
Co-authored-by: Malith Senaweera <6216000+malithsen@users.noreply.github.com>
diff --git a/plugins/woocommerce/changelog/62633-refactor-paypal-standard-6-buttons-class b/plugins/woocommerce/changelog/62633-refactor-paypal-standard-6-buttons-class
new file mode 100644
index 0000000000..b64d6ed9f0
--- /dev/null
+++ b/plugins/woocommerce/changelog/62633-refactor-paypal-standard-6-buttons-class
@@ -0,0 +1,4 @@
+Significance: patch
+Type: dev
+
+Deprecate WC_Gateway_Paypal_Buttons class in favor of Automattic\WooCommerce\Gateways\PayPal\Buttons class.
\ No newline at end of file
diff --git a/plugins/woocommerce/includes/gateways/paypal/class-wc-gateway-paypal-buttons.php b/plugins/woocommerce/includes/gateways/paypal/class-wc-gateway-paypal-buttons.php
index 8ee6791ea6..61a6a2d9f3 100644
--- a/plugins/woocommerce/includes/gateways/paypal/class-wc-gateway-paypal-buttons.php
+++ b/plugins/woocommerce/includes/gateways/paypal/class-wc-gateway-paypal-buttons.php
@@ -3,10 +3,14 @@
* Class WC_Gateway_Paypal_Buttons file.
*
* @package WooCommerce\Gateways
+ *
+ * @deprecated 10.5.0 Use Automattic\WooCommerce\Gateways\PayPal\Buttons instead. This class will be removed in 11.0.0.
*/
declare(strict_types=1);
+use Automattic\WooCommerce\Gateways\PayPal\Buttons as PayPalButtons;
+
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
@@ -17,37 +21,17 @@ if ( ! class_exists( 'WC_Gateway_Paypal_Request' ) ) {
/**
* Handles PayPal Buttons.
+ *
+ * @deprecated 10.5.0 Use Automattic\WooCommerce\Gateways\PayPal\Buttons instead. This class will be removed in 11.0.0.
*/
class WC_Gateway_Paypal_Buttons {
/**
- * The option for the client-id.
- *
- * @var string
- */
- private const CLIENT_ID_OPTION = 'woocommerce_paypal_client_id';
-
-
- /**
- * The gateway instance.
- *
- * @var WC_Gateway_Paypal
- */
- private $gateway;
-
- /**
- * Whether the gateway should use Orders v2 API.
- *
- * @var bool
- */
- private $enabled = false;
-
- /**
- * The request instance.
+ * The delegated buttons instance.
*
- * @var WC_Gateway_Paypal_Request
+ * @var PayPalButtons
*/
- private $request;
+ private $buttons;
/**
* Constructor.
@@ -55,94 +39,92 @@ class WC_Gateway_Paypal_Buttons {
* @param WC_Gateway_Paypal $gateway The gateway instance.
*/
public function __construct( WC_Gateway_Paypal $gateway ) {
- $this->gateway = $gateway;
- $this->request = new WC_Gateway_Paypal_Request( $this->gateway );
-
- // phpcs:ignore Generic.Commenting.Todo.TaskFound
- $this->enabled = $this->gateway->should_use_orders_v2() && 'yes' === $this->gateway->get_option( 'paypal_buttons', 'yes' );
+ $this->buttons = new PayPalButtons( $gateway );
}
/**
* Get the options for the PayPal buttons.
*
* @return array
+ *
+ * @deprecated 10.5.0 Use Automattic\WooCommerce\Gateways\PayPal\Buttons::get_options() instead. This method will be removed in 11.0.0.
*/
public function get_options() {
- $common_options = $this->get_common_options();
- $options = array(
- 'partner-attribution-id' => 'Woo_Cart_CoreUpgrade',
- 'page-type' => $this->get_page_type(),
+ wc_deprecated_function(
+ __METHOD__,
+ '10.5.0',
+ 'Use Automattic\WooCommerce\Gateways\PayPal\Buttons::get_options() instead.'
);
- return array_merge( $common_options, $options );
+ return $this->buttons->get_options();
}
/**
* Get the common attributes for the PayPal JS SDK script and modules.
*
* @return array
+ *
+ * @deprecated 10.5.0 Use Automattic\WooCommerce\Gateways\PayPal\Buttons::get_common_options() instead. This method will be removed in 11.0.0.
*/
public function get_common_options() {
- $intent = $this->gateway->get_option( 'paymentaction' ) === 'authorization' ? 'authorize' : 'capture';
-
- return array(
- 'client-id' => $this->get_client_id(),
- 'components' => 'buttons,funding-eligibility,messages',
- 'disable-funding' => 'card,applepay',
- 'enable-funding' => 'venmo,paylater',
- 'currency' => get_woocommerce_currency(),
- 'intent' => $intent,
- 'merchant-id' => $this->gateway->email,
+ wc_deprecated_function(
+ __METHOD__,
+ '10.5.0',
+ 'Use Automattic\WooCommerce\Gateways\PayPal\Buttons::get_common_options() instead.'
);
+
+ return $this->buttons->get_common_options();
}
/**
* Get the client-id for the PayPal buttons.
*
* @return string|null The PayPal client-id, or null if the request fails.
+ *
+ * @deprecated 10.5.0 Use Automattic\WooCommerce\Gateways\PayPal\Buttons::get_client_id() instead. This method will be removed in 11.0.0.
*/
public function get_client_id() {
- if ( ! $this->gateway->should_use_orders_v2() ) {
- return null;
- }
-
- $option_key = self::CLIENT_ID_OPTION . ( $this->gateway->testmode ? '_sandbox' : '_live' );
- $client_id = get_option( $option_key, null );
-
- if ( empty( $client_id ) ) {
- $client_id = $this->request->fetch_paypal_client_id();
- if ( empty( $client_id ) ) {
- return null;
- }
- update_option( $option_key, $client_id );
- }
-
- return $client_id;
+ wc_deprecated_function(
+ __METHOD__,
+ '10.5.0',
+ 'Use Automattic\WooCommerce\Gateways\PayPal\Buttons::get_client_id() instead.'
+ );
+
+ return $this->buttons->get_client_id();
}
/**
* Get the page type for the PayPal buttons.
*
* @return string
+ *
+ * @deprecated 10.5.0 Use Automattic\WooCommerce\Gateways\PayPal\Buttons::get_page_type() instead. This method will be removed in 11.0.0.
*/
public function get_page_type() {
- $page_type = 'checkout';
- if ( is_cart() || has_block( 'woocommerce/cart' ) ) {
- $page_type = 'cart';
- } elseif ( is_product() ) {
- $page_type = 'product-details';
- }
-
- return $page_type;
+ wc_deprecated_function(
+ __METHOD__,
+ '10.5.0',
+ 'Use Automattic\WooCommerce\Gateways\PayPal\Buttons::get_page_type() instead.'
+ );
+
+ return $this->buttons->get_page_type();
}
/**
* Whether PayPal Buttons is enabled.
*
* @return bool
+ *
+ * @deprecated 10.5.0 Use Automattic\WooCommerce\Gateways\PayPal\Buttons::is_enabled() instead. This method will be removed in 11.0.0.
*/
public function is_enabled() {
- return $this->enabled;
+ wc_deprecated_function(
+ __METHOD__,
+ '10.5.0',
+ 'Use Automattic\WooCommerce\Gateways\PayPal\Buttons::is_enabled() instead.'
+ );
+
+ return $this->buttons->is_enabled();
}
/**
@@ -150,13 +132,16 @@ class WC_Gateway_Paypal_Buttons {
* Limited to checkout, cart, and product pages for security.
*
* @return string
+ *
+ * @deprecated 10.5.0 Use Automattic\WooCommerce\Gateways\PayPal\Buttons::get_current_page_for_app_switch() instead. This method will be removed in 11.0.0.
*/
public function get_current_page_for_app_switch() {
- // If checkout, cart or product page, return the current page URL.
- if ( is_checkout() || is_cart() || is_product() ) {
- return get_permalink( get_the_ID() );
- }
+ wc_deprecated_function(
+ __METHOD__,
+ '10.5.0',
+ 'Use Automattic\WooCommerce\Gateways\PayPal\Buttons::get_current_page_for_app_switch() instead.'
+ );
- return '';
+ return $this->buttons->get_current_page_for_app_switch();
}
}
diff --git a/plugins/woocommerce/includes/gateways/paypal/class-wc-gateway-paypal.php b/plugins/woocommerce/includes/gateways/paypal/class-wc-gateway-paypal.php
index dfeb4f45e2..187048feec 100644
--- a/plugins/woocommerce/includes/gateways/paypal/class-wc-gateway-paypal.php
+++ b/plugins/woocommerce/includes/gateways/paypal/class-wc-gateway-paypal.php
@@ -13,6 +13,7 @@
use Automattic\Jetpack\Constants;
use Automattic\WooCommerce\Enums\PaymentGatewayFeature;
use Automattic\Jetpack\Connection\Manager as Jetpack_Connection_Manager;
+use Automattic\WooCommerce\Gateways\PayPal\Buttons as PayPalButtons;
use Automattic\WooCommerce\Gateways\PayPal\Constants as PayPalConstants;
use Automattic\WooCommerce\Gateways\PayPal\Helper as PayPalHelper;
use Automattic\WooCommerce\Gateways\PayPal\Notices as PayPalNotices;
@@ -211,7 +212,7 @@ class WC_Gateway_Paypal extends WC_Payment_Gateway {
// Hook for PayPal order responses to manage account restriction notices.
add_action( 'woocommerce_paypal_standard_order_created_response', array( $this, 'manage_account_restriction_status' ), 10, 3 );
- $buttons = new WC_Gateway_Paypal_Buttons( $this );
+ $buttons = new PayPalButtons( $this );
if ( $buttons->is_enabled() && ! $this->needs_setup() ) {
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
add_filter( 'wp_script_attributes', array( $this, 'add_paypal_sdk_attributes' ) );
@@ -783,7 +784,7 @@ class WC_Gateway_Paypal extends WC_Payment_Gateway {
$version = Constants::get_constant( 'WC_VERSION' );
$is_page_supported = is_checkout() || is_cart() || is_product();
- $buttons = new WC_Gateway_Paypal_Buttons( $this );
+ $buttons = new PayPalButtons( $this );
$options = $buttons->get_common_options();
if ( empty( $options['client-id'] ) || ! $is_page_supported ) {
@@ -824,7 +825,7 @@ class WC_Gateway_Paypal extends WC_Payment_Gateway {
*/
public function add_paypal_sdk_attributes( $attrs ) {
if ( 'paypal-standard-sdk-js' === $attrs['id'] ) {
- $buttons = new WC_Gateway_Paypal_Buttons( $this );
+ $buttons = new PayPalButtons( $this );
$page_type = $buttons->get_page_type();
$attrs['data-page-type'] = $page_type;
diff --git a/plugins/woocommerce/phpstan-baseline.neon b/plugins/woocommerce/phpstan-baseline.neon
index 5e21e3591e..8fd5db9468 100644
--- a/plugins/woocommerce/phpstan-baseline.neon
+++ b/plugins/woocommerce/phpstan-baseline.neon
@@ -24510,18 +24510,6 @@ parameters:
count: 1
path: includes/gateways/cod/class-wc-gateway-cod.php
- -
- message: '#^Method WC_Gateway_Paypal_Buttons\:\:get_current_page_for_app_switch\(\) should return string but returns string\|false\.$#'
- identifier: return.type
- count: 1
- path: includes/gateways/paypal/class-wc-gateway-paypal-buttons.php
-
- -
- message: '#^Parameter \#1 \$post of function get_permalink expects int\|WP_Post, int\|false given\.$#'
- identifier: argument.type
- count: 1
- path: includes/gateways/paypal/class-wc-gateway-paypal-buttons.php
-
-
message: '#^Access to an undefined property object\:\:\$ACK\.$#'
identifier: property.notFound
@@ -62703,12 +62691,6 @@ parameters:
count: 1
path: src/Blocks/Payments/Integrations/Cheque.php
- -
- message: '#^Constant WC_ABSPATH not found\.$#'
- identifier: constant.notFound
- count: 1
- path: src/Blocks/Payments/Integrations/PayPal.php
-
-
message: '#^Method Automattic\\WooCommerce\\Blocks\\Payments\\Integrations\\PayPal\:\:initialize\(\) has no return type specified\.$#'
identifier: missingType.return
@@ -64029,6 +64011,18 @@ parameters:
count: 1
path: src/Gateways/PayPal/AddressRequirements.php
+ -
+ message: '#^Method Automattic\\WooCommerce\\Gateways\\PayPal\\Buttons\:\:get_current_page_for_app_switch\(\) should return string but returns string\|false\.$#'
+ identifier: return.type
+ count: 1
+ path: src/Gateways/PayPal/Buttons.php
+
+ -
+ message: '#^Parameter \#1 \$post of function get_permalink expects int\|WP_Post, int\|false given\.$#'
+ identifier: argument.type
+ count: 1
+ path: src/Gateways/PayPal/Buttons.php
+
-
message: '#^Method Automattic\\WooCommerce\\Gateways\\PayPal\\Request\:\:send_wpcom_proxy_request\(\) never returns array so it can be removed from the return type\.$#'
identifier: return.unusedType
diff --git a/plugins/woocommerce/src/Blocks/Payments/Integrations/PayPal.php b/plugins/woocommerce/src/Blocks/Payments/Integrations/PayPal.php
index 39b6729026..06f2d191ee 100644
--- a/plugins/woocommerce/src/Blocks/Payments/Integrations/PayPal.php
+++ b/plugins/woocommerce/src/Blocks/Payments/Integrations/PayPal.php
@@ -3,6 +3,7 @@ namespace Automattic\WooCommerce\Blocks\Payments\Integrations;
use WC_Gateway_Paypal;
use Automattic\WooCommerce\Blocks\Assets\Api;
+use Automattic\WooCommerce\Gateways\PayPal\Buttons as PayPalButtons;
/**
* PayPal Standard payment method integration
@@ -74,8 +75,7 @@ final class PayPal extends AbstractPaymentMethodType {
return [];
}
- include_once WC_ABSPATH . 'includes/gateways/paypal/class-wc-gateway-paypal-buttons.php';
- $buttons = new \WC_Gateway_Paypal_Buttons( $gateway );
+ $buttons = new PayPalButtons( $gateway );
$options = $buttons->get_options();
return [
diff --git a/plugins/woocommerce/src/Gateways/PayPal/Buttons.php b/plugins/woocommerce/src/Gateways/PayPal/Buttons.php
new file mode 100644
index 0000000000..0af5a1f035
--- /dev/null
+++ b/plugins/woocommerce/src/Gateways/PayPal/Buttons.php
@@ -0,0 +1,176 @@
+<?php
+/**
+ * Class Automattic\WooCommerce\Gateways\PayPal\Buttons file.
+ *
+ * @package WooCommerce\Gateways
+ */
+
+declare(strict_types=1);
+
+namespace Automattic\WooCommerce\Gateways\PayPal;
+
+use Automattic\WooCommerce\Proxies\LegacyProxy;
+use Automattic\WooCommerce\Gateways\PayPal\Request as PayPalRequest;
+
+if ( ! defined( 'ABSPATH' ) ) {
+ exit;
+}
+
+/**
+ * Handles PayPal Buttons.
+ *
+ * @since 10.5.0
+ */
+class Buttons {
+
+ /**
+ * The option for the client-id.
+ *
+ * @var string
+ */
+ private const CLIENT_ID_OPTION = 'woocommerce_paypal_client_id';
+
+
+ /**
+ * The gateway instance.
+ *
+ * @var \WC_Gateway_Paypal
+ */
+ private \WC_Gateway_Paypal $gateway;
+
+ /**
+ * Whether the gateway should use Orders v2 API.
+ *
+ * @var bool
+ */
+ private bool $enabled;
+
+ /**
+ * The request instance.
+ *
+ * @var PayPalRequest
+ */
+ private PayPalRequest $request;
+
+ /**
+ * Constructor.
+ *
+ * @param \WC_Gateway_Paypal $gateway The gateway instance.
+ */
+ public function __construct( \WC_Gateway_Paypal $gateway ) {
+ $this->gateway = $gateway;
+ $this->request = new PayPalRequest( $this->gateway );
+
+ $this->enabled = $this->gateway->should_use_orders_v2() && 'yes' === $this->gateway->get_option( 'paypal_buttons', 'yes' );
+ }
+
+ /**
+ * Get the options for the PayPal buttons.
+ *
+ * @since 10.5.0
+ *
+ * @return array
+ */
+ public function get_options(): array {
+ $common_options = $this->get_common_options();
+ $options = array(
+ 'partner-attribution-id' => 'Woo_Cart_CoreUpgrade',
+ 'page-type' => $this->get_page_type(),
+ );
+
+ return array_merge( $common_options, $options );
+ }
+
+ /**
+ * Get the common attributes for the PayPal JS SDK script and modules.
+ *
+ * @since 10.5.0
+ *
+ * @return array
+ */
+ public function get_common_options(): array {
+ $intent = $this->gateway->get_option( 'paymentaction' ) === 'authorization' ? 'authorize' : 'capture';
+
+ return array(
+ 'client-id' => $this->get_client_id(),
+ 'components' => 'buttons,funding-eligibility,messages',
+ 'disable-funding' => 'card,applepay',
+ 'enable-funding' => 'venmo,paylater',
+ 'currency' => get_woocommerce_currency(),
+ 'intent' => $intent,
+ 'merchant-id' => $this->gateway->email,
+ );
+ }
+
+ /**
+ * Get the client-id for the PayPal buttons.
+ *
+ * @since 10.5.0
+ *
+ * @return string|null The PayPal client-id, or null if the request fails.
+ */
+ public function get_client_id(): ?string {
+ if ( ! $this->gateway->should_use_orders_v2() ) {
+ return null;
+ }
+
+ $option_key = self::CLIENT_ID_OPTION . ( $this->gateway->testmode ? '_sandbox' : '_live' );
+ $client_id = get_option( $option_key, null );
+
+ if ( empty( $client_id ) ) {
+ $client_id = $this->request->fetch_paypal_client_id();
+ if ( empty( $client_id ) ) {
+ return null;
+ }
+ update_option( $option_key, $client_id );
+ }
+
+ return $client_id;
+ }
+
+ /**
+ * Get the page type for the PayPal buttons.
+ *
+ * @since 10.5.0
+ *
+ * @return string
+ */
+ public function get_page_type(): string {
+ $page_type = 'checkout';
+ if ( is_cart() || has_block( 'woocommerce/cart' ) ) {
+ $page_type = 'cart';
+ } elseif ( is_product() ) {
+ $page_type = 'product-details';
+ }
+
+ return $page_type;
+ }
+
+ /**
+ * Whether PayPal Buttons is enabled.
+ *
+ * @since 10.5.0
+ *
+ * @return bool
+ */
+ public function is_enabled(): bool {
+ return $this->enabled;
+ }
+
+ /**
+ * Get the current page URL, to be used for App Switch.
+ * Limited to checkout, cart, and product pages for security.
+ *
+ * @since 10.5.0
+ *
+ * @return string
+ */
+ public function get_current_page_for_app_switch(): string {
+ // If checkout, cart or product page, return the current page URL.
+ if ( wc_get_container()->get( LegacyProxy::class )->call_function( 'is_checkout' ) || is_cart() || is_product() ) {
+ return get_permalink( get_the_ID() );
+ }
+
+ return '';
+ }
+}
diff --git a/plugins/woocommerce/tests/php/includes/gateways/paypal/class-wc-gateway-paypal-buttons-test.php b/plugins/woocommerce/tests/php/src/Gateways/PayPal/ButtonsTest.php
similarity index 75%
rename from plugins/woocommerce/tests/php/includes/gateways/paypal/class-wc-gateway-paypal-buttons-test.php
rename to plugins/woocommerce/tests/php/src/Gateways/PayPal/ButtonsTest.php
index 599b34d00c..2af23c78a9 100644
--- a/plugins/woocommerce/tests/php/includes/gateways/paypal/class-wc-gateway-paypal-buttons-test.php
+++ b/plugins/woocommerce/tests/php/src/Gateways/PayPal/ButtonsTest.php
@@ -1,42 +1,47 @@
<?php
/**
- * Unit tests for WC_Gateway_Paypal_Buttons class.
+ * Unit tests for Automattic\WooCommerce\Gateways\PayPal\Buttons class.
*
- * @package WooCommerce\Tests\Paypal.
+ * @package WooCommerce\Tests\Gateways\Paypal
*/
declare(strict_types=1);
-require_once WC_ABSPATH . 'includes/gateways/paypal/class-wc-gateway-paypal-buttons.php';
+namespace Automattic\WooCommerce\Tests\Gateways\PayPal;
+
+use Automattic\WooCommerce\Gateways\PayPal\Buttons as PayPalButtons;
+use Automattic\WooCommerce\Proxies\LegacyProxy;
/**
- * Class WC_Gateway_Paypal_Buttons_Test.
+ * Class ButtonsTest.
*/
-class WC_Gateway_Paypal_Buttons_Test extends \WC_Unit_Test_Case {
+class ButtonsTest extends \WC_Unit_Test_Case {
/**
* The buttons instance.
*
- * @var WC_Gateway_Paypal_Buttons
+ * @var PayPalButtons
*/
- private $buttons;
+ private PayPalButtons $buttons;
/**
* Mock gateway instance.
*
- * @var WC_Gateway_Paypal
+ * @var \WC_Gateway_Paypal
*/
- private $mock_gateway;
+ private \WC_Gateway_Paypal $mock_gateway;
/**
* Original global post.
*
- * @var WP_Post
+ * @var \WP_Post|null
*/
- private $original_post;
+ private ?\WP_Post $original_post;
/**
* Set up the test environment.
+ *
+ * @return void
*/
public function setUp(): void {
parent::setUp();
@@ -48,7 +53,7 @@ class WC_Gateway_Paypal_Buttons_Test extends \WC_Unit_Test_Case {
$this->original_post = $post;
// Create a mock gateway.
- $this->mock_gateway = $this->createMock( WC_Gateway_Paypal::class );
+ $this->mock_gateway = $this->createMock( \WC_Gateway_Paypal::class );
$this->mock_gateway->email = 'paypalmerchant@paypal.com';
$this->mock_gateway->testmode = false;
$this->mock_gateway->method( 'should_use_orders_v2' )->willReturn( true );
@@ -59,11 +64,13 @@ class WC_Gateway_Paypal_Buttons_Test extends \WC_Unit_Test_Case {
)
);
- $this->buttons = new WC_Gateway_Paypal_Buttons( $this->mock_gateway );
+ $this->buttons = new PayPalButtons( $this->mock_gateway );
}
/**
* Tear down the test environment.
+ *
+ * @return void
*/
public function tearDown(): void {
delete_option( 'woocommerce_paypal_client_id_live' );
@@ -71,6 +78,7 @@ class WC_Gateway_Paypal_Buttons_Test extends \WC_Unit_Test_Case {
// Restore original global post.
global $post;
+
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$post = $this->original_post;
@@ -84,10 +92,12 @@ class WC_Gateway_Paypal_Buttons_Test extends \WC_Unit_Test_Case {
/**
* Test get_options returns correct structure with common options and specific options.
+ *
+ * @return void
*/
- public function test_get_options_returns_correct_structure() {
+ public function test_get_options_returns_correct_structure(): void {
// Mock get_client_id and get_page_type to return test values.
- $buttons = $this->getMockBuilder( WC_Gateway_Paypal_Buttons::class )
+ $buttons = $this->getMockBuilder( PayPalButtons::class )
->setConstructorArgs( array( $this->mock_gateway ) )
->onlyMethods( array( 'get_client_id', 'get_page_type' ) )
->getMock();
@@ -111,10 +121,12 @@ class WC_Gateway_Paypal_Buttons_Test extends \WC_Unit_Test_Case {
/**
* Test get_common_options returns correct default values.
+ *
+ * @return void
*/
- public function test_get_common_options_returns_correct_defaults() {
+ public function test_get_common_options_returns_correct_defaults(): void {
// Mock get_client_id to return a test client ID.
- $buttons = $this->getMockBuilder( WC_Gateway_Paypal_Buttons::class )
+ $buttons = $this->getMockBuilder( PayPalButtons::class )
->setConstructorArgs( array( $this->mock_gateway ) )
->onlyMethods( array( 'get_client_id' ) )
->getMock();
@@ -138,10 +150,11 @@ class WC_Gateway_Paypal_Buttons_Test extends \WC_Unit_Test_Case {
*
* @param bool $is_cart Whether the current page is a cart page.
* @param string $expected_page_type The expected page type.
+ * @return void
*
* @dataProvider provider_page_type_scenarios
*/
- public function test_get_page_type_returns_correct_value( $is_cart, $expected_page_type ) {
+ public function test_get_page_type_returns_correct_value( $is_cart, $expected_page_type ): void {
// Mock WordPress conditional functions using filters.
if ( $is_cart ) {
add_filter( 'woocommerce_is_cart', '__return_true' );
@@ -159,7 +172,7 @@ class WC_Gateway_Paypal_Buttons_Test extends \WC_Unit_Test_Case {
*
* @return array
*/
- public function provider_page_type_scenarios() {
+ public function provider_page_type_scenarios(): array {
return array(
'cart_page' => array(
'is_cart' => true,
@@ -174,19 +187,23 @@ class WC_Gateway_Paypal_Buttons_Test extends \WC_Unit_Test_Case {
/**
* Test get_client_id returns null when Orders v2 is not enabled.
+ *
+ * @return void
*/
- public function test_get_client_id_returns_null_when_orders_v2_disabled() {
+ public function test_get_client_id_returns_null_when_orders_v2_disabled(): void {
$this->mock_gateway->method( 'should_use_orders_v2' )->willReturn( false );
- $buttons = new WC_Gateway_Paypal_Buttons( $this->mock_gateway );
+ $buttons = new PayPalButtons( $this->mock_gateway );
$this->assertNull( $buttons->get_client_id() );
}
/**
* Test get_client_id returns cached value when available.
+ *
+ * @return void
*/
- public function test_get_client_id_returns_cached_value() {
+ public function test_get_client_id_returns_cached_value(): void {
$this->mock_gateway->testmode = false;
// Set cached client ID.
@@ -199,8 +216,10 @@ class WC_Gateway_Paypal_Buttons_Test extends \WC_Unit_Test_Case {
/**
* Test get_client_id uses sandbox option when testmode is enabled.
+ *
+ * @return void
*/
- public function test_get_client_id_uses_sandbox_option_in_testmode() {
+ public function test_get_client_id_uses_sandbox_option_in_testmode(): void {
$this->mock_gateway->testmode = true;
// Set sandbox client ID.
@@ -213,14 +232,16 @@ class WC_Gateway_Paypal_Buttons_Test extends \WC_Unit_Test_Case {
/**
* Test get_client_id fetches from API when not cached.
+ *
+ * @return void
*/
- public function test_get_client_id_fetches_from_api_when_not_cached() {
- $mock_request = $this->createMock( WC_Gateway_Paypal_Request::class );
+ public function test_get_client_id_fetches_from_api_when_not_cached(): void {
+ $mock_request = $this->createMock( \WC_Gateway_Paypal_Request::class );
$mock_request->method( 'fetch_paypal_client_id' )->willReturn( 'test_client_id' );
- $buttons = new WC_Gateway_Paypal_Buttons( $this->mock_gateway );
+ $buttons = new PayPalButtons( $this->mock_gateway );
- $reflection = new ReflectionClass( $buttons );
+ $reflection = new \ReflectionClass( $buttons );
$request_property = $reflection->getProperty( 'request' );
$request_property->setAccessible( true );
$request_property->setValue( $buttons, $mock_request );
@@ -233,15 +254,17 @@ class WC_Gateway_Paypal_Buttons_Test extends \WC_Unit_Test_Case {
/**
* Test get_client_id returns null when API fails.
+ *
+ * @return void
*/
- public function test_get_client_id_returns_null_when_api_fails() {
- $mock_request = $this->createMock( WC_Gateway_Paypal_Request::class );
+ public function test_get_client_id_returns_null_when_api_fails(): void {
+ $mock_request = $this->createMock( \WC_Gateway_Paypal_Request::class );
$mock_request->method( 'fetch_paypal_client_id' )->willReturn( '' );
- $buttons = new WC_Gateway_Paypal_Buttons( $this->mock_gateway );
+ $buttons = new PayPalButtons( $this->mock_gateway );
// Use reflection to set the request property.
- $reflection = new ReflectionClass( $buttons );
+ $reflection = new \ReflectionClass( $buttons );
$request_property = $reflection->getProperty( 'request' );
$request_property->setAccessible( true );
$request_property->setValue( $buttons, $mock_request );
@@ -257,11 +280,19 @@ class WC_Gateway_Paypal_Buttons_Test extends \WC_Unit_Test_Case {
* @dataProvider provider_app_switch_url_scenarios
*
* @param string $page_type The page type.
- * @param string $filter_name The filter name.
+ * @param bool $is_checkout Whether the current page is checkout.
+ * @param bool $is_cart Whether the current page is cart.
* @param string $post_type The post type.
* @param bool $expected_contains Whether the expected contains.
+ * @return void
*/
- public function test_get_current_page_for_app_switch( $page_type, $filter_name = null, $post_type, $expected_contains ) {
+ public function test_get_current_page_for_app_switch(
+ string $page_type,
+ bool $is_checkout,
+ bool $is_cart,
+ string $post_type,
+ bool $expected_contains
+ ): void {
// Create a test post.
$post_id = $this->factory->post->create(
array(
@@ -273,12 +304,21 @@ class WC_Gateway_Paypal_Buttons_Test extends \WC_Unit_Test_Case {
// Set global post.
global $post;
+
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$post = get_post( $post_id );
// Mock the appropriate page type.
- if ( $filter_name ) {
- add_filter( $filter_name, '__return_true' );
+ wc_get_container()->get( LegacyProxy::class )->register_function_mocks(
+ array(
+ 'is_checkout' => fn() => $is_checkout,
+ )
+ );
+
+ if ( $is_cart ) {
+ add_filter( 'woocommerce_is_cart', '__return_true' );
+ } else {
+ add_filter( 'woocommerce_is_cart', '__return_false' );
}
$url = $this->buttons->get_current_page_for_app_switch();
@@ -292,6 +332,14 @@ class WC_Gateway_Paypal_Buttons_Test extends \WC_Unit_Test_Case {
// Clean up.
wp_delete_post( $post_id, true );
+
+ wc_get_container()->get( LegacyProxy::class )->reset();
+
+ if ( $is_cart ) {
+ remove_filter( 'woocommerce_is_cart', '__return_true' );
+ } else {
+ remove_filter( 'woocommerce_is_cart', '__return_false' );
+ }
}
/**
@@ -299,23 +347,26 @@ class WC_Gateway_Paypal_Buttons_Test extends \WC_Unit_Test_Case {
*
* @return array
*/
- public function provider_app_switch_url_scenarios() {
+ public function provider_app_switch_url_scenarios(): array {
return array(
'checkout_page' => array(
'page_type' => 'checkout',
- 'filter_name' => 'woocommerce_is_checkout',
+ 'is_checkout' => true,
+ 'is_cart' => false,
'post_type' => 'page',
'expected_contains' => true,
),
'cart_page' => array(
'page_type' => 'cart',
- 'filter_name' => 'woocommerce_is_cart',
+ 'is_checkout' => false,
+ 'is_cart' => true,
'post_type' => 'page',
'expected_contains' => true,
),
'other_page' => array(
'page_type' => 'other',
- 'filter_name' => null,
+ 'is_checkout' => false,
+ 'is_cart' => false,
'post_type' => 'page',
'expected_contains' => false,
),
@@ -324,8 +375,10 @@ class WC_Gateway_Paypal_Buttons_Test extends \WC_Unit_Test_Case {
/**
* Test get_current_page_for_app_switch returns empty string for other pages.
+ *
+ * @return void
*/
- public function test_get_current_page_for_app_switch_returns_empty_for_other_pages() {
+ public function test_get_current_page_for_app_switch_returns_empty_for_other_pages(): void {
// Create a test post.
$post_id = $this->factory->post->create(
array(
@@ -336,11 +389,16 @@ class WC_Gateway_Paypal_Buttons_Test extends \WC_Unit_Test_Case {
);
global $post;
+
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$post = get_post( $post_id );
// Mock all page types to return false.
- add_filter( 'woocommerce_is_checkout', '__return_false' );
+ wc_get_container()->get( LegacyProxy::class )->register_function_mocks(
+ array(
+ 'is_checkout' => fn() => false,
+ )
+ );
add_filter( 'woocommerce_is_cart', '__return_false' );
$url = $this->buttons->get_current_page_for_app_switch();
@@ -349,6 +407,8 @@ class WC_Gateway_Paypal_Buttons_Test extends \WC_Unit_Test_Case {
// Clean up.
wp_delete_post( $post_id, true );
+ wc_get_container()->get( LegacyProxy::class )->reset();
+ remove_filter( 'woocommerce_is_cart', '__return_false' );
}
/**
@@ -360,17 +420,23 @@ class WC_Gateway_Paypal_Buttons_Test extends \WC_Unit_Test_Case {
* @param string $buttons_option The buttons option value ('yes' or 'no').
* @param bool $expected_result The expected result from is_enabled().
* @param string $description Description of the test scenario.
+ * @return void
*/
- public function test_is_enabled_returns_correct_value( $orders_v2_enabled, $buttons_option, $expected_result, $description ) {
+ public function test_is_enabled_returns_correct_value(
+ bool $orders_v2_enabled,
+ string $buttons_option,
+ bool $expected_result,
+ string $description
+ ): void {
// Create a fresh mock gateway for each test scenario.
- $mock_gateway = $this->createMock( WC_Gateway_Paypal::class );
+ $mock_gateway = $this->createMock( \WC_Gateway_Paypal::class );
$mock_gateway->email = 'paypalmerchant@paypal.com';
$mock_gateway->testmode = false;
$mock_gateway->method( 'should_use_orders_v2' )->willReturn( $orders_v2_enabled );
$mock_gateway->method( 'get_option' )->with( 'paypal_buttons', 'yes' )->willReturn( $buttons_option );
- $buttons = new WC_Gateway_Paypal_Buttons( $mock_gateway );
+ $buttons = new PayPalButtons( $mock_gateway );
$this->assertEquals( $expected_result, $buttons->is_enabled(), $description );
}
@@ -380,7 +446,7 @@ class WC_Gateway_Paypal_Buttons_Test extends \WC_Unit_Test_Case {
*
* @return array
*/
- public function provider_is_enabled_scenarios() {
+ public function provider_is_enabled_scenarios(): array {
return array(
'enabled_when_orders_v2_and_buttons_enabled' => array(
'orders_v2_enabled' => true,