Skip to content

Commit

Permalink
Add more tests for form field help messages
Browse files Browse the repository at this point in the history
  • Loading branch information
javiereguiluz committed Oct 29, 2023
1 parent 7fdd14d commit 4797a90
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 10 deletions.
29 changes: 24 additions & 5 deletions tests/Controller/FormFieldHelpControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,35 @@ public function testFieldsHelpMessages()
{
$crawler = $this->client->request('GET', $this->generateNewFormUrl());

// fields with no help defined
static::assertSelectorNotExists('#tab-tab-1 .tab-help', 'The Tab 1 does not define a help message.');
static::assertSelectorNotExists('.form-column.column-1 .form-column-help', 'The Column 1 does not define a help message.');
static::assertSelectorNotExists('.form-fieldset-title:contains("Fieldset 1") .form-fieldset-help', 'The Fieldset 1 does not define a help message.');
static::assertSelectorNotExists('.form-group #BlogPost_id + .form-help', 'The ID field does not define a help message.');

static::assertSelectorNotExists('.form-group #BlogPost_title + .form-help', 'The title field defines an empty string as a help message, so it does not render an HTML element for that help message.');

static::assertSelectorTextContains('.form-group #BlogPost_slug + .form-help', 'Lorem Ipsum 1', 'The slug field defines a text help message.');
// fields with help defined as simple text strings
static::assertSelectorTextContains('#tab-tab-2 .tab-help', 'Tab 2 Lorem Ipsum', 'The Tab 2 field defines a text help message.');
static::assertSelectorTextContains('.form-column.column-2 .form-column-help', 'Column 2 Lorem Ipsum', 'The Column 2 field defines a text help message.');
static::assertSelectorTextContains('.form-fieldset-title:contains("Fieldset 2") .form-fieldset-help', 'Fieldset 2 Lorem Ipsum', 'The Fieldset 2 field defines a text help message.');
static::assertSelectorTextContains('.form-group #BlogPost_slug + .form-help', 'Slug Lorem Ipsum', 'The slug field defines a text help message.');

static::assertSame('<b>Lorem</b> Ipsum <em class="foo">2</em>', $crawler->filter('.form-group #BlogPost_content + .form-help')->html(), 'The content field defines an help message with HTML contents, which must be rendered instead of escaped.');
// fields with help defined as text strings with HTML contents
static::assertSame('<a href="https://example.com">Tab 3</a> <b>Lorem</b> Ipsum', trim($crawler->filter('#tab-tab-3 .tab-help')->html()), 'The Tab 3 field defines a help message with HTML contents, which must be rendered instead of escaped.');
static::assertSame('<a href="https://example.com">Column 3</a> <b>Lorem</b> Ipsum', trim($crawler->filter('.form-column.column-3 .form-column-help')->html()), 'The Column 3 field defines a help message with HTML contents, which must be rendered instead of escaped.');
static::assertSame('<a href="https://example.com">Fieldset 3</a> <b>Lorem</b> Ipsum', trim($crawler->filter('.form-fieldset-title:contains("Fieldset 3") .form-fieldset-help')->html()), 'The Fieldset 3 field defines a help message with HTML contents, which must be rendered instead of escaped.');
static::assertSame('<a href="https://example.com">Content</a> <b>Lorem</b> Ipsum', $crawler->filter('.form-group #BlogPost_content + .form-help')->html(), 'The content field defines an help message with HTML contents, which must be rendered instead of escaped.');

static::assertSelectorTextContains('.form-group:contains("Created At") .form-help', 'Lorem Ipsum 3', 'The createdAt field defines a translatable text help message.');
// fields with help defined as Translatable objects using simple text strings
static::assertSelectorTextContains('#tab-tab-4 .tab-help', 'Tab 4 Lorem Ipsum', 'The Tab 4 field defines a translatable text help message.');
static::assertSelectorTextContains('.form-column.column-4 .form-column-help', 'Column 4 Lorem Ipsum', 'The Column 4 field defines a translatable text help message.');
static::assertSelectorTextContains('.form-fieldset-title:contains("Fieldset 4") .form-fieldset-help', 'Fieldset 4 Lorem Ipsum', 'The Fieldset 4 field defines a translatable text help message.');
static::assertSelectorTextContains('.form-group:contains("Created At") .form-help', 'CreatedAt Lorem Ipsum', 'The createdAt field defines a translatable text help message.');

static::assertSame('Lorem <a href="https://example.com">Ipsum</a> <b>4</b>', $crawler->filter('.form-group:contains("Published At") .form-help')->html(), 'The publishedAt field defines a translatable help message with HTML contents, which must be rendered instead of escaped.');
// fields with help defined as Translatable objects using text strings with HTML contents
static::assertSelectorTextContains('#tab-tab-5 .tab-help', 'Tab 5 Lorem Ipsum', 'The Tab 5 field defines a translatable help message with HTML contents, which must be rendered instead of escaped.');
static::assertSelectorTextContains('.form-column.column-5 .form-column-help', 'Column 5 Lorem Ipsum', 'The Column 5 field defines a translatable help message with HTML contents, which must be rendered instead of escaped..');
static::assertSelectorTextContains('.form-fieldset-title:contains("Fieldset 5") .form-fieldset-help', 'Fieldset 5 Lorem Ipsum', 'The Fieldset 5 field defines a translatable help message with HTML contents, which must be rendered instead of escaped..');
static::assertSame('<a href="https://example.com">PublishedAt</a> <b>Lorem</b> Ipsum', $crawler->filter('.form-group:contains("Published At") .form-help')->html(), 'The publishedAt field defines a translatable help message with HTML contents, which must be rendered instead of escaped.');
}
}
35 changes: 30 additions & 5 deletions tests/TestApplication/src/Controller/FormFieldHelpController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;
use EasyCorp\Bundle\EasyAdminBundle\Field\FormField;
use EasyCorp\Bundle\EasyAdminBundle\Field\IdField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
use EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Entity\BlogPost;
Expand All @@ -23,12 +24,36 @@ public static function getEntityFqcn(): string
public function configureFields(string $pageName): iterable
{
return [
IdField::new('id'), // this field doesn't define a help message on purpose
// fields with no help defined
FormField::addTab('Tab 1'),
FormField::addColumn()->addCssClass('column-1'),
FormField::addFieldset('Fieldset 1'),
IdField::new('id'),
TextField::new('title')->setHelp(''),
TextField::new('slug')->setHelp('Lorem Ipsum 1'),
TextField::new('content')->setHelp('<b>Lorem</b> Ipsum <em class="foo">2</em>'),
DateTimeField::new('createdAt')->setHelp(t('Lorem Ipsum 3')),
DateTimeField::new('publishedAt')->setHelp(t('Lorem <a href="https://example.com">Ipsum</a> <b>4</b>')),

// fields with help defined as simple text strings
FormField::addTab('Tab 2')->setHelp('Tab 2 Lorem Ipsum'),
FormField::addColumn()->addCssClass('column-2')->setHelp('Column 2 Lorem Ipsum'),
FormField::addFieldset('Fieldset 2')->setHelp('Fieldset 2 Lorem Ipsum'),
TextField::new('slug')->setHelp('Slug Lorem Ipsum'),

// fields with help defined as text strings with HTML contents
FormField::addTab('Tab 3')->setHelp('<a href="https://example.com">Tab 3</a> <b>Lorem</b> Ipsum'),
FormField::addColumn()->addCssClass('column-3')->setHelp('<a href="https://example.com">Column 3</a> <b>Lorem</b> Ipsum'),
FormField::addFieldset('Fieldset 3')->setHelp('<a href="https://example.com">Fieldset 3</a> <b>Lorem</b> Ipsum'),
TextField::new('content')->setHelp('<a href="https://example.com">Content</a> <b>Lorem</b> Ipsum'),

// fields with help defined as Translatable objects using simple text strings
FormField::addTab('Tab 4')->setHelp(t('Tab 4 Lorem Ipsum')),
FormField::addColumn()->addCssClass('column-4')->setHelp(t('Column 4 Lorem Ipsum')),
FormField::addFieldset('Fieldset 4')->setHelp(t('Fieldset 4 Lorem Ipsum')),
DateTimeField::new('createdAt')->setHelp(t('CreatedAt Lorem Ipsum')),

// fields with help defined as Translatable objects using text strings with HTML contents
FormField::addTab('Tab 5')->setHelp(t('<a href="https://example.com">Tab 5</a> <b>Lorem</b> Ipsum')),
FormField::addColumn()->addCssClass('column-5')->setHelp(t('<a href="https://example.com">Column 5</a> <b>Lorem</b> Ipsum')),
FormField::addFieldset('Fieldset 5')->setHelp(t('<a href="https://example.com">Fieldset 5</a> <b>Lorem</b> Ipsum')),
DateTimeField::new('publishedAt')->setHelp(t('<a href="https://example.com">PublishedAt</a> <b>Lorem</b> Ipsum')),
];
}
}

0 comments on commit 4797a90

Please sign in to comment.