Skip to content

Commit

Permalink
add new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cconard96 committed Aug 29, 2024
1 parent 5c3debc commit 9135996
Show file tree
Hide file tree
Showing 6 changed files with 888 additions and 5 deletions.
9 changes: 5 additions & 4 deletions src/Dropdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -1111,13 +1111,14 @@ public static function getDeviceItemTypes()
/**
* Get the dropdown list name the user is allowed to edit
*
* @var boolean $force Force to rebuild the list of dropdowns
* @return array (group of dropdown) of array (itemtype => localized name)
**/
public static function getStandardDropdownItemTypes()
public static function getStandardDropdownItemTypes(bool $force = false)
{
static $optgroup = null;

if (is_null($optgroup)) {
if ($force || is_null($optgroup)) {
$optgroup = [
__('Common') => [
'Location' => null,
Expand Down Expand Up @@ -1322,7 +1323,7 @@ public static function getStandardDropdownItemTypes()
foreach ($optgroup as $label => &$dp) {
foreach ($dp as $key => &$val) {
if ($tmp = getItemForItemtype($key)) {
if (!$tmp->canView()) {
if (!$tmp::canView()) {
unset($optgroup[$label][$key]);
} else if ($val === null) {
$val = $key::getTypeName(Session::getPluralNumber());
Expand All @@ -1332,7 +1333,7 @@ public static function getStandardDropdownItemTypes()
}
}

if (count($optgroup[$label]) == 0) {
if (count($optgroup[$label]) === 0) {
unset($optgroup[$label]);
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/Glpi/Dropdown/DropdownTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ private function handleTreeFields(array $input): array

public function prepareInputForAdd($input)
{
if (empty($input['name'])) {
\Session::addMessageAfterRedirect(__('A name is required.'), false, ERROR);
return false;
}
$this->handleTreeFields($input);
$input = parent::prepareInputForAdd($input);
if ($input === false) {
Expand All @@ -159,6 +163,9 @@ public function prepareInputForAdd($input)

public function prepareInputForUpdate($input)
{
if (empty($input['name'])) {
unset($input['name']);
}
$this->handleTreeFields($input);
$input = parent::prepareInputForUpdate($input);
if ($input === false) {
Expand Down
46 changes: 45 additions & 1 deletion tests/DbTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

use Glpi\Asset\AssetDefinition;
use Glpi\Asset\AssetDefinitionManager;
use Glpi\Dropdown\DropdownDefinition;

class DbTestCase extends \GLPITestCase
{
Expand Down Expand Up @@ -320,8 +321,9 @@ protected function createRule(RuleBuilder $builder): Rule
/**
* Initialize a definition.
*
* @param string $system_name
* @param ?string $system_name
* @param array $capacities
* @param ?array $profiles
*
* @return AssetDefinition
*/
Expand Down Expand Up @@ -364,6 +366,48 @@ protected function initAssetDefinition(
return $definition;
}

/**
* Initialize a definition.
*
* @param ?string $system_name
* @param ?array $profiles
*
* @return DropdownDefinition
*/
protected function initDropdownDefinition(
?string $system_name = null,
?array $profiles = null,
): DropdownDefinition {
if ($profiles === null) {
// Initialize with all standard rights for super admin profile
$superadmin_p_id = getItemByTypeName(Profile::class, 'Super-Admin', true);
$profiles = [
$superadmin_p_id => ALLSTANDARDRIGHT,
];
}

$definition = $this->createItem(
DropdownDefinition::class,
[
'system_name' => $system_name ?? $this->getUniqueString(),
'is_active' => true,
'profiles' => $profiles,
],
skip_fields: ['profiles'] // JSON encoded fields cannot be automatically checked
);
$this->array($this->callPrivateMethod($definition, 'getDecodedProfilesField'))->isEqualTo($profiles);

$manager = \Glpi\Dropdown\DropdownDefinitionManager::getInstance();
$this->callPrivateMethod($manager, 'loadConcreteClass', $definition);
$this->callPrivateMethod($manager, 'boostrapConcreteClass', $definition);

// Clear definition cache
$rc = new ReflectionClass(\Glpi\Dropdown\DropdownDefinitionManager::class);
$rc->getProperty('definitions_data')->setValue(\Glpi\Dropdown\DropdownDefinitionManager::getInstance(), null);

return $definition;
}

/**
* Create a random text document.
* @return \Document
Expand Down
137 changes: 137 additions & 0 deletions tests/functional/Glpi/Dropdown/Dropdown.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php

/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2024 Teclib' and contributors.
* @copyright 2003-2014 by the INDEPNET Development Team.
* @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\Dropdown;

use DbTestCase;

class Dropdown extends DbTestCase
{
protected function getByIdProvider(): iterable
{
$foo_definition = $this->initDropdownDefinition();
$foo_classname = $foo_definition->getCustomObjectClassName();

$bar_definition = $this->initDropdownDefinition();
$bar_classname = $bar_definition->getCustomObjectClassName();

// Loop to ensure that switching between definition does not cause any issue
for ($i = 0; $i < 2; $i++) {
$fields = [
'name' => 'Foo dropdown ' . $i,
];
$dropdown = $this->createItem($foo_classname, $fields);
yield [
'id' => $dropdown->getID(),
'expected_class' => $foo_classname,
'expected_fields' => $fields,
];

$fields = [
'name' => 'Bar dropdown ' . $i,
];
$dropdown = $this->createItem($bar_classname, $fields);
yield [
'id' => $dropdown->getID(),
'expected_class' => $bar_classname,
'expected_fields' => $fields,
];
}
}

/**
* @dataProvider getByIdProvider
*/
public function testGetById(int $id, string $expected_class, array $expected_fields): void
{
$dropdown = \Glpi\Dropdown\Dropdown::getById($id);

$this->object($dropdown)->isInstanceOf($expected_class);

foreach ($expected_fields as $name => $value) {
$this->array($dropdown->fields)->hasKey($name);
$this->variable($dropdown->fields[$name])->isEqualTo($value);
}
}

public function testPrepareInputDefinition(): void
{
$definition = $this->initDropdownDefinition();
$classname = $definition->getCustomObjectClassName();
$dropdown = new $classname();

foreach (['prepareInputForAdd','prepareInputForUpdate'] as $method) {
// definition is automatically set if missing
$this->array($dropdown->{$method}(['name' => 'test']))->isEqualTo(['name' => 'test', 'dropdowns_dropdowndefinitions_id' => $definition->getID()]);

// an exception is thrown if definition is invalid
$this->exception(
function () use ($dropdown, $method, $definition) {
$dropdown->{$method}(['name' => 'test', 'dropdowns_dropdowndefinitions_id' => $definition->getID() + 1]);
}
)->message->contains('Dropdown definition does not match the current concrete class.');
}
}

public function testUpdateWithWrongDefinition(): void
{
$definition_1 = $this->initDropdownDefinition();
$classname_1 = $definition_1->getCustomObjectClassName();
$definition_2 = $this->initDropdownDefinition();
$classname_2 = $definition_2->getCustomObjectClassName();

$dropdown = $this->createItem($classname_1, ['name' => 'new dropdown']);

$this->exception(
function () use ($dropdown, $classname_2) {
$dropdown_2 = new $classname_2();
$dropdown_2->update(['id' => $dropdown->getID(), 'name' => 'updated']);
}
)->message->contains('Dropdown definition cannot be changed.');
}

public function testSearchOptionsUnicity(): void
{
$definition = $this->initDropdownDefinition();

$dropdown = $this->createItem($definition->getCustomObjectClassName(), ['name' => 'test dropdown']);

$this->when(
function () use ($dropdown) {
$this->array($dropdown->searchOptions());
}
)->error()->notExists();
}
}
Loading

0 comments on commit 9135996

Please sign in to comment.