Skip to content

Commit 49035dd

Browse files
committed
Add tests for the entity translations feature
1 parent 2230687 commit 49035dd

6 files changed

Lines changed: 268 additions & 0 deletions

File tree

tests/Functional/Apps/DefaultApp/config/packages/framework.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
'enabled' => true,
2727
'collect' => false,
2828
],
29+
'translator' => [
30+
'default_path' => '%kernel.project_dir%/translations',
31+
],
2932
];
3033

3134
if (EasyCorp\Bundle\EasyAdminBundle\Tests\Functional\Apps\DefaultApp\Kernel::VERSION_ID < 60000) {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace EasyCorp\Bundle\EasyAdminBundle\Tests\Functional\Apps\DefaultApp\Controller\Dashboard;
4+
5+
use EasyCorp\Bundle\EasyAdminBundle\Attribute\AdminDashboard;
6+
use EasyCorp\Bundle\EasyAdminBundle\Config\Dashboard;
7+
use EasyCorp\Bundle\EasyAdminBundle\Config\MenuItem;
8+
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractDashboardController;
9+
use EasyCorp\Bundle\EasyAdminBundle\Tests\Functional\Apps\DefaultApp\Entity\Category;
10+
use Symfony\Component\HttpFoundation\Response;
11+
12+
/**
13+
* Dashboard controller for testing entity translations functionality.
14+
*/
15+
#[AdminDashboard(routePath: '/entity_translations_admin', routeName: 'entity_translations_admin')]
16+
class EntityTranslationsDashboardController extends AbstractDashboardController
17+
{
18+
public function index(): Response
19+
{
20+
return parent::index();
21+
}
22+
23+
public function configureDashboard(): Dashboard
24+
{
25+
return Dashboard::new()
26+
->setTitle('Entity Translations Test Dashboard')
27+
->setTranslationDomain('messages')
28+
->useEntityTranslations();
29+
}
30+
31+
public function configureMenuItems(): iterable
32+
{
33+
yield MenuItem::linktoDashboard('Dashboard', 'fa fa-home');
34+
// use null label to test that entity translations are used for menu items
35+
yield MenuItem::linkToCrud(null, 'fas fa-tags', Category::class);
36+
}
37+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
return [
4+
'entities' => [
5+
\EasyCorp\Bundle\EasyAdminBundle\Tests\Functional\Apps\DefaultApp\Entity\Category::class => [
6+
'singular' => 'Category (singular from file)',
7+
'plural' => 'Categories (plural from file)',
8+
'properties' => [
9+
'id' => 'ID (from file)',
10+
'name' => 'Name (from file)',
11+
'slug' => 'Slug (from file)',
12+
'active' => 'Active (from file)',
13+
'activeWithNoPermission' => 'Active No Permission (from file)',
14+
'activeDisabled' => 'Active Disabled (from file)',
15+
],
16+
],
17+
],
18+
];
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
namespace EasyCorp\Bundle\EasyAdminBundle\Tests\Functional\Default\Dashboard;
4+
5+
use EasyCorp\Bundle\EasyAdminBundle\Test\AbstractCrudTestCase;
6+
use EasyCorp\Bundle\EasyAdminBundle\Tests\Functional\Apps\DefaultApp\Controller\CategoryCrudController;
7+
use EasyCorp\Bundle\EasyAdminBundle\Tests\Functional\Apps\DefaultApp\Controller\Dashboard\EntityTranslationsDashboardController;
8+
9+
/**
10+
* Tests for entity translations functionality in the dashboard.
11+
*/
12+
class EntityTranslationsTest extends AbstractCrudTestCase
13+
{
14+
protected function getControllerFqcn(): string
15+
{
16+
return CategoryCrudController::class;
17+
}
18+
19+
protected function getDashboardFqcn(): string
20+
{
21+
return EntityTranslationsDashboardController::class;
22+
}
23+
24+
protected function setUp(): void
25+
{
26+
parent::setUp();
27+
$this->client->followRedirects();
28+
}
29+
30+
public function testFieldLabelsUseEntityTranslationKeys(): void
31+
{
32+
$crawler = $this->client->request('GET', $this->generateIndexUrl());
33+
34+
static::assertResponseIsSuccessful();
35+
36+
$html = $crawler->html();
37+
38+
// when entity translations are enabled, field labels should use translation keys
39+
// the translation file defines labels with "(from file)" suffix
40+
static::assertStringContainsString('Name (from file)', $html);
41+
static::assertStringContainsString('Slug (from file)', $html);
42+
static::assertStringContainsString('Active (from file)', $html);
43+
}
44+
45+
public function testTranslatedLabelsAppearedInTableHeaders(): void
46+
{
47+
$crawler = $this->client->request('GET', $this->generateIndexUrl());
48+
49+
static::assertResponseIsSuccessful();
50+
51+
// check that translated labels appear in table headers
52+
$tableHeaders = $crawler->filter('table.datagrid thead th');
53+
54+
$headerTexts = [];
55+
$tableHeaders->each(function ($node) use (&$headerTexts) {
56+
$headerTexts[] = trim($node->text());
57+
});
58+
59+
// the headers should contain our translated field names
60+
static::assertContains('Name (from file)', $headerTexts);
61+
static::assertContains('Slug (from file)', $headerTexts);
62+
}
63+
64+
public function testMenuItemUsesEntityPluralTranslation(): void
65+
{
66+
$crawler = $this->client->request('GET', $this->generateIndexUrl());
67+
68+
static::assertResponseIsSuccessful();
69+
70+
// when menu item label is null, it should use the entity's plural translation
71+
$menuItems = $crawler->filter('.menu-item a');
72+
$menuTexts = [];
73+
$menuItems->each(function ($node) use (&$menuTexts) {
74+
$menuTexts[] = trim($node->text());
75+
});
76+
77+
static::assertContains('Categories (plural from file)', $menuTexts);
78+
}
79+
80+
public function testPageTitleUsesEntityPluralTranslation(): void
81+
{
82+
$crawler = $this->client->request('GET', $this->generateIndexUrl());
83+
84+
static::assertResponseIsSuccessful();
85+
86+
// the index page title should use the entity's plural translation
87+
$pageTitle = $crawler->filter('.content-header-title h1.title')->text();
88+
89+
static::assertStringContainsString('Categories (plural from file)', $pageTitle);
90+
}
91+
92+
public function testAddActionButtonUsesEntitySingularTranslation(): void
93+
{
94+
$crawler = $this->client->request('GET', $this->generateIndexUrl());
95+
96+
static::assertResponseIsSuccessful();
97+
98+
// the "Add" action button should use the entity's singular translation
99+
$html = $crawler->html();
100+
101+
static::assertStringContainsString('Category (singular from file)', $html);
102+
}
103+
}

tests/Unit/Dto/DashboardDtoTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,51 @@ public function testGetLocalesFromList(): void
2222
$this->assertSame('pl', $locale2->getLocale());
2323
$this->assertSame('polski', $locale2->getName());
2424
}
25+
26+
public function testUseEntityTranslationsDefaultsToFalse(): void
27+
{
28+
$dashboard = Dashboard::new();
29+
$dashboardDto = $dashboard->getAsDto();
30+
31+
$this->assertFalse($dashboardDto->isUseEntityTranslations());
32+
}
33+
34+
public function testUseEntityTranslationsCanBeEnabled(): void
35+
{
36+
$dashboard = Dashboard::new();
37+
$dashboard->useEntityTranslations();
38+
39+
$dashboardDto = $dashboard->getAsDto();
40+
41+
$this->assertTrue($dashboardDto->isUseEntityTranslations());
42+
}
43+
44+
public function testUseEntityTranslationsCanBeExplicitlyEnabled(): void
45+
{
46+
$dashboard = Dashboard::new();
47+
$dashboard->useEntityTranslations(true);
48+
49+
$dashboardDto = $dashboard->getAsDto();
50+
51+
$this->assertTrue($dashboardDto->isUseEntityTranslations());
52+
}
53+
54+
public function testUseEntityTranslationsCanBeDisabled(): void
55+
{
56+
$dashboard = Dashboard::new();
57+
$dashboard->useEntityTranslations(true);
58+
$dashboard->useEntityTranslations(false);
59+
60+
$dashboardDto = $dashboard->getAsDto();
61+
62+
$this->assertFalse($dashboardDto->isUseEntityTranslations());
63+
}
64+
65+
public function testUseEntityTranslationsMethodIsFluent(): void
66+
{
67+
$dashboard = Dashboard::new();
68+
$result = $dashboard->useEntityTranslations();
69+
70+
$this->assertSame($dashboard, $result);
71+
}
2572
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace EasyCorp\Bundle\EasyAdminBundle\Tests\Unit\Translation;
6+
7+
use EasyCorp\Bundle\EasyAdminBundle\Translation\EntityTranslationIdGenerator;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class EntityTranslationIdGeneratorTest extends TestCase
11+
{
12+
private EntityTranslationIdGenerator $generator;
13+
14+
protected function setUp(): void
15+
{
16+
$this->generator = new EntityTranslationIdGenerator();
17+
}
18+
19+
public function testGenerateForEntitySingular(): void
20+
{
21+
$result = $this->generator->generateForEntity('App\Entity\BlogPost', true);
22+
23+
$this->assertSame('entities.App\Entity\BlogPost.singular', $result);
24+
}
25+
26+
public function testGenerateForEntityPlural(): void
27+
{
28+
$result = $this->generator->generateForEntity('App\Entity\BlogPost', false);
29+
30+
$this->assertSame('entities.App\Entity\BlogPost.plural', $result);
31+
}
32+
33+
public function testGenerateForProperty(): void
34+
{
35+
$result = $this->generator->generateForProperty('App\Entity\BlogPost', 'title');
36+
37+
$this->assertSame('entities.App\Entity\BlogPost.properties.title', $result);
38+
}
39+
40+
public function testGenerateForPropertyWithCamelCaseName(): void
41+
{
42+
$result = $this->generator->generateForProperty('App\Entity\BlogPost', 'publishedAt');
43+
44+
$this->assertSame('entities.App\Entity\BlogPost.properties.publishedAt', $result);
45+
}
46+
47+
public function testGenerateForEntityWithDeepNamespace(): void
48+
{
49+
$result = $this->generator->generateForEntity('App\Domain\Blog\Entity\Comment', true);
50+
51+
$this->assertSame('entities.App\Domain\Blog\Entity\Comment.singular', $result);
52+
}
53+
54+
public function testGenerateForPropertyWithDeepNamespace(): void
55+
{
56+
$result = $this->generator->generateForProperty('App\Domain\Blog\Entity\Comment', 'content');
57+
58+
$this->assertSame('entities.App\Domain\Blog\Entity\Comment.properties.content', $result);
59+
}
60+
}

0 commit comments

Comments
 (0)