Add checks and unit tests to rule processors (#44448)

* Add checks and unit tests to rule processors

* Add checks and unit tests to PluginsActivatedRuleProcessor

* Add checks and unit tests to PluginsVersionRuleProcessor

* Add checks and unit tests to PublishAfterTimeRuleProcessor

* Add checks and unit tests to PublishBeforeTimeRuleProcessor and fix PublishAfterTimeRuleProcessor

* Add checks and unit tests to WCAdminActiveForRuleProcessor

* Add unit tests and refactor TotalPaymentsVolumeProcessor

* Fix unit tests

* Add changelog

* Fix unit tests

* Update plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/base-location-state-rule-processor.php

Co-authored-by: Ilyas Foo <foo.ilyas@gmail.com>

* Update plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/wcadmin-active-for-rule-processor.php

Co-authored-by: Ilyas Foo <foo.ilyas@gmail.com>

* Update plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/wcadmin-active-for-rule-processor.php

Co-authored-by: Ilyas Foo <foo.ilyas@gmail.com>

* Update plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/wcadmin-active-for-rule-processor.php

Co-authored-by: Ilyas Foo <foo.ilyas@gmail.com>

* Update plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/wcadmin-active-for-rule-processor.php

Co-authored-by: Ilyas Foo <foo.ilyas@gmail.com>

* Update plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/total-payments-volume-processor.php

Co-authored-by: Ilyas Foo <foo.ilyas@gmail.com>

* Update plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/is-woo-express-rule-processer.php

Co-authored-by: Ilyas Foo <foo.ilyas@gmail.com>

* Update plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/is-woo-express-rule-processer.php

Co-authored-by: Ilyas Foo <foo.ilyas@gmail.com>

* Use Throwable instead

* Update plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/total-payments-volume-processor.php

Co-authored-by: Ilyas Foo <foo.ilyas@gmail.com>

* Update plugins/woocommerce/src/Admin/RemoteInboxNotifications/BaseLocationCountryRuleProcessor.php

Co-authored-by: RJ <27843274+rjchow@users.noreply.github.com>

* Update plugins/woocommerce/src/Admin/RemoteInboxNotifications/PluginsActivatedRuleProcessor.php

Co-authored-by: RJ <27843274+rjchow@users.noreply.github.com>

* Update plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/plugin-version-rule-processor.php

Co-authored-by: Ilyas Foo <foo.ilyas@gmail.com>

* Update plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/publish-after-time-rule-processor.php

Co-authored-by: Ilyas Foo <foo.ilyas@gmail.com>

* Update plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/publish-before-time-rule-processor.php

Co-authored-by: Ilyas Foo <foo.ilyas@gmail.com>

* Update plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/total-payments-volume-processor.php

Co-authored-by: Ilyas Foo <foo.ilyas@gmail.com>

* Address PR feedback

---------

Co-authored-by: Ilyas Foo <foo.ilyas@gmail.com>
Co-authored-by: RJ <27843274+rjchow@users.noreply.github.com>
This commit is contained in:
Chi-Hsuan Huang 2024-02-16 18:10:15 +08:00 committed by GitHub
parent 0425857b7f
commit eb2b1fef1d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
20 changed files with 669 additions and 15 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: dev
Add checks and unit tests to rule processors

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -56,6 +56,12 @@ class PublishAfterTimeRuleProcessor implements RuleProcessorInterface {
return false;
}
try {
new \DateTime( $rule->publish_after );
} catch ( \Throwable $e ) {
return false;
}
return true;
}
}

View File

@ -56,6 +56,12 @@ class PublishBeforeTimeRuleProcessor implements RuleProcessorInterface {
return false;
}
try {
new \DateTime( $rule->publish_before );
} catch ( \Throwable $e ) {
return false;
}
return true;
}
}

View File

@ -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
);
}
}

View File

@ -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;
}

View File

@ -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 {
/**

View File

@ -0,0 +1,68 @@
<?php
/**
* Base Location state rule processor tests.
*
* @package WooCommerce\Admin\Tests\RemoteInboxNotifications
*/
use Automattic\WooCommerce\Admin\RemoteInboxNotifications\BaseLocationStateRuleProcessor;
use Automattic\WooCommerce\Internal\Admin\Onboarding\OnboardingProfile;
/**
* class WC_Admin_Tests_RemoteInboxNotifications_BaseLocationStateRuleProcessor
*/
class WC_Admin_Tests_RemoteInboxNotifications_BaseLocationStateRuleProcessor extends WC_Unit_Test_Case {
/**
* Get the base_location_state rule.
*
* @return object The rule.
*/
private function get_rule() {
return json_decode(
'{
"type": "base_location_state",
"operation": "=",
"value": "CA"
}'
);
}
/**
* Tear down.
*/
public function tearDown(): void {
parent::tearDown();
update_option( 'woocommerce_store_address', '' );
update_option( 'woocommerce_default_country', 'US:CA' );
update_option( OnboardingProfile::DATA_OPTION, array() );
}
/**
* Tests that the processor returns false if country is not set.
*
* @group fast
*/
public function test_spec_fails_if_country_is_not_set() {
update_option( 'woocommerce_default_country', '' );
$processor = new BaseLocationStateRuleProcessor();
$result = $processor->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 );
}
}

View File

@ -0,0 +1,112 @@
<?php
/**
* Is WooExpress rule processor tests.
*
* @package WooCommerce\Admin\Tests\RemoteInboxNotifications
*/
use Automattic\WooCommerce\Admin\RemoteInboxNotifications\IsWooExpressRuleProcessor;
/**
* class WC_Admin_Tests_RemoteInboxNotifications_IsWooExpressRuleProcessor
*/
class WC_Admin_Tests_RemoteInboxNotifications_IsWooExpressRuleProcessor extends WC_Unit_Test_Case {
/**
* Set Up Before Class.
*/
public static function setUpBeforeClass(): void {
/**
* Fake function wc_calypso_bridge_is_woo_express_plan so that we can test the processor.
*/
function wc_calypso_bridge_is_woo_express_plan() {
return apply_filters( 'test_wc_calypso_bridge_is_woo_express_plan', true ); // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment
}
}
/**
* Tear down.
*/
public function tearDown(): void {
parent::tearDown();
remove_all_filters( 'test_wc_calypso_bridge_is_woo_express_plan' );
}
/**
* Get the is_woo_express rule.
*
* @return object The rule.
*/
private function get_rule() {
return json_decode(
'{
"type": "is_woo_express",
"value": true
}'
);
}
/**
* Test that the processor returns true if the site is on a Woo Express plan.
* @group fast
*/
public function test_is_woo_express_plan() {
add_filter( 'test_wc_calypso_bridge_is_woo_express_plan', '__return_true' );
$processor = new IsWooExpressRuleProcessor();
$result = $processor->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 );
}
}

View File

@ -0,0 +1,78 @@
<?php
/**
* Onboarding profile rule processor tests.
*
* @package WooCommerce\Admin\Tests\RemoteInboxNotifications
*/
use Automattic\WooCommerce\Admin\RemoteInboxNotifications\OnboardingProfileRuleProcessor;
use Automattic\WooCommerce\Internal\Admin\Onboarding\OnboardingProfile;
/**
* class WC_Admin_Tests_RemoteInboxNotifications_OnboardingProfileRuleProcessor
*/
class WC_Admin_Tests_RemoteInboxNotifications_OnboardingProfileRuleProcessor extends WC_Unit_Test_Case {
/**
* Get the publish_before rule.
*
* @return object The rule.
*/
private function get_rule() {
return json_decode(
'{
"type": "onboarding_profile",
"index": "business_choice",
"operation": "=",
"value": "im_already_selling"
}'
);
}
/**
* Tear down.
*/
public function tearDown(): void {
parent::tearDown();
update_option( OnboardingProfile::DATA_OPTION, array() );
}
/**
* Tests that the processor returns false if onboarding profile is empty.
*
* @group fast
*/
public function test_spec_fails_if_on_boarding_profile_is_empty() {
$processor = new OnboardingProfileRuleProcessor();
$result = $processor->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 );
}
}

View File

@ -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.

View File

@ -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.

View File

@ -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 );
}
}

View File

@ -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 );
}
}

View File

@ -0,0 +1,137 @@
<?php
/**
* Total payments volume processor tests.
*
* @package WooCommerce\Admin\Tests\RemoteInboxNotification
*/
use Automattic\WooCommerce\Admin\RemoteInboxNotifications\TotalPaymentsVolumeProcessor;
use Automattic\WooCommerce\Admin\API\Reports\Revenue\Query as RevenueQuery;
/**
* class WC_Admin_Tests_RemoteInboxNotifications_TotalPaymentsVolumeProcessor
*/
class WC_Admin_Tests_RemoteInboxNotifications_TotalPaymentsVolumeProcessor extends WC_Unit_Test_Case {
/**
* Greater than 1000 total payments volume evaluates to false.
*
* @group fast
*/
public function test_total_payments_volume_greater_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' => 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 );
}
}

View File

@ -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"
}'
),
),
);
}
}