/** * @covers ::isForeignKeyField **/ public function testIsForeignKeyFieldMore() { $this->assertFalse(isForeignKeyField('FakeId')); $this->assertFalse(isForeignKeyField('id_Another_Fake_Id')); $this->assertTrue(isForeignKeyField('users_id_tech')); $this->assertFalse(isForeignKeyField('_id')); }
function loadDataset() { global $CFG_GLPI; // Unit test data definition $data = ['_version' => 2, 'Entity' => [['name' => '_test_root_entity', 'entities_id' => 0], ['name' => '_test_child_1', 'entities_id' => '_test_root_entity'], ['name' => '_test_child_2', 'entities_id' => '_test_root_entity']], 'Computer' => [['name' => '_test_pc01', 'entities_id' => '_test_root_entity'], ['name' => '_test_pc02', 'entities_id' => '_test_root_entity'], ['name' => '_test_pc11', 'entities_id' => '_test_child_1'], ['name' => '_test_pc12', 'entities_id' => '_test_child_1'], ['name' => '_test_pc21', 'entities_id' => '_test_child_2'], ['name' => '_test_pc22', 'entities_id' => '_test_child_2']], 'Software' => [['name' => '_test_soft', 'entities_id' => '_test_root_entity', 'is_recursive' => 1]], 'SoftwareVersion' => [['name' => '_test_softver_1', 'entities_id' => '_test_root_entity', 'is_recursive' => 1, 'softwares_id' => '_test_soft'], ['name' => '_test_softver_2', 'entities_id' => '_test_root_entity', 'is_recursive' => 1, 'softwares_id' => '_test_soft']], 'Printer' => [['name' => '_test_printer_all', 'entities_id' => '_test_root_entity', 'is_recursive' => 1], ['name' => '_test_printer_ent0', 'entities_id' => '_test_root_entity', 'is_recursive' => 0], ['name' => '_test_printer_ent1', 'entities_id' => '_test_child_1', 'is_recursive' => 0], ['name' => '_test_printer_ent2', 'entities_id' => '_test_child_2', 'is_recursive' => 0]], 'User' => [['name' => TU_USER, 'password' => TU_PASS, 'password2' => TU_PASS, 'entities_id' => '_test_root_entity', 'profiles_id' => 4, '_entities_id' => '_test_root_entity', '_profiles_id' => 4, '_is_recursive' => 1]]]; // To bypass various right checks $_SESSION['glpicronuserrunning'] = "cron_phpunit"; $_SESSION['glpi_use_mode'] = Session::NORMAL_MODE; $CFG_GLPI['root_doc'] = '/glpi'; // need to set theses in DB, because tests for API use http call and this bootstrap file is not called Config::setConfigurationValues('core', ['url_base' => GLPI_URI, 'url_base_api' => GLPI_URI . '/apirest.php']); $CFG_GLPI['url_base'] = GLPI_URI; $CFG_GLPI['url_base_api'] = GLPI_URI . '/apirest.php'; @mkdir(GLPI_LOG_DIR, 0755, true); $conf = Config::getConfigurationValues('phpunit'); if (isset($conf['dataset']) && $conf['dataset'] == $data['_version']) { printf("\nGLPI dataset version %d already loaded\n\n", $data['_version']); } else { printf("\nLoading GLPI dataset version %d\n", $data['_version']); $ids = array(); foreach ($data as $type => $inputs) { if ($type[0] == '_') { continue; } foreach ($inputs as $input) { // Resolve FK foreach ($input as $k => $v) { if (isForeignKeyField($k) && isset($ids[$v]) && !is_numeric($v)) { $input[$k] = $ids[$v]; } } if (isset($input['name']) && ($item = getItemByTypeName($type, $input['name']))) { $input['id'] = $ids[$input['name']] = $item->getField('id'); $item->update($input); echo "."; } else { // Not found, create it $item = getItemForItemtype($type); $id = $item->add($input); echo "+"; if (isset($input['name'])) { $ids[$input['name']] = $id; } } } } echo "\nDone\n\n"; Config::setConfigurationValues('phpunit', ['dataset' => $data['_version']]); } }
$tmpitem = new $data[$field](); if ($tmpitem->getFromDB($data['items_id'])) { $outputval = sprintf(__('%1$s - %2$s'), $tmpitem->getTypeName(), $tmpitem->getName()); } else { $outputval = $tmpitem->getTypeName(); } } else { $outputval = $data[$field]; } } $outputval = Toolbox::unclean_cross_side_scripting_deep($outputval); if ($displaywith) { foreach ($_POST['displaywith'] as $key) { if (isset($data[$key])) { $withoutput = $data[$key]; if (isForeignKeyField($key)) { $withoutput = Dropdown::getDropdownName(getTableNameForForeignKeyField($key), $data[$key]); } if (strlen($withoutput) > 0 && $withoutput != ' ') { $outputval = sprintf(__('%1$s - %2$s'), $outputval, $withoutput); } } } } $ID = $data['id']; $addcomment = ""; $title = $outputval; if (isset($data["comment"])) { if (isset($data['transcomment']) && !empty($data['transcomment'])) { $addcomment .= $data['transcomment']; } else {
/** * transform array of fields passed in parameter : * change value from integer id to string name of foreign key * You can pass an array of array, this method is recursive. * * @param $fields array to check and transform * @param $expand bool array of option to enable, could be : * - expand_dropdowns (default false) * - get_hateoas (default true) * * @return array altered $fields **/ protected static function parseDropdowns($fields, $params = array()) { // default params $default = array('expand_dropdowns' => false, 'get_hateoas' => true); $params = array_merge($default, $params); // parse fields recursively foreach ($fields as $key => &$value) { if (is_array($value)) { $value = self::parseDropdowns($value); } if (is_integer($key)) { continue; } if (isForeignKeyField($key)) { // specific key transformations if ($key == "items_id" && isset($fields['itemtype'])) { $key = getForeignKeyFieldForItemType($fields['itemtype']); } if ($key == "auths_id" && isset($fields['authtype']) && $fields['authtype'] == Auth::LDAP) { $key = "authldaps_id"; } if ($key == "default_requesttypes_id") { $key = "requesttypes_id"; } if (!empty($value) || $key == 'entities_id' && $value >= 0) { $tablename = getTableNameForForeignKeyField($key); $itemtype = getItemTypeForTable($tablename); // get hateoas if ($params['get_hateoas']) { $fields['links'][] = array('rel' => $itemtype, 'href' => self::$api_url . "/{$itemtype}/" . $value); } // expand dropdown if ($params['expand_dropdowns']) { $value = Dropdown::getDropdownName($tablename, $value); // fix value for inexistent items if ($value == " ") { $value = ""; } } } } } return $fields; }
function replaceids($array) { global $CFG_GLPI; foreach ($array as $key => $value) { if (!is_int($key) && ($key == "software" || $key == 'ipaddress' || $key == 'internalport')) { // do nothing } else { //if (is_array($value)) { if ((array) $value === $value) { $array[$key] = $this->replaceids($value); } else { if (!is_numeric($key) && ($key == "manufacturers_id" || $key == 'bios_manufacturers_id')) { $manufacturer = new Manufacturer(); $array[$key] = $manufacturer->processName($value); if ($key == 'bios_manufacturers_id') { $this->foreignkey_itemtype[$key] = getItemTypeForTable(getTableNameForForeignKeyField('manufacturers_id')); } else { if (isset($CFG_GLPI['plugin_fusioninventory_computermanufacturer'][$value])) { $CFG_GLPI['plugin_fusioninventory_computermanufacturer'][$value] = $array[$key]; } } } if (!is_numeric($key)) { if ($key == "bios_manufacturers_id") { $array[$key] = Dropdown::importExternal($this->foreignkey_itemtype['manufacturers_id'], $value); } else { if ($key == "locations_id") { $array[$key] = Dropdown::importExternal('Location', $value, $_SESSION["plugin_fusioninventory_entity"]); } else { if (isset($this->foreignkey_itemtype[$key])) { $array[$key] = Dropdown::importExternal($this->foreignkey_itemtype[$key], $value, $_SESSION["plugin_fusioninventory_entity"]); } else { if (isForeignKeyField($key) && $key != "users_id") { $this->foreignkey_itemtype[$key] = getItemTypeForTable(getTableNameForForeignKeyField($key)); if ($key == 'computermodels_id') { if (isset($CFG_GLPI['plugin_fusioninventory_computermanufacturer'])) { $manufacturer = current($CFG_GLPI['plugin_fusioninventory_computermanufacturer']); $array[$key] = Dropdown::importExternal($this->foreignkey_itemtype[$key], $value, $_SESSION["plugin_fusioninventory_entity"], array('manufacturer' => $manufacturer)); } else { $array[$key] = 0; } } else { $array[$key] = Dropdown::importExternal($this->foreignkey_itemtype[$key], $value, $_SESSION["plugin_fusioninventory_entity"]); } } } } } } } } } return $array; }
function loadDataset() { global $CFG_GLPI; // Unit test data definition $data = ['_version' => 3, 'Entity' => [['name' => '_test_root_entity', 'entities_id' => 0], ['name' => '_test_child_1', 'entities_id' => '_test_root_entity'], ['name' => '_test_child_2', 'entities_id' => '_test_root_entity']], 'Computer' => [['name' => '_test_pc01', 'entities_id' => '_test_root_entity', 'comment' => 'Comment for computer _test_pc01'], ['name' => '_test_pc02', 'entities_id' => '_test_root_entity', 'comment' => 'Comment for computer _test_pc02'], ['name' => '_test_pc11', 'entities_id' => '_test_child_1'], ['name' => '_test_pc12', 'entities_id' => '_test_child_1'], ['name' => '_test_pc21', 'entities_id' => '_test_child_2'], ['name' => '_test_pc22', 'entities_id' => '_test_child_2']], 'Software' => [['name' => '_test_soft', 'entities_id' => '_test_root_entity', 'is_recursive' => 1]], 'SoftwareVersion' => [['name' => '_test_softver_1', 'entities_id' => '_test_root_entity', 'is_recursive' => 1, 'softwares_id' => '_test_soft'], ['name' => '_test_softver_2', 'entities_id' => '_test_root_entity', 'is_recursive' => 1, 'softwares_id' => '_test_soft']], 'Printer' => [['name' => '_test_printer_all', 'entities_id' => '_test_root_entity', 'is_recursive' => 1], ['name' => '_test_printer_ent0', 'entities_id' => '_test_root_entity', 'is_recursive' => 0], ['name' => '_test_printer_ent1', 'entities_id' => '_test_child_1', 'is_recursive' => 0], ['name' => '_test_printer_ent2', 'entities_id' => '_test_child_2', 'is_recursive' => 0]], 'User' => [['name' => TU_USER, 'password' => TU_PASS, 'password2' => TU_PASS, 'entities_id' => '_test_root_entity', 'profiles_id' => 4, '_entities_id' => '_test_root_entity', '_profiles_id' => 4, '_is_recursive' => 1]], 'TaskCategory' => [['is_recursive' => 1, 'name' => '_cat_1', 'completename' => '_cat_1', 'comment' => 'Comment for category _cat_1', 'level' => 1], ['is_recursive' => 1, 'taskcategories_id' => '_cat_1', 'name' => '_subcat_1', 'completename' => '_cat_1 > _subcat_1', 'comment' => 'Comment for sub-category _subcat_1', 'level' => 2]], 'DropdownTranslation' => [['items_id' => '_cat_1', 'itemtype' => 'TaskCategory', 'language' => 'fr_FR', 'field' => 'name', 'value' => 'FR - _cat_1'], ['items_id' => '_cat_1', 'itemtype' => 'TaskCategory', 'language' => 'fr_FR', 'field' => 'comment', 'value' => 'FR - Commentaire pour catégorie _cat_1'], ['items_id' => '_subcat_1', 'itemtype' => 'TaskCategory', 'language' => 'fr_FR', 'field' => 'name', 'value' => 'FR - _subcat_1'], ['items_id' => '_subcat_1', 'itemtype' => 'TaskCategory', 'language' => 'fr_FR', 'field' => 'comment', 'value' => 'FR - Commentaire pour sous-catégorie _subcat_1']], 'Contact' => [['name' => '_contact01_name', 'firstname' => '_contact01_firstname', 'phone' => '0123456789', 'phone2' => '0123456788', 'mobile' => '0623456789', 'fax' => '0123456787', 'email' => '*****@*****.**', 'comment' => 'Comment for contact _contact01_name']], 'Supplier' => [['name' => '_suplier01_name', 'phonenumber' => '0123456789', 'fax' => '0123456787', 'email' => 'info@_supplier01_name.com', 'comment' => 'Comment for supplier _suplier01_name']], 'Location' => [['name' => '_location01', 'comment' => 'Comment for location _location01']], 'Netpoint' => [['name' => '_netpoint01', 'locations_id' => '_location01', 'comment' => 'Comment for netpoint _netpoint01']], 'BudgetType' => [['name' => '_budgettype01', 'comment' => 'Comment for budgettype _budgettype01']], 'Budget' => [['name' => '_budget01', 'comment' => 'Comment for budget _budget01', 'locations_id' => '_location01', 'budgettypes_id' => '_budgettype01', 'begin_date' => '2016-10-18', 'end_date' => '2016-12-31']], 'Ticket' => [['name' => '_ticket01', 'content' => 'Content for ticket _ticket01', 'users_id_recipient' => TU_USER]], 'TicketTask' => [['tickets_id' => '_ticket01', 'taskcategories_id' => '_subcat_1', 'users_id' => TU_USER, 'content' => 'Task to be done', 'is_private' => 0, 'users_id_tech' => TU_USER, 'date' => '2016-10-19 11:50:50']], 'UserEmail' => [['users_id' => TU_USER, 'is_default' => '1', 'is_dynamic' => '0', 'email' => TU_USER . '@glpi.com']]]; // To bypass various right checks $_SESSION['glpishowallentities'] = 1; $_SESSION['glpicronuserrunning'] = "cron_phpunit"; $_SESSION['glpi_use_mode'] = Session::NORMAL_MODE; $CFG_GLPI['root_doc'] = '/glpi'; // need to set theses in DB, because tests for API use http call and this bootstrap file is not called Config::setConfigurationValues('core', ['url_base' => GLPI_URI, 'url_base_api' => GLPI_URI . '/apirest.php']); $CFG_GLPI['url_base'] = GLPI_URI; $CFG_GLPI['url_base_api'] = GLPI_URI . '/apirest.php'; is_dir(GLPI_LOG_DIR) or mkdir(GLPI_LOG_DIR, 0755, true); $conf = Config::getConfigurationValues('phpunit'); if (isset($conf['dataset']) && $conf['dataset'] == $data['_version']) { printf("\nGLPI dataset version %d already loaded\n\n", $data['_version']); } else { printf("\nLoading GLPI dataset version %d\n", $data['_version']); $ids = array(); foreach ($data as $type => $inputs) { if ($type[0] == '_') { continue; } foreach ($inputs as $input) { // Resolve FK foreach ($input as $k => $v) { // $foreigntype = $type; // by default same type than current type (is the case of the dropdowns) $foreigntype = false; $match = array(); if (isForeignKeyField($k) && (preg_match("/(.*s)_id\$/", $k, $match) || preg_match("/(.*s)_id_/", $k, $match))) { $foreigntype = array_pop($match); $foreigntype = getItemTypeForTable("glpi_{$foreigntype}"); } if ($foreigntype && isset($ids[$foreigntype][$v]) && !is_numeric($v)) { $input[$k] = $ids[$foreigntype][$v]; } else { if ($k == 'items_id' && isset($input['itemtype']) && isset($ids[$input['itemtype']][$v]) && !is_numeric($v)) { $input[$k] = $ids[$input['itemtype']][$v]; } else { if ($foreigntype && $foreigntype != 'UNKNOWN' && !is_numeric($v)) { // not found in ids array, then must get it from DB if ($obj = getItemByTypeName($foreigntype, $v)) { $input[$k] = $obj->getID(); } } } } } if (isset($input['name']) && ($item = getItemByTypeName($type, $input['name']))) { $input['id'] = $ids[$type][$input['name']] = $item->getField('id'); $item->update($input); echo "."; } else { // Not found, create it $item = getItemForItemtype($type); $id = $item->add($input); echo "+"; if (isset($input['name'])) { $ids[$type][$input['name']] = $id; } } } } echo "\nDone\n\n"; Config::setConfigurationValues('phpunit', ['dataset' => $data['_version']]); } }