From 18e8e5c1489556a18d02c239134aeb67d98ef6e9 Mon Sep 17 00:00:00 2001 From: Corey McKrill <916023+coreymckrill@users.noreply.github.com> Date: Fri, 23 Jun 2023 15:23:42 -0700 Subject: [PATCH 1/3] WC Tracker: Add unit test for plugin feature compat data In #38849 a change was made that allowed the class mocking necessary for this test to happen in a way that wouldn't interfere with other unit tests. Fixes #38720 --- .../tests/legacy/mockable-functions.php | 1 + .../php/includes/class-wc-tracker-test.php | 94 +++++++++++++++++++ 2 files changed, 95 insertions(+) diff --git a/plugins/woocommerce/tests/legacy/mockable-functions.php b/plugins/woocommerce/tests/legacy/mockable-functions.php index 974619882e0..45ae0aabc25 100644 --- a/plugins/woocommerce/tests/legacy/mockable-functions.php +++ b/plugins/woocommerce/tests/legacy/mockable-functions.php @@ -10,6 +10,7 @@ return array( 'current_user_can', 'get_bloginfo', + 'get_plugins', 'get_woocommerce_currencies', 'get_woocommerce_currency_symbol', 'wc_get_price_excluding_tax', diff --git a/plugins/woocommerce/tests/php/includes/class-wc-tracker-test.php b/plugins/woocommerce/tests/php/includes/class-wc-tracker-test.php index 41da24722d7..10ec24e2771 100644 --- a/plugins/woocommerce/tests/php/includes/class-wc-tracker-test.php +++ b/plugins/woocommerce/tests/php/includes/class-wc-tracker-test.php @@ -5,6 +5,10 @@ * @package WooCommerce\Tests\WC_Tracker. */ +use Automattic\WooCommerce\Internal\Features\FeaturesController; +use Automattic\WooCommerce\Utilities\PluginUtil; +use Automattic\WooCommerce\Testing\Tools\CodeHacking\Hacks\FunctionsMockerHack; + // phpcs:disable Squiz.Classes.ClassFileName.NoMatch, Squiz.Classes.ValidClassName.NotCamelCaps -- Backward compatibility. /** * Class WC_Tracker_Test @@ -68,6 +72,96 @@ class WC_Tracker_Test extends \WC_Unit_Test_Case { $this->assertEquals( 'no', $tracking_data['wc_admin_disabled'] ); } + /** + * @testDox Test the features compatibility data for plugin tracking data. + */ + public function test_get_tracking_data_plugin_feature_compatibility() { + $legacy_mocks = array( + 'get_plugins' => function() { + return array( + 'plugin1' => array( + 'Name' => 'Plugin 1', + ), + 'plugin2' => array( + 'Name' => 'Plugin 2', + ), + 'plugin3' => array( + 'Name' => 'Plugin 3', + ), + ); + }, + ); + FunctionsMockerHack::add_function_mocks( $legacy_mocks ); + + update_option( 'active_plugins', array( 'plugin1', 'plugin2' ) ); + + $pluginutil_mock = new class() extends PluginUtil { + // phpcs:ignore Squiz.Commenting.FunctionComment.Missing + public function is_woocommerce_aware_plugin( $plugin ): bool { + if ( 'plugin1' === $plugin ) { + return false; + } + + return true; + } + }; + + $featurescontroller_mock = new class() extends FeaturesController { + // phpcs:ignore Squiz.Commenting.FunctionComment.Missing + public function get_compatible_features_for_plugin( string $plugin_name, bool $enabled_features_only = false ): array { + $compat = array(); + switch ( $plugin_name ) { + case 'plugin2': + $compat = array( + 'compatible' => array( 'feature1' ), + 'incompatible' => array( 'feature2' ), + 'uncertain' => array( 'feature3' ), + ); + break; + case 'plugin3': + $compat = array( + 'compatible' => array( 'feature2' ), + 'incompatible' => array(), + 'uncertain' => array( 'feature1', 'feature3' ), + ); + break; + } + + return $compat; + } + }; + + $container = wc_get_container(); + $container->get( PluginUtil::class ); // Ensure that the class is loaded. + $container->replace( PluginUtil::class, $pluginutil_mock ); + $container->replace( FeaturesController::class, $featurescontroller_mock ); + + $tracking_data = WC_Tracker::get_tracking_data(); + + $this->assertEquals( + array(), + $tracking_data['active_plugins']['plugin1']['feature_compatibility'] + ); + $this->assertEquals( + array( + 'compatible' => array( 'feature1' ), + 'incompatible' => array( 'feature2' ), + 'uncertain' => array( 'feature3' ), + ), + $tracking_data['active_plugins']['plugin2']['feature_compatibility'] + ); + $this->assertEquals( + array( + 'compatible' => array( 'feature2' ), + 'uncertain' => array( 'feature1', 'feature3' ), + ), + $tracking_data['inactive_plugins']['plugin3']['feature_compatibility'] + ); + + $this->reset_container_replacements(); + $container->reset_all_resolved(); + } + /** * @testDox Test orders tracking data. */ From faa478d61060e7e9e2a2f0b0444399e47b106995 Mon Sep 17 00:00:00 2001 From: Corey McKrill <916023+coreymckrill@users.noreply.github.com> Date: Fri, 23 Jun 2023 15:27:55 -0700 Subject: [PATCH 2/3] Add changelog file --- plugins/woocommerce/changelog/fix-38720-wc-tracker-unit-test | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 plugins/woocommerce/changelog/fix-38720-wc-tracker-unit-test diff --git a/plugins/woocommerce/changelog/fix-38720-wc-tracker-unit-test b/plugins/woocommerce/changelog/fix-38720-wc-tracker-unit-test new file mode 100644 index 00000000000..3838cafe0a4 --- /dev/null +++ b/plugins/woocommerce/changelog/fix-38720-wc-tracker-unit-test @@ -0,0 +1,4 @@ +Significance: minor +Type: dev + +Added a unit test for plugin feature compatibility data in WC Tracker From d513c7c73e10dcc9095a55cce0a7b79e35f77919 Mon Sep 17 00:00:00 2001 From: Corey McKrill <916023+coreymckrill@users.noreply.github.com> Date: Tue, 25 Jul 2023 11:07:52 -0700 Subject: [PATCH 3/3] Use the LegacyProxy to invoke get_plugins, remove CodeHacking usage --- plugins/woocommerce/includes/class-wc-tracker.php | 3 ++- plugins/woocommerce/tests/legacy/mockable-functions.php | 1 - .../woocommerce/tests/php/includes/class-wc-tracker-test.php | 3 +-- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/plugins/woocommerce/includes/class-wc-tracker.php b/plugins/woocommerce/includes/class-wc-tracker.php index f51065c2846..e720012ed8a 100644 --- a/plugins/woocommerce/includes/class-wc-tracker.php +++ b/plugins/woocommerce/includes/class-wc-tracker.php @@ -14,6 +14,7 @@ use Automattic\Jetpack\Constants; use Automattic\WooCommerce\Internal\DataStores\Orders\OrdersTableDataStore; use Automattic\WooCommerce\Utilities\{ FeaturesUtil, OrderUtil, PluginUtil }; use Automattic\WooCommerce\Internal\Utilities\BlocksUtil; +use Automattic\WooCommerce\Proxies\LegacyProxy; defined( 'ABSPATH' ) || exit; @@ -311,7 +312,7 @@ class WC_Tracker { include ABSPATH . '/wp-admin/includes/plugin.php'; } - $plugins = get_plugins(); + $plugins = wc_get_container()->get( LegacyProxy::class )->call_function( 'get_plugins' ); $active_plugins_keys = get_option( 'active_plugins', array() ); $active_plugins = array(); diff --git a/plugins/woocommerce/tests/legacy/mockable-functions.php b/plugins/woocommerce/tests/legacy/mockable-functions.php index 45ae0aabc25..974619882e0 100644 --- a/plugins/woocommerce/tests/legacy/mockable-functions.php +++ b/plugins/woocommerce/tests/legacy/mockable-functions.php @@ -10,7 +10,6 @@ return array( 'current_user_can', 'get_bloginfo', - 'get_plugins', 'get_woocommerce_currencies', 'get_woocommerce_currency_symbol', 'wc_get_price_excluding_tax', diff --git a/plugins/woocommerce/tests/php/includes/class-wc-tracker-test.php b/plugins/woocommerce/tests/php/includes/class-wc-tracker-test.php index 10ec24e2771..42936d5fc74 100644 --- a/plugins/woocommerce/tests/php/includes/class-wc-tracker-test.php +++ b/plugins/woocommerce/tests/php/includes/class-wc-tracker-test.php @@ -7,7 +7,6 @@ use Automattic\WooCommerce\Internal\Features\FeaturesController; use Automattic\WooCommerce\Utilities\PluginUtil; -use Automattic\WooCommerce\Testing\Tools\CodeHacking\Hacks\FunctionsMockerHack; // phpcs:disable Squiz.Classes.ClassFileName.NoMatch, Squiz.Classes.ValidClassName.NotCamelCaps -- Backward compatibility. /** @@ -91,7 +90,7 @@ class WC_Tracker_Test extends \WC_Unit_Test_Case { ); }, ); - FunctionsMockerHack::add_function_mocks( $legacy_mocks ); + $this->register_legacy_proxy_function_mocks( $legacy_mocks ); update_option( 'active_plugins', array( 'plugin1', 'plugin2' ) );