Merge pull request #26840 from woocommerce/test/template-cache-clearing

add template cache unit test
This commit is contained in:
Ron Rennick 2020-06-23 13:32:52 -03:00 committed by GitHub
commit 524b7e7876
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 65 additions and 0 deletions

View File

@ -0,0 +1,65 @@
<?php
/**
* Template cache tests class.
*
* @package WooCommerce\Tests\Core
*/
/**
* WC_Template_Cache class.
*/
class WC_Template_Cache extends WC_Unit_Test_Case {
/**
* Test wc_get_template_part().
*/
public function test_wc_get_template_part() {
// Clear cache to start.
$this->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' ) );
}
}