Fix RIM rules processor (#38312)
* Fix url comparison. * Add changelog * Fix lint * Fix `process` function * Fix `contains` switch * Create class "PrepareUrl" * Fix "PrepareUrl" * Fix "compare" and "process" * Fix "process" * Add readme and changelog * Refactor OptionRuleProcessor * Fix lint * Fix conditional * Add unit tests * Fix lint
This commit is contained in:
parent
340d74cdba
commit
5e3fe64598
|
@ -0,0 +1,4 @@
|
|||
Significance: minor
|
||||
Type: fix
|
||||
|
||||
Add "PrepareUrl" transformer to RIM rules processor
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
|
||||
namespace Automattic\WooCommerce\Admin\RemoteInboxNotifications\Transformers;
|
||||
|
||||
use Automattic\WooCommerce\Admin\RemoteInboxNotifications\TransformerInterface;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* Prepare site URL for comparison.
|
||||
*
|
||||
* @package Automattic\WooCommerce\Admin\RemoteInboxNotifications\Transformers
|
||||
*/
|
||||
class PrepareUrl implements TransformerInterface {
|
||||
/**
|
||||
* Prepares the site URL by removing the protocol and trailing slash.
|
||||
*
|
||||
* @param mixed $value a value to transform.
|
||||
* @param stdClass|null $arguments arguments.
|
||||
* @param string|null $default default value.
|
||||
*
|
||||
* @return mixed|null
|
||||
*/
|
||||
public function transform( $value, stdClass $arguments = null, $default = null ) {
|
||||
$url_parts = wp_parse_url( rtrim( $value, '/' ) );
|
||||
return isset( $url_parts['path'] ) ? $url_parts['host'] . $url_parts['path'] : $url_parts['host'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate Transformer arguments.
|
||||
*
|
||||
* @param stdClass|null $arguments arguments to validate.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function validate( stdClass $arguments = null ) {
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -365,4 +365,38 @@ Let's count # of users with `count`
|
|||
|
||||
**Output:** 3
|
||||
|
||||
## prepare_url
|
||||
|
||||
This prepares the site URL by removing the protocol and the last slash.
|
||||
|
||||
#### Arguments: N/A
|
||||
|
||||
####Definition:
|
||||
|
||||
```php
|
||||
"transformers": [
|
||||
{
|
||||
"use": "prepare_url"
|
||||
}
|
||||
],
|
||||
```
|
||||
|
||||
#### Example:
|
||||
|
||||
Given the following data
|
||||
|
||||
```php
|
||||
$siteurl = "https://mysite.com/"
|
||||
```
|
||||
|
||||
Removes the protocol and the last slash.
|
||||
|
||||
```php
|
||||
"transformers": [
|
||||
{
|
||||
"use": "prepare_url",
|
||||
}
|
||||
],
|
||||
```
|
||||
|
||||
**Output:** "mysite.com"
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
/**
|
||||
* PrepareUrl tests.
|
||||
*
|
||||
* @package WooCommerce\Admin\Tests\RemoteInboxNotifications
|
||||
*/
|
||||
|
||||
use Automattic\WooCommerce\Admin\RemoteInboxNotifications\Transformers\PrepareUrl;
|
||||
|
||||
/**
|
||||
* class WC_Admin_Tests_RemoteInboxNotifications_Transformers_PrepareUrl
|
||||
*/
|
||||
class WC_Admin_Tests_RemoteInboxNotifications_Transformers_PrepareUrl extends WC_Unit_Test_Case {
|
||||
/**
|
||||
* Test it returns url without protocol and trailing slash.
|
||||
*/
|
||||
public function test_it_returns_flatten_array() {
|
||||
$urls = array(
|
||||
'https://www.example.com',
|
||||
'https://www.example.com/',
|
||||
'http://www.example.com',
|
||||
'http://www.example.com/',
|
||||
'test://www.example.com/',
|
||||
);
|
||||
|
||||
$prepare_url = new PrepareUrl();
|
||||
|
||||
foreach ( $urls as $url ) {
|
||||
$result = $prepare_url->transform( $url );
|
||||
$this->assertEquals( 'www.example.com', $result );
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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' );
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue