Skip to content

Commit

Permalink
test(common): test menu redefinition
Browse files Browse the repository at this point in the history
  • Loading branch information
btry committed Feb 22, 2022
1 parent 7297624 commit d0cb564
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 68 deletions.
65 changes: 0 additions & 65 deletions hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -610,71 +610,6 @@ function plugin_formcreator_dynamicReport($params) {
return false;
}

function plugin_formcreator_redefine_menus($menus) {
if (Session::getCurrentInterface() != "helpdesk") {
return $menus;
}

if (plugin_formcreator_replaceHelpdesk() !== false) {
$newMenu = [];
$newMenu['seek_assistance'] = [
'default' => Plugin::getWebDir('formcreator', false) . '/front/wizard.php',
'title' => __('Seek assistance', 'formcreator'),
'icon' => 'fa fa-paper-plane',
];
$newMenu['my_assistance_requests'] = [
'default' => PluginFormcreatorIssue::getSearchURL(false),
'title' => __('My requests for assistance', 'formcreator'),
'icon' => 'fa fa-list',
];
if (PluginFormcreatorEntityConfig::getUsedConfig('is_kb_separated', Session::getActiveEntity()) == PluginFormcreatorEntityConfig::CONFIG_KB_DISTINCT
&& Session::haveRight('knowbase', KnowbaseItem::READFAQ)
) {
$newMenu['faq'] = $menus['faq'];
$newMenu['faq']['default'] = Plugin::getWebDir('formcreator', false) . '/front/knowbaseitem.php';
}
if (Session::haveRight("reservation", ReservationItem::RESERVEANITEM)) {
$newMenu['reservation'] = $menus['reservation'];
}
if (RSSFeed::canView()) {
$newMenu['feeds'] = [
'default' => Plugin::getWebDir('formcreator', false) . '/front/wizardfeeds.php',
'title' => __('Consult feeds', 'formcreator'),
'icon' => 'fa fa-rss',
];
}
return $newMenu;
}

// Using GLPI's helpdesk interface; then just modify the menu
$newMenus = [];
foreach ($menus as $key => $menu) {
switch ($key) {
case 'create_ticket':
$newMenus['forms'] = [
'default' => '/' . Plugin::getWebDir('formcreator', false) . '/front/formlist.php',
'title' => _n('Form', 'Forms', 2, 'formcreator'),
'icon' => 'fas fa-edit',
];
break;

case 'tickets':
$newMenus['tickets'] = [
'default' => PluginFormcreatorIssue::getSearchURL(false),
'title' => PluginFormcreatorIssue::getTypeName(Session::getPluralNumber()),
'icon' => 'fas fa-exclamation-circle',
];
break;

default:
$newMenus[$key] = $menu;
}
}
$menus = $newMenus;

return $menus;
}

function plugin_formcreator_hook_update_plugin(CommonDBTM $item) {
if ($item->fields['directory'] != 'formcreator') {
return;
Expand Down
67 changes: 67 additions & 0 deletions inc/common.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -945,4 +945,71 @@ public static function hookPostShowTab(array $options) {

$_SESSION["glpiactiveprofile"]["helpdesk_item_type"] = $_SESSION['plugin_formcreator']['helpdesk_item_type_backup'];
}

public static function hookRedefineMenu($menus) {
if (Session::getCurrentInterface() != 'helpdesk') {
return $menus;
}

if (plugin_formcreator_replaceHelpdesk() !== false) {
$newMenu = [];
$newMenu['seek_assistance'] = [
'default' => Plugin::getWebDir('formcreator', false) . '/front/wizard.php',
'title' => __('Seek assistance', 'formcreator'),
'icon' => 'fa fa-paper-plane',
];
$newMenu['my_assistance_requests'] = [
'default' => PluginFormcreatorIssue::getSearchURL(false),
'title' => __('My requests for assistance', 'formcreator'),
'icon' => 'fa fa-list',
];
if (PluginFormcreatorEntityConfig::getUsedConfig('is_kb_separated', Session::getActiveEntity()) == PluginFormcreatorEntityConfig::CONFIG_KB_DISTINCT
&& Session::haveRight('knowbase', KnowbaseItem::READFAQ)
) {
$newMenu['faq'] = $menus['faq'];
$newMenu['faq']['default'] = Plugin::getWebDir('formcreator', false) . '/front/knowbaseitem.php';
}
if (Session::haveRight("reservation", ReservationItem::RESERVEANITEM)) {
if (isset($menus['reservation'])) {
$newMenu['reservation'] = $menus['reservation'];
}
}
if (RSSFeed::canView()) {
$newMenu['feeds'] = [
'default' => Plugin::getWebDir('formcreator', false) . '/front/wizardfeeds.php',
'title' => __('Consult feeds', 'formcreator'),
'icon' => 'fa fa-rss',
];
}
return $newMenu;
}

// Using GLPI's helpdesk interface; then just modify the menu
$newMenus = [];
foreach ($menus as $key => $menu) {
switch ($key) {
case 'create_ticket':
$newMenus['forms'] = [
'default' => '/' . Plugin::getWebDir('formcreator', false) . '/front/formlist.php',
'title' => _n('Form', 'Forms', 2, 'formcreator'),
'icon' => 'fas fa-edit',
];
break;

case 'tickets':
$newMenus['tickets'] = [
'default' => PluginFormcreatorIssue::getSearchURL(false),
'title' => PluginFormcreatorIssue::getTypeName(Session::getPluralNumber()),
'icon' => 'fas fa-exclamation-circle',
];
break;

default:
$newMenus[$key] = $menu;
}
}
$menus = $newMenus;

return $menus;
}
}
2 changes: 1 addition & 1 deletion setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ function plugin_formcreator_hook() {
// Add a link in the main menu plugins for technician and admin panel
$PLUGIN_HOOKS['menu_entry']['formcreator'] = 'front/formlist.php';

$PLUGIN_HOOKS['redefine_menus']['formcreator'] = "plugin_formcreator_redefine_menus";
$PLUGIN_HOOKS['redefine_menus']['formcreator'] = [PluginFormcreatorCommon::class, 'hookRedefineMenu'];

// Config page
$links = [];
Expand Down
73 changes: 71 additions & 2 deletions tests/3-unit/PluginFormcreatorCommon.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@
*/
namespace tests\units;
use GlpiPlugin\Formcreator\Tests\CommonTestCase;
use ITILFollowup;
use KnowbaseItem;
use ReservationItem;
use Plugin;
use Session;
use Ticket;

class PluginFormcreatorCommon extends CommonTestCase {
public function beforeTestMethod($method) {
global $CFG_GLPI;

switch ($method) {
case 'testGetTicketStatusForIssue':
$this->login('glpi', 'glpi');
Expand Down Expand Up @@ -449,4 +453,69 @@ public function testGetInterface() {
$output = \PluginFormcreatorCommon::getInterface();
$this->string($output)->isEqualTo('servicecatalog');
}

public function testHookRedefineMenu() {
// Create an entity
$this->login('glpi', 'glpi');
$entity = new \Entity();
$entityId = $entity->import([
'entities_id' => '0',
'name' => __FUNCTION__ . $this->getUniqueString(),
]);
// Force creation of the entity config
\PluginFormcreatorEntityConfig::getUsedConfig('replace_helpdesk', $entityId);

// Use an not-self-service account
$this->login('glpi', 'glpi');

// Check the menu is left as is
$menus = \Html::generateMenuSession(true);
$output = \PluginFormcreatorCommon::hookRedefineMenu($menus);
$this->array($output)->isIdenticalTo($menus);

// Check taht service catalog enabled does not impacts the menu for Central users
$entityConfig = new \PluginFormcreatorEntityConfig();
$entityConfig->getFromDbByCrit(['entities_id' => $entityId]);
$this->boolean($entityConfig->isNewItem())->isFalse();
$entityConfig->update([
'id' => $entityConfig->getID(),
'replace_helpdesk' => '1',
]);
$this->login('glpi', 'glpi');

$menus = \Html::generateHelpMenu();
$output = \PluginFormcreatorCommon::hookRedefineMenu($menus);
$this->array($output)->isIdenticalTo($menus);

$this->login('post-only', 'postonly');
$menus = \Html::generateHelpMenu(true);
$expected = [
'seek_assistance' =>
[
'default' => 'plugins/formcreator/front/wizard.php',
'title' => 'Seek assistance',
'icon' => 'fa fa-paper-plane',
],
'my_assistance_requests' =>
[
'default' => '/plugins/formcreator/front/issue.php',
'title' => 'My requests for assistance',
'icon' => 'fa fa-list',
],
'reservation' =>
[
'default' => '/front/reservationitem.php',
'title' => 'Reservations',
'icon' => 'ti ti-calendar-event',
],
'feeds' =>
[
'default' => 'plugins/formcreator/front/wizardfeeds.php',
'title' => 'Consult feeds',
'icon' => 'fa fa-rss',
],
];
$output = \PluginFormcreatorCommon::hookRedefineMenu($menus);
$this->array($output)->isIdenticalTo($expected);
}
}

0 comments on commit d0cb564

Please sign in to comment.