Skip to content

Commit

Permalink
Merge pull request #1736 from cultuurnet/III-6239-tariffName-trimmed
Browse files Browse the repository at this point in the history
III-6239 tariff name trimmed
  • Loading branch information
JonasVHG committed Aug 6, 2024
2 parents d477892 + 73d22e2 commit 565405a
Show file tree
Hide file tree
Showing 4 changed files with 210 additions and 14 deletions.
68 changes: 68 additions & 0 deletions features/event/price-info.feature
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,74 @@ Feature: Test event priceInfo property
}
"""

Scenario: Try updating an event with a tariff that only differs in spacing
When I set the JSON request payload to:
"""
[
{
"category": "base",
"name": {
"nl": "Basistarief",
"fr": "Tarif de base",
"en": "Base tariff",
"de": "Basisrate"
},
"price": 10,
"priceCurrency": "EUR"
},
{
"category": "tariff",
"name": {
"nl": "Korting",
"fr": "Reduction",
"en": "Reduction",
"de": "Rabatt"
},
"price": 5,
"priceCurrency": "EUR"
},
{
"category": "tariff",
"name": {
"nl": "Korting ",
"fr": "Reduction ",
"en": "Reduction ",
"de": "Rabatt "
},
"price": 3,
"priceCurrency": "EUR"
}
]
"""
And I send a PUT request to "%{eventUrl}/priceInfo"
Then the response status should be "400"
Then the JSON response should be:
"""
{
"schemaErrors": [
{
"error": "Tariff name \"Korting\" must be unique.",
"jsonPointer": "/priceInfo/2/name/nl"
},
{
"error": "Tariff name \"Reduction\" must be unique.",
"jsonPointer": "/priceInfo/2/name/fr"
},
{
"error": "Tariff name \"Reduction\" must be unique.",
"jsonPointer": "/priceInfo/2/name/en"
},
{
"error": "Tariff name \"Rabatt\" must be unique.",
"jsonPointer": "/priceInfo/2/name/de"
}
],
"status": 400,
"title": "Invalid body data",
"type": "https://api.publiq.be/probs/body/invalid-data"
}
"""

Scenario: Try creating an event duplicate tariff names
Given I set the JSON request payload from "events/price-info/event-with-duplicate-tariff-names.json"
When I send a POST request to "/events/"
Expand Down
47 changes: 47 additions & 0 deletions features/place/price-info.feature
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,53 @@ Feature: Test place priceInfo property
}
"""

Scenario: Try Updating a place price with duplicate with different spacing
When I set the JSON request payload to:
"""
[
{
"category": "base",
"name": {
"nl": "Basistarief"
},
"price": 10,
"priceCurrency": "EUR"
},
{
"category": "tariff",
"name": {
"nl": "Early Birds"
},
"price": 10,
"priceCurrency": "EUR"
},
{
"category": "tariff",
"name": {
"nl": "Early Birds "
},
"price": 5,
"priceCurrency": "EUR"
}
]
"""
And I send a PUT request to "%{placeUrl}/priceInfo"
Then the response status should be "400"
And the JSON response should be:
"""
{
"schemaErrors": [
{
"error": "Tariff name \"Early Birds\" must be unique.",
"jsonPointer": "/priceInfo/2/name/nl"
}
],
"status": 400,
"title": "Invalid body data",
"type": "https://api.publiq.be/probs/body/invalid-data"
}
"""

Scenario: Try updating a place price as a string
When I set the JSON request payload to:
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ private function getSchemaErrors(array $priceInfos): array
$errors = [];
$nameMatrix = [];

$priceInfos = $this->trimTariffName($priceInfos);

foreach ($priceInfos as $index => $priceInfo) {
foreach ($priceInfo['name'] as $language => $name) {
if (isset($nameMatrix[$language]) && in_array($name, $nameMatrix[$language], true)) {
Expand All @@ -56,4 +58,16 @@ private function getSchemaErrors(array $priceInfos): array

return $errors;
}

private function trimTariffName(array $priceInfos): array
{
foreach ($priceInfos as $key => $tariff) {
if (array_key_exists('name', $tariff)) {
foreach ($tariff['name'] as $language => $name) {
$priceInfos[$key]['name'][$language] = trim($priceInfos[$key]['name'][$language]);
}
}
}
return $priceInfos;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use CultuurNet\UDB3\Http\ApiProblem\AssertApiProblemTrait;
use CultuurNet\UDB3\Http\ApiProblem\SchemaError;
use CultuurNet\UDB3\Http\Request\Psr7RequestBuilder;
use CultuurNet\UDB3\Json;
use PHPUnit\Framework\TestCase;

final class PriceInfoDuplicateNameValidatingRequestBodyParserTest extends TestCase
Expand Down Expand Up @@ -62,52 +63,113 @@ public function it_handles_priceInfo(): void
*/
public function it_throws_on_same_names(): void
{
$priceInfo = (object) [
'priceInfo' => (object) [
(object)[
$priceInfo = [
'priceInfo' => [
[
'category' => 'tariff',
'name' => (object) [
'name' => [
'nl' => 'Studenten',
],
'price' => 10,
'priceCurrency' => 'EUR',
],
(object)[
[
'category' => 'base',
'name' => (object) [
'name' => [
'nl' => 'Basistarief',
],
'price' => 50,
'priceCurrency' => 'EUR',
],
(object)[
[
'category' => 'tariff',
'name' => (object) [
'name' => [
'nl' => 'Studenten',
],
'price' => 10,
'priceCurrency' => 'EUR',
],
(object)[
[
'category' => 'tariff',
'name' => (object) [
'name' => [
'nl' => 'Leraren',
],
'price' => 20,
],
(object)[
[
'category' => 'tariff',
'name' => (object) [
'name' => [
'nl' => 'Studenten',
],
'price' => 10,
],
],
];

$request = $this->requestBuilder
->build('POST')
->withParsedBody($this->arrayAsObject($priceInfo));

$this->assertCallableThrowsApiProblem(
ApiProblem::bodyInvalidData(
new SchemaError('/priceInfo/2/name/nl', 'Tariff name "Studenten" must be unique.'),
new SchemaError('/priceInfo/4/name/nl', 'Tariff name "Studenten" must be unique.')
),
fn () => $this->priceInfoDuplicateNameValidatingRequestBodyParser->parse($request)
);
}

/**
* @test
*/
public function it_throws_on_same_names_with_different_spacing(): void
{
$priceInfo = [
'priceInfo' => [
[
'category' => 'tariff',
'name' => [
'nl' => 'Studenten',
],
'price' => 10,
'priceCurrency' => 'EUR',
],
];
[
'category' => 'base',
'name' => [
'nl' => 'Basistarief',
],
'price' => 50,
'priceCurrency' => 'EUR',
],
[
'category' => 'tariff',
'name' => [
'nl' => 'Studenten ',
],
'price' => 15,
'priceCurrency' => 'EUR',
],
[
'category' => 'tariff',
'name' => [
'nl' => 'Leraren',
],
'price' => 20,
],
[
'category' => 'tariff',
'name' => [
'nl' => 'Studenten ',
],
'price' => 30,
],
],
];

$request = $this->requestBuilder
->build('POST')
->withParsedBody($priceInfo);
->withParsedBody($this->arrayAsObject($priceInfo));

$this->assertCallableThrowsApiProblem(
ApiProblem::bodyInvalidData(
Expand All @@ -117,4 +179,9 @@ public function it_throws_on_same_names(): void
fn () => $this->priceInfoDuplicateNameValidatingRequestBodyParser->parse($request)
);
}

private function arrayAsObject(array $priceInfoArray): object
{
return Json::decode(Json::encode($priceInfoArray));
}
}

0 comments on commit 565405a

Please sign in to comment.