Skip to content

Commit

Permalink
Add range operator in Remote Inbox Notification (#45201)
Browse files Browse the repository at this point in the history
* Add range rule in Remote Inbox Notification

* Add changefile(s) from automation for the following project(s): woocommerce

* Add range

* Add changefile(s) from automation for the following project(s): packages/php/remote-specs-validation, woocommerce

* Update README.md

* Fix lint errors

* Make sure right-hand is an array with two elemetns -- range

* Update README

* Lint fixes

---------

Co-authored-by: github-actions <github-actions@github.com>
  • Loading branch information
moon0326 and github-actions committed Feb 29, 2024
1 parent 7278965 commit da64238
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 17 deletions.
Expand Up @@ -59,7 +59,8 @@
"contains",
"!contains",
"in",
"!in"
"!in",
"range"
]
},
"rules": {
Expand Down
Expand Up @@ -728,7 +728,8 @@
"contains",
"!contains",
"in",
"!in"
"!in",
"range"
]
},
"rules": {
Expand Down
Expand Up @@ -147,7 +147,8 @@
"contains",
"!contains",
"in",
"!in"
"!in",
"range"
]
},
"rules": {
Expand Down
Expand Up @@ -711,7 +711,8 @@
"contains",
"!contains",
"in",
"!in"
"!in",
"range"
]
},
"rules": {
Expand Down
Expand Up @@ -708,7 +708,8 @@
"contains",
"!contains",
"in",
"!in"
"!in",
"range"
]
},
"rules": {
Expand Down
@@ -0,0 +1,4 @@
Significance: minor
Type: add

Support range operator in Remote Inbox Notification
@@ -1,4 +1,4 @@
{
"type": "string",
"enum": ["=", "<", "<=", ">", ">=", "!=", "contains", "!contains", "in", "!in"]
"enum": ["=", "<", "<=", ">", ">=", "!=", "contains", "!contains", "in", "!in", "range"]
}
4 changes: 4 additions & 0 deletions plugins/woocommerce/changelog/45201-add-44787-add-range-rule
@@ -0,0 +1,4 @@
Significance: minor
Type: add

Support range operator in Remote Inbox Notification
Expand Up @@ -15,7 +15,7 @@ class ComparisonOperation {
* Compare two operands using the specified operation.
*
* @param object $left_operand The left hand operand.
* @param object $right_operand The right hand operand.
* @param object $right_operand The right hand operand -- 'value' from the rule definition.
* @param string $operation The operation used to compare the operands.
*/
public static function compare( $left_operand, $right_operand, $operation ) {
Expand Down Expand Up @@ -64,6 +64,11 @@ public static function compare( $left_operand, $right_operand, $operation ) {
return strpos( $left_operand, $right_operand ) === false;
}
break;
case 'range':
if ( ! is_array( $right_operand ) || count( $right_operand ) !== 2 ) {
return false;
}
return $left_operand >= $right_operand[0] && $left_operand <= $right_operand[1];
}

return false;
Expand Down
42 changes: 32 additions & 10 deletions plugins/woocommerce/src/Admin/RemoteInboxNotifications/README.md
Expand Up @@ -143,16 +143,19 @@ Rules in an array are executed as an AND operation. If there are no rules in the
Some rule types support an `operation` value, which is used to compare two
values. The following operations are implemented:

- `=`
- `<`
- `<=`
- `>`
- `>=`
- `!=`
- `contains`
- `!contains`
- `in` (Added in WooCommerce 8.2.0)
- `!in` (Added in WooCommerce 8.2.0)
- `=`
- `<`
- `<=`
- `>`
- `>=`
- `!=`
- `contains`
- `!contains`
- `in` (Added in WooCommerce 8.2.0)
- `!in` (Added in WooCommerce 8.2.0)
- `range` (Added in WooCommerce 8.8.0)

#### contains and !contains

`contains` and `!contains` allow checking if the provided value is present (or
not present) in the haystack value. An example of this is using the
Expand All @@ -169,6 +172,8 @@ onboarding profile:
}
```

#### in and !in

`in` and `!in` allow checking if a value is found (or not found) in a provided array. For example, using the `in` comparison operator to check if the base country location value is found in a given array, as below. This rule matches if the `base_location_country` is `US`, `NZ`, or `ZA`. **NOTE:** These comparisons were added in **WooCommerce 8.2.0**. If the spec is read by an older version of WooCommerce, the rule will evaluate to `false`.

```json
Expand All @@ -183,6 +188,23 @@ onboarding profile:
}
```

#### range

`range` operator performs an inclusive check to determine if a number falls within a certain range.
This means that both the 'from' and 'to' values of the specified range are included in the check.

The following rule returns true when `woocommerce_remote_variant_assignment` value is between 1 and 10.

```json
{
"type": "option",
"value": [ 1, 10 ],
"default": 0,
"operation": "range",
"option_name": "woocommerce_remote_variant_assignment",
}
```

### Plugins activated

This passes if all of the listed plugins are installed and activated.
Expand Down
@@ -0,0 +1,85 @@
<?php
/**
* ComparisonOperation Tests
*
* @package WooCommerce\Admin\Tests\RemoteInboxNotification
*/

use Automattic\WooCommerce\Admin\RemoteInboxNotifications\ComparisonOperation;

/**
* class WC_Admin_Tests_RemoteInboxNotifications_Comparison_Operation
*/
class WC_Admin_Tests_RemoteInboxNotifications_Comparison_Operation extends WC_Unit_Test_Case {
/**
* @var ComparisonOperation $operation
*/
private $operation;

/**
* setUp
* @return void
*/
public function setUp(): void {
parent::setUp();
$this->operation = new ComparisonOperation();
}

/**
* Test range
*
* @group fast
* @return void
*/
public function test_range() {
$this->assertTrue( $this->operation->compare( 1, array( 1, 10 ), 'range' ) );
$this->assertFalse( $this->operation->compare( 11, array( 1, 10 ), 'range' ) );
$this->assertFalse( $this->operation->compare( 11, array( 1, 10, 2 ), 'range' ) );
$this->assertFalse( $this->operation->compare( 11, 'string', 'range' ) );

}

/**
* Test contains
*
* @group fast
* @return void
*/
public function test_contains() {
$this->assertTrue( $this->operation->compare( array( 'test', 'test1' ), 'test', 'contains' ) );
$this->assertFalse( $this->operation->compare( array( 'a', 'b' ), 'test', 'contains' ) );
}

/**
* Test !contains
*
* @group fast
* @return void
*/
public function test_not_contains() {
$this->assertTrue( $this->operation->compare( array( 'test1', 'test2' ), 'test', '!contains' ) );
$this->assertFalse( $this->operation->compare( array( 'test' ), 'test', '!contains' ) );
}

/**
* Test in
*
* @group fast
* @return void
*/
public function test_in() {
$this->assertTrue( $this->operation->compare( 'test', array( 'test', 'test2' ), 'in' ) );
$this->assertFalse( $this->operation->compare( 'test', array( 'test1', 'test2' ), 'in' ) );
}

/**
* Test !in
*
* @group fast
* @return void
*/
public function test_not_in() {
$this->assertTrue( $this->operation->compare( 'test', array( 'test1', 'test2' ), '!in' ) );
$this->assertFalse( $this->operation->compare( 'test', array( 'test', 'test2' ), '!in' ) );
}
}

0 comments on commit da64238

Please sign in to comment.