Skip to content

Commit

Permalink
delete keys bug
Browse files Browse the repository at this point in the history
- fixed delete key routine where only the default locale was expected
  to be provided now iterates over the entire trnslator bag
  • Loading branch information
wickedOne committed Feb 8, 2023
1 parent ac57f0b commit 3fa7c28
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
35 changes: 18 additions & 17 deletions src/PhraseProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,29 +165,30 @@ public function write(TranslatorBagInterface $translatorBag): void

public function delete(TranslatorBagInterface $translatorBag): void
{
$defaultCatalogue = $translatorBag->getCatalogue($this->defaultLocale);
$keys = [[]];

/** @var string $domain */
foreach ($defaultCatalogue->getDomains() as $domain) {
/** @var string[] $keys */
if ([] === $keys = array_keys($defaultCatalogue->all($domain))) {
continue;
foreach ($translatorBag->getCatalogues() as $catalogue) {
/** @var string $domain */
foreach ($catalogue->getDomains() as $domain) {
/* @var string[] $keys */
$keys[] = array_keys($catalogue->all($domain));
}
}

$names = array_map(static fn ($v): ?string => preg_replace('/([\s:,])/', '\\\\\\\\$1', $v), $keys);
$keys = array_unique(array_merge(...$keys));
$names = array_map(static fn ($v): ?string => preg_replace('/([\s:,])/', '\\\\\\\\$1', $v), $keys);

foreach ($names as $name) {
$response = $this->httpClient->request('DELETE', 'keys', [
'query' => [
'q' => 'name:' . $name,
],
]);
foreach ($names as $name) {
$response = $this->httpClient->request('DELETE', 'keys', [
'query' => [
'q' => 'name:' . $name,
],
]);

if (200 !== $statusCode = $response->getStatusCode()) {
$this->logger->error(sprintf('Unable to delete key "%s" in phrase: "%s".', $name, $response->getContent(false)));
if (200 !== $statusCode = $response->getStatusCode()) {
$this->logger->error(sprintf('Unable to delete key "%s" in phrase: "%s".', $name, $response->getContent(false)));

$this->throwProviderException($statusCode, $response, 'Unable to delete key in phrase.');
}
$this->throwProviderException($statusCode, $response, 'Unable to delete key in phrase.');
}
}
}
Expand Down
19 changes: 18 additions & 1 deletion tests/PhraseProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -592,12 +592,29 @@ public function testDelete(): void
],
]));

$bag->addCatalogue(new MessageCatalogue('de', [
'validators' => [],
'messages' => [
'another:erroneous:key' => 'value to delete',
'delete this,erroneous:key' => 'translated value',
],
]));

$responses = [
'delete key' => function (string $method, string $url): ResponseInterface {
'delete key one' => function (string $method, string $url): ResponseInterface {
$this->assertSame('DELETE', $method);
// https://api.phrase.com/api/v2/projects/1/keys?q=name:delete\\ this\\,erroneous\\:key
$this->assertSame('https://api.phrase.com/api/v2/projects/1/keys?q=name%3Adelete%5C%5C%20this%5C%5C%2Cerroneous%5C%5C%3Akey', $url);

return new MockResponse('', [
'http_code' => 200,
]);
},
'delete key two' => function (string $method, string $url): ResponseInterface {
$this->assertSame('DELETE', $method);
// 'https://api.phrase.com/api/v2/projects/1/keys?q=name:another\\:erroneous\\:key'
$this->assertSame('https://api.phrase.com/api/v2/projects/1/keys?q=name%3Aanother%5C%5C%3Aerroneous%5C%5C%3Akey', $url);

return new MockResponse('', [
'http_code' => 200,
]);
Expand Down

0 comments on commit 3fa7c28

Please sign in to comment.