@@ -600,4 +600,99 @@ public function testCanStopErroredLogFromSavingViaFilter()
600600
601601 remove_filter ($ filterName , $ func );
602602 }
603+
604+ private function getFilterChainFunction (&$ wasChainedCalled , $ to , $ subject , $ message )
605+ {
606+ return function ($ args ) use (&$ wasChainedCalled , $ to , $ subject , $ message ) {
607+ $ wasChainedCalled = true ;
608+ $ this ->assertEquals ($ to , $ args ['to ' ]);
609+ $ this ->assertEquals ($ subject , $ args ['subject ' ]);
610+ $ this ->assertEquals ($ message , $ args ['message ' ]);
611+ return $ args ;
612+ };
613+ }
614+
615+ /**
616+ * Other plugins hook into the `wp_mail` filter AFTER mail catcher, as such
617+ * they will rely on the values we return from our function hooked into `wp_mail`.
618+ * This test ensures that we correctly return the value so other plugins can make
619+ * use of it
620+ */
621+ public function testCanChainWpMailFiltersWhenLog ()
622+ {
623+ $ to = 'test@test.com ' ;
624+ $ subject = 'Subject ' ;
625+ $ message = '<strong>Hello</strong> ' ;
626+ $ wasChainedCalled = false ;
627+ $ successFilterName = GeneralHelper::$ actionNameSpace . '_before_success_log_save ' ;
628+
629+ $ func = function ($ log ) {
630+ return $ log ;
631+ };
632+
633+ $ chainedFunc = $ this ->getFilterChainFunction ($ wasChainedCalled , $ to , $ subject , $ message );
634+
635+ add_filter ($ successFilterName , $ func );
636+ // Ensure our filter runs AFTER mail catcher
637+ add_filter ('wp_mail ' , $ chainedFunc , 9999999 );
638+
639+ wp_mail ($ to , $ subject , $ message );
640+
641+ $ this ->assertTrue ($ wasChainedCalled );
642+
643+ remove_filter ($ successFilterName , $ func );
644+ remove_filter ('wp_mail ' , $ chainedFunc );
645+ }
646+
647+ public function testCanChainWpMailFiltersWhenLogIsStopped ()
648+ {
649+ $ to = 'test@test.com ' ;
650+ $ subject = 'Subject ' ;
651+ $ message = '<strong>Hello</strong> ' ;
652+ $ wasChainedCalled = false ;
653+ $ successFilterName = GeneralHelper::$ actionNameSpace . '_before_success_log_save ' ;
654+
655+ $ func = function ($ log ) {
656+ return false ;
657+ };
658+
659+ $ chainedFunc = $ this ->getFilterChainFunction ($ wasChainedCalled , $ to , $ subject , $ message );
660+
661+ add_filter ($ successFilterName , $ func );
662+ // Ensure our filter runs AFTER mail catcher
663+ add_filter ('wp_mail ' , $ chainedFunc , 9999999 );
664+
665+ wp_mail ($ to , $ subject , $ message );
666+
667+ $ this ->assertTrue ($ wasChainedCalled );
668+
669+ remove_filter ($ successFilterName , $ func );
670+ remove_filter ('wp_mail ' , $ chainedFunc );
671+ }
672+
673+ public function testCanChainWpMailFiltersWhenLogIsErrored ()
674+ {
675+ $ to = 'testtest.com ' ;
676+ $ subject = 'Subject ' ;
677+ $ message = '<strong>Hello</strong> ' ;
678+ $ wasChainedCalled = false ;
679+ $ erroredFilterName = GeneralHelper::$ actionNameSpace . '_before_error_log_save ' ;
680+
681+ $ func = function ($ log ) {
682+ return false ;
683+ };
684+
685+ $ chainedFunc = $ this ->getFilterChainFunction ($ wasChainedCalled , $ to , $ subject , $ message );
686+
687+ add_filter ($ erroredFilterName , $ func );
688+ // Ensure our filter runs AFTER mail catcher
689+ add_filter ('wp_mail ' , $ chainedFunc , 9999999 );
690+
691+ wp_mail ($ to , $ subject , $ message );
692+
693+ $ this ->assertTrue ($ wasChainedCalled );
694+
695+ remove_filter ($ erroredFilterName , $ func );
696+ remove_filter ('wp_mail ' , $ chainedFunc );
697+ }
603698}
0 commit comments