From 5328b9469155959aa0ca8dd3c09544aa2aee1352 Mon Sep 17 00:00:00 2001 From: Joe McGill Date: Fri, 23 Feb 2024 16:53:39 +0000 Subject: [PATCH] Editor: Improve PHPUnit tests for block pattern registration. This is a followup to [57683], which adds additional unit test coverage to ensure block pattern content is not loaded from files during registration, but instead when those patterns are accessed. This also improves the `set_up` and `tear_down` methods for the `Tests_Blocks_wpBlockPattersRegistry` test class to ensure that any modifications made to registered blocks during the tests are reset after each test. Props thekt12, joemcgill. See #59532. git-svn-id: https://develop.svn.wordpress.org/trunk@57703 602fd350-edb4-49c9-b593-d223f7449a82 --- .../tests/blocks/wpBlockPatternsRegistry.php | 148 +++++++++++++++++- 1 file changed, 147 insertions(+), 1 deletion(-) diff --git a/tests/phpunit/tests/blocks/wpBlockPatternsRegistry.php b/tests/phpunit/tests/blocks/wpBlockPatternsRegistry.php index 64991a849dfe5..992325edd2b2c 100644 --- a/tests/phpunit/tests/blocks/wpBlockPatternsRegistry.php +++ b/tests/phpunit/tests/blocks/wpBlockPatternsRegistry.php @@ -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. * @@ -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(); } /** @@ -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(); } @@ -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 ); + } }