Unit testing the output of a function that is hooked to comment_post action

Question

I am writing a unit test for a function that is hooked to the “comment_post” action in a plugin, I set up the basic unit testing code using wp-cli but I am stuck on how to write a unit test that testes the output of this function, I don’t know if I should call the action or mock it, and I couldn’t find a clear answer or documentation about the subject.

Here is the function

add_action( 'comment_post', 'show_message_function', 10, 2 );
function show_message_function( $comment_ID, $comment_approved) {
    
    if( 1 === $comment_approved ){
        $comment_id_7 = get_comment($comment_ID); 
        $comment_post_id = $comment_id_7->comment_post_ID ;
        $current_post = get_post($comment_post_id);
        $comment_author_email = $comment_id_7->comment_author_email;
        $comment_date = $comment_id_7->comment_date;
        $comment_content = $comment_id_7->comment_content;
        
        
        
        // getting important data from the post
        $post_title = $current_post->post_title;
        $post_name = $current_post->post_name;
        $post_guid = $current_post->guid;
        $post_type = $current_post->post_type;
        $post_id = $current_post->ID;
        
        // getting the user type
        $user = wp_get_current_user();
        $user_meta=get_userdata($user->ID);

        $user_roles=$user_meta->roles; //array of roles the user is part of.
        $user_type = $user_roles[0];
        
        switch($post_type):
            
            // post_type need to be project
            // and the id of the offer
            // milestone title it self, subject, message
            case "milestone":
                
                $parent_offer = get_field('parent_offer',$comment_post_id);
                // set the variables needed
                    $expert_id = get_field('expert',$parent_offer);
                    $expert_meta = get_userdata($expert_id);
                    $expert_email = $expert_meta->user_email;
                    $expert_name = $expert_meta->display_name;
                    
                    // get the client id
                     $request_id = get_field('request',$parent_offer);
                
                    if(is_array($request_id)){
                            $request_id = $request_id[0];
                     }
                    $client_id = get_field('client',$request_id);
                    $client_meta = get_userdata($client_id);
                    $client_email = $client_meta->user_email;
                    $client_name = $client_meta->display_name;
                
                
                switch($user_type):
                    
                case "sfl-expert":
                    
                    $subject = 'You have a new comment for the Milestone '. $post_title .' from expert ' . $expert_name;
                    $notification_title = 'new comment for the Milestone from expert ' . $expert_name; 
                    $notification_message = $comment_content;
                    $call_to_action = $post_guid;
                    $send_to = $client_email; // email of the client
                    $user_id_to = $client_id;
                    $user_type_to = 'sfl-client';
                    
                break;
                
                case "sfl-client":
                    
                
                    $subject = 'You have a new comment for the Milestone '. $post_title .' from client ' . $comment_author_email;
                    $notification_title = 'new comment for the Milestone from client ' . $comment_author_email; 
                    $notification_message = $comment_content;
                    $call_to_action = $post_guid;
                    $send_to = $expert_name; // email of the client
                    $user_id_to = $expert_id;
                    $user_type_to = 'sfl-expert';
                    
                break;
                
                case "sfl-staff":
                case "administrator":
                case "editor":
                    
                    $subject = 'You have a new  comment for the Milestone '. $post_title .' from saferfood staff ' . $comment_author_email;
                    $notification_title = 'new comment for the Milestone from saferfood staff ' . $comment_author_email; 
                    $notification_message = $comment_content;
                    $call_to_action = $post_guid;
                    $send_to = array($expert_name,$client_email);
                    $user_id_to = array($expert_id,$client_id);
                    $user_type_to = array('sfl-expert','sfl-client');
                    
                break;
                    
                endswitch;
                
                
                
            break;
            
            // no comments on offers,
            // should be only on projects or milestones, and both need to have the post_type project and post_id (project_id)
            // need to know if the project is active or not active (add field status in the notification)
            case "offer":
                
                // set the variables needed
                    $expert_id = get_field('expert',$comment_post_id);
                    $expert_meta = get_userdata($expert_id);
                    $expert_email = $expert_meta->user_email;
                    $expert_name = $expert_meta->display_name;
                    
                    // get the client id
                    $client_id = get_field('client',$comment_post_id);
                    $client_meta = get_userdata($client_id);
                    $client_email = $client_meta->user_email;
                    $client_name = $client_meta->display_name;
                
                switch($user_type):
                    
                case "sfl-expert":
                    
                    $subject = 'You have a new comment in Offer '. $post_title .' from expert ' . $expert_name;
                    $notification_title = 'new Offer from expert ' . $expert_name; 
                    $notification_message = $comment_content;
                    $call_to_action = $post_guid;
                    $send_to = $client_email; // email of the client
                    
                break;
                
                case "sfl-client":
                    
                
                    $subject = 'You have a new comment in Offer '. $post_title .' from client ' . $comment_author_email;
                    $notification_title = 'new comment in Offer from client ' . $comment_author_email; 
                    $notification_message = $comment_content;
                    $call_to_action = $post_guid;
                    $send_to = $expert_name; // email of the client
                    
                break;
                
                case "sfl-staff":
                case "administrator":
                case "editor":
                    
                    $subject = 'You have a new comment in Offer '. $post_title .' from saferfood staff ' . $comment_author_email;
                    $notification_title = 'new Offer from saferfood staff ' . $comment_author_email; 
                    $notification_message = $comment_content;
                    $call_to_action = $post_guid;
                    $send_to = array($expert_name,$client_email);
                    
                break;
                    
                endswitch;
                
               
            break;
            
        endswitch;

        
        $new_notification = 1;
        
        
        require plugin_dir_path( __FILE__ ) .'notifications/template.php';
        
    }
}

I started writing the tests and got stuck because I didn’t know how to continue

class SFLATest extends WP_UnitTestCase {

    /**
     * test the show_message_function function
     */
    public function test_show_message_function() {
        $post = $this->factory->post->create_and_get([
            "post_title" => "Test Post"
        ]);

        $comment = $this->factory->comment->create_and_get(

        );

        $action = new MockAction();
        add_action('my_custom_hook', [&$action, 'action']);
    }
}

I want to test if the function is giving an output based on the post and user types but I don’t know how to continue from here, any answer or example that can help is appreciated.

0
Muhamad Bhaa Asfour 4 months 0 Answers 14 views 0

Leave an answer