- Timestamp:
- 10/20/2023 07:06:46 PM (20 months ago)
- File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/theme/wpThemeGetBlockPatterns.php
r56977 r56978 1 1 <?php 2 2 /** 3 * Tests for _wp_get_block_patterns.3 * Tests for WP_Theme::get_block_patterns. 4 4 * 5 5 * @package WordPress … … 8 8 * 9 9 * @group blocks 10 * @group themes 10 11 * 11 * @covers ::_wp_get_block_patterns12 * @covers WP_Theme::get_block_patterns 12 13 */ 13 class Tests_Blocks_WpGetBlockPatterns extends WP_UnitTestCase { 14 class Tests_Theme_WPThemeGetBlockPatterns extends WP_UnitTestCase { 15 16 public static function wpSetUpBeforeClass() { 17 // Ensure development mode is reset before running these tests. 18 unset( $GLOBALS['_wp_tests_development_mode'] ); 19 } 20 21 public static function wpTearDownAfterClass() { 22 // Ensure development mode is reset after running these tests. 23 unset( $GLOBALS['_wp_tests_development_mode'] ); 24 } 25 26 /** 27 * Test helper to access the private get_pattern_cache method of a theme. 28 * 29 * @param WP_Theme $wp_theme A WP_Theme object. 30 * @return array|false Returns an array of patterns if cache is found, otherwise false. 31 */ 32 private function get_pattern_cache( $wp_theme ) { 33 $reflection = new ReflectionMethod( $wp_theme, 'get_pattern_cache' ); 34 $reflection->setAccessible( true ); 35 36 $pattern_cache = $reflection->invoke( $wp_theme, 'get_pattern_cache' ); 37 $reflection->setAccessible( false ); 38 39 return $pattern_cache; 40 } 41 14 42 /** 15 43 * @ticket 59490 16 44 * 17 * @dataProvider data_ wp_get_block_patterns45 * @dataProvider data_get_block_patterns 18 46 * 19 * @param string $theme 20 * @param array $expected The expected pattern data.47 * @param string $theme_slug The theme's slug. 48 * @param array $expected The expected pattern data. 21 49 */ 22 public function test_should_return_block_patterns( $theme, $expected ) { 23 $patterns = _wp_get_block_patterns( wp_get_theme( $theme ) ); 50 public function test_should_return_block_patterns( $theme_slug, $expected ) { 51 $theme = wp_get_theme( $theme_slug ); 52 $patterns = $theme->get_block_patterns(); 24 53 $this->assertSameSets( $expected, $patterns ); 25 54 } … … 27 56 /** 28 57 * @ticket 59490 58 * 59 * @covers WP_Theme::delete_pattern_cache 29 60 */ 30 public function test_delete_ theme_cache() {61 public function test_delete_pattern_cache() { 31 62 $theme = wp_get_theme( 'block-theme-patterns' ); 32 _wp_get_block_patterns( $theme ); 63 64 $this->assertTrue( $theme->exists(), 'The test theme could not be found.' ); 65 66 $theme->get_block_patterns(); 67 33 68 $this->assertSameSets( 34 69 array( … … 40 75 ), 41 76 ), 42 $th eme->get_pattern_cache(),43 'The transient for block theme patterns should be set'77 $this->get_pattern_cache( $theme ), 78 'The cache for block theme patterns should match the expected.' 44 79 ); 45 80 $theme->delete_pattern_cache(); 46 81 $this->assertFalse( 47 $th eme->get_pattern_cache(),48 'The transient for block theme patterns should have been cleared'82 $this->get_pattern_cache( $theme ), 83 'The cache for block theme patterns should have been cleared.' 49 84 ); 50 85 } … … 53 88 * @ticket 59490 54 89 */ 55 public function test_should_clear_ transient_after_switching_theme() {90 public function test_should_clear_cache_after_switching_theme() { 56 91 switch_theme( 'block-theme' ); 57 92 $theme1 = wp_get_theme(); 58 _wp_get_block_patterns( $theme1 ); 93 94 $this->assertTrue( $theme1->exists(), 'The block-theme test theme could not be found.' ); 95 96 $theme1->get_block_patterns(); 59 97 $this->assertSameSets( 60 98 array(), 61 $th eme1->get_pattern_cache(),62 'The transient for block theme should be set'99 $this->get_pattern_cache( $theme1 ), 100 'The cache for block theme should be empty.' 63 101 ); 102 64 103 switch_theme( 'block-theme-patterns' ); 65 $this->assertFalse( $theme1->get_pattern_cache(), 'Transient should not be set for block theme after switch theme' ); 104 66 105 $theme2 = wp_get_theme(); 67 $this->assertFalse( $theme2->get_pattern_cache(), 'Transient should not be set for block theme patterns before being requested' ); 68 _wp_get_block_patterns( $theme2 ); 106 $this->assertTrue( $theme2->exists(), 'The block-theme-patterns test theme could not be found.' ); 107 108 $this->assertFalse( $this->get_pattern_cache( $theme1 ), 'Cache should not be set for block theme after switch theme.' ); 109 $this->assertFalse( $this->get_pattern_cache( $theme2 ), 'Cache should not be set for block theme patterns before being requested.' ); 110 111 $theme2->get_block_patterns( $theme2 ); 69 112 $this->assertSameSets( 70 113 array( … … 77 120 78 121 ), 79 $th eme2->get_pattern_cache(),80 'The transient for block theme patterns should be set'122 $this->get_pattern_cache( $theme2 ), 123 'The cache for block theme patterns should match the expected.' 81 124 ); 82 125 } … … 87 130 * @return array[] 88 131 */ 89 public function data_ wp_get_block_patterns() {132 public function data_get_block_patterns() { 90 133 return array( 91 134 array( … … 120 163 121 164 /** 122 * Tests that _wp_get_block_patterns() clears existing transientwhen in theme development mode.165 * Tests that WP_Theme::get_block_patterns() clears existing cache when in theme development mode. 123 166 * 124 167 * @ticket 59591 125 168 */ 126 public function test_should_clear_existing_ transient_when_in_development_mode() {169 public function test_should_clear_existing_cache_when_in_development_mode() { 127 170 $theme = wp_get_theme( 'block-theme-patterns' ); 128 171 172 $this->assertTrue( $theme->exists(), 'The test theme could not be found.' ); 173 129 174 // Calling the function should set the cache. 130 _wp_get_block_patterns( $theme);175 $theme->get_block_patterns(); 131 176 $this->assertSameSets( 132 177 array( … … 138 183 ), 139 184 ), 140 $th eme->get_pattern_cache(),141 'The transient for block theme patterns should be set'185 $this->get_pattern_cache( $theme ), 186 'The cache for block theme patterns should be set.' 142 187 ); 143 188 144 189 // Calling the function while in theme development mode should clear the cache. 145 190 $GLOBALS['_wp_tests_development_mode'] = 'theme'; 146 _wp_get_block_patterns( $theme );191 $theme->get_block_patterns( $theme ); 147 192 unset( $GLOBALS['_wp_tests_development_mode'] ); // Reset to not pollute other tests. 148 193 $this->assertFalse( 149 $th eme->get_pattern_cache(),150 'The transient for block theme patterns should have been cleared due to theme development mode'194 $this->get_pattern_cache( $theme ), 195 'The cache for block theme patterns should have been cleared due to theme development mode.' 151 196 ); 152 197 }
Note: See TracChangeset
for help on using the changeset viewer.