Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Glpi/Form/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ private function prepareInput(array $input): array
unset($input['_conditions']);
}

$input = $this->removeSavedConditionsIfAlwaysVisible($input);

return $input;
}

Expand Down
3 changes: 3 additions & 0 deletions src/Glpi/Form/Condition/ConditionableCreationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,7 @@
interface ConditionableCreationInterface extends ConditionableInterface
{
public function getConfiguredCreationStrategy(): CreationStrategy;

// TODO: uncomment on main to prevent BC breaks
// protected function removeSavedConditionsIfAlwaysCreated(array $input): array;
}
17 changes: 17 additions & 0 deletions src/Glpi/Form/Condition/ConditionableCreationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@

namespace Glpi\Form\Condition;

use function Safe\json_encode;

trait ConditionableCreationTrait
{
use ConditionableTrait;
Expand All @@ -44,4 +46,19 @@ public function getConfiguredCreationStrategy(): CreationStrategy
$strategy = CreationStrategy::tryFrom($strategy_value);
return $strategy ?? CreationStrategy::ALWAYS_CREATED;
}

protected function removeSavedConditionsIfAlwaysCreated(array $input): array
{
$strategy_field = 'creation_strategy';
$condition_field = $this->getConditionsFieldName();

if (
isset($input[$strategy_field])
&& $input[$strategy_field] == CreationStrategy::ALWAYS_CREATED->value
) {
$input[$condition_field] = json_encode([]);
}

return $input;
}
}
17 changes: 17 additions & 0 deletions src/Glpi/Form/Condition/ConditionableValidationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@

namespace Glpi\Form\Condition;

use function Safe\json_encode;

trait ConditionableValidationTrait
{
use ConditionableTrait {
Expand Down Expand Up @@ -76,4 +78,19 @@ public function getConfiguredValidationStrategy(): ValidationStrategy
$strategy = ValidationStrategy::tryFrom($strategy_value);
return $strategy ?? ValidationStrategy::NO_VALIDATION;
}

protected function removeSavedConditionsIfNoValidation(array $input): array
{
$strategy_field = $this->getValidationStrategyFieldName();
$condition_field = $this->getValidationConditionsFieldName();

if (
isset($input[$strategy_field])
&& $input[$strategy_field] == ValidationStrategy::NO_VALIDATION->value
) {
$input[$condition_field] = json_encode([]);
}

return $input;
}
}
3 changes: 3 additions & 0 deletions src/Glpi/Form/Condition/ConditionableVisibilityInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,7 @@ public function getConfiguredVisibilityStrategy(): VisibilityStrategy;
* @return string
*/
public function getUUID(): string;

// TODO: uncomment on main to prevent BC breaks
// protected function removeSavedConditionsIfAlwaysVisible(array $input): array;
}
17 changes: 17 additions & 0 deletions src/Glpi/Form/Condition/ConditionableVisibilityTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@

namespace Glpi\Form\Condition;

use function Safe\json_encode;

trait ConditionableVisibilityTrait
{
use ConditionableTrait;
Expand All @@ -56,4 +58,19 @@ public function getConfiguredVisibilityStrategy(): VisibilityStrategy
$strategy = VisibilityStrategy::tryFrom($strategy_value);
return $strategy ?? VisibilityStrategy::ALWAYS_VISIBLE;
}

protected function removeSavedConditionsIfAlwaysVisible(array $input): array
{
$visibility_field = $this->getVisibilityStrategyFieldName();
$condition_field = $this->getConditionsFieldName();

if (
isset($input[$visibility_field])
&& $input[$visibility_field] == VisibilityStrategy::ALWAYS_VISIBLE->value
) {
$input[$condition_field] = json_encode([]);
}

return $input;
}
}
2 changes: 2 additions & 0 deletions src/Glpi/Form/Destination/FormDestination.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,8 @@ public function prepareInput($input): array
unset($input['_conditions']);
}

$input = $this->removeSavedConditionsIfAlwaysCreated($input);

return $input;
}

Expand Down
2 changes: 2 additions & 0 deletions src/Glpi/Form/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,8 @@ private function prepareInput(array $input): array
unset($input['_submit_button_conditions']);
}

$input = $this->removeSavedConditionsIfAlwaysVisible($input);

return $input;
}

Expand Down
3 changes: 3 additions & 0 deletions src/Glpi/Form/Question.php
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,9 @@ private function prepareInput(array $input): array
}
}

$input = $this->removeSavedConditionsIfAlwaysVisible($input);
$input = $this->removeSavedConditionsIfNoValidation($input);

return $input;
}

Expand Down
2 changes: 2 additions & 0 deletions src/Glpi/Form/Section.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ private function prepareInput(array $input): array
unset($input['_conditions']);
}

$input = $this->removeSavedConditionsIfAlwaysVisible($input);

return $input;
}

Expand Down
77 changes: 77 additions & 0 deletions tests/functional/Glpi/Form/CommentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2025 Teclib' and contributors.
* @licence https://www.gnu.org/licenses/gpl-3.0.html
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* This program 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 3 of the License, or
* (at your option) any later version.
*
* This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
*
* ---------------------------------------------------------------------
*/

namespace tests\units\Glpi\Form;

use DbTestCase;
use Glpi\Form\Comment;
use Glpi\Form\Condition\LogicOperator;
use Glpi\Form\Condition\Type;
use Glpi\Form\Condition\ValueOperator;
use Glpi\Form\Condition\VisibilityStrategy;
use Glpi\Form\QuestionType\QuestionTypeShortText;
use Glpi\Tests\FormBuilder;
use Glpi\Tests\FormTesterTrait;

final class CommentTest extends DbTestCase
{
use FormTesterTrait;

public function testConditionsDataAreCleanedWhenStrategyIsReset(): void
{
// Arrange: create a form with visibility conditions on a comment
$builder = new FormBuilder();
$builder->addQuestion("My question", QuestionTypeShortText::class);
$builder->addComment("My comment");
$builder->setCommentVisibility("My comment", VisibilityStrategy::VISIBLE_IF, [
[
'logic_operator' => LogicOperator::AND,
'item_name' => "My question",
'item_type' => Type::QUESTION,
'value_operator' => ValueOperator::EQUALS,
'value' => "Yes",
],
]);
$form = $this->createForm($builder);

// Act: reset the comment's visibility strategy
$comment_id = $this->getCommentId($form, "My comment");
$comment = $this->updateItem(Comment::class, $comment_id, [
'visibility_strategy' => VisibilityStrategy::ALWAYS_VISIBLE->value,
]);

// Assert: the conditions should be deleted
$this->assertEmpty($comment->getConfiguredConditionsData());
}
}
30 changes: 30 additions & 0 deletions tests/functional/Glpi/Form/Destination/FormDestinationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
use CommonGLPI;
use DBmysql;
use DbTestCase;
use Glpi\Form\Condition\CreationStrategy;
use Glpi\Form\Condition\LogicOperator;
use Glpi\Form\Condition\Type;
use Glpi\Form\Condition\ValueOperator;
use Glpi\Form\Destination\FormDestination;
use Glpi\Form\Destination\FormDestinationTicket;
use Glpi\Form\Form;
Expand Down Expand Up @@ -153,6 +157,32 @@ public function testWarningsAreRendereredInTabContent()
);
}

public function testConditionsDataAreCleanedWhenStrategyIsReset(): void
{
// Arrange: create a form with creation conditions on a destination
$builder = new FormBuilder();
$builder->addQuestion("My question", QuestionTypeShortText::class);
$builder->setDestinationCondition("Ticket", CreationStrategy::CREATED_IF, [
[
'logic_operator' => LogicOperator::AND,
'item_name' => "My question",
'item_type' => Type::QUESTION,
'value_operator' => ValueOperator::EQUALS,
'value' => "Yes",
],
]);
$form = $this->createForm($builder);

// Act: reset the destiantion's creation strategy
$destination_id = $this->getDestinationId($form, "Ticket");
$destination = $this->updateItem(FormDestination::class, $destination_id, [
'creation_strategy' => CreationStrategy::ALWAYS_CREATED->value,
]);

// Assert: the conditions should be deleted
$this->assertEmpty($destination->getConfiguredConditionsData());
}

private function createAndGetFormWithFourDestinations(): Form
{
$builder = new FormBuilder();
Expand Down
29 changes: 29 additions & 0 deletions tests/functional/Glpi/Form/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
use Glpi\Form\AccessControl\FormAccessControl;
use Glpi\Form\AnswersHandler\AnswersHandler;
use Glpi\Form\Comment;
use Glpi\Form\Condition\LogicOperator;
use Glpi\Form\Condition\Type;
use Glpi\Form\Condition\ValueOperator;
use Glpi\Form\Condition\VisibilityStrategy;
use Glpi\Form\Destination\FormDestination;
use Glpi\Form\Destination\FormDestinationChange;
use Glpi\Form\Destination\FormDestinationProblem;
Expand Down Expand Up @@ -811,4 +815,29 @@ public function testDefinedTabs(
// Assert: the tabs should match the expected data
$this->assertEquals($expected_tabs, $tabs);
}

public function testSubmitConditionsDataAreCleanedWhenStrategyIsReset(): void
{
// Arrange: create a form with a condition on its submit button
$builder = new FormBuilder();
$builder->addQuestion("My question", QuestionTypeShortText::class);
$builder->setSubmitButtonVisibility(VisibilityStrategy::VISIBLE_IF, [
[
'logic_operator' => LogicOperator::AND,
'item_name' => "My question",
'item_type' => Type::QUESTION,
'value_operator' => ValueOperator::EQUALS,
'value' => "Yes",
],
]);
$form = $this->createForm($builder);

// Act: reset the submit button's visibility strategy
$form = $this->updateItem(Form::class, $form->getID(), [
'submit_button_visibility_strategy' => VisibilityStrategy::ALWAYS_VISIBLE->value,
]);

// Assert: the conditions should be deleted
$this->assertEmpty($form->getConfiguredConditionsData());
}
}
Loading