diff --git a/plugins/woocommerce/changelog/update-audit-rule-processors b/plugins/woocommerce/changelog/update-audit-rule-processors new file mode 100644 index 00000000000..2220dbe10b1 --- /dev/null +++ b/plugins/woocommerce/changelog/update-audit-rule-processors @@ -0,0 +1,4 @@ +Significance: patch +Type: dev + +Add checks and unit tests to rule processors diff --git a/plugins/woocommerce/src/Admin/RemoteInboxNotifications/BaseLocationCountryRuleProcessor.php b/plugins/woocommerce/src/Admin/RemoteInboxNotifications/BaseLocationCountryRuleProcessor.php index 941233ae800..366c6699885 100644 --- a/plugins/woocommerce/src/Admin/RemoteInboxNotifications/BaseLocationCountryRuleProcessor.php +++ b/plugins/woocommerce/src/Admin/RemoteInboxNotifications/BaseLocationCountryRuleProcessor.php @@ -25,7 +25,11 @@ class BaseLocationCountryRuleProcessor implements RuleProcessorInterface { */ public function process( $rule, $stored_state ) { $base_location = wc_get_base_location(); - if ( ! $base_location ) { + if ( + ! is_array( $base_location ) || + ! array_key_exists( 'country', $base_location ) || + ! array_key_exists( 'state', $base_location ) + ) { return false; } diff --git a/plugins/woocommerce/src/Admin/RemoteInboxNotifications/BaseLocationStateRuleProcessor.php b/plugins/woocommerce/src/Admin/RemoteInboxNotifications/BaseLocationStateRuleProcessor.php index f2f70c3c4ab..52d7751b029 100644 --- a/plugins/woocommerce/src/Admin/RemoteInboxNotifications/BaseLocationStateRuleProcessor.php +++ b/plugins/woocommerce/src/Admin/RemoteInboxNotifications/BaseLocationStateRuleProcessor.php @@ -23,7 +23,7 @@ class BaseLocationStateRuleProcessor implements RuleProcessorInterface { */ public function process( $rule, $stored_state ) { $base_location = wc_get_base_location(); - if ( ! $base_location ) { + if ( ! is_array( $base_location ) || ! array_key_exists( 'state', $base_location ) ) { return false; } diff --git a/plugins/woocommerce/src/Admin/RemoteInboxNotifications/OnboardingProfileRuleProcessor.php b/plugins/woocommerce/src/Admin/RemoteInboxNotifications/OnboardingProfileRuleProcessor.php index e0686094744..b6ec890eb3f 100644 --- a/plugins/woocommerce/src/Admin/RemoteInboxNotifications/OnboardingProfileRuleProcessor.php +++ b/plugins/woocommerce/src/Admin/RemoteInboxNotifications/OnboardingProfileRuleProcessor.php @@ -25,7 +25,7 @@ class OnboardingProfileRuleProcessor implements RuleProcessorInterface { public function process( $rule, $stored_state ) { $onboarding_profile = get_option( 'woocommerce_onboarding_profile' ); - if ( empty( $onboarding_profile ) ) { + if ( empty( $onboarding_profile ) || ! is_array( $onboarding_profile ) ) { return false; } diff --git a/plugins/woocommerce/src/Admin/RemoteInboxNotifications/PluginVersionRuleProcessor.php b/plugins/woocommerce/src/Admin/RemoteInboxNotifications/PluginVersionRuleProcessor.php index e619c9d8d21..f564d9d432a 100644 --- a/plugins/woocommerce/src/Admin/RemoteInboxNotifications/PluginVersionRuleProcessor.php +++ b/plugins/woocommerce/src/Admin/RemoteInboxNotifications/PluginVersionRuleProcessor.php @@ -52,7 +52,7 @@ class PluginVersionRuleProcessor implements RuleProcessorInterface { $plugin_data = $this->plugins_provider->get_plugin_data( $rule->plugin ); - if ( ! $plugin_data ) { + if ( ! is_array( $plugin_data ) || ! array_key_exists( 'Version', $plugin_data ) ) { return false; } diff --git a/plugins/woocommerce/src/Admin/RemoteInboxNotifications/PluginsActivatedRuleProcessor.php b/plugins/woocommerce/src/Admin/RemoteInboxNotifications/PluginsActivatedRuleProcessor.php index 2da1698e890..18db3d45152 100644 --- a/plugins/woocommerce/src/Admin/RemoteInboxNotifications/PluginsActivatedRuleProcessor.php +++ b/plugins/woocommerce/src/Admin/RemoteInboxNotifications/PluginsActivatedRuleProcessor.php @@ -48,6 +48,14 @@ class PluginsActivatedRuleProcessor implements RuleProcessorInterface { $active_plugin_slugs = $this->plugins_provider->get_active_plugin_slugs(); foreach ( $rule->plugins as $plugin_slug ) { + if ( ! is_string( $plugin_slug ) ) { + $logger = wc_get_logger(); + $logger->warning( + __( 'Invalid plugin slug provided in the plugins activated rule.', 'woocommerce' ) + ); + return false; + } + if ( ! in_array( $plugin_slug, $active_plugin_slugs, true ) ) { return false; } diff --git a/plugins/woocommerce/src/Admin/RemoteInboxNotifications/PublishAfterTimeRuleProcessor.php b/plugins/woocommerce/src/Admin/RemoteInboxNotifications/PublishAfterTimeRuleProcessor.php index 24a0753fabb..75ad931400f 100644 --- a/plugins/woocommerce/src/Admin/RemoteInboxNotifications/PublishAfterTimeRuleProcessor.php +++ b/plugins/woocommerce/src/Admin/RemoteInboxNotifications/PublishAfterTimeRuleProcessor.php @@ -56,6 +56,12 @@ class PublishAfterTimeRuleProcessor implements RuleProcessorInterface { return false; } + try { + new \DateTime( $rule->publish_after ); + } catch ( \Throwable $e ) { + return false; + } + return true; } } diff --git a/plugins/woocommerce/src/Admin/RemoteInboxNotifications/PublishBeforeTimeRuleProcessor.php b/plugins/woocommerce/src/Admin/RemoteInboxNotifications/PublishBeforeTimeRuleProcessor.php index 77f11220483..9cde77c98b0 100644 --- a/plugins/woocommerce/src/Admin/RemoteInboxNotifications/PublishBeforeTimeRuleProcessor.php +++ b/plugins/woocommerce/src/Admin/RemoteInboxNotifications/PublishBeforeTimeRuleProcessor.php @@ -56,6 +56,12 @@ class PublishBeforeTimeRuleProcessor implements RuleProcessorInterface { return false; } + try { + new \DateTime( $rule->publish_before ); + } catch ( \Throwable $e ) { + return false; + } + return true; } } diff --git a/plugins/woocommerce/src/Admin/RemoteInboxNotifications/TotalPaymentsVolumeProcessor.php b/plugins/woocommerce/src/Admin/RemoteInboxNotifications/TotalPaymentsVolumeProcessor.php index 13d103b1973..03f14eac586 100644 --- a/plugins/woocommerce/src/Admin/RemoteInboxNotifications/TotalPaymentsVolumeProcessor.php +++ b/plugins/woocommerce/src/Admin/RemoteInboxNotifications/TotalPaymentsVolumeProcessor.php @@ -23,17 +23,22 @@ class TotalPaymentsVolumeProcessor implements RuleProcessorInterface { * @return bool The result of the operation. */ public function process( $rule, $stored_state ) { - $dates = TimeInterval::get_timeframe_dates( $rule->timeframe ); - $reports_revenue = new RevenueQuery( + $dates = TimeInterval::get_timeframe_dates( $rule->timeframe ); + $reports_revenue = $this->get_reports_query( array( - 'before' => $dates['end'], - 'after' => $dates['start'], + 'before' => $dates['end'], + 'after' => $dates['start'], 'interval' => 'year', - 'fields' => array( 'total_sales' ), + 'fields' => array( 'total_sales' ), ) ); - $report_data = $reports_revenue->get_data(); - $value = $report_data->totals->total_sales; + $report_data = $reports_revenue->get_data(); + + if ( ! $report_data || ! isset( $report_data->totals->total_sales ) ) { + return false; + } + + $value = $report_data->totals->total_sales; return ComparisonOperation::compare( $value, @@ -62,7 +67,7 @@ class TotalPaymentsVolumeProcessor implements RuleProcessorInterface { return false; } - if ( ! isset( $rule->value ) ) { + if ( ! isset( $rule->value ) || ! is_numeric( $rule->value ) ) { return false; } @@ -72,4 +77,17 @@ class TotalPaymentsVolumeProcessor implements RuleProcessorInterface { return true; } + + /** + * Get the report query. + * + * @param array $args The query args. + * + * @return RevenueQuery The report query. + */ + protected function get_reports_query( $args ) { + return new RevenueQuery( + $args + ); + } } diff --git a/plugins/woocommerce/src/Admin/RemoteInboxNotifications/WCAdminActiveForRuleProcessor.php b/plugins/woocommerce/src/Admin/RemoteInboxNotifications/WCAdminActiveForRuleProcessor.php index 4476d86b667..afb6195702c 100644 --- a/plugins/woocommerce/src/Admin/RemoteInboxNotifications/WCAdminActiveForRuleProcessor.php +++ b/plugins/woocommerce/src/Admin/RemoteInboxNotifications/WCAdminActiveForRuleProcessor.php @@ -43,7 +43,12 @@ class WCAdminActiveForRuleProcessor implements RuleProcessorInterface { */ public function process( $rule, $stored_state ) { $active_for_seconds = $this->wcadmin_active_for_provider->get_wcadmin_active_for_in_seconds(); - $rule_seconds = $rule->days * DAY_IN_SECONDS; + + if ( ! $active_for_seconds || ! is_numeric( $active_for_seconds ) || $active_for_seconds < 0 ) { + return false; + } + + $rule_seconds = $rule->days * DAY_IN_SECONDS; return ComparisonOperation::compare( $active_for_seconds, @@ -60,7 +65,8 @@ class WCAdminActiveForRuleProcessor implements RuleProcessorInterface { * @return bool Pass/fail. */ public function validate( $rule ) { - if ( ! isset( $rule->days ) ) { + // Ensure that 'days' property is set and is a valid numeric value. + if ( ! isset( $rule->days ) || ! is_numeric( $rule->days ) || $rule->days < 0 ) { return false; } diff --git a/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/base-location-country-rule-processor.php b/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/base-location-country-rule-processor.php index d38cf1d00ba..ded865a4e39 100644 --- a/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/base-location-country-rule-processor.php +++ b/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/base-location-country-rule-processor.php @@ -9,7 +9,7 @@ use Automattic\WooCommerce\Admin\RemoteInboxNotifications\BaseLocationCountryRul use Automattic\WooCommerce\Internal\Admin\Onboarding\OnboardingProfile; /** - * class WC_Admin_Tests_RemoteInboxNotifications_PublishBeforeTimeRuleProcessor + * class WC_Admin_Tests_RemoteInboxNotifications_BaseLocationCountryRuleProcessor */ class WC_Admin_Tests_RemoteInboxNotifications_BaseLocationCountryRuleProcessor extends WC_Unit_Test_Case { /** diff --git a/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/base-location-state-rule-processor.php b/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/base-location-state-rule-processor.php new file mode 100644 index 00000000000..04fdd11f310 --- /dev/null +++ b/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/base-location-state-rule-processor.php @@ -0,0 +1,68 @@ +process( $this->get_rule(), new stdClass() ); + + $this->assertEquals( false, $result ); + } + + /** + * Tests that the processor returns true if location is the same. + * + * @group fast + */ + public function test_spec_passes_if_location_is_the_same() { + update_option( 'woocommerce_default_country', 'US:CA' ); + + $processor = new BaseLocationStateRuleProcessor(); + $result = $processor->process( $this->get_rule(), new stdClass() ); + + $this->assertEquals( true, $result ); + } +} diff --git a/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/is-woo-express-rule-processor.php b/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/is-woo-express-rule-processor.php new file mode 100644 index 00000000000..0f1f52f7ea5 --- /dev/null +++ b/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/is-woo-express-rule-processor.php @@ -0,0 +1,112 @@ +process( $this->get_rule(), new stdClass() ); + + $this->assertEquals( true, $result ); + } + + /** + * Test that the processor returns false if the site is not on a Woo Express plan. + * @group fast + */ + public function test_is_not_woo_express_plan() { + add_filter( 'test_wc_calypso_bridge_is_woo_express_plan', '__return_false' ); + + $processor = new IsWooExpressRuleProcessor(); + $result = $processor->process( $this->get_rule(), new stdClass() ); + + $this->assertEquals( false, $result ); + } + + /** + * Test that the processor returns false if plan name is not defined. + * @group fast + */ + public function test_invalid_plan_name() { + $rule = (object) array( + 'type' => 'is_woo_express', + 'value' => true, + 'plan' => 'invalid_plan', + ); + + $processor = new IsWooExpressRuleProcessor(); + $result = $processor->process( $rule, new stdClass() ); + + $this->assertEquals( false, $result ); + } + + /** + * Test that the processor returns true if it's a trial plan. + * @group fast + */ + public function test_is_trial_plan() { + /** Fake function wc_calypso_bridge_is_woo_express_trial_plan. */ + function wc_calypso_bridge_is_woo_express_trial_plan() { + return true; + } + + $processor = new IsWooExpressRuleProcessor(); + + $rule = (object) array( + 'type' => 'is_woo_express', + 'value' => true, + 'plan' => 'trial', + ); + + $result = $processor->process( $rule, new stdClass() ); + $this->assertEquals( true, $result ); + } +} diff --git a/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/onboarding-profile-rule-processor.php b/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/onboarding-profile-rule-processor.php new file mode 100644 index 00000000000..576abb10b6f --- /dev/null +++ b/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/onboarding-profile-rule-processor.php @@ -0,0 +1,78 @@ +process( $this->get_rule(), new stdClass() ); + + $this->assertEquals( false, $result ); + } + + /** + * Tests that the processor returns false if onboarding profile is not an array. + * + * @group fast + */ + public function test_spec_fails_if_on_boarding_profile_is_not_an_array() { + update_option( OnboardingProfile::DATA_OPTION, 'not an array' ); + + $processor = new OnboardingProfileRuleProcessor(); + $result = $processor->process( $this->get_rule(), new stdClass() ); + + $this->assertEquals( false, $result ); + } + + /** + * Tests that the processor returns true if the criteria is not met. + * + * @group fast + */ + public function test_spec_passes_if_criteria_is_met() { + update_option( OnboardingProfile::DATA_OPTION, array( 'business_choice' => 'im_already_selling' ) ); + + $processor = new OnboardingProfileRuleProcessor(); + $result = $processor->process( $this->get_rule(), new stdClass() ); + + $this->assertEquals( true, $result ); + } +} diff --git a/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/plugin-version-rule-processor.php b/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/plugin-version-rule-processor.php index 7ae0573213b..fdf687b69d1 100644 --- a/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/plugin-version-rule-processor.php +++ b/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/plugin-version-rule-processor.php @@ -60,6 +60,35 @@ class WC_Admin_Tests_RemoteInboxNotifications_PluginVersionRuleProcessor extends $this->assertEquals( false, $result ); } + /** + * Test that the processor does not pass if plugin version does not exist in the data. + * @group fast + */ + public function test_spec_does_not_pass_if_plugin_version_does_not_exist() { + $mock_plugins_provider = new MockPluginsProvider( + array( + 'jetpack', + ), + array( + 'jetpack/jetpack.php' => array( + 'name' => 'jetpack', + ), + ) + ); + $processor = new PluginVersionRuleProcessor( $mock_plugins_provider ); + $rule = json_decode( + '{ + "type": "plugin_version", + "plugin": "jetpack", + "version": "1.2.3", + "operator": "=" + }' + ); + + $result = $processor->process( $rule, new stdClass() ); + $this->assertEquals( false, $result ); + } + /** * Test that the processor does not pass if the installed version is less * than the required version. diff --git a/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/plugins-activated-rule-processor.php b/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/plugins-activated-rule-processor.php index ab3e1dbb240..2a32ae241c7 100644 --- a/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/plugins-activated-rule-processor.php +++ b/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/plugins-activated-rule-processor.php @@ -113,6 +113,35 @@ class WC_Admin_Tests_RemoteInboxNotifications_PluginsActivatedRuleProcessor exte $this->assertEquals( false, $result ); } + /** + * Tests that the processor does not pass a plugins_activated rule when + * plugin slug is invalid. + * + * @group fast + */ + public function test_spec_does_not_pass_with_invalid_plugin_slug() { + $mock_plugins_provider = new MockPluginsProvider( + array( + 'plugin-slug-1', + 'plugin-slug-2', + ) + ); + $processor = new PluginsActivatedRuleProcessor( $mock_plugins_provider ); + $rule = json_decode( + '{ + "type": "plugins_activated", + "plugins": [ + {}, + 1 + ] + }' + ); + + $result = $processor->process( $rule, new stdClass() ); + + $this->assertEquals( false, $result ); + } + /** * Tests that the processor passes a plugins_activated rule with both * matching plugins. diff --git a/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/publish-after-time-rule-processor.php b/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/publish-after-time-rule-processor.php index 6ad2d288b5a..7bd30432c63 100644 --- a/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/publish-after-time-rule-processor.php +++ b/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/publish-after-time-rule-processor.php @@ -76,4 +76,23 @@ class WC_Admin_Tests_RemoteInboxNotifications_PublishAfterTimeRuleProcessor exte $this->assertEquals( false, $result ); } + + /** + * Tests that the rule validation fails if publish_after_time is not in a valid date time format. + * + * @group fast + */ + public function test_spec_fails_for_invalid_date_time_format() { + $processor = new PublishAfterTimeRuleProcessor(); + + $rules = json_decode( + '{ + "type": "publish_after_time", + "publish_after": "wrong-format" + }' + ); + $result = $processor->validate( $rules ); + + $this->assertEquals( false, $result ); + } } diff --git a/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/publish-before-time-rule-processor.php b/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/publish-before-time-rule-processor.php index 3c1ead3f214..96d93d6a1a3 100644 --- a/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/publish-before-time-rule-processor.php +++ b/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/publish-before-time-rule-processor.php @@ -76,4 +76,23 @@ class WC_Admin_Tests_RemoteInboxNotifications_PublishBeforeTimeRuleProcessor ext $this->assertEquals( false, $result ); } + + /** + * Tests that the rule validation fails if publish_before_time is not in a valid date time format. + * + * @group fast + */ + public function test_spec_fails_for_invalid_date_time_format() { + $processor = new PublishBeforeTimeRuleProcessor(); + + $rules = json_decode( + '{ + "type": "publish_before_time", + "publish_before": "wrong-format" + }' + ); + $result = $processor->validate( $rules ); + + $this->assertEquals( false, $result ); + } } diff --git a/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/total-payments-volume-processor.php b/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/total-payments-volume-processor.php new file mode 100644 index 00000000000..a3c98ac4870 --- /dev/null +++ b/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/total-payments-volume-processor.php @@ -0,0 +1,137 @@ +getMockBuilder( RevenueQuery::class ) + ->onlyMethods( array( 'get_data' ) ) + ->getMock(); + + $mocked_query->expects( $this->once() ) + ->method( 'get_data' ) + ->willReturn( + (object) array( + 'totals' => (object) array( + 'total_sales' => 1000, + ), + ) + ); + + $mock = $this->getMockBuilder( TotalPaymentsVolumeProcessor::class ) + ->onlyMethods( array( 'get_reports_query' ) ) + ->getMock(); + + $mock->expects( $this->once() ) + ->method( 'get_reports_query' ) + ->willReturn( $mocked_query ); + + $rule = json_decode( + '{ + "type": "total_payments_value", + "operation": "<", + "timeframe": "last_month", + "value": 1000 + }' + ); + + $result = $mock->process( $rule, new stdClass() ); + + $this->assertEquals( false, $result ); + } + + /** + * Less than 1000 total payments volume evaluates to true. + * + * @group fast + */ + public function test_total_payments_volume_less_than_1000_evaluates_to_false() { + $mocked_query = $this->getMockBuilder( RevenueQuery::class ) + ->onlyMethods( array( 'get_data' ) ) + ->getMock(); + + $mocked_query->expects( $this->once() ) + ->method( 'get_data' ) + ->willReturn( + (object) array( + 'totals' => (object) array( + 'total_sales' => 999, + ), + ) + ); + + $mock = $this->getMockBuilder( TotalPaymentsVolumeProcessor::class ) + ->onlyMethods( array( 'get_reports_query' ) ) + ->getMock(); + + $mock->expects( $this->once() ) + ->method( 'get_reports_query' ) + ->willReturn( $mocked_query ); + + $rule = json_decode( + '{ + "type": "total_payments_value", + "operation": "<", + "timeframe": "last_month", + "value": 1000 + }' + ); + + $result = $mock->process( $rule, new stdClass() ); + + $this->assertEquals( true, $result ); + } + + /** + * Invalid report data evaluates to false. + * + * @group fast + */ + public function test_invalid_report_data_evaluates_to_false() { + $mocked_query = $this->getMockBuilder( RevenueQuery::class ) + ->onlyMethods( array( 'get_data' ) ) + ->getMock(); + + $mocked_query->expects( $this->once() ) + ->method( 'get_data' ) + ->willReturn( + (object) array() + ); + + $mock = $this->getMockBuilder( TotalPaymentsVolumeProcessor::class ) + ->onlyMethods( array( 'get_reports_query' ) ) + ->getMock(); + + $mock->expects( $this->once() ) + ->method( 'get_reports_query' ) + ->willReturn( $mocked_query ); + + $rule = json_decode( + '{ + "type": "total_payments_value", + "operation": "<", + "timeframe": "last_month", + "value": 1000 + }' + ); + + $result = $mock->process( $rule, new stdClass() ); + + $this->assertEquals( false, $result ); + } +} diff --git a/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/wcadmin-active-for-rule-processor.php b/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/wcadmin-active-for-rule-processor.php index b8f0463d61b..2b994a1aa79 100644 --- a/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/wcadmin-active-for-rule-processor.php +++ b/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/wcadmin-active-for-rule-processor.php @@ -54,4 +54,115 @@ class WC_Admin_Tests_RemoteInboxNotifications_WCAdminActiveForRuleProcessor exte $this->assertEquals( false, $result ); } + + /** + * Invalid value returns false + * + * @dataProvider data_provider_for_invalid_admin_active_for + * @group fast + * @param mixed $wcadmin_active_for WCAdmin active for value. + * + */ + public function test_admin_active_for_provider_returns_invalid_value( $wcadmin_active_for ) { + $mocked = $this->getMockBuilder( MockWCAdminActiveForProvider::class )->getMock(); + $mocked + ->expects( $this->once() ) + ->method( 'get_wcadmin_active_for_in_seconds' ) + ->willReturn( $wcadmin_active_for ); + + $processor = new WCAdminActiveForRuleProcessor( $mocked ); + $rule = json_decode( + '{ + "type": "wcadmin_active_for", + "operation": ">", + "days": 12 + }' + ); + + $result = $processor->process( $rule, new stdClass() ); + + $this->assertEquals( false, $result ); + } + + /** + * Data provider for invalid admin active for. + */ + public function data_provider_for_invalid_admin_active_for() { + return array( + array( null ), + array( 'invalid' ), + array( array() ), + array( -1 ), + array( '-10' ), + ); + } + + /** + * Valid rule returns true + * + * @group fast + */ + public function test_valid_rule() { + $processor = new WCAdminActiveForRuleProcessor(); + $rule = json_decode( + '{ + "type": "wcadmin_active_for", + "operation": ">", + "days": 12 + }' + ); + + $result = $processor->validate( $rule ); + + $this->assertEquals( true, $result ); + } + + /** + * Invalid rule returns false + * + * @dataProvider data_provider_for_invalid_rule + * @group fast + * @param mixed $rule Rule. + */ + public function test_invalid_rule( $rule ) { + $processor = new WCAdminActiveForRuleProcessor(); + $result = $processor->validate( $rule ); + + $this->assertEquals( false, $result ); + } + + /** + * Data provider for invalid rule + */ + public function data_provider_for_invalid_rule() { + return array( + array( + json_decode( + '{ + "type": "wcadmin_active_for", + "operation": ">", + "days": -1 + }' + ), + ), + array( + json_decode( + '{ + "type": "wcadmin_active_for", + "operation": ">", + "days": null + }' + ), + ), + array( + json_decode( + '{ + "type": "wcadmin_active_for", + "operation": ">", + "days": "wrong type" + }' + ), + ), + ); + } }