Skip to content

Commit

Permalink
Merge branch '10.0/bugfixes'
Browse files Browse the repository at this point in the history
  • Loading branch information
cedric-anne committed Sep 17, 2024
2 parents 8591aa8 + e6aaf71 commit b952a20
Show file tree
Hide file tree
Showing 10 changed files with 372 additions and 17 deletions.
31 changes: 16 additions & 15 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 51 additions & 0 deletions install/migrations/update_10.0.16_to_10.0.17/rule_tickets.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/>.
*
* ---------------------------------------------------------------------
*/

/**
* @var \DBmysql $DB
* @var \Migration $migration
*/

$migration->addPostQuery(
$DB->buildUpdate(
'glpi_rulecriterias',
[
'criteria' => 'users_id_recipient',
],
[
'criteria' => '_users_id_recipient',
]
)
);
136 changes: 136 additions & 0 deletions phpunit/functional/KnowbaseItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1485,4 +1485,140 @@ public function testGetVisibilityCriteria()
$this->assertEqualsCanonicalizing($value['articles'], $names);
}
}

public function testClone()
{
$this->login();
$this->setEntity('_test_root_entity', true);

$entity = new \Entity();
$entity->getFromDBByCrit(['name' => '_test_root_entity']);

$date = date('Y-m-d H:i:s');
$_SESSION['glpi_currenttime'] = $date;

// Test item cloning
$knowbaseitem = new \KnowbaseItem();
$this->assertGreaterThan(
0,
$id = $knowbaseitem->add([
'name' => 'Test clone knowbaseitem',
'answer' => 'Test clone knowbaseitem',
'is_faq' => false
])
);

//add target
$tentity = new \Entity();
$this->assertGreaterThan(
0,
$eid = $tentity->add([
'name' => 'Test kb clone',
])
);
$kbentity = new \Entity_KnowbaseItem();
$this->assertGreaterThan(
0,
$kbentity->add([
'entities_id' => $eid,
'knowbaseitems_id' => $id
])
);

//add associated elements
$computer = new \Computer();
$this->assertGreaterThan(
0,
$cid = $computer->add([
'name' => 'Test kb clone Cpt',
'entities_id' => $entity->fields['id']
])
);
$linked_item = new \KnowbaseItem_Item();
$this->assertGreaterThan(
0,
$linked_item->add([
'itemtype' => 'Computer',
'items_id' => $cid,
'knowbaseitems_id' => $id
])
);

//add document
$document = new \Document();
$docid = (int)$document->add(['name' => 'Test link document']);
$this->assertGreaterThan(0, $docid);

$docitem = new \Document_Item();
$this->assertGreaterThan(
0,
$docitem->add([
'documents_id' => $docid,
'itemtype' => 'KnowbaseItem',
'items_id' => $id
])
);

//clone!
$kbitem = new \KnowbaseItem();
$this->assertTrue($kbitem->getFromDB($id));
$added = $kbitem->clone();
$this->assertGreaterThan(0, (int)$added);
$this->assertNotEquals($kbitem->fields['id'], $added);

$clonedKbitem = new \KnowbaseItem();
$this->assertTrue($clonedKbitem->getFromDB($added));

$fields = $kbitem->fields;

// Check the knowbaseitem values. Id and dates must be different, everything else must be equal
foreach ($fields as $k => $v) {
switch ($k) {
case 'id':
$this->assertNotEquals($kbitem->getField($k), $clonedKbitem->getField($k));
break;
case 'date_mod':
case 'date_creation':
$dateClone = new \DateTime($clonedKbitem->getField($k));
$expectedDate = new \DateTime($date);
$this->assertEquals($expectedDate, $dateClone);
break;
case 'name':
$this->assertEquals("{$kbitem->getField($k)} (copy)", $clonedKbitem->getField($k));
break;
default:
$this->assertEquals($kbitem->getField($k), $clonedKbitem->getField($k));
}
}

//check relations
$relations = [
\Entity_KnowbaseItem::class => 1,
\KnowbaseItem_Item::class => 1,
];

foreach ($relations as $relation => $expected) {
$this->assertSame(
$expected,
countElementsInTable(
$relation::getTable(),
[
'knowbaseitems_id' => $clonedKbitem->fields['id'],
]
)
);
}

//check document
$this->assertSame(
1,
countElementsInTable(
\Document_Item::getTable(),
[
'itemtype' => 'KnowbaseItem',
'items_id' => $clonedKbitem->fields['id'],
]
)
);
}
}
55 changes: 55 additions & 0 deletions phpunit/functional/RuleTicketTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1192,4 +1192,59 @@ public function testAssignLocationFromUser(): void
$this->assertEquals($expected_location_after_creation, $ticket->fields['locations_id']);
}
}

/**
* Test writer criterion in rules.
*/
public function testWriterCriterion()
{
$this->login();

$user_id = (int) getItemByTypeName('User', '_test_user', true);

$requesttypes_id = $this->createItem('RequestType', [
'name' => 'requesttype_' . __METHOD__,
])->getID();

// Create rule
$ruleticket = new \RuleTicket();
$rulecrit = new \RuleCriteria();
$ruleaction = new \RuleAction();

$ruletid = $ruleticket->add($ruletinput = [
'name' => 'test writer criterion',
'match' => 'AND',
'is_active' => 1,
'sub_type' => 'RuleTicket',
'condition' => \RuleTicket::ONADD,
'is_recursive' => 1,
]);
$this->checkInput($ruleticket, $ruletid, $ruletinput);

$crit_id = $rulecrit->add($crit_input = [
'rules_id' => $ruletid,
'criteria' => 'users_id_recipient',
'condition' => \Rule::PATTERN_IS,
'pattern' => $user_id,
]);
$this->checkInput($rulecrit, $crit_id, $crit_input);

$act_id = $ruleaction->add($act_input = [
'rules_id' => $ruletid,
'action_type' => 'assign',
'field' => 'requesttypes_id',
'value' => $requesttypes_id,
]);
$this->checkInput($ruleaction, $act_id, $act_input);

// Check ticket that trigger rule on creation
$ticket = new \Ticket();
$tickets_id = $ticket->add($ticket_input = [
'name' => __METHOD__,
'content' => __METHOD__,
]);
$this->checkInput($ticket, $tickets_id, $ticket_input);
$this->assertEquals($user_id, (int)$ticket->getField('users_id_recipient'));
$this->assertEquals($requesttypes_id, (int)$ticket->getField('requesttypes_id'));
}
}
5 changes: 5 additions & 0 deletions src/Glpi/Config/LegacyConfigurators/ConfigRest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ public function execute(): void
// Manage entity change
if (isset($_REQUEST["force_entity"]) && ($_SESSION["glpiactive_entity"] ?? -1) != $_REQUEST["force_entity"]) {
Session::changeActiveEntities($_REQUEST["force_entity"], true);
} elseif (Session::shouldReloadActiveEntities()) {
Session::changeActiveEntities(
$_SESSION["glpiactive_entity"],
$_SESSION["glpiactive_entity_recursive"]
);
}

// The user's current groups are stored in his session
Expand Down
7 changes: 5 additions & 2 deletions src/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,12 @@ public function getTabNameForItem(CommonGLPI $item, $withtemplate = 0)
$ong[4] = self::createTabEntry(__('Child groups'), $nb, $item::class);

if ($item->getField('is_itemgroup')) {
$ong[1] = self::createTabEntry(__('Used items'), 0, $item::class, 'ti ti-package');
$count = countElementsInTable(Group_Item::getTable(), ['groups_id' => $item->getID(), 'type' => Group_Item::GROUP_TYPE_NORMAL]);
$ong[1] = self::createTabEntry(__('Used items'), $count, $item::class, 'ti ti-package');
}
if ($item->getField('is_assign')) {
$ong[2] = self::createTabEntry(__('Managed items'), 0, $item::class, 'ti ti-package');
$count = countElementsInTable(Group_Item::getTable(), ['groups_id' => $item->getID(), 'type' => Group_Item::GROUP_TYPE_TECH]);
$ong[2] = self::createTabEntry(__('Managed items'), $count, $item::class, 'ti ti-package');
}
if (
$item->getField('is_usergroup')
Expand All @@ -147,6 +149,7 @@ public function getTabNameForItem(CommonGLPI $item, $withtemplate = 0)
return $ong;
}
}

return '';
}

Expand Down
Loading

0 comments on commit b952a20

Please sign in to comment.