-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathPluginFormcreatorSection.php
More file actions
603 lines (545 loc) · 24.3 KB
/
Copy pathPluginFormcreatorSection.php
File metadata and controls
603 lines (545 loc) · 24.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
<?php
/**
* ---------------------------------------------------------------------
* Formcreator is a plugin which allows creation of custom forms of
* easy access.
* ---------------------------------------------------------------------
* LICENSE
*
* This file is part of Formcreator.
*
* Formcreator is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Formcreator is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Formcreator. If not, see <http://www.gnu.org/licenses/>.
* ---------------------------------------------------------------------
* @copyright Copyright © 2011 - 2021 Teclib'
* @license http://www.gnu.org/licenses/gpl.txt GPLv3+
* @link https://github.com/pluginsGLPI/formcreator/
* @link https://pluginsglpi.github.io/formcreator/
* @link http://plugins.glpi-project.org/#/plugin/formcreator
* ---------------------------------------------------------------------
*/
namespace tests\units;
use GlpiPlugin\Formcreator\Tests\CommonTestCase;
use PluginFormcreatorCommon;
use PluginFormcreatorCondition;
use PluginFormcreatorConditionnableInterface;
use PluginFormcreatorForm;
use PluginFormcreatorLinker;
use PluginFormcreatorQuestion;
use PluginFormcreatorForm_Validator;
class PluginFormcreatorSection extends CommonTestCase {
public function setup() {
// instanciate classes
$form = new PluginFormcreatorForm;
$form_section = new \PluginFormcreatorSection;
$form_question = new PluginFormcreatorQuestion;
// create objects
$forms_id = $form->add([
'name' => "test clone form",
'is_active' => true,
'validation_required' => PluginFormcreatorForm_Validator::VALIDATION_USER
]);
$sections_id = $form_section->add([
'name' => "test clone section",
'plugin_formcreator_forms_id' => $forms_id
]);
$form_question->add([
'name' => "test clone question 1",
'fieldtype' => 'text',
'plugin_formcreator_sections_id' => $sections_id
]);
$form_question->add([
'name' => "test clone question 2",
'fieldtype' => 'textarea',
'plugin_formcreator_sections_id' => $sections_id
]);
}
public function beforeTestMethod($method) {
parent::beforeTestMethod($method);
switch ($method) {
case 'testImport':
case 'testGetTranslatableStrings':
$this->login('glpi', 'glpi');
}
}
/**
* @cover PluginFormcreatorSection::clone
*/
public function testDuplicate() {
global $DB;
// create objects
$form = $this->getForm([
'name' => "test clone form",
'is_active' => true,
'validation_required' => PluginFormcreatorForm_Validator::VALIDATION_USER
]);
$other_section = $this->getSection([
'plugin_formcreator_forms_id' => $form->getID(),
]);
$other_question = $this->getQuestion([
'plugin_formcreator_sections_id' => $other_section->getID(),
]);
$section = $this->getSection([
'name' => "test clone section",
'plugin_formcreator_forms_id' => $form->getID(),
]);
$condition = new PluginFormcreatorCondition();
$condition->add([
'itemtype' => $section::getType(),
'items_id' => $section->getID(),
'show_logic' => PluginFormcreatorCondition::SHOW_LOGIC_AND,
'show_condition' => PluginFormcreatorCondition::SHOW_CONDITION_EQ,
'show_value' => 'foo',
'plugin_formcreator_questions_id' => $other_question->getID(),
]);
$this->boolean($condition->isNewItem())->isFalse();
$DB->update($section::getTable(), [
'show_rule' => PluginFormcreatorCondition::SHOW_RULE_HIDDEN,
], [
'id' => $section->getID(),
]);
$section->getFromDB($section->getID()); // Refresh instance
$this->getQuestion([
'name' => "test clone question 1",
'fieldtype' => 'text',
'plugin_formcreator_sections_id' => $section->getID(),
]);
$this->getQuestion([
'name' => "test clone question 2",
'fieldtype' => 'textarea',
'plugin_formcreator_sections_id' => $section->getID(),
]);
//get max order of sections
$max = PluginFormcreatorCommon::getMax(
$section,
['plugin_formcreator_forms_id' => $section->fields['plugin_formcreator_forms_id']],
'order'
);
//clone it
$newSection_id = $section->duplicate();
$this->integer($newSection_id)->isGreaterThan(0);
//get cloned section
$new_section = $this->newTestedInstance();
$new_section->getFromDB($newSection_id);
// check uuid
$this->string($new_section->getField('uuid'))->isNotEqualTo($section->getField('uuid'));
// check order
$this->integer((int) $new_section->fields['order'])->isEqualTo($max + 1);
// check questions
$all_questions = $DB->request([
'SELECT' => ['uuid'],
'FROM' => PluginFormcreatorQuestion::getTable(),
'WHERE' => [
'plugin_formcreator_sections_id' => $section->getID()
]
]);
$all_new_questions = $DB->request([
'SELECT' => ['uuid'],
'FROM' => PluginFormcreatorQuestion::getTable(),
'WHERE' => [
'plugin_formcreator_sections_id' => $new_section->getID()
]
]);
$this->integer(count($all_new_questions))->isEqualTo(count($all_questions));
// check that all question uuid are new
$uuids = $new_uuids = [];
foreach ($all_questions as $question) {
$uuids[] = $question['uuid'];
}
foreach ($all_new_questions as $question) {
$new_uuids[] = $question['uuid'];
}
$this->integer(count(array_diff($new_uuids, $uuids)))->isEqualTo(count($new_uuids));
// Check conditions
$all_conditions = $DB->request([
'SELECT' => ['uuid'],
'FROM' => PluginFormcreatorCondition::getTable(),
'WHERE' => [
'itemtype' => $section::getType(),
'items_id' => $section->getID(),
],
]);
$all_new_conditions = $DB->request([
'SELECT' => ['uuid'],
'FROM' => PluginFormcreatorCondition::getTable(),
'WHERE' => [
'itemtype' => $new_section::getType(),
'items_id' => $new_section->getID(),
],
]);
$this->integer(count($all_new_questions))->isEqualTo(count($all_questions));
// check that all conditions uuid are new
$uuids = $new_uuids = [];
foreach ($all_conditions as $condition) {
$uuids[] = $condition['uuid'];
}
foreach ($all_new_conditions as $condition) {
$new_uuids[] = $condition['uuid'];
}
$this->integer(count(array_diff($new_uuids, $uuids)))->isEqualTo(count($new_uuids));
// Check that new section has same show rule as original
$this->integer($section->fields['show_rule'])->isEqualTo($new_section->fields['show_rule']);
}
public function testExport() {
$instance = $this->newTestedInstance();
// Try to export an empty item
$this->exception(function () use ($instance) {
$instance->export();
})->isInstanceOf(\GlpiPlugin\Formcreator\Exception\ExportFailureException::class);
// Prepare an item to export
$instance = $this->getSection();
$instance->getFromDB($instance->getID());
// Export the item without the ID and with UUID
$output = $instance->export(false);
// Test the exported data
$fieldsWithoutID = [
'name',
'order',
'show_rule',
];
$extraFields = [
'_questions',
'_conditions',
];
$this->array($output)
->hasKeys($fieldsWithoutID + $extraFields + ['uuid'])
->hasSize(1 + count($fieldsWithoutID) + count($extraFields));
// Export the item without the UUID and with ID
$output = $instance->export(true);
$this->array($output)
->hasKeys($fieldsWithoutID + $extraFields + ['id'])
->hasSize(1 + count($fieldsWithoutID) + count($extraFields));
}
public function testImport() {
$form = $this->getForm();
$uuid = plugin_formcreator_getUuid();
$input = [
'name' => $this->getUniqueString(),
'order' => '1',
'show_rule' => PluginFormcreatorCondition::SHOW_RULE_ALWAYS,
'uuid' => $uuid,
];
$linker = new \PluginFormcreatorLinker();
$sectionId = \PluginFormcreatorSection::import($linker, $input, $form->getID());
$this->integer($sectionId)->isGreaterThan(0);
unset($input['uuid']);
$testedClassName = $this->getTestedClassName();
$this->exception(
function() use($linker, $input, $form, $testedClassName) {
$testedClassName::import($linker, $input, $form->getID());
}
)->isInstanceOf(\GlpiPlugin\Formcreator\Exception\ImportFailureException::class)
->hasMessage('UUID or ID is mandatory for Section'); // passes
$input['id'] = $sectionId;
$sectionId2 = $testedClassName::import($linker, $input, $form->getID());
$this->variable($sectionId2)->isNotFalse();
$this->integer((int) $sectionId)->isNotEqualTo($sectionId2);
}
public function testMoveUp() {
$formFk = PluginFormcreatorForm::getForeignKeyField();
$form = $this->getForm();
$section = $this->getSection(
[
$formFk => $form->getID(),
]
);
$sectionToMove = $this->getSection(
[
$formFk => $form->getID(),
]
);
// Move up the section
$expectedOrder = $sectionToMove->fields['order'] - 1;
$sectionToMove->moveUp();
// Check the order of the section
$this->integer((int) $sectionToMove->fields['order'])
->isEqualTo($expectedOrder);
// check the order of the other section
$expectedOrder = $section->fields['order'] + 1;
$section->getFromDB($section->getID());
$this->integer((int) $section->fields['order'])
->isEqualTo($expectedOrder);
}
public function testMoveDown() {
$formFk = PluginFormcreatorForm::getForeignKeyField();
$form = $this->getForm();
$sectionToMove = $this->getSection(
[
$formFk => $form->getID(),
]
);
$section = $this->getSection(
[
$formFk => $form->getID(),
]
);
// Move down the section
$expectedOrder = $sectionToMove->fields['order'] + 1;
$sectionToMove->moveDown();
// Check the order of the section
$this->integer((int) $sectionToMove->fields['order'])
->isEqualTo($expectedOrder);
// check the order of the other section
$expectedOrder = $section->fields['order'] - 1;
$section->getFromDB($section->getID());
$this->integer((int) $section->fields['order'])
->isEqualTo($expectedOrder);
}
public function testIsEmptyRow() {
$testedClassName = $this->getTestedClassName();
$section = $this->getSection();
$sectionFk = $testedClassName::getForeignKeyField();
[
0 => $this->getQuestion([
$sectionFk => $section->getID(),
'row' => 0,
]),
1 => $this->getQuestion([
$sectionFk => $section->getID(),
'row' => 2,
]),
2 => $this->getQuestion([
$sectionFk => $section->getID(),
'row' => 4,
]),
];
$this->boolean($section->isRowEmpty(0))->isFalse();
$this->boolean($section->isRowEmpty(1))->isFalse();
$this->boolean($section->isRowEmpty(2))->isFalse();
$this->boolean($section->isRowEmpty(3))->isTrue();
$this->boolean($section->isRowEmpty(4))->isTrue();
$this->boolean($section->isRowEmpty(5))->isTrue();
}
public function testGetTranslatableStrings() {
$data = file_get_contents(dirname(__DIR__) . '/fixture/all_question_types_form.json');
$data = json_decode($data, true);
foreach ($data['forms'] as $formData) {
$form = new PluginFormcreatorForm();
$formId = $form->import(new PluginFormcreatorLinker(), $formData);
$this->boolean($form->isNewID($formId))->isFalse();
}
$form->getFromDB($formId);
$this->boolean($form->isNewItem())->isFalse();
$section = $this->newTestedInstance();
$section->getFromDBByCrit([
'plugin_formcreator_forms_id' => $formId,
'name' => 'section',
]);
$this->boolean($section->isNewItem())->isFalse();
$output = $section->getTranslatableStrings();
$this->array($output)->isIdenticalTo([
'itemlink' =>
[
'73d5342eba070f636ac3246f319bf77f' => 'section',
'8c647f55ac463429f736aea1ad64d318' => 'actors question',
'de1ece2a98dacb86a2b65334373ccb99' => 'checkboxes question',
'e121a8d9e19bf923a648d6bfb33094d8' => 'date question',
'7d3246feb9616461eee152642ad9f1fb' => 'datetime question',
'824d1cc309c56586a33b52858cbc146b' => 'description question',
'8347ce048fc3fe8b954dbc6cd9c4b716' => 'dropdown question',
'895472a7be51fe6b1b9591a150fb55d8' => 'email question',
'75c4f52e98ebd4a57410d882780353db' => 'file question',
'037cad549bb834c2fab44fe14480f9a9' => 'float question',
'97ee07194ba5af1c81eb5a9b22141241' => 'GLPI object question',
'74b8be9aff59bf5ddd149248d6156baa' => 'hidden question',
'0550a71495224d60dfcd00826345f0fa' => 'hostname question',
'd767bdc805e010bfd2302c2516501ffb' => 'IP address question',
'b5c09bbe5587577a8c86ada678664877' => 'integer question',
'5b3ebb576a3977eaa267f0769bdd8e98' => 'LDAP question',
'35226e073fabdcce01c547c5bce62d14' => 'multiselect question',
'58e2a2355ba7ac135d42f558591d6a6a' => 'radio question',
'2637b4d11281dffbaa2e340561347ebc' => 'request type question',
'212afc3240debecf859880ea9ab4fc2e' => 'select question',
'6fd6eacf3005974a7489a199ed7b45ee' => 'text question',
'b99b0833f1dab41a14eb421fa2ce690d' => 'textarea question',
'e3a0dfbc9d24603beddcbd1388808a7a' => 'time question',
'49dce550d75300e99052ed4e8006b65a' => 'urgency question',
],
'string' =>
[
'bc41fd6c06a851dc3e5f52ef82c46357' => 'a (checkbox)',
'2e2682dc7fe28972eede52a085f9b8da' => 'b (checkbox)',
'a212352098d74d20ad0869e8b11870dd' => 'c (checkbox)',
'2ee11338e1d5571cdcdc959e05d13fdd' => 'hidden value',
'26b6a3b22c4a9eacd9bcca663c6bfb98' => 'a (multiselect)',
'fe3ba23b6c304bcfccab1c4037170043' => 'b (multiselect)',
'76abd40f08cc003cfb75e02d8603a618' => 'c (multiselect)',
'aa08e69f50f9d7e4a280b5e395a926f3' => 'a (radio)',
'3d8f74862a3f325c160d5b4090cc1344' => 'b (radio)',
'60459f8c72beb121493ec56bd0b41473' => 'c (radio)',
'3e6b3c27f45682bbe11ed102ff9cbd31' => 'a (select)',
'12f59df90d7b53129d8e6da91f60cf86' => 'b (select)',
'1dd65ffc0516477159ec9ba8c170ef94' => 'c (select)',
'4f87be8f6e593d167f5fd1ab238cfc2d' => '/foo/',
],
'text' =>
[
'06ff4080ef6f9ee755cc45cba5f80360' => 'actors description',
'874e42442b551ef2769cc498157f542d' => 'checkboxes description',
'42be0556a01c9e0a28da37d2e3c5153d' => 'date description',
'b698fbcd4b9acf232b8b88755a1728f0' => 'datetime description',
'ab87cc96356a7d5c1d37c877fd56c6b0' => 'description text',
'59ef614a194389f0b54e46b728fe22a2' => 'dropdown description',
'b70e872f17f616049c642f2db8f35c8a' => 'email description',
'2b4f8f08c4162a2dac4a9b82e97605c0' => 'file description',
'b1a3d83a831e20619e1f14f6dbc64105' => 'float description',
'54ee213f0c0aae084d5712dc96bac833' => 'GLPI object description',
'91ca037d3ec611f6c684114abce7296f' => 'hidden description',
'98443bed844ba97392d8a8fb364b5d66' => 'hostname description',
'4b2e461a0b3c307923176188fb6273c6' => 'IP address description',
'51d8d951cf91a008f5b87c7d36ee6789' => 'integer description',
'c0117d3ded05c5c672425a48a63c83d7' => 'LDAP description',
'2d0b83793d10440b70c33a2229c88a09' => 'multiselect description',
'06cdb33e33e576a973d7bf54fcded96e' => 'radios description',
'471217363e6922ff6b1c9fd9cd57cd2a' => 'request type description',
'64dfbbc489b074af269e0b0fbf0d901b' => 'select description',
'b371eae37f18f0b6125002999b2404ba' => 'text description',
'7e4dc84bec34373c30ee35b8c90a401c' => '<p>textarea description</p>',
'8d544ed7c846a47654b2f55db879d7b2' => 'time description',
'e634ce2f4abe0deaa3f7cd44e13f4af6' => 'urgency description',
],
'id' =>
[
'73d5342eba070f636ac3246f319bf77f' => 'itemlink',
'8c647f55ac463429f736aea1ad64d318' => 'itemlink',
'06ff4080ef6f9ee755cc45cba5f80360' => 'text',
'de1ece2a98dacb86a2b65334373ccb99' => 'itemlink',
'874e42442b551ef2769cc498157f542d' => 'text',
'bc41fd6c06a851dc3e5f52ef82c46357' => 'string',
'2e2682dc7fe28972eede52a085f9b8da' => 'string',
'a212352098d74d20ad0869e8b11870dd' => 'string',
'e121a8d9e19bf923a648d6bfb33094d8' => 'itemlink',
'42be0556a01c9e0a28da37d2e3c5153d' => 'text',
'7d3246feb9616461eee152642ad9f1fb' => 'itemlink',
'b698fbcd4b9acf232b8b88755a1728f0' => 'text',
'824d1cc309c56586a33b52858cbc146b' => 'itemlink',
'ab87cc96356a7d5c1d37c877fd56c6b0' => 'text',
'8347ce048fc3fe8b954dbc6cd9c4b716' => 'itemlink',
'59ef614a194389f0b54e46b728fe22a2' => 'text',
'895472a7be51fe6b1b9591a150fb55d8' => 'itemlink',
'b70e872f17f616049c642f2db8f35c8a' => 'text',
'75c4f52e98ebd4a57410d882780353db' => 'itemlink',
'2b4f8f08c4162a2dac4a9b82e97605c0' => 'text',
'037cad549bb834c2fab44fe14480f9a9' => 'itemlink',
'b1a3d83a831e20619e1f14f6dbc64105' => 'text',
'97ee07194ba5af1c81eb5a9b22141241' => 'itemlink',
'54ee213f0c0aae084d5712dc96bac833' => 'text',
'74b8be9aff59bf5ddd149248d6156baa' => 'itemlink',
'91ca037d3ec611f6c684114abce7296f' => 'text',
'2ee11338e1d5571cdcdc959e05d13fdd' => 'string',
'0550a71495224d60dfcd00826345f0fa' => 'itemlink',
'98443bed844ba97392d8a8fb364b5d66' => 'text',
'd767bdc805e010bfd2302c2516501ffb' => 'itemlink',
'4b2e461a0b3c307923176188fb6273c6' => 'text',
'b5c09bbe5587577a8c86ada678664877' => 'itemlink',
'51d8d951cf91a008f5b87c7d36ee6789' => 'text',
'5b3ebb576a3977eaa267f0769bdd8e98' => 'itemlink',
'c0117d3ded05c5c672425a48a63c83d7' => 'text',
'35226e073fabdcce01c547c5bce62d14' => 'itemlink',
'2d0b83793d10440b70c33a2229c88a09' => 'text',
'26b6a3b22c4a9eacd9bcca663c6bfb98' => 'string',
'fe3ba23b6c304bcfccab1c4037170043' => 'string',
'76abd40f08cc003cfb75e02d8603a618' => 'string',
'58e2a2355ba7ac135d42f558591d6a6a' => 'itemlink',
'06cdb33e33e576a973d7bf54fcded96e' => 'text',
'aa08e69f50f9d7e4a280b5e395a926f3' => 'string',
'3d8f74862a3f325c160d5b4090cc1344' => 'string',
'60459f8c72beb121493ec56bd0b41473' => 'string',
'2637b4d11281dffbaa2e340561347ebc' => 'itemlink',
'471217363e6922ff6b1c9fd9cd57cd2a' => 'text',
'212afc3240debecf859880ea9ab4fc2e' => 'itemlink',
'64dfbbc489b074af269e0b0fbf0d901b' => 'text',
'3e6b3c27f45682bbe11ed102ff9cbd31' => 'string',
'12f59df90d7b53129d8e6da91f60cf86' => 'string',
'1dd65ffc0516477159ec9ba8c170ef94' => 'string',
'6fd6eacf3005974a7489a199ed7b45ee' => 'itemlink',
'b371eae37f18f0b6125002999b2404ba' => 'text',
'b99b0833f1dab41a14eb421fa2ce690d' => 'itemlink',
'7e4dc84bec34373c30ee35b8c90a401c' => 'text',
'4f87be8f6e593d167f5fd1ab238cfc2d' => 'string',
'e3a0dfbc9d24603beddcbd1388808a7a' => 'itemlink',
'8d544ed7c846a47654b2f55db879d7b2' => 'text',
'49dce550d75300e99052ed4e8006b65a' => 'itemlink',
'e634ce2f4abe0deaa3f7cd44e13f4af6' => 'text',
],
]);
}
public function post_purgeItem() {
$sections = [];
$sections[1] = $this->getSection();
$this->boolean($sections[1]->isNewItem())->isFalse();
$this->integer((int) $sections[1]->fields['order'])->isEqualTo(1);
/** @var PluginFormcreatorForm $form */
$form = PluginFormcreatorForm::getById($sections[1]->fields['plugin_formcreator_forms_id']);
$this->boolean($form->isNewItem())->isFalse();
$sections[2] = $this->getSection([
'plugin_formcreator_forms_id' => $form->getID(),
]);
$this->boolean($sections[2]->isNewItem())->isFalse();
$this->integer((int) $sections[2]->fields['order'])->isEqualTo(2);
$sections[3] = $this->getSection([
'plugin_formcreator_forms_id' => $form->getID(),
]);
$this->boolean($sections[3]->isNewItem())->isFalse();
$this->integer((int) $sections[3]->fields['order'])->isEqualTo(3);
$questions = [];
$questions[1] = $this->getQuestion([
'plugin_formcreator_sections_id' => $sections[1]->getID(),
]);
$this->boolean($questions[1]->isNewItem())->isFalse();
$questions[1] = $this->getQuestion([
'plugin_formcreator_sections_id' => $sections[1]->getID(),
]);
$this->boolean($questions[2]->isNewItem())->isFalse();
// A form with 3 sections, the first sections has 2 questions
$sections[1]->post_purgeItem();
// check the sections 2 and 3 decreased their position
$sections[2]->getFromDB($sections[2]->getID());
$sections[3]->getFromDB($sections[3]->getID());
$this->integer((int) $sections[2]->fields['order'])->isEqualTo(1);
$this->integer((int) $sections[3]->fields['order'])->isEqualTo(2);
// check the questions are deleted
$this->boolean(PluginFormcreatorQuestion::getById($questions[1]->getID()))->isFalse();
$this->boolean(PluginFormcreatorQuestion::getById($questions[2]->getID()))->isFalse();
}
public function testGetSectionsFromForm() {
$form = $this->getForm();
$this->boolean($form->isNewItem())->isFalse();
$testedClassName = $this->getTestedClassName();
$output = $testedClassName::getSectionsFromForm($form->getID());
$this->array(iterator_to_array($output))->hasSize(0);
$sections = [];
$section = $this->getSection([
'plugin_formcreator_forms_id' => $form->getID(),
]);
$this->boolean($section->isNewItem())->isFalse();
$sections[] = $section;
$section = $this->getSection([
'plugin_formcreator_forms_id' => $form->getID(),
]);
$this->boolean($section->isNewItem())->isFalse();
$sections[] = $section;
$output = $testedClassName::getSectionsFromForm($form->getID());
$this->array(iterator_to_array($output))->hasSize(2);
$output = $testedClassName::getSectionsFromForm($form->getID());
$found = 0;
foreach ($output as $section) {
foreach ($sections as $search) {
if ($search->getID() == $section->getID()) {
$found++;
}
}
}
$this->integer($found)->isEqualTo(2);
}
}