Files
phpvms/tests/FlightTest.php
Nabeel S 073e48c396 7.0.0-beta3 Release (#541)
* 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)
2020-02-08 13:29:34 -05:00

541 lines
18 KiB
PHP

<?php
use App\Cron\Nightly\SetActiveFlights;
use App\Events\CronNightly;
use App\Models\Enums\Days;
use App\Models\Enums\NavaidType;
use App\Models\Flight;
use App\Models\User;
use App\Repositories\SettingRepository;
use App\Services\AirportService;
use App\Services\FlightService;
class FlightTest extends TestCase
{
protected $flightSvc;
protected $settingsRepo;
public function setUp(): void
{
parent::setUp();
$this->addData('base');
$this->flightSvc = app(FlightService::class);
$this->settingsRepo = app(SettingRepository::class);
}
/**
* Add a single flight
*
* @param $user
*
* @return mixed
*/
public function addFlight($user)
{
$flight = factory(App\Models\Flight::class)->create([
'airline_id' => $user->airline_id,
]);
$flight->subfleets()->syncWithoutDetaching([
factory(App\Models\Subfleet::class)->create([
'airline_id' => $user->airline_id,
])->id,
]);
return $flight;
}
/**
* Add a given number of flights for a subfleet
*
* @param $subfleet
* @param $num_flights
*
* @return \App\Models\Flight[]
*/
public function addFlightsForSubfleet($subfleet, $num_flights)
{
return factory(App\Models\Flight::class, $num_flights)->create([
'airline_id' => $subfleet->airline->id,
])->each(function (Flight $f) use ($subfleet) {
$f->subfleets()->syncWithoutDetaching([
$subfleet->id,
]);
});
}
/**
* Test adding a flight and also if there are duplicates
*/
public function testDuplicateFlight()
{
$this->user = factory(App\Models\User::class)->create();
$flight = $this->addFlight($this->user);
// first flight shouldn't be a duplicate
$this->assertFalse($this->flightSvc->isFlightDuplicate($flight));
$flight_dupe = new Flight([
'airline_id' => $flight->airline_id,
'flight_number' => $flight->flight_number,
'route_code' => $flight->route_code,
'route_leg' => $flight->route_leg,
]);
$this->assertTrue($this->flightSvc->isFlightDuplicate($flight_dupe));
// same flight but diff airline shouldn't be a dupe
$new_airline = factory(App\Models\Airline::class)->create();
$flight_dupe = new Flight([
'airline_id' => $new_airline->airline_id,
'flight_number' => $flight->flight_number,
'route_code' => $flight->route_code,
'route_leg' => $flight->route_leg,
]);
$this->assertFalse($this->flightSvc->isFlightDuplicate($flight_dupe));
// add another flight with a code
$flight_leg = factory(App\Models\Flight::class)->create([
'airline_id' => $flight->airline_id,
'flight_number' => $flight->flight_number,
'route_code' => 'A',
]);
$this->assertFalse($this->flightSvc->isFlightDuplicate($flight_leg));
// Add both a route and leg
$flight_leg = factory(App\Models\Flight::class)->create([
'airline_id' => $flight->airline_id,
'flight_number' => $flight->flight_number,
'route_code' => 'A',
'route_leg' => 1,
]);
$this->assertFalse($this->flightSvc->isFlightDuplicate($flight_leg));
}
public function testGetFlight()
{
$this->user = factory(App\Models\User::class)->create();
$flight = $this->addFlight($this->user);
$req = $this->get('/api/flights/'.$flight->id);
$req->assertStatus(200);
$body = $req->json()['data'];
$this->assertEquals($flight->id, $body['id']);
$this->assertEquals($flight->dpt_airport_id, $body['dpt_airport_id']);
$this->assertEquals($flight->arr_airport_id, $body['arr_airport_id']);
// Distance conversion
$this->assertHasKeys($body['distance'], ['mi', 'nmi', 'km']);
$this->get('/api/flights/INVALID', self::$auth_headers)
->assertStatus(404);
}
/**
* Search based on all different criteria
*/
public function testSearchFlight()
{
$this->user = factory(App\Models\User::class)->create();
$flight = $this->addFlight($this->user);
// search specifically for a flight ID
$query = 'flight_id='.$flight->id;
$req = $this->get('/api/flights/search?'.$query);
$req->assertStatus(200);
}
/**
* Get the flight's route
*
* @throws Exception
*/
public function testFlightRoute()
{
$this->user = factory(App\Models\User::class)->create();
$flight = $this->addFlight($this->user);
$route_count = random_int(4, 6);
$route = factory(App\Models\Navdata::class, $route_count)->create();
$route_text = implode(' ', $route->pluck('id')->toArray());
$flight->route = $route_text;
$flight->save();
$res = $this->get('/api/flights/'.$flight->id.'/route');
$res->assertStatus(200);
$body = $res->json();
$this->assertCount($route_count, $body['data']);
$first_point = $body['data'][0];
$this->assertEquals($first_point['id'], $route[0]->id);
$this->assertEquals($first_point['name'], $route[0]->name);
$this->assertEquals($first_point['type']['type'], $route[0]->type);
$this->assertEquals($first_point['type']['name'], NavaidType::label($route[0]->type));
}
/**
* Find all of the flights
*/
public function testFindAllFlights()
{
$this->user = factory(App\Models\User::class)->create();
factory(App\Models\Flight::class, 20)->create([
'airline_id' => $this->user->airline_id,
]);
$res = $this->get('/api/flights?limit=10');
$body = $res->json();
$this->assertEquals(2, $body['meta']['last_page']);
$res = $this->get('/api/flights?page=2&limit=5');
$res->assertJsonCount(5, 'data');
}
/**
* Search for flights based on a subfleet. If subfleet is blank
*/
public function testSearchFlightBySubfleet()
{
$airline = factory(App\Models\Airline::class)->create();
$subfleetA = factory(App\Models\Subfleet::class)->create(['airline_id' => $airline->id]);
$subfleetB = factory(App\Models\Subfleet::class)->create(['airline_id' => $airline->id]);
$rank = $this->createRank(0, [$subfleetB->id]);
$this->user = factory(App\Models\User::class)->create([
'airline_id' => $airline->id,
'rank_id' => $rank->id,
]);
$this->addFlightsForSubfleet($subfleetA, 5);
$this->addFlightsForSubfleet($subfleetB, 10);
// search specifically for a given subfleet
//$query = 'subfleet_id='.$subfleetB->id;
$query = 'subfleet_id='.$subfleetB->id;
$res = $this->get('/api/flights/search?'.$query);
$res->assertStatus(200);
$res->assertJsonCount(10, 'data');
$body = $res->json('data');
collect($body)->each(function ($flight) use ($subfleetB) {
self::assertNotEmpty($flight['subfleets']);
self::assertEquals($subfleetB->id, $flight['subfleets'][0]['id']);
});
}
/**
* Test the bitmasks that they work for setting the day of week and
* then retrieving by searching on those
*/
public function testFindDaysOfWeek(): void
{
$this->user = factory(App\Models\User::class)->create();
factory(App\Models\Flight::class, 20)->create([
'airline_id' => $this->user->airline_id,
]);
$saved_flight = factory(App\Models\Flight::class)->create([
'airline_id' => $this->user->airline_id,
'days' => Days::getDaysMask([
Days::SUNDAY,
Days::THURSDAY,
]),
]);
$flight = Flight::findByDays([Days::SUNDAY])->first();
$this->assertTrue($flight->on_day(Days::SUNDAY));
$this->assertTrue($flight->on_day(Days::THURSDAY));
$this->assertFalse($flight->on_day(Days::MONDAY));
$this->assertEquals($saved_flight->id, $flight->id);
$flight = Flight::findByDays([Days::SUNDAY, Days::THURSDAY])->first();
$this->assertEquals($saved_flight->id, $flight->id);
$flight = Flight::findByDays([Days::WEDNESDAY, Days::THURSDAY])->first();
$this->assertNull($flight);
}
/**
* Make sure that flights are marked as inactive when they're out of the start/end
* zones. also make sure that flights with a specific day of the week are only
* active on those days
*/
public function testDayOfWeekActive(): void
{
$this->user = factory(App\Models\User::class)->create();
// Set it to Monday or Tuesday, depending on what today is
if (date('N') === '1') { // today is a monday
$days = Days::getDaysMask([Days::TUESDAY]);
} else {
$days = Days::getDaysMask([Days::MONDAY]);
}
factory(App\Models\Flight::class, 5)->create();
$flight = factory(App\Models\Flight::class)->create([
'days' => $days,
]);
// Run the event that will enable/disable flights
$event = new CronNightly();
(new SetActiveFlights())->handle($event);
$res = $this->get('/api/flights');
$body = $res->json('data');
$flights = collect($body)->where('id', $flight->id)->first();
$this->assertNull($flights);
}
public function testDayOfWeekTests(): void
{
$mask = 127;
$this->assertTrue(Days::in($mask, Days::$isoDayMap[1]));
$this->assertTrue(Days::in($mask, Days::$isoDayMap[2]));
$this->assertTrue(Days::in($mask, Days::$isoDayMap[3]));
$this->assertTrue(Days::in($mask, Days::$isoDayMap[4]));
$this->assertTrue(Days::in($mask, Days::$isoDayMap[5]));
$this->assertTrue(Days::in($mask, Days::$isoDayMap[6]));
$this->assertTrue(Days::in($mask, Days::$isoDayMap[7]));
$mask = 125;
$this->assertTrue(Days::in($mask, Days::$isoDayMap[1]));
$this->assertFalse(Days::in($mask, Days::$isoDayMap[2]));
$this->assertTrue(Days::in($mask, Days::$isoDayMap[3]));
$this->assertTrue(Days::in($mask, Days::$isoDayMap[4]));
$this->assertTrue(Days::in($mask, Days::$isoDayMap[5]));
$this->assertTrue(Days::in($mask, Days::$isoDayMap[6]));
$this->assertTrue(Days::in($mask, Days::$isoDayMap[7]));
}
public function testStartEndDate(): void
{
$this->user = factory(App\Models\User::class)->create();
factory(App\Models\Flight::class, 5)->create();
$flight = factory(App\Models\Flight::class)->create([
'start_date' => Carbon\Carbon::now('UTC')->subDays(1),
'end_date' => Carbon\Carbon::now('UTC')->addDays(1),
]);
$flight_not_active = factory(App\Models\Flight::class)->create([
'start_date' => Carbon\Carbon::now('UTC')->subDays(10),
'end_date' => Carbon\Carbon::now('UTC')->subDays(2),
]);
// Run the event that will enable/disable flights
$event = new CronNightly();
(new SetActiveFlights())->handle($event);
$res = $this->get('/api/flights');
$body = $res->json('data');
$flights = collect($body)->where('id', $flight->id)->first();
$this->assertNotNull($flights);
$flights = collect($body)->where('id', $flight_not_active->id)->first();
$this->assertNull($flights);
}
public function testStartEndDateDayOfWeek(): void
{
$this->user = factory(App\Models\User::class)->create();
// Set it to Monday or Tuesday, depending on what today is
if (date('N') === '1') { // today is a monday
$days = Days::getDaysMask([Days::TUESDAY]);
} else {
$days = Days::getDaysMask([Days::MONDAY]);
}
factory(App\Models\Flight::class, 5)->create();
$flight = factory(App\Models\Flight::class)->create([
'start_date' => Carbon\Carbon::now('UTC')->subDays(1),
'end_date' => Carbon\Carbon::now('UTC')->addDays(1),
'days' => Days::$isoDayMap[date('N')],
]);
// Not active because of days of week not today
$flight_not_active = factory(App\Models\Flight::class)->create([
'start_date' => Carbon\Carbon::now('UTC')->subDays(1),
'end_date' => Carbon\Carbon::now('UTC')->addDays(1),
'days' => $days,
]);
// Run the event that will enable/disable flights
$event = new CronNightly();
(new SetActiveFlights())->handle($event);
$res = $this->get('/api/flights');
$body = $res->json('data');
$flights = collect($body)->where('id', $flight->id)->first();
$this->assertNotNull($flights);
$flights = collect($body)->where('id', $flight_not_active->id)->first();
$this->assertNull($flights);
}
public function testFlightSearchApi()
{
$this->user = factory(App\Models\User::class)->create();
$flights = factory(App\Models\Flight::class, 10)->create([
'airline_id' => $this->user->airline_id,
]);
$flight = $flights->random();
$query = 'flight_number='.$flight->flight_number;
$req = $this->get('/api/flights/search?'.$query);
$body = $req->json();
$this->assertEquals($flight->id, $body['data'][0]['id']);
}
public function testFlightSearchApiDepartureAirport()
{
$this->user = factory(App\Models\User::class)->create();
factory(App\Models\Flight::class, 10)->create([
'airline_id' => $this->user->airline_id,
]);
$flight = factory(App\Models\Flight::class)->create([
'airline_id' => $this->user->airline_id,
'dpt_airport_id' => 'KAUS',
]);
$query = 'dpt_airport_id=kaus';
$req = $this->get('/api/flights/search?'.$query);
$body = $req->json();
$this->assertCount(1, $body['data']);
$this->assertEquals($flight->id, $body['data'][0]['id']);
}
public function testFlightSearchApiDistance()
{
$total_flights = 10;
$this->user = factory(App\Models\User::class)->create();
$flights = factory(App\Models\Flight::class, $total_flights)->create([
'airline_id' => $this->user->airline_id,
]);
// Max distance generated in factory is 1000, so set a random flight
// and try to find it again through the search
$flight = $flights->random();
$flight->distance = 1500;
$flight->save();
$distance_gt = 1100;
$distance_lt = 1600;
// look for all of the flights now less than the "factory default" of 1000
$query = 'dlt=1000&ignore_restrictions=1';
$req = $this->get('/api/flights/search?'.$query);
$body = $req->json();
$this->assertCount($total_flights - 1, $body['data']);
// Try using greater than
$query = 'dgt='.$distance_gt.'&ignore_restrictions=1';
$req = $this->get('/api/flights/search?'.$query);
$body = $req->json();
$this->assertCount(1, $body['data']);
$this->assertEquals($flight->id, $body['data'][0]['id']);
$query = 'dgt='.$distance_gt.'&dlt='.$distance_lt.'&ignore_restrictions=1';
$req = $this->get('/api/flights/search?'.$query);
$body = $req->json();
$this->assertCount(1, $body['data']);
$this->assertEquals($flight->id, $body['data'][0]['id']);
}
public function testAddSubfleet()
{
$subfleet = factory(App\Models\Subfleet::class)->create();
$flight = factory(App\Models\Flight::class)->create();
$fleetSvc = app(App\Services\FleetService::class);
$fleetSvc->addSubfleetToFlight($subfleet, $flight);
$flight->refresh();
$found = $flight->subfleets()->get();
$this->assertCount(1, $found);
// Make sure it hasn't been added twice
$fleetSvc->addSubfleetToFlight($subfleet, $flight);
$flight->refresh();
$found = $flight->subfleets()->get();
$this->assertCount(1, $found);
}
/**
* Delete a flight and make sure all the bids are gone
*/
public function testDeleteFlight()
{
$user = factory(User::class)->create();
$flight = $this->addFlight($user);
$this->flightSvc->deleteFlight($flight);
$empty_flight = Flight::find($flight->id);
$this->assertNull($empty_flight);
}
public function testAirportDistance()
{
// KJFK
$fromIcao = factory(App\Models\Airport::class)->create([
'lat' => 40.6399257,
'lon' => -73.7786950,
]);
// KSFO
$toIcao = factory(App\Models\Airport::class)->create([
'lat' => 37.6188056,
'lon' => -122.3754167,
]);
$airportSvc = app(AirportService::class);
$distance = $airportSvc->calculateDistance($fromIcao->id, $toIcao->id);
$this->assertNotNull($distance);
$this->assertEquals(2244.33, $distance['nmi']);
}
public function testAirportDistanceApi()
{
$user = factory(User::class)->create();
$headers = $this->headers($user);
// KJFK
$fromIcao = factory(App\Models\Airport::class)->create([
'lat' => 40.6399257,
'lon' => -73.7786950,
]);
// KSFO
$toIcao = factory(App\Models\Airport::class)->create([
'lat' => 37.6188056,
'lon' => -122.3754167,
]);
$req = $this->get('/api/airports/'.$fromIcao->id.'/distance/'.$toIcao->id, $headers);
$req->assertStatus(200);
$body = $req->json()['data'];
$this->assertNotNull($body['distance']);
$this->assertEquals(2244.33, $body['distance']['nmi']);
}
}