Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FlightRouteAwards - fix if last pirep is empty/other error conditions #886

Merged
merged 3 commits into from
Oct 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/Contracts/Award.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ abstract public function check($parameter = null): bool;
* You don't really need to mess with anything below here
*/

/** @var \App\Models\Award|null */
protected $award;

/** @var \App\Models\User|null */
protected $user;

public function __construct(AwardModel $award = null, User $user = null)
Expand Down
1 change: 1 addition & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
* @property int state
* @property bool opt_in
* @property string last_pirep_id
* @property Pirep last_pirep
* @property UserFieldValue[] fields
*
* @mixin \Illuminate\Database\Eloquent\Builder
Expand Down
25 changes: 18 additions & 7 deletions modules/Awards/Awards/FlightRouteAwards.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Modules\Awards\Awards;

use App\Contracts\Award;
use ErrorException;
use Illuminate\Support\Facades\Log;

/**
* All award classes need to extend Award and implement the check() method
Expand Down Expand Up @@ -44,17 +46,26 @@ class FlightRouteAwards extends Award
*/
public function check($dptarr = null): bool
{
if (!$dptarr) {
$dptarr = 'XXXX:YYYY';
if ($this->user->last_pirep_id === null) {
return false;
}

$dptarr = strtoupper(trim($dptarr));
if (empty($dptarr)) {
Log::error('FlightRouteAwards: empty departure/arrival string');
return false;
}

try {
[$dpt_icao, $arr_icao] = explode(':', $dptarr);
} catch (ErrorException $e) {
Log::error('FlightRouteAwards: Invalid departure/arrival, val="'.$dptarr.'\"');
return false;
}

$pieces = explode(':', $dptarr);
$dpt = $this->user->last_pirep->dpt_airport_id;
$arr = $this->user->last_pirep->arr_airport_id;

if (strcasecmp($dpt, $pieces[0]) == 0 && strcasecmp($arr, $pieces[1]) == 0) {
return true;
}
return false;
return $dpt === $dpt_icao && $arr === $arr_icao;
}
}
60 changes: 60 additions & 0 deletions tests/AwardsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Models\UserAward;
use App\Services\AwardService;
use App\Services\PirepService;
use Modules\Awards\Awards\FlightRouteAwards;
use Modules\Awards\Awards\PilotFlightAwards;

class AwardsTest extends TestCase
Expand Down Expand Up @@ -69,4 +70,63 @@ public function testAwardsGiven()
$found_award = UserAward::where($w)->first();
$this->assertNotNull($found_award);
}

/**
* Test the flight route
*/
public function testFlightRouteAward()
{
/** @var \App\Models\User $user */
$user = factory(User::class)->create([
'flights' => 0,
]);

/** @var \App\Models\Award $award */
$award = factory(Award::class)->create([
'ref_model' => FlightRouteAwards::class,
'ref_model_params' => 1,
]);

/** @var Pirep $pirep */
$pirep = factory(Pirep::class)->create([
'airline_id' => $user->airline->id,
'user_id' => $user->id,
]);

$flightAward = new FlightRouteAwards($award, $user);

// Test no last PIREP for the user
$this->assertFalse($flightAward->check(''));

// Reinit award, add a last user PIREP id
$user->last_pirep_id = $pirep->id;
$user->save();

$flightAward = new FlightRouteAwards($award, $user);
$validStrs = [
$pirep->dpt_airport_id.':'.$pirep->arr_airport_id,
$pirep->dpt_airport_id.':'.$pirep->arr_airport_id.' ',
$pirep->dpt_airport_id.':'.$pirep->arr_airport_id.':',
strtolower($pirep->dpt_airport_id).':'.strtolower($pirep->arr_airport_id),
];

foreach ($validStrs as $str) {
$this->assertTrue($flightAward->check($str));
}

// Check error conditions
$errStrs = [
'',
' ',
':',
'ABCD:EDFSDF',
$pirep->dpt_airport_id.':',
':'.$pirep->arr_airport_id,
':'.$pirep->arr_airport_id.':',
];

foreach ($errStrs as $err) {
$this->assertFalse($flightAward->check($err));
}
}
}