diff --git a/phpunit/functional/Glpi/Inventory/Assets/AntivirusTest.php b/phpunit/functional/Glpi/Inventory/Assets/AntivirusTest.php index 990e7958872..e4b6f9023db 100644 --- a/phpunit/functional/Glpi/Inventory/Assets/AntivirusTest.php +++ b/phpunit/functional/Glpi/Inventory/Assets/AntivirusTest.php @@ -350,5 +350,175 @@ public function testInventoryUpdate() $this->assertTrue($antivirus->getFromDB($antivirus_3_id)); $this->assertSame(0, $antivirus->fields['is_dynamic']); + + //remove all antiviruses from inventory + $xml_source = " + + + + pc002 + + + ggheb7ne7 + + FusionInventory-Agent_v2.3.19 + + test-pc002 + INVENTORY +"; + + $this->doInventory($xml_source, true); + + //we now have 1 antivirus only + $results = $antivirus->find(['computers_id' => $computers_id]); + $this->assertCount(1, $results); + + } + + public function testPartialUpdate() + { + $computer = new \Computer(); + $antivirus = new \ComputerAntivirus(); + + //create manually a computer, with 3 antivirus + $computers_id = $computer->add([ + 'name' => 'pc002', + 'serial' => 'ggheb7ne7', + 'entities_id' => 0 + ]); + $this->assertGreaterThan(0, $computers_id); + + $antivirus_1_id = $antivirus->add([ + 'computers_id' => $computers_id, + 'name' => 'Kaspersky Endpoint Security 10 for Windows', + 'antivirus_version' => '2021 21.3.10.391', + 'is_active' => 1 + ]); + $this->assertGreaterThan(0, $antivirus_1_id); + + $antivirus_2_id = $antivirus->add([ + 'computers_id' => $computers_id, + 'name' => 'Microsoft Security Essentials', + 'antivirus_version' => '4.3.216.0', + 'is_active' => 1 + ]); + $this->assertGreaterThan(0, $antivirus_2_id); + + $antivirus_3_id = $antivirus->add([ + 'computers_id' => $computers_id, + 'name' => 'Avast Antivirus', + 'antivirus_version' => '19', + 'is_active' => 1 + ]); + $this->assertGreaterThan(0, $antivirus_3_id); + + $results = $antivirus->find(['computers_id' => $computers_id]); + $this->assertCount(3, $results); + foreach ($results as $result) { + $this->assertEquals(0, $result['is_dynamic']); + } + + $source = '{ + "action": "inventory", + "content": { + "hardware": { + "name": "pc002" + }, + "antivirus": [ + { + "base_version": "20200310.007", + "company": "Kaspersky", + "enabled": true, + "guid": "{B41C7598-35F6-4D89-7D0E-7ADE69B4047B}", + "name": "Kaspersky Endpoint Security 10 for Windows", + "uptodate": true, + "version": "2021 21.3.10.391" + }, + { + "company": "Microsoft Corporation", + "enabled": true, + "guid": "{641105E6-77ED-3F35-A304-765193BCB75F}", + "name": "Microsoft Security Essentials", + "uptodate": true, + "version": "4.3.216.0" + } + ], + "versionclient": "GLPI-Agent_v1.4" + }, + "deviceid": "pc.site.ru-2023-01-20-11-41-00", + "itemtype": "Computer" + }'; + + //computer inventory knows only 2 antivirus: Microsoft and Kaspersky + $this->doInventory(json_decode($source)); + + //we still have 3 antivirus linked to the computer + $results = $antivirus->find(['computers_id' => $computers_id]); + $this->assertCount(3, $results); + + //antivirus present in the inventory source are now dynamic + $results = $antivirus->find(['computers_id' => $computers_id, 'is_dynamic' => 1]); + $this->assertCount(2, $results); + + $this->assertTrue($antivirus->getFromDB($antivirus_1_id)); + $this->assertSame(1, $antivirus->fields['is_dynamic']); + + $this->assertTrue($antivirus->getFromDB($antivirus_2_id)); + $this->assertSame(1, $antivirus->fields['is_dynamic']); + + //antivirus not present in the inventory is still not dynamic + $results = $antivirus->find(['computers_id' => $computers_id, 'is_dynamic' => 0]); + $this->assertCount(1, $results); + + $this->assertTrue($antivirus->getFromDB($antivirus_3_id)); + $this->assertSame(0, $antivirus->fields['is_dynamic']); + + //Redo a partial inventory, with removed microsoft antivirus + $source = '{ + "action": "inventory", + "content": { + "hardware": { + "name": "pc002" + }, + "antivirus": [ + { + "base_version": "20200310.007", + "company": "Kaspersky", + "enabled": true, + "guid": "{B41C7598-35F6-4D89-7D0E-7ADE69B4047B}", + "name": "Kaspersky Endpoint Security 10 for Windows", + "uptodate": true, + "version": "2021 21.3.10.391" + } + ], + "versionclient": "GLPI-Agent_v1.4" + }, + "deviceid": "pc.site.ru-2023-01-20-11-41-00", + "itemtype": "Computer", + "partial": true + }'; + + $this->doInventory(json_decode($source)); + + //we now have 2 antivirus only + $results = $antivirus->find(['computers_id' => $computers_id]); + $this->assertCount(2, $results); + + //antivirus present in the inventory source are still dynamic + $results = $antivirus->find(['computers_id' => $computers_id, 'is_dynamic' => 1]); + $this->assertCount(1, $results); + + $this->assertTrue($antivirus->getFromDB($antivirus_1_id)); + $this->assertSame(1, $antivirus->fields['is_dynamic']); + + //microsoft has been removed + $this->assertFalse($antivirus->getFromDB($antivirus_2_id)); + + //antivirus not present in the inventory is still not dynamic + $results = $antivirus->find(['computers_id' => $computers_id, 'is_dynamic' => 0]); + $this->assertCount(1, $results); + + $this->assertTrue($antivirus->getFromDB($antivirus_3_id)); + $this->assertSame(0, $antivirus->fields['is_dynamic']); } } diff --git a/src/Inventory/Asset/Antivirus.php b/src/Inventory/Asset/Antivirus.php index 7e091c76414..6c6ef65fe1f 100644 --- a/src/Inventory/Asset/Antivirus.php +++ b/src/Inventory/Asset/Antivirus.php @@ -116,7 +116,7 @@ public function handle() $value = $this->data; $computerAntivirus = new ComputerAntivirus(); - //check for existing + //check for existing foreach ($value as $k => $val) { $compare = ['name' => $val->name, 'antivirus_version' => $val->antivirus_version]; $compare = array_map('strtolower', $compare); @@ -135,7 +135,7 @@ public function handle() } } - if ((!$this->main_asset || !$this->main_asset->isPartial()) && count($db_antivirus) !== 0) { + if (count($db_antivirus) !== 0) { foreach ($db_antivirus as $idtmp => $data) { if ($data['is_dynamic'] == 1) { $computerAntivirus->delete(['id' => $idtmp], true); diff --git a/src/Inventory/Asset/MainAsset.php b/src/Inventory/Asset/MainAsset.php index 2a2f236e1b2..f0ba1227110 100644 --- a/src/Inventory/Asset/MainAsset.php +++ b/src/Inventory/Asset/MainAsset.php @@ -901,7 +901,7 @@ public function rulepassed($items_id, $itemtype, $rules_id, $ports_id = 0) $input = $this->handleInput($val, $this->item); if ($this->isNew()) { - // ONADD were already exececuted, and we want to skip rules that are only ONUPDATE + // ONADD were already executed, and we want to skip rules that are only ONUPDATE $input['_skip_rules'] = true; } diff --git a/src/Inventory/Inventory.php b/src/Inventory/Inventory.php index 3cba2f5cd17..ddf86e87fc2 100644 --- a/src/Inventory/Inventory.php +++ b/src/Inventory/Inventory.php @@ -291,7 +291,8 @@ function ($property_name) { $empty_props = [ 'virtualmachines', 'remote_mgmt', - 'monitors' + 'monitors', + 'antivirus', ]; }