/** * @test * @dataProvider dataprovider */ public function AddComputer($data) { $_SESSION['glpiactive_entity'] = 0; $_SESSION['glpiactiveentities_string'] = 0; $_SESSION['glpishowallentities'] = 1; $_SESSION['glpiname'] = 'glpi'; $pfiComputerInv = new PluginFusioninventoryInventoryComputerInventory(); $inventory = array(); $inventory['CONTENT'] = $data['inventory']['CONTENT']; // ** Add agent $pfAgent = new PluginFusioninventoryAgent(); $agent_name = $data['inventory']['AGENT']['name']; $computer_name = $data['inventory']['CONTENT']['HARDWARE']['NAME']; $agents_id = $pfAgent->add($data['inventory']['AGENT']); $_SESSION['plugin_fusioninventory_agents_id'] = $agents_id; // ** Add $pfiComputerInv->import($data['inventory']['AGENT']['device_id'], "", $inventory); // creation $this->CountSoftwares($data); $this->CountVersions($data); }
/** * @test */ public function PeripheralUniqueSerialimport() { global $DB; $DB->connect(); self::restore_database(); $_SESSION['glpiactive_entity'] = 0; $_SESSION["plugin_fusioninventory_entity"] = 0; $_SESSION["glpiname"] = 'Plugin_FusionInventory'; $pfConfig = new PluginFusioninventoryConfig(); $pfiComputerLib = new PluginFusioninventoryInventoryComputerLib(); $computer = new Computer(); $GLPIlog = new GLPIlogs(); $pxml = @simplexml_load_string($this->a_computer1_XML, 'SimpleXMLElement', LIBXML_NOCDATA); $arrayinventory = PluginFusioninventoryFormatconvert::XMLtoArray($pxml); $agent = new PluginFusioninventoryAgent(); $agents_id = $agent->importToken($arrayinventory); $_SESSION['plugin_fusioninventory_agents_id'] = $agents_id; $pfInventoryComputerInventory = new PluginFusioninventoryInventoryComputerInventory(); $pfInventoryComputerInventory->import('deviceid', $arrayinventory['CONTENT'], $arrayinventory); $computer->getFromDB(1); $this->assertEquals('ggheb7ne7', $computer->fields['serial'], 'Computer not updated correctly'); $a_ref = array(1 => array('name' => 'Périphérique USB composite', 'id' => '1', 'serial' => '10075973', 'peripheraltypes_id' => '0', 'peripheralmodels_id' => '0', 'manufacturers_id' => '2', 'is_global' => '0', 'is_deleted' => '0', 'is_template' => '0', 'is_dynamic' => '0'), 2 => array('name' => 'H5321 gw Mobile Broadband Device', 'id' => '2', 'serial' => '187A047919938CM0', 'peripheraltypes_id' => '0', 'peripheralmodels_id' => '0', 'manufacturers_id' => '7', 'is_global' => '0', 'is_deleted' => '0', 'is_template' => '0', 'is_dynamic' => '0'), 3 => array('name' => 'Périphérique d’entrée USB', 'id' => '3', 'serial' => 'STM32_EMOTION2', 'peripheraltypes_id' => '0', 'peripheralmodels_id' => '0', 'manufacturers_id' => '8', 'is_global' => '0', 'is_deleted' => '0', 'is_template' => '0', 'is_dynamic' => '0')); $a_db_peripherals = getAllDatasFromTable('glpi_peripherals'); foreach ($a_db_peripherals as $id => $data) { $data_temp = array('name' => $data['name'], 'id' => $data['id'], 'serial' => $data['serial'], 'peripheraltypes_id' => $data['peripheraltypes_id'], 'peripheralmodels_id' => $data['peripheralmodels_id'], 'manufacturers_id' => $data['manufacturers_id'], 'is_global' => $data['is_global'], 'is_deleted' => $data['is_deleted'], 'is_template' => $data['is_template'], 'is_dynamic' => $data['is_dynamic']); $a_db_peripherals[$id] = $data_temp; } $this->assertEquals($a_ref, $a_db_peripherals, 'List of peripherals'); // Update computer and may not have new values in glpi_logs $query = "SELECT * FROM `glpi_logs`\n ORDER BY `id` DESC LIMIT 1"; $result = $DB->query($query); $data = $DB->fetch_assoc($result); $last_id = $data['id']; $pfInventoryComputerInventory->import('deviceid', $arrayinventory['CONTENT'], $arrayinventory); $data = getAllDatasFromTable('glpi_logs', "`id`>'" . $last_id . "'"); $this->assertEquals(array(), $data, 'On update peripherals, may not have new lines in glpi_logs'); }
/** * @test */ public function AddComputerStep3() { global $DB; $this->mark_incomplete(); return; // TODO: recode this test $DB->connect(); $DB->query("UPDATE `glpi_entities`\n SET `entities_id_software` = '-2'\n WHERE `id`='1'"); $_SESSION['glpiactive_entity'] = 0; $_SESSION['glpiactiveentities_string'] = 0; $_SESSION['glpishowallentities'] = 1; $_SESSION['glpiname'] = 'glpi'; $pfiComputerInv = new PluginFusioninventoryInventoryComputerInventory(); $computer = new Computer(); $software = new Software(); $input = array('id' => 1, 'is_recursive' => 0); $software->update($input); $computer->add(array('name' => 'pc2', 'entities_id' => 1)); $a_inventory = array(); $a_inventory['CONTENT']['HARDWARE'] = array('NAME' => 'pc2'); $a_inventory['CONTENT']['SOFTWARES'][] = array('COMMENTS' => "Non-interactive tool to get files from FTP, GOPHER, HTTP(S)", 'NAME' => "curl", 'VERSION' => "7.24.0_1"); // ** Add agent $pfAgent = new PluginFusioninventoryAgent(); $a_agents_id = $pfAgent->add(array('name' => 'pc-2013-02-13', 'device_id' => 'pc-2013-02-13')); $_SESSION['plugin_fusioninventory_agents_id'] = $a_agents_id; // ** Add $pfiComputerInv->import("pc2-2013-02-13", "", $a_inventory); // creation $computer->getFromDB(2); $this->assertEquals(1, $computer->fields['entities_id'], 'Add computer'); $nbSoftwares = countElementsInTable("glpi_softwares"); $softs = getAllDatasFromTable("glpi_softwares"); $this->assertEquals(2, $nbSoftwares, 'Nb softwares ' . print_r($softs, true)); $software->getFromDB(2); $this->assertEquals(1, $software->fields['entities_id'], "May be on entity 1"); // Software not in same entity as computer, may be recursive $this->assertEquals(0, $software->fields['is_recursive'], 'Software may have recursive = 0'); }
/** * @test */ public function PrinterDicoRename() { global $DB; $DB->connect(); $_SESSION['glpiactive_entity'] = 0; $_SESSION["plugin_fusioninventory_entity"] = 0; $_SESSION["glpiname"] = 'Plugin_FusionInventory'; $pfConfig = new PluginFusioninventoryConfig(); $computer = new Computer(); $DB->query("TRUNCATE TABLE `glpi_printers`"); $pfici = new PluginFusioninventoryInventoryComputerInventory(); $_SESSION['plugin_fusioninventory_agents_id'] = 1; $this->a_computer1_beforeformat["CONTENT"]['PRINTERS'][1]['NAME'] = 'p3'; $pfici->sendCriteria("toto", $this->a_computer1_beforeformat); $computer->getFromDB(1); $this->assertEquals('ggheb7ne7', $computer->fields['serial'], 'Computer not updated correctly'); $this->assertEquals(1, countElementsInTable('glpi_printers'), 'May have 1 printer in DB (1)'); // Test re-inventory to see if not have more than 2 printers $pfici->sendCriteria("toto", $this->a_computer1_beforeformat); $this->assertEquals(1, countElementsInTable('glpi_printers'), 'May have 1 printer in DB (2)'); $printer = new Printer(); $printer->getFromDB(1); $this->assertEquals('p3bis', $printer->fields['name'], 'Printer p3 may be renamed p3bis'); }
/** * Update computer with restrict entity (in this case computer added) * * @test */ public function updateaddComputerRestrictEntity() { global $DB; $DB->connect(); $computer = new Computer(); $pfiComputerInv = new PluginFusioninventoryInventoryComputerInventory(); // Disable all rules $DB->query("UPDATE `glpi_rules`\n SET `is_active`='0'\n WHERE `sub_type`='PluginFusioninventoryInventoryRuleImport'"); // Add rule name + restrict entity search $rulecollection = new PluginFusioninventoryInventoryRuleImportCollection(); $input = array(); $input['is_active'] = 1; $input['name'] = 'Computer name + restrict'; $input['match'] = 'AND'; $input['sub_type'] = 'PluginFusioninventoryInventoryRuleImport'; $input['ranking'] = 1; $rule_id = $rulecollection->add($input); // Add criteria $rule = $rulecollection->getRuleClass(); $rulecriteria = new RuleCriteria(get_class($rule)); $input = array(); $input['rules_id'] = $rule_id; $input['criteria'] = "name"; $input['pattern'] = 1; $input['condition'] = 10; $rulecriteria->add($input); $input = array(); $input['rules_id'] = $rule_id; $input['criteria'] = "name"; $input['pattern'] = 1; $input['condition'] = 8; $rulecriteria->add($input); $input = array(); $input['rules_id'] = $rule_id; $input['criteria'] = "entityrestrict"; $input['pattern'] = ''; $input['condition'] = 202; $rulecriteria->add($input); $input = array(); $input['rules_id'] = $rule_id; $input['criteria'] = "itemtype"; $input['pattern'] = 'Computer'; $input['condition'] = 0; $rulecriteria->add($input); // Add action $ruleaction = new RuleAction(get_class($rule)); $input = array(); $input['rules_id'] = $rule_id; $input['action_type'] = 'assign'; $input['field'] = '_fusion'; $input['value'] = '1'; $ruleaction->add($input); $a_inventory = array(); $a_inventory['CONTENT']['HARDWARE'] = array('NAME' => 'pc1'); $a_inventory['CONTENT']['BIOS'] = array('SSN' => 'xxyyzz'); $pfiComputerInv->import("pc-2013-02-13", "", $a_inventory); // Update $this->assertEquals(2, countElementsInTable('glpi_computers'), 'Must have only 2 computer'); $computer->getFromDB(2); $this->assertEquals(1, $computer->fields['entities_id'], 'Second computer added'); }
/** * If rule have found computer or rule give to create computer * * @param $items_id integer id of the computer found (or 0 if must be created) * @param $itemtype value Computer type here * * @return nothing * **/ function rulepassed($items_id, $itemtype) { global $DB, $PLUGIN_FUSIONINVENTORY_XML, $PF_ESXINVENTORY, $CFG_GLPI; PluginFusioninventoryToolbox::logIfExtradebug("pluginFusioninventory-rules", "Rule passed : " . $items_id . ", " . $itemtype . "\n"); $pfFormatconvert = new PluginFusioninventoryFormatconvert(); $a_computerinventory = $pfFormatconvert->replaceids($this->arrayinventory); $entities_id = $_SESSION["plugin_fusioninventory_entity"]; if ($itemtype == 'Computer') { $pfInventoryComputerLib = new PluginFusioninventoryInventoryComputerLib(); $pfAgent = new PluginFusioninventoryAgent(); $computer = new Computer(); if ($items_id == '0') { if ($entities_id == -1) { $entities_id = 0; $_SESSION["plugin_fusioninventory_entity"] = 0; } $_SESSION['glpiactiveentities'] = array($entities_id); $_SESSION['glpiactiveentities_string'] = $entities_id; $_SESSION['glpiactive_entity'] = $entities_id; } else { $computer->getFromDB($items_id); $a_computerinventory['Computer']['states_id'] = $computer->fields['states_id']; $input = array(); PluginFusioninventoryInventoryComputerInventory::addDefaultStateIfNeeded($input); if (isset($input['states_id'])) { $a_computerinventory['Computer']['states_id'] = $input['states_id']; } if ($entities_id == -1) { $entities_id = $computer->fields['entities_id']; $_SESSION["plugin_fusioninventory_entity"] = $computer->fields['entities_id']; } $_SESSION['glpiactiveentities'] = array($entities_id); $_SESSION['glpiactiveentities_string'] = $entities_id; $_SESSION['glpiactive_entity'] = $entities_id; if ($computer->fields['entities_id'] != $entities_id) { $pfEntity = new PluginFusioninventoryEntity(); $pfInventoryComputerComputer = new PluginFusioninventoryInventoryComputerComputer(); $moveentity = FALSE; if ($pfEntity->getValue('transfers_id_auto', $computer->fields['entities_id']) > 0) { if (!$pfInventoryComputerComputer->getLock($items_id)) { $moveentity = TRUE; } } if ($moveentity) { $pfEntity = new PluginFusioninventoryEntity(); $transfer = new Transfer(); $transfer->getFromDB($pfEntity->getValue('transfers_id_auto', $entities_id)); $item_to_transfer = array("Computer" => array($items_id => $items_id)); $transfer->moveItems($item_to_transfer, $entities_id, $transfer->fields); } else { $_SESSION["plugin_fusioninventory_entity"] = $computer->fields['entities_id']; $_SESSION['glpiactiveentities'] = array($computer->fields['entities_id']); $_SESSION['glpiactiveentities_string'] = $computer->fields['entities_id']; $_SESSION['glpiactive_entity'] = $computer->fields['entities_id']; $entities_id = $computer->fields['entities_id']; } } } $a_computerinventory = $pfFormatconvert->extraCollectInfo($a_computerinventory, $items_id); $a_computerinventory = $pfFormatconvert->computerSoftwareTransformation($a_computerinventory, $entities_id); $no_history = FALSE; // * New $setdynamic = 1; if ($items_id == '0') { $input = array(); $input['entities_id'] = $entities_id; PluginFusioninventoryInventoryComputerInventory::addDefaultStateIfNeeded($input); if (isset($input['states_id'])) { $a_computerinventory['Computer']['states_id'] = $input['states_id']; } else { $a_computerinventory['Computer']['states_id'] = 0; } $items_id = $computer->add($input); $no_history = TRUE; $setdynamic = 0; } if (isset($_SESSION['plugin_fusioninventory_locations_id'])) { $a_computerinventory['Computer']['locations_id'] = $_SESSION['plugin_fusioninventory_locations_id']; unset($_SESSION['plugin_fusioninventory_locations_id']); } $serialized = gzcompress(serialize($a_computerinventory)); $a_computerinventory['fusioninventorycomputer']['serialized_inventory'] = Toolbox::addslashes_deep($serialized); if (!$PF_ESXINVENTORY) { $pfAgent->setAgentWithComputerid($items_id, $this->device_id, $entities_id); } $pfConfig = new PluginFusioninventoryConfig(); $query = "INSERT INTO `glpi_plugin_fusioninventory_dblockinventories`\n SET `value`='" . $items_id . "'"; $CFG_GLPI["use_log_in_files"] = FALSE; if (!$DB->query($query)) { $communication = new PluginFusioninventoryCommunication(); $communication->setMessage("<?xml version='1.0' encoding='UTF-8'?>\n <REPLY>\n <ERROR>ERROR: SAME COMPUTER IS CURRENTLY UPDATED</ERROR>\n </REPLY>"); $communication->sendMessage($_SESSION['plugin_fusioninventory_compressmode']); exit; } $CFG_GLPI["use_log_in_files"] = TRUE; // * For benchs //$start = microtime(TRUE); PluginFusioninventoryInventoryComputerStat::increment(); $pfInventoryComputerLib->updateComputer($a_computerinventory, $items_id, $no_history, $setdynamic); $query = "DELETE FROM `glpi_plugin_fusioninventory_dblockinventories`\n WHERE `value`='" . $items_id . "'"; $DB->query($query); $plugin = new Plugin(); if ($plugin->isActivated('monitoring')) { Plugin::doOneHook("monitoring", "ReplayRulesForItem", array('Computer', $items_id)); } // * For benchs //Toolbox::logInFile("exetime", (microtime(TRUE) - $start)." (".$items_id.")\n". // memory_get_usage()."\n". // memory_get_usage(TRUE)."\n". // memory_get_peak_usage()."\n". // memory_get_peak_usage()."\n"); if (isset($_SESSION['plugin_fusioninventory_rules_id'])) { $pfRulematchedlog = new PluginFusioninventoryRulematchedlog(); $inputrulelog = array(); $inputrulelog['date'] = date('Y-m-d H:i:s'); $inputrulelog['rules_id'] = $_SESSION['plugin_fusioninventory_rules_id']; if (isset($_SESSION['plugin_fusioninventory_agents_id'])) { $inputrulelog['plugin_fusioninventory_agents_id'] = $_SESSION['plugin_fusioninventory_agents_id']; } $inputrulelog['items_id'] = $items_id; $inputrulelog['itemtype'] = $itemtype; $inputrulelog['method'] = 'inventory'; $pfRulematchedlog->add($inputrulelog, array(), FALSE); $pfRulematchedlog->cleanOlddata($items_id, $itemtype); unset($_SESSION['plugin_fusioninventory_rules_id']); } // Write XML file if (!empty($PLUGIN_FUSIONINVENTORY_XML)) { PluginFusioninventoryToolbox::writeXML($items_id, $PLUGIN_FUSIONINVENTORY_XML->asXML(), 'computer'); } } else { if ($itemtype == 'PluginFusioninventoryUnmanaged') { $a_computerinventory = $pfFormatconvert->computerSoftwareTransformation($a_computerinventory, $entities_id); $class = new $itemtype(); if ($items_id == "0") { if ($entities_id == -1) { $entities_id = 0; $_SESSION["plugin_fusioninventory_entity"] = 0; } $input = array(); $input['date_mod'] = date("Y-m-d H:i:s"); $items_id = $class->add($input); if (isset($_SESSION['plugin_fusioninventory_rules_id'])) { $pfRulematchedlog = new PluginFusioninventoryRulematchedlog(); $inputrulelog = array(); $inputrulelog['date'] = date('Y-m-d H:i:s'); $inputrulelog['rules_id'] = $_SESSION['plugin_fusioninventory_rules_id']; if (isset($_SESSION['plugin_fusioninventory_agents_id'])) { $inputrulelog['plugin_fusioninventory_agents_id'] = $_SESSION['plugin_fusioninventory_agents_id']; } $inputrulelog['items_id'] = $items_id; $inputrulelog['itemtype'] = $itemtype; $inputrulelog['method'] = 'inventory'; $pfRulematchedlog->add($inputrulelog); $pfRulematchedlog->cleanOlddata($items_id, $itemtype); unset($_SESSION['plugin_fusioninventory_rules_id']); } } $class->getFromDB($items_id); $_SESSION["plugin_fusioninventory_entity"] = $class->fields['entities_id']; $input = array(); $input['id'] = $class->fields['id']; // Write XML file if (!empty($PLUGIN_FUSIONINVENTORY_XML)) { PluginFusioninventoryToolbox::writeXML($items_id, $PLUGIN_FUSIONINVENTORY_XML->asXML(), 'PluginFusioninventoryUnmanaged'); } if (isset($a_computerinventory['Computer']['name'])) { $input['name'] = $a_computerinventory['Computer']['name']; } $input['item_type'] = "Computer"; if (isset($a_computerinventory['Computer']['domains_id'])) { $input['domain'] = $a_computerinventory['Computer']['domains_id']; } if (isset($a_computerinventory['Computer']['serial'])) { $input['serial'] = $a_computerinventory['Computer']['serial']; } $class->update($input); } } }
function setDynamicLinkItems($computers_id) { global $DB; $computer = new Computer(); $input = array('id' => $computers_id); PluginFusioninventoryInventoryComputerInventory::addDefaultStateIfNeeded($input); $computer->update($input); $DB->query("UPDATE `glpi_computerdisks` SET `is_dynamic`='1'\n WHERE `computers_id`='" . $computers_id . "'"); $DB->query("UPDATE `glpi_computers_items` SET `is_dynamic`='1'\n WHERE `computers_id`='" . $computers_id . "'"); $DB->query("UPDATE `glpi_computers_softwareversions` SET `is_dynamic`='1'\n WHERE `computers_id`='" . $computers_id . "'"); $DB->query("UPDATE `glpi_computervirtualmachines` SET `is_dynamic`='1'\n WHERE `computers_id`='" . $computers_id . "'"); $a_tables = array("glpi_networkports", "glpi_items_devicecases", "glpi_items_devicecontrols", "glpi_items_devicedrives", "glpi_items_devicegraphiccards", "glpi_items_deviceharddrives", "glpi_items_devicememories", "glpi_items_devicemotherboards", "glpi_items_devicenetworkcards", "glpi_items_devicepcis", "glpi_items_devicepowersupplies", "glpi_items_deviceprocessors", "glpi_items_devicesoundcards"); foreach ($a_tables as $table) { $DB->query("UPDATE `" . $table . "` SET `is_dynamic`='1'\n WHERE `items_id`='" . $computers_id . "'\n AND `itemtype`='Computer'"); } }
/** * @test * computer inventory */ public function IgnoreComputerImport() { global $DB; $DB->connect(); $_SESSION['glpiactive_entity'] = 0; $_SESSION['glpiactiveentities_string'] = 0; $_SESSION['glpishowallentities'] = 1; $_SESSION['glpiname'] = 'glpi'; $pfiComputerInv = new PluginFusioninventoryInventoryComputerInventory(); $computer = new Computer(); $pfUnmanaged = new PluginFusioninventoryUnmanaged(); $pfIgnoredimportdevice = new PluginFusioninventoryIgnoredimportdevice(); $a_inventory = array(); $a_inventory['CONTENT']['HARDWARE'] = array('NAME' => 'pc1'); $a_inventory['CONTENT']['SOFTWARES'][] = array(); // ** Add agent $pfAgent = new PluginFusioninventoryAgent(); $a_agents_id = $pfAgent->add(array('name' => 'pc-2013-02-13', 'device_id' => 'pc-2013-02-13')); $_SESSION['plugin_fusioninventory_agents_id'] = $a_agents_id; $pfiComputerInv->import("pc-2013-02-13", "", $a_inventory); // creation $a_computers = $computer->find(); $this->assertEquals(0, count($a_computers), 'Computer may not be added'); $a_unknown = $pfUnmanaged->find(); $this->assertEquals(0, count($a_unknown), 'Unmanaged may not be added'); $a_ignored = $pfIgnoredimportdevice->find(); $this->assertEquals(1, count($a_ignored), 'May have only one ignored device import'); $a_ignore = current($a_ignored); $a_reference = array('id' => '1', 'name' => 'pc1', 'itemtype' => 'Computer', 'entities_id' => '0', 'ip' => NULL, 'mac' => NULL, 'rules_id' => '48', 'method' => 'inventory', 'serial' => '', 'uuid' => ''); unset($a_ignore['date']); $this->assertEquals($a_ignore, $a_reference, 'Ignored import computer'); $pfIgnoredimportdevice->delete($a_ignore); }