diff --git a/plugins/woocommerce/changelog/fix-38167_RIM_rules_processor b/plugins/woocommerce/changelog/fix-38167_RIM_rules_processor new file mode 100644 index 00000000000..71810b2bee9 --- /dev/null +++ b/plugins/woocommerce/changelog/fix-38167_RIM_rules_processor @@ -0,0 +1,4 @@ +Significance: minor +Type: fix + +Add "PrepareUrl" transformer to RIM rules processor diff --git a/plugins/woocommerce/src/Admin/RemoteInboxNotifications/ComparisonOperation.php b/plugins/woocommerce/src/Admin/RemoteInboxNotifications/ComparisonOperation.php index 2da4aa28633..8be740a8a10 100644 --- a/plugins/woocommerce/src/Admin/RemoteInboxNotifications/ComparisonOperation.php +++ b/plugins/woocommerce/src/Admin/RemoteInboxNotifications/ComparisonOperation.php @@ -33,9 +33,15 @@ class ComparisonOperation { case '!=': return $left_operand !== $right_operand; case 'contains': - return in_array( $right_operand, $left_operand, true ); + if ( is_array( $left_operand ) && is_string( $right_operand ) ) { + return in_array( $right_operand, $left_operand, true ); + } + return strpos( $right_operand, $left_operand ) !== false; case '!contains': - return ! in_array( $right_operand, $left_operand, true ); + if ( is_array( $left_operand ) && is_string( $right_operand ) ) { + return ! in_array( $right_operand, $left_operand, true ); + } + return strpos( $right_operand, $left_operand ) === false; } return false; diff --git a/plugins/woocommerce/src/Admin/RemoteInboxNotifications/OptionRuleProcessor.php b/plugins/woocommerce/src/Admin/RemoteInboxNotifications/OptionRuleProcessor.php index 3ef30a9b97e..24c0b0f5462 100644 --- a/plugins/woocommerce/src/Admin/RemoteInboxNotifications/OptionRuleProcessor.php +++ b/plugins/woocommerce/src/Admin/RemoteInboxNotifications/OptionRuleProcessor.php @@ -23,9 +23,33 @@ class OptionRuleProcessor implements RuleProcessorInterface { $is_contains = $rule->operation && strpos( $rule->operation, 'contains' ) !== false; $default_value = $is_contains ? array() : false; $default = isset( $rule->default ) ? $rule->default : $default_value; - $option_value = get_option( $rule->option_name, $default ); + $option_value = $this->get_option_value( $rule, $default, $is_contains ); - if ( $is_contains && ! is_array( $option_value ) ) { + if ( isset( $rule->transformers ) && is_array( $rule->transformers ) ) { + $option_value = TransformerService::apply( $option_value, $rule->transformers, $default ); + } + + return ComparisonOperation::compare( + $option_value, + $rule->value, + $rule->operation + ); + } + + /** + * Retrieves the option value and handles logging if necessary. + * + * @param object $rule The specific rule being processed. + * @param mixed $default The default value. + * @param bool $is_contains Indicates whether the operation is "contains". + * + * @return mixed The option value. + */ + private function get_option_value( $rule, $default, $is_contains ) { + $option_value = get_option( $rule->option_name, $default ); + $is_contains_valid = $is_contains && ( is_array( $option_value ) || ( is_string( $option_value ) && is_string( $rule->value ) ) ); + + if ( $is_contains && ! $is_contains_valid ) { $logger = wc_get_logger(); $logger->warning( sprintf( @@ -41,15 +65,7 @@ class OptionRuleProcessor implements RuleProcessorInterface { $option_value = array(); } - if ( isset( $rule->transformers ) && is_array( $rule->transformers ) ) { - $option_value = TransformerService::apply( $option_value, $rule->transformers, $default ); - } - - return ComparisonOperation::compare( - $option_value, - $rule->value, - $rule->operation - ); + return $option_value; } /** diff --git a/plugins/woocommerce/src/Admin/RemoteInboxNotifications/Transformers/PrepareUrl.php b/plugins/woocommerce/src/Admin/RemoteInboxNotifications/Transformers/PrepareUrl.php new file mode 100644 index 00000000000..99ad4076abc --- /dev/null +++ b/plugins/woocommerce/src/Admin/RemoteInboxNotifications/Transformers/PrepareUrl.php @@ -0,0 +1,38 @@ +transform( $url ); + $this->assertEquals( 'www.example.com', $result ); + } + } +} diff --git a/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/option-rule-processor.php b/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/option-rule-processor.php index f31282385a1..788908884c5 100644 --- a/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/option-rule-processor.php +++ b/plugins/woocommerce/tests/legacy/unit-tests/woocommerce-admin/remote-inbox-notifications/option-rule-processor.php @@ -63,14 +63,14 @@ class WC_Admin_Tests_RemoteInboxNotifications_OptionRuleProcessor extends WC_Uni * * @group fast */ - public function test_rule_passes_for_contains() { - add_option( 'contain_item', array( 'test' ) ); + public function test_rule_passes_for_contains_array() { + add_option( 'array_contain_item', array( 'test' ) ); $processor = new OptionRuleProcessor(); $rule = json_decode( ' { "type": "option", - "option_name": "contain_item", + "option_name": "array_contain_item", "value": "test", "default": [], "operation": "contains" @@ -84,7 +84,36 @@ class WC_Admin_Tests_RemoteInboxNotifications_OptionRuleProcessor extends WC_Uni $rule->value = 'not_included'; $result = $processor->process( $rule, null ); $this->assertEquals( false, $result ); - delete_option( 'contain_item' ); + delete_option( 'array_contain_item' ); + } + + /** + * Test contains of substring + * + * @group fast + */ + public function test_rule_passes_for_contains_substring() { + add_option( 'string_contain_substring', array( 'test' ) ); + $processor = new OptionRuleProcessor(); + $rule = json_decode( + ' + { + "type": "option", + "option_name": "string_contain_substring", + "value": "test", + "default": "", + "operation": "contains" + } + ' + ); + + $result = $processor->process( $rule, null ); + $this->assertEquals( true, $result ); + + $rule->value = 'not_included'; + $result = $processor->process( $rule, null ); + $this->assertEquals( false, $result ); + delete_option( 'string_contain_substring' ); } /**