Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cconard96 committed Aug 23, 2024
1 parent 145e134 commit 7ed7b33
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 5 deletions.
4 changes: 2 additions & 2 deletions phpunit/DbTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,8 @@ protected function initAssetDefinition(
],
skip_fields: ['capacities', 'profiles'] // JSON encoded fields cannot be automatically checked
);
$this->array($this->callPrivateMethod($definition, 'getDecodedCapacitiesField'))->isEqualTo($capacities);
$this->array($this->callPrivateMethod($definition, 'getDecodedProfilesField'))->isEqualTo($profiles);
$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
4 changes: 2 additions & 2 deletions src/Glpi/Asset/AssetDefinitionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -538,14 +538,14 @@ public static function getSearchURL(\$full = true)
{
/** @var array \$CFG_GLPI */
global \$CFG_GLPI;
return (\$full ? \$CFG_GLPI['root_doc'] : '') . 'asset/ruledictionarytype.php?class={$definition->fields['system_name']}';
return (\$full ? \$CFG_GLPI['root_doc'] : '') . '/front/asset/ruledictionarytype.php?class={$definition->fields['system_name']}';
}
public static function getFormURL(\$full = true)
{
/** @var array \$CFG_GLPI */
global \$CFG_GLPI;
return (\$full ? \$CFG_GLPI['root_doc'] : '') . 'asset/ruledictionarytype.form.php?class={$definition->fields['system_name']}';
return (\$full ? \$CFG_GLPI['root_doc'] : '') . '/front/asset/ruledictionarytype.form.php?class={$definition->fields['system_name']}';
}
}
PHP
Expand Down

0 comments on commit 7ed7b33

Please sign in to comment.