Skip to content

Commit

Permalink
Merge branch '7.1' into 7.2
Browse files Browse the repository at this point in the history
* 7.1:
  remove conflict with symfony/serializer < 6.4
  Reviewed and Translated zh_CN
  [PropertyInfo] Fix write visibility for Asymmetric Visibility and Virtual Properties
  [Translation] [Bridge][Lokalise] Fix empty keys array in PUT, DELETE requests causing Lokalise API error
  • Loading branch information
nicolas-grekas committed Nov 27, 2024
2 parents 734f783 + ed9b0cc commit 0d24089
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
8 changes: 8 additions & 0 deletions LokaliseProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ public function delete(TranslatorBagInterface $translatorBag): void
$keysIds += $this->getKeysIds($keysToDelete, $domain);
}

if (!$keysIds) {
return;
}

$response = $this->client->request('DELETE', 'keys', [
'json' => ['keys' => array_values($keysIds)],
]);
Expand Down Expand Up @@ -245,6 +249,10 @@ private function updateTranslations(array $keysByDomain, TranslatorBagInterface
}
}

if (!$keysToUpdate) {
return;
}

$response = $this->client->request('PUT', 'keys', [
'json' => ['keys' => $keysToUpdate],
]);
Expand Down
82 changes: 82 additions & 0 deletions Tests/LokaliseProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,56 @@ public function testCompleteWriteProcess()
$this->assertTrue($updateProcessed, 'Translations update was not called.');
}

public function testUpdateProcessWhenLocalTranslationsMatchLokaliseTranslations()
{
$getLanguagesResponse = function (string $method, string $url): ResponseInterface {
$this->assertSame('GET', $method);
$this->assertSame('https://api.lokalise.com/api2/projects/PROJECT_ID/languages', $url);

return new MockResponse(json_encode([
'languages' => [
['lang_iso' => 'en'],
['lang_iso' => 'fr'],
],
]));
};

$failOnPutRequest = function (string $method, string $url, array $options = []): void {
$this->assertSame('PUT', $method);
$this->assertSame('https://api.lokalise.com/api2/projects/PROJECT_ID/keys', $url);
$this->assertSame(json_encode(['keys' => []]), $options['body']);

$this->fail('PUT request is invalid: an empty `keys` array was provided, resulting in a Lokalise API error');
};

$mockHttpClient = (new MockHttpClient([
$getLanguagesResponse,
$failOnPutRequest,
]))->withOptions([
'base_uri' => 'https://api.lokalise.com/api2/projects/PROJECT_ID/',
'headers' => ['X-Api-Token' => 'API_KEY'],
]);

$provider = self::createProvider(
$mockHttpClient,
$this->getLoader(),
$this->getLogger(),
$this->getDefaultLocale(),
'api.lokalise.com'
);

// TranslatorBag with catalogues that do not store any message to mimic the behaviour of
// Symfony\Component\Translation\Command\TranslationPushCommand when local translations and Lokalise
// translations match without any changes in both translation sets
$translatorBag = new TranslatorBag();
$translatorBag->addCatalogue(new MessageCatalogue('en', []));
$translatorBag->addCatalogue(new MessageCatalogue('fr', []));

$provider->write($translatorBag);

$this->assertSame(1, $mockHttpClient->getRequestsCount());
}

public function testWriteGetLanguageServerError()
{
$getLanguagesResponse = function (string $method, string $url, array $options = []): ResponseInterface {
Expand Down Expand Up @@ -723,6 +773,38 @@ public function testDeleteProcess()
$provider->delete($translatorBag);
}

public function testDeleteProcessWhenLocalTranslationsMatchLokaliseTranslations()
{
$failOnDeleteRequest = function (string $method, string $url, array $options = []): void {
$this->assertSame('DELETE', $method);
$this->assertSame('https://api.lokalise.com/api2/projects/PROJECT_ID/keys', $url);
$this->assertSame(json_encode(['keys' => []]), $options['body']);

$this->fail('DELETE request is invalid: an empty `keys` array was provided, resulting in a Lokalise API error');
};

// TranslatorBag with catalogues that do not store any message to mimic the behaviour of
// Symfony\Component\Translation\Command\TranslationPushCommand when local translations and Lokalise
// translations match without any changes in both translation sets
$translatorBag = new TranslatorBag();
$translatorBag->addCatalogue(new MessageCatalogue('en', []));
$translatorBag->addCatalogue(new MessageCatalogue('fr', []));

$mockHttpClient = new MockHttpClient([$failOnDeleteRequest], 'https://api.lokalise.com/api2/projects/PROJECT_ID/');

$provider = self::createProvider(
$mockHttpClient,
$this->getLoader(),
$this->getLogger(),
$this->getDefaultLocale(),
'api.lokalise.com'
);

$provider->delete($translatorBag);

$this->assertSame(0, $mockHttpClient->getRequestsCount());
}

public static function getResponsesForOneLocaleAndOneDomain(): \Generator
{
$arrayLoader = new ArrayLoader();
Expand Down

0 comments on commit 0d24089

Please sign in to comment.