Move tthe date filtering tests to a trait.

Since the new date filtering parameters 'modified_before' and
'modified_after' applies to products, orders and coupons, the tests
for the date filtering have been moved to a trait and included
in the three corresponding test files.
This commit is contained in:
Nestor Soriano 2021-08-26 15:41:57 +02:00
parent 0b84d2d8b8
commit 7cccf62a89
No known key found for this signature in database
GPG Key ID: 08110F3518C12CAD
4 changed files with 176 additions and 84 deletions

View File

@ -1,11 +1,24 @@
<?php
/**
* Tests for Coupons API.
*
* @package WooCommerce\Tests\API
*/
// phpcs:ignore Squiz.Commenting.FileComment.Missing
require __DIR__ . '/date-filtering.php';
/**
* Coupon API Tests
* @package WooCommerce\Tests\API
* @since 3.5.0
*/
class WC_Tests_API_Coupons extends WC_REST_Unit_Test_Case {
use DateFilteringForCrudControllers;
/**
* @var WC_REST_Coupons_Controller
*/
protected $endpoint;
/**
@ -468,4 +481,22 @@ class WC_Tests_API_Coupons extends WC_REST_Unit_Test_Case {
$this->assertArrayHasKey( 'email_restrictions', $properties );
$this->assertArrayHasKey( 'used_by', $properties );
}
/**
* Create an object for the tests in DateFilteringForCrudControllers.
*
* @return object The created object.
*/
private function get_item_for_date_filtering_tests() {
return \Automattic\WooCommerce\RestApi\UnitTests\Helpers\CouponHelper::create_coupon( 'dummycoupon-1' );
}
/**
* Get the REST API endpoint for the tests in DateFilteringForCrudControllers.
*
* @return string REST API endpoint for querying items.
*/
private function get_endpoint_for_date_filtering_tests() {
return '/wc/v3/coupons';
}
}

View File

@ -0,0 +1,111 @@
<?php
/**
* Trait for testing the date filtering on controllers that inherit from WC_REST_CRUD_Controller.
*
* The class using this trait needs to provide two methods:
*
* object get_item_for_date_filtering_tests() --> create (in db) and return an object for testing (with get_id method).
* string get_endpoint_for_date_filtering_tests() --> get the REST API endpoint for querying items.
*
* @package WooCommerce\Tests\API
*/
/**
* Trait for testing the date filtering on controllers that inherit from WC_REST_CRUD_Controller.
*/
trait DateFilteringForCrudControllers {
/**
* @testdox Items can be filtered by creation or modification date, and the specified dates can be GMT or not.
*
* @testWith ["after", false, true]
* ["after", true, false]
* ["before", true, true]
* ["before", false, false]
* ["modified_after", false, true]
* ["modified_after", true, false]
* ["modified_before", true, true]
* ["modified_before", false, false]
*
* @param string $param_name The name of the date parameter to filter by.
* @param bool $filter_by_gmt Whether the dates to filter by are GMT or not.
* @param bool $expected_to_be_returned True if the created item is expected to be included in the response, false otherwise.
*/
public function test_foo_filter_by_creation_or_modification_date( $param_name, $filter_by_gmt, $expected_to_be_returned ) {
global $wpdb;
wp_set_current_user( $this->user );
$item_id = $this->get_item_for_date_filtering_tests()->get_id();
// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared
$wpdb->query(
'UPDATE ' . $wpdb->prefix . "posts SET
post_date = '2000-01-01T12:00:00',
post_date_gmt = '2000-01-01T10:00:00',
post_modified = '2000-02-01T12:00:00',
post_modified_gmt = '2000-02-01T10:00:00'
WHERE ID = " . $item_id
);
// phpcs:enable WordPress.DB.PreparedSQL.NotPrepared
$request = new WP_REST_Request( 'GET', $this->get_endpoint_for_date_filtering_tests() );
$request->set_query_params(
array(
$param_name => false === strpos( $param_name, 'modified' ) ? '2000-01-01T11:00:00' : '2000-02-01T11:00:00',
'dates_are_gmt' => $filter_by_gmt ? 'true' : 'false',
)
);
$response = $this->server->dispatch( $request );
$response_items = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( $expected_to_be_returned ? 1 : 0, count( $response_items ) );
}
/**
* @testdox Items can be filtered by any combination of more than one date (but all must either be GMT or not be it).
*
* @testWith ["after", "2000-01-01T11:59:59", "modified_before", "2000-02-01T12:00:01", false, true]
* ["after", "2000-01-01T11:59:59", "modified_before", "2000-02-01T11:59:59", false, false]
* ["before", "2000-01-01T10:00:01", "modified_after", "2000-02-01T09:59:59", true, true]
* ["before", "2000-01-01T09:59:59", "modified_after", "2000-02-01T09:59:59", true, false]
*
* @param string $first_param_name Name of the first parameter to filter by.
* @param string $first_param_value Value of the first parameter to filter by.
* @param string $second_param_name Name of the second parameter to filter by.
* @param string $second_param_value Value of the second parameter to filter by.
* @param bool $filter_by_gmt Whether the dates to filter by are GMT or not.
* @param bool $expected_to_be_returned True if the created item is expected to be included in the response, false otherwise.
*/
public function test_foo_can_filter_by_more_than_one_date( $first_param_name, $first_param_value, $second_param_name, $second_param_value, $filter_by_gmt, $expected_to_be_returned ) {
global $wpdb;
wp_set_current_user( $this->user );
$item_id = $this->get_item_for_date_filtering_tests()->get_id();
// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared
$wpdb->query(
'UPDATE ' . $wpdb->prefix . "posts SET
post_date = '2000-01-01T12:00:00',
post_date_gmt = '2000-01-01T10:00:00',
post_modified = '2000-02-01T12:00:00',
post_modified_gmt = '2000-02-01T10:00:00'
WHERE ID = " . $item_id
);
// phpcs:enable WordPress.DB.PreparedSQL.NotPrepared
$request = new WP_REST_Request( 'GET', $this->get_endpoint_for_date_filtering_tests() );
$request->set_query_params(
array(
$first_param_name => $first_param_value,
$second_param_name => $second_param_value,
'dates_are_gmt' => $filter_by_gmt ? 'true' : 'false',
)
);
$response = $this->server->dispatch( $request );
$response_items = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( $expected_to_be_returned ? 1 : 0, count( $response_items ) );
}
}

View File

@ -6,6 +6,9 @@
* @since 3.5.0
*/
// phpcs:ignore Squiz.Commenting.FileComment.Missing
require __DIR__ . '/date-filtering.php';
use Automattic\WooCommerce\RestApi\UnitTests\Helpers\CouponHelper;
use Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper;
@ -14,6 +17,7 @@ use Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper;
*/
class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
use WC_REST_API_Complex_Meta;
use DateFilteringForCrudControllers;
/**
* Array of order to track
@ -1160,4 +1164,22 @@ class WC_Tests_API_Orders extends WC_REST_Unit_Test_Case {
$this->assertEquals( 5, count( $meta_data_item_properties ) );
$this->assertEquals( array( 'id', 'key', 'value', 'display_key', 'display_value' ), array_keys( $meta_data_item_properties ) );
}
/**
* Create an object for the tests in DateFilteringForCrudControllers.
*
* @return object The created object.
*/
private function get_item_for_date_filtering_tests() {
return OrderHelper::create_order();
}
/**
* Get the REST API endpoint for the tests in DateFilteringForCrudControllers.
*
* @return string REST API endpoint for querying items.
*/
private function get_endpoint_for_date_filtering_tests() {
return '/wc/v3/orders';
}
}

View File

@ -6,11 +6,15 @@
* @since 3.5.0
*/
// phpcs:ignore Squiz.Commenting.FileComment.Missing
require __DIR__ . '/date-filtering.php';
/**
* WC_Tests_API_Product class.
*/
class WC_Tests_API_Product extends WC_REST_Unit_Test_Case {
use WC_REST_API_Complex_Meta;
use DateFilteringForCrudControllers;
/**
* Setup our test server, endpoints, and user info.
@ -910,96 +914,20 @@ class WC_Tests_API_Product extends WC_REST_Unit_Test_Case {
}
/**
* @testdox Products can be filtered by creation or modification date, and the specified dates can be GMT or not.
* Create an object for the tests in DateFilteringForCrudControllers.
*
* @testWith ["after", false, true]
* ["after", true, false]
* ["before", true, true]
* ["before", false, false]
* ["modified_after", false, true]
* ["modified_after", true, false]
* ["modified_before", true, true]
* ["modified_before", false, false]
*
* @param string $param_name The name of the date parameter to filter by.
* @param bool $filter_by_gmt Whether the dates to filter by are GMT or not.
* @param bool $expected_to_be_returned True if the created product is expected to be included in the response, false otherwise.
* @return object The created object.
*/
public function test_filter_by_creation_or_modification_date( $param_name, $filter_by_gmt, $expected_to_be_returned ) {
global $wpdb;
wp_set_current_user( $this->user );
$product_id = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_simple_product()->get_id();
// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared
$wpdb->query(
'UPDATE ' . $wpdb->prefix . "posts SET
post_date = '2000-01-01T12:00:00',
post_date_gmt = '2000-01-01T10:00:00',
post_modified = '2000-02-01T12:00:00',
post_modified_gmt = '2000-02-01T10:00:00'
WHERE ID = " . $product_id
);
// phpcs:enable WordPress.DB.PreparedSQL.NotPrepared
$request = new WP_REST_Request( 'GET', '/wc/v3/products' );
$request->set_query_params(
array(
$param_name => false === strpos( $param_name, 'modified' ) ? '2000-01-01T11:00:00' : '2000-02-01T11:00:00',
'dates_are_gmt' => $filter_by_gmt ? 'true' : 'false',
)
);
$response = $this->server->dispatch( $request );
$response_products = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( $expected_to_be_returned ? 1 : 0, count( $response_products ) );
private function get_item_for_date_filtering_tests() {
return \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_simple_product();
}
/**
* @testdox Products can be filtered by any combination of more than one date (but all must either be GMT or not be it).
* Get the REST API endpoint for the tests in DateFilteringForCrudControllers.
*
* @testWith ["after", "2000-01-01T11:59:59", "modified_before", "2000-02-01T12:00:01", false, true]
* ["after", "2000-01-01T11:59:59", "modified_before", "2000-02-01T11:59:59", false, false]
* ["before", "2000-01-01T10:00:01", "modified_after", "2000-02-01T09:59:59", true, true]
* ["before", "2000-01-01T09:59:59", "modified_after", "2000-02-01T09:59:59", true, false]
*
* @param string $first_param_name Name of the first parameter to filter by.
* @param string $first_param_value Value of the first parameter to filter by.
* @param string $second_param_name Name of the second parameter to filter by.
* @param string $second_param_value Value of the second parameter to filter by.
* @param bool $filter_by_gmt Whether the dates to filter by are GMT or not.
* @param bool $expected_to_be_returned True if the created product is expected to be included in the response, false otherwise.
* @return string REST API endpoint for querying items.
*/
public function test_can_filter_by_more_than_one_date( $first_param_name, $first_param_value, $second_param_name, $second_param_value, $filter_by_gmt, $expected_to_be_returned ) {
global $wpdb;
wp_set_current_user( $this->user );
$product_id = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_simple_product()->get_id();
// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared
$wpdb->query(
'UPDATE ' . $wpdb->prefix . "posts SET
post_date = '2000-01-01T12:00:00',
post_date_gmt = '2000-01-01T10:00:00',
post_modified = '2000-02-01T12:00:00',
post_modified_gmt = '2000-02-01T10:00:00'
WHERE ID = " . $product_id
);
// phpcs:enable WordPress.DB.PreparedSQL.NotPrepared
$request = new WP_REST_Request( 'GET', '/wc/v3/products' );
$request->set_query_params(
array(
$first_param_name => $first_param_value,
$second_param_name => $second_param_value,
'dates_are_gmt' => $filter_by_gmt ? 'true' : 'false',
)
);
$response = $this->server->dispatch( $request );
$response_products = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( $expected_to_be_returned ? 1 : 0, count( $response_products ) );
private function get_endpoint_for_date_filtering_tests() {
return '/wc/v3/products';
}
}