@@ -243,12 +243,92 @@ public function test_state_injects_both_show_and_hide() {
243243 $ this ->assertStringContainsString ( 'data-hide-if="disabled" ' , $ result );
244244 }
245245
246- public function test_state_trigger_only_does_not_inject_visibility_attrs () {
246+ public function test_state_trigger_injects_trigger_attrs_not_visibility () {
247247 $ block = $ this ->make_block ( 'atomic-wind/text ' , array ( 'stateTrigger ' => 'tabs ' ) );
248248 $ content = '<p class="wp-block">Trigger</p> ' ;
249249
250250 $ result = $ this ->instance ->render_state_attrs ( $ content , $ block );
251251
252+ $ this ->assertStringContainsString ( 'data-state-trigger="tabs" ' , $ result );
253+ $ this ->assertStringContainsString ( 'data-state-action="toggle" ' , $ result );
254+ $ this ->assertStringNotContainsString ( 'data-show-if ' , $ result );
255+ $ this ->assertStringNotContainsString ( 'data-hide-if ' , $ result );
256+ }
257+
258+ public function test_state_trigger_set_action_injects_value () {
259+ $ block = $ this ->make_block ( 'atomic-wind/link ' , array (
260+ 'stateTrigger ' => 'season ' ,
261+ 'stateAction ' => 'set ' ,
262+ 'stateValue ' => 'spring ' ,
263+ ) );
264+ $ content = '<a class="wp-block">Spring</a> ' ;
265+
266+ $ result = $ this ->instance ->render_state_attrs ( $ content , $ block );
267+
268+ $ this ->assertStringContainsString ( 'data-state-trigger="season" ' , $ result );
269+ $ this ->assertStringContainsString ( 'data-state-action="set" ' , $ result );
270+ $ this ->assertStringContainsString ( 'data-state-value="spring" ' , $ result );
271+ }
272+
273+ public function test_state_trigger_toggle_omits_value () {
274+ $ block = $ this ->make_block ( 'atomic-wind/text ' , array (
275+ 'stateTrigger ' => 'panel ' ,
276+ 'stateAction ' => 'toggle ' ,
277+ ) );
278+ $ content = '<p class="wp-block">Toggle</p> ' ;
279+
280+ $ result = $ this ->instance ->render_state_attrs ( $ content , $ block );
281+
282+ $ this ->assertStringContainsString ( 'data-state-action="toggle" ' , $ result );
283+ $ this ->assertStringNotContainsString ( 'data-state-value ' , $ result );
284+ }
285+
286+ public function test_state_trigger_default_attr () {
287+ $ block = $ this ->make_block ( 'atomic-wind/link ' , array (
288+ 'stateTrigger ' => 'season ' ,
289+ 'stateAction ' => 'set ' ,
290+ 'stateValue ' => 'all ' ,
291+ 'stateDefault ' => true ,
292+ ) );
293+ $ content = '<a class="wp-block">All</a> ' ;
294+
295+ $ result = $ this ->instance ->render_state_attrs ( $ content , $ block );
296+
297+ $ this ->assertStringContainsString ( 'data-state-default ' , $ result );
298+ }
299+
300+ public function test_state_handles_leading_whitespace () {
301+ $ block = $ this ->make_block ( 'atomic-wind/box ' , array ( 'showIf ' => 'active ' ) );
302+ $ content = "\n<div class= \"wp-block \">Content</div> " ;
303+
304+ $ result = $ this ->instance ->render_state_attrs ( $ content , $ block );
305+
306+ $ this ->assertStringContainsString ( 'data-show-if="active" ' , $ result );
307+ }
308+
309+ public function test_state_trigger_value_escapes_xss () {
310+ $ block = $ this ->make_block ( 'atomic-wind/link ' , array (
311+ 'stateTrigger ' => 'x ' ,
312+ 'stateAction ' => 'set ' ,
313+ 'stateValue ' => '"><script>alert(1)</script> ' ,
314+ ) );
315+ $ content = '<a class="wp-block">Link</a> ' ;
316+
317+ $ result = $ this ->instance ->render_state_attrs ( $ content , $ block );
318+
319+ $ this ->assertStringNotContainsString ( '<script> ' , $ result );
320+ }
321+
322+ public function test_state_skips_trigger_when_already_present () {
323+ $ block = $ this ->make_block ( 'atomic-wind/link ' , array (
324+ 'stateTrigger ' => 'tabs ' ,
325+ 'stateAction ' => 'set ' ,
326+ 'stateValue ' => 'one ' ,
327+ ) );
328+ $ content = '<a data-state-trigger="tabs" data-state-action="set" data-state-value="one" class="wp-block">Tab</a> ' ;
329+
330+ $ result = $ this ->instance ->render_state_attrs ( $ content , $ block );
331+
252332 $ this ->assertSame ( $ content , $ result );
253333 }
254334
0 commit comments