<commit_before><?php

/*
   ------------------------------------------------------------------------
   FusionInventory
   Copyright (C) 2010-2012 by the FusionInventory Development Team.

   http://www.fusioninventory.org/   http://forge.fusioninventory.org/
   ------------------------------------------------------------------------

   LICENSE

   This file is part of FusionInventory project.

   FusionInventory is free software: you can redistribute it and/or modify
   it under the terms of the GNU Affero General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   FusionInventory 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 Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with Behaviors. If not, see <http://www.gnu.org/licenses/>.

   ------------------------------------------------------------------------

   @package   FusionInventory
   @author    <NAME>
   @co-author
   @copyright Copyright (c) 2010-2012 FusionInventory team
   @license   AGPL License 3.0 or (at your option) any later version
              http://www.gnu.org/licenses/agpl-3.0-standalone.html
   @link      http://www.fusioninventory.org/
   @link      http://forge.fusioninventory.org/projects/fusioninventory-for-glpi/
   @since     2010

   ------------------------------------------------------------------------
 */

class InventoryComputer extends PHPUnit_Framework_TestCase {


    public function testSetModuleInventoryOff() {
       global $DB,$CFG_GLPI;
       $DB->connect();

       $plugin = new Plugin();
       $plugin->getFromDBbyDir("fusioninventory");
       $plugin->activate($plugin->fields['id']);
       Plugin::load("fusioninventory");
       $plugin->getFromDBbyDir("fusinvinventory");
       $plugin->activate($plugin->fields['id']);
       Plugin::load("fusinvinventory");

       Session::loadLanguage("en_GB");

       $CFG_GLPI['root_doc'] = "http://127.0.0.1/fusion0.84/";
         //Toolbox::deleteDir(GLPI_ROOT."/files/_plugins/fusioninventory/criterias");
         //Toolbox::deleteDir(GLPI_ROOT."/files/_plugins/fusioninventory/machines");
         system("rm -fr ".GLPI_ROOT."/files/_plugins/fusioninventory/criterias");
         system("rm -fr ".GLPI_ROOT."/files/_plugins/fusioninventory/machines");


         // Add in blacklit : 30003000000000000000000000300000000000000000000000000000000000000000000000000000000000
         $pfBlacklist = new PluginFusioninventoryInventoryComputerBlacklist();
         $input = array();
         $input['plugin_fusioninventory_criterium_id'] = '1';
         $input['value'] = '30003000000000000000000000300000000000000000000000000000000000000000000000000000000000';
         $pfBlacklist->add($input);


        // set in config module inventory = yes by default
        $query = "UPDATE `glpi_plugin_fusioninventory_agentmodules`
           SET `is_active`='0'
           WHERE `modulename`='INVENTORY' ";
        $DB->query($query);

        // Activate Extra-debug
         $plugin = new Plugin();
         $data = $plugin->find("`name` = 'FusionInventory'");
         $fields = current($data);
         $plugins_id = $fields['id'];
         $pfConfig = new PluginFusioninventoryConfig();
         $pfConfig->updateValue("extradebug", "1");

    }


//    public function testSendinventoryOff() {
//       $this->testSendinventory();
//    }
//
//
//   public function testMachinesCriteriasFoldersOff() {
//      $exist = 0;
//      if (file_exists(GLPI_ROOT."/files/_plugins/fusioninventory/machines")) {
//         $exist = 1;
//      }
//      $this->assertEquals($exist, 0, 'Problem on inventory, machines & criterias folder must not create because inventory not allowed on this agent');
//   }



    public function testSetModuleInventoryOn() {
       global $DB;
       $DB->connect();

        // set in config module inventory = yes by default
        $query = "UPDATE `glpi_plugin_fusioninventory_agentmodules`
           SET `is_active` = '1'
           WHERE `modulename` = 'INVENTORY'";
        $DB->query($query);
     }



   public function testSendinventories() {
      global $DB;
      $DB->connect();
       
      $MyDirectory = opendir("InventoryComputer/xml");
      $k = 0;
      while(false !== ($Entry = readdir($MyDirectory))) {
         if(is_dir('InventoryComputer/xml/'.$Entry)&& $Entry != '.' && $Entry != '..') {
            $myVersion = opendir("InventoryComputer/xml/".$Entry);
            while(false !== ($xmlFilename = readdir($myVersion))) {
               if ($xmlFilename != '.' && $xmlFilename != '..') {
                  // We have the XML of each computer inventory
                  $xml = simplexml_load_file("InventoryComputer/xml/".$Entry."/".$xmlFilename, 'SimpleXMLElement', LIBXML_NOCDATA);
                  if ($xml->asXML()) {
echo "************************\n";
echo "Memory : ".memory_get_usage()." / ".memory_get_peak_usage()."\n";
                     $deviceid_ok = 0;
                     if (!empty($xml->DEVICEID)) {
                        $deviceid_ok = 1;
                     }
                     $this->assertEquals($deviceid_ok, 1, 'Problem on XML, DEVICEID of file InventoryComputer/xml/'.$Entry.'/'.$xmlFilename.' not good!');

                     $inputProlog = '<?xml version="1.0" encoding="UTF-8"?>
<REQUEST>
  <DEVICEID>'.$xml->DEVICEID.'</DEVICEID>
  <QUERY>PROLOG</QUERY>
  <TOKEN>NT<PASSWORD></TOKEN>
</REQUEST>';
echo "# testProlog\n";
                     $this->testProlog($inputProlog, $xml->DEVICEID);
                     $k++;
echo "# testSendinventory\n";
                     $array = $this->testSendinventory("InventoryComputer/xml/".$Entry."/".$xmlFilename, $xml);
                     $items_id = $array[0];
                     $unknown  = $array[1];
echo "# testPrinter\n";
                     $this->testPrinter("InventoryComputer/xml/".$Entry."/".$xmlFilename, $items_id, $unknown);
echo "# testMonitor\n";
                     $this->testMonitor("InventoryComputer/xml/".$Entry."/".$xmlFilename, $items_id, $unknown);
echo "# testCPU\n";
                     $this->testCPU("InventoryComputer/xml/".$Entry."/".$xmlFilename, $items_id, $unknown);
echo "# testDrive\n";
                     $this->testDrive("InventoryComputer/xml/".$Entry."/".$xmlFilename, $items_id, $unknown);
echo "# testController\n";
                     $this->testController("InventoryComputer/xml/".$Entry."/".$xmlFilename, $items_id, $unknown);
echo "# testSound\n";
                     $this->testSound("InventoryComputer/xml/".$Entry."/".$xmlFilename, $items_id, $unknown);
echo "# testVideo\n";
                     $this->testVideo("InventoryComputer/xml/".$Entry."/".$xmlFilename, $items_id, $unknown);
echo "# testMemory\n";
                     $this->testMemory("InventoryComputer/xml/".$Entry."/".$xmlFilename, $items_id, $unknown);
echo "# testNetwork\n";
                     $this->testNetwork($xml, $items_id, $unknown, "InventoryComputer/xml/".$Entry."/".$xmlFilename);
echo "# testSoftware\n";
                     $this->testSoftware("InventoryComputer/xml/".$Entry."/".$xmlFilename, $items_id, $unknown);
echo "# testHardware\n";
                     $this->testHardware("InventoryComputer/xml/".$Entry."/".$xmlFilename, $items_id, $unknown);
echo "# testHardwareModifications\n";
                     $this->testHardwareModifications("InventoryComputer/xml/".$Entry."/".$xmlFilename, $items_id);
                     echo "Number of files : ".$k."\n";

                     $GLPIlog = new GLPIlogs();
                     $GLPIlog->testSQLlogs();
                     $GLPIlog->testPHPlogs();
                  } else {
                     $this->assertEquals(0, 1, "InventoryComputer/xml/".$Entry."/".$xmlFilename." : XML not well formed");  
                  }
               }
            }
         }
      }
   }



//   public function testMachinesCriteriasFolders() {
//      $exist = 0;
//      if (file_exists(GLPI_ROOT."/files/_plugins/fusioninventory/machines")) {
//         $exist = 1;
//      }
//      $this->assertEquals($exist, 1, 'Problem on inventory, machines & criterias folder not create successfully!');
//   }


   function testProlog($inputXML='', $deviceID='') {
      global $DB;
      $DB->connect();

      if (empty($inputXML)) {
         echo "testProlog with no arguments...\n";
         return;
      }
      $emulatorAgent = new emulatorAgent;
      $emulatorAgent->server_urlpath = "/fusion0.84/plugins/fusioninventory/";
      $prologXML = $emulatorAgent->sendProlog($inputXML);
      $pfAgent = new PluginFusioninventoryAgent();
      $a_agent = $pfAgent->find("`device_id`='".$deviceID."'");
      $this->assertEquals(count($a_agent), 1, 'Problem on prolog, agent ('.$deviceID.') not right created!');

      $this->assertEquals(preg_match("/<RESPONSE>SEND<\/RESPONSE>/", $prologXML), 1, 'Prolog not send to agent!');
   }

   function testSendinventory($xmlFile='', $xml='') {

      if (empty($xmlFile)) {
         echo "testSendinventory with no arguments...\n";
         return;
      }

      $emulatorAgent = new emulatorAgent;
      $emulatorAgent->server_urlpath = "/fusion0.84/plugins/fusioninventory/";
      echo "====================\n";
      echo $xmlFile."\n";
      $input_xml = $xml->asXML();
      $returnAgent = $emulatorAgent->sendProlog($input_xml);
      echo $returnAgent."\n";

      $Computer = new Computer();
//      $xml = simplexml_load_file($xmlFile,'SimpleXMLElement', LIBXML_NOCDATA);
      if (isset($xml->CONTENT->BIOS->SSN)) {
         if ($xml->CONTENT->BIOS->SSN == '30003000000000000000000000300000000000000000000000000000000000000000000000000000000000') {
            unset($xml->CONTENT->BIOS->SSN);
         } else if ($xml->CONTENT->BIOS->SSN == 'To Be Filled By O.E.M.') {
            unset($xml->CONTENT->BIOS->SSN);
         } else {
            $xml->CONTENT->BIOS->SSN = trim($xml->CONTENT->BIOS->SSN);
         }
      }
      $serial = "(`serial` IS NULL OR `serial`='')";
      if ((isset($xml->CONTENT->BIOS->SSN)) AND (!empty($xml->CONTENT->BIOS->SSN))) {
         $serial = "`serial`='".$xml->CONTENT->BIOS->SSN."'";
      }
      $a_computers = $Computer->find("`name`='".$xml->CONTENT->HARDWARE->NAME."' AND ".$serial);
      $unknown = 0;
      if (count($a_computers) == 0) {
         // Search in unknown device
         $pfUnknownDevice = new PluginFusioninventoryUnknownDevice();
         $a_computers = $pfUnknownDevice->find("`name`='".$xml->CONTENT->HARDWARE->NAME."'");
         $unknown = 1;
      }
      $this->assertEquals(count($a_computers), 1, 'Problem on creation computer, not created ('.$xmlFile.')');
      foreach($a_computers as $items_id => $data) {
         return array($items_id, $unknown);
      }
   }


   function testPrinter($xmlFile='', $items_id=0, $unknown=0) {
      global $DB;
      $DB->connect();

      if (empty($xmlFile)) {
         echo "testPrinter with no arguments...\n";
         return;
      }
      if ($unknown == '1') {
         return;
      }

      $Printer  = new Printer();

      $xml = simplexml_load_file($xmlFile, 'SimpleXMLElement', LIBXML_NOCDATA);

      if (!isset($xml->CONTENT->PRINTERS)) {
         return;
      }
      // Verify not have 2 printer in DB with same printer serial
      foreach ($xml->CONTENT->PRINTERS as $child) {
         if (isset($child->SERIAL)) {
            $child->SERIAL = preg_replace('/\/$/', '', (string)$child->SERIAL);
            $a_printer = $Printer->find("`serial`='".$child->SERIAL."'");
            $this->assertEquals(count($a_printer), 1, 'Problem on printers, printer created "'.count($a_printer).'" instead 1 times (serial : '.$child->SERIAL.')');
         }
      }
      // Verify all printers are connected to the computer
         // Get all printers connected to computer in DB
         $query = "SELECT * FROM `glpi_computers_items`
                  INNER JOIN `glpi_printers` on `glpi_printers`.`id`=`items_id`
                      WHERE `computers_id` = '".$items_id."'
                            AND `itemtype` = 'Printer'";
         $result=$DB->query($query);
         $a_printerDB = array();
         while ($data=$DB->fetch_array($result)) {
            $a_printerDB["'".$data['name']."'"] = 1;
         }
         // Verify printers in XML
         $a_printerXML = array();
         $a_serial = array();
         foreach ($xml->CONTENT->PRINTERS as $child) {
            if (isset($child->SERIAL)) {
               if (isset($a_serial[(string)$child->SERIAL])) {
                  unset($a_printerXML[$a_serial[(string)$child->SERIAL]]);
                  $a_printerXML["'".(string)$child->NAME."'"] = 1;
               } else {
                  $a_printerXML["'".(string)$child->NAME."'"] = 1;                  
               }               
               $a_serial[(string)$child->SERIAL] = (string)$child->NAME;
            } else {
               $a_printerXML["'".(string)$child->NAME."'"] = 1;
            }
         }
         // Display (test) differences
         $a_printerDiff = array();
         $a_printerDiff = array_diff_key($a_printerDB, $a_printerXML);
         if (count($a_printerDiff) < count(array_diff_key($a_printerXML, $a_printerDB))) {
            $a_printerDiff = array_diff_key($a_printerXML, $a_printerDB);
         }
         $this->assertEquals(count($a_printerDiff), 0, 'Difference of printers "'.print_r($a_printerDiff, true).'" ['.$xmlFile.']');


         // Verify fields in GLPI
         foreach($xml->CONTENT->PRINTERS as $child) {
            if (isset($child->SERIAL)) {
               $a_printer = $Printer->find("`serial`='".$child->SERIAL."' ");
               foreach ($a_printer as $datas) {
                  if (isset($child->NAME)) {
                     $this->assertEquals(trim($child->NAME), $datas['name'], 'Difference of printers fields ['.$xmlFile.']');
                  } else if (isset($child->DRIVER)) {
                     $this->assertEquals($child->DRIVER, $datas['name'], 'Difference of printers fields ['.$xmlFile.']');
                  }
                  if (strstr($child->PORT, "USB")) {
                     $this->assertEquals("1", $datas['have_usb'], 'Difference of printers fields ['.$xmlFile.']');
                  }
                  // Find in USBDEVICES to find manufacturer
                  foreach($xml->CONTENT->USBDEVICES as $childusb) {
                     if (isset($childusb->SERIAL)) {
                        if (file_exists(GLPI_ROOT."/files/_plugins/fusioninventory/DataFilter/usbids/".strtolower($childusb->VENDORID)."/".strtolower($childusb->PRODUCTID)."info")) {
                           $info = file_get_contents(GLPI_ROOT."/files/_plugins/fusioninventory/DataFilter/usbids/".strtolower($childusb->VENDORID)."/".strtolower($childusb->PRODUCTID)."info");
                           $array = explode("\n", $info);
                           $manufacturer_id = Dropdown::importExternal('Manufacturer', $array[0]);
                           $this->assertEquals($manufacturer_id, $datas['manufacturers_id'], 'Difference of printers fields ['.$xmlFile.']');
                        }
                     }
                  }
               }
            } else {
               $query = "SELECT `glpi_printers`.* FROM `glpi_computers_items`
                        INNER JOIN `glpi_printers` on `glpi_printers`.`id`=`items_id`
                        WHERE `computers_id` = '".$items_id."'
                            AND `itemtype` = 'Printer'";
               $result=$DB->query($query);
               $printer_select = array();
               while ($data=$DB->fetch_array($result)) {
                  if (count($printer_select) == '0') {
                     if ((isset($child->NAME)) AND ($data['name'] == $child->NAME)) {
                        $printer_select = $data;
//                     } else if ((isset($child->DRIVER)) AND ($data['name'] == $child->DRIVER)) {
//                        $printer_select = $data;
                     }
                  }
               }
               $this->assertEquals(count($printer_select['id']), "1", 'Problem to find printer for fields verification ['.$xmlFile.']');
               if (strstr($child->PORT, "USB")) {
                  $this->assertEquals("1", $printer_select['have_usb'], 'Difference of printers fields ['.$xmlFile.']');
               }
            }

         }
   }


   function testMonitor($xmlFile='', $items_id=0, $unknown=0) {
      global $DB;
      $DB->connect();

      if (empty($xmlFile)) {
         echo "testMonitor with no arguments...\n";
         return;
      }
      if ($unknown == '1') {
         return;
      }

      $Monitor  = new Monitor();

      $xml = simplexml_load_file($xmlFile, 'SimpleXMLElement', LIBXML_NOCDATA);

      if (!isset($xml->CONTENT->MONITORS)) {
         return;
      }

      // Verify not have 2 monitor in DB with same printer serial
      foreach ($xml->CONTENT->MONITORS as $child) {
         if (isset($child->SERIAL)
                 AND (string)$child->SERIAL != '') {
            $a_monitor = $Monitor->find("`serial`='".$child->SERIAL."'");
            $this->assertEquals(count($a_monitor), 1, 'Problem on monitors, monitor created "'.count($a_monitor).'" instead 1 times [serial:'.$child->SERIAL.']');
         }
      }

      // Verify all monitors are connected to the computer
         // Get all monitors connected to computer in DB
         $query = "SELECT * FROM `glpi_computers_items`
                  INNER JOIN `glpi_monitors` on `glpi_monitors`.`id`=`items_id`
                      WHERE `computers_id` = '".$items_id."'
                            AND `itemtype` = 'Monitor'";
         $result=$DB->query($query);
         $a_monitorDB = array();
         while ($data=$DB->fetch_array($result)) {
            $a_monitorDB["'".$data['name']."'"] = 1;
         }
         // Verifiy monitors in XML
         $a_monitorXML = array();
         foreach ($xml->CONTENT->MONITORS as $child) {
            $a_monitorXML["'".$child->CAPTION."'"] = 1;
         }
         // Display (test) differences
         $a_monitorDiff = array();
         $a_monitorDiff = array_diff_key($a_monitorDB, $a_monitorXML);
         if (count($a_monitorDiff) < count(array_diff_key($a_monitorXML, $a_monitorDB))) {
            $a_monitorDiff = array_diff_key($a_monitorXML, $a_monitorDB);
         }
         $this->assertEquals(count($a_monitorDiff), 0, 'Difference of monitors "'.print_r($a_monitorDiff, true).'" ('.$xmlFile.')');

   }


   function testCPU($xmlFile='', $items_id=0, $unknown=0) {
      global $DB;
       $DB->connect();

      if (empty($xmlFile)) {
         echo "testCPU with no arguments...\n";
         return;
      }
      if ($unknown == '1') {
         return;
      }

      $xml = simplexml_load_file($xmlFile, 'SimpleXMLElement', LIBXML_NOCDATA);

      if (!isset($xml->CONTENT->CPUS)) {
         return;
      }

      $a_cpuXML = array();
      $a_speed = array();
      $i = 0;
      foreach ($xml->CONTENT->CPUS as $child) {
         if (isset($child->NAME)) {
            $a_cpuXML["'".$i."-".$child->NAME."'"] = 1;
            $i++;
            if (isset($child->SPEED)
                    AND count($child) > 1) {
               if (!isset($a_speed[(string)$child->SPEED])) {
                  $a_speed[(string)$child->SPEED] = 0;
               }
               $a_speed[(string)$child->SPEED]++;
            }
         } else if (isset($child->TYPE)) {
            $a_cpuXML["'".$i."-".$child->TYPE."'"] = 1;
            $i++;
            if (isset($child->SPEED)
                    AND count($child) > 1) {
               if (!isset($a_speed[(string)$child->SPEED])) {
                  $a_speed[(string)$child->SPEED] = 0;
               }
               $a_speed[(string)$child->SPEED]++;
            }
         }

      }

      $query = "SELECT * FROM `glpi_items_deviceprocessors`
         WHERE `items_id`='".$items_id."' 
            AND `itemtype`='Computer'";
      $result=$DB->query($query);

      $this->assertEquals($DB->numrows($result), count($a_cpuXML), 'Difference of CPUs, created '.$DB->numrows($result).' times instead '.count($a_cpuXML).' ['.$xmlFile.']');

      foreach ($a_speed as $speed=>$nb) {
         $query = "SELECT * FROM `glpi_items_deviceprocessors`
            WHERE `items_id`='".$items_id."'
               AND `itemtype`='Computer'
               AND `frequency`='".$speed."'";
         $result=$DB->query($query);
         $this->assertEquals($DB->numrows($result), $nb, 'Difference of Processor speed '.$speed.' ['.$xmlFile.']');
      }

   }



   function testDrive($xmlFile='', $items_id=0, $unknown=0) {
      global $DB;
       $DB->connect();

      if (empty($xmlFile)) {
         echo "testDrive with no arguments...\n";
         return;
      }
      if ($unknown == '1') {
         return;
      }

      $xml = simplexml_load_file($xmlFile, 'SimpleXMLElement', LIBXML_NOCDATA);

      if (!isset($xml->CONTENT->DRIVE)) {
         return;
      }

      $a_driveXML = array();
      $i = 0;
      foreach ($xml->CONTENT->DRIVES as $child) {
         if (isset($child->CAPTION)) {
            $a_driveXML["'".$i."-".$child->CAPTION."'"] = 1;
            $i++;
         }
      }

      $query = "SELECT * FROM `glpi_computerdisks`
         WHERE `computers_id`='".$items_id."' ";
      $result=$DB->query($query);

      $this->assertEquals($DB->numrows($result), count($a_driveXML), 'Difference of Drives, created '.$DB->numrows($result).' times instead '.count($a_driveXML).' ['.$xmlFile.']');
   }


   function testController($xmlFile='', $items_id=0, $unknown=0) {
      global $DB;
       $DB->connect();

      if (empty($xmlFile)) {
         echo "testController with no arguments...\n";
         return;
      }
      if ($unknown == '1') {
         return;
      }

      $xml = simplexml_load_file($xmlFile, 'SimpleXMLElement', LIBXML_NOCDATA);

      if (!isset($xml->CONTENT->CONTROLLERS)) {
         return;
      }

      // Controller to ignore
      $ignore_controllers = array();
      foreach ($xml->CONTENT->VIDEOS as $child) {
         $ignore_controllers["'".$child->NAME."'"] = 1;
         if (isset($child->CHIPSET)) {
            $ignore_controllers["'".$child->CHIPSET."'"] = 1;
         }
      }
      foreach ($xml->CONTENT->SOUNDS as $child) {
         $ignore_controllers["'".$child->NAME."'"] = 1;
      }

      $a_controllerXML = array();
      $i = 0;
      foreach ($xml->CONTENT->CONTROLLERS as $child) {
         if ((isset($child->NAME))
                 AND (!isset($ignore_controllers["'".$child->NAME."'"]))) {
            if (!(isset($child->NAME)
                    AND isset($child->CAPTION)
                    AND isset($child->TYPE)
                    AND empty($child->NAME)
                    AND empty($child->CAPTION)
                    AND empty($child->TYPE))) {

               $a_controllerXML["'".$i."-".$child->NAME."'"] = 1;
               $i++;
            }
         }
      }

      $query = "SELECT * FROM `glpi_items_devicecontrols`
         WHERE `items_id`='".$items_id."' 
            AND `itemtype`='Computer'";
      $result=$DB->query($query);

      $this->assertEquals($DB->numrows($result), count($a_controllerXML), 'Difference of Controllers, created '.$DB->numrows($result).' times instead '.count($a_controllerXML).' ['.$xmlFile.']');
   }


   function testSound($xmlFile='', $items_id=0, $unknown=0) {
      global $DB;
       $DB->connect();

      if (empty($xmlFile)) {
         echo "testSound with no arguments...\n";
         return;
      }
      if ($unknown == '1') {
         return;
      }

      $xml = simplexml_load_file($xmlFile, 'SimpleXMLElement', LIBXML_NOCDATA);

      if (!isset($xml->CONTENT->SOUNDS)) {
         return;
      }

      $a_soundXML = array();
      $i = 0;
      foreach ($xml->CONTENT->SOUNDS as $child) {
         if (isset($child->NAME)) {
            $a_soundXML["'".$i."-".$child->NAME."'"] = 1;
            $i++;
         }
      }

      $query = "SELECT * FROM `glpi_items_devicesoundcards`
         WHERE `items_id`='".$items_id."' 
            AND `itemtype`='Computer'";
      $result=$DB->query($query);

      $this->assertEquals($DB->numrows($result), count($a_soundXML), 'Difference of Sounds, created '.$DB->numrows($result).' times instead '.count($a_soundXML).' ['.$xmlFile.']');
   }


  function testVideo($xmlFile='', $items_id=0, $unknown=0) {
      global $DB;
       $DB->connect();

      if (empty($xmlFile)) {
         echo "testVideo with no arguments...\n";
         return;
      }
      if ($unknown == '1') {
         return;
      }

      $xml = simplexml_load_file($xmlFile, 'SimpleXMLElement', LIBXML_NOCDATA);

      if (!isset($xml->CONTENT->VIDEOS)) {
         return;
      }

      $a_videoXML = array();
      $i = 0;
      foreach ($xml->CONTENT->VIDEOS as $child) {
         if (isset($child->NAME)) {
            $a_videoXML["'".$i."-".$child->NAME."'"] = 1;
            $i++;
         }
      }

      $query = "SELECT * FROM `glpi_items_devicegraphiccards`
         WHERE `items_id`='".$items_id."' 
            AND `itemtype`='Computer'";
      $result=$DB->query($query);

      $this->assertEquals($DB->numrows($result), count($a_videoXML), 'Difference of Videos, created '.$DB->numrows($result).' times instead '.count($a_videoXML).' ['.$xmlFile.']');
   }



   function testMemory($xmlFile='', $items_id=0, $unknown=0) {
      global $DB;
       $DB->connect();

      if (empty($xmlFile)) {
         echo "testMemory with no arguments...\n";
         return;
      }
      if ($unknown == '1') {
         return;
      }

      $xml = simplexml_load_file($xmlFile, 'SimpleXMLElement', LIBXML_NOCDATA);

      if (!isset($xml->CONTENT->MEMORIES)) {
         return;
      }

      $a_memoryXML = array();
      $a_capacity = array();
      $i = 0;
      foreach ($xml->CONTENT->MEMORIES as $child) {
         if (isset($child->CAPACITY)
                 AND (string)$child->CAPACITY == 'No') {
            $child->CAPACITY = 0;
         }

         if (isset($child->CAPTION)
                 AND preg_match("/^[0-9]+$/i", (string)$child->CAPACITY)
                 AND (isset($child->TYPE)
                         AND !preg_match('/Flash/', (string)$child->TYPE))) {
            if ((string)$child->CAPACITY != '0') {
               $a_memoryXML["'".$i."-".$child->CAPTION."'"] = 1;
               $i++;
               if (!isset($a_capacity[(string)$child->CAPACITY])) {
                  $a_capacity[(string)$child->CAPACITY] = 0;
               }
               $a_capacity[(string)$child->CAPACITY]++;
            }
         }
      }

      $query = "SELECT * FROM `glpi_items_devicememories`
         WHERE `items_id`='".$items_id."' 
            AND `itemtype`='Computer'";
      $result=$DB->query($query);

      $this->assertEquals($DB->numrows($result), count($a_memoryXML), 'Difference of Memories, created '.$DB->numrows($result).' times instead '.count($a_memoryXML).' ['.$xmlFile.']');

      foreach ($a_capacity as $capacity=>$nb) {
         $query = "SELECT * FROM `glpi_items_devicememories`
            WHERE `items_id`='".$items_id."'
               AND `itemtype`='Computer'
               AND `size`='".$capacity."'";
         $result=$DB->query($query);
         $this->assertEquals($DB->numrows($result), $nb, 'Difference of Memories capacity for capacity '.$capacity.' ['.$xmlFile.']');
      }
   }



  function testNetwork($xml='', $items_id=0, $unknown=0, $xmlFile='') {
      global $DB;
       $DB->connect();

      if (empty($xml)) {
         echo "testNetwork with no arguments...\n";
         return;
      }

      $pfBlacklist = new PluginFusioninventoryInventoryComputerBlacklist();

      if (!isset($xml->CONTENT->NETWORKS)) {
         return;
      }

      $a_networkXML = array();
      $i = 0;
      foreach ($xml->CONTENT->NETWORKS as $child) {
         if (isset($child->DESCRIPTION)) {
            $a_networkXML["'".$i."-".$child->DESCRIPTION."'"] = 1;
            $i++;
         } else if (isset($child->IPADDRESS)) {
            $a_networkXML["'".$i."-".$child->IPADDRESS."'"] = 1;
            $i++;
         }
      }
<commit_msg>Fix test for network ports<commit_after><?php

/*
   ------------------------------------------------------------------------
   FusionInventory
   Copyright (C) 2010-2012 by the FusionInventory Development Team.

   http://www.fusioninventory.org/   http://forge.fusioninventory.org/
   ------------------------------------------------------------------------

   LICENSE

   This file is part of FusionInventory project.

   FusionInventory is free software: you can redistribute it and/or modify
   it under the terms of the GNU Affero General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   FusionInventory 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 Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with Behaviors. If not, see <http://www.gnu.org/licenses/>.

   ------------------------------------------------------------------------

   @package   FusionInventory
   @author    <NAME>
   @co-author
   @copyright Copyright (c) 2010-2012 FusionInventory team
   @license   AGPL License 3.0 or (at your option) any later version
              http://www.gnu.org/licenses/agpl-3.0-standalone.html
   @link      http://www.fusioninventory.org/
   @link      http://forge.fusioninventory.org/projects/fusioninventory-for-glpi/
   @since     2010

   ------------------------------------------------------------------------
 */

class InventoryComputer extends PHPUnit_Framework_TestCase {


    public function testSetModuleInventoryOff() {
       global $DB,$CFG_GLPI;
       $DB->connect();

       $plugin = new Plugin();
       $plugin->getFromDBbyDir("fusioninventory");
       $plugin->activate($plugin->fields['id']);
       Plugin::load("fusioninventory");
       $plugin->getFromDBbyDir("fusinvinventory");
       $plugin->activate($plugin->fields['id']);
       Plugin::load("fusinvinventory");

       Session::loadLanguage("en_GB");

       $CFG_GLPI['root_doc'] = "http://127.0.0.1/fusion0.84/";
         //Toolbox::deleteDir(GLPI_ROOT."/files/_plugins/fusioninventory/criterias");
         //Toolbox::deleteDir(GLPI_ROOT."/files/_plugins/fusioninventory/machines");
         system("rm -fr ".GLPI_ROOT."/files/_plugins/fusioninventory/criterias");
         system("rm -fr ".GLPI_ROOT."/files/_plugins/fusioninventory/machines");


         // Add in blacklit : 30003000000000000000000000300000000000000000000000000000000000000000000000000000000000
         $pfBlacklist = new PluginFusioninventoryInventoryComputerBlacklist();
         $input = array();
         $input['plugin_fusioninventory_criterium_id'] = '1';
         $input['value'] = '30003000000000000000000000300000000000000000000000000000000000000000000000000000000000';
         $pfBlacklist->add($input);


        // set in config module inventory = yes by default
        $query = "UPDATE `glpi_plugin_fusioninventory_agentmodules`
           SET `is_active`='0'
           WHERE `modulename`='INVENTORY' ";
        $DB->query($query);

        // Activate Extra-debug
         $plugin = new Plugin();
         $data = $plugin->find("`name` = 'FusionInventory'");
         $fields = current($data);
         $plugins_id = $fields['id'];
         $pfConfig = new PluginFusioninventoryConfig();
         $pfConfig->updateValue("extradebug", "1");

    }


//    public function testSendinventoryOff() {
//       $this->testSendinventory();
//    }
//
//
//   public function testMachinesCriteriasFoldersOff() {
//      $exist = 0;
//      if (file_exists(GLPI_ROOT."/files/_plugins/fusioninventory/machines")) {
//         $exist = 1;
//      }
//      $this->assertEquals($exist, 0, 'Problem on inventory, machines & criterias folder must not create because inventory not allowed on this agent');
//   }



    public function testSetModuleInventoryOn() {
       global $DB;
       $DB->connect();

        // set in config module inventory = yes by default
        $query = "UPDATE `glpi_plugin_fusioninventory_agentmodules`
           SET `is_active` = '1'
           WHERE `modulename` = 'INVENTORY'";
        $DB->query($query);
     }



   public function testSendinventories() {
      global $DB;
      $DB->connect();
       
      $MyDirectory = opendir("InventoryComputer/xml");
      $k = 0;
      while(false !== ($Entry = readdir($MyDirectory))) {
         if(is_dir('InventoryComputer/xml/'.$Entry)&& $Entry != '.' && $Entry != '..') {
            $myVersion = opendir("InventoryComputer/xml/".$Entry);
            while(false !== ($xmlFilename = readdir($myVersion))) {
               if ($xmlFilename != '.' && $xmlFilename != '..') {
                  // We have the XML of each computer inventory
                  $xml = simplexml_load_file("InventoryComputer/xml/".$Entry."/".$xmlFilename, 'SimpleXMLElement', LIBXML_NOCDATA);
                  if ($xml->asXML()) {
echo "************************\n";
echo "Memory : ".memory_get_usage()." / ".memory_get_peak_usage()."\n";
                     $deviceid_ok = 0;
                     if (!empty($xml->DEVICEID)) {
                        $deviceid_ok = 1;
                     }
                     $this->assertEquals($deviceid_ok, 1, 'Problem on XML, DEVICEID of file InventoryComputer/xml/'.$Entry.'/'.$xmlFilename.' not good!');

                     $inputProlog = '<?xml version="1.0" encoding="UTF-8"?>
<REQUEST>
  <DEVICEID>'.$xml->DEVICEID.'</DEVICEID>
  <QUERY>PROLOG</QUERY>
  <TOKEN><PASSWORD></TOKEN>
</REQUEST>';
echo "# testProlog\n";
                     $this->testProlog($inputProlog, $xml->DEVICEID);
                     $k++;
echo "# testSendinventory\n";
                     $array = $this->testSendinventory("InventoryComputer/xml/".$Entry."/".$xmlFilename, $xml);
                     $items_id = $array[0];
                     $unknown  = $array[1];
echo "# testPrinter\n";
                     $this->testPrinter("InventoryComputer/xml/".$Entry."/".$xmlFilename, $items_id, $unknown);
echo "# testMonitor\n";
                     $this->testMonitor("InventoryComputer/xml/".$Entry."/".$xmlFilename, $items_id, $unknown);
echo "# testCPU\n";
                     $this->testCPU("InventoryComputer/xml/".$Entry."/".$xmlFilename, $items_id, $unknown);
echo "# testDrive\n";
                     $this->testDrive("InventoryComputer/xml/".$Entry."/".$xmlFilename, $items_id, $unknown);
echo "# testController\n";
                     $this->testController("InventoryComputer/xml/".$Entry."/".$xmlFilename, $items_id, $unknown);
echo "# testSound\n";
                     $this->testSound("InventoryComputer/xml/".$Entry."/".$xmlFilename, $items_id, $unknown);
echo "# testVideo\n";
                     $this->testVideo("InventoryComputer/xml/".$Entry."/".$xmlFilename, $items_id, $unknown);
echo "# testMemory\n";
                     $this->testMemory("InventoryComputer/xml/".$Entry."/".$xmlFilename, $items_id, $unknown);
echo "# testNetwork\n";
                     $this->testNetwork($xml, $items_id, $unknown, "InventoryComputer/xml/".$Entry."/".$xmlFilename);
echo "# testSoftware\n";
                     $this->testSoftware("InventoryComputer/xml/".$Entry."/".$xmlFilename, $items_id, $unknown);
echo "# testHardware\n";
                     $this->testHardware("InventoryComputer/xml/".$Entry."/".$xmlFilename, $items_id, $unknown);
echo "# testHardwareModifications\n";
                     $this->testHardwareModifications("InventoryComputer/xml/".$Entry."/".$xmlFilename, $items_id);
                     echo "Number of files : ".$k."\n";

                     $GLPIlog = new GLPIlogs();
                     $GLPIlog->testSQLlogs();
                     $GLPIlog->testPHPlogs();
                  } else {
                     $this->assertEquals(0, 1, "InventoryComputer/xml/".$Entry."/".$xmlFilename." : XML not well formed");  
                  }
               }
            }
         }
      }
   }



//   public function testMachinesCriteriasFolders() {
//      $exist = 0;
//      if (file_exists(GLPI_ROOT."/files/_plugins/fusioninventory/machines")) {
//         $exist = 1;
//      }
//      $this->assertEquals($exist, 1, 'Problem on inventory, machines & criterias folder not create successfully!');
//   }


   function testProlog($inputXML='', $deviceID='') {
      global $DB;
      $DB->connect();

      if (empty($inputXML)) {
         echo "testProlog with no arguments...\n";
         return;
      }
      $emulatorAgent = new emulatorAgent;
      $emulatorAgent->server_urlpath = "/fusion0.84/plugins/fusioninventory/";
      $prologXML = $emulatorAgent->sendProlog($inputXML);
      $pfAgent = new PluginFusioninventoryAgent();
      $a_agent = $pfAgent->find("`device_id`='".$deviceID."'");
      $this->assertEquals(count($a_agent), 1, 'Problem on prolog, agent ('.$deviceID.') not right created!');

      $this->assertEquals(preg_match("/<RESPONSE>SEND<\/RESPONSE>/", $prologXML), 1, 'Prolog not send to agent!');
   }

   function testSendinventory($xmlFile='', $xml='') {

      if (empty($xmlFile)) {
         echo "testSendinventory with no arguments...\n";
         return;
      }

      $emulatorAgent = new emulatorAgent;
      $emulatorAgent->server_urlpath = "/fusion0.84/plugins/fusioninventory/";
      echo "====================\n";
      echo $xmlFile."\n";
      $input_xml = $xml->asXML();
      $returnAgent = $emulatorAgent->sendProlog($input_xml);
      echo $returnAgent."\n";

      $Computer = new Computer();
//      $xml = simplexml_load_file($xmlFile,'SimpleXMLElement', LIBXML_NOCDATA);
      if (isset($xml->CONTENT->BIOS->SSN)) {
         if ($xml->CONTENT->BIOS->SSN == '30003000000000000000000000300000000000000000000000000000000000000000000000000000000000') {
            unset($xml->CONTENT->BIOS->SSN);
         } else if ($xml->CONTENT->BIOS->SSN == 'To Be Filled By O.E.M.') {
            unset($xml->CONTENT->BIOS->SSN);
         } else {
            $xml->CONTENT->BIOS->SSN = trim($xml->CONTENT->BIOS->SSN);
         }
      }
      $serial = "(`serial` IS NULL OR `serial`='')";
      if ((isset($xml->CONTENT->BIOS->SSN)) AND (!empty($xml->CONTENT->BIOS->SSN))) {
         $serial = "`serial`='".$xml->CONTENT->BIOS->SSN."'";
      }
      $a_computers = $Computer->find("`name`='".$xml->CONTENT->HARDWARE->NAME."' AND ".$serial);
      $unknown = 0;
      if (count($a_computers) == 0) {
         // Search in unknown device
         $pfUnknownDevice = new PluginFusioninventoryUnknownDevice();
         $a_computers = $pfUnknownDevice->find("`name`='".$xml->CONTENT->HARDWARE->NAME."'");
         $unknown = 1;
      }
      $this->assertEquals(count($a_computers), 1, 'Problem on creation computer, not created ('.$xmlFile.')');
      foreach($a_computers as $items_id => $data) {
         return array($items_id, $unknown);
      }
   }


   function testPrinter($xmlFile='', $items_id=0, $unknown=0) {
      global $DB;
      $DB->connect();

      if (empty($xmlFile)) {
         echo "testPrinter with no arguments...\n";
         return;
      }
      if ($unknown == '1') {
         return;
      }

      $Printer  = new Printer();

      $xml = simplexml_load_file($xmlFile, 'SimpleXMLElement', LIBXML_NOCDATA);

      if (!isset($xml->CONTENT->PRINTERS)) {
         return;
      }
      // Verify not have 2 printer in DB with same printer serial
      foreach ($xml->CONTENT->PRINTERS as $child) {
         if (isset($child->SERIAL)) {
            $child->SERIAL = preg_replace('/\/$/', '', (string)$child->SERIAL);
            $a_printer = $Printer->find("`serial`='".$child->SERIAL."'");
            $this->assertEquals(count($a_printer), 1, 'Problem on printers, printer created "'.count($a_printer).'" instead 1 times (serial : '.$child->SERIAL.')');
         }
      }
      // Verify all printers are connected to the computer
         // Get all printers connected to computer in DB
         $query = "SELECT * FROM `glpi_computers_items`
                  INNER JOIN `glpi_printers` on `glpi_printers`.`id`=`items_id`
                      WHERE `computers_id` = '".$items_id."'
                            AND `itemtype` = 'Printer'";
         $result=$DB->query($query);
         $a_printerDB = array();
         while ($data=$DB->fetch_array($result)) {
            $a_printerDB["'".$data['name']."'"] = 1;
         }
         // Verify printers in XML
         $a_printerXML = array();
         $a_serial = array();
         foreach ($xml->CONTENT->PRINTERS as $child) {
            if (isset($child->SERIAL)) {
               if (isset($a_serial[(string)$child->SERIAL])) {
                  unset($a_printerXML[$a_serial[(string)$child->SERIAL]]);
                  $a_printerXML["'".(string)$child->NAME."'"] = 1;
               } else {
                  $a_printerXML["'".(string)$child->NAME."'"] = 1;                  
               }               
               $a_serial[(string)$child->SERIAL] = (string)$child->NAME;
            } else {
               $a_printerXML["'".(string)$child->NAME."'"] = 1;
            }
         }
         // Display (test) differences
         $a_printerDiff = array();
         $a_printerDiff = array_diff_key($a_printerDB, $a_printerXML);
         if (count($a_printerDiff) < count(array_diff_key($a_printerXML, $a_printerDB))) {
            $a_printerDiff = array_diff_key($a_printerXML, $a_printerDB);
         }
         $this->assertEquals(count($a_printerDiff), 0, 'Difference of printers "'.print_r($a_printerDiff, true).'" ['.$xmlFile.']');


         // Verify fields in GLPI
         foreach($xml->CONTENT->PRINTERS as $child) {
            if (isset($child->SERIAL)) {
               $a_printer = $Printer->find("`serial`='".$child->SERIAL."' ");
               foreach ($a_printer as $datas) {
                  if (isset($child->NAME)) {
                     $this->assertEquals(trim($child->NAME), $datas['name'], 'Difference of printers fields ['.$xmlFile.']');
                  } else if (isset($child->DRIVER)) {
                     $this->assertEquals($child->DRIVER, $datas['name'], 'Difference of printers fields ['.$xmlFile.']');
                  }
                  if (strstr($child->PORT, "USB")) {
                     $this->assertEquals("1", $datas['have_usb'], 'Difference of printers fields ['.$xmlFile.']');
                  }
                  // Find in USBDEVICES to find manufacturer
                  foreach($xml->CONTENT->USBDEVICES as $childusb) {
                     if (isset($childusb->SERIAL)) {
                        if (file_exists(GLPI_ROOT."/files/_plugins/fusioninventory/DataFilter/usbids/".strtolower($childusb->VENDORID)."/".strtolower($childusb->PRODUCTID)."info")) {
                           $info = file_get_contents(GLPI_ROOT."/files/_plugins/fusioninventory/DataFilter/usbids/".strtolower($childusb->VENDORID)."/".strtolower($childusb->PRODUCTID)."info");
                           $array = explode("\n", $info);
                           $manufacturer_id = Dropdown::importExternal('Manufacturer', $array[0]);
                           $this->assertEquals($manufacturer_id, $datas['manufacturers_id'], 'Difference of printers fields ['.$xmlFile.']');
                        }
                     }
                  }
               }
            } else {
               $query = "SELECT `glpi_printers`.* FROM `glpi_computers_items`
                        INNER JOIN `glpi_printers` on `glpi_printers`.`id`=`items_id`
                        WHERE `computers_id` = '".$items_id."'
                            AND `itemtype` = 'Printer'";
               $result=$DB->query($query);
               $printer_select = array();
               while ($data=$DB->fetch_array($result)) {
                  if (count($printer_select) == '0') {
                     if ((isset($child->NAME)) AND ($data['name'] == $child->NAME)) {
                        $printer_select = $data;
//                     } else if ((isset($child->DRIVER)) AND ($data['name'] == $child->DRIVER)) {
//                        $printer_select = $data;
                     }
                  }
               }
               $this->assertEquals(count($printer_select['id']), "1", 'Problem to find printer for fields verification ['.$xmlFile.']');
               if (strstr($child->PORT, "USB")) {
                  $this->assertEquals("1", $printer_select['have_usb'], 'Difference of printers fields ['.$xmlFile.']');
               }
            }

         }
   }


   function testMonitor($xmlFile='', $items_id=0, $unknown=0) {
      global $DB;
      $DB->connect();

      if (empty($xmlFile)) {
         echo "testMonitor with no arguments...\n";
         return;
      }
      if ($unknown == '1') {
         return;
      }

      $Monitor  = new Monitor();

      $xml = simplexml_load_file($xmlFile, 'SimpleXMLElement', LIBXML_NOCDATA);

      if (!isset($xml->CONTENT->MONITORS)) {
         return;
      }

      // Verify not have 2 monitor in DB with same printer serial
      foreach ($xml->CONTENT->MONITORS as $child) {
         if (isset($child->SERIAL)
                 AND (string)$child->SERIAL != '') {
            $a_monitor = $Monitor->find("`serial`='".$child->SERIAL."'");
            $this->assertEquals(count($a_monitor), 1, 'Problem on monitors, monitor created "'.count($a_monitor).'" instead 1 times [serial:'.$child->SERIAL.']');
         }
      }

      // Verify all monitors are connected to the computer
         // Get all monitors connected to computer in DB
         $query = "SELECT * FROM `glpi_computers_items`
                  INNER JOIN `glpi_monitors` on `glpi_monitors`.`id`=`items_id`
                      WHERE `computers_id` = '".$items_id."'
                            AND `itemtype` = 'Monitor'";
         $result=$DB->query($query);
         $a_monitorDB = array();
         while ($data=$DB->fetch_array($result)) {
            $a_monitorDB["'".$data['name']."'"] = 1;
         }
         // Verifiy monitors in XML
         $a_monitorXML = array();
         foreach ($xml->CONTENT->MONITORS as $child) {
            $a_monitorXML["'".$child->CAPTION."'"] = 1;
         }
         // Display (test) differences
         $a_monitorDiff = array();
         $a_monitorDiff = array_diff_key($a_monitorDB, $a_monitorXML);
         if (count($a_monitorDiff) < count(array_diff_key($a_monitorXML, $a_monitorDB))) {
            $a_monitorDiff = array_diff_key($a_monitorXML, $a_monitorDB);
         }
         $this->assertEquals(count($a_monitorDiff), 0, 'Difference of monitors "'.print_r($a_monitorDiff, true).'" ('.$xmlFile.')');

   }


   function testCPU($xmlFile='', $items_id=0, $unknown=0) {
      global $DB;
       $DB->connect();

      if (empty($xmlFile)) {
         echo "testCPU with no arguments...\n";
         return;
      }
      if ($unknown == '1') {
         return;
      }

      $xml = simplexml_load_file($xmlFile, 'SimpleXMLElement', LIBXML_NOCDATA);

      if (!isset($xml->CONTENT->CPUS)) {
         return;
      }

      $a_cpuXML = array();
      $a_speed = array();
      $i = 0;
      foreach ($xml->CONTENT->CPUS as $child) {
         if (isset($child->NAME)) {
            $a_cpuXML["'".$i."-".$child->NAME."'"] = 1;
            $i++;
            if (isset($child->SPEED)
                    AND count($child) > 1) {
               if (!isset($a_speed[(string)$child->SPEED])) {
                  $a_speed[(string)$child->SPEED] = 0;
               }
               $a_speed[(string)$child->SPEED]++;
            }
         } else if (isset($child->TYPE)) {
            $a_cpuXML["'".$i."-".$child->TYPE."'"] = 1;
            $i++;
            if (isset($child->SPEED)
                    AND count($child) > 1) {
               if (!isset($a_speed[(string)$child->SPEED])) {
                  $a_speed[(string)$child->SPEED] = 0;
               }
               $a_speed[(string)$child->SPEED]++;
            }
         }

      }

      $query = "SELECT * FROM `glpi_items_deviceprocessors`
         WHERE `items_id`='".$items_id."' 
            AND `itemtype`='Computer'";
      $result=$DB->query($query);

      $this->assertEquals($DB->numrows($result), count($a_cpuXML), 'Difference of CPUs, created '.$DB->numrows($result).' times instead '.count($a_cpuXML).' ['.$xmlFile.']');

      foreach ($a_speed as $speed=>$nb) {
         $query = "SELECT * FROM `glpi_items_deviceprocessors`
            WHERE `items_id`='".$items_id."'
               AND `itemtype`='Computer'
               AND `frequency`='".$speed."'";
         $result=$DB->query($query);
         $this->assertEquals($DB->numrows($result), $nb, 'Difference of Processor speed '.$speed.' ['.$xmlFile.']');
      }

   }



   function testDrive($xmlFile='', $items_id=0, $unknown=0) {
      global $DB;
       $DB->connect();

      if (empty($xmlFile)) {
         echo "testDrive with no arguments...\n";
         return;
      }
      if ($unknown == '1') {
         return;
      }

      $xml = simplexml_load_file($xmlFile, 'SimpleXMLElement', LIBXML_NOCDATA);

      if (!isset($xml->CONTENT->DRIVE)) {
         return;
      }

      $a_driveXML = array();
      $i = 0;
      foreach ($xml->CONTENT->DRIVES as $child) {
         if (isset($child->CAPTION)) {
            $a_driveXML["'".$i."-".$child->CAPTION."'"] = 1;
            $i++;
         }
      }

      $query = "SELECT * FROM `glpi_computerdisks`
         WHERE `computers_id`='".$items_id."' ";
      $result=$DB->query($query);

      $this->assertEquals($DB->numrows($result), count($a_driveXML), 'Difference of Drives, created '.$DB->numrows($result).' times instead '.count($a_driveXML).' ['.$xmlFile.']');
   }


   function testController($xmlFile='', $items_id=0, $unknown=0) {
      global $DB;
       $DB->connect();

      if (empty($xmlFile)) {
         echo "testController with no arguments...\n";
         return;
      }
      if ($unknown == '1') {
         return;
      }

      $xml = simplexml_load_file($xmlFile, 'SimpleXMLElement', LIBXML_NOCDATA);

      if (!isset($xml->CONTENT->CONTROLLERS)) {
         return;
      }

      // Controller to ignore
      $ignore_controllers = array();
      foreach ($xml->CONTENT->VIDEOS as $child) {
         $ignore_controllers["'".$child->NAME."'"] = 1;
         if (isset($child->CHIPSET)) {
            $ignore_controllers["'".$child->CHIPSET."'"] = 1;
         }
      }
      foreach ($xml->CONTENT->SOUNDS as $child) {
         $ignore_controllers["'".$child->NAME."'"] = 1;
      }

      $a_controllerXML = array();
      $i = 0;
      foreach ($xml->CONTENT->CONTROLLERS as $child) {
         if ((isset($child->NAME))
                 AND (!isset($ignore_controllers["'".$child->NAME."'"]))) {
            if (!(isset($child->NAME)
                    AND isset($child->CAPTION)
                    AND isset($child->TYPE)
                    AND empty($child->NAME)
                    AND empty($child->CAPTION)
                    AND empty($child->TYPE))) {

               $a_controllerXML["'".$i."-".$child->NAME."'"] = 1;
               $i++;
            }
         }
      }

      $query = "SELECT * FROM `glpi_items_devicecontrols`
         WHERE `items_id`='".$items_id."' 
            AND `itemtype`='Computer'";
      $result=$DB->query($query);

      $this->assertEquals($DB->numrows($result), count($a_controllerXML), 'Difference of Controllers, created '.$DB->numrows($result).' times instead '.count($a_controllerXML).' ['.$xmlFile.']');
   }


   function testSound($xmlFile='', $items_id=0, $unknown=0) {
      global $DB;
       $DB->connect();

      if (empty($xmlFile)) {
         echo "testSound with no arguments...\n";
         return;
      }
      if ($unknown == '1') {
         return;
      }

      $xml = simplexml_load_file($xmlFile, 'SimpleXMLElement', LIBXML_NOCDATA);

      if (!isset($xml->CONTENT->SOUNDS)) {
         return;
      }

      $a_soundXML = array();
      $i = 0;
      foreach ($xml->CONTENT->SOUNDS as $child) {
         if (isset($child->NAME)) {
            $a_soundXML["'".$i."-".$child->NAME."'"] = 1;
            $i++;
         }
      }

      $query = "SELECT * FROM `glpi_items_devicesoundcards`
         WHERE `items_id`='".$items_id."' 
            AND `itemtype`='Computer'";
      $result=$DB->query($query);

      $this->assertEquals($DB->numrows($result), count($a_soundXML), 'Difference of Sounds, created '.$DB->numrows($result).' times instead '.count($a_soundXML).' ['.$xmlFile.']');
   }


  function testVideo($xmlFile='', $items_id=0, $unknown=0) {
      global $DB;
       $DB->connect();

      if (empty($xmlFile)) {
         echo "testVideo with no arguments...\n";
         return;
      }
      if ($unknown == '1') {
         return;
      }

      $xml = simplexml_load_file($xmlFile, 'SimpleXMLElement', LIBXML_NOCDATA);

      if (!isset($xml->CONTENT->VIDEOS)) {
         return;
      }

      $a_videoXML = array();
      $i = 0;
      foreach ($xml->CONTENT->VIDEOS as $child) {
         if (isset($child->NAME)) {
            $a_videoXML["'".$i."-".$child->NAME."'"] = 1;
            $i++;
         }
      }

      $query = "SELECT * FROM `glpi_items_devicegraphiccards`
         WHERE `items_id`='".$items_id."' 
            AND `itemtype`='Computer'";
      $result=$DB->query($query);

      $this->assertEquals($DB->numrows($result), count($a_videoXML), 'Difference of Videos, created '.$DB->numrows($result).' times instead '.count($a_videoXML).' ['.$xmlFile.']');
   }



   function testMemory($xmlFile='', $items_id=0, $unknown=0) {
      global $DB;
       $DB->connect();

      if (empty($xmlFile)) {
         echo "testMemory with no arguments...\n";
         return;
      }
      if ($unknown == '1') {
         return;
      }

      $xml = simplexml_load_file($xmlFile, 'SimpleXMLElement', LIBXML_NOCDATA);

      if (!isset($xml->CONTENT->MEMORIES)) {
         return;
      }

      $a_memoryXML = array();
      $a_capacity = array();
      $i = 0;
      foreach ($xml->CONTENT->MEMORIES as $child) {
         if (isset($child->CAPACITY)
                 AND (string)$child->CAPACITY == 'No') {
            $child->CAPACITY = 0;
         }

         if (isset($child->CAPTION)
                 AND preg_match("/^[0-9]+$/i", (string)$child->CAPACITY)
                 AND (isset($child->TYPE)
                         AND !preg_match('/Flash/', (string)$child->TYPE))) {
            if ((string)$child->CAPACITY != '0') {
               $a_memoryXML["'".$i."-".$child->CAPTION."'"] = 1;
               $i++;
               if (!isset($a_capacity[(string)$child->CAPACITY])) {
                  $a_capacity[(string)$child->CAPACITY] = 0;
               }
               $a_capacity[(string)$child->CAPACITY]++;
            }
         }
      }

      $query = "SELECT * FROM `glpi_items_devicememories`
         WHERE `items_id`='".$items_id."' 
            AND `itemtype`='Computer'";
      $result=$DB->query($query);

      $this->assertEquals($DB->numrows($result), count($a_memoryXML), 'Difference of Memories, created '.$DB->numrows($result).' times instead '.count($a_memoryXML).' ['.$xmlFile.']');

      foreach ($a_capacity as $capacity=>$nb) {
         $query = "SELECT * FROM `glpi_items_devicememories`
            WHERE `items_id`='".$items_id."'
               AND `itemtype`='Computer'
               AND `size`='".$capacity."'";
         $result=$DB->query($query);
         $this->assertEquals($DB->numrows($result), $nb, 'Difference of Memories capacity for capacity '.$capacity.' ['.$xmlFile.']');
      }
   }



  function testNetwork($xml='', $items_id=0, $unknown=0, $xmlFile='') {
      global $DB;
       $DB->connect();

      if (empty($xml)) {
         echo "testNetwork with no arguments...\n";
         return;
      }

      $pfBlacklist = new PluginFusioninventoryInventoryComputerBlacklist();

      if (!isset($xml->CONTENT->NETWORKS)) {
         return;
      }

      $a_networkXML = array();
      $i = 0;
      $networkunique = array();
      foreach ($xml->CONTENT->NETWORKS as $child) {
         if (isset($child->DESCRIPTION)) {
            $add = 1;
            if (isset($child->MAC)) {
               if (isset($networkunique[$child->DESCRIPTION."-".$child->MAC])) {
                  $add = 0;
               }
            } else {
               if (isset($networkunique[$child->DESCRIPTION])) {
                  $add = 0;
               }
            }
            if ($add == 1) {
               $a_networkXML["'".$i."-".$child->DESCRIPTION."'"] = 1;
               $i++;
               $networkunique[$child->DESCRIPTION."-".$child->MAC] = 1;
            }
         } else if (isset($child->IPADDRESS)) {
            $a_networkXML["'".$i."-".$child->IPADDRESS."'"] = 1;
            $i++;
         }
      }

{'fraction_non_alphanumeric': 0.18271690888521452, 'fraction_numerical': 0.013941345400748411, 'mean_word_length': 2.062686567164179, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 2, '>': 597, 'https://': 0, 'lorem ipsum': 0, 'www.': 8, 'xml': 306}, 'pii_count': 2, 'substrings_counts': 12, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7554235', 'n_tokens_mistral': 18364, 'n_tokens_neox': 17104, 'n_words': 3128}