Skip to content

Commit

Permalink
fix(clone): no date calculation (#17659)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rom1-B authored Aug 8, 2024
1 parent 0e6327a commit 1d56b79
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 5 deletions.
90 changes: 90 additions & 0 deletions phpunit/functional/PrinterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,94 @@ public function testDeleteByCriteria()
$nb_after = (int)countElementsInTable('glpi_logs', ['itemtype' => 'Printer', 'items_id' => $id]);
$this->assertSame($nb_before + 1, $nb_after);
}

public function testCloneFromTemplateWithInfocoms()
{
global $DB, $GLPI_CACHE;

$entity_id = getItemByTypeName('Entity', '_test_root_entity', true);

// Create Status
$state = new \State();
$state->add([
'name' => __METHOD__,
'entities_id' => $entity_id
]);
$this->assertTrue($state->getFromDB($state->getID()));

// Create template
$template = new \Printer();
$template->add([
'name' => __METHOD__,
'entities_id' => $entity_id,
'states_id' => $state->getID(),
'is_template' => 1
]);
$this->assertTrue($template->getFromDB($template->getID()));
$this->assertEquals(0, $template->getField('is_deleted'));
$this->assertEquals(0, $template->isDeleted());

// Add infocoms to template
$infocom = new \Infocom();
$infocom->add([
'items_id' => $template->getID(),
'itemtype' => 'Printer',
'warranty_duration' => 36,
]);
$this->assertTrue($infocom->getFromDB($infocom->getID()));
$this->assertEquals(0, $infocom->isDeleted());

// Create printer from template
$printer = new \Printer();
$printer_id = $template->clone();
$this->assertTrue($printer->getFromDB($printer_id));
$this->assertEquals(0, $printer->getField('is_template'));
$this->assertEquals($state->getID(), $printer->getField('states_id'));

// Check infocoms
$infocom = new \Infocom();
$infocom->getFromDBByCrit([
'itemtype' => \Printer::getType(),
'items_id' => $printer_id,
]);
$this->assertTrue($infocom->getFromDB($infocom->getID()));
$this->assertEquals(36, $infocom->getField('warranty_duration'));
$this->assertEquals(null, $infocom->getField('delivery_date'));
$this->assertEquals(null, $infocom->getField('warranty_date'));

// Update entity config
$state_param = \Infocom::ON_STATUS_CHANGE . '_' . $state->getID();

$entity = new \Entity();
$DB->update(
\Entity::getTable(),
[
'autofill_delivery_date' => $state_param,
'autofill_warranty_date' => \Infocom::COPY_DELIVERY_DATE,
],
['id' => $entity_id]
);
$GLPI_CACHE->clear();
$this->assertTrue($entity->getFromDB($entity_id));
$this->assertEquals($state_param, $entity->getField('autofill_delivery_date'));
$this->assertEquals(\Infocom::COPY_DELIVERY_DATE, $entity->getField('autofill_warranty_date'));

// Create printer from template
$printer = new \Printer();
$printer_id = $template->clone();
$this->assertTrue($printer->getFromDB($printer_id));
$this->assertEquals(0, $printer->getField('is_template'));
$this->assertEquals($state->getID(), $printer->getField('states_id'));

// Check infocoms
$infocom = new \Infocom();
$infocom->getFromDBByCrit([
'itemtype' => \Printer::getType(),
'items_id' => $printer_id,
]);
$this->assertTrue($infocom->getFromDB($infocom->getID()));
$this->assertEquals(36, $infocom->getField('warranty_duration'));
$this->assertEquals(date('Y-m-d'), $infocom->getField('delivery_date')); // = today
$this->assertEquals($infocom->getField('warranty_date'), $infocom->getField('warranty_date'));
}
}
5 changes: 3 additions & 2 deletions src/CommonDBTM.php
Original file line number Diff line number Diff line change
Expand Up @@ -1380,8 +1380,9 @@ public function add(array $input, $options = [], $history = true)
if (
Infocom::canApplyOn($this)
&& isset($this->input['states_id'])
&& (!isset($this->input['is_template'])
|| !$this->input['is_template'])
&& (!isset($this->input['is_template'])
|| !$this->input['is_template'])
&& !($this->input['clone'] ?? false)
) {
//Check if we have to automatically fill dates
Infocom::manageDateOnStatusChange($this);
Expand Down
9 changes: 9 additions & 0 deletions src/Features/Clonable.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,15 @@ public function clone(array $override_input = [], bool $history = true)
if ($newID !== false) {
$new_item->cloneRelations($this, $history);
$new_item->post_clone($this, $history);

if (
\Infocom::canApplyOn($this)
&& isset($new_item->input['states_id'])
&& !($new_item->input['is_template'] ?? false)
) {
//Check if we have to automatically fill dates
\Infocom::manageDateOnStatusChange($new_item);
}
}

return $newID;
Expand Down
9 changes: 6 additions & 3 deletions src/Infocom.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ public static function manageDateOnStatusChange(CommonDBTM $item, $action_add =
public static function autofillDates(&$infocoms = [], $field = '', $action = 0, $params = [])
{

if (isset($infocoms[$field])) {
if (isset($infocoms[$field]) || is_null($infocoms[$field])) {
switch ($action) {
default:
case 0:
Expand Down Expand Up @@ -400,15 +400,18 @@ public static function getAutoManagemendDatesFields()
public function prepareInputForUpdate($input)
{

//Check if one or more dates needs to be updated
//Check if one or more dates needs to be updated
foreach (self::getAutoManagemendDatesFields() as $key => $field) {
$result = Entity::getUsedConfig($key, $this->fields['entities_id']);

//Only update date if it's empty in DB. Otherwise do nothing
//Only update date if it's empty in DB. Otherwise do nothing
if (
($result > 0)
&& !isset($this->fields[$field])
) {
if (!isset($input[$field])) {
$input[$field] = null;
}
self::autofillDates($input, $field, $result);
}
}
Expand Down

0 comments on commit 1d56b79

Please sign in to comment.