diff --git a/.github/workflows/tests-wordpress.yml b/.github/workflows/tests-wordpress.yml index 9bf06502..cc51b5f9 100644 --- a/.github/workflows/tests-wordpress.yml +++ b/.github/workflows/tests-wordpress.yml @@ -15,14 +15,22 @@ jobs: runs-on: ubuntu-latest name: WordPress ${{ matrix.wordpress }} Integration Tests on PHP ${{ matrix.php }} strategy: + fail-fast: false matrix: php: [ '8.0', '7.4' ] wordpress: [ '5.9', '5.8', '5.7.2', '5.6' ] + wpgraphql_version: [ 'latest' ] include: - php: '8.0' wordpress: '6.0' - php: '8.1' wordpress: '6.1' + # Temporary inclusion while we resolve the tests that broke in the WPGraphQL v1.14.5 release + # this allows us to continue testing new features to this codebase while we work + # toward updating the tests impacted by that release + - php: '8.1' + wordpress: '6.2' + wpgraphql_version: '1.14.4' steps: - name: Checkout uses: actions/checkout@v2 @@ -58,7 +66,7 @@ jobs: run: composer update - name: Run acceptance, functional tests - run: docker-compose run -e DEBUG=1 testing + run: docker-compose run -e DEBUG=1 -e WPGRAPHQL_VERSION=${{matrix.wpgraphql_version}} testing env: WP_VERSION: ${{ matrix.wordpress }} PHP_VERSION: ${{ matrix.php }} diff --git a/src/Admin/Settings.php b/src/Admin/Settings.php index fa37f0f2..9a6a09d1 100644 --- a/src/Admin/Settings.php +++ b/src/Admin/Settings.php @@ -23,8 +23,29 @@ public static function caching_enabled() { // get the cache_toggle setting $option = function_exists( 'get_graphql_setting' ) ? \get_graphql_setting( 'cache_toggle', false, 'graphql_cache_section' ) : false; + $enabled = ( 'on' === $option ); + + $enabled = apply_filters( 'wpgraphql_cache_wordpress_cache_enabled', (bool) $enabled ); + // if there's no user logged in, and GraphQL Caching is enabled - return ( 'on' === $option ); + return (bool) $enabled; + } + + /** + * Whether cache maps are enabled. + * + * Cache maps are used to track which nodes and list keys are associated with which queries, + * and can be referenced to purge specific queries. + * + * Default behavior is to only enable building and storage of the cache maps if "WordPress Cache" (non-network cache) is enabled, but this can be filtered to be enabled without WordPress cache being enabled. + * + * @return bool + */ + public static function cache_maps_enabled() { + + // Whether "WordPress Cache" (object/transient) cache is enabled + $enabled = self::caching_enabled(); + return (bool) apply_filters( 'wpgraphql_cache_enable_cache_maps', (bool) $enabled ); } /** diff --git a/src/Cache/Collection.php b/src/Cache/Collection.php index f9ad2b0a..f926c442 100644 --- a/src/Cache/Collection.php +++ b/src/Cache/Collection.php @@ -10,6 +10,7 @@ use GraphQL\Executor\ExecutionResult; use GraphQL\Type\Schema; use WPGraphQL\Request; +use WPGraphQL\SmartCache\Admin\Settings; class Collection extends Query { @@ -62,6 +63,12 @@ public function save_query_mapping_cb( $request, $query_id ) { + + // If cache maps are not enabled, do nothing + if ( ! Settings::cache_maps_enabled() ) { + return; + } + $request_key = $this->build_key( $query_id, $query, $variables, $operation ); // get the runtime nodes from the query analyzer diff --git a/tests/_support/TestCase/WPGraphQLSmartCacheTestCaseWithSeedDataAndPopulatedCaches.php b/tests/_support/TestCase/WPGraphQLSmartCacheTestCaseWithSeedDataAndPopulatedCaches.php index 2132ee41..a651aea9 100644 --- a/tests/_support/TestCase/WPGraphQLSmartCacheTestCaseWithSeedDataAndPopulatedCaches.php +++ b/tests/_support/TestCase/WPGraphQLSmartCacheTestCaseWithSeedDataAndPopulatedCaches.php @@ -187,6 +187,9 @@ public function setUp(): void { */ $this->clearSchema(); + // enable graphql cache maps + add_filter( 'wpgraphql_cache_enable_cache_maps', '__return_true' ); + // prevent default category from being added to posts on creation update_option( 'default_category', 0 ); diff --git a/tests/wpunit/CacheCollectionTest.php b/tests/wpunit/CacheCollectionTest.php index ff6ff44f..e22baa37 100644 --- a/tests/wpunit/CacheCollectionTest.php +++ b/tests/wpunit/CacheCollectionTest.php @@ -8,7 +8,15 @@ class CacheCollectionTest extends \Codeception\TestCase\WPTestCase { - public function testAddData() { + public function _setUp() { + + // enable graphql cache maps + add_filter( 'wpgraphql_cache_enable_cache_maps', '__return_true' ); + + parent::_setUp(); // TODO: Change the autogenerated stub + } + + public function testAddData() { $key = uniqid( 'test-' ); $content = 'foo-bar'; diff --git a/tests/wpunit/CacheFromConnectionFieldNameTest.php b/tests/wpunit/CacheFromConnectionFieldNameTest.php index a23c633d..2f10d34b 100644 --- a/tests/wpunit/CacheFromConnectionFieldNameTest.php +++ b/tests/wpunit/CacheFromConnectionFieldNameTest.php @@ -10,7 +10,10 @@ class CacheFromConnectionFieldNameTest extends \Codeception\TestCase\WPTestCase public function setUp(): void { // clear schema so that the register connection works - \WPGraphQL::clear_schema(); + // enable graphql cache maps + add_filter( 'wpgraphql_cache_enable_cache_maps', '__return_true' ); + + \WPGraphQL::clear_schema(); parent::setUp(); } diff --git a/tests/wpunit/CacheInvalidationTest.php b/tests/wpunit/CacheInvalidationTest.php index d6eb704c..dd9e710f 100644 --- a/tests/wpunit/CacheInvalidationTest.php +++ b/tests/wpunit/CacheInvalidationTest.php @@ -9,8 +9,12 @@ class CacheInvalidationTest extends \Codeception\TestCase\WPTestCase { protected $collection; public function setUp(): void { + \WPGraphQL::clear_schema(); + // enable graphql cache maps + add_filter( 'wpgraphql_cache_enable_cache_maps', '__return_true' ); + if ( ! defined( 'GRAPHQL_DEBUG' ) ) { define( 'GRAPHQL_DEBUG', true ); }