From 61c189f4567781675b1ddd06f006b89d45ef6f4a Mon Sep 17 00:00:00 2001 From: Bec Scott Date: Tue, 13 Oct 2020 08:47:12 +1000 Subject: [PATCH] Add option remote inbox notifications rule processor (https://github.com/woocommerce/woocommerce-admin/pull/5206) * Add option remote inbox notifications rule processor * Add default option value to option rule Co-authored-by: Rebecca Scott --- .../GetRuleProcessor.php | 2 + .../OptionRuleProcessor.php | 55 +++++++++++++++++ .../src/RemoteInboxNotifications/README.md | 15 +++++ .../option-rule-processor.php | 60 +++++++++++++++++++ 4 files changed, 132 insertions(+) create mode 100644 plugins/woocommerce-admin/src/RemoteInboxNotifications/OptionRuleProcessor.php create mode 100644 plugins/woocommerce-admin/tests/remote-inbox-notifications/option-rule-processor.php diff --git a/plugins/woocommerce-admin/src/RemoteInboxNotifications/GetRuleProcessor.php b/plugins/woocommerce-admin/src/RemoteInboxNotifications/GetRuleProcessor.php index d858f275e12..e81179d522a 100644 --- a/plugins/woocommerce-admin/src/RemoteInboxNotifications/GetRuleProcessor.php +++ b/plugins/woocommerce-admin/src/RemoteInboxNotifications/GetRuleProcessor.php @@ -54,6 +54,8 @@ class GetRuleProcessor { return new BaseLocationStateRuleProcessor(); case 'note_status': return new NoteStatusRuleProcessor(); + case 'option': + return new OptionRuleProcessor(); } return new FailRuleProcessor(); diff --git a/plugins/woocommerce-admin/src/RemoteInboxNotifications/OptionRuleProcessor.php b/plugins/woocommerce-admin/src/RemoteInboxNotifications/OptionRuleProcessor.php new file mode 100644 index 00000000000..b9c6e3b9b4a --- /dev/null +++ b/plugins/woocommerce-admin/src/RemoteInboxNotifications/OptionRuleProcessor.php @@ -0,0 +1,55 @@ +default ) ? $rule->default : false; + $option_value = get_option( $rule->option_name, $default ); + + return ComparisonOperation::compare( + $option_value, + $rule->value, + $rule->operation + ); + } + + /** + * Validates the rule. + * + * @param object $rule The rule to validate. + * + * @return bool Pass/fail. + */ + public function validate( $rule ) { + if ( ! isset( $rule->option_name ) ) { + return false; + } + + if ( ! isset( $rule->value ) ) { + return false; + } + + if ( ! isset( $rule->operation ) ) { + return false; + } + + return true; + } +} diff --git a/plugins/woocommerce-admin/src/RemoteInboxNotifications/README.md b/plugins/woocommerce-admin/src/RemoteInboxNotifications/README.md index d28b58ced78..4f85e3a05f3 100644 --- a/plugins/woocommerce-admin/src/RemoteInboxNotifications/README.md +++ b/plugins/woocommerce-admin/src/RemoteInboxNotifications/README.md @@ -381,3 +381,18 @@ actioned. "operation": "!=" } ``` + +### Option +This passes when the option value matches the value using the operation. + +``` +{ + "type": "option", + "option_name": "woocommerce_currency", + "value": "USD", + "default": "USD", + "operation": "=" +} +``` + +`option_name`, `value`, and `operation` are all required. `default` is not required and allows a default value to be used if the option does not exist. diff --git a/plugins/woocommerce-admin/tests/remote-inbox-notifications/option-rule-processor.php b/plugins/woocommerce-admin/tests/remote-inbox-notifications/option-rule-processor.php new file mode 100644 index 00000000000..84a368ee837 --- /dev/null +++ b/plugins/woocommerce-admin/tests/remote-inbox-notifications/option-rule-processor.php @@ -0,0 +1,60 @@ +process( $rule, null ); + + $this->assertEquals( true, $result ); + } + + /** + * Default option of true resolves to true. + * + * @group fast + */ + public function test_rule_passes_for_default_option() { + $processor = new OptionRuleProcessor(); + $rule = json_decode( + ' + { + "type": "option", + "option_name": "NON_EXISTENT_OPTION", + "value": true, + "default": true, + "operation": "=" + } + ' + ); + + $result = $processor->process( $rule, null ); + + $this->assertEquals( true, $result ); + } +}