-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1 parent
c588a3d
commit 09a7631
Showing
7 changed files
with
293 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace App\Rules; | ||
|
||
use App\Models\Language; | ||
use Closure; | ||
use Illuminate\Contracts\Validation\ValidationRule; | ||
|
||
class TranslationTextRule implements ValidationRule | ||
{ | ||
public function validate(string $attribute, mixed $value, Closure $fail): void | ||
{ | ||
$allLanguageCodes = Language::pluck('code')->toArray(); | ||
|
||
$clientLangCodes = array_keys($value); | ||
|
||
$isOk = empty(array_diff($allLanguageCodes, $clientLangCodes)); | ||
|
||
if (!$isOk) { | ||
$fail('Missing languageCode in the object. Required langCodes: ' . implode(', ', $allLanguageCodes)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,208 @@ | ||
<?php | ||
|
||
namespace Tests\Feature; | ||
|
||
use App\Models\Language; | ||
use App\Models\Translation; | ||
use App\Models\TranslationGroup; | ||
use Tests\TestCase; | ||
|
||
class TranslationControllerTest extends TestCase | ||
{ | ||
public function testIndexReturnsAllTranslationGroups() | ||
{ | ||
$baseGroup = TranslationGroup::factory()->create([ | ||
'key' => 'base', | ||
'name' => 'Base Group', | ||
]); | ||
$loveTranslation = Translation::factory()->create([ | ||
'translation_group_id' => $baseGroup->uuid, | ||
'key' => 'base.love', | ||
'name' => 'Love', | ||
'text' => [ | ||
'en' => 'Love', | ||
'vi' => 'Yêu', | ||
], | ||
]); | ||
$youTranslation = Translation::factory()->create([ | ||
'translation_group_id' => $baseGroup->uuid, | ||
'key' => 'base.you', | ||
'name' => 'You', | ||
'text' => [ | ||
'en' => 'You', | ||
'vi' => 'Bạn', | ||
], | ||
]); | ||
|
||
$response = $this->get('api/v1/translations'); | ||
|
||
$response->assertStatus(200) | ||
->assertJsonFragment([ | ||
'key' => 'base.love', | ||
'name' => 'Love', | ||
'text' => [ | ||
'en' => 'Love', | ||
'vi' => 'Yêu', | ||
], | ||
]) | ||
->assertJsonFragment([ | ||
'key' => 'base.you', | ||
'name' => 'You', | ||
'text' => [ | ||
'en' => 'You', | ||
'vi' => 'Bạn', | ||
], | ||
]) | ||
->assertJsonFragment([ | ||
'key' => 'base', | ||
'name' => 'Base Group', | ||
]); | ||
} | ||
|
||
public function testShowReturnsAGivenTranslation() | ||
{ | ||
$youTranslation = Translation::factory()->create([ | ||
'key' => 'base.you', | ||
'name' => 'You', | ||
'text' => [ | ||
'en' => 'You', | ||
'vi' => 'Bạn', | ||
], | ||
]); | ||
|
||
$response = $this->get("api/v1/translations/{$youTranslation->uuid}"); | ||
|
||
$response->assertStatus(200) | ||
->assertJsonFragment([ | ||
'key' => 'base.you', | ||
'name' => 'You', | ||
'text' => [ | ||
'en' => 'You', | ||
'vi' => 'Bạn', | ||
], | ||
]); | ||
} | ||
|
||
public function testStoreCreatesANewTranslation() | ||
{ | ||
$pricingGroup = TranslationGroup::factory()->create([ | ||
'key' => 'pricing', | ||
'name' => 'Pricing Group', | ||
]); | ||
$language = Language::factory()->create([ | ||
'code' => 'en', | ||
]); | ||
|
||
$this->json('POST', "api/v1/translations", [ | ||
'translation_group_id' => $pricingGroup->uuid, | ||
'key' => 'expensive', | ||
'name' => 'Expensive', | ||
'text' => [ | ||
'en' => 'Expensive (expensive)', | ||
], | ||
])->assertOk() | ||
->assertJsonFragment([ | ||
'created' => true, | ||
]); | ||
|
||
$this->assertDatabaseHas((new Translation())->getTable(), [ | ||
'key' => 'pricing.expensive', | ||
'name' => 'Expensive', | ||
'text->en' => 'Expensive (expensive)', | ||
]); | ||
} | ||
|
||
public function testStoreReturnsErrorOnInvalidTextFormat() | ||
{ | ||
$pricingGroup = TranslationGroup::factory()->create([ | ||
'key' => 'pricing', | ||
'name' => 'Pricing Group', | ||
]); | ||
$language = Language::factory()->create([ | ||
'code' => 'en', | ||
]); | ||
$language = Language::factory()->create([ | ||
'code' => 'vi', | ||
]); | ||
|
||
$this->json('POST', "api/v1/translations", [ | ||
'translation_group_id' => $pricingGroup->uuid, | ||
'key' => 'inexpensive', | ||
'name' => 'Inexpensive', | ||
'text' => [ | ||
'en' => 'Inexpensive', | ||
// missing vi | ||
], | ||
])->assertUnprocessable(); | ||
|
||
$this->json('POST', "api/v1/translations", [ | ||
'translation_group_id' => $pricingGroup->uuid, | ||
'key' => 'inexpensive', | ||
'name' => 'Inexpensive', | ||
'text' => [1, 2, 3], // wrong type | ||
])->assertUnprocessable(); | ||
|
||
} | ||
|
||
public function testUpdateUpdatesAGivenLanguage() | ||
{ | ||
$oldGroup = TranslationGroup::factory()->create([ | ||
'key' => 'old', | ||
'name' => 'Old Group', | ||
]); | ||
$language = Language::factory()->create([ | ||
'code' => 'en', | ||
]); | ||
$translation = Translation::factory()->create([ | ||
'translation_group_id' => $oldGroup->uuid, | ||
'key' => 'due-date', | ||
'text' => [ | ||
'en' => 'Due date', | ||
], | ||
]); | ||
|
||
$invoiceGroup = TranslationGroup::factory()->create([ | ||
'key' => 'invoice', | ||
'name' => 'Invoice Group', | ||
]); | ||
|
||
$response = $this->json('PUT', "api/v1/translations/{$translation->uuid}", [ | ||
'translation_group_id' => $invoiceGroup->uuid, | ||
'key' => 'due-date-v2', | ||
'name' => 'due date v2', | ||
'text' => [ | ||
'en' => 'Due date ok', | ||
], | ||
]); | ||
|
||
$response->assertOk() | ||
->assertJsonFragment([ | ||
'updated' => true, | ||
]); | ||
|
||
$this->assertDatabaseHas((new Translation())->getTable(), [ | ||
'translation_group_id' => $invoiceGroup->uuid, | ||
'key' => 'invoice.due-date-v2', | ||
'name' => 'due date v2', | ||
'text->en' => 'Due date ok', | ||
]); | ||
} | ||
|
||
public function testDestroyDeletesAGivenTranslation() | ||
{ | ||
$translation = Translation::factory()->create([ | ||
'key' => 'to-be-deleted', | ||
]); | ||
|
||
$response = $this->json('DELETE', "api/v1/translations/{$translation->uuid}"); | ||
|
||
$response->assertOk() | ||
->assertJsonFragment([ | ||
'deleted' => true, | ||
]); | ||
|
||
$this->assertDatabaseMissing((new TranslationGroup())->getTable(), [ | ||
'key' => 'to-be-deleted', | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace Tests\Unit\Models; | ||
|
||
use App\Models\Translation; | ||
use App\Models\TranslationGroup; | ||
use Tests\TestCase; | ||
|
||
class TranslationGroupTest extends TestCase | ||
{ | ||
public function testTranslationGroupHasManyTranslations() | ||
{ | ||
/** | ||
* @var TranslationGroup $template | ||
*/ | ||
$group = TranslationGroup::factory()->create(); | ||
$translations = Translation::factory()->count(2) | ||
->create([ | ||
'translation_group_id' => $group->uuid, | ||
]); | ||
|
||
$foundTranslations = $group->translations()->get(); | ||
|
||
$this->assertSame( | ||
$translations->pluck('uuid')->toArray(), | ||
$foundTranslations->pluck('uuid')->toArray() | ||
); | ||
} | ||
} |