2017-06-09 12:00:30 +08:00
|
|
|
<?php
|
|
|
|
|
2020-05-23 23:43:29 +08:00
|
|
|
namespace Tests;
|
|
|
|
|
2018-01-02 03:48:02 +08:00
|
|
|
use App\Models\Acars;
|
2020-02-25 08:35:28 +08:00
|
|
|
use App\Models\Aircraft;
|
2018-03-16 07:20:07 +08:00
|
|
|
use App\Models\Bid;
|
2018-01-02 03:48:02 +08:00
|
|
|
use App\Models\Enums\AcarsType;
|
2018-02-21 12:33:09 +08:00
|
|
|
use App\Models\Enums\PirepState;
|
2020-05-23 19:18:28 +08:00
|
|
|
use App\Models\Enums\UserState;
|
2020-05-23 23:43:29 +08:00
|
|
|
use App\Models\Flight;
|
|
|
|
use App\Models\Navdata;
|
2017-07-04 14:05:23 +08:00
|
|
|
use App\Models\Pirep;
|
2020-10-11 02:50:00 +08:00
|
|
|
use App\Models\Rank;
|
2018-01-02 03:48:02 +08:00
|
|
|
use App\Models\User;
|
2022-03-14 23:45:18 +08:00
|
|
|
use App\Notifications\Messages\Broadcast\PirepPrefiled;
|
|
|
|
use App\Notifications\Messages\Broadcast\PirepStatusChanged;
|
2019-11-20 23:16:01 +08:00
|
|
|
use App\Notifications\Messages\PirepAccepted;
|
2022-02-09 04:07:02 +08:00
|
|
|
use App\Notifications\Messages\PirepFiled;
|
2018-02-21 03:28:53 +08:00
|
|
|
use App\Repositories\SettingRepository;
|
2020-02-25 08:35:28 +08:00
|
|
|
use App\Services\AircraftService;
|
2019-11-06 00:44:31 +08:00
|
|
|
use App\Services\BidService;
|
2018-03-16 07:20:07 +08:00
|
|
|
use App\Services\FlightService;
|
2019-08-05 20:27:53 +08:00
|
|
|
use App\Services\PirepService;
|
2021-03-15 20:30:14 +08:00
|
|
|
use App\Support\Units\Fuel;
|
2018-02-21 12:33:09 +08:00
|
|
|
use Carbon\Carbon;
|
2019-11-20 23:16:01 +08:00
|
|
|
use Illuminate\Support\Facades\Notification;
|
2017-07-04 14:05:23 +08:00
|
|
|
|
2017-06-09 12:00:30 +08:00
|
|
|
class PIREPTest extends TestCase
|
|
|
|
{
|
2020-05-23 19:18:28 +08:00
|
|
|
/** @var PirepService */
|
2018-08-27 00:40:04 +08:00
|
|
|
protected $pirepSvc;
|
2020-05-23 19:18:28 +08:00
|
|
|
|
|
|
|
/** @var SettingRepository */
|
2018-08-27 00:40:04 +08:00
|
|
|
protected $settingsRepo;
|
2017-07-05 02:57:08 +08:00
|
|
|
|
2019-05-12 23:50:38 +08:00
|
|
|
public function setUp(): void
|
2017-06-09 12:00:30 +08:00
|
|
|
{
|
2019-05-12 23:50:38 +08:00
|
|
|
parent::setUp();
|
2017-07-04 14:05:23 +08:00
|
|
|
$this->addData('base');
|
2019-08-05 20:27:53 +08:00
|
|
|
$this->addData('fleet');
|
2018-02-21 02:59:49 +08:00
|
|
|
|
2019-08-05 20:27:53 +08:00
|
|
|
$this->pirepSvc = app(PirepService::class);
|
2018-02-21 02:59:49 +08:00
|
|
|
$this->settingsRepo = app(SettingRepository::class);
|
2017-07-04 14:05:23 +08:00
|
|
|
}
|
|
|
|
|
2018-01-02 03:48:02 +08:00
|
|
|
protected function createNewRoute()
|
|
|
|
{
|
|
|
|
$route = [];
|
2022-03-14 23:45:18 +08:00
|
|
|
$navpoints = Navdata::factory()->count(5)->create();
|
2018-01-02 03:48:02 +08:00
|
|
|
foreach ($navpoints as $point) {
|
|
|
|
$route[] = $point->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $route;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getAcarsRoute($pirep)
|
|
|
|
{
|
|
|
|
$saved_route = [];
|
|
|
|
$route_points = Acars::where(
|
|
|
|
['pirep_id' => $pirep->id, 'type' => AcarsType::ROUTE]
|
2018-01-02 23:40:42 +08:00
|
|
|
)->orderBy('order', 'asc')->get();
|
2018-01-02 03:48:02 +08:00
|
|
|
|
|
|
|
foreach ($route_points as $point) {
|
|
|
|
$saved_route[] = $point->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $saved_route;
|
|
|
|
}
|
|
|
|
|
2018-08-27 02:50:08 +08:00
|
|
|
/**
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2017-07-04 14:05:23 +08:00
|
|
|
public function testAddPirep()
|
|
|
|
{
|
2022-03-14 23:45:18 +08:00
|
|
|
$user = User::factory()->create();
|
2020-05-23 19:18:28 +08:00
|
|
|
|
2018-01-02 03:48:02 +08:00
|
|
|
$route = $this->createNewRoute();
|
2022-03-14 23:45:18 +08:00
|
|
|
$pirep = Pirep::factory()->create([
|
2018-01-11 02:39:13 +08:00
|
|
|
'user_id' => $user->id,
|
2018-08-27 00:40:04 +08:00
|
|
|
'route' => implode(' ', $route),
|
2018-01-02 03:48:02 +08:00
|
|
|
]);
|
|
|
|
|
2017-07-04 14:05:23 +08:00
|
|
|
$pirep = $this->pirepSvc->create($pirep, []);
|
2018-08-27 02:51:47 +08:00
|
|
|
|
2018-08-27 02:50:08 +08:00
|
|
|
try {
|
|
|
|
$this->pirepSvc->saveRoute($pirep);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
throw $e;
|
|
|
|
}
|
2017-07-04 14:05:23 +08:00
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
/*
|
2017-12-20 10:19:36 +08:00
|
|
|
* Check the initial state info
|
2017-07-04 14:05:23 +08:00
|
|
|
*/
|
2017-12-20 10:19:36 +08:00
|
|
|
$this->assertEquals($pirep->state, PirepState::PENDING);
|
2017-07-04 14:05:23 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Now set the PIREP state to ACCEPTED
|
|
|
|
*/
|
2020-03-26 06:04:26 +08:00
|
|
|
$new_pirep_count = $pirep->user->flights + 1;
|
|
|
|
$new_flight_time = $pirep->user->flight_time + $pirep->flight_time;
|
2017-12-14 00:56:26 +08:00
|
|
|
|
2017-12-20 10:19:36 +08:00
|
|
|
$this->pirepSvc->changeState($pirep, PirepState::ACCEPTED);
|
2017-12-14 00:56:26 +08:00
|
|
|
$this->assertEquals($new_pirep_count, $pirep->pilot->flights);
|
|
|
|
$this->assertEquals($new_flight_time, $pirep->pilot->flight_time);
|
|
|
|
$this->assertEquals($pirep->arr_airport_id, $pirep->pilot->curr_airport_id);
|
2017-07-04 14:05:23 +08:00
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// Check the location of the current aircraft
|
2018-01-11 02:39:13 +08:00
|
|
|
$this->assertEquals($pirep->aircraft->airport_id, $pirep->arr_airport_id);
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// Also check via API:
|
|
|
|
$this->get('/api/fleet/aircraft/'.$pirep->aircraft_id, [], $user)
|
2022-03-14 23:45:18 +08:00
|
|
|
->assertJson(['data' => ['airport_id' => $pirep->arr_airport_id]]);
|
2018-01-11 02:39:13 +08:00
|
|
|
|
2019-11-20 23:16:01 +08:00
|
|
|
// Make sure a notification was sent out to both the user and the admin(s)
|
|
|
|
Notification::assertSentTo([$user], PirepAccepted::class);
|
|
|
|
|
2019-09-17 01:08:26 +08:00
|
|
|
// Try cancelling it
|
|
|
|
$uri = '/api/pireps/'.$pirep->id.'/cancel';
|
|
|
|
$response = $this->put($uri, [], [], $user);
|
|
|
|
$response->assertStatus(400);
|
|
|
|
|
2017-07-04 14:05:23 +08:00
|
|
|
/**
|
|
|
|
* Now go from ACCEPTED to REJECTED
|
|
|
|
*/
|
2017-12-14 00:56:26 +08:00
|
|
|
$new_pirep_count = $pirep->pilot->flights - 1;
|
|
|
|
$new_flight_time = $pirep->pilot->flight_time - $pirep->flight_time;
|
2017-12-20 10:19:36 +08:00
|
|
|
$this->pirepSvc->changeState($pirep, PirepState::REJECTED);
|
2017-12-14 00:56:26 +08:00
|
|
|
$this->assertEquals($new_pirep_count, $pirep->pilot->flights);
|
|
|
|
$this->assertEquals($new_flight_time, $pirep->pilot->flight_time);
|
2017-07-05 02:57:08 +08:00
|
|
|
$this->assertEquals($pirep->arr_airport_id, $pirep->pilot->curr_airport_id);
|
2018-01-02 03:48:02 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check the ACARS table
|
|
|
|
*/
|
|
|
|
$saved_route = $this->getAcarsRoute($pirep);
|
|
|
|
$this->assertEquals($route, $saved_route);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Recreate the route with new options points. Make sure that the
|
|
|
|
* old route is erased from the ACARS table and then recreated
|
|
|
|
*/
|
|
|
|
$route = $this->createNewRoute();
|
|
|
|
$pirep->route = implode(' ', $route);
|
|
|
|
$pirep->save();
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// this should delete the old route from the acars table
|
2018-01-02 03:48:02 +08:00
|
|
|
$this->pirepSvc->saveRoute($pirep);
|
|
|
|
|
|
|
|
$saved_route = $this->getAcarsRoute($pirep);
|
|
|
|
$this->assertEquals($route, $saved_route);
|
2017-07-05 02:57:08 +08:00
|
|
|
}
|
|
|
|
|
2018-02-21 04:07:33 +08:00
|
|
|
/**
|
|
|
|
* Make sure the unit conversions look to be proper
|
|
|
|
*/
|
|
|
|
public function testUnitFields()
|
|
|
|
{
|
2022-02-09 04:07:02 +08:00
|
|
|
/** @var Pirep $pirep */
|
2018-02-21 04:07:33 +08:00
|
|
|
$pirep = $this->createPirep();
|
|
|
|
$pirep->save();
|
|
|
|
|
|
|
|
$uri = '/api/pireps/'.$pirep->id;
|
|
|
|
|
|
|
|
$response = $this->get($uri);
|
|
|
|
$body = $response->json('data');
|
|
|
|
|
|
|
|
// Check that it has the fuel units
|
2021-03-15 20:30:14 +08:00
|
|
|
$this->assertHasKeys($body['block_fuel'], ['lbs', 'kg']);
|
2022-03-14 23:45:18 +08:00
|
|
|
$this->assertEquals(
|
|
|
|
round($pirep->block_fuel->toUnit('lbs')),
|
|
|
|
round($body['block_fuel']['lbs'])
|
|
|
|
);
|
2021-03-25 20:59:45 +08:00
|
|
|
|
2018-02-21 04:07:33 +08:00
|
|
|
$this->assertHasKeys($body['fuel_used'], ['lbs', 'kg']);
|
2022-03-14 23:45:18 +08:00
|
|
|
$this->assertEquals(
|
|
|
|
round($pirep->fuel_used->toUnit('lbs')),
|
|
|
|
round($body['fuel_used']['lbs'])
|
|
|
|
);
|
2018-02-21 04:07:33 +08:00
|
|
|
|
|
|
|
// Check that it has the distance units
|
|
|
|
$this->assertHasKeys($body['distance'], ['km', 'nmi', 'mi']);
|
2022-03-14 23:45:18 +08:00
|
|
|
$this->assertEquals(
|
|
|
|
round($pirep->distance->toUnit('nmi')),
|
|
|
|
round($body['distance']['nmi'])
|
|
|
|
);
|
2018-02-21 04:07:33 +08:00
|
|
|
|
|
|
|
// Check the planned_distance field
|
|
|
|
$this->assertHasKeys($body['planned_distance'], ['km', 'nmi', 'mi']);
|
2022-03-14 23:45:18 +08:00
|
|
|
$this->assertEquals(
|
|
|
|
round($pirep->planned_distance->toUnit('nmi')),
|
|
|
|
round($body['planned_distance']['nmi'])
|
|
|
|
);
|
2021-03-15 20:30:14 +08:00
|
|
|
|
|
|
|
//Check conversion on save
|
|
|
|
$val = random_int(1000, 9999999);
|
|
|
|
$pirep->block_fuel = $val;
|
|
|
|
$pirep->fuel_used = $val;
|
2021-03-25 20:59:45 +08:00
|
|
|
|
2021-03-15 20:30:14 +08:00
|
|
|
// no conversion with plain numbers
|
2022-03-14 23:45:18 +08:00
|
|
|
$this->assertEquals($pirep->block_fuel->internal(2), $val);
|
|
|
|
$this->assertEquals($pirep->fuel_used->internal(2), $val);
|
2021-03-25 20:59:45 +08:00
|
|
|
|
2021-03-15 20:30:14 +08:00
|
|
|
// no conversion with lbs
|
|
|
|
$pirep->block_fuel = new Fuel($val, 'lbs');
|
2022-03-14 23:45:18 +08:00
|
|
|
$this->assertEquals($pirep->block_fuel->internal(2), round($val, 2));
|
2021-03-25 20:59:45 +08:00
|
|
|
|
2021-03-15 20:30:14 +08:00
|
|
|
$pirep->fuel_used = new Fuel($val, 'lbs');
|
2022-03-14 23:45:18 +08:00
|
|
|
$this->assertEquals($pirep->fuel_used->internal(2), round($val, 2));
|
2021-03-25 20:59:45 +08:00
|
|
|
|
2021-03-15 20:30:14 +08:00
|
|
|
// conversion of kg to lbs
|
|
|
|
$pirep->block_fuel = new Fuel($val, 'kg');
|
2022-03-14 23:45:18 +08:00
|
|
|
$this->assertEquals(
|
|
|
|
$pirep->block_fuel->internal(2),
|
|
|
|
Fuel::make($val, 'kg')->toUnit('lbs', 2)
|
|
|
|
);
|
2021-03-25 20:59:45 +08:00
|
|
|
|
2021-03-15 20:30:14 +08:00
|
|
|
$pirep->fuel_used = new Fuel($val, 'kg');
|
2022-03-14 23:45:18 +08:00
|
|
|
$this->assertEquals(
|
|
|
|
$pirep->fuel_used->internal(2),
|
|
|
|
Fuel::make($val, 'kg')->toUnit('lbs', 2)
|
|
|
|
);
|
2018-02-21 04:07:33 +08:00
|
|
|
}
|
|
|
|
|
2018-02-20 00:35:49 +08:00
|
|
|
public function testGetUserPireps()
|
|
|
|
{
|
2022-03-14 23:45:18 +08:00
|
|
|
$this->user = User::factory()->create();
|
|
|
|
$pirep_done = Pirep::factory()->create([
|
2018-02-20 00:35:49 +08:00
|
|
|
'user_id' => $this->user->id,
|
2018-08-27 00:40:04 +08:00
|
|
|
'state' => PirepState::ACCEPTED,
|
2018-02-20 00:35:49 +08:00
|
|
|
]);
|
|
|
|
|
2022-03-14 23:45:18 +08:00
|
|
|
$pirep_in_progress = Pirep::factory()->create([
|
2018-02-20 00:35:49 +08:00
|
|
|
'user_id' => $this->user->id,
|
2018-08-27 00:40:04 +08:00
|
|
|
'state' => PirepState::IN_PROGRESS,
|
2018-02-20 00:35:49 +08:00
|
|
|
]);
|
|
|
|
|
2022-03-14 23:45:18 +08:00
|
|
|
$pirep_cancelled = Pirep::factory()->create([
|
2018-02-20 00:35:49 +08:00
|
|
|
'user_id' => $this->user->id,
|
2018-08-27 00:40:04 +08:00
|
|
|
'state' => PirepState::CANCELLED,
|
2018-02-20 00:35:49 +08:00
|
|
|
]);
|
|
|
|
|
|
|
|
$pireps = $this->get('/api/user/pireps')
|
2022-03-14 23:45:18 +08:00
|
|
|
->assertStatus(200)
|
|
|
|
->json();
|
2018-02-20 00:35:49 +08:00
|
|
|
|
|
|
|
$pirep_ids = collect($pireps['data'])->pluck('id');
|
|
|
|
|
|
|
|
$this->assertTrue($pirep_ids->contains($pirep_done->id));
|
|
|
|
$this->assertTrue($pirep_ids->contains($pirep_in_progress->id));
|
|
|
|
$this->assertFalse($pirep_ids->contains($pirep_cancelled->id));
|
|
|
|
|
|
|
|
// Get only status
|
|
|
|
$pireps = $this->get('/api/user/pireps?state='.PirepState::IN_PROGRESS)
|
|
|
|
->assertStatus(200)
|
|
|
|
->json();
|
|
|
|
|
|
|
|
$pirep_ids = collect($pireps['data'])->pluck('id');
|
|
|
|
$this->assertTrue($pirep_ids->contains($pirep_in_progress->id));
|
|
|
|
$this->assertFalse($pirep_ids->contains($pirep_done->id));
|
|
|
|
$this->assertFalse($pirep_ids->contains($pirep_cancelled->id));
|
|
|
|
}
|
|
|
|
|
2020-09-04 00:50:42 +08:00
|
|
|
/**
|
|
|
|
* Make sure that a notification has been sent out to admins when a PIREP is submitted
|
|
|
|
*
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function testPirepNotifications()
|
|
|
|
{
|
|
|
|
/** @var User $user */
|
2022-03-14 23:45:18 +08:00
|
|
|
$user = User::factory()->create([
|
2022-02-09 04:07:02 +08:00
|
|
|
'name' => 'testPirepNotifications user',
|
2020-09-04 00:50:42 +08:00
|
|
|
'flights' => 0,
|
|
|
|
'flight_time' => 0,
|
|
|
|
'rank_id' => 1,
|
|
|
|
]);
|
|
|
|
|
2022-02-09 04:07:02 +08:00
|
|
|
$admin = $this->createAdminUser(['name' => 'testPirepNotifications Admin']);
|
2020-09-04 00:50:42 +08:00
|
|
|
|
2022-03-14 23:45:18 +08:00
|
|
|
$pirep = Pirep::factory()->create([
|
2020-09-04 00:50:42 +08:00
|
|
|
'airline_id' => 1,
|
|
|
|
'user_id' => $user->id,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->pirepSvc->create($pirep);
|
|
|
|
$this->pirepSvc->submit($pirep);
|
|
|
|
|
|
|
|
// Make sure a notification was sent out to the admin
|
2022-02-09 04:07:02 +08:00
|
|
|
Notification::assertSentTo([$admin], PirepFiled::class);
|
|
|
|
Notification::assertNotSentTo([$user], PirepFiled::class);
|
2020-09-04 00:50:42 +08:00
|
|
|
}
|
|
|
|
|
2017-07-05 02:57:08 +08:00
|
|
|
/**
|
|
|
|
* check the stats/ranks, etc have incremented properly
|
|
|
|
*/
|
|
|
|
public function testPilotStatsIncr()
|
|
|
|
{
|
2019-08-09 00:50:43 +08:00
|
|
|
$this->updateSetting('pilots.count_transfer_hours', false);
|
|
|
|
|
2022-02-09 04:07:02 +08:00
|
|
|
/** @var User $user */
|
2022-03-14 23:45:18 +08:00
|
|
|
$user = User::factory()->create([
|
2018-08-27 00:40:04 +08:00
|
|
|
'flights' => 0,
|
2018-01-03 01:12:53 +08:00
|
|
|
'flight_time' => 0,
|
2018-08-27 00:40:04 +08:00
|
|
|
'rank_id' => 1,
|
2018-01-03 01:12:53 +08:00
|
|
|
]);
|
2017-12-14 00:56:26 +08:00
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// Submit two PIREPs
|
2022-03-14 23:45:18 +08:00
|
|
|
$pireps = Pirep::factory()->count(2)->create([
|
2019-08-05 20:27:53 +08:00
|
|
|
'airline_id' => $user->airline_id,
|
|
|
|
'aircraft_id' => 1,
|
|
|
|
'user_id' => $user->id,
|
2018-08-27 00:40:04 +08:00
|
|
|
// 360min == 6 hours, rank should bump up
|
2017-12-14 00:56:26 +08:00
|
|
|
'flight_time' => 360,
|
|
|
|
]);
|
|
|
|
|
2020-05-06 02:36:14 +08:00
|
|
|
$aircraft = Aircraft::find(1);
|
|
|
|
$flight_time_initial = $aircraft->flight_time;
|
|
|
|
|
2017-12-14 00:56:26 +08:00
|
|
|
foreach ($pireps as $pirep) {
|
|
|
|
$this->pirepSvc->create($pirep);
|
|
|
|
$this->pirepSvc->accept($pirep);
|
|
|
|
}
|
|
|
|
|
2022-02-09 04:07:02 +08:00
|
|
|
/** @var User $pilot */
|
2018-01-03 01:12:53 +08:00
|
|
|
$pilot = User::find($user->id);
|
2017-07-05 02:57:08 +08:00
|
|
|
$last_pirep = Pirep::where('id', $pilot->last_pirep_id)->first();
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// Make sure rank went up
|
2018-01-03 01:12:53 +08:00
|
|
|
$this->assertGreaterThan($user->rank_id, $pilot->rank_id);
|
2017-07-05 02:57:08 +08:00
|
|
|
$this->assertEquals($last_pirep->arr_airport_id, $pilot->curr_airport_id);
|
2019-08-02 03:23:59 +08:00
|
|
|
$this->assertEquals(2, $pilot->flights);
|
|
|
|
|
2020-05-06 02:36:14 +08:00
|
|
|
$aircraft = Aircraft::find(1);
|
|
|
|
$after_time = $flight_time_initial + 720;
|
|
|
|
$this->assertEquals($after_time, $aircraft->flight_time);
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
//
|
|
|
|
// Submit another PIREP, adding another 6 hours
|
|
|
|
// it should automatically be accepted
|
|
|
|
//
|
2022-03-14 23:45:18 +08:00
|
|
|
$pirep = Pirep::factory()->create([
|
2017-12-26 05:19:34 +08:00
|
|
|
'airline_id' => 1,
|
2018-08-27 00:40:04 +08:00
|
|
|
'user_id' => $user->id,
|
|
|
|
// 120min == 2 hours, currently at 9 hours
|
|
|
|
// Rank bumps up at 10 hours
|
2017-12-14 00:56:26 +08:00
|
|
|
'flight_time' => 120,
|
|
|
|
]);
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// Pilot should be at rank 2, where accept should be automatic
|
2017-12-14 00:56:26 +08:00
|
|
|
$this->pirepSvc->create($pirep);
|
2018-05-10 23:35:10 +08:00
|
|
|
$this->pirepSvc->submit($pirep);
|
2017-12-14 00:56:26 +08:00
|
|
|
|
|
|
|
$pilot->refresh();
|
2019-08-02 03:23:59 +08:00
|
|
|
|
|
|
|
$this->assertEquals(3, $pilot->flights);
|
|
|
|
|
2017-07-05 02:57:08 +08:00
|
|
|
$latest_pirep = Pirep::where('id', $pilot->last_pirep_id)->first();
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// Make sure PIREP was auto updated
|
2017-12-20 10:19:36 +08:00
|
|
|
$this->assertEquals(PirepState::ACCEPTED, $latest_pirep->state);
|
2017-12-14 00:56:26 +08:00
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// Make sure latest PIREP was updated
|
2017-07-05 02:57:08 +08:00
|
|
|
$this->assertNotEquals($last_pirep->id, $latest_pirep->id);
|
2017-06-09 12:00:30 +08:00
|
|
|
}
|
2018-01-03 03:17:22 +08:00
|
|
|
|
2020-10-11 02:50:00 +08:00
|
|
|
/**
|
|
|
|
* Assign a rank to a user which is not set to auto-promote; make that that the flight
|
|
|
|
* hours and submitted PIREP doesn't change the rank
|
|
|
|
*
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function testPilotDontChangeRank()
|
|
|
|
{
|
|
|
|
/** @var Rank $rank */
|
2022-03-14 23:45:18 +08:00
|
|
|
$rank = Rank::factory()->create([
|
2020-10-11 02:50:00 +08:00
|
|
|
'hours' => 15,
|
|
|
|
'auto_promote' => false,
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Set the user to the above rank, non-promote, they shouldn't bump down
|
|
|
|
|
|
|
|
/** @var User $user */
|
2022-03-14 23:45:18 +08:00
|
|
|
$user = User::factory()->create([
|
2020-10-11 02:50:00 +08:00
|
|
|
'flights' => 0,
|
|
|
|
'flight_time' => 0,
|
|
|
|
'rank_id' => $rank->id,
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Submit two PIREPs
|
2022-03-14 23:45:18 +08:00
|
|
|
$pirep = Pirep::factory()->create([
|
2020-10-11 02:50:00 +08:00
|
|
|
'airline_id' => $user->airline_id,
|
|
|
|
'aircraft_id' => 1,
|
|
|
|
'user_id' => $user->id,
|
|
|
|
'flight_time' => 10 * 60, // 10 hours, eligible for Junior First Officer
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->pirepSvc->create($pirep);
|
|
|
|
$this->pirepSvc->accept($pirep);
|
|
|
|
|
|
|
|
$pilot = User::find($user->id);
|
|
|
|
|
|
|
|
// Make sure rank didn't change
|
|
|
|
$this->assertEquals($rank->id, $pilot->rank_id);
|
|
|
|
}
|
|
|
|
|
2019-08-09 00:50:43 +08:00
|
|
|
/**
|
|
|
|
* check the stats/ranks, etc have incremented properly
|
|
|
|
*/
|
|
|
|
public function testPilotStatsIncrWithTransferHours()
|
|
|
|
{
|
|
|
|
$this->updateSetting('pilots.count_transfer_hours', true);
|
|
|
|
|
2022-03-14 23:45:18 +08:00
|
|
|
$user = User::factory()->create([
|
2019-08-09 00:50:43 +08:00
|
|
|
'flights' => 0,
|
|
|
|
'flight_time' => 0,
|
|
|
|
'transfer_time' => 720,
|
|
|
|
'rank_id' => 1,
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Submit two PIREPs
|
|
|
|
// 1 hour flight times, but the rank should bump up because of the transfer hours
|
2022-03-14 23:45:18 +08:00
|
|
|
$pireps = Pirep::factory()->count(2)->create([
|
2019-08-09 00:50:43 +08:00
|
|
|
'airline_id' => $user->airline_id,
|
|
|
|
'aircraft_id' => 1,
|
|
|
|
'user_id' => $user->id,
|
|
|
|
'flight_time' => 60,
|
|
|
|
]);
|
|
|
|
|
|
|
|
foreach ($pireps as $pirep) {
|
|
|
|
$this->pirepSvc->create($pirep);
|
|
|
|
$this->pirepSvc->accept($pirep);
|
|
|
|
}
|
|
|
|
|
|
|
|
$pilot = User::find($user->id);
|
2020-06-05 21:19:18 +08:00
|
|
|
$last_pirep = $pilot->last_pirep;
|
|
|
|
$this->assertEquals($pilot->last_pirep_id, $last_pirep->id);
|
2019-08-09 00:50:43 +08:00
|
|
|
|
|
|
|
// Make sure rank went up
|
|
|
|
$this->assertGreaterThan($user->rank_id, $pilot->rank_id);
|
2020-02-25 08:35:28 +08:00
|
|
|
|
|
|
|
// Check the aircraft
|
|
|
|
$aircraft = Aircraft::where('id', 1)->first();
|
|
|
|
$this->assertEquals(120, $aircraft->flight_time);
|
|
|
|
|
|
|
|
// Reset the aircraft flight time
|
|
|
|
$aircraft->flight_time = 10;
|
|
|
|
$aircraft->save();
|
|
|
|
|
|
|
|
// Recalculate the status
|
|
|
|
/** @var AircraftService $aircraftSvc */
|
|
|
|
$aircraftSvc = app(AircraftService::class);
|
|
|
|
$aircraftSvc->recalculateStats();
|
|
|
|
|
|
|
|
$aircraft = Aircraft::where('id', 1)->first();
|
|
|
|
$this->assertEquals(120, $aircraft->flight_time);
|
2019-08-09 00:50:43 +08:00
|
|
|
}
|
|
|
|
|
2020-05-23 19:18:28 +08:00
|
|
|
/**
|
|
|
|
* When a PIREP is filed by a user on leave, make sure they flip from leave to active
|
|
|
|
* It doesn't matter if the PIREP is accepted or rejected
|
|
|
|
*/
|
|
|
|
public function testPilotStatusChange()
|
|
|
|
{
|
|
|
|
/** @var \App\Models\User $user */
|
2022-03-14 23:45:18 +08:00
|
|
|
$user = User::factory()->create([
|
2020-05-23 19:18:28 +08:00
|
|
|
'state' => UserState::ON_LEAVE,
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Submit two PIREPs
|
|
|
|
// 1 hour flight times, but the rank should bump up because of the transfer hours
|
2021-01-25 19:54:17 +08:00
|
|
|
/** @var Pirep $pirep */
|
2022-03-14 23:45:18 +08:00
|
|
|
$pirep = Pirep::factory()->create([
|
2020-05-23 19:18:28 +08:00
|
|
|
'airline_id' => $user->airline_id,
|
|
|
|
'user_id' => $user->id,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->pirepSvc->create($pirep);
|
2021-01-25 19:54:17 +08:00
|
|
|
$this->pirepSvc->submit($pirep);
|
2020-05-23 19:18:28 +08:00
|
|
|
|
2021-01-25 19:54:17 +08:00
|
|
|
/** @var User $user */
|
2020-05-23 19:18:28 +08:00
|
|
|
$user = User::find($user->id);
|
|
|
|
$this->assertEquals(UserState::ACTIVE, $user->state);
|
|
|
|
}
|
|
|
|
|
2018-01-03 03:17:22 +08:00
|
|
|
/**
|
|
|
|
* Find and check for any duplicate PIREPs by a user
|
|
|
|
*/
|
|
|
|
public function testDuplicatePireps()
|
|
|
|
{
|
2022-03-14 23:45:18 +08:00
|
|
|
$user = User::factory()->create();
|
|
|
|
$pirep = Pirep::factory()->create([
|
2018-08-27 00:40:04 +08:00
|
|
|
'user_id' => $user->id,
|
2018-01-07 02:07:22 +08:00
|
|
|
]);
|
2018-01-03 03:17:22 +08:00
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// This should find itself...
|
2018-01-03 03:17:22 +08:00
|
|
|
$dupe_pirep = $this->pirepSvc->findDuplicate($pirep);
|
|
|
|
$this->assertNotFalse($dupe_pirep);
|
|
|
|
$this->assertEquals($pirep->id, $dupe_pirep->id);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a PIREP outside of the check time interval
|
|
|
|
*/
|
|
|
|
$minutes = setting('pireps.duplicate_check_time') + 1;
|
2022-03-14 23:45:18 +08:00
|
|
|
$pirep = Pirep::factory()->create([
|
2021-04-13 21:25:38 +08:00
|
|
|
'created_at' => Carbon::now('UTC')->subMinutes($minutes)->toDateTimeString(),
|
2018-01-03 03:17:22 +08:00
|
|
|
]);
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// This should find itself...
|
2018-01-03 03:17:22 +08:00
|
|
|
$dupe_pirep = $this->pirepSvc->findDuplicate($pirep);
|
|
|
|
$this->assertFalse($dupe_pirep);
|
|
|
|
}
|
2018-01-04 00:25:55 +08:00
|
|
|
|
|
|
|
public function testCancelViaAPI()
|
|
|
|
{
|
2018-02-21 02:59:49 +08:00
|
|
|
$pirep = $this->createPirep()->toArray();
|
2018-01-05 11:05:26 +08:00
|
|
|
|
2018-01-04 00:25:55 +08:00
|
|
|
$uri = '/api/pireps/prefile';
|
2018-01-07 02:07:22 +08:00
|
|
|
$response = $this->post($uri, $pirep);
|
2018-02-11 08:17:38 +08:00
|
|
|
$pirep_id = $response->json()['data']['id'];
|
2018-01-04 00:25:55 +08:00
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
$uri = '/api/pireps/'.$pirep_id.'/acars/position';
|
2022-03-14 23:45:18 +08:00
|
|
|
$acars = Acars::factory()->make()->toArray();
|
2018-01-07 02:07:22 +08:00
|
|
|
$response = $this->post($uri, [
|
2018-08-27 00:40:04 +08:00
|
|
|
'positions' => [$acars],
|
2018-01-05 09:33:23 +08:00
|
|
|
]);
|
|
|
|
|
|
|
|
$response->assertStatus(200);
|
2018-01-04 00:25:55 +08:00
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// Cancel it
|
|
|
|
$uri = '/api/pireps/'.$pirep_id.'/cancel';
|
2018-01-07 02:07:22 +08:00
|
|
|
$response = $this->delete($uri, $acars);
|
2018-01-04 00:25:55 +08:00
|
|
|
$response->assertStatus(200);
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// Should get a 400 when posting an ACARS update
|
|
|
|
$uri = '/api/pireps/'.$pirep_id.'/acars/position';
|
2022-03-14 23:45:18 +08:00
|
|
|
$acars = Acars::factory()->make()->toArray();
|
2018-01-29 01:12:13 +08:00
|
|
|
|
2018-01-07 02:07:22 +08:00
|
|
|
$response = $this->post($uri, $acars);
|
2018-01-04 00:25:55 +08:00
|
|
|
$response->assertStatus(400);
|
|
|
|
}
|
2018-03-16 07:20:07 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* When a PIREP is accepted, ensure that the bid is removed
|
|
|
|
*/
|
|
|
|
public function testPirepBidRemoved()
|
|
|
|
{
|
2019-11-06 00:44:31 +08:00
|
|
|
$bidSvc = app(BidService::class);
|
2018-03-16 07:20:07 +08:00
|
|
|
$flightSvc = app(FlightService::class);
|
|
|
|
|
2022-03-14 23:45:18 +08:00
|
|
|
$user = User::factory()->create([
|
2018-03-16 07:20:07 +08:00
|
|
|
'flight_time' => 0,
|
|
|
|
]);
|
|
|
|
|
2022-03-14 23:45:18 +08:00
|
|
|
$flight = Flight::factory()->create([
|
2018-03-16 07:20:07 +08:00
|
|
|
'route_code' => null,
|
2018-08-27 00:40:04 +08:00
|
|
|
'route_leg' => null,
|
2018-03-16 07:20:07 +08:00
|
|
|
]);
|
|
|
|
|
2019-11-06 00:44:31 +08:00
|
|
|
$bidSvc->addBid($flight, $user);
|
2018-03-16 07:20:07 +08:00
|
|
|
|
2022-03-14 23:45:18 +08:00
|
|
|
$pirep = Pirep::factory()->create([
|
2018-08-27 00:40:04 +08:00
|
|
|
'user_id' => $user->id,
|
|
|
|
'airline_id' => $flight->airline_id,
|
2019-11-06 00:44:31 +08:00
|
|
|
'flight_id' => $flight->id,
|
2018-03-16 07:20:07 +08:00
|
|
|
'flight_number' => $flight->flight_number,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$pirep = $this->pirepSvc->create($pirep, []);
|
2021-03-27 20:33:21 +08:00
|
|
|
$this->pirepSvc->submit($pirep);
|
2018-03-16 07:20:07 +08:00
|
|
|
|
|
|
|
$user_bid = Bid::where([
|
2018-08-27 00:40:04 +08:00
|
|
|
'user_id' => $user->id,
|
2018-03-16 07:20:07 +08:00
|
|
|
'flight_id' => $flight->id,
|
|
|
|
])->first();
|
|
|
|
|
|
|
|
$this->assertNull($user_bid);
|
|
|
|
}
|
2022-03-14 23:45:18 +08:00
|
|
|
|
2022-03-19 04:43:34 +08:00
|
|
|
public function testPirepProgressPercent()
|
|
|
|
{
|
|
|
|
$this->updateSetting('units.distance', 'km');
|
|
|
|
|
|
|
|
/** @var User $user */
|
|
|
|
$user = User::factory()->create();
|
|
|
|
|
|
|
|
/** @var Pirep $pirep */
|
|
|
|
$pirep = Pirep::factory()->create([
|
|
|
|
'user_id' => $user->id,
|
|
|
|
'distance' => 100,
|
|
|
|
'planned_distance' => 200,
|
|
|
|
'flight_time' => 60,
|
|
|
|
'planned_flight_time' => 90,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$progress = $pirep->progress_percent;
|
|
|
|
$this->assertEquals(50, $progress);
|
|
|
|
|
|
|
|
$pirep->planned_distance = null;
|
|
|
|
$progress = $pirep->progress_percent;
|
|
|
|
$this->assertEquals(100, $progress);
|
|
|
|
|
|
|
|
$pirep->planned_distance = 0;
|
|
|
|
$progress = $pirep->progress_percent;
|
|
|
|
$this->assertEquals(100, $progress);
|
|
|
|
}
|
|
|
|
|
2022-03-14 23:45:18 +08:00
|
|
|
/**
|
|
|
|
* See that the notifications are properly formatted
|
|
|
|
*/
|
|
|
|
public function testNotificationFormatting()
|
|
|
|
{
|
|
|
|
$this->updateSetting('units.distance', 'km');
|
|
|
|
|
|
|
|
/** @var User $user */
|
|
|
|
$user = User::factory()->create();
|
|
|
|
|
|
|
|
/** @var Pirep $pirep */
|
|
|
|
$pirep = Pirep::factory()->create([
|
|
|
|
'user_id' => $user->id,
|
|
|
|
'distance' => 100,
|
|
|
|
'planned_distance' => 200,
|
|
|
|
'flight_time' => 60,
|
|
|
|
'planned_flight_time' => 90,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$discordNotif = new PirepPrefiled($pirep);
|
|
|
|
$fields = $discordNotif->createFields($pirep);
|
|
|
|
$this->assertEquals('1h 30m', $fields['Flight Time (Planned)']);
|
|
|
|
$this->assertEquals('370.4 km', $fields['Distance']);
|
|
|
|
|
|
|
|
$discordNotif = new PirepStatusChanged($pirep);
|
|
|
|
$fields = $discordNotif->createFields($pirep);
|
|
|
|
$this->assertEquals('1h 0m', $fields['Flight Time']);
|
|
|
|
$this->assertEquals('185.2/370.4 km', $fields['Distance']);
|
|
|
|
|
|
|
|
$discordNotif = new \App\Notifications\Messages\Broadcast\PirepFiled($pirep);
|
|
|
|
$fields = $discordNotif->createFields($pirep);
|
|
|
|
$this->assertEquals('1h 0m', $fields['Flight Time']);
|
|
|
|
$this->assertEquals('185.2 km', $fields['Distance']);
|
|
|
|
}
|
2017-06-09 12:00:30 +08:00
|
|
|
}
|