Skip to content
Closed
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
104 changes: 104 additions & 0 deletions src/Command/DisableFeatureCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

/**
* -------------------------------------------------------------------------
* advancedforms plugin for GLPI
* -------------------------------------------------------------------------
*
* MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* -------------------------------------------------------------------------
* @copyright Copyright (C) 2025 by the advancedforms plugin team.
* @license MIT https://opensource.org/licenses/mit-license.php
* @link https://github.com/pluginsGLPI/advancedforms
* -------------------------------------------------------------------------
*/

namespace GlpiPlugin\Advancedforms\Command;

use Config;
use Glpi\Console\AbstractCommand;
use GlpiPlugin\Advancedforms\Service\ConfigManager;
use Override;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

final class DisableFeatureCommand extends AbstractCommand
{
#[Override]
protected function configure(): void
{
$this->setName('plugins:advancedforms:disable');
$this->setDescription(__('Disable a feature of the advancedforms plugin.', 'advancedforms'));
$this->setHelp(
__('This command disables a specific feature (question type) of the advancedforms plugin.', 'advancedforms')
. "\n\n"
. __('Available features:', 'advancedforms')
. "\n"
. implode("\n", array_map(
fn($type) => sprintf(' - <info>%s</info>: %s', $type::getConfigKey(), $type->getConfigTitle()),
ConfigManager::getInstance()->getConfigurableQuestionTypes(),
))
);

$this->addArgument(
'feature',
InputArgument::REQUIRED,
__('The feature config key to disable (e.g., enable_question_type_ip_address).', 'advancedforms'),
);
}

#[Override]
protected function execute(InputInterface $input, OutputInterface $output): int
{
$feature = $input->getArgument('feature');
$config_manager = ConfigManager::getInstance();

$valid_keys = array_map(
fn($type) => $type::getConfigKey(),
$config_manager->getConfigurableQuestionTypes(),
);

if (!in_array($feature, $valid_keys, true)) {
$output->writeln(sprintf(
'<error>' . __('Invalid feature key "%s".', 'advancedforms') . '</error>',
$feature,

Check failure on line 84 in src/Command/DisableFeatureCommand.php

View workflow job for this annotation

GitHub Actions / GLPI 11.0.x - php:8.2 - mariadb:10.6 / Continuous integration

Parameter #2 ...$values of function sprintf expects bool|float|int|string|null, mixed given.

Check failure on line 84 in src/Command/DisableFeatureCommand.php

View workflow job for this annotation

GitHub Actions / GLPI 11.0.x - php:8.5 - mariadb:11.8 / Continuous integration

Parameter #2 ...$values of function sprintf expects bool|float|int|string|null, mixed given.
));
$output->writeln(__('Available features:', 'advancedforms'));
foreach ($config_manager->getConfigurableQuestionTypes() as $type) {
$output->writeln(sprintf(' - <info>%s</info>: %s', $type::getConfigKey(), $type->getConfigTitle()));
}
return Command::FAILURE;
}

Config::setConfigurationValues('advancedforms', [
$feature => 0,
]);

$output->writeln(sprintf(
'<info>' . __('Feature "%s" has been disabled.', 'advancedforms') . '</info>',
$feature,
));

return Command::SUCCESS;
}
}
104 changes: 104 additions & 0 deletions src/Command/EnableFeatureCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

/**
* -------------------------------------------------------------------------
* advancedforms plugin for GLPI
* -------------------------------------------------------------------------
*
* MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* -------------------------------------------------------------------------
* @copyright Copyright (C) 2025 by the advancedforms plugin team.
* @license MIT https://opensource.org/licenses/mit-license.php
* @link https://github.com/pluginsGLPI/advancedforms
* -------------------------------------------------------------------------
*/

namespace GlpiPlugin\Advancedforms\Command;

use Config;
use Glpi\Console\AbstractCommand;
use GlpiPlugin\Advancedforms\Service\ConfigManager;
use Override;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

final class EnableFeatureCommand extends AbstractCommand
{
#[Override]
protected function configure(): void
{
$this->setName('plugins:advancedforms:enable');
$this->setDescription(__('Enable a feature of the advancedforms plugin.', 'advancedforms'));
$this->setHelp(
__('This command enables a specific feature (question type) of the advancedforms plugin.', 'advancedforms')
. "\n\n"
. __('Available features:', 'advancedforms')
. "\n"
. implode("\n", array_map(
fn($type) => sprintf(' - <info>%s</info>: %s', $type::getConfigKey(), $type->getConfigTitle()),
ConfigManager::getInstance()->getConfigurableQuestionTypes(),
))
);

$this->addArgument(
'feature',
InputArgument::REQUIRED,
__('The feature config key to enable (e.g., enable_question_type_ip_address).', 'advancedforms'),
);
}

#[Override]
protected function execute(InputInterface $input, OutputInterface $output): int
{
$feature = $input->getArgument('feature');
$config_manager = ConfigManager::getInstance();

$valid_keys = array_map(
fn($type) => $type::getConfigKey(),
$config_manager->getConfigurableQuestionTypes(),
);

if (!in_array($feature, $valid_keys, true)) {
$output->writeln(sprintf(
'<error>' . __('Invalid feature key "%s".', 'advancedforms') . '</error>',
$feature,

Check failure on line 84 in src/Command/EnableFeatureCommand.php

View workflow job for this annotation

GitHub Actions / GLPI 11.0.x - php:8.2 - mariadb:10.6 / Continuous integration

Parameter #2 ...$values of function sprintf expects bool|float|int|string|null, mixed given.

Check failure on line 84 in src/Command/EnableFeatureCommand.php

View workflow job for this annotation

GitHub Actions / GLPI 11.0.x - php:8.5 - mariadb:11.8 / Continuous integration

Parameter #2 ...$values of function sprintf expects bool|float|int|string|null, mixed given.
));
$output->writeln(__('Available features:', 'advancedforms'));
foreach ($config_manager->getConfigurableQuestionTypes() as $type) {
$output->writeln(sprintf(' - <info>%s</info>: %s', $type::getConfigKey(), $type->getConfigTitle()));
}
return Command::FAILURE;
}

Config::setConfigurationValues('advancedforms', [
$feature => 1,
]);

$output->writeln(sprintf(
'<info>' . __('Feature "%s" has been enabled.', 'advancedforms') . '</info>',
$feature,
));

return Command::SUCCESS;
}
}
85 changes: 85 additions & 0 deletions tests/Command/DisableFeatureCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

/**
* -------------------------------------------------------------------------
* advancedforms plugin for GLPI
* -------------------------------------------------------------------------
*
* MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* -------------------------------------------------------------------------
* @copyright Copyright (C) 2025 by the advancedforms plugin team.
* @license MIT https://opensource.org/licenses/mit-license.php
* @link https://github.com/pluginsGLPI/advancedforms
* -------------------------------------------------------------------------
*/

namespace GlpiPlugin\Advancedforms\Tests\Command;

use GlpiPlugin\Advancedforms\Command\DisableFeatureCommand;
use GlpiPlugin\Advancedforms\Model\Config\ConfigurableItemInterface;
use GlpiPlugin\Advancedforms\Service\ConfigManager;
use GlpiPlugin\Advancedforms\Tests\AdvancedFormsTestCase;
use PHPUnit\Framework\Attributes\DataProvider;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Tester\CommandTester;

final class DisableFeatureCommandTest extends AdvancedFormsTestCase
{
#[DataProvider('provideQuestionTypes')]
public function testDisableFeature(ConfigurableItemInterface $item): void
{
// Arrange: enable the feature first
$this->enableConfigurableItem($item);
$this->assertTrue(
ConfigManager::getInstance()->isConfigurableItemEnabled($item),
);

// Act: run the disable command
$command = new DisableFeatureCommand();
$tester = new CommandTester($command);
$tester->execute(['feature' => $item::getConfigKey()]);

// Assert: command succeeded and feature is disabled
$this->assertEquals(Command::SUCCESS, $tester->getStatusCode());
$this->assertFalse(
ConfigManager::getInstance()->isConfigurableItemEnabled($item),
);
$this->assertStringContainsString(
$item::getConfigKey(),
$tester->getDisplay(),
);
}

public function testDisableInvalidFeature(): void
{
// Act: run the disable command with an invalid feature key
$command = new DisableFeatureCommand();
$tester = new CommandTester($command);
$tester->execute(['feature' => 'invalid_feature_key']);

// Assert: command failed
$this->assertEquals(Command::FAILURE, $tester->getStatusCode());
$this->assertStringContainsString(
'invalid_feature_key',
$tester->getDisplay(),
);
}
}
85 changes: 85 additions & 0 deletions tests/Command/EnableFeatureCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

/**
* -------------------------------------------------------------------------
* advancedforms plugin for GLPI
* -------------------------------------------------------------------------
*
* MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* -------------------------------------------------------------------------
* @copyright Copyright (C) 2025 by the advancedforms plugin team.
* @license MIT https://opensource.org/licenses/mit-license.php
* @link https://github.com/pluginsGLPI/advancedforms
* -------------------------------------------------------------------------
*/

namespace GlpiPlugin\Advancedforms\Tests\Command;

use GlpiPlugin\Advancedforms\Command\EnableFeatureCommand;
use GlpiPlugin\Advancedforms\Model\Config\ConfigurableItemInterface;
use GlpiPlugin\Advancedforms\Service\ConfigManager;
use GlpiPlugin\Advancedforms\Tests\AdvancedFormsTestCase;
use PHPUnit\Framework\Attributes\DataProvider;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Tester\CommandTester;

final class EnableFeatureCommandTest extends AdvancedFormsTestCase
{
#[DataProvider('provideQuestionTypes')]
public function testEnableFeature(ConfigurableItemInterface $item): void
{
// Arrange: disable the feature first
$this->disableConfigurableItem($item);
$this->assertFalse(
ConfigManager::getInstance()->isConfigurableItemEnabled($item),
);

// Act: run the enable command
$command = new EnableFeatureCommand();
$tester = new CommandTester($command);
$tester->execute(['feature' => $item::getConfigKey()]);

// Assert: command succeeded and feature is enabled
$this->assertEquals(Command::SUCCESS, $tester->getStatusCode());
$this->assertTrue(
ConfigManager::getInstance()->isConfigurableItemEnabled($item),
);
$this->assertStringContainsString(
$item::getConfigKey(),
$tester->getDisplay(),
);
}

public function testEnableInvalidFeature(): void
{
// Act: run the enable command with an invalid feature key
$command = new EnableFeatureCommand();
$tester = new CommandTester($command);
$tester->execute(['feature' => 'invalid_feature_key']);

// Assert: command failed
$this->assertEquals(Command::FAILURE, $tester->getStatusCode());
$this->assertStringContainsString(
'invalid_feature_key',
$tester->getDisplay(),
);
}
}
Loading