Skip to content

Commit

Permalink
Fix replace translations when translated_fields not found (#82)
Browse files Browse the repository at this point in the history
* fix: avoid errors when translated fields are missing

* test: improve tests for replaceWithTranslation

* fix: ensure array
  • Loading branch information
batopa authored Oct 28, 2022
1 parent 1bf0cd2 commit 2963b01
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 12 deletions.
4 changes: 3 additions & 1 deletion src/Controller/Component/ApiFormatterComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ protected function extractTranslatedFields(array $data, string $lang): array
return [];
}

return array_filter(current(Hash::extract($data, $path)));
$translatedFields = (array)Hash::extract($data, $path);

return array_filter((array)array_shift($translatedFields));
}
}
136 changes: 125 additions & 11 deletions tests/TestCase/Controller/Component/ApiFormatterComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,19 @@ public function testEmbedIncluded(array $response, array $expected): void
public function replaceWithTranslationProvider(): array
{
return [
'single object with translation' => [
'single object without translation' => [
[
'data' => [
'id' => 1,
'type' => 'documents',
'attributes' => [
'lang' => 'it',
'title' => 'Ciao',
'description' => 'Mondo',
],
'relationships' => [],
],
],
[
'data' => [
'id' => 1,
Expand All @@ -241,6 +253,21 @@ public function replaceWithTranslationProvider(): array
'title' => 'Ciao',
'description' => 'Mondo',
],
'relationships' => [],
],
],
'en',
],
'single object with translation' => [
[
'data' => [
'id' => 1,
'type' => 'documents',
'attributes' => [
'lang' => 'it',
'title' => 'Hello',
'description' => 'World',
],
'relationships' => [
'translations' => [
'links' => [],
Expand Down Expand Up @@ -268,8 +295,8 @@ public function replaceWithTranslationProvider(): array
'type' => 'documents',
'attributes' => [
'lang' => 'it',
'title' => 'Hello',
'description' => 'World',
'title' => 'Ciao',
'description' => 'Mondo',
],
'relationships' => [
'translations' => [
Expand Down Expand Up @@ -365,8 +392,8 @@ public function replaceWithTranslationProvider(): array
'type' => 'documents',
'attributes' => [
'lang' => 'it',
'title' => 'Ciao',
'description' => 'Mondo',
'title' => 'Hello',
'description' => 'World',
],
'relationships' => [
'translations' => [
Expand All @@ -393,7 +420,7 @@ public function replaceWithTranslationProvider(): array
'type' => 'documents',
'attributes' => [
'lang' => 'it',
'title' => 'Questo è il titolo',
'title' => 'This is the title',
'description' => 'La descrizione',
],
'relationships' => [
Expand Down Expand Up @@ -425,8 +452,8 @@ public function replaceWithTranslationProvider(): array
'type' => 'documents',
'attributes' => [
'lang' => 'it',
'title' => 'Hello',
'description' => 'World',
'title' => 'Ciao',
'description' => 'Mondo',
],
'relationships' => [
'translations' => [
Expand All @@ -453,7 +480,7 @@ public function replaceWithTranslationProvider(): array
'type' => 'documents',
'attributes' => [
'lang' => 'it',
'title' => 'This is the title',
'title' => 'Questo è il titolo',
'description' => 'La descrizione',
],
'relationships' => [
Expand Down Expand Up @@ -603,21 +630,108 @@ public function replaceWithTranslationProvider(): array
],
'it',
],
'multiple objects with and without translation' => [
[
'data' => [
[
'id' => 1,
'type' => 'documents',
'attributes' => [
'lang' => 'it',
'title' => 'Hello',
'description' => 'World',
],
'relationships' => [
'translations' => [
'links' => [],
'data' => [
[
'id' => 10,
'type' => 'translations',
'attributes' => [
'lang' => 'en',
'object_id' => 1,
'translated_fields' => [
'title' => 'Hello',
'description' => 'World',
],
],
],
],
],
],
],
[
'id' => 1,
'type' => 'documents',
'attributes' => [
'lang' => 'it',
'title' => 'Questo è il titolo',
'description' => 'La descrizione',
],
'relationships' => [],
],
],
],
[
'data' => [
[
'id' => 1,
'type' => 'documents',
'attributes' => [
'lang' => 'it',
'title' => 'Ciao',
'description' => 'Mondo',
],
'relationships' => [
'translations' => [
'links' => [],
'data' => [
[
'id' => 10,
'type' => 'translations',
'attributes' => [
'lang' => 'en',
'object_id' => 1,
'translated_fields' => [
'title' => 'Hello',
'description' => 'World',
],
],
],
],
],
],
],
[
'id' => 1,
'type' => 'documents',
'attributes' => [
'lang' => 'it',
'title' => 'Questo è il titolo',
'description' => 'La descrizione',
],
'relationships' => [],
],
],
],
'en',
],
];
}

/**
* Test `replaceWithTranslation()` method.
*
* @param array $response The response data
* @param array $expected The expected data
* @param array $response The response data
* @param string $lang The lang requested
* @return void
* @covers ::testReplaceWithTranslation()
* @covers ::extractTranslatedFields()
* @dataProvider replaceWithTranslationProvider()
*/
public function testReplaceWithTranslation(array $response, array $expected, string $lang): void
public function testReplaceWithTranslation(array $expected, array $response, string $lang): void
{
$actual = $this->ApiFormatter->replaceWithTranslation($response, $lang);
static::assertEquals($expected, $actual);
Expand Down

0 comments on commit 2963b01

Please sign in to comment.