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 <me@becdetat.com>
This commit is contained in:
parent
319923f435
commit
61c189f456
|
@ -54,6 +54,8 @@ class GetRuleProcessor {
|
||||||
return new BaseLocationStateRuleProcessor();
|
return new BaseLocationStateRuleProcessor();
|
||||||
case 'note_status':
|
case 'note_status':
|
||||||
return new NoteStatusRuleProcessor();
|
return new NoteStatusRuleProcessor();
|
||||||
|
case 'option':
|
||||||
|
return new OptionRuleProcessor();
|
||||||
}
|
}
|
||||||
|
|
||||||
return new FailRuleProcessor();
|
return new FailRuleProcessor();
|
||||||
|
|
|
@ -0,0 +1,55 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Rule processor that performs a comparison operation against an option value.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Automattic\WooCommerce\Admin\RemoteInboxNotifications;
|
||||||
|
|
||||||
|
defined( 'ABSPATH' ) || exit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rule processor that performs a comparison operation against an option value.
|
||||||
|
*/
|
||||||
|
class OptionRuleProcessor implements RuleProcessorInterface {
|
||||||
|
/**
|
||||||
|
* Performs a comparison operation against the option value.
|
||||||
|
*
|
||||||
|
* @param object $rule The specific rule being processed by this rule processor.
|
||||||
|
* @param object $stored_state Stored state.
|
||||||
|
*
|
||||||
|
* @return bool The result of the operation.
|
||||||
|
*/
|
||||||
|
public function process( $rule, $stored_state ) {
|
||||||
|
$default = isset( $rule->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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -381,3 +381,18 @@ actioned.
|
||||||
"operation": "!="
|
"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.
|
||||||
|
|
|
@ -0,0 +1,60 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Option rule processor tests.
|
||||||
|
*
|
||||||
|
* @package WooCommerce\Admin\Tests\RemoteInboxNotifications
|
||||||
|
*/
|
||||||
|
|
||||||
|
use Automattic\WooCommerce\Admin\RemoteInboxNotifications\OptionRuleProcessor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* class WC_Tests_RemoteInboxNotifications_OptionRuleProcessor
|
||||||
|
*/
|
||||||
|
class WC_Tests_RemoteInboxNotifications_OptionRuleProcessor extends WC_Unit_Test_Case {
|
||||||
|
/**
|
||||||
|
* No default option resolves to false.
|
||||||
|
*
|
||||||
|
* @group fast
|
||||||
|
*/
|
||||||
|
public function test_rule_passes_for_no_default_option() {
|
||||||
|
$processor = new OptionRuleProcessor();
|
||||||
|
$rule = json_decode(
|
||||||
|
'
|
||||||
|
{
|
||||||
|
"type": "option",
|
||||||
|
"option_name": "NON_EXISTENT_OPTION",
|
||||||
|
"value": false,
|
||||||
|
"operation": "="
|
||||||
|
}
|
||||||
|
'
|
||||||
|
);
|
||||||
|
|
||||||
|
$result = $processor->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 );
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue