Skip to content

Commit bd0842d

Browse files
committed
Tests: Add missing @Covers tags for wp_strip_custom_css_from_blocks and enhance filter tests
1 parent 94bdf42 commit bd0842d

1 file changed

Lines changed: 91 additions & 1 deletion

File tree

tests/phpunit/tests/block-supports/wpStripCustomCssFromBlocks.php

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
/**
44
* @group block-supports
55
*
6-
* @covers ::wp_strip_custom_css_from_blocks
76
*/
87
class Tests_Block_Supports_WpStripCustomCssFromBlocks extends WP_UnitTestCase {
98

@@ -12,6 +11,7 @@ class Tests_Block_Supports_WpStripCustomCssFromBlocks extends WP_UnitTestCase {
1211
*
1312
* @ticket 64771
1413
*
14+
* @covers ::wp_strip_custom_css_from_blocks
1515
* @dataProvider data_strips_css_from_blocks
1616
*
1717
* @param string $content Post content containing blocks.
@@ -42,6 +42,7 @@ public function data_strips_css_from_blocks() {
4242
/**
4343
* Tests that style.css is stripped from nested inner blocks.
4444
*
45+
* @covers ::wp_strip_custom_css_from_blocks
4546
* @ticket 64771
4647
*/
4748
public function test_strips_css_from_inner_blocks() {
@@ -57,6 +58,7 @@ public function test_strips_css_from_inner_blocks() {
5758
/**
5859
* Tests that content without blocks is returned unchanged.
5960
*
61+
* @covers ::wp_strip_custom_css_from_blocks
6062
* @ticket 64771
6163
*/
6264
public function test_returns_non_block_content_unchanged() {
@@ -70,6 +72,7 @@ public function test_returns_non_block_content_unchanged() {
7072
/**
7173
* Tests that content without style.css attributes is returned unchanged.
7274
*
75+
* @covers ::wp_strip_custom_css_from_blocks
7376
* @ticket 64771
7477
*/
7578
public function test_returns_unchanged_when_no_css_attributes() {
@@ -83,6 +86,7 @@ public function test_returns_unchanged_when_no_css_attributes() {
8386
/**
8487
* Tests that other style properties are preserved when css is stripped.
8588
*
89+
* @covers ::wp_strip_custom_css_from_blocks
8690
* @ticket 64771
8791
*/
8892
public function test_preserves_other_style_properties() {
@@ -98,6 +102,7 @@ public function test_preserves_other_style_properties() {
98102
/**
99103
* Tests that empty style object is cleaned up after stripping css.
100104
*
105+
* @covers ::wp_strip_custom_css_from_blocks
101106
* @ticket 64771
102107
*/
103108
public function test_cleans_up_empty_style_object() {
@@ -112,6 +117,7 @@ public function test_cleans_up_empty_style_object() {
112117
/**
113118
* Tests that slashed content is handled correctly.
114119
*
120+
* @covers ::wp_strip_custom_css_from_blocks
115121
* @ticket 64771
116122
*/
117123
public function test_handles_slashed_content() {
@@ -123,4 +129,88 @@ public function test_handles_slashed_content() {
123129

124130
$this->assertArrayNotHasKey( 'css', $blocks[0]['attrs']['style'] ?? array(), 'style.css should be stripped even from slashed content.' );
125131
}
132+
133+
/**
134+
* Tests that the content_save_pre filter is added for a user without edit_css.
135+
*
136+
* @ticket 64771
137+
*
138+
* @covers ::wp_custom_css_kses_init
139+
* @covers ::wp_custom_css_kses_init_filters
140+
*/
141+
public function test_filter_added_for_user_without_edit_css() {
142+
$author_id = self::factory()->user->create( array( 'role' => 'author' ) );
143+
wp_set_current_user( $author_id );
144+
wp_custom_css_kses_init();
145+
146+
$this->assertSame( 8, has_filter( 'content_save_pre', 'wp_strip_custom_css_from_blocks' ), 'content_save_pre filter should be added at priority 8 for users without edit_css.' );
147+
$this->assertSame( 8, has_filter( 'content_filtered_save_pre', 'wp_strip_custom_css_from_blocks' ), 'content_filtered_save_pre filter should be added at priority 8 for users without edit_css.' );
148+
149+
wp_set_current_user( 0 );
150+
wp_custom_css_remove_filters();
151+
}
152+
153+
/**
154+
* Tests that the content_save_pre filter is not added for a user with edit_css.
155+
*
156+
* @ticket 64771
157+
*
158+
* @covers ::wp_custom_css_kses_init
159+
* @covers ::wp_custom_css_remove_filters
160+
*/
161+
public function test_filter_not_added_for_user_with_edit_css() {
162+
$admin_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
163+
wp_set_current_user( $admin_id );
164+
wp_custom_css_kses_init();
165+
166+
$this->assertFalse( has_filter( 'content_save_pre', 'wp_strip_custom_css_from_blocks' ), 'content_save_pre filter should not be added for users with edit_css.' );
167+
$this->assertFalse( has_filter( 'content_filtered_save_pre', 'wp_strip_custom_css_from_blocks' ), 'content_filtered_save_pre filter should not be added for users with edit_css.' );
168+
169+
wp_set_current_user( 0 );
170+
}
171+
172+
/**
173+
* Tests that switching to a user with edit_css removes the filter via the set_current_user action.
174+
*
175+
* wp_custom_css_kses_init() is hooked to set_current_user, so wp_set_current_user()
176+
* alone should update the filter state without a manual call.
177+
*
178+
* @ticket 64771
179+
*
180+
* @covers ::wp_custom_css_kses_init
181+
*/
182+
public function test_set_current_user_action_triggers_reinit() {
183+
$admin_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
184+
$author_id = self::factory()->user->create( array( 'role' => 'author' ) );
185+
186+
wp_set_current_user( $author_id );
187+
$this->assertNotFalse( has_filter( 'content_save_pre', 'wp_strip_custom_css_from_blocks' ), 'Filter should be active for user without edit_css.' );
188+
189+
wp_set_current_user( $admin_id );
190+
$this->assertFalse( has_filter( 'content_save_pre', 'wp_strip_custom_css_from_blocks' ), 'Filter should be removed after switching to a user with edit_css.' );
191+
192+
wp_set_current_user( 0 );
193+
}
194+
195+
/**
196+
* Tests that the filter is enabled during import regardless of user capability.
197+
*
198+
* @ticket 64771
199+
*
200+
* @covers ::wp_custom_css_force_filtered_html_on_import_filter
201+
*/
202+
public function test_force_filtered_html_on_import_enables_filter_for_privileged_user() {
203+
$admin_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
204+
wp_set_current_user( $admin_id );
205+
wp_custom_css_kses_init();
206+
207+
$this->assertFalse( has_filter( 'content_save_pre', 'wp_strip_custom_css_from_blocks' ), 'Filter should not be active for admin before import.' );
208+
209+
apply_filters( 'force_filtered_html_on_import', true );
210+
211+
$this->assertNotFalse( has_filter( 'content_save_pre', 'wp_strip_custom_css_from_blocks' ), 'Filter should be enabled during import regardless of user capability.' );
212+
213+
wp_set_current_user( 0 );
214+
wp_custom_css_remove_filters();
215+
}
126216
}

0 commit comments

Comments
 (0)