2018-01-20 01:46:30 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
use App\Repositories\SettingRepository;
|
2018-02-21 12:33:09 +08:00
|
|
|
use App\Services\UserService;
|
2018-01-20 01:46:30 +08:00
|
|
|
|
|
|
|
class UserTest extends TestCase
|
|
|
|
{
|
2018-08-27 00:40:04 +08:00
|
|
|
protected $settingsRepo;
|
|
|
|
protected $userSvc;
|
2018-01-20 01:46:30 +08:00
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
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
|
|
|
|
|
|
|
$user = factory(App\Models\User::class)->create([
|
|
|
|
'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
|
|
|
|
*/
|
|
|
|
public function testGetAllAircraft()
|
|
|
|
{
|
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
|
|
|
|
|
|
|
$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
|
|
|
|
|
|
|
$user = factory(App\Models\User::class)->create([
|
|
|
|
'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);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check via API
|
|
|
|
*/
|
|
|
|
$resp = $this->get('/api/user/fleet', [], $user)->assertStatus(200);
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// Get all the aircraft from that subfleet
|
2018-02-11 08:17:38 +08:00
|
|
|
$body = $resp->json()['data'];
|
2018-01-20 01:46:30 +08:00
|
|
|
$aircraft_from_api = array_merge(
|
|
|
|
collect($body[0]['aircraft'])->pluck('id')->toArray(),
|
|
|
|
collect($body[1]['aircraft'])->pluck('id')->toArray()
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals($added_aircraft, $aircraft_from_api);
|
|
|
|
}
|
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
|
2018-02-10 05:07:34 +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]);
|
2018-02-10 05:07:34 +08:00
|
|
|
$user = factory(App\Models\User::class)->create([
|
|
|
|
'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
|
|
|
]);
|
|
|
|
|
|
|
|
$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
|
|
|
]);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now we can do some actual tests
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do some sanity checks first
|
|
|
|
*/
|
2018-02-10 05:36:13 +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
|
|
|
}
|
2018-01-20 01:46:30 +08:00
|
|
|
}
|