Make WordPress Core

Changeset 57703


Ignore:
Timestamp:
02/23/2024 04:53:39 PM (2 years ago)
Author:
joemcgill
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/blocks/wpBlockPatternsRegistry.php

    r57627 r57703  
    2222
    2323        /**
     24         * Original registered patterns.
     25         * This is the value from the internal private property.
     26         *
     27         * @since 6.5.0
     28         * @var array
     29         */
     30        private $original_registered_patterns = null;
     31
     32        /**
    2433         * Set up each test method.
    2534         *
     
    2938                parent::set_up();
    3039
    31                 $this->registry = new WP_Block_Patterns_Registry();
     40                $this->registry                     = new WP_Block_Patterns_Registry();
     41                $this->original_registered_patterns = $this->get_registered_patterns_variable_value();
    3242        }
    3343
     
    4656                }
    4757
     58                $this->set_registered_patterns_variable_value( $this->original_registered_patterns );
    4859                parent::tear_down();
    4960        }
     
    544555                $this->assertEmpty( array_intersect( $theme_patterns, $registered ), 'Theme patterns were were incorrectly registered.' );
    545556        }
     557
     558        /**
     559         * Ensures theme patterns are lazy loaded.
     560         *
     561         * @ticket 59532
     562         *
     563         * @covers WP_Block_Patterns_Registry::get_all_registered
     564         */
     565        public function test_lazy_loading_block_patterns_get_all_registered() {
     566                // This test needs to use access static class properties.
     567                $registry = WP_Block_Patterns_Registry::get_instance();
     568
     569                // Testing only the first pattern loaded from the theme.
     570                $pattern_name = 'twentytwentythree/footer-default';
     571
     572                // Ensure we're using a theme with patterns.
     573                switch_theme( 'twentytwentythree' );
     574
     575                // This helper is fired on the init hook.
     576                _register_theme_block_patterns();
     577
     578                // Get the value of the private property.
     579                $registered_patterns = $this->get_registered_patterns_variable_value();
     580
     581                $this->assertTrue(
     582                        isset( $registered_patterns[ $pattern_name ]['file_path'] ) &&
     583                        ! isset( $registered_patterns[ $pattern_name ]['content'] ),
     584                        'Pattern was not lazy loaded.'
     585                );
     586
     587                $all_patterns = $registry->get_all_registered();
     588
     589                $loaded_pattern = array_values(
     590                        array_filter(
     591                                $all_patterns,
     592                                function ( $pattern ) use ( $pattern_name ) {
     593                                        return $pattern['name'] === $pattern_name;
     594                                }
     595                        )
     596                );
     597
     598                $this->assertTrue(
     599                        ! empty( $loaded_pattern[0]['content'] ),
     600                        'Content not loaded.'
     601                );
     602
     603                // Check if the original property was updated.
     604                $registered_patterns = $this->get_registered_patterns_variable_value();
     605
     606                $this->assertTrue(
     607                        ! empty( $registered_patterns[ $pattern_name ]['content'] ),
     608                        'Content not updated.'
     609                );
     610        }
     611
     612        /**
     613         * Ensures theme patterns are lazy loaded.
     614         *
     615         * @ticket 59532
     616         *
     617         * @covers WP_Block_Patterns_Registry::get_registered
     618         */
     619        public function test_lazy_loading_block_patterns_get_registered() {
     620                // This test needs to use access static class properties.
     621                $registry = WP_Block_Patterns_Registry::get_instance();
     622
     623                // Testing only the first pattern loaded from the theme.
     624                $pattern_name = 'twentytwentythree/footer-default';
     625
     626                // Ensure we're using a theme with patterns.
     627                switch_theme( 'twentytwentythree' );
     628
     629                // This helper is fired on the init hook.
     630                _register_theme_block_patterns();
     631
     632                // Get the value of the private property.
     633                $registered_patterns = $this->get_registered_patterns_variable_value();
     634
     635                $this->assertTrue(
     636                        isset( $registered_patterns[ $pattern_name ]['file_path'] ) &&
     637                        ! isset( $registered_patterns[ $pattern_name ]['content'] ),
     638                        'Pattern was not lazy loaded.'
     639                );
     640
     641                $loaded_pattern = $registry->get_registered( $pattern_name );
     642
     643                $this->assertTrue(
     644                        ! empty( $loaded_pattern['content'] ),
     645                        'Content not loaded.'
     646                );
     647
     648                // Check if the original property was updated.
     649                $registered_patterns = $this->get_registered_patterns_variable_value();
     650
     651                $this->assertTrue(
     652                        ! empty( $registered_patterns[ $pattern_name ]['content'] ),
     653                        'Content not updated.'
     654                );
     655        }
     656
     657        /**
     658         * Get the value of the `$registered_patterns` private property.
     659         *
     660         * @return array
     661         */
     662        private function get_registered_patterns_variable_value() {
     663                $registry = WP_Block_Patterns_Registry::get_instance();
     664                // Use Reflection to access private property.
     665                $reflection = new ReflectionClass( $registry );
     666                $property   = $reflection->getProperty( 'registered_patterns' );
     667                $property->setAccessible( true );
     668
     669                // Get the value of the private property.
     670                $registered_patterns = $property->getValue( $registry );
     671                $property->setAccessible( false );
     672
     673                return $registered_patterns;
     674        }
     675
     676        /**
     677         * Set the value of the `$registered_patterns` private property.
     678         *
     679         * @param array $value The value to set.
     680         */
     681        private function set_registered_patterns_variable_value( $value ) {
     682                $registry = WP_Block_Patterns_Registry::get_instance();
     683                // Use Reflection to access private property.
     684                $reflection = new ReflectionClass( $registry );
     685                $property   = $reflection->getProperty( 'registered_patterns' );
     686                $property->setAccessible( true );
     687
     688                // Set the value of the private property.
     689                $property->setValue( $registry, $value );
     690                $property->setAccessible( false );
     691        }
    546692}
Note: See TracChangeset for help on using the changeset viewer.