Skip to content

Commit

Permalink
part one, now on to testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
robertbrowncc committed Apr 22, 2024
1 parent a4346ba commit 5917961
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
3 changes: 3 additions & 0 deletions app/Http/Controllers/Traits/Sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
trait Sanitizer
{
/**
* @param array $validated
*
* @return array
*/
public function sanitizeData(array $validated): array
Expand All @@ -19,6 +21,7 @@ public function sanitizeData(array $validated): array
$validated['end_date_service_restriction'] = $this->sanitizeDate($validated['end_date_service_restriction'] ?? null);

$validated['territorial_scope'] = $this->european_countries_service->filterSortEuropeanCountries($validated['territorial_scope'] ?? []);

$validated['content_type'] = array_unique($validated['content_type']);
sort($validated['content_type']);
if (array_key_exists('decision_visibility', $validated)) {
Expand Down
10 changes: 10 additions & 0 deletions app/Services/GroupedSubmissionsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@

class GroupedSubmissionsService
{
public function __construct(protected EuropeanCountriesService $european_countries_service)
{

}

public function sanitizePayloadStatement(&$payload_statement): void
{
Expand Down Expand Up @@ -47,8 +51,10 @@ public function sanitizePayloadStatement(&$payload_statement): void
}

/**
* @param array $payload_statement
* @param $field
* @param $needle
*
* @return void
*/
private function handleOtherFieldWithinArray(array &$payload_statement, $field, $needle): void
Expand Down Expand Up @@ -126,6 +132,8 @@ public function sanitizePayload(
$statement['decision_visibility'] ?? [], true);
$content_type_other_required = in_array('CONTENT_TYPE_OTHER', $statement['content_type'] ?? [], true);



// Create a new validator instance for each statement
$validator = Validator::make($statement,
$this->multi_rules($decision_visibility_other_required, $content_type_other_required),
Expand All @@ -136,6 +144,8 @@ public function sanitizePayload(
$errors['statement_' . $index] = $validator->errors()->toArray();
}

$statement['territorial_scope'] = $this->european_countries_service->filterSortEuropeanCountries($statement['territorial_scope'] ?? []);

try {
$payload['statements'][$index] = $validator->validated();
} catch (ValidationException) {
Expand Down
15 changes: 14 additions & 1 deletion tests/Feature/Services/EuropeanCountriesServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,18 @@ public function ensure_that_country_codes_arrays_are_in_alphabetical_order(): vo
$this->assertEquals($result, $codes);
}


/**
* @test
* @throws \JsonException
*/
public function the_char_length_is_ok(): void
{
$max = 255;
$all = json_encode(EuropeanCountriesService::EUROPEAN_UNION_COUNTRY_CODES, JSON_THROW_ON_ERROR);
$this->assertLessThan($max, strlen($all));
$all = json_encode(EuropeanCountriesService::EUROPEAN_COUNTRY_CODES, JSON_THROW_ON_ERROR);
$this->assertLessThan($max, strlen($all));
$all = json_encode(EuropeanCountriesService::EUROPEAN_ECONOMIC_AREA_COUNTRY_CODES, JSON_THROW_ON_ERROR);
$this->assertLessThan($max, strlen($all));
}
}

0 comments on commit 5917961

Please sign in to comment.