Skip to content

Commit 18c9430

Browse files
committed
refactor(form): getByItem method
1 parent 0931764 commit 18c9430

3 files changed

Lines changed: 88 additions & 57 deletions

File tree

inc/form.class.php

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2103,35 +2103,32 @@ public static function getByItem(?CommonDBTM $item): ?self {
21032103

21042104
$form = PluginFormcreatorCommon::getForm();
21052105
$formFk = self::getForeignKeyField();
2106-
switch ($item::getType()) {
2107-
case PluginFormcreatorSection::getType():
2108-
if (!isset($item->fields[$formFk])) {
2109-
return null;
2110-
}
2111-
$form->getFromDB($item->fields[$formFk]);
2112-
break;
2113-
2114-
case PluginFormcreatorQuestion::getType():
2115-
$sectionFk = PluginFormcreatorSection::getForeignKeyField();
2116-
if (!isset($item->fields[$sectionFk])) {
2117-
return null;
2118-
}
2119-
$iterator = $DB->request([
2120-
'SELECT' => self::getForeignKeyField(),
2121-
'FROM' => PluginFormcreatorSection::getTable(),
2122-
'WHERE' => [
2123-
'id' => $item->fields[$sectionFk],
2124-
]
2125-
]);
2126-
if ($iterator->count() !== 1) {
2127-
return null;
2128-
}
2129-
$form->getFromDB($iterator->current()[$formFk]);
2130-
break;
2131-
}
21322106

2133-
if ($item instanceof PluginFormcreatorTargetInterface) {
2107+
if ($DB->fieldExists($item::getTable(), $formFk)) {
2108+
if (!isset($item->fields[$formFk])) {
2109+
return null;
2110+
}
21342111
$form->getFromDB($item->fields[$formFk]);
2112+
} else {
2113+
switch ($item::getType()) {
2114+
case PluginFormcreatorQuestion::getType():
2115+
$sectionFk = PluginFormcreatorSection::getForeignKeyField();
2116+
if (!isset($item->fields[$sectionFk])) {
2117+
return null;
2118+
}
2119+
$iterator = $DB->request([
2120+
'SELECT' => self::getForeignKeyField(),
2121+
'FROM' => PluginFormcreatorSection::getTable(),
2122+
'WHERE' => [
2123+
'id' => $item->fields[$sectionFk],
2124+
]
2125+
]);
2126+
if ($iterator->count() !== 1) {
2127+
return null;
2128+
}
2129+
$form->getFromDB($iterator->current()[$formFk]);
2130+
break;
2131+
}
21352132
}
21362133

21372134
if ($form->isNewItem()) {

tests/3-unit/PluginFormcreatorForm.php

Lines changed: 61 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@
2929
* ---------------------------------------------------------------------
3030
*/
3131
namespace tests\units;
32+
use Config;
3233
use GlpiPlugin\Formcreator\Tests\CommonTestCase;
34+
use PluginFormcreatorSection;
35+
use PluginFormcreatorQuestion;
36+
use PluginFormcreatorForm_Language;
3337

3438
class PluginFormcreatorForm extends CommonTestCase {
3539

@@ -49,7 +53,7 @@ public function beforeTestMethod($method) {
4953

5054
switch ($method) {
5155
case 'testCreateValidationNotification':
52-
\Config::setConfigurationValues(
56+
Config::setConfigurationValues(
5357
'core',
5458
['use_notifications' => 1, 'notifications_mailing' => 1]
5559
);
@@ -60,7 +64,7 @@ public function afterTestMethod($method) {
6064
parent::afterTestMethod($method);
6165
switch ($method) {
6266
case 'testCreateValidationNotification':
63-
\Config::setConfigurationValues(
67+
Config::setConfigurationValues(
6468
'core',
6569
['use_notifications' => 0, 'notifications_mailing' => 0]
6670
);
@@ -626,32 +630,61 @@ public function testIsPublicAcess($input, $expected) {
626630
}
627631

628632
public function providerGetByItem() {
633+
$testedClassName = $this->getTestedClassName();
634+
$formFk = $testedClassName::getForeignKeyField();
629635
$section = $this->getSection();
636+
637+
$expected_id = $section->fields['plugin_formcreator_forms_id'];
638+
yield [
639+
'item' => $section,
640+
'expectedType' => $testedClassName,
641+
'expected' => $expected_id,
642+
];
643+
644+
yield [
645+
'item' => new PluginFormcreatorSection(),
646+
'expectedType' => $testedClassName,
647+
'expected' => false,
648+
];
649+
630650
$question = $this->getQuestion();
651+
$section = PluginFormcreatorSection::getById($question->fields['plugin_formcreator_sections_id']);
652+
$expected_id = $section->fields['plugin_formcreator_forms_id'];
653+
yield [
654+
'item' => $question,
655+
'expectedType' => $testedClassName,
656+
'expected' => $expected_id,
657+
];
631658

632-
$dataset = [
633-
[
634-
'item' => $section,
635-
'expectedType' => \PluginFormcreatorForm::getType(),
636-
'expected' => true,
637-
],
638-
[
639-
'item' => new \PluginFormcreatorSection(),
640-
'expectedType' => \PluginFormcreatorForm::getType(),
641-
'expected' => false,
642-
],
643-
[
644-
'item' => $question,
645-
'expectedType' => \PluginFormcreatorForm::getType(),
646-
'expected' => true,
647-
],
648-
[
649-
'item' => new \PluginFormcreatorQuestion(),
650-
'expectedType' => \PluginFormcreatorForm::getType(),
651-
'expected' => false,
652-
],
659+
yield [
660+
'item' => new PluginFormcreatorQuestion(),
661+
'expectedType' => $testedClassName,
662+
'expected' => false,
663+
];
664+
665+
$form = $this->getForm();
666+
$formAnswer = $this->getFormAnswer([
667+
$formFk => $form->getID(),
668+
]);
669+
670+
yield [
671+
'item' => $formAnswer,
672+
'expectedType' => $testedClassName,
673+
'expected' => $formAnswer->fields[$formFk],
674+
];
675+
676+
$form_language = new PluginFormcreatorForm_Language();
677+
$form_language->add([
678+
$formFk => $form->getID(),
679+
'name' => 'fr_FR',
680+
]);
681+
$this->boolean($form_language->isNewItem())->isFalse();
682+
683+
yield [
684+
'item' => $form_language,
685+
'expectedType' => $testedClassName,
686+
'expected' => $form_language->fields[$formFk],
653687
];
654-
return $dataset;
655688
}
656689

657690
/**
@@ -898,9 +931,9 @@ public function testDuplicate() {
898931
$this->string($new_form->getField('uuid'))->isNotEqualTo($form->getField('uuid'));
899932

900933
// check sections
901-
$all_sections = (new \PluginFormcreatorSection())->getSectionsFromForm($form->getID());
934+
$all_sections = (new PluginFormcreatorSection())->getSectionsFromForm($form->getID());
902935
$this->integer(count($all_sections))->isEqualTo(count($section_ids));
903-
$all_new_sections = (new \PluginFormcreatorSection())->getSectionsFromForm($new_form->getID());
936+
$all_new_sections = (new PluginFormcreatorSection())->getSectionsFromForm($new_form->getID());
904937
$this->integer(count($all_sections))->isEqualTo(count($section_ids));
905938

906939
// check that all sections uuid are new
@@ -1349,14 +1382,14 @@ public function testAdd() {
13491382
]);
13501383

13511384
$this->boolean($instance->isNewItem())->isFalse();
1352-
$section = new \PluginFormcreatorSection();
1385+
$section = new PluginFormcreatorSection();
13531386
$rows = $section->find([
13541387
'plugin_formcreator_forms_id' => $instance->getID(),
13551388
]);
13561389

13571390
$this->array($rows)->hasSize(1);
13581391
$row = array_shift($rows);
1359-
$this->string($row['name'])->isEqualTo(\PluginFormcreatorSection::getTypeName(1));
1392+
$this->string($row['name'])->isEqualTo(PluginFormcreatorSection::getTypeName(1));
13601393
}
13611394

13621395
/**

tests/src/CommonTestCase.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use atoum;
99
use Ticket;
1010
use User;
11+
use PluginFormcreatorSection;
1112

1213
abstract class CommonTestCase extends atoum
1314
{
@@ -212,7 +213,7 @@ protected function getSection($input = [], $formInput = []) {
212213
if (!isset($input['name'])) {
213214
$input['name'] = $this->getUniqueString();
214215
}
215-
$section = new \PluginFormcreatorSection();
216+
$section = new PluginFormcreatorSection();
216217
$section->add($input);
217218
$this->boolean($section->isNewItem())->isFalse();
218219
return $section;
@@ -222,7 +223,7 @@ protected function getQuestion($input = [], $sectionInput = [], $formInput = [])
222223
if (!isset($input['name'])) {
223224
$input['name'] = 'question';
224225
}
225-
$sectionFk = \PluginFormcreatorSection::getForeignKeyField();
226+
$sectionFk = PluginFormcreatorSection::getForeignKeyField();
226227
if (!isset($input[$sectionFk])) {
227228
$sectionId = $this->getSection($sectionInput, $formInput)->getID();
228229
$input[$sectionFk] = $sectionId;

0 commit comments

Comments
 (0)