vishal.varshney
3 years ago

How to test form containing php captcha validation in laravel

Posted 3 years ago by vishal.varshney

hi guys, while writing php unit test case in laravel, how we send php captcha value. i am using php captcha

public function testSignupStoreFunction() {
        $data['first_name'] = "vsdghvfas";
        $data['last_name'] = "asdas";
        $data['email_id'] = "[email protected]";
        $data['password'] = "asd1#";
        $data['password_confirmation'] = "asd1#";
        $data['d_o_b'] = "1/20/1970";
        $data['gender'] = "M";
        $data['terms_condition'] = true;
        Session::start(); // Start a session for the current test
        $data['captcha_code'] = ""; // session('catcha_code')// <---------------------- here
        $data['_token'] = csrf_token(); // Retrieve current csrf token
        $response = $this->call('POST', 'candidate/store', $data); 
        $this->assertResponseStatus(302);
    }

My rules in CandidateSignupRequest.php

public function rules()
    {
        return [
            'first_name'=>'required|string',
            'last_name'=>'required|string',
            'email_id'=>'bail|required|email|unique:r2_roster,ros_unv_email',
            'password'=>'required|regex:/^(?=.*[a-z])(?=.*\d)(?=.*[^a-zA-Z\d])[#-_&a-zA-Z\d]+$/',
            'password_confirmation'=>'required|same:password',
            'gender'=>'required|in:M,F',
            'd_o_b'=>'required|date',
            'captcha_code'=>'required|in:'.session("captcha_code"),
            'terms_condition'=>'required|accepted',
        ];
    }

Please sign in or create an account to participate in this conversation.