Skip to content

Commit

Permalink
Merge pull request #1782 from cultuurnet/III-6307/bugfix-escape-chars…
Browse files Browse the repository at this point in the history
…-duplicate-place

III-6307/bugfix escape chars duplicate place
  • Loading branch information
grubolsch authored Sep 11, 2024
2 parents 245604a + af973af commit af9ec0a
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
8 changes: 8 additions & 0 deletions features/Steps/UtilitySteps.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ trait UtilitySteps
{
private bool $initialPreventDuplicatePlaceCreationValue;

/**
* @Given /^I create a name that includes special characters of elastic search and keep it as "([^"]*)"$/
*/
public function iCreateANameThatIncludesSpecialCharactersOfElasticSearchAndKeepItAs(string $variableName): void
{
$this->variableState->setVariable($variableName, '(a)![a]' . uniqid('', true));
}

/**
* @Given I create a random name of :nrOfCharacters characters
*/
Expand Down
20 changes: 19 additions & 1 deletion features/place/duplicate.feature
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,22 @@ Feature: Test creating places
"duplicatePlaceUri": "%{baseUrl}/place/%{originalPlaceId}"
}
"""
Then I restore the duplicate configuration
Then I restore the duplicate configuration

Scenario: Be prevented from creating a new place if we already have one on that address when the the address contains special chars
Given I prevent duplicate place creation
Given I create a name that includes special characters of elastic search and keep it as "name"
Given I create a minimal place and save the "id" as "originalPlaceId" then I should get a "201" response code
Then I wait for the place with url "/places/%{originalPlaceId}" to be indexed
Given I create a minimal place then I should get a "409" response code
Then the JSON response should be:
"""
{
"type": "https://api.publiq.be/probs/uitdatabank/duplicate-place",
"title": "Duplicate place",
"status": 409,
"detail": "A place with this address / name combination already exists. Please use the existing place for your purposes.",
"duplicatePlaceUri": "%{baseUrl}/place/%{originalPlaceId}"
}
"""
Then I restore the duplicate configuration
15 changes: 14 additions & 1 deletion src/Place/ReadModel/Duplicate/UniqueAddressIdentifierFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function create(string $title, Address $address, string $currentUserId):
$this->getParts($title, $address, $currentUserId)
);

return mb_strtolower(implode('_', array_filter($parts)));
return mb_strtolower($this->escapeReservedElasticsearchCharacters(implode('_', array_filter($parts))));
}

private function getParts(string $title, Address $address, string $currentUserId): array
Expand All @@ -29,4 +29,17 @@ private function getParts(string $title, Address $address, string $currentUserId
$currentUserId,
];
}

private function escapeReservedElasticsearchCharacters(string $query): string
{
// List of special characters that need escaping
$specialChars = ['\\', '!', '(', ')', '{', '}', '[', ']', '^', '"', '~', '*', '?', ':', '/'];

// Escape each character
foreach ($specialChars as $char) {
$query = str_replace($char, '\\' . $char, $query);
}

return $query;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ public function hashDataProvider(): array
'user123',
'kerkstraat_1_2000_antwerpen_be_user123',
],
'address with special chars' => [
'',
new Udb3Address(
new Udb3Street('Kerkstraat 1!'),
new Udb3PostalCode('2000'),
new Udb3Locality('Antwerpen(Berchem)'),
new CountryCode('BE')
),
'user123',
'kerkstraat_1\!_2000_antwerpen\(berchem\)_be_user123',
],
];
}
}

0 comments on commit af9ec0a

Please sign in to comment.