From 52580233870d14ac9972c7a43e0a0fa8c91c9265 Mon Sep 17 00:00:00 2001 From: Rom1-B Date: Mon, 3 Nov 2025 14:07:16 +0100 Subject: [PATCH 1/2] Feat(CI): add rector --- rector.php | 59 +++++++++++++++++++ .../GetAuthLdapFilterController.php | 2 +- src/Controller/LdapDropdownController.php | 2 + src/Model/Dropdown/LdapDropdown.php | 20 ++++--- src/Model/Dropdown/LdapDropdownQuery.php | 2 +- src/Model/QuestionType/LdapQuestion.php | 18 ++---- src/Model/QuestionType/LdapQuestionConfig.php | 4 +- src/Service/ConfigManager.php | 2 +- src/Utils/SafeCommonDBTM.php | 1 + tests/AdvancedFormsTestCase.php | 10 ++-- .../Controller/LdapDropdownControllerTest.php | 7 --- tests/Front/FrontTestCase.php | 9 ++- .../FormcreatorHiddenTypeMapperTest.php | 5 +- .../FormcreatorHostnameTypeMapperTest.php | 5 +- .../Mapper/FormcreatorIpTypeMapperTest.php | 5 +- .../FormcreatorLdapSelectTypeMapperTest.php | 7 ++- tests/Model/Mapper/MapperTestCase.php | 7 +-- .../QuestionType/QuestionTypeTestCase.php | 2 + tests/Service/ConfigManagerTest.php | 4 +- tests/Service/InstallManagerTest.php | 1 + 20 files changed, 115 insertions(+), 57 deletions(-) create mode 100644 rector.php diff --git a/rector.php b/rector.php new file mode 100644 index 0000000..6ab267c --- /dev/null +++ b/rector.php @@ -0,0 +1,59 @@ +withPaths([ + __DIR__ . '/src', + __DIR__ . '/tests', + ]) + ->withPhpVersion(PhpVersion::PHP_82) + ->withCache( + cacheDirectory: __DIR__ . '/var/rector', + cacheClass: FileCacheStorage::class, + ) + ->withRootFiles() + ->withParallel(timeoutSeconds: 300) + ->withImportNames(removeUnusedImports: true) + ->withPreparedSets( + deadCode: true, + codeQuality: true, + codingStyle: true, + ) + ->withPhpSets(php82: true) // apply PHP sets up to PHP 8.2 +; diff --git a/src/Controller/GetAuthLdapFilterController.php b/src/Controller/GetAuthLdapFilterController.php index b9c5ade..e5e2a58 100644 --- a/src/Controller/GetAuthLdapFilterController.php +++ b/src/Controller/GetAuthLdapFilterController.php @@ -70,7 +70,7 @@ public function __invoke(Request $request): Response : '' ; return new JsonResponse([ - 'filter' => "(& $filter $ldap_condition)", + 'filter' => sprintf('(& %s %s)', $filter, $ldap_condition), ]); } } diff --git a/src/Controller/LdapDropdownController.php b/src/Controller/LdapDropdownController.php index 756804b..4583fca 100644 --- a/src/Controller/LdapDropdownController.php +++ b/src/Controller/LdapDropdownController.php @@ -65,6 +65,7 @@ public function __invoke(Request $request): Response if ($condition_uuid === "") { throw new BadRequestHttpException(); } + // We don't control this array // @phpstan-ignore offsetAccess.nonOffsetAccessible $condition = $_SESSION['glpicondition'][$condition_uuid] ?? null; @@ -78,6 +79,7 @@ public function __invoke(Request $request): Response if (!isset($condition[$fkey])) { throw new BadRequestHttpException(); } + $question_id = $condition[$fkey]; $question = Question::getById($question_id); if (!$question) { diff --git a/src/Model/Dropdown/LdapDropdown.php b/src/Model/Dropdown/LdapDropdown.php index 0d7b820..f37d4f6 100644 --- a/src/Model/Dropdown/LdapDropdown.php +++ b/src/Model/Dropdown/LdapDropdown.php @@ -130,6 +130,7 @@ public function getDropdownValues(LdapDropdownQuery $query): array if (!$attribute) { throw new RuntimeException(); } + $attribute = $attribute->fields['value']; if (!is_string($attribute)) { throw new LogicException(); @@ -142,7 +143,7 @@ public function getDropdownValues(LdapDropdownQuery $query): array } // Insert search text into filter if specified - if ($search_text != '') { + if ($search_text !== '') { $ldap_filter = sprintf( "(& %s (%s))", $config->getLdapFilter(), @@ -154,7 +155,7 @@ public function getDropdownValues(LdapDropdownQuery $query): array try { // Transform LDAP warnings into errors - set_error_handler([self::class, 'ldapErrorHandler'], E_WARNING); + set_error_handler(self::ldapErrorHandler(...), E_WARNING); // Execute search $ldap_values = $this->executeLdapSearch( @@ -164,16 +165,14 @@ public function getDropdownValues(LdapDropdownQuery $query): array $page_size, $ldap_filter, ); - } catch (Throwable $e) { - throw new RuntimeException("Failed LDAP query", previous: $e); + } catch (Throwable $throwable) { + throw new RuntimeException("Failed LDAP query", $throwable->getCode(), previous: $throwable); } finally { restore_error_handler(); } // Sort results - usort($ldap_values, function ($a, $b) { - return strnatcmp($a['text'], $b['text']); - }); + usort($ldap_values, fn($a, $b) => strnatcmp($a['text'], $b['text'])); // Set expected select2 format return [ @@ -226,6 +225,7 @@ private function executeLdapSearch( if (!$result instanceof Result) { throw new RuntimeException(); } + ldap_parse_result($ds, $result, $errcode, $matcheddn, $errmsg, $referrals, $controls); // PHPstan doens't know that this is safe @@ -260,11 +260,12 @@ private function executeLdapSearch( } $found_count++; - if ($found_count < ((int) $page - 1) * (int) $page_size + 1) { + if ($found_count < ($page - 1) * $page_size + 1) { // before the requested page continue; } - if ($found_count > ((int) $page) * (int) $page_size) { + + if ($found_count > ($page) * $page_size) { // after the requested page break; } @@ -280,6 +281,7 @@ private function executeLdapSearch( break; } } + // @phpstan-ignore notIdentical.alwaysTrue } while ($cookie !== null && $cookie != '' && $count < $page_size); diff --git a/src/Model/Dropdown/LdapDropdownQuery.php b/src/Model/Dropdown/LdapDropdownQuery.php index 6949eaf..83bafc0 100644 --- a/src/Model/Dropdown/LdapDropdownQuery.php +++ b/src/Model/Dropdown/LdapDropdownQuery.php @@ -35,7 +35,7 @@ use Glpi\Form\Question; -final class LdapDropdownQuery +final readonly class LdapDropdownQuery { public function __construct( private Question $question, diff --git a/src/Model/QuestionType/LdapQuestion.php b/src/Model/QuestionType/LdapQuestion.php index 6f7fed0..1c04799 100644 --- a/src/Model/QuestionType/LdapQuestion.php +++ b/src/Model/QuestionType/LdapQuestion.php @@ -35,6 +35,7 @@ use AuthLDAP; use Glpi\Application\View\TemplateRenderer; +use Glpi\DBAL\JsonFieldInterface; use Glpi\Form\Migration\FormQuestionDataConverterInterface; use Glpi\Form\Question; use Glpi\Form\QuestionType\AbstractQuestionType; @@ -83,7 +84,7 @@ public function renderAdministrationTemplate(Question|null $question): string { // Read extra config specific to this question type $decoded_extra_data = []; - if ($question !== null && is_string($question->fields['extra_data'])) { + if ($question instanceof Question && is_string($question->fields['extra_data'])) { $decoded_extra_data = json_decode( $question->fields['extra_data'], associative: true, @@ -94,8 +95,9 @@ public function renderAdministrationTemplate(Question|null $question): string $decoded_extra_data = []; } } + $config = $this->getExtraDataConfig($decoded_extra_data); - if ($config === null) { + if (!$config instanceof JsonFieldInterface) { $config = new LdapQuestionConfig(); } @@ -119,15 +121,7 @@ public function renderAdministrationTemplate(Question|null $question): string public function validateExtraDataInput(array $input): bool { // Check if the itemtype is set - if ( - !isset($input[LdapQuestionConfig::AUTHLDAP_ID]) - && !isset($input[LdapQuestionConfig::LDAP_FILTER]) - && !isset($input[LdapQuestionConfig::LDAP_ATTRIBUTE_ID]) - ) { - return false; - } - - return true; + return !(!isset($input[LdapQuestionConfig::AUTHLDAP_ID]) && !isset($input[LdapQuestionConfig::LDAP_FILTER]) && !isset($input[LdapQuestionConfig::LDAP_ATTRIBUTE_ID])); } #[Override] @@ -139,7 +133,7 @@ public function getExtraDataConfigClass(): string #[Override] public function renderEndUserTemplate(Question|null $question): string { - if ($question === null) { + if (!$question instanceof Question) { throw new LogicException(); } diff --git a/src/Model/QuestionType/LdapQuestionConfig.php b/src/Model/QuestionType/LdapQuestionConfig.php index fa5679e..ae3b0b7 100644 --- a/src/Model/QuestionType/LdapQuestionConfig.php +++ b/src/Model/QuestionType/LdapQuestionConfig.php @@ -36,11 +36,13 @@ use Glpi\DBAL\JsonFieldInterface; use Override; -final class LdapQuestionConfig implements JsonFieldInterface +final readonly class LdapQuestionConfig implements JsonFieldInterface { // Unique reference to hardcoded name used for serialization public const AUTHLDAP_ID = "authldap_id"; + public const LDAP_FILTER = "ldap_filter"; + public const LDAP_ATTRIBUTE_ID = "ldap_attribute_id"; public function __construct( diff --git a/src/Service/ConfigManager.php b/src/Service/ConfigManager.php index d60a7fb..7ce6be8 100644 --- a/src/Service/ConfigManager.php +++ b/src/Service/ConfigManager.php @@ -93,6 +93,6 @@ public function getEnabledQuestionsTypes(): array public function hasAtLeastOneQuestionTypeEnabled(): bool { - return count($this->getEnabledQuestionsTypes()) > 0; + return $this->getEnabledQuestionsTypes() !== []; } } diff --git a/src/Utils/SafeCommonDBTM.php b/src/Utils/SafeCommonDBTM.php index 888a716..532b8f0 100644 --- a/src/Utils/SafeCommonDBTM.php +++ b/src/Utils/SafeCommonDBTM.php @@ -70,6 +70,7 @@ public static function getForeignKeyField(string $class): string if (!is_string($field)) { throw new RuntimeException(); } + return $field; } } diff --git a/tests/AdvancedFormsTestCase.php b/tests/AdvancedFormsTestCase.php index b35888c..5aea986 100644 --- a/tests/AdvancedFormsTestCase.php +++ b/tests/AdvancedFormsTestCase.php @@ -63,8 +63,6 @@ final public static function provideQuestionTypes(): array public function setUp(): void { - parent::setUp(); - // Delete form related single instances $this->deleteSingletonInstance([ QuestionTypesManager::class, @@ -72,10 +70,7 @@ public function setUp(): void ]); } - public function tearDown(): void - { - parent::tearDown(); - } + public function tearDown(): void {} protected function enableConfigurableItem( ConfigurableItemInterface|string $item, @@ -91,6 +86,7 @@ protected function enableConfigurableItems( foreach ($items as $item) { $this->setConfigurableItemConfig($item, true); } + InitManager::getInstance()->init(); } @@ -108,6 +104,7 @@ protected function disableConfigurableItems( foreach ($items as $item) { $this->setConfigurableItemConfig($item, false); } + InitManager::getInstance()->init(); } @@ -169,6 +166,7 @@ private function deleteSingletonInstance(array $classes) $reflection_property = $reflection_class->getProperty('instance'); $reflection_property->setValue(null, null); } + if ($reflection_class->hasProperty('_instances')) { $reflection_property = $reflection_class->getProperty('_instances'); $reflection_property->setValue(null, []); diff --git a/tests/Controller/LdapDropdownControllerTest.php b/tests/Controller/LdapDropdownControllerTest.php index e2bd030..55d2699 100644 --- a/tests/Controller/LdapDropdownControllerTest.php +++ b/tests/Controller/LdapDropdownControllerTest.php @@ -130,13 +130,6 @@ public function testInvalidConditonParameter(): void ]); } - private function testWithInvalidForm(): void - { - $this->enableConfigurableItem(LdapQuestion::class); - $ldap = $this->setupAuthLdap(); - $form = $this->createFormWithLdapQuestion($ldap); - } - private function renderRoute(array $post): Response { $controller = new LdapDropdownController(); diff --git a/tests/Front/FrontTestCase.php b/tests/Front/FrontTestCase.php index 3c8fd49..19dec0c 100644 --- a/tests/Front/FrontTestCase.php +++ b/tests/Front/FrontTestCase.php @@ -55,10 +55,10 @@ public function get(string $url, array $params = []): Crawler $_SERVER['REQUEST_URI'] = GLPI_ROOT . $url; require(GLPI_ROOT . $url); $html = ob_get_clean(); - } catch (Throwable $e) { + } catch (Throwable $throwable) { $_GET = $old_GET; ob_get_clean(); - throw $e; + throw $throwable; } finally { $_GET = $old_GET; } @@ -129,17 +129,20 @@ public function sendForm( if (count($submits) > 1) { throw new RuntimeException("Only forms with a single submit are supported"); } + $submit = $submits->getNode(0); if (!$submit instanceof DOMElement) { throw new LogicException(); // Impossible } + $payload[$submit->getAttribute('name')] = $submit->getAttribute('value'); // Insert specified payload values foreach ($form_values as $key => $value) { if (!isset($payload[$key])) { - throw new RuntimeException("Input '$key' does not exist"); + throw new RuntimeException(sprintf("Input '%s' does not exist", $key)); } + $payload[$key] = $value; } diff --git a/tests/Model/Mapper/FormcreatorHiddenTypeMapperTest.php b/tests/Model/Mapper/FormcreatorHiddenTypeMapperTest.php index 41faa21..e0247a2 100644 --- a/tests/Model/Mapper/FormcreatorHiddenTypeMapperTest.php +++ b/tests/Model/Mapper/FormcreatorHiddenTypeMapperTest.php @@ -33,6 +33,7 @@ namespace GlpiPlugin\Advancedforms\Tests\Model\Mapper; +use DBmysql; use Glpi\Form\AccessControl\FormAccessControlManager; use Glpi\Form\Migration\FormMigration; use Glpi\Form\Question; @@ -43,7 +44,7 @@ final class FormcreatorHiddenTypeMapperTest extends MapperTestCase { public function testHiddenTypeMigrationWhenEnabled(): void { - /** @var \DBmysql $DB */ + /** @var DBmysql $DB */ global $DB; // Arrange: enable ip question type and add some fomrcreator data @@ -81,7 +82,7 @@ public function testHiddenTypeMigrationWhenEnabled(): void public function testHiddenTypeMigrationWhenDisabled(): void { - /** @var \DBmysql $DB */ + /** @var DBmysql $DB */ global $DB; // Arrange: add some fomrcreator data diff --git a/tests/Model/Mapper/FormcreatorHostnameTypeMapperTest.php b/tests/Model/Mapper/FormcreatorHostnameTypeMapperTest.php index e046c2c..423ca1a 100644 --- a/tests/Model/Mapper/FormcreatorHostnameTypeMapperTest.php +++ b/tests/Model/Mapper/FormcreatorHostnameTypeMapperTest.php @@ -33,6 +33,7 @@ namespace GlpiPlugin\Advancedforms\Tests\Model\Mapper; +use DBmysql; use Glpi\Form\AccessControl\FormAccessControlManager; use Glpi\Form\Migration\FormMigration; use Glpi\Form\Question; @@ -43,7 +44,7 @@ final class FormcreatorHostnameTypeMapperTest extends MapperTestCase { public function testHostnameTypeMigrationWhenEnabled(): void { - /** @var \DBmysql $DB */ + /** @var DBmysql $DB */ global $DB; // Arrange: enable ip question type and add some fomrcreator data @@ -71,7 +72,7 @@ public function testHostnameTypeMigrationWhenEnabled(): void public function testHostnameTypeMigrationWhenDisabled(): void { - /** @var \DBmysql $DB */ + /** @var DBmysql $DB */ global $DB; // Arrange: add some fomrcreator data diff --git a/tests/Model/Mapper/FormcreatorIpTypeMapperTest.php b/tests/Model/Mapper/FormcreatorIpTypeMapperTest.php index b8685be..92b3441 100644 --- a/tests/Model/Mapper/FormcreatorIpTypeMapperTest.php +++ b/tests/Model/Mapper/FormcreatorIpTypeMapperTest.php @@ -33,6 +33,7 @@ namespace GlpiPlugin\Advancedforms\Tests\Model\Mapper; +use DBmysql; use Glpi\Form\AccessControl\FormAccessControlManager; use Glpi\Form\Migration\FormMigration; use Glpi\Form\Question; @@ -43,7 +44,7 @@ final class FormcreatorIpTypeMapperTest extends MapperTestCase { public function testIpTypeMigrationWhenEnabled(): void { - /** @var \DBmysql $DB */ + /** @var DBmysql $DB */ global $DB; // Arrange: enable ip question type and add some fomrcreator data @@ -71,7 +72,7 @@ public function testIpTypeMigrationWhenEnabled(): void public function testIpTypeMigrationWhenDisabled(): void { - /** @var \DBmysql $DB */ + /** @var DBmysql $DB */ global $DB; // Arrange: add some fomrcreator data diff --git a/tests/Model/Mapper/FormcreatorLdapSelectTypeMapperTest.php b/tests/Model/Mapper/FormcreatorLdapSelectTypeMapperTest.php index 56d59b0..7b2ccdf 100644 --- a/tests/Model/Mapper/FormcreatorLdapSelectTypeMapperTest.php +++ b/tests/Model/Mapper/FormcreatorLdapSelectTypeMapperTest.php @@ -33,10 +33,10 @@ namespace GlpiPlugin\Advancedforms\Tests\Model\Mapper; +use DBmysql; use Glpi\Form\AccessControl\FormAccessControlManager; use Glpi\Form\Migration\FormMigration; use Glpi\Form\Question; -use GlpiPlugin\Advancedforms\Model\QuestionType\IpAddressQuestion; use GlpiPlugin\Advancedforms\Model\QuestionType\LdapQuestion; use GlpiPlugin\Advancedforms\Model\QuestionType\LdapQuestionConfig; use LogicException; @@ -46,7 +46,7 @@ final class FormcreatorLdapSelectTypeMapperTest extends MapperTestCase { public function testIpTypeMigrationWhenEnabled(): void { - /** @var \DBmysql $DB */ + /** @var DBmysql $DB */ global $DB; // Arrange: enable ip question type and add some fomrcreator data @@ -80,6 +80,7 @@ public function testIpTypeMigrationWhenEnabled(): void if (!$config instanceof LdapQuestionConfig) { throw new LogicException(); } + $this->assertEquals(123, $config->getAuthLdapId()); $this->assertEquals(456, $config->getLdapAttributeId()); $this->assertEquals( @@ -90,7 +91,7 @@ public function testIpTypeMigrationWhenEnabled(): void public function testIpTypeMigrationWhenDisabled(): void { - /** @var \DBmysql $DB */ + /** @var DBmysql $DB */ global $DB; // Arrange: add some fomrcreator data diff --git a/tests/Model/Mapper/MapperTestCase.php b/tests/Model/Mapper/MapperTestCase.php index fcd1cc3..993e551 100644 --- a/tests/Model/Mapper/MapperTestCase.php +++ b/tests/Model/Mapper/MapperTestCase.php @@ -33,6 +33,7 @@ namespace GlpiPlugin\Advancedforms\Tests\Model\Mapper; +use DBmysql; use GlpiPlugin\Advancedforms\Tests\AdvancedFormsTestCase; abstract class MapperTestCase extends AdvancedFormsTestCase @@ -41,8 +42,6 @@ public static function setUpBeforeClass(): void { global $DB; - parent::setUpBeforeClass(); - $tables = $DB->listTables('glpi\_plugin\_formcreator\_%'); foreach ($tables as $table) { $DB->dropTable($table['TABLE_NAME']); @@ -65,15 +64,13 @@ public static function tearDownAfterClass(): void foreach ($tables as $table) { $DB->dropTable($table['TABLE_NAME']); } - - parent::tearDownAfterClass(); } protected function createSimpleFormcreatorForm( string $name, array $questions, ): void { - /** @var \DBmysql $DB */ + /** @var DBmysql $DB */ global $DB; // Add form diff --git a/tests/Model/QuestionType/QuestionTypeTestCase.php b/tests/Model/QuestionType/QuestionTypeTestCase.php index 51a9979..0f72261 100644 --- a/tests/Model/QuestionType/QuestionTypeTestCase.php +++ b/tests/Model/QuestionType/QuestionTypeTestCase.php @@ -86,6 +86,7 @@ final public function testQuestionIsAvailableInTypeDropdownWhenEnabled(): void $builder = new FormBuilder("My form"); $builder->addQuestion("My question", QuestionTypeShortText::class); + $form = $this->createForm($builder); // Act: render form editor @@ -108,6 +109,7 @@ final public function testIpAddressIsNotAvailableInTypeDropdownWhenDisabled(): v // Arrange: create a form $builder = new FormBuilder("My form"); $builder->addQuestion("My question", QuestionTypeShortText::class); + $form = $this->createForm($builder); // Act: render form editor diff --git a/tests/Service/ConfigManagerTest.php b/tests/Service/ConfigManagerTest.php index 453e8b4..7857742 100644 --- a/tests/Service/ConfigManagerTest.php +++ b/tests/Service/ConfigManagerTest.php @@ -56,7 +56,7 @@ public function testQuestionTypeConfigFormWhenEnabled( // Assert: the input should be checked $testid = Toolbox::slugify($item::class); $html_disabled = (new Crawler($html_disabled)) - ->filter("[data-testid=\"feature-$testid\"]") + ->filter(sprintf('[data-testid="feature-%s"]', $testid)) ->filter('input[data-testid="feature-toggle"]') ->getNode(0) ; @@ -78,7 +78,7 @@ public function testQuestionTypeConfigFormWhenDisabled( // Assert: the input should not be checked $testid = Toolbox::slugify($item::class); $html_disabled = (new Crawler($html_disabled)) - ->filter("[data-testid=\"feature-$testid\"]") + ->filter(sprintf('[data-testid="feature-%s"]', $testid)) ->filter('input[data-testid="feature-toggle"]') ->getNode(0); $this->assertInstanceOf(DOMElement::class, $html_disabled); diff --git a/tests/Service/InstallManagerTest.php b/tests/Service/InstallManagerTest.php index c753a1a..3ffc3bf 100644 --- a/tests/Service/InstallManagerTest.php +++ b/tests/Service/InstallManagerTest.php @@ -46,6 +46,7 @@ public function testUninstallRemoveConfig(): void foreach (self::provideQuestionTypes() as $type) { $config_values[$type[0]->getConfigKey()] = 1; } + Config::setConfigurationValues('advancedforms', $config_values); // Act: uninstall plugin From 17c2ae3859b1bad7b1ec0d5d4d5fb40f9e4bdc74 Mon Sep 17 00:00:00 2001 From: Rom1-B Date: Mon, 3 Nov 2025 15:07:44 +0100 Subject: [PATCH 2/2] fix --- rector.php | 1 - tests/AdvancedFormsTestCase.php | 10 ++++++---- tests/Controller/LdapDropdownControllerTest.php | 7 +++++++ tests/Front/FrontTestCase.php | 9 +++------ tests/Model/Mapper/FormcreatorHiddenTypeMapperTest.php | 5 ++--- .../Model/Mapper/FormcreatorHostnameTypeMapperTest.php | 5 ++--- tests/Model/Mapper/FormcreatorIpTypeMapperTest.php | 5 ++--- .../Mapper/FormcreatorLdapSelectTypeMapperTest.php | 7 +++---- tests/Model/Mapper/MapperTestCase.php | 7 +++++-- tests/Model/QuestionType/QuestionTypeTestCase.php | 2 -- tests/Service/ConfigManagerTest.php | 4 ++-- tests/Service/InstallManagerTest.php | 1 - 12 files changed, 32 insertions(+), 31 deletions(-) diff --git a/rector.php b/rector.php index 6ab267c..632ef48 100644 --- a/rector.php +++ b/rector.php @@ -40,7 +40,6 @@ return RectorConfig::configure() ->withPaths([ __DIR__ . '/src', - __DIR__ . '/tests', ]) ->withPhpVersion(PhpVersion::PHP_82) ->withCache( diff --git a/tests/AdvancedFormsTestCase.php b/tests/AdvancedFormsTestCase.php index 5aea986..b35888c 100644 --- a/tests/AdvancedFormsTestCase.php +++ b/tests/AdvancedFormsTestCase.php @@ -63,6 +63,8 @@ final public static function provideQuestionTypes(): array public function setUp(): void { + parent::setUp(); + // Delete form related single instances $this->deleteSingletonInstance([ QuestionTypesManager::class, @@ -70,7 +72,10 @@ public function setUp(): void ]); } - public function tearDown(): void {} + public function tearDown(): void + { + parent::tearDown(); + } protected function enableConfigurableItem( ConfigurableItemInterface|string $item, @@ -86,7 +91,6 @@ protected function enableConfigurableItems( foreach ($items as $item) { $this->setConfigurableItemConfig($item, true); } - InitManager::getInstance()->init(); } @@ -104,7 +108,6 @@ protected function disableConfigurableItems( foreach ($items as $item) { $this->setConfigurableItemConfig($item, false); } - InitManager::getInstance()->init(); } @@ -166,7 +169,6 @@ private function deleteSingletonInstance(array $classes) $reflection_property = $reflection_class->getProperty('instance'); $reflection_property->setValue(null, null); } - if ($reflection_class->hasProperty('_instances')) { $reflection_property = $reflection_class->getProperty('_instances'); $reflection_property->setValue(null, []); diff --git a/tests/Controller/LdapDropdownControllerTest.php b/tests/Controller/LdapDropdownControllerTest.php index 55d2699..e2bd030 100644 --- a/tests/Controller/LdapDropdownControllerTest.php +++ b/tests/Controller/LdapDropdownControllerTest.php @@ -130,6 +130,13 @@ public function testInvalidConditonParameter(): void ]); } + private function testWithInvalidForm(): void + { + $this->enableConfigurableItem(LdapQuestion::class); + $ldap = $this->setupAuthLdap(); + $form = $this->createFormWithLdapQuestion($ldap); + } + private function renderRoute(array $post): Response { $controller = new LdapDropdownController(); diff --git a/tests/Front/FrontTestCase.php b/tests/Front/FrontTestCase.php index 19dec0c..3c8fd49 100644 --- a/tests/Front/FrontTestCase.php +++ b/tests/Front/FrontTestCase.php @@ -55,10 +55,10 @@ public function get(string $url, array $params = []): Crawler $_SERVER['REQUEST_URI'] = GLPI_ROOT . $url; require(GLPI_ROOT . $url); $html = ob_get_clean(); - } catch (Throwable $throwable) { + } catch (Throwable $e) { $_GET = $old_GET; ob_get_clean(); - throw $throwable; + throw $e; } finally { $_GET = $old_GET; } @@ -129,20 +129,17 @@ public function sendForm( if (count($submits) > 1) { throw new RuntimeException("Only forms with a single submit are supported"); } - $submit = $submits->getNode(0); if (!$submit instanceof DOMElement) { throw new LogicException(); // Impossible } - $payload[$submit->getAttribute('name')] = $submit->getAttribute('value'); // Insert specified payload values foreach ($form_values as $key => $value) { if (!isset($payload[$key])) { - throw new RuntimeException(sprintf("Input '%s' does not exist", $key)); + throw new RuntimeException("Input '$key' does not exist"); } - $payload[$key] = $value; } diff --git a/tests/Model/Mapper/FormcreatorHiddenTypeMapperTest.php b/tests/Model/Mapper/FormcreatorHiddenTypeMapperTest.php index e0247a2..41faa21 100644 --- a/tests/Model/Mapper/FormcreatorHiddenTypeMapperTest.php +++ b/tests/Model/Mapper/FormcreatorHiddenTypeMapperTest.php @@ -33,7 +33,6 @@ namespace GlpiPlugin\Advancedforms\Tests\Model\Mapper; -use DBmysql; use Glpi\Form\AccessControl\FormAccessControlManager; use Glpi\Form\Migration\FormMigration; use Glpi\Form\Question; @@ -44,7 +43,7 @@ final class FormcreatorHiddenTypeMapperTest extends MapperTestCase { public function testHiddenTypeMigrationWhenEnabled(): void { - /** @var DBmysql $DB */ + /** @var \DBmysql $DB */ global $DB; // Arrange: enable ip question type and add some fomrcreator data @@ -82,7 +81,7 @@ public function testHiddenTypeMigrationWhenEnabled(): void public function testHiddenTypeMigrationWhenDisabled(): void { - /** @var DBmysql $DB */ + /** @var \DBmysql $DB */ global $DB; // Arrange: add some fomrcreator data diff --git a/tests/Model/Mapper/FormcreatorHostnameTypeMapperTest.php b/tests/Model/Mapper/FormcreatorHostnameTypeMapperTest.php index 423ca1a..e046c2c 100644 --- a/tests/Model/Mapper/FormcreatorHostnameTypeMapperTest.php +++ b/tests/Model/Mapper/FormcreatorHostnameTypeMapperTest.php @@ -33,7 +33,6 @@ namespace GlpiPlugin\Advancedforms\Tests\Model\Mapper; -use DBmysql; use Glpi\Form\AccessControl\FormAccessControlManager; use Glpi\Form\Migration\FormMigration; use Glpi\Form\Question; @@ -44,7 +43,7 @@ final class FormcreatorHostnameTypeMapperTest extends MapperTestCase { public function testHostnameTypeMigrationWhenEnabled(): void { - /** @var DBmysql $DB */ + /** @var \DBmysql $DB */ global $DB; // Arrange: enable ip question type and add some fomrcreator data @@ -72,7 +71,7 @@ public function testHostnameTypeMigrationWhenEnabled(): void public function testHostnameTypeMigrationWhenDisabled(): void { - /** @var DBmysql $DB */ + /** @var \DBmysql $DB */ global $DB; // Arrange: add some fomrcreator data diff --git a/tests/Model/Mapper/FormcreatorIpTypeMapperTest.php b/tests/Model/Mapper/FormcreatorIpTypeMapperTest.php index 92b3441..b8685be 100644 --- a/tests/Model/Mapper/FormcreatorIpTypeMapperTest.php +++ b/tests/Model/Mapper/FormcreatorIpTypeMapperTest.php @@ -33,7 +33,6 @@ namespace GlpiPlugin\Advancedforms\Tests\Model\Mapper; -use DBmysql; use Glpi\Form\AccessControl\FormAccessControlManager; use Glpi\Form\Migration\FormMigration; use Glpi\Form\Question; @@ -44,7 +43,7 @@ final class FormcreatorIpTypeMapperTest extends MapperTestCase { public function testIpTypeMigrationWhenEnabled(): void { - /** @var DBmysql $DB */ + /** @var \DBmysql $DB */ global $DB; // Arrange: enable ip question type and add some fomrcreator data @@ -72,7 +71,7 @@ public function testIpTypeMigrationWhenEnabled(): void public function testIpTypeMigrationWhenDisabled(): void { - /** @var DBmysql $DB */ + /** @var \DBmysql $DB */ global $DB; // Arrange: add some fomrcreator data diff --git a/tests/Model/Mapper/FormcreatorLdapSelectTypeMapperTest.php b/tests/Model/Mapper/FormcreatorLdapSelectTypeMapperTest.php index 7b2ccdf..56d59b0 100644 --- a/tests/Model/Mapper/FormcreatorLdapSelectTypeMapperTest.php +++ b/tests/Model/Mapper/FormcreatorLdapSelectTypeMapperTest.php @@ -33,10 +33,10 @@ namespace GlpiPlugin\Advancedforms\Tests\Model\Mapper; -use DBmysql; use Glpi\Form\AccessControl\FormAccessControlManager; use Glpi\Form\Migration\FormMigration; use Glpi\Form\Question; +use GlpiPlugin\Advancedforms\Model\QuestionType\IpAddressQuestion; use GlpiPlugin\Advancedforms\Model\QuestionType\LdapQuestion; use GlpiPlugin\Advancedforms\Model\QuestionType\LdapQuestionConfig; use LogicException; @@ -46,7 +46,7 @@ final class FormcreatorLdapSelectTypeMapperTest extends MapperTestCase { public function testIpTypeMigrationWhenEnabled(): void { - /** @var DBmysql $DB */ + /** @var \DBmysql $DB */ global $DB; // Arrange: enable ip question type and add some fomrcreator data @@ -80,7 +80,6 @@ public function testIpTypeMigrationWhenEnabled(): void if (!$config instanceof LdapQuestionConfig) { throw new LogicException(); } - $this->assertEquals(123, $config->getAuthLdapId()); $this->assertEquals(456, $config->getLdapAttributeId()); $this->assertEquals( @@ -91,7 +90,7 @@ public function testIpTypeMigrationWhenEnabled(): void public function testIpTypeMigrationWhenDisabled(): void { - /** @var DBmysql $DB */ + /** @var \DBmysql $DB */ global $DB; // Arrange: add some fomrcreator data diff --git a/tests/Model/Mapper/MapperTestCase.php b/tests/Model/Mapper/MapperTestCase.php index 993e551..fcd1cc3 100644 --- a/tests/Model/Mapper/MapperTestCase.php +++ b/tests/Model/Mapper/MapperTestCase.php @@ -33,7 +33,6 @@ namespace GlpiPlugin\Advancedforms\Tests\Model\Mapper; -use DBmysql; use GlpiPlugin\Advancedforms\Tests\AdvancedFormsTestCase; abstract class MapperTestCase extends AdvancedFormsTestCase @@ -42,6 +41,8 @@ public static function setUpBeforeClass(): void { global $DB; + parent::setUpBeforeClass(); + $tables = $DB->listTables('glpi\_plugin\_formcreator\_%'); foreach ($tables as $table) { $DB->dropTable($table['TABLE_NAME']); @@ -64,13 +65,15 @@ public static function tearDownAfterClass(): void foreach ($tables as $table) { $DB->dropTable($table['TABLE_NAME']); } + + parent::tearDownAfterClass(); } protected function createSimpleFormcreatorForm( string $name, array $questions, ): void { - /** @var DBmysql $DB */ + /** @var \DBmysql $DB */ global $DB; // Add form diff --git a/tests/Model/QuestionType/QuestionTypeTestCase.php b/tests/Model/QuestionType/QuestionTypeTestCase.php index 0f72261..51a9979 100644 --- a/tests/Model/QuestionType/QuestionTypeTestCase.php +++ b/tests/Model/QuestionType/QuestionTypeTestCase.php @@ -86,7 +86,6 @@ final public function testQuestionIsAvailableInTypeDropdownWhenEnabled(): void $builder = new FormBuilder("My form"); $builder->addQuestion("My question", QuestionTypeShortText::class); - $form = $this->createForm($builder); // Act: render form editor @@ -109,7 +108,6 @@ final public function testIpAddressIsNotAvailableInTypeDropdownWhenDisabled(): v // Arrange: create a form $builder = new FormBuilder("My form"); $builder->addQuestion("My question", QuestionTypeShortText::class); - $form = $this->createForm($builder); // Act: render form editor diff --git a/tests/Service/ConfigManagerTest.php b/tests/Service/ConfigManagerTest.php index 7857742..453e8b4 100644 --- a/tests/Service/ConfigManagerTest.php +++ b/tests/Service/ConfigManagerTest.php @@ -56,7 +56,7 @@ public function testQuestionTypeConfigFormWhenEnabled( // Assert: the input should be checked $testid = Toolbox::slugify($item::class); $html_disabled = (new Crawler($html_disabled)) - ->filter(sprintf('[data-testid="feature-%s"]', $testid)) + ->filter("[data-testid=\"feature-$testid\"]") ->filter('input[data-testid="feature-toggle"]') ->getNode(0) ; @@ -78,7 +78,7 @@ public function testQuestionTypeConfigFormWhenDisabled( // Assert: the input should not be checked $testid = Toolbox::slugify($item::class); $html_disabled = (new Crawler($html_disabled)) - ->filter(sprintf('[data-testid="feature-%s"]', $testid)) + ->filter("[data-testid=\"feature-$testid\"]") ->filter('input[data-testid="feature-toggle"]') ->getNode(0); $this->assertInstanceOf(DOMElement::class, $html_disabled); diff --git a/tests/Service/InstallManagerTest.php b/tests/Service/InstallManagerTest.php index 3ffc3bf..c753a1a 100644 --- a/tests/Service/InstallManagerTest.php +++ b/tests/Service/InstallManagerTest.php @@ -46,7 +46,6 @@ public function testUninstallRemoveConfig(): void foreach (self::provideQuestionTypes() as $type) { $config_values[$type[0]->getConfigKey()] = 1; } - Config::setConfigurationValues('advancedforms', $config_values); // Act: uninstall plugin