Skip to content

Commit 4afe8b3

Browse files
committed
test: add tsts
Signed-off-by: Thierry Bugier <[email protected]>
1 parent 9683807 commit 4afe8b3

File tree

2 files changed

+89
-18
lines changed

2 files changed

+89
-18
lines changed

inc/form.class.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,6 @@ public function rawSearchOptions() {
228228
* @return String Html string to be displayed for the form field
229229
*/
230230
public static function getSpecificValueToSelect($field, $name = '', $values = '', array $options = []) {
231-
232231
if (!is_array($values)) {
233232
$values = [$field => $values];
234233
}
@@ -607,11 +606,6 @@ public function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) {
607606
global $DB;
608607

609608
switch ($item->getType()) {
610-
case PluginFormcreatorConfig::class:
611-
$found = $DB->request(self::getTable(), []);
612-
$number = count($found);
613-
return self::createTabEntry(self::getTypeName($number), $number);
614-
break;
615609
case PluginFormcreatorForm::class:
616610
$nb = 0;
617611
foreach ($this->getTargetTypes() as $targetType) {
@@ -632,7 +626,7 @@ public function getTabNameForItem(CommonGLPI $item, $withtemplate = 0) {
632626
2 => __('Preview'),
633627
];
634628
break;
635-
case 'Central':
629+
case Central::class:
636630
return _n('Form', 'Forms', Session::getPluralNumber(), 'formcreator');
637631
break;
638632
}
@@ -1903,7 +1897,7 @@ public static function import(PluginFormcreatorLinker $linker, $input = [], $con
19031897
// Find an existing form to update, only if an UUID is available
19041898
$itemId = false;
19051899
/** @var string $idKey key to use as ID (id or uuid) */
1906-
$idKey = 'id';
1900+
$idKey = 'id';
19071901
if (isset($input['uuid'])) {
19081902
// Try to find an existing item to update
19091903
$idKey = 'uuid';

tests/suite-unit/PluginFormcreatorForm.php

Lines changed: 87 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function beforeTestMethod($method) {
3939
parent::beforeTestMethod($method);
4040
switch ($method) {
4141
case 'testImport':
42+
case 'testCanPurgeItem':
4243
self::login('glpi', 'glpi');
4344
}
4445
}
@@ -113,6 +114,19 @@ public function testCanPurge() {
113114
$this->boolean((bool) $output)->isFalse();
114115
}
115116

117+
public function testCanPurgeItem() {
118+
$form = $this->getForm();
119+
$output = $form->canPurgeItem();
120+
$this->boolean((boolean) $output)->isTrue();
121+
122+
$form->saveForm([
123+
\PluginFormcreatorForm::getForeignKeyField() => $form->getID(),
124+
]);
125+
126+
$output = $form->canPurgeItem();
127+
$this->boolean((boolean) $output)->isFalse();
128+
}
129+
116130
protected function formProvider() {
117131
return [
118132
[
@@ -204,18 +218,81 @@ public function testPrepareInputForUpdate($input, $expected, $expectedMessage) {
204218
}
205219
}
206220

207-
/**
208-
* @dataProvider formProvider
209-
*/
210-
public function testPurgeForm($formData) {
211-
$form = new \PluginFormcreatorForm();
212-
$form->add($formData);
213-
$this->boolean($form->isNewItem())->isFalse();
221+
public function testDefineTabs() {
222+
$form = $this->getForm();
223+
$output = $form->defineTabs();
224+
$this->array($output)->isEqualTo([
225+
'PluginFormcreatorForm$main' => "Form",
226+
'PluginFormcreatorQuestion$1' => "Questions",
227+
'PluginFormcreatorForm_Profile$1' => "Access types",
228+
'PluginFormcreatorForm$1' => "Targets",
229+
'PluginFormcreatorForm$2' => "Preview",
230+
'PluginFormcreatorFormAnswer$1' => "Form answers",
231+
]);
232+
}
233+
234+
public function testGetTabNameForItem() {
235+
$form = $this->getForm();
236+
$item = new \Central();
237+
$output = $form->getTabNameForItem($item);
238+
$this->string($output)->isEqualTo('Forms');
239+
240+
$item = $form;
241+
$output = $form->getTabNameForItem($item);
242+
$this->array($output)->isEqualTo([
243+
1 => 'Targets',
244+
2 => 'Preview',
245+
]);
246+
247+
$item = new \User();
248+
$output = $form->getTabNameForItem($item);
249+
$this->string($output)->isEqualTo('');
250+
}
251+
252+
public function testPost_purgeItem() {
253+
$form = $this->getForm([
254+
'validation_required' => \PluginFormcreatorForm_Validator::VALIDATION_USER,
255+
'users_id' => 2, // glpi
256+
]);
257+
$section = $this->getSection([
258+
'plugin_formcreator_forms_id' => $form->getID(),
259+
]);
260+
$targetChange = $this->getTargetChange([
261+
'plugin_formcreator_forms_id' => $form->getID(),
262+
]);
263+
$targetTicket = $this->getTargetTicket([
264+
'plugin_formcreator_forms_id' => $form->getID(),
265+
]);
266+
$validator = new \PluginFormcreatorForm_Validator();
267+
$validator->getFromDBByCrit([
268+
'plugin_formcreator_forms_id' => $form->getID(),
269+
'itemtype' => \User::class,
270+
]);
214271

215-
$success = $form->delete([
216-
'id' => $form->getID(),
272+
$formProfile = new \PluginFormcreatorForm_Profile();
273+
$formProfile->add([
274+
'plugin_formcreator_forms_id' => $form->getID(),
275+
'profiles_id' => 6 // technician
276+
]);
277+
278+
$form->delete([
279+
'id' => $form->getID(),
217280
], 1);
218-
$this->boolean($success)->isTrue();
281+
282+
$output = $section->getFromDB($section->getID()) ;
283+
$this->boolean($output)->isFalse();
284+
285+
$output = $targetChange->getFromDB($targetChange->getID());
286+
$this->boolean($output)->isFalse();
287+
288+
$output = $targetTicket->getFromDB($targetTicket->getID());
289+
$this->boolean($output)->isFalse();
290+
291+
$output = $validator->getFromDB($validator->getID());
292+
$this->boolean($output)->isFalse();
293+
294+
$output = $formProfile->getFromDB($formProfile->getID());
295+
$this->boolean($output)->isFalse();
219296
}
220297

221298
public function testCreateValidationNotification() {

0 commit comments

Comments
 (0)