Skip to content

Commit

Permalink
Translated UI test suite (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
loic425 authored Oct 8, 2024
2 parents 55a09a0 + 805a9f7 commit 5b7843c
Show file tree
Hide file tree
Showing 13 changed files with 314 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
# Run "composer dump-env prod" to compile .env files for production use (requires symfony/flex >=1.2).
# https://symfony.com/doc/current/best_practices.html#use-environment-variables-for-infrastructure-configuration

APP_LOCALE=en

###> symfony/framework-bundle ###
APP_ENV=dev
APP_SECRET=28de35e787e86cb435f0312627623283
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ jobs:
- name: Run PHPUnit
run: vendor/bin/phpunit

- name: Run Translations Test Suite with PHPUnit
run: |
APP_LOCALE=fr vendor/bin/phpunit --testsuite="TranslationsTestSuite"
- name: "Restrict packages' versions (Admin Ui)"
run: |
(cd src/AdminUi/ && composer global config --no-plugins allow-plugins.symfony/flex true)
Expand Down
4 changes: 2 additions & 2 deletions app/Grid/BookGrid.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ public function buildGrid(GridBuilderInterface $gridBuilder): void
)
->addField(
StringField::create('title')
->setLabel('Title')
->setLabel('app.ui.title')
->setSortable(true),
)
->addField(
StringField::create('authorName')
->setLabel('Author Name')
->setLabel('app.ui.author_name')
->setSortable(true),
)
->addActionGroup(
Expand Down
7 changes: 7 additions & 0 deletions config/packages/translation.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
framework:
default_locale: '%env(APP_LOCALE)%'
translator:
default_path: '%kernel.project_dir%/translations'
fallbacks:
- en
providers:
2 changes: 1 addition & 1 deletion config/services.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
locale: en_US
locale: '%kernel.default_locale%'

services:
# Default configuration for services in *this* file
Expand Down
14 changes: 9 additions & 5 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,30 @@
</php>

<testsuites>
<testsuite name="Main Test Suite">
<testsuite name="MainTestSuite">
<directory>tests</directory>
</testsuite>

<testsuite name="AdminUi Test Suite">
<testsuite name="TranslationsTestSuite">
<directory>tests/Translations</directory>
</testsuite>

<testsuite name="AdminUiTestSuite">
<directory>src/AdminUi/tests</directory>
<exclude>src/AdminUi/tests/Functional</exclude>
</testsuite>

<testsuite name="TwigExtra Test Suite">
<testsuite name="TwigExtraTestSuite">
<directory>src/TwigExtra/tests</directory>
<exclude>src/TwigExtra/tests/Functional</exclude>
</testsuite>

<testsuite name="TwigHooks Test Suite">
<testsuite name="TwigHooksTestSuite">
<directory>src/TwigHooks/tests</directory>
<exclude>src/TwigHooks/tests/Functional</exclude>
</testsuite>

<testsuite name="UiTranslations Test Suite">
<testsuite name="UiTranslationsTestSuite">
<directory>src/UiTranslations/tests</directory>
<exclude>src/UiTranslations/tests/Functional</exclude>
</testsuite>
Expand Down
2 changes: 1 addition & 1 deletion src/UiTranslations/translations/messages.en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ sylius:
pagination:
number_of_results: 'Showing %from% to %to% of %total% entries'
reset: Reset
show: Show
search: Search
search_menu: 'Search menu'
show: Show
success: Success
this_form_contains_errors: 'This form contains errors.'
to: To
Expand Down
46 changes: 46 additions & 0 deletions src/UiTranslations/translations/messages.fr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
sylius:
form:
login:
username: 'Identifiant'
password: 'Mot de passe'
remember_me: 'Rester connecté'
ui:
actions: 'Actions'
add_new_entry: 'Nouvel item'
administration_panel_login: 'Connexion à l''espace d''administration'
all: 'Tous'
are_your_sure_you_want_to_perform_this_action: 'Êtes-vous sûr de vouloir effectuer cette action ?'
cancel: 'Annuler'
contains: 'Contient'
create: 'Créer'
dashboard: 'Tableau de bord'
date_filter: '%label% | %altLabel%'
delete: 'Supprimer'
details: 'Détails'
edit: 'Modifier'
error: 'Erreur'
filter: 'Filtrer'
filters: 'Filtres'
from: 'De'
hello: 'Bonjour'
info: 'Info'
login: 'Connexion'
login_to_your_account: 'Connection à votre compte'
logout: 'Déconnexion'
new: 'Nouveau'
no_label: 'Non'
no_results: 'Aucun résultat'
no_results_adjust_your_search: 'Modifie ta recherche et réessaie'
pagination:
number_of_results: 'Affiche de %from à %to sur %total% items'
reset: 'Réinitialiser'
search: 'Rechercher'
search_menu: 'Recherche menu'
show: 'Afficher'
success: 'Succès'
this_form_contains_errors: 'Ce formulaire contient des erreurs.'
to: 'À'
update: 'Mise à jour'
value: 'Valeur'
warning: 'Avertissement'
yes_label: 'Oui'
2 changes: 1 addition & 1 deletion tests/Functional/BookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function testBrowsingBooks(): void

// Validate Table header
self::assertSelectorTextContains('.sylius-table-column-title', 'Title');
self::assertSelectorTextContains('.sylius-table-column-authorName', 'Author Name');
self::assertSelectorTextContains('.sylius-table-column-authorName', 'Author name');
self::assertSelectorTextContains('.sylius-table-column-actions', 'Actions');

// Validate Table data
Expand Down
193 changes: 193 additions & 0 deletions tests/Translations/FrenchTranslatedUiTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
<?php

declare(strict_types=1);

namespace MainTests\Sylius\Translations;

use App\Entity\Book;
use App\Factory\BookFactory;
use App\Factory\UserFactory;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpFoundation\Response;
use Zenstruck\Foundry\Persistence\Proxy;
use Zenstruck\Foundry\Test\Factories;
use Zenstruck\Foundry\Test\ResetDatabase;

final class FrenchTranslatedUiTest extends WebTestCase
{
Use Factories;
use ResetDatabase;
use MarkTestSkippedTrait;

private KernelBrowser $client;

protected function setUp(): void
{
$this->client = static::createClient();

$user = UserFactory::new()
->admin()
->create()
;

$this->client->loginUser($user->_real());
}

public function testShowItem(): void
{
$this->markTestSkippedIfNecessary('fr');

$book = BookFactory::new()
->withTitle('Shinning')
->withAuthorName('Stephen King')
->create()
;

$this->client->request('GET', sprintf('/admin/books/%s', $book->getId()));

self::assertResponseIsSuccessful();

// Validate Header
self::assertSelectorTextContains('h1.page-title', 'Afficher Livre');
}

public function testBrowsingItems(): void
{
$this->markTestSkippedIfNecessary('fr');

BookFactory::new()
->withTitle('Shinning')
->withAuthorName('Stephen King')
->create()
;

BookFactory::new()
->withTitle('Carrie')
->withAuthorName('Stephen King')
->create()
;

$this->client->request('GET', '/admin/books');

self::assertResponseIsSuccessful();

// Validate Header
self::assertSelectorTextContains('h1.page-title', 'Livres');

// Validate Table header
self::assertSelectorTextContains('.sylius-table-column-title', 'Titre');
self::assertSelectorTextContains('.sylius-table-column-authorName', 'Auteur');
self::assertSelectorTextContains('.sylius-table-column-actions', 'Actions');
}

public function testAddingNewItem(): void
{
$this->markTestSkippedIfNecessary('fr');

$this->client->request('GET', '/admin/books/new');

$this->client->submitForm('Créer', [
'sylius_resource[title]' => 'Shinning',
'sylius_resource[authorName]' => 'Stephen King',
]);

self::assertResponseRedirects(expectedCode: Response::HTTP_FOUND);

$this->client->request('GET', '/admin/books');

// Test flash message
self::assertSelectorTextContains('[data-test-sylius-flash-message]', 'Book a bien été créé.');
}

public function testValidationErrorsWhenAddingNewItem(): void
{
$this->markTestSkippedIfNecessary('fr');

$this->client->request('GET', '/admin/books/new');
$this->client->submitForm('Créer', [
'sylius_resource[title]' => null,
'sylius_resource[authorName]' => null,
]);

self::assertResponseStatusCodeSame(Response::HTTP_UNPROCESSABLE_ENTITY);
self::assertSelectorTextContains('[data-test-form-error-alert] .alert-title', 'Erreur');
self::assertSelectorTextContains('[data-test-form-error-alert] .text-secondary', 'Ce formulaire contient des erreurs.');
self::assertSelectorTextContains('#sylius_resource_title + .invalid-feedback', 'Cette valeur ne doit pas être vide.');
self::assertSelectorTextContains('#sylius_resource_authorName + .invalid-feedback', 'Cette valeur ne doit pas être vide.');
}

public function testEditingItem(): void
{
$this->markTestSkippedIfNecessary('fr');

$book = BookFactory::new()
->withTitle('Shinning')
->withAuthorName('Stephen King')
->create();

$this->client->request('GET', sprintf('/admin/books/%s/edit', $book->getId()));

$this->client->submitForm('Mise à jour', [
'sylius_resource[title]' => 'Carrie',
'sylius_resource[authorName]' => 'Stephen King',
]);

self::assertResponseRedirects(expectedCode: Response::HTTP_FOUND);

$this->client->request('GET', '/admin/books');

// Test flash message
self::assertSelectorTextContains('[data-test-sylius-flash-message]', 'Book a bien été mis à jour.');

/** @var Proxy<Book> $book */
$book = BookFactory::find(['title' => 'Carrie']);

self::assertSame('Carrie', $book->getTitle());
self::assertSame('Stephen King', $book->getAuthorName());
}

public function testValidationErrorsWhenEditingItem(): void
{
$this->markTestSkippedIfNecessary('fr');

$book = BookFactory::new()
->withTitle('Shinning')
->withAuthorName('Stephen King')
->create();

$this->client->request('GET', sprintf('/admin/books/%s/edit', $book->getId()));
$this->client->submitForm('Mise à jour', [
'sylius_resource[title]' => null,
'sylius_resource[authorName]' => null,
]);

self::assertResponseStatusCodeSame(Response::HTTP_UNPROCESSABLE_ENTITY);
self::assertSelectorTextContains('[data-test-form-error-alert] .alert-title', 'Erreur');
self::assertSelectorTextContains('[data-test-form-error-alert] .text-secondary', 'Ce formulaire contient des erreurs.');
self::assertSelectorTextContains('#sylius_resource_title + .invalid-feedback', 'Cette valeur ne doit pas être vide.');
self::assertSelectorTextContains('#sylius_resource_authorName + .invalid-feedback', 'Cette valeur ne doit pas être vide.');
}

public function testRemovingItem(): void
{
$this->markTestSkippedIfNecessary('fr');

BookFactory::new()
->withTitle('Shinning')
->withAuthorName('Stephen King')
->create();

$this->client->request('GET', '/admin/books');
$deleteButton = $this->client->getCrawler()->filter('tr.item:first-child [data-test-confirm-button]');

$this->client->submit($deleteButton->form());

self::assertResponseRedirects();

$this->client->request('GET', '/admin/books');

// Test flash message
self::assertSelectorTextContains('[data-test-sylius-flash-message]', 'Book a bien été supprimé.');
}
}
18 changes: 18 additions & 0 deletions tests/Translations/MarkTestSkippedTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace MainTests\Sylius\Translations;

use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

/**
* @mixin KernelTestCase
*/
trait MarkTestSkippedTrait
{
private function markTestSkippedIfNecessary(string $locale): void
{
if ($this->getContainer()->getParameter('kernel.default_locale') !== $locale) {
$this->markTestSkipped();
}
}
}
1 change: 1 addition & 0 deletions translations/messages.en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ app:
ui:
archival: Archival
author: Author
author_name: Author name
avatar: Avatar
biz: Biz
book: Book
Expand Down
Loading

0 comments on commit 5b7843c

Please sign in to comment.