Skip to content
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: 1 addition & 2 deletions app/Console/Commands/GroupCountryField.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace App\Console\Commands;

use App\Group;
use App\Helpers\Fixometer;
use Illuminate\Console\Command;

class GroupCountryField extends Command
Expand All @@ -30,7 +29,7 @@ public function handle(): void
$groups = Group::all();

foreach ($groups as $group) {
$group->country = Fixometer::getCountryFromCountryCode($group->country_code);
$group->country = Group::countryNameForCode($group->country_code);
$group->save();
}
}
Expand Down
26 changes: 26 additions & 0 deletions app/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,32 @@ public function toArray()

// Setters

/**
* Keep the legacy `country` column in step with `country_code`. `country` is still read by external
* consumers (e.g. ORA exports, direct DB reporting) so it must be populated as soon as the group is saved,
* rather than waiting for the hourly groups:country job. Countries are stored in English.
*/
public function setCountryCodeAttribute($value)
{
$this->attributes['country_code'] = $value;
$this->attributes['country'] = self::countryNameForCode($value);
}

/**
* The English country name stored in the legacy `country` column; '' for an empty or unknown code. Group
* imports have used 'UK' for the United Kingdom, which isn't an ISO code.
*/
public static function countryNameForCode($code): string
{
if (! $code) {
return '';
}

$code = strtoupper($code) === 'UK' ? 'GB' : $code;

return \App\Helpers\Fixometer::getAllCountries('en')[$code] ?? '';
}

//Getters
public function findAll()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

use App\Group;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

return new class extends Migration
{
public function up(): void
{
// Groups created since country_code was introduced only had `country` filled in by the hourly
// groups:country job, and recent groups were found with it empty. The Group model now sets it on
// save; backfill any existing gaps here. Query builder rather than models so no audit rows or
// update listeners (WordPress/Discourse sync) fire. Countries are stored in English.
$groups = DB::table('groups')
->whereNotNull('country_code')
->where(function ($q) {
$q->whereNull('country')->orWhere('country', '');
})
->get(['idgroups', 'country_code']);

foreach ($groups as $group) {
$country = Group::countryNameForCode($group->country_code);

if ($country !== '') {
DB::table('groups')->where('idgroups', $group->idgroups)->update(['country' => $country]);
} else {
Log::warning("Group {$group->idgroups}: no country name for code '{$group->country_code}'");
}
}
}

public function down(): void
{
// Data-only backfill; nothing to undo.
}
};
24 changes: 24 additions & 0 deletions tests/Feature/Groups/GroupCountryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,30 @@
$this->artisan('groups:country');

$group = Group::find($group->idgroups);
$this->assertEquals('United Kingdom', $group->country);

Check failure on line 30 in tests/Feature/Groups/GroupCountryTest.php

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Define a constant instead of duplicating this literal "United Kingdom" 3 times.

See more on https://sonarcloud.io/project/issues?id=TheRestartProject_restarters.net&issues=AaDZMaq9kXWrHeTEHMil&open=AaDZMaq9kXWrHeTEHMil&pullRequest=916
}

public function testCountrySetOnSaveWithoutJob(): void {
// fr-BE has its own country names (e.g. 'Belgique'); the stored value must still be English.
app()->setLocale('fr-BE');

$group = Group::factory()->create(['country_code' => 'BE']);
$this->assertEquals('Belgium', Group::find($group->idgroups)->country);

$group->country_code = 'GB';
$group->save();
$this->assertEquals('United Kingdom', Group::find($group->idgroups)->country);
}

public function testUnknownAndLegacyCodes(): void {
$this->assertEquals('United Kingdom', Group::countryNameForCode('UK'));
$this->assertEquals('', Group::countryNameForCode('ZZ'));
$this->assertEquals('', Group::countryNameForCode(null));

// The model and the hourly job agree, so the column doesn't flip between values.
$group = Group::factory()->create(['country_code' => 'ZZ']);
$this->assertEquals('', Group::find($group->idgroups)->country);
$this->artisan('groups:country');
$this->assertEquals('', Group::find($group->idgroups)->country);
}
}
Loading