/**
  * @test
  */
 public function addAgent()
 {
     $pfAgent = new PluginFusioninventoryAgent();
     $agent_id = $pfAgent->add(array('name' => 'port004.bureau.siprossii.com-2012-12-20-16-27-27', 'device_id' => 'port004.bureau.siprossii.com-2012-12-20-16-27-27', 'computers_id' => 100));
     $this->assertNotEquals(FALSE, $agent_id);
     return $pfAgent;
 }
 /**
  * @test
  */
 public function prepareDB()
 {
     global $DB;
     $DB->connect();
     $computer = new Computer();
     $pfAgent = new PluginFusioninventoryAgent();
     $pfTask = new PluginFusioninventoryTask();
     $pfTaskjob = new PluginFusioninventoryTaskjob();
     $pfIPRange = new PluginFusioninventoryIPRange();
     // Create computers + agents
     $input = array('entities_id' => 0, 'name' => 'computer1');
     $computers_id = $computer->add($input);
     $input = array('entities_id' => 0, 'name' => 'computer1', 'version' => '{"INVENTORY":"v2.3.11"}', 'device_id' => 'computer1', 'useragent' => 'FusionInventory-Agent_v2.3.11', 'computers_id' => $computers_id);
     $pfAgent->add($input);
     $input = array('entities_id' => 0, 'name' => 'computer2');
     $computers_id = $computer->add($input);
     $input = array('entities_id' => 0, 'name' => 'computer2', 'version' => '{"INVENTORY":"v2.3.11"}', 'device_id' => 'computer2', 'useragent' => 'FusionInventory-Agent_v2.3.11', 'computers_id' => $computers_id);
     $pfAgent->add($input);
     $input = array('entities_id' => 0, 'name' => 'computer3');
     $computers_id = $computer->add($input);
     $input = array('entities_id' => 0, 'name' => 'computer3', 'version' => '{"INVENTORY":"v2.3.11"}', 'device_id' => 'computer3', 'useragent' => 'FusionInventory-Agent_v2.3.11', 'computers_id' => $computers_id);
     $pfAgent->add($input);
     // Add IPRange
     $input = array('entities_id' => 0, 'name' => 'Office', 'ip_start' => '10.0.0.1', 'ip_end' => '10.0.0.254');
     $ipranges_id = $pfIPRange->add($input);
     $input = array('entities_id' => 0, 'name' => 'Office2', 'ip_start' => '10.0.2.1', 'ip_end' => '10.0.2.254');
     $ipranges_id2 = $pfIPRange->add($input);
     // Allow all agents to do network discovery
     $query = "UPDATE `glpi_plugin_fusioninventory_agentmodules` " . " SET `is_active`='1' " . " WHERE `modulename`='NETWORKDISCOVERY'";
     $DB->query($query);
     // create task
     $input = array('entities_id' => 0, 'name' => 'network discovery', 'is_active' => 1);
     $tasks_id = $pfTask->add($input);
     // create taskjob
     $input = array('plugin_fusioninventory_tasks_id' => $tasks_id, 'entities_id' => 0, 'name' => 'discovery', 'method' => 'networkdiscovery', 'targets' => '[{"PluginFusioninventoryIPRange":"' . $ipranges_id . '"}]', 'actors' => '[{"PluginFusioninventoryAgent":"2"}]');
     $pfTaskjob->add($input);
     // create task
     $input = array('entities_id' => 0, 'name' => 'network discovery2', 'is_active' => 1);
     $tasks2_id = $pfTask->add($input);
     // create taskjob
     $input = array('plugin_fusioninventory_tasks_id' => $tasks2_id, 'entities_id' => 0, 'name' => 'discovery', 'method' => 'networkdiscovery', 'targets' => '[{"PluginFusioninventoryIPRange":"' . $ipranges_id2 . '"}]', 'actors' => '[{"PluginFusioninventoryAgent":"3"}]');
     $pfTaskjob->add($input);
 }
 /**
  * @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 prepareDB()
 {
     global $DB;
     $DB->connect();
     $_SESSION['glpiactive_entity'] = 0;
     $_SESSION["plugin_fusioninventory_entity"] = 0;
     $_SESSION["glpiname"] = 'Plugin_FusionInventory';
     $query = "INSERT INTO `glpi_entities` " . " (`id`, `name`, `entities_id`, `level`) " . " VALUES ('1', 'ent1', '0', '2')";
     $DB->query($query);
     $entities_id = 1;
     $pfAgent = new PluginFusioninventoryAgent();
     $input = array('name' => 'toto', 'entities_id' => $entities_id, 'device_id' => 'toto-device');
     $agents_id = $pfAgent->add($input);
     $config = new PluginFusioninventoryConfig();
     $config->loadCache();
     $pfEntity = new PluginFusioninventoryEntity();
     $input = array('id' => 1, 'entities_id' => 0, 'agent_base_url' => 'http://127.0.0.1/glpi085');
     $pfEntity->update($input);
     $input = array('entities_id' => $entities_id, 'agent_base_url' => 'http://10.0.2.2/glpi085');
     $pfEntity->add($input);
     // active all modules
     $query = "UPDATE `glpi_plugin_fusioninventory_agentmodules`" . " SET `is_active`='1'";
     $DB->query($query);
 }
 /**
  * @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 PrinterToInventoryWithoutIP()
 {
     self::restore_database();
     $printer = new Printer();
     $networkport = new NetworkPort();
     $networkName = new NetworkName();
     $pfPrinter = new PluginFusioninventoryPrinter();
     $pfTask = new PluginFusioninventoryTask();
     $pfTaskjob = new PluginFusioninventoryTaskjob();
     $computer = new Computer();
     $pfAgent = new PluginFusioninventoryAgent();
     $communication = new PluginFusioninventoryCommunication();
     // Create computers + agents
     $input = array('entities_id' => 0, 'name' => 'computer1');
     $computers_id = $computer->add($input);
     $input = array('entities_id' => 0, 'name' => 'computer1', 'version' => '{"INVENTORY":"v2.3.11"}', 'device_id' => 'computer1', 'useragent' => 'FusionInventory-Agent_v2.3.11', 'computers_id' => $computers_id);
     $pfAgent->add($input);
     // Create printer
     $input = array('name' => 'printer 001', 'entities_id' => 0);
     $printers_id = $printer->add($input);
     // Add port
     $networkports_id = $networkport->add(array('itemtype' => 'Printer', 'instantiation_type' => 'NetworkPortEthernet', 'items_id' => $printers_id, 'entities_id' => 0));
     $networknames_id = $networkName->add(array('entities_id' => 0, 'itemtype' => 'NetworkPort', 'items_id' => $networkports_id));
     $input = array('printers_id' => 1, 'plugin_fusioninventory_configsecurities_id' => 2);
     $pfPrinter->add($input);
     // Add task
     // create task
     $input = array('entities_id' => 0, 'name' => 'network inventory', 'is_active' => 1);
     $tasks_id = $pfTask->add($input);
     // create taskjob
     $input = array('plugin_fusioninventory_tasks_id' => $tasks_id, 'entities_id' => 0, 'name' => 'inventory', 'method' => 'networkinventory', 'targets' => '[{"Printer":"' . $printers_id . '"}]', 'actors' => '[{"PluginFusioninventoryAgent":"1"}]');
     $pfTaskjob->add($input);
     PluginFusioninventoryTask::cronTaskscheduler();
     $data = $pfTask->getJoblogs(array(1));
     // Task is prepared
     // Agent will get data
     $communication->getTaskAgent(1);
     $message = $communication->getMessage();
     $json = json_encode($message);
     $array = json_decode($json, TRUE);
     $ref = array();
     $this->assertEquals($ref, $array, 'XML of SNMP inventory task');
 }
 /**
  * Add computer in entity `ent1` (with rules)
  *
  * @test
  */
 public function AddComputer()
 {
     global $DB;
     $DB->connect();
     plugin_init_fusioninventory();
     $DB->query("INSERT INTO `glpi_entities`\n         (`id`, `name`, `entities_id`, `completename`, `level`)\n         VALUES (1, 'ent1', 0, 'Entité racine > ent1', 2)");
     $DB->query("INSERT INTO `glpi_entities`\n         (`id`, `name`, `entities_id`, `completename`, `level`)\n         VALUES (2, 'ent2', 0, 'Entité racine > ent2', 2)");
     $_SESSION['glpiactive_entity'] = 0;
     $pfiComputerInv = new PluginFusioninventoryInventoryComputerInventory();
     $computer = new Computer();
     $pfEntity = new PluginFusioninventoryEntity();
     $pfEntity->update(array('id' => 1, 'entities_id' => 0, 'transfers_id_auto' => 1));
     $a_inventory = array();
     $a_inventory['CONTENT']['HARDWARE'] = array('NAME' => 'pc1');
     $a_inventory['CONTENT']['BIOS'] = array('SSN' => 'xxyyzz');
     // * Add rule ignore
     $rule = new Rule();
     $ruleCriteria = new RuleCriteria();
     $ruleAction = new RuleAction();
     $input = array();
     $input['sub_type'] = 'PluginFusioninventoryInventoryRuleEntity';
     $input['name'] = 'pc1';
     $input['match'] = 'AND';
     $input['is_active'] = 1;
     $rules_id = $rule->add($input);
     $input = array();
     $input['rules_id'] = $rules_id;
     $input['criteria'] = 'name';
     $input['condition'] = 0;
     $input['pattern'] = 'pc1';
     $ruleCriteria->add($input);
     $input = array();
     $input['rules_id'] = $rules_id;
     $input['action_type'] = 'assign';
     $input['field'] = 'entities_id';
     $input['value'] = 1;
     $ruleAction->add($input);
     // ** 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("pc-2013-02-13", "", $a_inventory);
     // creation
     $computer->getFromDB(1);
     $this->assertEquals(1, $computer->fields['entities_id'], 'Add computer');
     $this->AgentEntity(1, 1, 'Add computer on entity 1');
     // ** Update
     $pfiComputerInv->import("pc-2013-02-13", "", $a_inventory);
     // update
     $nbComputers = countElementsInTable("glpi_computers");
     $this->assertEquals(1, $nbComputers, 'Nb computer for update computer');
     $computer->getFromDB(1);
     $this->assertEquals(1, $computer->fields['entities_id'], 'Update computer');
     $this->AgentEntity(1, 1, 'Update computer on entity 1 (not changed)');
 }
 /**
  * Import agent : create if not exist and update if yet exist
  *
  * @param $p_xml simpleXMLobject
  *
  **/
 function importToken($arrayinventory)
 {
     if (isset($arrayinventory['DEVICEID'])) {
         $pfAgent = new PluginFusioninventoryAgent();
         $a_agent = $pfAgent->find("`device_id`='" . $arrayinventory['DEVICEID'] . "'", "", "1");
         if (empty($a_agent)) {
             $a_input = array();
             if (isset($arrayinventory['TOKEN'])) {
                 $a_input['token'] = $arrayinventory['TOKEN'];
             }
             $a_input['name'] = $arrayinventory['DEVICEID'];
             $a_input['device_id'] = $arrayinventory['DEVICEID'];
             $a_input['entities_id'] = 0;
             $a_input['last_contact'] = date("Y-m-d H:i:s");
             if (isset($_SERVER['HTTP_USER_AGENT'])) {
                 $a_input['useragent'] = $_SERVER['HTTP_USER_AGENT'];
             }
             return $pfAgent->add($a_input);
         } else {
             foreach ($a_agent as $data) {
                 $input = array();
                 $input['id'] = $data['id'];
                 if (isset($arrayinventory['TOKEN'])) {
                     $input['token'] = $arrayinventory['TOKEN'];
                 }
                 $input['last_contact'] = date("Y-m-d H:i:s");
                 if (isset($_SERVER['HTTP_USER_AGENT'])) {
                     $input['useragent'] = $_SERVER['HTTP_USER_AGENT'];
                 }
                 $pfAgent->update($input);
                 return $data['id'];
             }
         }
     }
     return;
 }
 /**
  * @test
  */
 public function prepareTaskWithNewComputer()
 {
     global $DB;
     // Verify add new agent when have new computer (dynamic group) in deploy task
     $DB->connect();
     $computer = new Computer();
     $pfAgent = new PluginFusioninventoryAgent();
     $input = array('entities_id' => 0, 'name' => 'computer4');
     $computers_id = $computer->add($input);
     $input = array('entities_id' => 0, 'name' => 'computer4', 'version' => '{"INVENTORY":"v2.3.11"}', 'device_id' => 'computer4', 'useragent' => 'FusionInventory-Agent_v2.3.11', 'computers_id' => $computers_id);
     $pfAgent->add($input);
     PluginFusioninventoryTask::cronTaskscheduler();
     $pfTask = new PluginFusioninventoryTask();
     $data = $pfTask->getJoblogs(array(1));
     $ref = array(1 => 'portdavid', 2 => 'computer2', 3 => 'computer3', 4 => 'computer4');
     $this->assertEquals($ref, $data['agents']);
 }
 protected function setUp()
 {
     parent::setUp();
     self::restore_database();
     // Add some computers
     $computer = new Computer();
     $pfAgent = new PluginFusioninventoryAgent();
     $computer->add(array('name' => 'pc01', 'entities_id' => 0));
     $pfAgent->add(array('computers_id' => 1, 'entities_id' => 0));
     $computer->add(array('name' => 'pc02', 'entities_id' => 0));
     $pfAgent->add(array('computers_id' => 2, 'entities_id' => 0));
     $computer->add(array('name' => 'pc03', 'entities_id' => 0));
     $pfAgent->add(array('computers_id' => 3, 'entities_id' => 0));
     $computer->add(array('name' => 'pc04', 'entities_id' => 0));
     $pfAgent->add(array('computers_id' => 4, 'entities_id' => 0));
     $computer->add(array('name' => 'pc05', 'entities_id' => 0));
     $pfAgent->add(array('computers_id' => 5, 'entities_id' => 0));
     $computer->add(array('name' => 'pc06', 'entities_id' => 0));
     $pfAgent->add(array('computers_id' => 6, 'entities_id' => 0));
     $computer->add(array('name' => 'pc07', 'entities_id' => 0));
     $pfAgent->add(array('computers_id' => 7, 'entities_id' => 0));
     $computer->add(array('name' => 'pc08', 'entities_id' => 0));
     $pfAgent->add(array('computers_id' => 8, 'entities_id' => 0));
     $computer->add(array('name' => 'pc09', 'entities_id' => 0));
     $pfAgent->add(array('computers_id' => 9, 'entities_id' => 0));
     $computer->add(array('name' => 'pc10', 'entities_id' => 0));
     $pfAgent->add(array('computers_id' => 10, 'entities_id' => 0));
     $computer->add(array('name' => 'pc11', 'entities_id' => 0));
     $pfAgent->add(array('computers_id' => 11, 'entities_id' => 0));
     $computer->add(array('name' => 'pc12', 'entities_id' => 0));
     $pfAgent->add(array('computers_id' => 12, 'entities_id' => 0));
     $computer->add(array('name' => 'pc13', 'entities_id' => 0));
     $pfAgent->add(array('computers_id' => 13, 'entities_id' => 0));
     $computer->add(array('name' => 'srv01', 'entities_id' => 0));
     $pfAgent->add(array('computers_id' => 14, 'entities_id' => 0));
     $computer->add(array('name' => 'srv02', 'entities_id' => 0));
     $pfAgent->add(array('computers_id' => 15, 'entities_id' => 0));
     $computer->add(array('name' => 'srv03', 'entities_id' => 0));
     $pfAgent->add(array('computers_id' => 16, 'entities_id' => 0));
     $computer->add(array('name' => 'srv04', 'entities_id' => 0));
     $pfAgent->add(array('computers_id' => 17, 'entities_id' => 0));
     $computer->add(array('name' => 'srv05', 'entities_id' => 0));
     $pfAgent->add(array('computers_id' => 18, 'entities_id' => 0));
 }
 /**
  * @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);
 }