woocommerce/plugins/woocommerce-admin/tests/remote-inbox-notifications/stored-state-rule-processor...

451 lines
11 KiB
PHP
Raw Normal View History

Add stored state between remote inbox notification runs (https://github.com/woocommerce/woocommerce-admin/pull/4260) * Fix comment and convert if..elseif to switch * Take out intermediate function * make ProductsProvider a non-static class * Adds RINDS data rule, and set up for woocommerce/woocommerce-admin#4223 # Conflicts: # src/RemoteInboxNotifications/EvaluateAndGetStatus.php * Fix rebase issues # Conflicts: # src/RemoteInboxNotifications/DataRuleProcessor.php * Split product data setup out, use product query # Conflicts: # src/RemoteInboxNotifications/RemoteInboxNotificationsEngine.php * Remove RINDS references * Remove unused products provider * Add validation to the data rule processor * Fix some issues that were failing tests * Remove unused var * Make data setup for products return the data object * Rework product query * Rename $data to $stored_state * Fix condition in query * Add time after wcadmin activation rule (https://github.com/woocommerce/woocommerce-admin/pull/4337) * Add wcadmin active for rule # Conflicts: # src/RemoteInboxNotifications/GetRuleProcessor.php * Use DAY_IN_SECONDS constant * Fix logic error * Fix test naming Co-authored-by: Rebecca Scott <me@becdetat.com> * Add order count rule (https://github.com/woocommerce/woocommerce-admin/pull/4335) * Order count rule # Conflicts: # src/RemoteInboxNotifications/GetRuleProcessor.php * Use default provider Co-authored-by: Rebecca Scott <me@becdetat.com> * Use correct 'return' value * Move back to using a SQL query to get the product count as the WC_Product_Query has issues during the product publish lifecycle. The product is initially published with a taxonomy term of uncategorized which isn't accepted by the query's filter. * Change back to using WC_Product_Query, and do prelim init via admin_init action * If there are no specs when running the engine, run the poller first then retry * Fix some failing tests * Don't perform init if we're feature toggled off * Move feature check inside RIN engine class because the feature filter isn't ready during FeaturePlugin init Co-authored-by: Rebecca Scott <me@becdetat.com>
2020-06-15 23:42:35 +00:00
<?php
/**
* Stored state rule processor tests.
*
* @package WooCommerce\Tests\RemoteInboxNotifications
*/
use Automattic\WooCommerce\Admin\RemoteInboxNotifications\StoredStateRuleProcessor;
use Automattic\WooCommerce\Admin\RemoteInboxNotifications\RuleEvaluator;
/**
* class WC_Tests_RemoteInboxNotifications_StoredStateRuleProcessor
*/
class WC_Tests_RemoteInboxNotifications_StoredStateRuleProcessor extends WC_Unit_Test_Case {
/**
* Empty $stored_state evaluates to false.
*
* @group fast
*/
public function test_empty_stored_state_evaluates_to_false() {
$processor = new StoredStateRuleProcessor();
$rule = json_decode(
'{
"type": "stored_state",
"index": "there_are_products",
"operation": "=",
"value": true
}'
);
$stored_state = new stdClass();
$result = $processor->process( $rule, $stored_state );
$this->assertEquals( false, $result );
}
/**
* No matching data keys evaluates to false.
*
* @group fast
*/
public function test_no_matching_stored_state_keys_evaluates_to_false() {
$processor = new StoredStateRuleProcessor();
$rule = json_decode(
'{
"type": "stored_state",
"index": "there_are_products",
"operation": "=",
"value": true
}'
);
$stored_state = new stdClass();
$stored_state->non_matching_key = 'test';
$result = $processor->process( $rule, $stored_state );
$this->assertEquals( false, $result );
}
/**
* Unrecognized operator fails
*
* @group fast
*/
public function test_unrecognized_operator_fails() {
$processor = new StoredStateRuleProcessor();
$rule = json_decode(
'{
"type": "stored_state",
"index": "there_are_products",
"operation": "@@@",
"value": true
}'
);
$stored_state = new stdClass();
$stored_state->there_are_products = true;
$result = $processor->process( $rule, $stored_state );
$this->assertEquals( false, $result );
}
/**
* Matching data key and equality operator that fails evaluates to false.
*
* @group fast
*/
public function test_matching_stored_state_key_and_equality_op_that_fails_evaluates_to_false() {
$processor = new StoredStateRuleProcessor();
$rule = json_decode(
'{
"type": "stored_state",
"index": "there_are_products",
"operation": "=",
"value": true
}'
);
$stored_state = new stdClass();
$stored_state->there_are_products = false;
$result = $processor->process( $rule, $stored_state );
$this->assertEquals( false, $result );
}
/**
* Matching data key and equality operator that succeeds evaluates to true.
*
* @group fast
*/
public function test_matching_stored_state_key_and_equality_op_that_succeeds_evaluates_to_true() {
$processor = new StoredStateRuleProcessor();
$rule = json_decode(
'{
"type": "stored_state",
"index": "there_are_products",
"operation": "=",
"value": true
}'
);
$stored_state = new stdClass();
$stored_state->there_are_products = true;
$result = $processor->process( $rule, $stored_state );
$this->assertEquals( true, $result );
}
/**
* Equality operator works with strings in a failing case
*
* @group fast
*/
public function test_equality_op_works_with_strings_in_failing_case_evaluates_to_false() {
$processor = new StoredStateRuleProcessor();
$rule = json_decode(
'{
"type": "stored_state",
"index": "there_are_products",
"operation": "=",
"value": "yes there are"
}'
);
$stored_state = new stdClass();
$stored_state->there_are_products = 'no there is not';
$result = $processor->process( $rule, $stored_state );
$this->assertEquals( false, $result );
}
/**
* Equality operator works with strings in a passing case
*
* @group fast
*/
public function test_equality_op_works_with_strings_in_passing_case_evaluates_to_true() {
$processor = new StoredStateRuleProcessor();
$rule = json_decode(
'{
"type": "stored_state",
"index": "there_are_products",
"operation": "=",
"value": "yes there are"
}'
);
$stored_state = new stdClass();
$stored_state->there_are_products = 'yes there are';
$result = $processor->process( $rule, $stored_state );
$this->assertEquals( true, $result );
}
/**
* Equality operator works with integers
*
* @group fast
*/
public function test_equality_op_works_with_integers() {
$processor = new StoredStateRuleProcessor();
$rule = json_decode(
'{
"type": "stored_state",
"index": "there_are_products",
"operation": "=",
"value": 123
}'
);
$stored_state = new stdClass();
$stored_state->there_are_products = 123;
$result = $processor->process( $rule, $stored_state );
$this->assertEquals( true, $result );
}
/**
* Fails on different types
*
* @group fast
*/
public function test_equality_op_fails_on_different_types() {
$processor = new StoredStateRuleProcessor();
$rule = json_decode(
'{
"type": "stored_state",
"index": "there_are_products",
"operation": "=",
"value": 123
}'
);
$stored_state = new stdClass();
$stored_state->there_are_products = 123.45;
$result = $processor->process( $rule, $stored_state, true );
$this->assertEquals( false, $result );
}
/**
* Fails on failing less than op
*
* @group fast
*/
public function test_less_than_op_fails() {
$processor = new StoredStateRuleProcessor();
$rule = json_decode(
'{
"type": "stored_state",
"index": "product_count",
"operation": "<",
"value": 100
}'
);
$stored_state = new stdClass();
$stored_state->product_count = 120;
$result = $processor->process( $rule, $stored_state, true );
$this->assertEquals( false, $result );
}
/**
* Passes on passing less than op.
*
* @group fast
*/
public function test_less_than_op_passes() {
$processor = new StoredStateRuleProcessor();
$rule = json_decode(
'{
"type": "stored_state",
"index": "product_count",
"operation": "<",
"value": 100
}'
);
$stored_state = new stdClass();
$stored_state->product_count = 80;
$result = $processor->process( $rule, $stored_state, true );
$this->assertEquals( true, $result );
}
/**
* Fails on failing greater than op
*
* @group fast
*/
public function test_greater_than_op_fails() {
$processor = new StoredStateRuleProcessor();
$rule = json_decode(
'{
"type": "stored_state",
"index": "product_count",
"operation": ">",
"value": 100
}'
);
$stored_state = new stdClass();
$stored_state->product_count = 80;
$result = $processor->process( $rule, $stored_state, true );
$this->assertEquals( false, $result );
}
/**
* Passes on passing greater than op.
*
* @group fast
*/
public function test_greater_than_op_passes() {
$processor = new StoredStateRuleProcessor();
$rule = json_decode(
'{
"type": "stored_state",
"index": "product_count",
"operation": ">",
"value": 100
}'
);
$stored_state = new stdClass();
$stored_state->product_count = 120;
$result = $processor->process( $rule, $stored_state, true );
$this->assertEquals( true, $result );
}
/**
* Fails on failing greater or equal than op
*
* @group fast
*/
public function test_greater_than_or_equal_op_fails() {
$processor = new StoredStateRuleProcessor();
$rule = json_decode(
'{
"type": "stored_state",
"index": "product_count",
"operation": ">=",
"value": 100
}'
);
$stored_state = new stdClass();
$stored_state->product_count = 80;
$result = $processor->process( $rule, $stored_state, true );
$this->assertEquals( false, $result );
}
/**
* Passes on passing greater or equal than op.
*
* @group fast
*/
public function test_greater_than_or_equal_op_passes() {
$processor = new StoredStateRuleProcessor();
$rule = json_decode(
'{
"type": "stored_state",
"index": "product_count",
"operation": ">=",
"value": 100
}'
);
$stored_state = new stdClass();
$stored_state->product_count = 100;
$result = $processor->process( $rule, $stored_state, true );
$this->assertEquals( true, $result );
}
/**
* Fails on failing less than or equal than op
*
* @group fast
*/
public function test_less_than_or_equal_op_fails() {
$processor = new StoredStateRuleProcessor();
$rule = json_decode(
'{
"type": "stored_state",
"index": "product_count",
"operation": "<=",
"value": 100
}'
);
$stored_state = new stdClass();
$stored_state->product_count = 120;
$result = $processor->process( $rule, $stored_state, true );
$this->assertEquals( false, $result );
}
/**
* Passes on passing less than or equal than op.
*
* @group fast
*/
public function test_less_than_or_equal_op_passes() {
$processor = new StoredStateRuleProcessor();
$rule = json_decode(
'{
"type": "stored_state",
"index": "product_count",
"operation": "<=",
"value": 100
}'
);
$stored_state = new stdClass();
$stored_state->product_count = 100;
$result = $processor->process( $rule, $stored_state, true );
$this->assertEquals( true, $result );
}
/**
* Fails on failing not equal than op
*
* @group fast
*/
public function test_not_equal_op_fails() {
$processor = new StoredStateRuleProcessor();
$rule = json_decode(
'{
"type": "stored_state",
"index": "product_count",
"operation": "!=",
"value": 100
}'
);
$stored_state = new stdClass();
$stored_state->product_count = 100;
$result = $processor->process( $rule, $stored_state, true );
$this->assertEquals( false, $result );
}
/**
* Passes on passing not equal than op.
*
* @group fast
*/
public function test_not_equal_op_passes() {
$processor = new StoredStateRuleProcessor();
$rule = json_decode(
'{
"type": "stored_state",
"index": "product_count",
"operation": "!=",
"value": 100
}'
);
$stored_state = new stdClass();
$stored_state->product_count = 110;
$result = $processor->process( $rule, $stored_state, true );
$this->assertEquals( true, $result );
}
}