Skip to content

Commit

Permalink
Editor: Improve PHPUnit tests for block pattern registration.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
joemcgill committed Feb 23, 2024
1 parent 38ee6fc commit 5328b94
Showing 1 changed file with 147 additions and 1 deletion.
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 );
}
}

0 comments on commit 5328b94

Please sign in to comment.