Skip to content

Commit

Permalink
Add a test
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonjd committed Oct 18, 2024
1 parent 8ac6b4b commit fb5e0f3
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions phpunit/class-wp-theme-json-resolver-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ class WP_Theme_JSON_Resolver_Gutenberg_Test extends WP_UnitTestCase {
*/
private static $property_blocks_cache_orig_value;

/**
* WP_Theme_JSON_Resolver_Gutenberg::$resolved_theme_uris_cache property.
*
* @var ReflectionProperty
*/
private static $property_resolved_theme_uris_cache;

/**
* Original value of the WP_Theme_JSON_Resolver_Gutenberg::$resolved_theme_uris_cache property.
*
* @var array
*/
private static $property_resolved_theme_uris_cache_orig_value;

/**
* WP_Theme_JSON_Resolver_Gutenberg::$core property.
*
Expand Down Expand Up @@ -81,11 +95,16 @@ public static function set_up_before_class() {
static::$property_core = new ReflectionProperty( WP_Theme_JSON_Resolver_Gutenberg::class, 'core' );
static::$property_core->setAccessible( true );
static::$property_core_orig_value = static::$property_core->getValue();

static::$property_resolved_theme_uris_cache = new ReflectionProperty( WP_Theme_JSON_Resolver_Gutenberg::class, 'resolved_theme_uris_cache' );
static::$property_resolved_theme_uris_cache->setAccessible( true );
static::$property_resolved_theme_uris_cache_orig_value = static::$property_resolved_theme_uris_cache->getValue();
}

public static function tear_down_after_class() {
static::$property_blocks_cache->setValue( null, static::$property_blocks_cache_orig_value );
static::$property_core->setValue( null, static::$property_core_orig_value );
static::$property_resolved_theme_uris_cache->setValue( null, static::$property_resolved_theme_uris_cache_orig_value );
parent::tear_down_after_class();
}

Expand Down Expand Up @@ -1350,6 +1369,54 @@ public function test_get_resolved_theme_uris() {
$this->assertSame( $expected_data, $actual );
}

public function test_get_resolved_theme_uris_cache() {
$theme_json = new WP_Theme_JSON_Gutenberg(
array(
'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA,
'styles' => array(
'background' => array(
'backgroundImage' => array(
'url' => 'file:./example/img/image.png',
),
),
'blocks' => array(
'core/quote' => array(
'background' => array(
'backgroundImage' => array(
'url' => 'file:./example/img/quote.jpg',
),
),
),
'core/verse' => array(
'background' => array(
'backgroundImage' => array(
'url' => 'file:./example/img/verse.gif',
),
),
),
),
),
)
);

/*
* This filter callback normalizes the return value from `get_theme_file_uri`
* to guard against changes in test environments.
* The test suite otherwise returns full system dir path, e.g.,
* /wordpress-phpunit/includes/../data/themedir1/default/example/img/image.png
*/
$filter_theme_file_uri_callback = function ( $file ) {
return 'https://example.org/wp-content/themes/example-theme/example/' . explode( 'example/', $file )[1];
};
add_filter( 'theme_file_uri', $filter_theme_file_uri_callback );
$actual = WP_Theme_JSON_Resolver_Gutenberg::get_resolved_theme_uris( $theme_json );
remove_filter( 'theme_file_uri', $filter_theme_file_uri_callback );

$current_stylesheet_directory = get_stylesheet_directory();
$expected_data = array( "$current_stylesheet_directory" => $actual );
$this->assertSame( $expected_data, static::$property_resolved_theme_uris_cache->getValue() );
}

/**
* Tests that block style variations data gets merged in the following
* priority order, from highest priority to lowest.
Expand Down

0 comments on commit fb5e0f3

Please sign in to comment.