Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit Test for trac 59532 #6154

Closed
wants to merge 21 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
148 changes: 147 additions & 1 deletion tests/phpunit/tests/blocks/wpBlockPatternsRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ class Tests_Blocks_wpBlockPattersRegistry extends WP_UnitTestCase {
*/
private $registry = null;

/**
* Original registered patterns.
* This is the value from the internal private property.
*
* @since 6.5.0
* @var array
*/
private $original_registered_patterns = null;

/**
* Set up each test method.
*
Expand All @@ -28,7 +37,8 @@ class Tests_Blocks_wpBlockPattersRegistry extends WP_UnitTestCase {
public function set_up() {
parent::set_up();

$this->registry = new WP_Block_Patterns_Registry();
$this->registry = new WP_Block_Patterns_Registry();
$this->original_registered_patterns = $this->get_registered_patterns_variable_value();
}

/**
Expand All @@ -45,6 +55,7 @@ public function tear_down() {
$registry->unregister( 'tests/my-block' );
}

$this->set_registered_patterns_variable_value( $this->original_registered_patterns );
parent::tear_down();
}

Expand Down Expand Up @@ -543,4 +554,139 @@ public function test_register_theme_block_patterns_on_init_skipped_during_instal

$this->assertEmpty( array_intersect( $theme_patterns, $registered ), 'Theme patterns were were incorrectly registered.' );
}

/**
* Ensures theme patterns are lazy loaded.
*
* @ticket 59532
*
* @covers WP_Block_Patterns_Registry::get_all_registered
*/
public function test_lazy_loading_block_patterns_get_all_registered() {
// This test needs to use access static class properties.
$registry = WP_Block_Patterns_Registry::get_instance();

// Testing only the first pattern loaded from the theme.
$pattern_name = 'twentytwentythree/footer-default';

// Ensure we're using a theme with patterns.
switch_theme( 'twentytwentythree' );

// This helper is fired on the init hook.
_register_theme_block_patterns();

// Get the value of the private property.
$registered_patterns = $this->get_registered_patterns_variable_value();

$this->assertTrue(
isset( $registered_patterns[ $pattern_name ]['file_path'] ) &&
! isset( $registered_patterns[ $pattern_name ]['content'] ),
'Pattern was not lazy loaded.'
);

$all_patterns = $registry->get_all_registered();

$loaded_pattern = array_values(
array_filter(
$all_patterns,
function ( $pattern ) use ( $pattern_name ) {
return $pattern['name'] === $pattern_name;
}
)
);

$this->assertTrue(
! empty( $loaded_pattern[0]['content'] ),
'Content not loaded.'
);

// Check if the original property was updated.
$registered_patterns = $this->get_registered_patterns_variable_value();

$this->assertTrue(
! empty( $registered_patterns[ $pattern_name ]['content'] ),
'Content not updated.'
);
}

/**
* Ensures theme patterns are lazy loaded.
*
* @ticket 59532
*
* @covers WP_Block_Patterns_Registry::get_registered
*/
public function test_lazy_loading_block_patterns_get_registered() {
// This test needs to use access static class properties.
$registry = WP_Block_Patterns_Registry::get_instance();

// Testing only the first pattern loaded from the theme.
$pattern_name = 'twentytwentythree/footer-default';

// Ensure we're using a theme with patterns.
switch_theme( 'twentytwentythree' );

// This helper is fired on the init hook.
_register_theme_block_patterns();

// Get the value of the private property.
$registered_patterns = $this->get_registered_patterns_variable_value();

$this->assertTrue(
isset( $registered_patterns[ $pattern_name ]['file_path'] ) &&
! isset( $registered_patterns[ $pattern_name ]['content'] ),
'Pattern was not lazy loaded.'
);

$loaded_pattern = $registry->get_registered( $pattern_name );

$this->assertTrue(
! empty( $loaded_pattern['content'] ),
'Content not loaded.'
);

// Check if the original property was updated.
$registered_patterns = $this->get_registered_patterns_variable_value();

$this->assertTrue(
! empty( $registered_patterns[ $pattern_name ]['content'] ),
'Content not updated.'
);
}

/**
* Get the value of the `$registered_patterns` private property.
*
* @return array
*/
private function get_registered_patterns_variable_value() {
$registry = WP_Block_Patterns_Registry::get_instance();
// Use Reflection to access private property.
$reflection = new ReflectionClass( $registry );
$property = $reflection->getProperty( 'registered_patterns' );
$property->setAccessible( true );

// Get the value of the private property.
$registered_patterns = $property->getValue( $registry );
$property->setAccessible( false );

return $registered_patterns;
}

/**
* Set the value of the `$registered_patterns` private property.
*
* @param array $value The value to set.
*/
private function set_registered_patterns_variable_value( $value ) {
$registry = WP_Block_Patterns_Registry::get_instance();
// Use Reflection to access private property.
$reflection = new ReflectionClass( $registry );
$property = $reflection->getProperty( 'registered_patterns' );
$property->setAccessible( true );

// Set the value of the private property.
$property->setValue( $registry, $value );
$property->setAccessible( false );
}
}