From 28e4ce95ef0a9d0e3ca49c85ddca7e3abf5f8bf4 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] 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. */