/**
  * @test
  */
 public function prepareDB()
 {
     global $DB;
     $DB->connect();
     $pfDeployPackage = new PluginFusioninventoryDeployPackage();
     $pfDeployGroup = new PluginFusioninventoryDeployGroup();
     $pfTask = new PluginFusioninventoryTask();
     $pfTaskjob = new PluginFusioninventoryTaskjob();
     $pfTaskjobState = new PluginFusioninventoryTaskjobstate();
     $pfTaskjoblog = new PluginFusioninventoryTaskjoblog();
     $pfDeployGroup_Dynamicdata = new PluginFusioninventoryDeployGroup_Dynamicdata();
     // Create package
     $input = array('entities_id' => 0, 'name' => 'package');
     $packages_id = $pfDeployPackage->add($input);
     // Create fusioninventory dynamic group
     $input = array('name' => 'all computers have name computer', 'type' => 'DYNAMIC');
     $groups_id = $pfDeployGroup->add($input);
     $input = array('plugin_fusioninventory_deploygroups_id' => $groups_id, 'fields_array' => 'a:2:{s:8:"criteria";a:1:{i:0;a:3:{s:5:"field";s:1:"1";s:10:"searchtype";s:8:"contains";s:5:"value";s:8:"computer";}}s:12:"metacriteria";s:0:"";}');
     $pfDeployGroup_Dynamicdata->add($input);
     // create task
     $input = array('entities_id' => 0, 'name' => 'deploy', 'is_active' => 1);
     self::$tasks_id = $pfTask->add($input);
     // create taskjob
     $input = array('plugin_fusioninventory_tasks_id' => self::$tasks_id, 'entities_id' => 0, 'name' => 'deploy', 'method' => 'deployinstall', 'targets' => '[{"PluginFusioninventoryDeployPackage":"' . $packages_id . '"}]', 'actors' => '[{"PluginFusioninventoryDeployGroup":"' . self::$tasks_id . '"}]');
     self::$taskjobs_id = $pfTaskjob->add($input);
     //create taskjobstate
     $input = array('plugin_fusioninventory_taskjobs_id' => self::$taskjobs_id, 'items_id' => 0, 'itemtype' => 'Computer', 'state' => PluginFusioninventoryTaskjobstate::FINISHED, 'plugin_fusioninventory_agents_id' => 0, 'specificity' => 0, 'uniqid' => 0);
     self::$taskjobstates_id = $pfTaskjobState->add($input);
     //crfeate taskjoblogR
     $input = array('plugin_fusioninventory_taskjobstates_id' => self::$taskjobstates_id, 'date ' => date('Y-m-d H:i:s'), 'items_id' => 0, 'itemtype' => 'Computer', 'state' => PluginFusioninventoryTaskjoblog::TASK_RUNNING, 'comment' => "1 ==devicesfound==");
     self::$taskjoblogs_id = $pfTaskjoblog->add($input);
 }
 /**
  * @test
  */
 public function prepareDB()
 {
     global $DB;
     $DB->connect();
     $computer = new Computer();
     $pfAgent = new PluginFusioninventoryAgent();
     $pfDeployPackage = new PluginFusioninventoryDeployPackage();
     $pfDeployGroup = new PluginFusioninventoryDeployGroup();
     $pfTask = new PluginFusioninventoryTask();
     $pfTaskjob = new PluginFusioninventoryTaskjob();
     $pfDeployGroup_Dynamicdata = new PluginFusioninventoryDeployGroup_Dynamicdata();
     // Create package
     $input = array('entities_id' => 0, 'name' => 'package');
     $packages_id = $pfDeployPackage->add($input);
     // Create fusioninventory dynamic group
     $input = array('name' => 'all computers have name computer', 'type' => 'DYNAMIC');
     $groups_id = $pfDeployGroup->add($input);
     $input = array('plugin_fusioninventory_deploygroups_id' => $groups_id, 'fields_array' => 'a:2:{s:8:"criteria";a:1:{i:0;a:3:{s:5:"field";s:1:"1";s:10:"searchtype";s:8:"contains";s:5:"value";s:8:"computer";}}s:12:"metacriteria";s:0:"";}');
     $pfDeployGroup_Dynamicdata->add($input);
     // create task
     $input = array('entities_id' => 0, 'name' => 'deploy', 'is_active' => 1);
     $tasks_id = $pfTask->add($input);
     // create takjob
     $input = array('plugin_fusioninventory_tasks_id' => $tasks_id, 'entities_id' => 0, 'name' => 'deploy', 'method' => 'deployinstall', 'targets' => '[{"PluginFusioninventoryDeployPackage":"' . $packages_id . '"}]', 'actors' => '[{"PluginFusioninventoryDeployGroup":"' . $tasks_id . '"}]');
     $pfTaskjob->add($input);
     // Create computers + agents
     $input = array('entities_id' => 0, 'name' => 'computer1');
     $computers_id = $computer->add($input);
     $input = array('entities_id' => 0, 'name' => 'portdavid', 'version' => '{"INVENTORY":"v2.3.11"}', 'device_id' => 'portdavid', '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);
 }
  @link      http://www.fusioninventory.org/
  @link      http://forge.fusioninventory.org/projects/fusioninventory-for-glpi/
  @since     2010

  ------------------------------------------------------------------------
*/
include "../../../inc/includes.php";
Session::checkLoginUser();
$group = new PluginFusioninventoryDeployGroup();
if (isset($_POST['save'])) {
    $group_item = new PluginFusioninventoryDeployGroup_Dynamicdata();
    if (!countElementsInTable($group_item->getTable(), "plugin_fusioninventory_deploygroups_id='" . $_POST['id'] . "'")) {
        $criteria = array('criteria' => $_POST['criteria'], 'metacriteria' => $_POST['metacriteria']);
        $values['fields_array'] = serialize($criteria);
        $values['plugin_fusioninventory_deploygroups_id'] = $_POST['id'];
        $group_item->add($values);
    } else {
        $item = getAllDatasFromTable($group_item->getTable(), "plugin_fusioninventory_deploygroups_id='" . $_POST['id'] . "'");
        $values = array_pop($item);
        $criteria = array('criteria' => $_POST['criteria'], 'metacriteria' => $_POST['metacriteria']);
        $values['fields_array'] = serialize($criteria);
        $group_item->update($values);
    }
    Html::redirect(Toolbox::getItemTypeFormURL("PluginFusioninventoryDeployGroup") . "?id=" . $_GET['id']);
} elseif (isset($_POST["add"])) {
    $group->check(-1, UPDATE, $_POST);
    $newID = $group->add($_POST);
    Html::redirect(Toolbox::getItemTypeFormURL("PluginFusioninventoryDeployGroup") . "?id=" . $newID);
} else {
    if (isset($_POST["delete"])) {
        //   $group->check($_POST['id'], DELETE);
 /**
  * @test
  */
 public function TaskWithPC()
 {
     $this->mark_incomplete();
     global $DB;
     $_SESSION['glpiactiveentities_string'] = 0;
     $pfDeployGroup = new PluginFusioninventoryDeployGroup();
     $pfDeployGroup_Dynamicdata = new PluginFusioninventoryDeployGroup_Dynamicdata();
     $pfDeployPackage = new PluginFusioninventoryDeployPackage();
     $pfTask = new PluginFusioninventoryTask();
     $pfTaskJob = new PluginFusioninventoryTaskjob();
     $input = array('name' => 'test', 'type' => 'DYNAMIC');
     $pfDeployGroup->add($input);
     $input = array('groups_id' => 1, 'fields_array' => '{"entities_id":"0","name":"","field":["1"],"searchtype":["contains"],"contains":["^pc"],"plugin_fusioninventory_deploygroup_dynamicdatas_id":"1","id":"1","updaterule":"Update this rule","itemtype":"Computer"}');
     $pfDeployGroup_Dynamicdata->add($input);
     $input = array('name' => 'ls', 'entities_id' => 0);
     $pfDeployPackage->add($input);
     $input = array('name' => 'deploy', 'is_active' => 1, 'communication' => 'pull');
     $pfTask->add($input);
     $a_plugins = current(getAllDatasFromTable('glpi_plugins', '`directory`="fusioninventory"'));
     $input = array('plugin_fusioninventory_tasks_id' => 1, 'name' => 'deploy', 'plugins_id' => $a_plugins['id'], 'method' => 'deployinstall', 'action' => '[{"PluginFusioninventoryDeployGroup":"1"}]');
     $pfTaskJob->add($input);
     $DB->query('UPDATE `glpi_plugin_fusioninventory_taskjobs`
      SET `definition`=\'[{"PluginFusioninventoryDeployPackage":"1"}]\'
      WHERE `id`="1"');
     // Force task prepation
     $pfTaskJob->forceRunningTask(1);
     $a_jobstates = getAllDatasFromTable("glpi_plugin_fusioninventory_taskjobstates");
     foreach ($a_jobstates as $num => $data) {
         unset($data['uniqid']);
         $a_jobstates[$num] = $data;
     }
     $a_reference = array(1 => array('id' => 1, 'plugin_fusioninventory_taskjobs_id' => 1, 'items_id' => 1, 'itemtype' => "PluginFusioninventoryDeployPackage", 'state' => 0, 'plugin_fusioninventory_agents_id' => 1, 'specificity' => '', 'execution_id' => '0'), 2 => array('id' => 2, 'plugin_fusioninventory_taskjobs_id' => 1, 'items_id' => 1, 'itemtype' => "PluginFusioninventoryDeployPackage", 'state' => 0, 'plugin_fusioninventory_agents_id' => 2, 'specificity' => '', 'execution_id' => '0'), 3 => array('id' => 3, 'plugin_fusioninventory_taskjobs_id' => 1, 'items_id' => 1, 'itemtype' => "PluginFusioninventoryDeployPackage", 'state' => 0, 'plugin_fusioninventory_agents_id' => 3, 'specificity' => '', 'execution_id' => '0'), 4 => array('id' => 4, 'plugin_fusioninventory_taskjobs_id' => 1, 'items_id' => 1, 'itemtype' => "PluginFusioninventoryDeployPackage", 'state' => 0, 'plugin_fusioninventory_agents_id' => 4, 'specificity' => '', 'execution_id' => '0'), 5 => array('id' => 5, 'plugin_fusioninventory_taskjobs_id' => 1, 'items_id' => 1, 'itemtype' => "PluginFusioninventoryDeployPackage", 'state' => 0, 'plugin_fusioninventory_agents_id' => 5, 'specificity' => '', 'execution_id' => '0'), 6 => array('id' => 6, 'plugin_fusioninventory_taskjobs_id' => 1, 'items_id' => 1, 'itemtype' => "PluginFusioninventoryDeployPackage", 'state' => 0, 'plugin_fusioninventory_agents_id' => 6, 'specificity' => '', 'execution_id' => '0'), 7 => array('id' => 7, 'plugin_fusioninventory_taskjobs_id' => 1, 'items_id' => 1, 'itemtype' => "PluginFusioninventoryDeployPackage", 'state' => 0, 'plugin_fusioninventory_agents_id' => 7, 'specificity' => '', 'execution_id' => '0'), 8 => array('id' => 8, 'plugin_fusioninventory_taskjobs_id' => 1, 'items_id' => 1, 'itemtype' => "PluginFusioninventoryDeployPackage", 'state' => 0, 'plugin_fusioninventory_agents_id' => 8, 'specificity' => '', 'execution_id' => '0'), 9 => array('id' => 9, 'plugin_fusioninventory_taskjobs_id' => 1, 'items_id' => 1, 'itemtype' => "PluginFusioninventoryDeployPackage", 'state' => 0, 'plugin_fusioninventory_agents_id' => 9, 'specificity' => '', 'execution_id' => '0'), 10 => array('id' => 10, 'plugin_fusioninventory_taskjobs_id' => 1, 'items_id' => 1, 'itemtype' => "PluginFusioninventoryDeployPackage", 'state' => 0, 'plugin_fusioninventory_agents_id' => 10, 'specificity' => '', 'execution_id' => '0'), 11 => array('id' => 11, 'plugin_fusioninventory_taskjobs_id' => 1, 'items_id' => 1, 'itemtype' => "PluginFusioninventoryDeployPackage", 'state' => 0, 'plugin_fusioninventory_agents_id' => 11, 'specificity' => '', 'execution_id' => '0'), 12 => array('id' => 12, 'plugin_fusioninventory_taskjobs_id' => 1, 'items_id' => 1, 'itemtype' => "PluginFusioninventoryDeployPackage", 'state' => 0, 'plugin_fusioninventory_agents_id' => 12, 'specificity' => '', 'execution_id' => '0'), 13 => array('id' => 13, 'plugin_fusioninventory_taskjobs_id' => 1, 'items_id' => 1, 'itemtype' => "PluginFusioninventoryDeployPackage", 'state' => 0, 'plugin_fusioninventory_agents_id' => 13, 'specificity' => '', 'execution_id' => '0'));
     $this->assertEquals($a_reference, $a_jobstates);
 }