2018-01-20 01:46:30 +08:00
|
|
|
<?php
|
|
|
|
|
2020-05-23 23:43:29 +08:00
|
|
|
namespace Tests;
|
|
|
|
|
2020-05-16 23:46:25 +08:00
|
|
|
use App\Exceptions\PilotIdNotFound;
|
2019-09-17 01:08:26 +08:00
|
|
|
use App\Exceptions\UserPilotIdExists;
|
2020-07-15 10:02:35 +08:00
|
|
|
use App\Models\Airline;
|
2021-02-18 07:54:18 +08:00
|
|
|
use App\Models\Enums\UserState;
|
2020-09-30 01:21:24 +08:00
|
|
|
use App\Models\Fare;
|
2021-02-18 07:54:18 +08:00
|
|
|
use App\Models\Pirep;
|
2019-07-17 01:54:14 +08:00
|
|
|
use App\Models\User;
|
2018-01-20 01:46:30 +08:00
|
|
|
use App\Repositories\SettingRepository;
|
2020-09-30 01:21:24 +08:00
|
|
|
use App\Services\FareService;
|
2018-02-21 12:33:09 +08:00
|
|
|
use App\Services\UserService;
|
2021-02-18 07:54:18 +08:00
|
|
|
use Carbon\Carbon;
|
2020-02-24 06:21:26 +08:00
|
|
|
use Illuminate\Support\Facades\Hash;
|
2018-01-20 01:46:30 +08:00
|
|
|
|
|
|
|
class UserTest extends TestCase
|
|
|
|
{
|
2020-05-16 23:46:25 +08:00
|
|
|
/** @var SettingRepository */
|
2018-08-27 00:40:04 +08:00
|
|
|
protected $settingsRepo;
|
2020-05-16 23:46:25 +08:00
|
|
|
|
|
|
|
/** @var UserService */
|
2018-08-27 00:40:04 +08:00
|
|
|
protected $userSvc;
|
2018-01-20 01:46:30 +08:00
|
|
|
|
2019-05-12 23:50:38 +08:00
|
|
|
public function setUp(): void
|
2018-01-20 01:46:30 +08:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
$this->userSvc = app(UserService::class);
|
|
|
|
$this->settingsRepo = app(SettingRepository::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes sure that the subfleet/aircraft returned are allowable
|
|
|
|
* by the users rank.
|
|
|
|
*/
|
|
|
|
public function testRankSubfleets()
|
|
|
|
{
|
2018-08-27 00:40:04 +08:00
|
|
|
// Add subfleets and aircraft, but also add another
|
|
|
|
// set of subfleets
|
2018-02-21 02:59:49 +08:00
|
|
|
$subfleetA = $this->createSubfleetWithAircraft();
|
|
|
|
$this->createSubfleetWithAircraft();
|
2018-01-20 01:46:30 +08:00
|
|
|
|
2018-02-21 02:59:49 +08:00
|
|
|
$rank = $this->createRank(10, [$subfleetA['subfleet']->id]);
|
2018-01-20 01:46:30 +08:00
|
|
|
|
2020-07-15 10:02:35 +08:00
|
|
|
$user = factory(User::class)->create([
|
2018-01-20 01:46:30 +08:00
|
|
|
'rank_id' => $rank->id,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$added_aircraft = $subfleetA['aircraft']->pluck('id');
|
|
|
|
|
|
|
|
$subfleets = $this->userSvc->getAllowableSubfleets($user);
|
|
|
|
$this->assertEquals(1, $subfleets->count());
|
|
|
|
|
|
|
|
$subfleet = $subfleets[0];
|
|
|
|
$all_aircraft = $subfleet->aircraft->pluck('id');
|
|
|
|
$this->assertEquals($added_aircraft, $all_aircraft);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check via API
|
|
|
|
*/
|
|
|
|
$resp = $this->get('/api/user/fleet', [], $user)->assertStatus(200);
|
2018-02-11 08:17:38 +08:00
|
|
|
$body = $resp->json()['data'];
|
2018-01-20 01:46:30 +08:00
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// Get the subfleet that's been added in
|
2018-01-20 01:46:30 +08:00
|
|
|
$subfleet_from_api = $body[0];
|
|
|
|
$this->assertEquals($subfleet->id, $subfleet_from_api['id']);
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// Get all the aircraft from that subfleet
|
2018-01-20 01:46:30 +08:00
|
|
|
$aircraft_from_api = collect($subfleet_from_api['aircraft'])->pluck('id');
|
|
|
|
$this->assertEquals($added_aircraft, $aircraft_from_api);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check the user ID call
|
|
|
|
*/
|
2018-08-27 00:40:04 +08:00
|
|
|
$resp = $this->get('/api/users/'.$user->id.'/fleet', [], $user)->assertStatus(200);
|
2018-02-11 08:17:38 +08:00
|
|
|
$body = $resp->json()['data'];
|
2018-01-20 01:46:30 +08:00
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// Get the subfleet that's been added in
|
2018-01-20 01:46:30 +08:00
|
|
|
$subfleet_from_api = $body[0];
|
|
|
|
$this->assertEquals($subfleet->id, $subfleet_from_api['id']);
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// Get all the aircraft from that subfleet
|
2018-01-20 01:46:30 +08:00
|
|
|
$aircraft_from_api = collect($subfleet_from_api['aircraft'])->pluck('id');
|
|
|
|
$this->assertEquals($added_aircraft, $aircraft_from_api);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Flip the setting for getting all of the user's aircraft restricted
|
|
|
|
* by rank. Make sure that they're all returned
|
2020-10-01 00:58:45 +08:00
|
|
|
*
|
|
|
|
* @throws \Exception
|
2018-01-20 01:46:30 +08:00
|
|
|
*/
|
|
|
|
public function testGetAllAircraft()
|
|
|
|
{
|
2020-09-30 01:21:24 +08:00
|
|
|
$fare_svc = app(FareService::class);
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// Add subfleets and aircraft, but also add another
|
|
|
|
// set of subfleets
|
2018-02-21 02:59:49 +08:00
|
|
|
$subfleetA = $this->createSubfleetWithAircraft();
|
|
|
|
$subfleetB = $this->createSubfleetWithAircraft();
|
2018-01-20 01:46:30 +08:00
|
|
|
|
2020-09-30 01:21:24 +08:00
|
|
|
$fare = factory(Fare::class)->create([
|
|
|
|
'price' => 20,
|
|
|
|
'capacity' => 200,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$overrides = [
|
|
|
|
'price' => 50,
|
|
|
|
'capacity' => 400,
|
|
|
|
];
|
|
|
|
|
|
|
|
$fare_svc->setForSubfleet($subfleetA['subfleet'], $fare, $overrides);
|
|
|
|
|
2018-01-20 01:46:30 +08:00
|
|
|
$added_aircraft = array_merge(
|
|
|
|
$subfleetA['aircraft']->pluck('id')->toArray(),
|
|
|
|
$subfleetB['aircraft']->pluck('id')->toArray()
|
|
|
|
);
|
|
|
|
|
2018-02-21 02:59:49 +08:00
|
|
|
$rank = $this->createRank(10, [$subfleetA['subfleet']->id]);
|
2018-01-20 01:46:30 +08:00
|
|
|
|
2020-07-15 10:02:35 +08:00
|
|
|
$user = factory(User::class)->create([
|
2018-01-20 01:46:30 +08:00
|
|
|
'rank_id' => $rank->id,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->settingsRepo->store('pireps.restrict_aircraft_to_rank', false);
|
|
|
|
|
|
|
|
$subfleets = $this->userSvc->getAllowableSubfleets($user);
|
|
|
|
$this->assertEquals(2, $subfleets->count());
|
|
|
|
|
|
|
|
$all_aircraft = array_merge(
|
|
|
|
$subfleets[0]->aircraft->pluck('id')->toArray(),
|
|
|
|
$subfleets[1]->aircraft->pluck('id')->toArray()
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals($added_aircraft, $all_aircraft);
|
|
|
|
|
2020-09-30 01:21:24 +08:00
|
|
|
$subfleetACalled = collect($subfleets)->firstWhere('id', $subfleetA['subfleet']->id);
|
|
|
|
$this->assertEquals($subfleetACalled->fares[0]['price'], $overrides['price']);
|
|
|
|
$this->assertEquals($subfleetACalled->fares[0]['capacity'], $overrides['capacity']);
|
|
|
|
|
2018-01-20 01:46:30 +08:00
|
|
|
/**
|
2020-10-01 00:58:45 +08:00
|
|
|
* Check via API, but should only show the single subfleet being returned
|
2018-01-20 01:46:30 +08:00
|
|
|
*/
|
2020-09-30 01:21:24 +08:00
|
|
|
$this->settingsRepo->store('pireps.restrict_aircraft_to_rank', true);
|
|
|
|
|
2018-01-20 01:46:30 +08:00
|
|
|
$resp = $this->get('/api/user/fleet', [], $user)->assertStatus(200);
|
|
|
|
|
2020-09-30 01:21:24 +08:00
|
|
|
// Get all the aircraft from that subfleet, check the fares
|
2018-02-11 08:17:38 +08:00
|
|
|
$body = $resp->json()['data'];
|
2020-09-30 01:21:24 +08:00
|
|
|
$subfleetAFromApi = collect($body)->firstWhere('id', $subfleetA['subfleet']->id);
|
|
|
|
$this->assertEquals($subfleetAFromApi['fares'][0]['price'], $overrides['price']);
|
|
|
|
$this->assertEquals($subfleetAFromApi['fares'][0]['capacity'], $overrides['capacity']);
|
2020-10-01 00:58:45 +08:00
|
|
|
|
|
|
|
// Read the user's profile and make sure that subfleet C is not part of this
|
|
|
|
// Should only return a single subfleet (subfleet A)
|
|
|
|
$resp = $this->get('/api/user', [], $user);
|
|
|
|
$resp->assertStatus(200);
|
|
|
|
|
|
|
|
$body = $resp->json('data');
|
|
|
|
$subfleets = $body['rank']['subfleets'];
|
|
|
|
|
|
|
|
$this->assertEquals(1, count($subfleets));
|
|
|
|
$this->assertEquals($subfleets[0]['fares'][0]['price'], $overrides['price']);
|
|
|
|
$this->assertEquals($subfleets[0]['fares'][0]['capacity'], $overrides['capacity']);
|
2018-01-20 01:46:30 +08:00
|
|
|
}
|
2018-02-10 04:26:14 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Flip the setting for getting all of the user's aircraft restricted
|
|
|
|
* by rank. Make sure that they're all returned. Create two subfleets,
|
|
|
|
* assign only one of them to the user's rank. When calling the api
|
|
|
|
* to retrieve the flight, only subfleetA should be showing
|
|
|
|
*/
|
|
|
|
public function testGetAircraftAllowedFromFlight()
|
|
|
|
{
|
2018-08-27 00:40:04 +08:00
|
|
|
// Add subfleets and aircraft, but also add another
|
|
|
|
// set of subfleets
|
2020-05-23 23:43:29 +08:00
|
|
|
$airport = factory(\App\Models\Airport::class)->create();
|
2018-02-21 02:59:49 +08:00
|
|
|
$subfleetA = $this->createSubfleetWithAircraft(2, $airport->id);
|
|
|
|
$subfleetB = $this->createSubfleetWithAircraft(2);
|
2018-02-10 04:26:14 +08:00
|
|
|
|
2018-02-21 02:59:49 +08:00
|
|
|
$rank = $this->createRank(10, [$subfleetA['subfleet']->id]);
|
2020-07-15 10:02:35 +08:00
|
|
|
$user = factory(User::class)->create([
|
2018-02-10 05:07:34 +08:00
|
|
|
'curr_airport_id' => $airport->id,
|
2018-08-27 00:40:04 +08:00
|
|
|
'rank_id' => $rank->id,
|
2018-02-10 05:07:34 +08:00
|
|
|
]);
|
|
|
|
|
2020-05-23 23:43:29 +08:00
|
|
|
$flight = factory(\App\Models\Flight::class)->create([
|
2018-08-27 00:40:04 +08:00
|
|
|
'airline_id' => $user->airline_id,
|
2018-02-10 05:07:34 +08:00
|
|
|
'dpt_airport_id' => $airport->id,
|
|
|
|
]);
|
2018-02-10 04:26:14 +08:00
|
|
|
|
|
|
|
$flight->subfleets()->syncWithoutDetaching([
|
|
|
|
$subfleetA['subfleet']->id,
|
2018-08-27 00:40:04 +08:00
|
|
|
$subfleetB['subfleet']->id,
|
2018-02-10 04:26:14 +08:00
|
|
|
]);
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// Make sure no flights are filtered out
|
2018-02-10 05:36:13 +08:00
|
|
|
$this->settingsRepo->store('pilots.only_flights_from_current', false);
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// And restrict the aircraft
|
2018-02-10 04:26:14 +08:00
|
|
|
$this->settingsRepo->store('pireps.restrict_aircraft_to_rank', false);
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
$response = $this->get('/api/flights/'.$flight->id, [], $user);
|
2018-02-10 04:26:14 +08:00
|
|
|
$response->assertStatus(200);
|
2018-02-11 08:17:38 +08:00
|
|
|
$this->assertCount(2, $response->json()['data']['subfleets']);
|
2018-02-10 04:26:14 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Now make sure it's filtered out
|
|
|
|
*/
|
|
|
|
$this->settingsRepo->store('pireps.restrict_aircraft_to_rank', true);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make sure it's filtered out from the single flight call
|
|
|
|
*/
|
2018-08-27 00:40:04 +08:00
|
|
|
$response = $this->get('/api/flights/'.$flight->id, [], $user);
|
2018-02-10 04:26:14 +08:00
|
|
|
$response->assertStatus(200);
|
2018-02-11 08:17:38 +08:00
|
|
|
$this->assertCount(1, $response->json()['data']['subfleets']);
|
2018-02-10 04:26:14 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Make sure it's filtered out from the flight list
|
|
|
|
*/
|
|
|
|
$response = $this->get('/api/flights', [], $user);
|
2018-02-11 08:17:38 +08:00
|
|
|
$body = $response->json()['data'];
|
2018-02-10 05:36:13 +08:00
|
|
|
$response->assertStatus(200);
|
2018-02-11 08:17:38 +08:00
|
|
|
$this->assertCount(1, $body[0]['subfleets']);
|
2018-02-10 04:26:14 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Filtered from search?
|
|
|
|
*/
|
2018-08-27 00:40:04 +08:00
|
|
|
$response = $this->get('/api/flights/search?flight_id='.$flight->id, [], $user);
|
2018-02-10 04:26:14 +08:00
|
|
|
$response->assertStatus(200);
|
2018-02-11 08:17:38 +08:00
|
|
|
$body = $response->json()['data'];
|
|
|
|
$this->assertCount(1, $body[0]['subfleets']);
|
2018-02-10 04:26:14 +08:00
|
|
|
}
|
2019-07-17 01:54:14 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the pilot ID being added when a new user is created
|
|
|
|
*/
|
|
|
|
public function testUserPilotIdChangeAlreadyExists()
|
|
|
|
{
|
2019-09-17 01:08:26 +08:00
|
|
|
$this->expectException(UserPilotIdExists::class);
|
2020-07-15 10:02:35 +08:00
|
|
|
$user1 = factory(User::class)->create(['id' => 1]);
|
|
|
|
$user2 = factory(User::class)->create(['id' => 2]);
|
2019-07-17 01:54:14 +08:00
|
|
|
|
|
|
|
// Now try to change the original user's pilot_id to 2 (should conflict)
|
|
|
|
$this->userSvc->changePilotId($user1, 2);
|
|
|
|
}
|
|
|
|
|
2020-05-16 23:46:25 +08:00
|
|
|
/**
|
|
|
|
* Make sure that the splitting of the user ID works
|
|
|
|
*/
|
|
|
|
public function testUserPilotIdSplit(): void
|
|
|
|
{
|
2020-07-15 10:02:35 +08:00
|
|
|
/** @var User $user */
|
|
|
|
$user = factory(User::class)->create();
|
2020-05-16 23:46:25 +08:00
|
|
|
$found_user = $this->userSvc->findUserByPilotId($user->ident);
|
|
|
|
$this->assertEquals($user->id, $found_user->id);
|
|
|
|
|
|
|
|
// Look for them with the IATA code
|
|
|
|
$found_user = $this->userSvc->findUserByPilotId($user->airline->iata.$user->id);
|
|
|
|
$this->assertEquals($user->id, $found_user->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pilot ID not found
|
|
|
|
*/
|
|
|
|
public function testUserPilotIdSplitInvalidId(): void
|
|
|
|
{
|
2020-07-15 10:02:35 +08:00
|
|
|
/** @var User $user */
|
|
|
|
$user = factory(User::class)->create();
|
2020-05-16 23:46:25 +08:00
|
|
|
|
|
|
|
$this->expectException(PilotIdNotFound::class);
|
|
|
|
$this->userSvc->findUserByPilotId($user->airline->iata);
|
|
|
|
}
|
|
|
|
|
2020-07-15 10:02:35 +08:00
|
|
|
public function testUserPilotIdInvalidIATA(): void
|
|
|
|
{
|
|
|
|
/** @var Airline $airline */
|
|
|
|
$airline = factory(Airline::class)->create(['icao' => 'ABC', 'iata' => null]);
|
|
|
|
|
|
|
|
/** @var User $user */
|
|
|
|
$user = factory(User::class)->create(['airline_id' => $airline->id]);
|
|
|
|
|
|
|
|
$this->expectException(PilotIdNotFound::class);
|
|
|
|
$this->userSvc->findUserByPilotId('123');
|
|
|
|
}
|
|
|
|
|
2019-07-17 01:54:14 +08:00
|
|
|
/**
|
|
|
|
* Test the pilot ID being added when a new user is created
|
|
|
|
*/
|
|
|
|
public function testUserPilotIdAdded()
|
|
|
|
{
|
2020-07-15 10:02:35 +08:00
|
|
|
$new_user = factory(User::class)->make()->toArray();
|
2020-02-24 06:21:26 +08:00
|
|
|
$new_user['password'] = Hash::make('secret');
|
2019-07-17 01:54:14 +08:00
|
|
|
$user = $this->userSvc->createUser($new_user);
|
|
|
|
$this->assertEquals($user->id, $user->pilot_id);
|
|
|
|
|
|
|
|
// Add a second user
|
2020-07-15 10:02:35 +08:00
|
|
|
$new_user = factory(User::class)->make()->toArray();
|
2020-02-24 06:21:26 +08:00
|
|
|
$new_user['password'] = Hash::make('secret');
|
2019-07-17 01:54:14 +08:00
|
|
|
$user2 = $this->userSvc->createUser($new_user);
|
|
|
|
$this->assertEquals($user2->id, $user2->pilot_id);
|
|
|
|
|
|
|
|
// Now try to change the original user's pilot_id to 3
|
|
|
|
$user = $this->userSvc->changePilotId($user, 3);
|
|
|
|
$this->assertEquals(3, $user->pilot_id);
|
|
|
|
|
|
|
|
// Create a new user and the pilot_id should be 4
|
2020-07-15 10:02:35 +08:00
|
|
|
$user3 = factory(User::class)->create();
|
2019-07-17 01:54:14 +08:00
|
|
|
$this->assertEquals(4, $user3->pilot_id);
|
|
|
|
}
|
2020-04-07 00:03:19 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that a user's name is private
|
|
|
|
*/
|
|
|
|
public function testUserNamePrivate()
|
|
|
|
{
|
|
|
|
$vals = [
|
|
|
|
'Firstname' => 'Firstname',
|
|
|
|
'Firstname Lastname' => 'Firstname L',
|
|
|
|
'Firstname Middlename Lastname' => 'Firstname L',
|
|
|
|
];
|
|
|
|
|
|
|
|
foreach ($vals as $input => $expected) {
|
|
|
|
$user = new User(['name' => $input]);
|
|
|
|
$this->assertEquals($expected, $user->name_private);
|
|
|
|
}
|
|
|
|
}
|
2021-02-18 07:54:18 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function testUserLeave(): void
|
|
|
|
{
|
|
|
|
$this->createUser(['status' => UserState::ACTIVE]);
|
|
|
|
|
|
|
|
$users_on_leave = $this->userSvc->findUsersOnLeave();
|
|
|
|
$this->assertEquals(0, count($users_on_leave));
|
|
|
|
|
|
|
|
$this->updateSetting('pilots.auto_leave_days', 1);
|
|
|
|
$user = $this->createUser([
|
|
|
|
'status' => UserState::ACTIVE,
|
|
|
|
'created_at' => Carbon::now('UTC')->subDays(5),
|
|
|
|
]);
|
|
|
|
|
|
|
|
$users_on_leave = $this->userSvc->findUsersOnLeave();
|
|
|
|
$this->assertEquals(1, count($users_on_leave));
|
|
|
|
$this->assertEquals($user->id, $users_on_leave[0]->id);
|
|
|
|
|
|
|
|
// Give that user a new PIREP, still old
|
|
|
|
/** @var \App\Models\Pirep $pirep */
|
|
|
|
$pirep = factory(Pirep::class)->create(['submitted_at' => Carbon::now('UTC')->subDays(5)]);
|
|
|
|
|
|
|
|
$user->last_pirep_id = $pirep->id;
|
|
|
|
$user->save();
|
|
|
|
$user->refresh();
|
|
|
|
|
|
|
|
$users_on_leave = $this->userSvc->findUsersOnLeave();
|
|
|
|
$this->assertEquals(1, count($users_on_leave));
|
|
|
|
$this->assertEquals($user->id, $users_on_leave[0]->id);
|
|
|
|
|
|
|
|
// Create a new PIREP
|
|
|
|
/** @var \App\Models\Pirep $pirep */
|
|
|
|
$pirep = factory(Pirep::class)->create(['submitted_at' => Carbon::now('UTC')]);
|
|
|
|
|
|
|
|
$user->last_pirep_id = $pirep->id;
|
|
|
|
$user->save();
|
|
|
|
$user->refresh();
|
|
|
|
|
|
|
|
$users_on_leave = $this->userSvc->findUsersOnLeave();
|
|
|
|
$this->assertEquals(0, count($users_on_leave));
|
|
|
|
}
|
2018-01-20 01:46:30 +08:00
|
|
|
}
|