Skip to content

Commit

Permalink
Merge branch 'feature/double-territories' into dev
Browse files Browse the repository at this point in the history
* feature/double-territories:
  finalizes the countries bug.

# Conflicts:
#	app/Services/GroupedSubmissionsService.php
  • Loading branch information
robertbrowncc committed Apr 23, 2024
2 parents edc2053 + 81a143e commit fdc18e0
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/Services/EuropeanCountriesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public function getOptionsArray(): array
public function filterSortEuropeanCountries(array $countries): array
{
$intersection = array_intersect($countries, self::EUROPEAN_COUNTRY_CODES) ?? [];
$filtered = array_unique($intersection);
$filtered = array_unique(array_values($intersection));
sort($filtered);
return $filtered;
}
Expand Down
2 changes: 1 addition & 1 deletion app/Services/GroupedSubmissionsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public function sanitizePayload(

try {
$payload['statements'][$index] = $validator->validated();
$payload['statements'][$index]['territorial_scope'] = $this->european_countries_service->filterSortEuropeanCountries($statement['territorial_scope'] ?? []);
$payload['statements'][$index]['territorial_scope'] = $this->european_countries_service->filterSortEuropeanCountries($payload['statements'][$index]['territorial_scope'] ?? []);
} catch (ValidationException) {
}
}
Expand Down
20 changes: 20 additions & 0 deletions tests/Feature/Models/StatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use App\Models\Statement;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class StatementTest extends TestCase
Expand Down Expand Up @@ -61,6 +62,25 @@ public function territorial_scope_is_always_sorted(): void
$statement->save();
$statement->refresh();

// Get it back in alpha order
$territorial_scope = $statement->territorial_scope;
$this->assertEquals(["AU", "BE", "SK"], $territorial_scope);
}

/**
* @return void
* @test
*/
public function territorial_scope_is_always_unique(): void
{
$statement = Statement::all()->random()->first();

// Store in non alpha order
$statement->territorial_scope = ['SK', 'BE', 'AU', 'SK', 'BE', 'AU', 'SK', 'BE', 'AU', 'SK', 'BE', 'AU', 'AU', 'AU', 'AU'];
$statement->save();
$statement->refresh();


// Get it back in alpha order
$territorial_scope = $statement->territorial_scope;
$this->assertEquals(["AU", "BE", "SK"], $territorial_scope);
Expand Down
12 changes: 12 additions & 0 deletions tests/Feature/Services/EuropeanCountriesServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ public function it_condenses_european_union_countries(): void
$this->assertEquals(['European Union'], $names);
}

/**
* @return void
* @test
*/
public function it_condenses_countries_uniquely(): void
{
$country_codes = ['SK', 'BE', 'AU', 'SK', 'BE', 'AU', 'SK', 'BE', 'AU', 'SK', 'BE', 'AU', 'AU', 'AU', 'AU'];
$result = $this->european_countries_service->filterSortEuropeanCountries($country_codes);
$this->assertEquals(["BE", "SK"], $result);
}


/**
* @return void
* @test
Expand Down

0 comments on commit fdc18e0

Please sign in to comment.