CakePHP 2.3 - Unit testing User Login

后端 未结 2 705
星月不相逢
星月不相逢 2021-01-13 05:23

I thought I have to ask here some help to my problem. I\'ve spend whole evening with this. I have a login method in UsersController like this:

p         


        
2条回答
  •  悲哀的现实
    2021-01-13 05:40

    I got this working with the following code:

    function testLogin() {
    
            //mock user
            $this->Users = $this->generate( 'Users', array(
                    'components' => array(
                        'Security' => array( '_validatePost' ),
                    )
                ) );
    
            //create user data array with valid info
            $data = array();
            $data['User']['student_number'] = 1234567;
            $data['User']['password'] = '[valid password here]';
    
            //test login action
            $result = $this->testAction( "/users/login", array(
                    "method" => "post",
                    "return" => "contents",
                    "data" => $data
                )
            );
    
            $foo[] = $this->view;
            //debug($foo);
    
            //test successful login
            $this->assertNotNull( $this->headers['Location'] );
            $this->assertContains( 'reservations', $this->headers['Location'] );
            $this->assertNotContains( '"/users/login" id="UserLoginForm"', $foo );
    
            //logout mocked user
            $this->Users->Auth->logout();
        }
    

提交回复
热议问题