073e48c396
* 391 Notification refactorings (#441) * Refactor notifications to allow easier plugins * Notification refactoring * Formatting * Move news to NewsService; cleanup of events * More refactoring; added send email out for news item and the template * Formatting * Formatting * Fix missing newsRepo (#445) * Refactor and add importer to Installer module #443 (#444) * Refactor and add importer to Installer module #443 * Refactor for finances to use in import * Import groups into roles * Formatting * Formatting * Add interface in installer for import * Notes about importing * Check for installer folder * Formatting * Fix pirep->user mapping * Unused import * Formatting * Replace importer with AJAX powered; better error handling #443 (#447) * Replace importer with AJAX powered; better error handling #443 * Formatting * Fix command line importer * Remove bootstrap cache (#448) * Cleanup the bootstrap/cache directory when packaging * Fix removal of bootstrap cache * Formatting * Stricter checks on ACARS API data (#451) * Stricter checks on ACARS API data * More checks * Fix for flight_number check forcing to exist * Allow nullable on flight_id * Avoid proc_open use #455 (#456) * Use PhpExecutableFinder() closes #457 #458 (#460) * Use DateTimeZone instead of int for creating datetime closes #461 * Fix CSV imports giving Storage class not found #454 (#462) * Fix CSV imports giving Storage class not found #454 * Update yarn files for security alert * Add PHP 7.4 support (#464) * Add PHP 7.4 to build matrix * DB fix * YAML parser fix in test data * Show versions * Package updates * Track used ICAOs * 7.4 METAR parsing fix * METAR parser fix * Formatting * Add meters to response units * Call instance for unit conversion * Return value * Catch exception for unknown quantity * Comment fix * Formatting * METAR parsing fixes on PHP 7.4 * Package updates * More random airport ID * More random airport ID * Properly disable toolbar * Semver written out to version file * Use dev as default identifier * Fix BindingResolutionError when debug toolbar isn't present (#465) * Fix BindingResolutionError when debug toolbar isn't present * Formatting * Split the importer module out from the installer module (#468) * Split the importer module out from the installer module * Cleanup of unused imports * Move updater into separate module #453 * Remove unused imports/formatting * Disable the install and importer modules at the end of the setup * Unused imports; update IJ style * test explicit stage for php+mysql * add more to matrix * Add different MariaDB versions * undo * Cleanup Model doc * Pilots cannot use the dashboard or flights without admin rights (#481) * Use auth middleware instead of specific groups for logged in state * Auth check for admin access * Check user admin access for updates * Formatting * Allow nullable field and calculate distance if nulled for flight import #478 (#482) * Check for no roles being attached #480 (#483) * Return the flight fares if there are no subfleet fares #488 (#489) * Return the flight fares if there are no subfleet fares #488 * Formatting * Formatting * Account for units when entering fuel amounts #493 * Search for ICAO not working properly (#496) * /flights and /flights/search direct to the same endpoint * Properly set the distance/planned_distance on save (#497) * 491 Installation Error (#495) * Disable CSRF token * Add error handling around looking up the theme and set a default * Note about logs in issue template * Formatting * Fix GeoService errors when viewing PIREP #498 (#499) * Add new command to export a specific PIREP for debugging (#501) * Set a default model value for airports on PIREP (#500) * Set a default model value for airports on PIREP * Fix airport icao reference * Default airport models * Catch broader exception writing out config files #491 * style * Add reference to docs on doc site (#502) * Properly create/update rows importing #486 (#503) * Add base Dockerfile for Dockerhub builds (#504) * New subfleet not being attached to an airline on import #479 (#505) * Fix subfleet not being attached to an airline on creation in import #479 * Call airline name with optional() around subfleet * Minor cleanup * Search flights by subfleet #484 (#506) * API level search of flights #484 * Add Subfleet to flights page for search * Make the fuel used optional (#512) * Add make to Docker container * Add getRootDomain() to Utils (#514) * Show admin dropdown for admin-access ability (#515) * Show admin dropdown for admin-access ability closes #509 * Formatting * Check user permissions on the routes #508 (#516) * Check user permissions on the routes #508 * Formatting * Return default value on exception for setting() * Correct text for no subfleets #507 (#518) * Add a public_url() helper #513 (#519) * Reduce number of queries for update check (#520) * Try to clear caches before updating (#522) * Try to clear caches before updating * Add clear-compiled to maintenance cache list * Formatting * Set PIREPs page to public (#526) Set PIREPs page to public * Fix live and route map errors #527 (#528) * Add menu bar for mobile (#529) * Format all blade templates to 2 spaces #530 (#531) * Fix PIREP edit endpoint closes #533 (#534) * Fix import during flight cron #532 (#535) * PIREPS resource except for show (#536) * Use optional() around the airport fields (#537) * Use optional() around the airport fields * Add null-coalesce around full_name * Add link to download ACARS config from profile (#539) * Add link to download ACARS config from profile * Formatting * Update xml config file template (#540)
399 lines
13 KiB
PHP
399 lines
13 KiB
PHP
<?php
|
|
|
|
use App\Models\Acars;
|
|
use App\Models\Bid;
|
|
use App\Models\Enums\AcarsType;
|
|
use App\Models\Enums\PirepState;
|
|
use App\Models\Pirep;
|
|
use App\Models\User;
|
|
use App\Notifications\Messages\PirepAccepted;
|
|
use App\Repositories\SettingRepository;
|
|
use App\Services\BidService;
|
|
use App\Services\FlightService;
|
|
use App\Services\PirepService;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Facades\Notification;
|
|
|
|
class PIREPTest extends TestCase
|
|
{
|
|
protected $pirepSvc;
|
|
protected $settingsRepo;
|
|
|
|
public function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
$this->addData('base');
|
|
$this->addData('fleet');
|
|
|
|
$this->pirepSvc = app(PirepService::class);
|
|
$this->settingsRepo = app(SettingRepository::class);
|
|
}
|
|
|
|
protected function createNewRoute()
|
|
{
|
|
$route = [];
|
|
$navpoints = factory(App\Models\Navdata::class, 5)->create();
|
|
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]
|
|
)->orderBy('order', 'asc')->get();
|
|
|
|
foreach ($route_points as $point) {
|
|
$saved_route[] = $point->name;
|
|
}
|
|
|
|
return $saved_route;
|
|
}
|
|
|
|
/**
|
|
* @throws Exception
|
|
*/
|
|
public function testAddPirep()
|
|
{
|
|
$user = factory(App\Models\User::class)->create();
|
|
$route = $this->createNewRoute();
|
|
$pirep = factory(App\Models\Pirep::class)->create([
|
|
'user_id' => $user->id,
|
|
'route' => implode(' ', $route),
|
|
]);
|
|
|
|
$pirep = $this->pirepSvc->create($pirep, []);
|
|
|
|
try {
|
|
$this->pirepSvc->saveRoute($pirep);
|
|
} catch (Exception $e) {
|
|
throw $e;
|
|
}
|
|
|
|
/*
|
|
* Check the initial state info
|
|
*/
|
|
$this->assertEquals($pirep->state, PirepState::PENDING);
|
|
|
|
/**
|
|
* Now set the PIREP state to ACCEPTED
|
|
*/
|
|
$new_pirep_count = $pirep->pilot->flights + 1;
|
|
$new_flight_time = $pirep->pilot->flight_time + $pirep->flight_time;
|
|
|
|
$this->pirepSvc->changeState($pirep, PirepState::ACCEPTED);
|
|
$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);
|
|
|
|
// Check the location of the current aircraft
|
|
$this->assertEquals($pirep->aircraft->airport_id, $pirep->arr_airport_id);
|
|
|
|
// Also check via API:
|
|
$this->get('/api/fleet/aircraft/'.$pirep->aircraft_id, [], $user)
|
|
->assertJson(['data' => ['airport_id' => $pirep->arr_airport_id]]);
|
|
|
|
// Make sure a notification was sent out to both the user and the admin(s)
|
|
Notification::assertSentTo([$user], PirepAccepted::class);
|
|
|
|
// Try cancelling it
|
|
$uri = '/api/pireps/'.$pirep->id.'/cancel';
|
|
$response = $this->put($uri, [], [], $user);
|
|
$response->assertStatus(400);
|
|
|
|
/**
|
|
* Now go from ACCEPTED to REJECTED
|
|
*/
|
|
$new_pirep_count = $pirep->pilot->flights - 1;
|
|
$new_flight_time = $pirep->pilot->flight_time - $pirep->flight_time;
|
|
$this->pirepSvc->changeState($pirep, PirepState::REJECTED);
|
|
$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);
|
|
|
|
/**
|
|
* 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();
|
|
|
|
// this should delete the old route from the acars table
|
|
$this->pirepSvc->saveRoute($pirep);
|
|
|
|
$saved_route = $this->getAcarsRoute($pirep);
|
|
$this->assertEquals($route, $saved_route);
|
|
}
|
|
|
|
/**
|
|
* Make sure the unit conversions look to be proper
|
|
*/
|
|
public function testUnitFields()
|
|
{
|
|
$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
|
|
$this->assertHasKeys($body['fuel_used'], ['lbs', 'kg']);
|
|
$this->assertEquals($pirep->fuel_used, $body['fuel_used']['lbs']);
|
|
|
|
// Check that it has the distance units
|
|
$this->assertHasKeys($body['distance'], ['km', 'nmi', 'mi']);
|
|
$this->assertEquals($pirep->distance, $body['distance']['nmi']);
|
|
|
|
// Check the planned_distance field
|
|
$this->assertHasKeys($body['planned_distance'], ['km', 'nmi', 'mi']);
|
|
$this->assertEquals($pirep->planned_distance, $body['planned_distance']['nmi']);
|
|
}
|
|
|
|
public function testGetUserPireps()
|
|
{
|
|
$this->user = factory(App\Models\User::class)->create();
|
|
$pirep_done = factory(App\Models\Pirep::class)->create([
|
|
'user_id' => $this->user->id,
|
|
'state' => PirepState::ACCEPTED,
|
|
]);
|
|
|
|
$pirep_in_progress = factory(App\Models\Pirep::class)->create([
|
|
'user_id' => $this->user->id,
|
|
'state' => PirepState::IN_PROGRESS,
|
|
]);
|
|
|
|
$pirep_cancelled = factory(App\Models\Pirep::class)->create([
|
|
'user_id' => $this->user->id,
|
|
'state' => PirepState::CANCELLED,
|
|
]);
|
|
|
|
$pireps = $this->get('/api/user/pireps')
|
|
->assertStatus(200)
|
|
->json();
|
|
|
|
$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));
|
|
}
|
|
|
|
/**
|
|
* check the stats/ranks, etc have incremented properly
|
|
*/
|
|
public function testPilotStatsIncr()
|
|
{
|
|
$this->updateSetting('pilots.count_transfer_hours', false);
|
|
|
|
$user = factory(User::class)->create([
|
|
'flights' => 0,
|
|
'flight_time' => 0,
|
|
'rank_id' => 1,
|
|
]);
|
|
|
|
// Submit two PIREPs
|
|
$pireps = factory(Pirep::class, 2)->create([
|
|
'airline_id' => $user->airline_id,
|
|
'aircraft_id' => 1,
|
|
'user_id' => $user->id,
|
|
// 360min == 6 hours, rank should bump up
|
|
'flight_time' => 360,
|
|
]);
|
|
|
|
foreach ($pireps as $pirep) {
|
|
$this->pirepSvc->create($pirep);
|
|
$this->pirepSvc->accept($pirep);
|
|
}
|
|
|
|
$pilot = User::find($user->id);
|
|
$last_pirep = Pirep::where('id', $pilot->last_pirep_id)->first();
|
|
|
|
// Make sure rank went up
|
|
$this->assertGreaterThan($user->rank_id, $pilot->rank_id);
|
|
$this->assertEquals($last_pirep->arr_airport_id, $pilot->curr_airport_id);
|
|
|
|
$this->assertEquals(2, $pilot->flights);
|
|
|
|
//
|
|
// Submit another PIREP, adding another 6 hours
|
|
// it should automatically be accepted
|
|
//
|
|
$pirep = factory(Pirep::class)->create([
|
|
'airline_id' => 1,
|
|
'user_id' => $user->id,
|
|
// 120min == 2 hours, currently at 9 hours
|
|
// Rank bumps up at 10 hours
|
|
'flight_time' => 120,
|
|
]);
|
|
|
|
// Pilot should be at rank 2, where accept should be automatic
|
|
$this->pirepSvc->create($pirep);
|
|
$this->pirepSvc->submit($pirep);
|
|
|
|
$pilot->refresh();
|
|
|
|
$this->assertEquals(3, $pilot->flights);
|
|
|
|
$latest_pirep = Pirep::where('id', $pilot->last_pirep_id)->first();
|
|
|
|
// Make sure PIREP was auto updated
|
|
$this->assertEquals(PirepState::ACCEPTED, $latest_pirep->state);
|
|
|
|
// Make sure latest PIREP was updated
|
|
$this->assertNotEquals($last_pirep->id, $latest_pirep->id);
|
|
}
|
|
|
|
/**
|
|
* check the stats/ranks, etc have incremented properly
|
|
*/
|
|
public function testPilotStatsIncrWithTransferHours()
|
|
{
|
|
$this->updateSetting('pilots.count_transfer_hours', true);
|
|
|
|
$user = factory(User::class)->create([
|
|
'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
|
|
$pireps = factory(Pirep::class, 2)->create([
|
|
'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);
|
|
$last_pirep = Pirep::where('id', $pilot->last_pirep_id)->first();
|
|
|
|
// Make sure rank went up
|
|
$this->assertGreaterThan($user->rank_id, $pilot->rank_id);
|
|
}
|
|
|
|
/**
|
|
* Find and check for any duplicate PIREPs by a user
|
|
*/
|
|
public function testDuplicatePireps()
|
|
{
|
|
$user = factory(App\Models\User::class)->create();
|
|
$pirep = factory(Pirep::class)->create([
|
|
'user_id' => $user->id,
|
|
]);
|
|
|
|
// This should find itself...
|
|
$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;
|
|
$pirep = factory(Pirep::class)->create([
|
|
'created_at' => Carbon::now()->subMinutes($minutes)->toDateTimeString(),
|
|
]);
|
|
|
|
// This should find itself...
|
|
$dupe_pirep = $this->pirepSvc->findDuplicate($pirep);
|
|
$this->assertFalse($dupe_pirep);
|
|
}
|
|
|
|
public function testCancelViaAPI()
|
|
{
|
|
$pirep = $this->createPirep()->toArray();
|
|
|
|
$uri = '/api/pireps/prefile';
|
|
$response = $this->post($uri, $pirep);
|
|
$pirep_id = $response->json()['data']['id'];
|
|
|
|
$uri = '/api/pireps/'.$pirep_id.'/acars/position';
|
|
$acars = factory(App\Models\Acars::class)->make()->toArray();
|
|
$response = $this->post($uri, [
|
|
'positions' => [$acars],
|
|
]);
|
|
|
|
$response->assertStatus(200);
|
|
|
|
// Cancel it
|
|
$uri = '/api/pireps/'.$pirep_id.'/cancel';
|
|
$response = $this->delete($uri, $acars);
|
|
$response->assertStatus(200);
|
|
|
|
// Should get a 400 when posting an ACARS update
|
|
$uri = '/api/pireps/'.$pirep_id.'/acars/position';
|
|
$acars = factory(App\Models\Acars::class)->make()->toArray();
|
|
|
|
$response = $this->post($uri, $acars);
|
|
$response->assertStatus(400);
|
|
}
|
|
|
|
/**
|
|
* When a PIREP is accepted, ensure that the bid is removed
|
|
*/
|
|
public function testPirepBidRemoved()
|
|
{
|
|
$bidSvc = app(BidService::class);
|
|
$flightSvc = app(FlightService::class);
|
|
$this->settingsRepo->store('pireps.remove_bid_on_accept', true);
|
|
|
|
$user = factory(App\Models\User::class)->create([
|
|
'flight_time' => 0,
|
|
]);
|
|
|
|
$flight = factory(App\Models\Flight::class)->create([
|
|
'route_code' => null,
|
|
'route_leg' => null,
|
|
]);
|
|
|
|
$bidSvc->addBid($flight, $user);
|
|
|
|
$pirep = factory(App\Models\Pirep::class)->create([
|
|
'user_id' => $user->id,
|
|
'airline_id' => $flight->airline_id,
|
|
'flight_id' => $flight->id,
|
|
'flight_number' => $flight->flight_number,
|
|
]);
|
|
|
|
$pirep = $this->pirepSvc->create($pirep, []);
|
|
$this->pirepSvc->changeState($pirep, PirepState::ACCEPTED);
|
|
|
|
$user_bid = Bid::where([
|
|
'user_id' => $user->id,
|
|
'flight_id' => $flight->id,
|
|
])->first();
|
|
|
|
$this->assertNull($user_bid);
|
|
}
|
|
}
|