<?php
use App\User;
use App\Employee;

class EmployeeTest extends TestCase
{
    /**
     * get employees
     *
     * @return void
     */
    public function testEmployee()
    {
     $this->post('api/v1/employee/all', [
                'feedback'     => 'yes',
                ])
                 ->seeJson([
                    'status' => 'success',
                 ]);
    }



    public function testEmployeeOther(){
    $this->post('api/v1/employee/all', [
                'feedback'     => 'no',
                ])
                 ->seeJson([
                    'status' => 'success',
                 ]);
}

    /**
     * Test to check save manager
     *
     * @return void
     */
    public function testSaveEmployee()
    {
       $subManager = User::where('role', 'ROLE_MANAGER')->where('plan','!=',null)->first();
       $manager = User::where('role', 'ROLE_MANAGER')->where('plan',null)->first();
       if($subManager){
       $this->post('api/v1/employee', [
              'name'            => 'sample',
              'email'           => str_random(5).'@ggg.com',
              'password'        => '123456',
              'mobile'          => '1234567',
              'organization_id' => 1,
              'branch_id'       => 1,
              'status'          => 1,
              'role'            => 'ROLE_EMPLOYEE',
              'userId'          => $subManager->id
            ])
             ->seeJson([
                'status' => 'success',
             ]);

       }
       $this->post('api/v1/employee', [
              'name'            => 'sample',
              'email'           => str_random(5).'@ggg.com',
              'password'        => '123456',
              'mobile'          => '1234567',
              'organization_id' => 1,
              'branch_id'       => 1,
              'status'          => 1,
              'role'            => 'ROLE_EMPLOYEE',
              'userId'          => 10
            ])
             ->seeJson([
                'status' => 'fail',
             ]);

       $this->post('api/v1/employee', [
              'name'            => 'sample1111',
              'email'           => str_random(5).'@ggg.com',
              'password'        => '123456',
              'mobile'          => '1234567',
              'organization_id' => 1,
              'branch_id'       => 1,
              'status'          => 1,
              'role'            => 'ROLE_EMPLOYEE',
              'userId'          => 10
            ])
             ->seeJson([
                'status' => 'fail',
             ]);
       
       $this->post('api/v1/employee', [
              'name'            => 'sample',
              'email'           => str_random(5).'@ggg.com',
              'password'        => '123456',
              'mobile'          => '1234567',
              'organization_id' => 1,
              'branch_id'       => 1,
              'status'          => 1,
              'role'            => 'ROLE_EMPLOYEE',
              'userId'          => 27
            ])
             ->seeJson([
                'status' => 'fail',
             ]);
    }
    
    
    /**
     * Test to check get user
     *
     * @return void
     */
    public function testGetEmployee()
    {
       $response = $this->call('GET', 'api/v1/employee/4');
       $this->assertEquals(200, $response->status());
    }
    
    
     /**
     * Test to check change status
     *
     * @return void
     */
    public function testChangeEmployeeStatus()
    {
        $this->put('api/v1/employee', ['id' => '5','status' => 1])
             ->seeJson([
                'status' => 'success',
             ]);
       
        $this->put('api/v1/employee', ['id' => '1', 'status' => 0])
             ->seeJson([
                'status' => 'fail',
             ]);
    }
    
    /**
     * Test to check change status
     *
     * @return void
     */
    public function testUpdateEmployee()
    {
        $email = str_random(5).'@ggg.com';
        $email2 = str_random(5).'@ss.com';
        $this->put('api/v1/employee/5', ['name' => 'sam', 'password' => '123456','email' => $email, 'mobile' => '123456',
            'organization_id' => 1, 'branch_id' => 1, 'status' => 1])
             ->seeJson([
                'status' => 'success',
             ]);
       
        $this->put('api/v1/employee/11111', ['name' => 'sam', 'password' => '123456', 'email' => $email2, 'mobile' => '123456',
            'organization_id' => 1, 'branch_id' => 1, 'status' => 1])
             ->seeJson([
                'status' => 'fail',
             ]);
    }
    
    /**
     * Test to check save ownfeedback
     *
     * @return void
     */
    public function testOwnFeedback()
    {
        $feedback = str_random(20);
        $this->post('api/v1/employee/saveownfeedback', ['user_id' => 11580,
           'feedback' => $feedback,
           ])
             ->seeJson([
                'status' => 'fail',
             ]);
       $last = Employee::max('id');
       $employee = Employee::find($last);
       $this->post('api/v1/employee/saveownfeedback', ['user_id' => $employee->user_id,
           'feedback' => $feedback,
           ])
             ->seeJson([
                'status' => 'success',
             ]);
    }
    
    
    /**
     * Test to check selected employees
     *
     * @return void
     */
    public function testGetSelected()
    {
       $response = $this->call('POST', 'api/v1/employee/selected', ['manager_id' => 12, 'feedback' => 'Yes']); 
       $this->assertEquals(200, $response->status());
    }
    
    
    /**
     * Test to check selected employees
     *
     * @return void
     */
    public function testLogin()
    {
        $this->post('api/v1/employee/login', [
            'email'     => str_random(20).'@employee.com',
            'password'  => '123456',
            'device_id' => 'asas',
            'os_type'   => 'ios',
            ])
             ->seeJson([
                'status' => 'fail',
             ]);
        
         $employee = User::where('role', 'ROLE_EMPLOYEE')
                 ->orderBy('id', 'desc')->first();
         $useremail = $employee->email;
         $employee->status = 2;
         $employee->save();
        
         $this->post('api/v1/employee/login', [
             'email'     => $useremail,
             'password'  => '123456',
             'device_id' => 'asas',
             'os_type'   => 'ios',
             ])
              ->seeJson([
                 'status' => 'fail',
              ]);
         $employee->status = 1;
         $employee->save();
          $this->post('api/v1/employee/login', [
             'email'     => $useremail,
             'password'  => '123456',
             'device_id' => 'asas',
             'os_type'   => 'ios',
             ])
              ->seeJson([
                 'status' => 'success',
              ]);
    }
    
    
    /**
     * Test to check employee forgot password
     *
     * @return void
     */
    public function testEmployeeForgotPassword()
    {
        
        $user = DB::table('users')
                ->select('email')
                ->where('role', 'ROLE_EMPLOYEE')->where('status',1)->first();
        $this->post('api/v1/employee/forgotpassword', [
              'email' => 'ketan.patel@brainvire.com',
            ])
             ->seeJson([
                'status' => 'fail',
             ]);
        
        $this->post('api/v1/employee/forgotpassword', [
              'email' => 'employee@employee.com',
            ])
             ->seeJson([
                'status' => 'fail',
             ]);
        $this->post('api/v1/employee/forgotpassword', [
              'email' => $user->email,
            ])
             ->seeJson([
                'status' => 'success',
             ]);
        
    }
    
    
    /**
     * Test to check employee forgot password
     *
     * @return void
     */
    public function testEmployeeChangePassword()
    {
        // EMAIL_NOT_FOUND
        $user = DB::table('users')
                ->select('email')
                ->where('role', 'ROLE_EMPLOYEE')->where('status',1)->first();
        $this->post('api/v1/employee/changepassword', [
              'email' => str_random(4).'@gmail.com',
              'new_password'  => '123456',
              'temp_password' => '123456',
              'type' => 'before',
            ])
             ->seeJson([
                'status' => 'fail',
             ]);
        
       
        $user = User::where('email',$user->email)->first();
        $user->status = 2;
        $user->save();
        
        // ACC_BLOCKED
        $this->post('api/v1/employee/changepassword', [
             'email' => 'emp@emp.com',
             'new_password'  => '123456',
             'temp_password' => '1234567',
             'type' => 'before',
           ])
            ->seeJson([
               'status' => 'fail',
            ]);
        
        $user->status = 1;
        $user->password_token = 1234567;
        $user->save();
        
        // type before and password token not equal to temp pass : WRONG_TEMP_PASS
        $this->post('api/v1/employee/changepassword', [
              'email' => $user->email,
              'new_password'  => '123456',
              'temp_password' => '1',
              'type' => 'before',
            ])
             ->seeJson([
                'status' => 'fail',
             ]);
        $user->expire_on = '2016-12-12 00:00:00';
        $user->save();

        // type before and TEMP_PASS_EXPIRED        
        $this->post('api/v1/employee/changepassword', [
             'email' => $user->email,
             'new_password'  => '123456',
             'temp_password' => '1234567',
             'type' => 'before',
           ])
            ->seeJson([
               'status' => 'fail',
            ]);
        
        
        // type after and WRONG_CURRENT_PASS
        $this->post('api/v1/employee/changepassword', [
              'email' => $user->email,
              'new_password'  => '123456',
              'temp_password' => '11',
              'type' => 'after',
            ])
             ->seeJson([
                'status' => 'fail',
             ]);
        
        $user->status = 1;
        $date = new \DateTime();
        $user->expire_on = $date->format('Y-m-d H:i:s');
        $user->save();
        
        $this->post('api/v1/employee/changepassword', [
             'email' => $user->email,
             'new_password'  => '123456',
             'temp_password' => '1234567',
             'type' => 'before',
           ])
            ->seeJson([
               'status' => 'success',
            ]);
    }
    
    
    /**
     * Test to check change status
     *
     * @return void
     */
    public function testDashboard()
    {
        $this->post('api/v1/employee/dashboard', [
              'user_id' => '122',
              'duration' => 'today',
            ])
             ->seeJson([
                'status' => 'success',
             ]);
        
        $this->post('api/v1/employee/dashboard', [
              'user_id' => '122',
              'duration' => 'week',
            ])
             ->seeJson([
                'status' => 'success',
             ]);
        
        
        $this->post('api/v1/employee/dashboard', [
              'user_id' => '122',
              'duration' => 'month',
            ])
             ->seeJson([
                'status' => 'success',
             ]);
        
        $this->post('api/v1/employee/dashboard', [
              'user_id' => '122',
              'duration' => 'year',
            ])
             ->seeJson([
                'status' => 'success',
             ]);
        $this->post('api/v1/employee/dashboard', [
              'user_id' => '122',
              'duration' => 'default',
            ])
             ->seeJson([
                'status' => 'success',
             ]);
        
    }
    
    
    
    /**
     * Test to delete employee
     *
     * @return void
     */
    public function testDeleteEmployee()
    {
       $subManager = User::where('role', 'ROLE_MANAGER')->where('plan','!=',null)->first();
       if($subManager){
       $email = str_random(5).'@ggg.com';
       $this->post('api/v1/employee', [
              'name'            => 'sample',
              'email'           => $email,
              'password'        => '123456',
              'mobile'          => '1234567',
              'organization_id' => 1,
              'branch_id'       => 1,
              'status'          => 1,
              'role'            => 'ROLE_EMPLOYEE',
              'userId'          => $subManager->id
            ])
             ->seeJson([
                'status' => 'success',
             ]);
        
       }
        $result = User::where('ROLE','ROLE_EMPLOYEE')->first();;
        $id = $result->id;
        
        $this->json('DELETE', "api/v1/employee/$id")
             ->seeJson([
                'status' => 'success',
             ]);
    }
    
     
}
