/**
  * Setup the test environment.
  *
  * @return void
  */
 public function setUp()
 {
     parent::setUp();
     $this->signInWithManager();
     $this->propertyId = $this->getTestPropertyId(true);
     $this->userId = User::where('username', 'test')->first()->getAttribute('id');
 }
 /**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     if (app()->environment(['local', 'testing'])) {
         foreach (['test' => 'student', 'testLab' => 'lab', 'testManager' => 'manager'] as $key => $value) {
             if (!User::where('username', $key)->exists()) {
                 factory(User::class)->create(['username' => $key, 'password' => bcrypt('test')])->roles()->save(Role::where('name', $value)->first());
             }
         }
     }
 }
 /**
  * Login with the provided username & password in local first, if
  * failed, try login in center.
  *
  * @param Request
  * @return Json
  */
 public function login(Request $request)
 {
     $username = $request->input('username');
     $password = $request->input('password');
     // if login failed
     if (!Auth::attempt(['username' => $username, 'password' => $password]) && self::loginCheckSSO($username, $password) === 'SUCCESS') {
         $user = User::where('username', '=', $username)->first();
         Auth::login($user);
     }
     $response = ['is_student' => \Entrust::hasRole('student'), 'is_manager' => \Entrust::hasRole('manager'), 'status' => Auth::check()];
     return response()->json($response);
 }
 /**
  * Display a listing of the others borrow.
  *
  * @param Request $request
  * @return \Illuminate\Http\Response
  */
 public function login($account, $passwd)
 {
     if (login_check_sso($account, $passwd) == 'SUCCESS') {
         $user = User::where('username', '=', $account)->find();
         if ($user == null) {
             //add this user to database;
             //User::create();
             //Role::Add...;
             $user = User::where('username', '=', $account)->find();
         }
         Auth::loginUsingId($user->id);
     } else {
         //Manager
         $user = User::where('username', '=', $account)->where('password', '=', $passwd)->find();
         if ($user != null) {
             Auth::loginUsingId($user->id);
         }
     }
 }
 /**
  * Remove the specified resource from storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @return \Illuminate\Http\Response
  */
 public function destroy(Request $request)
 {
     $result = User::whereIn('username', $request->input('usernames'))->delete();
     return response()->json(['status' => $result ? 0 : 2]);
 }
    $faker = Faker\Factory::create('zh_TW');
    return ['name' => $faker->name, 'describe' => $faker->realText(16), 'category' => Category::getCategories('property')->random()->getAttribute('id'), 'status' => Category::getCategories('property.status')->random()->getAttribute('id'), 'code' => mt_rand(10000000, 99999999)];
});
$factory->define(\App\Affair\Repair::class, function () {
    $faker = Faker\Factory::create('zh_TW');
    return ['user_id' => User::all()->random()->getAttribute('id'), 'title' => mt_rand(0, 1) ? $faker->realText(mt_rand(10, 15)) : '', 'type' => Category::getCategories('repair.type')->random()->getAttribute('id'), 'remark' => $faker->realText(32), 'status' => Category::getCategories('repair.status')->random()->getAttribute('id')];
});
$factory->define(\App\Affair\Loan::class, function () {
    $faker = Faker\Factory::create('zh_TW');
    $time = mt_rand(0, 1);
    // get randomal Timezone
    $began_date = Timezone::all()->random()->getAttribute('date_began_at');
    $day = (new Carbon($began_date))->addDays(rand(1, 10));
    // get date begin/end
    $begin = $day->toDateString();
    $end = $day->addDays(mt_rand(0, 10))->toDateString();
    return ['user_id' => User::all()->random()->getAttribute('id'), 'type' => Category::getCategories('loan.type')->random()->getAttribute('id'), 'date_began_at' => $begin, 'date_ended_at' => $end, 'time_began_at' => $time ? $day->addHours(mt_rand(0, 7))->toTimeString() : null, 'time_ended_at' => $time ? $day->addHours(mt_rand(7, 15))->toTimeString() : null, 'remark' => $faker->realText(32), 'status' => Category::getCategories('loan.status')->random()->getAttribute('id'), 'long_term_token' => $begin != $end ? mt_rand(0, 127) : 1 << date('w', strtotime($begin))];
});
$factory->define(\App\Affair\Timezone::class, function () {
    $faker = Faker\Factory::create('zh_TW');
    $day = Carbon::now()->startOfDay()->addDays(mt_rand(1, 365));
    $stu_start = clone $day;
    $lab_start = clone $day;
    if (mt_rand(0, 1) === 1) {
        $type = Category::getCategoryId('time.type', 'semester');
    } else {
        $type = Category::getCategoryId('time.type', 'vacation');
        $stu_start->subDays(7);
    }
    return ['zone_name' => $faker->realText(mt_rand(10, 15)), 'type' => $type, 'date_began_at' => $day->toDateString(), 'date_ended_at' => $day->addDays(mt_rand(30, 150))->toDateString(), 'stu_date_began_at' => $stu_start->toDateString(), 'lab_date_began_at' => $lab_start->toDateString()];
});
 /**
  * 以一般使用者身份登入.
  *
  * @param string $username
  */
 public function signIn($username = '******')
 {
     $this->actingAs(User::where('username', $username)->first());
 }
 /**
  * Store a newly created other property borrow in storage.
  *
  * @param Request $request
  * @return Json
  */
 public function store(Request $request)
 {
     $user = User::where('username', '=', $request->input('username'))->first();
     if ($user === null) {
         return response()->json(['status' => 2]);
     }
     // Borrow out other property, no check for user should borrow property by going to the office personally.
     Loan::create(array_merge(array_only($request->all(), ['property_id', 'date_began_at', 'date_ended_at', 'remark']), ['user_id' => $user->id, 'type' => Category::getCategoryId('loan.type', $request->input('type', 'others')), 'status' => Category::getCategoryId('loan.status', 'accepted')]));
     return response()->json(['status' => 0]);
 }
 /** @test */
 public function create_loan_2()
 {
     $this->call('POST', '/other-create', ['property_id' => $this->getTestPropertyId(), 'date_began_at' => '2016-03-02', 'date_ended_at' => '2016-03-07', 'user_id' => User::where('username', 'test')->first()->getAttribute('id'), 'type' => $this->randomCategoryName('loan.type')]);
     $this->assertResponseCreated();
     $this->seeJson();
 }