@@ -408,4 +408,161 @@ public function test_unserialize_data_mixed_depth() {
408408 $ this ->assertStringContainsString ('name="base_group[test_group][deep_text]" ' , $ html );
409409 $ this ->assertStringContainsString ( '> ' . $ data ['test_group ' ]['deep_text ' ] . '</textarea> ' , $ html );
410410 }
411+
412+ /**
413+ * @see https://github.com/alleyinteractive/wordpress-fieldmanager/issues/831
414+ */
415+ public function test_term_saving_side_effects_on_term_update () {
416+ $ name = 'side-effect ' ;
417+ $ value = 'Fieldmanager was here ' ;
418+
419+ // Register a Fieldmanager Field and add the term context.
420+ $ fm = new Fieldmanager_TextField ( compact ( 'name ' ) );
421+ $ fm ->add_term_meta_box ( 'Testing Side Effects ' , [ $ this ->taxonomy ] );
422+
423+ // Set the POST data.
424+ $ _POST = [
425+ 'tag_ID ' => $ this ->term_id ,
426+ 'taxonomy ' => $ this ->taxonomy ,
427+ 'name ' => 'News ' ,
428+ 'slug ' => 'news ' ,
429+ 'description ' => 'General news ' ,
430+ 'parent ' => '-1 ' ,
431+ "fieldmanager- {$ name }-nonce " => wp_create_nonce ( "fieldmanager-save- {$ name }" ),
432+ $ name => $ value ,
433+ ];
434+
435+ // Trigger the intended save.
436+ do_action (
437+ 'edited_term ' ,
438+ $ this ->term_id ,
439+ $ this ->tt_id ,
440+ $ this ->taxonomy
441+ );
442+
443+ // Fake a side effect.
444+ $ side_effect_term = self ::factory ()->term ->create_and_get (
445+ [
446+ 'taxonomy ' => $ this ->taxonomy ,
447+ ]
448+ );
449+ do_action (
450+ 'edited_term ' ,
451+ $ side_effect_term ->term_id ,
452+ $ side_effect_term ->term_taxonomy_id ,
453+ $ this ->taxonomy
454+ );
455+
456+ $ this ->assertSame ( $ value , get_term_meta ( $ this ->term_id , $ name , true ) );
457+ $ this ->assertSame ( [], get_term_meta ( $ side_effect_term ->term_id , $ name ) );
458+ }
459+
460+ /**
461+ * @see https://github.com/alleyinteractive/wordpress-fieldmanager/issues/831
462+ */
463+ public function test_term_saving_side_effects_on_term_create () {
464+ $ name = 'side-effect ' ;
465+ $ value = 'Fieldmanager was here ' ;
466+ $ new_term_name = 'New Term ' ;
467+
468+ // Register a Fieldmanager Field and add the term context.
469+ $ fm = new Fieldmanager_TextField ( compact ( 'name ' ) );
470+ $ fm ->add_term_meta_box ( 'Testing Side Effects ' , [ $ this ->taxonomy ] );
471+
472+ // Set the POST data.
473+ $ _POST = [
474+ 'taxonomy ' => $ this ->taxonomy ,
475+ 'tag-name ' => $ new_term_name ,
476+ 'slug ' => '' ,
477+ 'description ' => '' ,
478+ 'parent ' => '-1 ' ,
479+ "fieldmanager- {$ name }-nonce " => wp_create_nonce ( "fieldmanager-save- {$ name }" ),
480+ $ name => $ value ,
481+ ];
482+
483+ // Trigger the intended save.
484+ $ new_term = wp_insert_term ( $ new_term_name , $ this ->taxonomy , $ _POST );
485+
486+ // Fake a side effect.
487+ $ side_effect_term = self ::factory ()->term ->create_and_get (
488+ [
489+ 'taxonomy ' => $ this ->taxonomy ,
490+ ]
491+ );
492+ do_action (
493+ 'edited_term ' ,
494+ $ side_effect_term ->term_id ,
495+ $ side_effect_term ->term_taxonomy_id ,
496+ $ this ->taxonomy
497+ );
498+
499+ $ this ->assertSame ( $ value , get_term_meta ( $ new_term ['term_id ' ], $ name , true ) );
500+ $ this ->assertSame ( [], get_term_meta ( $ side_effect_term ->term_id , $ name ) );
501+ }
502+
503+ /**
504+ * @see https://github.com/alleyinteractive/wordpress-fieldmanager/issues/831
505+ */
506+ public function test_term_meta_saving_on_term_create_when_a_filter_alters_the_term_name () {
507+ $ name = 'testing ' ;
508+ $ value = 'Fieldmanager was here ' ;
509+ $ new_term_name = 'New Term ' ;
510+
511+ // Register a Fieldmanager Field and add the term context.
512+ $ fm = new Fieldmanager_TextField ( compact ( 'name ' ) );
513+ $ fm ->add_term_meta_box ( 'Testing ' , [ $ this ->taxonomy ] );
514+
515+ // Set the POST data.
516+ $ _POST = [
517+ 'taxonomy ' => $ this ->taxonomy ,
518+ 'tag-name ' => $ new_term_name ,
519+ 'slug ' => '' ,
520+ 'description ' => '' ,
521+ 'parent ' => '-1 ' ,
522+ "fieldmanager- {$ name }-nonce " => wp_create_nonce ( "fieldmanager-save- {$ name }" ),
523+ $ name => $ value ,
524+ ];
525+
526+ // Manipualte the term name prior to insert.
527+ add_filter (
528+ 'pre_insert_term ' ,
529+ function ( $ term_name ) {
530+ return "Edited: {$ term_name }" ;
531+ }
532+ );
533+
534+ // Insert the term.
535+ $ term = wp_insert_term ( $ new_term_name , $ this ->taxonomy , $ _POST );
536+
537+ $ this ->assertSame ( $ value , get_term_meta ( $ term ['term_id ' ], $ name , true ) );
538+ }
539+
540+ /**
541+ * @see https://github.com/alleyinteractive/wordpress-fieldmanager/issues/831
542+ */
543+ public function test_term_meta_saving_on_term_create_when_term_name_has_special_characters () {
544+ $ name = 'testing ' ;
545+ $ value = 'Fieldmanager was here ' ;
546+ $ new_term_name = 'Aprés & Mañana™ ' ;
547+
548+ // Register a Fieldmanager Field and add the term context.
549+ $ fm = new Fieldmanager_TextField ( compact ( 'name ' ) );
550+ $ fm ->add_term_meta_box ( 'Testing ' , [ $ this ->taxonomy ] );
551+
552+ // Set the POST data.
553+ $ _POST = [
554+ 'taxonomy ' => $ this ->taxonomy ,
555+ 'tag-name ' => $ new_term_name ,
556+ 'slug ' => '' ,
557+ 'description ' => '' ,
558+ 'parent ' => '-1 ' ,
559+ "fieldmanager- {$ name }-nonce " => wp_create_nonce ( "fieldmanager-save- {$ name }" ),
560+ $ name => $ value ,
561+ ];
562+
563+ // Insert the term.
564+ $ term = wp_insert_term ( $ new_term_name , $ this ->taxonomy , $ _POST );
565+
566+ $ this ->assertSame ( $ value , get_term_meta ( $ term ['term_id ' ], $ name , true ) );
567+ }
411568}
0 commit comments