Skip to content

Commit

Permalink
Forms destination: Use class name for config to avoid conflicts (#17667)
Browse files Browse the repository at this point in the history
* Forms destination: Use class name for config to avoid conflicts

* Slugify class name to avoid breaking html (+ more readable)

* Update existing tests
  • Loading branch information
AdrienClairembault authored Aug 19, 2024
1 parent 18af00c commit 0258854
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,16 @@ private function sendFormAndAssertTicketType(
array $answers,
int $expected_request_type,
): void {
$field = new RequestTypeField();

// Insert config
$destinations = $form->getDestinations();
$this->assertCount(1, $destinations);
$destination = current($destinations);
$this->updateItem(
$destination::getType(),
$destination->getId(),
['config' => ['request_type' => $config->jsonSerialize()]],
['config' => [$field->getKey() => $config->jsonSerialize()]],
["config"],
);

Expand Down
7 changes: 7 additions & 0 deletions src/Glpi/Form/Destination/AbstractConfigField.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,16 @@
use Glpi\Form\Form;
use LogicException;
use Override;
use Toolbox;

abstract class AbstractConfigField implements ConfigFieldInterface
{
#[Override]
public function getKey(): string
{
return Toolbox::slugify(static::class);
}

#[Override]
public function supportAutoConfiguration(): bool
{
Expand Down
6 changes: 0 additions & 6 deletions src/Glpi/Form/Destination/CommonITILField/ContentField.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,6 @@

class ContentField extends AbstractConfigField
{
#[Override]
public function getKey(): string
{
return 'content';
}

#[Override]
public function getLabel(): string
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,6 @@

class RequestTypeField extends AbstractConfigField
{
#[Override]
public function getKey(): string
{
return 'request_type';
}

#[Override]
public function getLabel(): string
{
Expand Down
6 changes: 0 additions & 6 deletions src/Glpi/Form/Destination/CommonITILField/TitleField.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,6 @@

class TitleField extends AbstractConfigField
{
#[Override]
public function getKey(): string
{
return 'title';
}

#[Override]
public function getLabel(): string
{
Expand Down
13 changes: 9 additions & 4 deletions tests/src/Form/Destination/AbstractFormDestinationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
use DbTestCase;
use Glpi\Form\AnswersHandler\AnswersHandler;
use Glpi\Form\AnswersSet;
use Glpi\Form\Destination\CommonITILField\ContentField;
use Glpi\Form\Destination\CommonITILField\SimpleValueConfig;
use Glpi\Form\Destination\CommonITILField\TitleField;
use Glpi\Form\Form;
use Glpi\Form\QuestionType\QuestionTypeShortText;
use Glpi\Tests\FormBuilder;
Expand All @@ -65,6 +67,9 @@ final public function testCreateDestinations(): void
$itemtype = $this->getTestedInstance()::getTargetItemtype();
$link_itemtype = $itemtype::getItemLinkClass();

$title_field = new TitleField();
$content_field = new ContentField();

// Create a form with a single FormDestinationTicket destination
$form = $this->createForm(
(new FormBuilder("Test form 1"))
Expand All @@ -73,10 +78,10 @@ final public function testCreateDestinations(): void
$this->getTestedInstance()::class,
'test',
[
'title' => (new SimpleValueConfig("My title"))->jsonSerialize(),
'title_auto' => 0,
'content' => (new SimpleValueConfig("My content"))->jsonSerialize(),
'content_auto' => 0,
$title_field->getKey() => (new SimpleValueConfig("My title"))->jsonSerialize(),
$title_field->getAutoConfigKey() => 0,
$content_field->getKey() => (new SimpleValueConfig("My content"))->jsonSerialize(),
$content_field->getAutoConfigKey() => 0,
]
)
);
Expand Down

0 comments on commit 0258854

Please sign in to comment.