- Timestamp:
- 10/18/2024 09:53:45 PM (3 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/theme/wpAddGlobalStylesForBlocks.php
r58710 r59256 19 19 private $test_blocks = array(); 20 20 21 /** 22 * Administrator ID. 23 * 24 * @var int 25 */ 26 private static $administrator_id; 27 28 public static function set_up_before_class() { 29 parent::set_up_before_class(); 30 self::$administrator_id = self::factory()->user->create( 31 array( 32 'role' => 'administrator', 33 'user_email' => 'administrator@example.com', 34 ) 35 ); 36 } 37 21 38 public function set_up() { 22 39 parent::set_up(); … … 77 94 78 95 /** 96 * Tests that the block cache is set for global styles. 97 * 98 * @ticket 61679 99 */ 100 public function test_styles_for_blocks_cache_is_set() { 101 $this->set_up_third_party_block(); 102 103 wp_register_style( 'global-styles', false, array(), true, true ); 104 105 $cache_key = 'wp_styles_for_blocks'; 106 $styles_for_blocks_before = get_transient( $cache_key ); 107 $this->assertFalse( $styles_for_blocks_before, 'No block styles should be cached yet.' ); 108 109 wp_add_global_styles_for_blocks(); 110 111 $styles_for_blocks_after = get_transient( $cache_key ); 112 $this->assertNotEmpty( $styles_for_blocks_after, 'No block styles were cached.' ); 113 } 114 115 /** 116 * Tests that the block cache is skipped when in dev mode for themes. 117 * 118 * @ticket 61679 119 */ 120 public function test_styles_for_blocks_skips_cache_in_dev_mode() { 121 global $_wp_tests_development_mode; 122 123 $orig_dev_mode = $_wp_tests_development_mode; 124 125 // Setting development mode to theme should skip the cache. 126 $_wp_tests_development_mode = 'theme'; 127 128 wp_register_style( 'global-styles', false, array(), true, true ); 129 130 // Initial register of global styles. 131 wp_add_global_styles_for_blocks(); 132 133 $styles_for_blocks_initial = get_transient( 'wp_styles_for_blocks' ); 134 135 // Cleanup. 136 $_wp_tests_development_mode = $orig_dev_mode; 137 138 $this->assertFalse( $styles_for_blocks_initial ); 139 } 140 141 /** 142 * Tests that the block cache is updated if the block meta has changed. 143 * 144 * @ticket 61679 145 */ 146 public function test_styles_for_blocks_cache_is_skipped() { 147 wp_register_style( 'global-styles', false, array(), true, true ); 148 149 // Initial register of global styles. 150 wp_add_global_styles_for_blocks(); 151 152 $styles_for_blocks_initial = get_transient( 'wp_styles_for_blocks' ); 153 $this->assertNotEmpty( $styles_for_blocks_initial, 'Initial cache was not set.' ); 154 155 $this->set_up_third_party_block(); 156 157 /* 158 * Call register of global styles again to ensure the cache is updated. 159 * In normal conditions, this function is only called once per request. 160 */ 161 wp_add_global_styles_for_blocks(); 162 163 $styles_for_blocks_updated = get_transient( 'wp_styles_for_blocks' ); 164 $this->assertNotEmpty( $styles_for_blocks_updated, 'Updated cache was not set.' ); 165 166 $this->assertNotSame( 167 $styles_for_blocks_initial, 168 $styles_for_blocks_updated, 169 'Block style cache was not updated.' 170 ); 171 } 172 173 /** 174 * Confirms that `wp_styles_for_blocks` cache is cleared when a user modifies global styles. 175 * @ticket 61679 176 */ 177 public function test_styles_for_blocks_cache_is_reset_when_user_styles_change() { 178 // Only administrators can update the global styles post. 179 wp_set_current_user( self::$administrator_id ); 180 181 $this->set_up_third_party_block(); 182 183 wp_register_style( 'global-styles', false, array(), true, true ); 184 wp_add_global_styles_for_blocks(); 185 186 $cache_key = 'wp_styles_for_blocks'; 187 $styles_for_blocks_before = get_transient( $cache_key ); 188 189 // Update the global styles post. 190 $post_id = WP_Theme_JSON_Resolver::get_user_global_styles_post_id(); 191 $before = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( wp_get_theme() ); 192 $old_content = json_decode( $before['post_content'], true ); 193 194 // Mock a change in the global styles. 195 $new_content = array_merge( 196 $old_content, 197 array( 198 'styles' => array( 199 'elements' => array( 200 'button' => array( 201 'color' => array( 202 'background' => 'orange', 203 ), 204 ), 205 ), 206 ), 207 ) 208 ); 209 210 wp_update_post( 211 array( 212 'ID' => $post_id, 213 'post_content' => wp_json_encode( $new_content ), 214 ) 215 ); 216 217 // Reset the static cache, since this would be reset between requests. 218 WP_Theme_JSON_Resolver::clean_cached_data(); 219 220 /* 221 * Call register of global styles again to ensure the cache is updated. 222 * In normal conditions, this function is only called once per request. 223 */ 224 wp_add_global_styles_for_blocks(); 225 226 $cache_key = 'wp_styles_for_blocks'; 227 $styles_for_blocks_after = get_transient( $cache_key ); 228 229 $this->assertNotSame( 230 $styles_for_blocks_before, 231 $styles_for_blocks_after, 232 'Block style cache was not updated.' 233 ); 234 } 235 236 /** 79 237 * @ticket 56915 80 238 * @ticket 61165
Note: See TracChangeset
for help on using the changeset viewer.