2020-03-23 21:31:35 +08:00
|
|
|
<?php
|
|
|
|
|
2020-05-23 23:43:29 +08:00
|
|
|
namespace Tests;
|
|
|
|
|
2020-03-23 21:31:35 +08:00
|
|
|
use App\Models\Acars;
|
2021-03-03 01:29:04 +08:00
|
|
|
use App\Models\Aircraft;
|
2020-03-23 21:31:35 +08:00
|
|
|
use App\Models\Enums\AcarsType;
|
2021-03-03 01:29:04 +08:00
|
|
|
use App\Models\Enums\FareType;
|
|
|
|
use App\Models\Enums\UserState;
|
2020-05-23 23:43:29 +08:00
|
|
|
use App\Models\Flight;
|
|
|
|
use App\Models\Pirep;
|
2020-03-23 21:31:35 +08:00
|
|
|
use App\Models\SimBrief;
|
2020-05-23 23:43:29 +08:00
|
|
|
use App\Models\User;
|
2020-03-23 21:31:35 +08:00
|
|
|
use App\Services\SimBriefService;
|
|
|
|
use App\Support\Utils;
|
|
|
|
use Carbon\Carbon;
|
|
|
|
|
|
|
|
class SimBriefTest extends TestCase
|
|
|
|
{
|
2020-04-15 22:11:06 +08:00
|
|
|
private static $simbrief_flight_id = 'simbriefflightid';
|
|
|
|
|
2021-03-03 01:29:04 +08:00
|
|
|
/**
|
|
|
|
* @param array $attrs Additional user attributes
|
|
|
|
*
|
|
|
|
* @throws \Exception
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function createUserData(array $attrs = []): array
|
|
|
|
{
|
|
|
|
$subfleet = $this->createSubfleetWithAircraft(1);
|
|
|
|
$rank = $this->createRank(2, [$subfleet['subfleet']->id]);
|
|
|
|
|
|
|
|
/** @var User $user */
|
|
|
|
$user = factory(User::class)->create(array_merge([
|
|
|
|
'flight_time' => 1000,
|
|
|
|
'rank_id' => $rank->id,
|
|
|
|
'state' => UserState::ACTIVE,
|
|
|
|
], $attrs));
|
|
|
|
|
|
|
|
return [
|
|
|
|
'subfleet' => $subfleet['subfleet'],
|
|
|
|
'aircraft' => $subfleet['aircraft'],
|
|
|
|
'user' => $user,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-03-23 21:31:35 +08:00
|
|
|
/**
|
|
|
|
* Load SimBrief
|
|
|
|
*
|
2021-03-03 01:29:04 +08:00
|
|
|
* @param \App\Models\User $user
|
|
|
|
* @param \App\Models\Aircraft|null $aircraft
|
|
|
|
* @param array $fares
|
2021-03-07 02:49:42 +08:00
|
|
|
* @param string|null $flight_id
|
2020-03-23 21:31:35 +08:00
|
|
|
*
|
|
|
|
* @return \App\Models\SimBrief
|
|
|
|
*/
|
2021-03-07 02:49:42 +08:00
|
|
|
protected function loadSimBrief(User $user, Aircraft $aircraft, $fares = [], $flight_id = null): SimBrief
|
2020-03-23 21:31:35 +08:00
|
|
|
{
|
2021-03-07 02:49:42 +08:00
|
|
|
if (empty($flight_id)) {
|
|
|
|
$flight_id = self::$simbrief_flight_id;
|
|
|
|
}
|
|
|
|
|
2021-01-25 19:54:17 +08:00
|
|
|
/** @var \App\Models\Flight $flight */
|
2020-05-23 23:43:29 +08:00
|
|
|
$flight = factory(Flight::class)->create([
|
2021-03-07 02:49:42 +08:00
|
|
|
'id' => $flight_id,
|
2020-03-23 21:31:35 +08:00
|
|
|
'dpt_airport_id' => 'OMAA',
|
|
|
|
'arr_airport_id' => 'OMDB',
|
|
|
|
]);
|
|
|
|
|
2021-03-07 02:49:42 +08:00
|
|
|
return $this->downloadOfp($user, $flight, $aircraft, $fares);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Download an OFP file
|
|
|
|
*
|
|
|
|
* @param $user
|
|
|
|
* @param $flight
|
|
|
|
* @param $aircraft
|
|
|
|
* @param $fares
|
|
|
|
*
|
|
|
|
* @return \App\Models\SimBrief|null
|
|
|
|
*/
|
|
|
|
protected function downloadOfp($user, $flight, $aircraft, $fares)
|
|
|
|
{
|
2020-03-27 23:49:19 +08:00
|
|
|
$this->mockXmlResponse([
|
|
|
|
'simbrief/briefing.xml',
|
|
|
|
'simbrief/acars_briefing.xml',
|
|
|
|
]);
|
2020-03-23 21:31:35 +08:00
|
|
|
|
|
|
|
/** @var SimBriefService $sb */
|
|
|
|
$sb = app(SimBriefService::class);
|
|
|
|
|
2021-03-03 01:29:04 +08:00
|
|
|
return $sb->downloadOfp($user->id, Utils::generateNewId(), $flight->id, $aircraft->id, $fares);
|
2020-03-23 21:31:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read from the SimBrief URL
|
|
|
|
*/
|
|
|
|
public function testReadSimbrief()
|
|
|
|
{
|
2021-03-03 01:29:04 +08:00
|
|
|
$userinfo = $this->createUserData();
|
|
|
|
$this->user = $userinfo['user'];
|
|
|
|
$briefing = $this->loadSimBrief($this->user, $userinfo['aircraft']->first(), []);
|
2020-03-23 21:31:35 +08:00
|
|
|
|
|
|
|
$this->assertNotEmpty($briefing->ofp_xml);
|
|
|
|
$this->assertNotNull($briefing->xml);
|
|
|
|
|
|
|
|
// Spot check reading of the files
|
|
|
|
$files = $briefing->files;
|
2020-03-27 23:49:19 +08:00
|
|
|
$this->assertEquals(47, $files->count());
|
2020-03-23 21:31:35 +08:00
|
|
|
$this->assertEquals(
|
|
|
|
'http://www.simbrief.com/ofp/flightplans/OMAAOMDB_PDF_1584226092.pdf',
|
|
|
|
$files->firstWhere('name', 'PDF Document')['url']
|
|
|
|
);
|
|
|
|
|
|
|
|
// Spot check reading of images
|
|
|
|
$images = $briefing->images;
|
|
|
|
$this->assertEquals(5, $images->count());
|
|
|
|
$this->assertEquals(
|
|
|
|
'http://www.simbrief.com/ofp/uads/OMAAOMDB_1584226092_ROUTE.gif',
|
|
|
|
$images->firstWhere('name', 'Route')['url']
|
|
|
|
);
|
|
|
|
|
|
|
|
$level = $briefing->xml->getFlightLevel();
|
|
|
|
$this->assertEquals('380', $level);
|
|
|
|
|
|
|
|
// Read the flight route
|
|
|
|
$routeStr = $briefing->xml->getRouteString();
|
|
|
|
$this->assertEquals(
|
|
|
|
'DCT BOMUP DCT LOVIM DCT RESIG DCT NODVI DCT OBMUK DCT LORID DCT '.
|
|
|
|
'ORGUR DCT PEBUS DCT EMOPO DCT LOTUK DCT LAGTA DCT LOVOL DCT',
|
|
|
|
$routeStr
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check that the API calls are working (simbrief in the response, can retrieve the briefing)
|
|
|
|
*/
|
|
|
|
public function testApiCalls()
|
|
|
|
{
|
2021-03-03 01:29:04 +08:00
|
|
|
$userinfo = $this->createUserData();
|
|
|
|
$this->user = $userinfo['user'];
|
|
|
|
$briefing = $this->loadSimBrief($this->user, $userinfo['aircraft']->first(), [
|
|
|
|
[
|
|
|
|
'id' => 100,
|
|
|
|
'code' => 'F',
|
|
|
|
'name' => 'Test Fare',
|
|
|
|
'type' => 'P',
|
|
|
|
'capacity' => 100,
|
|
|
|
'count' => 99,
|
|
|
|
],
|
|
|
|
]);
|
2020-03-23 21:31:35 +08:00
|
|
|
|
|
|
|
// Check the flight API response
|
|
|
|
$response = $this->get('/api/flights/'.$briefing->flight_id);
|
|
|
|
$response->assertOk();
|
|
|
|
$flight = $response->json('data');
|
|
|
|
|
|
|
|
$this->assertNotNull($flight['simbrief']);
|
|
|
|
$this->assertEquals($briefing->id, $flight['simbrief']['id']);
|
2020-03-25 22:58:03 +08:00
|
|
|
|
|
|
|
$url = str_replace('http://', 'https://', $flight['simbrief']['url']);
|
2021-03-05 06:08:51 +08:00
|
|
|
/*$this->assertEquals(
|
2021-01-25 19:54:17 +08:00
|
|
|
'https://localhost/api/flights/'.$briefing->id.'/briefing',
|
2020-03-25 22:58:03 +08:00
|
|
|
$url
|
2021-03-05 06:08:51 +08:00
|
|
|
);*/
|
|
|
|
$this->assertTrue(str_ends_with($url, $briefing->id.'/briefing'));
|
2020-03-23 21:31:35 +08:00
|
|
|
|
|
|
|
// Retrieve the briefing via API, and then check the doctype
|
2021-01-25 19:54:17 +08:00
|
|
|
$response = $this->get('/api/flights/'.$briefing->id.'/briefing', [], $this->user);
|
2020-03-23 21:31:35 +08:00
|
|
|
$response->assertOk();
|
|
|
|
|
|
|
|
$xml = simplexml_load_string($response->content());
|
|
|
|
$this->assertNotNull($xml);
|
|
|
|
|
|
|
|
$this->assertEquals('VMSAcars', $xml->getName());
|
|
|
|
$this->assertEquals('FlightPlan', $xml->attributes()->Type);
|
|
|
|
}
|
|
|
|
|
2020-04-15 22:11:06 +08:00
|
|
|
/**
|
|
|
|
* Make sure the user's bids have the Simbrief data show up
|
|
|
|
*/
|
|
|
|
public function testUserBidSimbrief()
|
|
|
|
{
|
2021-03-03 01:29:04 +08:00
|
|
|
$fares = [
|
|
|
|
[
|
|
|
|
'id' => 100,
|
|
|
|
'code' => 'F',
|
|
|
|
'name' => 'Test Fare',
|
|
|
|
'type' => FareType::PASSENGER,
|
|
|
|
'capacity' => 100,
|
|
|
|
'count' => 99,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
$userinfo = $this->createUserData();
|
|
|
|
$this->user = $userinfo['user'];
|
|
|
|
$this->loadSimBrief($this->user, $userinfo['aircraft']->first(), $fares);
|
2020-04-15 22:11:06 +08:00
|
|
|
|
|
|
|
// Find the flight
|
|
|
|
$uri = '/api/user/bids';
|
|
|
|
$data = ['flight_id' => self::$simbrief_flight_id];
|
|
|
|
|
|
|
|
$body = $this->put($uri, $data);
|
|
|
|
$body = $body->json('data');
|
|
|
|
|
|
|
|
// Make sure Simbrief is there
|
|
|
|
$this->assertNotNull($body['flight']['simbrief']['id']);
|
2021-03-03 01:29:04 +08:00
|
|
|
$this->assertNotNull($body['flight']['simbrief']['subfleet']['fares']);
|
|
|
|
|
|
|
|
$subfleet = $body['flight']['simbrief']['subfleet'];
|
|
|
|
$this->assertEquals($fares[0]['id'], $subfleet['fares'][0]['id']);
|
|
|
|
$this->assertEquals($fares[0]['count'], $subfleet['fares'][0]['count']);
|
2020-04-15 22:11:06 +08:00
|
|
|
}
|
|
|
|
|
2021-03-07 02:49:42 +08:00
|
|
|
/**
|
|
|
|
* Make sure that the bids/simbrief created for the same flight by two different
|
|
|
|
* users doesn't leak across users
|
|
|
|
*
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function testUserBidSimbriefDoesntLeak()
|
|
|
|
{
|
|
|
|
$this->updateSetting('bids.disable_flight_on_bid', false);
|
|
|
|
$fares = [
|
|
|
|
[
|
|
|
|
'id' => 100,
|
|
|
|
'code' => 'F',
|
|
|
|
'name' => 'Test Fare',
|
|
|
|
'type' => FareType::PASSENGER,
|
|
|
|
'capacity' => 100,
|
|
|
|
'count' => 99,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
/** @var \App\Models\Flight $flight */
|
|
|
|
$flight = factory(Flight::class)->create();
|
|
|
|
|
|
|
|
// Create two briefings and make sure it doesn't leak
|
|
|
|
$userinfo2 = $this->createUserData();
|
|
|
|
$user2 = $userinfo2['user'];
|
|
|
|
$this->downloadOfp($user2, $flight, $userinfo2['aircraft']->first(), $fares);
|
|
|
|
|
|
|
|
$userinfo = $this->createUserData();
|
|
|
|
$user = $userinfo['user'];
|
|
|
|
$briefing = $this->downloadOfp($user, $flight, $userinfo['aircraft']->first(), $fares);
|
|
|
|
|
|
|
|
// Add the flight to the user's bids
|
|
|
|
$uri = '/api/user/bids';
|
|
|
|
$data = ['flight_id' => $flight->id];
|
|
|
|
|
|
|
|
// add for both users
|
|
|
|
$body = $this->put($uri, $data, [], $user2)->json('data');
|
|
|
|
$this->assertNotEmpty($body);
|
|
|
|
|
|
|
|
$body = $this->put($uri, $data, [], $user)->json('data');
|
|
|
|
$this->assertNotEmpty($body);
|
|
|
|
|
|
|
|
$body = $this->get('/api/user/bids', [], $user);
|
|
|
|
$body = $body->json('data')[0];
|
|
|
|
|
|
|
|
// Make sure Simbrief is there
|
|
|
|
$this->assertNotNull($body['flight']['simbrief']['id']);
|
|
|
|
$this->assertNotNull($body['flight']['simbrief']['subfleet']['fares']);
|
|
|
|
$this->assertEquals($body['flight']['simbrief']['id'], $briefing->id);
|
|
|
|
|
|
|
|
$subfleet = $body['flight']['simbrief']['subfleet'];
|
|
|
|
$this->assertEquals($fares[0]['id'], $subfleet['fares'][0]['id']);
|
|
|
|
$this->assertEquals($fares[0]['count'], $subfleet['fares'][0]['count']);
|
|
|
|
}
|
|
|
|
|
2020-03-23 21:31:35 +08:00
|
|
|
public function testAttachToPirep()
|
|
|
|
{
|
2021-03-03 01:29:04 +08:00
|
|
|
$userinfo = $this->createUserData();
|
|
|
|
$this->user = $userinfo['user'];
|
|
|
|
|
|
|
|
/** @var Pirep $pirep */
|
2020-05-23 23:43:29 +08:00
|
|
|
$pirep = factory(Pirep::class)->create([
|
2021-03-03 01:29:04 +08:00
|
|
|
'user_id' => $this->user->id,
|
2020-03-23 21:31:35 +08:00
|
|
|
'dpt_airport_id' => 'OMAA',
|
|
|
|
'arr_airport_id' => 'OMDB',
|
|
|
|
]);
|
|
|
|
|
2021-03-03 01:29:04 +08:00
|
|
|
$briefing = $this->loadSimBrief($this->user, $userinfo['aircraft']->first(), [
|
|
|
|
[
|
|
|
|
'id' => 100,
|
|
|
|
'code' => 'F',
|
|
|
|
'name' => 'Test Fare',
|
|
|
|
'type' => 'P',
|
|
|
|
'capacity' => 100,
|
|
|
|
'count' => 99,
|
|
|
|
],
|
|
|
|
]);
|
2020-03-23 21:31:35 +08:00
|
|
|
|
|
|
|
/** @var SimBriefService $sb */
|
|
|
|
$sb = app(SimBriefService::class);
|
|
|
|
$sb->attachSimbriefToPirep($pirep, $briefing);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Checks - ACARS entries for the route are loaded
|
|
|
|
*/
|
|
|
|
$acars = Acars::where(['pirep_id' => $pirep->id, 'type' => AcarsType::ROUTE])->get();
|
|
|
|
$this->assertEquals(12, $acars->count());
|
|
|
|
|
|
|
|
$fix = $acars->firstWhere('name', 'BOMUP');
|
|
|
|
$this->assertEquals($fix['name'], 'BOMUP');
|
|
|
|
$this->assertEquals($fix['lat'], 24.484639);
|
|
|
|
$this->assertEquals($fix['lon'], 54.578444);
|
|
|
|
$this->assertEquals($fix['order'], 1);
|
|
|
|
|
|
|
|
$briefing->refresh();
|
|
|
|
|
|
|
|
$this->assertEmpty($briefing->flight_id);
|
|
|
|
$this->assertEquals($pirep->id, $briefing->pirep_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test clearing expired briefs
|
|
|
|
*/
|
|
|
|
public function testClearExpiredBriefs()
|
|
|
|
{
|
2021-03-03 01:29:04 +08:00
|
|
|
$userinfo = $this->createUserData();
|
|
|
|
$user = $userinfo['user'];
|
|
|
|
|
2020-03-23 21:31:35 +08:00
|
|
|
$sb_ignored = factory(SimBrief::class)->create([
|
|
|
|
'user_id' => $user->id,
|
|
|
|
'flight_id' => 'a_flight_id',
|
|
|
|
'pirep_id' => 'a_pirep_id',
|
|
|
|
'created_at' => Carbon::now('UTC')->subDays(6)->toDateTimeString(),
|
|
|
|
]);
|
|
|
|
|
|
|
|
factory(SimBrief::class)->create([
|
|
|
|
'user_id' => $user->id,
|
|
|
|
'flight_id' => 'a_flight_Id',
|
|
|
|
'pirep_id' => '',
|
|
|
|
'created_at' => Carbon::now('UTC')->subDays(6)->toDateTimeString(),
|
|
|
|
]);
|
|
|
|
|
|
|
|
/** @var SimBriefService $sb */
|
|
|
|
$sb = app(SimBriefService::class);
|
|
|
|
$sb->removeExpiredEntries();
|
|
|
|
|
|
|
|
$all_briefs = SimBrief::all();
|
|
|
|
$this->assertEquals(1, $all_briefs->count());
|
|
|
|
$this->assertEquals($sb_ignored->id, $all_briefs[0]->id);
|
|
|
|
}
|
|
|
|
}
|