From 59f2bdbe46d260a88e2d4b93824045a8666b215a Mon Sep 17 00:00:00 2001 From: Ron Rennick Date: Mon, 22 Jun 2020 16:05:29 -0300 Subject: [PATCH] add template cache unit test --- .../legacy/unit-tests/core/template-cache.php | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 tests/legacy/unit-tests/core/template-cache.php diff --git a/tests/legacy/unit-tests/core/template-cache.php b/tests/legacy/unit-tests/core/template-cache.php new file mode 100644 index 00000000000..f1658e17d8d --- /dev/null +++ b/tests/legacy/unit-tests/core/template-cache.php @@ -0,0 +1,65 @@ +clear_template_cache(); + + // Prevent template being loaded. + add_filter( 'wc_get_template_part', '__return_false' ); + // Use content-* templates. + $templates = array(); + foreach ( glob( dirname( WC_PLUGIN_FILE ) . '/templates/content*.php' ) as $template ) { + $name = preg_replace( '|content-(.*)\.php|', '$1', basename( $template ) ); + $cache_key = sanitize_key( + implode( + '-', + array( + 'template-part', + 'content', + $name, + WC_VERSION, + ) + ) + ); + $templates[ $cache_key ] = $template; + wc_get_template_part( 'content', $name ); + } + remove_filter( 'wc_get_template_part', '__return_false' ); + + // Check cached template list. + $this->assertEquals( array_keys( $templates ), wp_cache_get( 'cached_templates', 'woocommerce' ) ); + + // Check individual templates. + foreach ( $templates as $cache_key => $template ) { + $this->assertEquals( $template, wp_cache_get( $cache_key, 'woocommerce' ) ); + } + + // Clear cache. + $this->clear_template_cache(); + } + + /** + * Clear the template cache. + */ + protected function clear_template_cache() { + $cached_templates = wp_cache_get( 'cached_templates', 'woocommerce' ); + wc_clear_template_cache(); + + foreach ( (array) $cached_templates as $template ) { + $this->assertEmpty( wp_cache_get( $template, 'woocommerce' ) ); + } + $this->assertEmpty( wp_cache_get( 'cached_templates', 'woocommerce' ) ); + } +}