Skip to content

Commit

Permalink
add type and model dicts
Browse files Browse the repository at this point in the history
  • Loading branch information
cconard96 committed Sep 11, 2024
1 parent 00f3fd2 commit 8f2426d
Show file tree
Hide file tree
Showing 15 changed files with 598 additions and 35 deletions.
51 changes: 51 additions & 0 deletions front/asset/ruledictionarymodel.form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?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/>.
*
* ---------------------------------------------------------------------
*/

use Glpi\Asset\AssetDefinition;
use Glpi\Http\Response;

$definition = new AssetDefinition();
$classname = array_key_exists('class', $_GET) && $definition->getFromDBBySystemName((string)$_GET['class'])
? $definition->getAssetClassName()
: null;

if ($classname === null || !class_exists($classname)) {
Response::sendError(400, 'Bad request', Response::CONTENT_TYPE_TEXT_HTML);
}

$rulecollection_class = $definition->getAssetModelDictionaryCollectionClassName();
$rulecollection = new $rulecollection_class();

include(GLPI_ROOT . "/front/rule.common.form.php");
51 changes: 51 additions & 0 deletions front/asset/ruledictionarymodel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?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/>.
*
* ---------------------------------------------------------------------
*/

use Glpi\Asset\AssetDefinition;
use Glpi\Http\Response;

$definition = new AssetDefinition();
$classname = array_key_exists('class', $_GET) && $definition->getFromDBBySystemName((string)$_GET['class'])
? $definition->getAssetClassName()
: null;

if ($classname === null || !class_exists($classname)) {
Response::sendError(400, 'Bad request', Response::CONTENT_TYPE_TEXT_HTML);
}

$rulecollection_class = $definition->getAssetModelDictionaryCollectionClassName();
$rulecollection = new $rulecollection_class();

include(GLPI_ROOT . "/front/rule.common.php");
51 changes: 51 additions & 0 deletions front/asset/ruledictionarytype.form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?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/>.
*
* ---------------------------------------------------------------------
*/

use Glpi\Asset\AssetDefinition;
use Glpi\Http\Response;

$definition = new AssetDefinition();
$classname = array_key_exists('class', $_GET) && $definition->getFromDBBySystemName((string)$_GET['class'])
? $definition->getAssetClassName()
: null;

if ($classname === null || !class_exists($classname)) {
Response::sendError(400, 'Bad request', Response::CONTENT_TYPE_TEXT_HTML);
}

$rulecollection_class = $definition->getAssetTypeDictionaryCollectionClassName();
$rulecollection = new $rulecollection_class();

include(GLPI_ROOT . "/front/rule.common.form.php");
51 changes: 51 additions & 0 deletions front/asset/ruledictionarytype.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?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/>.
*
* ---------------------------------------------------------------------
*/

use Glpi\Asset\AssetDefinition;
use Glpi\Http\Response;

$definition = new AssetDefinition();
$classname = array_key_exists('class', $_GET) && $definition->getFromDBBySystemName((string)$_GET['class'])
? $definition->getAssetClassName()
: null;

if ($classname === null || !class_exists($classname)) {
Response::sendError(400, 'Bad request', Response::CONTENT_TYPE_TEXT_HTML);
}

$rulecollection_class = $definition->getAssetTypeDictionaryCollectionClassName();
$rulecollection = new $rulecollection_class();

include(GLPI_ROOT . "/front/rule.common.php");
10 changes: 2 additions & 8 deletions phpunit/DbTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -347,14 +347,8 @@ protected function initAssetDefinition(
],
skip_fields: ['capacities', 'profiles'] // JSON encoded fields cannot be automatically checked
);
$this->assertEquals(
$capacities,
$this->callPrivateMethod($definition, 'getDecodedCapacitiesField')
);
$this->assertEquals(
$profiles,
$this->callPrivateMethod($definition, 'getDecodedProfilesField')
);
$this->assertEquals($capacities, $this->callPrivateMethod($definition, 'getDecodedCapacitiesField'));
$this->assertEquals($profiles, $this->callPrivateMethod($definition, 'getDecodedProfilesField'));

$manager = AssetDefinitionManager::getInstance();
$this->callPrivateMethod($manager, 'loadConcreteClass', $definition);
Expand Down
1 change: 1 addition & 0 deletions phpunit/GLPITestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
* ---------------------------------------------------------------------
*/

use Glpi\Asset\AssetDefinition;
use Glpi\Tests\Log\TestHandler;
use Monolog\Level;
use Monolog\Logger;
Expand Down
119 changes: 119 additions & 0 deletions phpunit/functional/Glpi/Asset/DictionaryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2024 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\Asset;

use DbTestCase;
use Rule;
use RuleCollection;
use Session;

class DictionaryTest extends DbTestCase
{
public function testInDictionaryList()
{
$this->login();

$definition = $this->initAssetDefinition();
$dictionaries = RuleCollection::getDictionnaries();

$has_model_dictionary = false;
$has_type_dictionary = false;

foreach ($dictionaries as $group) {
if ($group['type'] === 'Models') {
foreach ($group['entries'] as $entry) {
if (
$entry['label'] === $definition->getAssetModelClassName()::getTypeName(Session::getPluralNumber())
&& str_contains($entry['link'], 'front/asset/ruledictionarymodel.php?class=' . $definition->fields['system_name'])
&& $entry['icon'] === $definition->getAssetModelClassName()::getIcon()
) {
$has_model_dictionary = true;
break;
}
}
} else if ($group['type'] === 'Types') {
foreach ($group['entries'] as $entry) {
if (
$entry['label'] === $definition->getAssetTypeClassName()::getTypeName(Session::getPluralNumber())
&& str_contains($entry['link'], 'front/asset/ruledictionarytype.php?class=' . $definition->fields['system_name'])
&& $entry['icon'] === $definition->getAssetTypeClassName()::getIcon()
) {
$has_type_dictionary = true;
break;
}
}
}
}

$this->assertTrue($has_model_dictionary);
$this->assertTrue($has_type_dictionary);
}

public function testMenuContent()
{
$this->login();
$definition = $this->initAssetDefinition();
$rule_menu_content = Rule::getMenuContent();
$model_key = 'model.' . $definition->fields['system_name'];
$type_key = 'type.' . $definition->fields['system_name'];

$this->assertArrayHasKey($model_key, $rule_menu_content['dictionnary']['options']);
$this->assertArrayHasKey($type_key, $rule_menu_content['dictionnary']['options']);

$this->assertEquals(
$definition->getAssetModelClassName()::getTypeName(Session::getPluralNumber()),
$rule_menu_content['dictionnary']['options'][$model_key]['title']
);
$model_page = $rule_menu_content['dictionnary']['options'][$model_key]['page'];
$this->assertEquals('/front/asset/ruledictionarymodel.php?class=' . $definition->fields['system_name'], $model_page);
$this->assertEquals($model_page, $rule_menu_content['dictionnary']['options'][$model_key]['links']['search']);
$this->assertStringContainsString(
'/front/asset/ruledictionarymodel.form.php?class=' . $definition->fields['system_name'],
$rule_menu_content['dictionnary']['options'][$model_key]['links']['add']
);

$this->assertEquals(
$definition->getAssetTypeClassName()::getTypeName(Session::getPluralNumber()),
$rule_menu_content['dictionnary']['options'][$type_key]['title']
);
$type_page = $rule_menu_content['dictionnary']['options'][$type_key]['page'];
$this->assertEquals('/front/asset/ruledictionarytype.php?class=' . $definition->fields['system_name'], $type_page);
$this->assertEquals($type_page, $rule_menu_content['dictionnary']['options'][$type_key]['links']['search']);
$this->assertStringContainsString(
'/front/asset/ruledictionarytype.form.php?class=' . $definition->fields['system_name'],
$rule_menu_content['dictionnary']['options'][$type_key]['links']['add']
);
}
}
2 changes: 1 addition & 1 deletion phpunit/functional/HtmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ public function testGetMenuInfos()
'Item_DeviceSimcard'
];
$this->assertSame('Assets', $menu['assets']['title']);
$this->assertSame($expected, $menu['assets']['types']);
$this->assertSame($expected, array_intersect($expected, $menu['assets']['types']));

$expected = [
'Ticket',
Expand Down
2 changes: 1 addition & 1 deletion src/Glpi/Api/HL/Controller/RuleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ public function getCollections(Request $request): Response
/** @var \RuleCollection $instance */
$instance = new $collection();
if ($instance->canList()) {
$rule_class = $instance->getRuleClassName();
$rule_class = $instance::getRuleClassName();
if (str_starts_with($rule_class, 'Rule')) {
// Only handle rules from the core in the global namespace here
$visible_collections[] = [
Expand Down
Loading

0 comments on commit 8f2426d

Please sign in to comment.