Example #1
0
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 $tcStatusConfig;
    if (!$tcData) {
        return;
    }
    // $tprojectHas = array('customFields' => false, 'reqSpec' => false);
    $hasCustomFieldsInfo = false;
    $hasRequirements = false;
    // init static data
    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);
        $messages['cf_warning'] = lang_get('no_cf_defined_can_not_import');
        $messages['reqspec_warning'] = lang_get('no_reqspec_defined_can_not_import');
        $messages['already_exists_updated'] = lang_get('already_exists_updated');
        $messages['original_name'] = lang_get('original_name');
        $messages['testcase_name_too_long'] = lang_get('testcase_name_too_long');
        $messages['start_warning'] = lang_get('start_warning');
        $messages['end_warning'] = lang_get('end_warning');
        $messages['testlink_warning'] = lang_get('testlink_warning');
        $messages['start_feedback'] = $messages['start_warning'] . "\n" . $messages['testlink_warning'] . "\n";
        $messages['create_new_version'] = lang_get('create_new_version');
        $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
        // $customFields=$tproject_mgr->get_linked_custom_fields($tproject_id,'testcase','name');
        // function get_linked_cfields_at_design($tproject_id,$enabled,$filters=null,
        //                                       $node_type=null,$node_id=null,$access_key='id')
        //
        $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');
        $tcStatusConfig = getConfigAndLabels('testCaseStatus');
    }
    $resultMap = array();
    $tc_qty = sizeof($tcData);
    // new dBug($tcData);
    //$xx=$tcase_mgr->getDuplicatesByExternalID(1,$container_id);
    //new dBug($xx);
    //die();
    //
    for ($idx = 0; $idx < $tc_qty; $idx++) {
        $tc = $tcData[$idx];
        $name = $tc['name'];
        $summary = $tc['summary'];
        $steps = $tc['steps'];
        $externalid = $tc['externalid'];
        $internalid = $tc['internalid'];
        $preconditions = $tc['preconditions'];
        $node_order = isset($tc['node_order']) ? intval($tc['node_order']) : testcase::DEFAULT_ORDER;
        $exec_type = isset($tc['execution_type']) ? $tc['execution_type'] : TESTCASE_EXECUTION_TYPE_MANUAL;
        $importance = isset($tc['importance']) ? $tc['importance'] : MEDIUM;
        $status = isset($tc['status']) ? $tc['status'] : null;
        // $tcStatusConfig['cfg']['draft'];
        $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']));
        }
        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[$internalid] = $dummy;
                }
                break;
            case 'externalID':
                $info = $tcase_mgr->get_by_external($externalid, $container_id);
                break;
        }
        $doCreate = true;
        $doAnalisys = !is_null($info);
        $tcase_qty = $doAnalisys ? count($info) : 0;
        if ($doAnalisys) {
            $tcase_id = key($info);
            switch ($duplicatedLogic['actionOnHit']) {
                case 'update_last_version':
                    switch ($tcase_qty) {
                        case 1:
                            $doCreate = false;
                            $last_version = $tcase_mgr->get_last_version_info($tcase_id, $getVersionOpt);
                            $tcversion_id = $last_version['id'];
                            // 20110604 - franciscom
                            // missing optional argument $status
                            $ret = $tcase_mgr->update($tcase_id, $tcversion_id, $name, $summary, $preconditions, $steps, $userID, $kwIDs, $node_order, $exec_type, $importance, $status);
                            $ret['id'] = $tcase_id;
                            $ret['tcversion_id'] = $tcversion_id;
                            $resultMap[] = array($name, $messages['already_exists_updated']);
                            break;
                        case 0:
                            $doCreate = true;
                            break;
                        default:
                            $doCreate = false;
                            break;
                    }
                    break;
                case 'create_new_version':
                    switch ($tcase_qty) {
                        case 1:
                            $doCreate = false;
                            $tcase_id = key($info);
                            $ret = $tcase_mgr->create_new_version($tcase_id, $userID);
                            $tcversion_id = $ret['id'];
                            $msg = sprintf($messages['create_new_version'], $ret['version']);
                            $ret = $tcase_mgr->update($tcase_id, $tcversion_id, $name, $summary, $preconditions, $steps, $userID, $kwIDs, $node_order, $exec_type, $importance, $status);
                            $ret['id'] = $tcase_id;
                            $ret['tcversion_id'] = $tcversion_id;
                            $resultMap[] = array($name, $msg);
                            break;
                        case 0:
                            $doCreate = true;
                            break;
                        default:
                            $doCreate = false;
                            break;
                    }
                    break;
                case 'generate_new':
                    // trick
                    // create() will create a new test case with new name/title when
                    // we ask it:
                    //  'check_duplicate_name' => testcase::CHECK_DUPLICATE_NAME,
                    // we will IGNORE name provided on XML when
                    if ($duplicatedLogic['hitCriteria'] != name) {
                        $name = $info[$tcase_id]['name'];
                    }
                    break;
            }
            // switch
        }
        // if doAnalisys
        if ($doCreate) {
            $createOptions = array('check_duplicate_name' => testcase::CHECK_DUPLICATE_NAME, 'action_on_duplicate_name' => $duplicatedLogic['actionOnHit']);
            if ($ret = $tcase_mgr->create($container_id, $name, $summary, $preconditions, $steps, $userID, $kwIDs, $node_order, testcase::AUTOMATIC_ID, $exec_type, $importance, $status, $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) {
            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);
            }
        }
        // Requirements Management
        // Check if Requirement ...
        // If Check fails => give message to user.
        // Else Import
        //
        $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);
                if (!is_null($msg)) {
                    $resultMap = array_merge($resultMap, $msg);
                }
            } else {
                $msg[] = array($name, $messages['reqspec_warning']);
                $resultMap = array_merge($resultMap, $msg);
            }
        }
    }
    return $resultMap;
}