Exemple #1
0
 * @internal Revisions:
 *	20101008 - Julian - reload navBar after changing personal data (localization)
 */
require_once '../../config.inc.php';
require_once 'users.inc.php';
require_once '../../lib/api/APIKey.php';
testlinkInitPage($db);
$templateCfg = templateConfiguration();
$args = init_args();
$user = new tlUser($args->userID);
$user->readFromDB($db);
$gui = new stdClass();
$gui->tproject_id = $args->tproject_id;
$gui->update_title_bar = 0;
$gui->external_password_mgmt = tlUser::isPasswordMgtExternal();
$gui->mgt_view_events = $user->hasRight($db, "mgt_view_events", $gui->tproject_id);
$op = new stdClass();
$op->auditMsg = null;
$op->user_feedback = null;
$op->status = tl::OK;
$doUpdate = false;
switch ($args->doAction) {
    case 'editUser':
        $doUpdate = true;
        foreach ($args->user as $key => $value) {
            $user->{$key} = $value;
        }
        $op->status = tl::OK;
        $op->auditMsg = "audit_user_saved";
        $op->user_feedback = lang_get('result_user_changed');
        $gui->update_title_bar = 1;
 function get_accessible_for_user($user_id, $output_type = 'map', $order_by = null)
 {
     $my['order_by'] = is_null($order_by) ? config_get('gui')->tprojects_combo_order_by : $order_by;
     $items = array();
     // Get default role
     $sql = " SELECT id,role_id FROM {$this->tables['users']} where id={$user_id}";
     $user_info = $this->db->get_recordset($sql);
     $role_id = $user_info[0]['role_id'];
     $sql = " SELECT nodes_hierarchy.name,testprojects.*\n \t          FROM {$this->tables['nodes_hierarchy']} nodes_hierarchy\n \t          JOIN {$this->object_table} testprojects ON nodes_hierarchy.id=testprojects.id\n\t          LEFT OUTER JOIN {$this->tables['user_testproject_roles']} user_testproject_roles\n\t\t        ON testprojects.id = user_testproject_roles.testproject_id AND\n\t\t \t      user_testproject_roles.user_id = {$user_id} WHERE 1=1 ";
     // Private test project
     if ($role_id != TL_ROLES_ADMIN) {
         if ($role_id != TL_ROLES_NO_RIGHTS) {
             // $sql .=  "(role_id IS NULL OR role_id != ".TL_ROLES_NO_RIGHTS.")";
             // (A AND (B OR C) ) OR (NOT A AND C)
             $sql .= " AND ";
             $sql_public = " ( is_public = 1 AND (role_id IS NULL OR role_id != " . TL_ROLES_NO_RIGHTS . ") )";
             $sql_private = " ( is_public = 0 AND role_id != " . TL_ROLES_NO_RIGHTS . ") ";
             $sql .= " ( {$sql_public}  OR {$sql_private} ) ";
         } else {
             // User need specific role
             $sql .= " AND (role_id IS NOT NULL AND role_id != " . TL_ROLES_NO_RIGHTS . ")";
         }
     }
     $userObj = new tlUser($user_id);
     $userObj->readFromDB($this->db);
     if ($userObj->hasRight($this->db, 'mgt_modify_product') != 'yes') {
         $sql .= " AND active=1 ";
     }
     $sql .= $my['order_by'];
     if ($output_type == 'array_of_map') {
         $items = $this->db->get_recordset($sql);
         $this->parseTestProjectRecordset($items);
         $do_post_process = 0;
     } else {
         $arrTemp = $this->db->fetchRowsIntoMap($sql, 'id');
         $do_post_process = 1;
     }
     if ($do_post_process && sizeof($arrTemp)) {
         switch ($output_type) {
             case 'map':
                 foreach ($arrTemp as $id => $row) {
                     $noteActive = '';
                     if (!$row['active']) {
                         $noteActive = TL_INACTIVE_MARKUP;
                     }
                     $items[$id] = $noteActive . $row['name'];
                 }
                 break;
             case 'map_of_map':
                 foreach ($arrTemp as $id => $row) {
                     $items[$id] = array('name' => $row['name'], 'active' => $row['active']);
                 }
                 break;
         }
     }
     return $items;
 }
Exemple #3
0
/**
 * Check if the user has the needed rights to view this page (testplan metrics).
 * 
 * @author Andreas Simon
 * @param Database $db reference to database object
 * @param tlUser $user reference to user object
 */
function checkRights(&$db, &$user)
{
    return $user->hasRight($db, 'testplan_metrics');
}
function saveImportedTCData(&$db, $tcData, $tproject_id, $container_id, $userID, $kwMap, $duplicatedLogic = array('hitCriteria' => 'name', 'actionOnHit' => null))
{
    static $messages;
    static $fieldSizeCfg;
    static $feedbackMsg;
    static $tcase_mgr;
    static $tproject_mgr;
    static $req_spec_mgr;
    static $req_mgr;
    static $safeSizeCfg;
    static $linkedCustomFields;
    static $tprojectHas;
    static $reqSpecSet;
    static $getVersionOpt;
    static $userObj;
    static $tcasePrefix;
    static $glueChar;
    static $userRights;
    $ret = null;
    if (!$tcData) {
        return;
    }
    // $tprojectHas = array('customFields' => false, 'reqSpec' => false);
    $hasCustomFieldsInfo = false;
    $hasRequirements = false;
    if (is_null($messages)) {
        $feedbackMsg = array();
        $messages = array();
        $fieldSizeCfg = config_get('field_size');
        $tcase_mgr = new testcase($db);
        $tproject_mgr = new testproject($db);
        $req_spec_mgr = new requirement_spec_mgr($db);
        $req_mgr = new requirement_mgr($db);
        $userObj = new tlUser($userID);
        $userObj->readFromDB($db, tlUser::TLOBJ_O_SEARCH_BY_ID);
        $userRights['can_edit_executed'] = $userObj->hasRight($db, 'testproject_edit_executed_testcases', $tproject_id);
        $k2l = array('already_exists_updated', 'original_name', 'testcase_name_too_long', 'already_exists_not_updated', 'start_warning', 'end_warning', 'testlink_warning', 'hit_with_same_external_ID');
        foreach ($k2l as $k) {
            $messages[$k] = lang_get($k);
        }
        $messages['start_feedback'] = $messages['start_warning'] . "\n" . $messages['testlink_warning'] . "\n";
        $messages['cf_warning'] = lang_get('no_cf_defined_can_not_import');
        $messages['reqspec_warning'] = lang_get('no_reqspec_defined_can_not_import');
        $feedbackMsg['cfield'] = lang_get('cf_value_not_imported_missing_cf_on_testproject');
        $feedbackMsg['tcase'] = lang_get('testcase');
        $feedbackMsg['req'] = lang_get('req_not_in_req_spec_on_tcimport');
        $feedbackMsg['req_spec'] = lang_get('req_spec_ko_on_tcimport');
        // because name can be changed automatically during item creation
        // to avoid name conflict adding a suffix automatically generated,
        // is better to use a max size < max allowed size
        $safeSizeCfg = new stdClass();
        $safeSizeCfg->testcase_name = $fieldSizeCfg->testcase_name * 0.8;
        // Get CF with scope design time and allowed for test cases linked to this test project
        $linkedCustomFields = $tcase_mgr->cfield_mgr->get_linked_cfields_at_design($tproject_id, 1, null, 'testcase', null, 'name');
        $tprojectHas['customFields'] = !is_null($linkedCustomFields);
        $reqSpecSet = $tproject_mgr->getReqSpec($tproject_id, null, array('RSPEC.id', 'NH.name AS title', 'RSPEC.doc_id as rspec_doc_id', 'REQ.req_doc_id'), 'req_doc_id');
        $tprojectHas['reqSpec'] = !is_null($reqSpecSet) && count($reqSpecSet) > 0;
        $getVersionOpt = array('output' => 'minimun');
        $tcasePrefix = $tproject_mgr->getTestCasePrefix($tproject_id);
        $glueChar = config_get('testcase_cfg')->glue_character;
    }
    $resultMap = array();
    $tc_qty = sizeof($tcData);
    $userIDCache = array();
    for ($idx = 0; $idx < $tc_qty; $idx++) {
        $tc = $tcData[$idx];
        $name = $tc['name'];
        $summary = $tc['summary'];
        $steps = $tc['steps'];
        // I've changed value to use when order has not been provided
        // from testcase:DEFAULT_ORDER to a counter, because with original solution
        // an issue arise with 'save execution and go next'
        // if use has not provided order I think is OK TestLink make any choice.
        $node_order = isset($tc['node_order']) ? intval($tc['node_order']) : $idx + 1;
        $internalid = $tc['internalid'];
        $preconditions = $tc['preconditions'];
        $exec_type = isset($tc['execution_type']) ? $tc['execution_type'] : TESTCASE_EXECUTION_TYPE_MANUAL;
        $importance = isset($tc['importance']) ? $tc['importance'] : MEDIUM;
        $attr = null;
        if (isset($tc['estimated_exec_duration']) && !is_null($tc['estimated_exec_duration'])) {
            $attr['estimatedExecDuration'] = trim($tc['estimated_exec_duration']);
            $attr['estimatedExecDuration'] = $attr['estimatedExecDuration'] == '' ? null : floatval($attr['estimatedExecDuration']);
        }
        if (isset($tc['status'])) {
            $attr['status'] = trim($tc['status']);
        }
        $externalid = $tc['externalid'];
        if (intval($externalid) <= 0) {
            $externalid = null;
        }
        $personID = $userID;
        if (!is_null($tc['author_login'])) {
            if (isset($userIDCache[$tc['author_login']])) {
                $personID = $userIDCache[$tc['author_login']];
            } else {
                $userObj->login = $tc['author_login'];
                if ($userObj->readFromDB($db, tlUser::USER_O_SEARCH_BYLOGIN) == tl::OK) {
                    $personID = $userObj->dbID;
                }
                // I will put always a valid userID on this cache,
                // this way if author_login does not exit, and is used multiple times
                // i will do check for existence JUST ONCE.
                $userIDCache[$tc['author_login']] = $personID;
            }
        }
        $name_len = tlStringLen($name);
        if ($name_len > $fieldSizeCfg->testcase_name) {
            // Will put original name inside summary
            $xx = $messages['start_feedback'];
            $xx .= sprintf($messages['testcase_name_too_long'], $name_len, $fieldSizeCfg->testcase_name) . "\n";
            $xx .= $messages['original_name'] . "\n" . $name . "\n" . $messages['end_warning'] . "\n";
            $summary = nl2br($xx) . $summary;
            $name = tlSubStr($name, 0, $safeSizeCfg->testcase_name);
        }
        $kwIDs = null;
        if (isset($tc['keywords']) && $tc['keywords']) {
            $kwIDs = implode(",", buildKeywordList($kwMap, $tc['keywords']));
        }
        $doCreate = true;
        if ($duplicatedLogic['actionOnHit'] == 'update_last_version') {
            $updOpt['blockIfExecuted'] = !$userRights['can_edit_executed'];
            switch ($duplicatedLogic['hitCriteria']) {
                case 'name':
                    $info = $tcase_mgr->getDuplicatesByName($name, $container_id);
                    break;
                case 'internalID':
                    $dummy = $tcase_mgr->tree_manager->get_node_hierarchy_info($internalid, $container_id);
                    if (!is_null($dummy)) {
                        $info = null;
                        // TICKET 4925
                        $info[$internalid] = $dummy;
                    }
                    break;
                case 'externalID':
                    $info = $tcase_mgr->get_by_external($externalid, $container_id);
                    break;
            }
            if (!is_null($info)) {
                $tcase_qty = count($info);
                switch ($tcase_qty) {
                    case 1:
                        $doCreate = false;
                        $tcase_id = key($info);
                        $last_version = $tcase_mgr->get_last_version_info($tcase_id, $getVersionOpt);
                        $tcversion_id = $last_version['id'];
                        $ret = $tcase_mgr->update($tcase_id, $tcversion_id, $name, $summary, $preconditions, $steps, $personID, $kwIDs, $node_order, $exec_type, $importance, $attr, $updOpt);
                        $ret['id'] = $tcase_id;
                        $ret['tcversion_id'] = $tcversion_id;
                        if ($ret['status_ok']) {
                            $resultMap[] = array($name, $messages['already_exists_updated']);
                        } else {
                            if ($ret['reason'] == '') {
                                $resultMap[] = array($name, sprintf($messages['already_exists_not_updated'], $tcasePrefix . $glueChar . $externalid, $tcasePrefix . $glueChar . $ret['hit_on']['tc_external_id']));
                            } else {
                                $resultMap[] = array($name, $ret['msg']);
                            }
                        }
                        break;
                    case 0:
                        $doCreate = true;
                        break;
                    default:
                        $doCreate = false;
                        break;
                }
            }
        }
        if ($doCreate) {
            // Want to block creation of with existent EXTERNAL ID, if containers ARE DIFFERENT.
            $item_id = intval($tcase_mgr->getInternalID($externalid, array('tproject_id' => $tproject_id)));
            if ($item_id > 0) {
                // who is his parent ?
                $owner = $tcase_mgr->getTestSuite($item_id);
                if ($owner != $container_id) {
                    // Get full path of existent Test Cases
                    $stain = $tcase_mgr->tree_manager->get_path($item_id, null, 'name');
                    $n = count($stain);
                    $stain[$n - 1] = $tcasePrefix . config_get('testcase_cfg')->glue_character . $externalid . ':' . $stain[$n - 1];
                    $stain = implode('/', $stain);
                    $resultMap[] = array($name, $messages['hit_with_same_external_ID'] . $stain);
                    $doCreate = false;
                }
            }
        }
        if ($doCreate) {
            $createOptions = array('check_duplicate_name' => testcase::CHECK_DUPLICATE_NAME, 'action_on_duplicate_name' => $duplicatedLogic['actionOnHit'], 'external_id' => $externalid, 'importLogic' => $duplicatedLogic);
            if (!is_null($attr)) {
                $createOptions += $attr;
            }
            if ($ret = $tcase_mgr->create($container_id, $name, $summary, $preconditions, $steps, $personID, $kwIDs, $node_order, testcase::AUTOMATIC_ID, $exec_type, $importance, $createOptions)) {
                $resultMap[] = array($name, $ret['msg']);
            }
        }
        // Custom Fields Management
        // Check if CF with this name and that can be used on Test Cases is defined in current Test Project.
        // If Check fails => give message to user.
        // Else Import CF data
        //
        $hasCustomFieldsInfo = isset($tc['customfields']) && !is_null($tc['customfields']);
        if ($hasCustomFieldsInfo && !is_null($ret)) {
            if ($tprojectHas['customFields']) {
                $msg = processCustomFields($tcase_mgr, $name, $ret['id'], $ret['tcversion_id'], $tc['customfields'], $linkedCustomFields, $feedbackMsg);
                if (!is_null($msg)) {
                    $resultMap = array_merge($resultMap, $msg);
                }
            } else {
                // Can not import Custom Fields Values, give feedback
                $msg[] = array($name, $messages['cf_warning']);
                $resultMap = array_merge($resultMap, $msg);
            }
        }
        $hasRequirements = isset($tc['requirements']) && !is_null($tc['requirements']);
        if ($hasRequirements) {
            if ($tprojectHas['reqSpec']) {
                $msg = processRequirements($db, $req_mgr, $name, $ret['id'], $tc['requirements'], $reqSpecSet, $feedbackMsg, $userID);
                if (!is_null($msg)) {
                    $resultMap = array_merge($resultMap, $msg);
                }
            } else {
                $msg[] = array($name, $messages['reqspec_warning']);
                $resultMap = array_merge($resultMap, $msg);
            }
        }
    }
    return $resultMap;
}