Allow report caching layer to be filtered off. (https://github.com/woocommerce/woocommerce-admin/pull/3434)

* Add filter to conditionally disable report caching.

* Disable caching for Orders Stats unit tests.

Fixes the display of the last query when tests fail.
This commit is contained in:
Jeff Stieler 2019-12-17 16:21:44 -05:00 committed by GitHub
parent dd5929ea46
commit 162a8cd550
2 changed files with 40 additions and 2 deletions

View File

@ -119,6 +119,23 @@ class DataStore extends SqlQuery {
}
}
/**
* Whether or not the report should use the caching layer.
*
* Provides an opportunity for plugins to prevent reports from using cache.
*
* @return boolean Whether or not to utilize caching.
*/
protected function should_use_cache() {
/**
* Determines if a report will utilize caching.
*
* @param bool $use_cache Whether or not to use cache.
* @param string $cache_key The report's cache key. Used to identify the report.
*/
return (bool) apply_filters( 'woocommerce_analytics_report_should_use_cache', true, $this->cache_key );
}
/**
* Returns string to be used as cache key for the data.
*
@ -143,9 +160,13 @@ class DataStore extends SqlQuery {
* @return mixed
*/
protected function get_cached_data( $cache_key ) {
if ( $this->should_use_cache() ) {
return Cache::get( $cache_key );
}
return false;
}
/**
* Wrapper around Cache::set().
*
@ -154,9 +175,13 @@ class DataStore extends SqlQuery {
* @return bool
*/
protected function set_cached_data( $cache_key, $value ) {
if ( $this->should_use_cache() ) {
return Cache::set( $cache_key, $value );
}
return true;
}
/**
* Compares two report data objects by pre-defined object property and ASC/DESC ordering.
*

View File

@ -13,6 +13,19 @@ use \Automattic\WooCommerce\Admin\API\Reports\TimeInterval;
* Class WC_Tests_Reports_Orders_Stats
*/
class WC_Tests_Reports_Orders_Stats extends WC_Unit_Test_Case {
/**
* Don't cache report data during these tests.
*/
public static function setUpBeforeClass() {
add_filter( 'woocommerce_analytics_report_should_use_cache', '__return_false' );
}
/**
* Restore cache for other tests.
*/
public static function tearDownAfterClass() {
remove_filter( 'woocommerce_analytics_report_should_use_cache', '__return_false' );
}
/**
* Test the calculations and querying works correctly for the base case of 1 order.