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;
    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();
        $k2l = array('already_exists_updated', 'original_name', 'testcase_name_too_long', '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);
    }
    $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;
        $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') {
            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;
                        $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);
                        $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;
                }
            }
        }
        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);
            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']);
            }
        }
    }
    return $resultMap;
}
Esempio n. 2
0
function importTestSuitesFromSimpleXML(&$dbHandler, &$xml, $parentID, $tproject_id, $userID, $kwMap, $importIntoProject = 0, $duplicateLogic)
{
    static $tsuiteXML;
    static $tsuiteMgr;
    static $myself;
    static $callCounter = 0;
    $resultMap = array();
    // $callCounter++;
    if (is_null($tsuiteXML)) {
        $tsuiteXML = array();
        $tsuiteXML['elements'] = array('string' => array("details"), 'integer' => array("node_order"));
        $tsuiteXML['attributes'] = array('string' => array("name"));
        $tsuiteMgr = new testsuite($dbHandler);
        $myself = __FUNCTION__;
    }
    if ($xml->getName() == 'testsuite') {
        // getItemsFromSimpleXMLObj() first argument must be an array
        $dummy = getItemsFromSimpleXMLObj(array($xml), $tsuiteXML);
        $tsuite = current($dummy);
        $tsuiteID = $parentID;
        // hmmm, not clear
        if ($tsuite['name'] != "") {
            // Check if Test Suite with this name exists on this container
            // if yes -> update instead of create
            $info = $tsuiteMgr->get_by_name($tsuite['name'], $parentID);
            if (is_null($info)) {
                $ret = $tsuiteMgr->create($parentID, $tsuite['name'], $tsuite['details'], $tsuite['node_order']);
                $tsuiteID = $ret['id'];
            } else {
                $tsuiteID = $info[0]['id'];
                $ret = $tsuiteMgr->update($tsuiteID, $tsuite['name'], $tsuite['details'], null, $tsuite['node_order']);
            }
            unset($tsuite);
            unset($dummy);
            if (!$tsuiteID) {
                return null;
            }
        } else {
            if ($importIntoProject) {
                $tsuiteID = $tproject_id;
            }
        }
        $childrenNodes = $xml->children();
        $loop2do = sizeof($childrenNodes);
        for ($idx = 0; $idx < $loop2do; $idx++) {
            $target = $childrenNodes[$idx];
            switch ($target->getName()) {
                case 'testcase':
                    // getTestCaseSetFromSimpleXMLObj() first argument must be an array
                    $tcData = getTestCaseSetFromSimpleXMLObj(array($target));
                    // 20100904 - francisco.mancardi@gruppotesi.com
                    // echo 'Going to work on Test Case INSIDE Test Suite:' . $tsuite['name'] . '<br>';
                    $resultMap = array_merge($resultMap, saveImportedTCData($dbHandler, $tcData, $tproject_id, $tsuiteID, $userID, $kwMap, $duplicateLogic));
                    unset($tcData);
                    break;
                case 'testsuite':
                    $resultMap = array_merge($resultMap, $myself($dbHandler, $target, $tsuiteID, $tproject_id, $userID, $kwMap, $importIntoProject, $duplicateLogic));
                    break;
                    // do not understand why we need to do this particular logic.
                    // Need to understand
                // do not understand why we need to do this particular logic.
                // Need to understand
                case 'details':
                    if (!$importIntoProject) {
                        $keywords = getKeywordsFromSimpleXMLObj($target->xpath("//keyword"));
                        if ($keywords) {
                            $kwIDs = buildKeywordList($kwMap, $keywords);
                            $tsuiteMgr->addKeywords($tsuiteID, $kwIDs);
                        }
                    }
                    break;
            }
        }
    }
    return $resultMap;
}
function importTestSuitesFromSimpleXML(&$dbHandler, &$xml, $parentID, $tproject_id, $userID, $kwMap, $importIntoProject = 0, $duplicateLogic)
{
    static $tsuiteXML;
    static $tsuiteMgr;
    static $myself;
    static $callCounter = 0;
    static $cfSpec;
    static $doCF;
    $resultMap = array();
    if (is_null($tsuiteXML)) {
        $myself = __FUNCTION__;
        $tsuiteXML = array();
        $tsuiteXML['elements'] = array('string' => array("details" => null), 'integer' => array("node_order" => null));
        $tsuiteXML['attributes'] = array('string' => array("name" => 'trim'));
        $tsuiteMgr = new testsuite($dbHandler);
        $doCF = !is_null($cfSpec = $tsuiteMgr->get_linked_cfields_at_design(null, null, null, $tproject_id, 'name'));
    }
    if ($xml->getName() == 'testsuite') {
        // getItemsFromSimpleXMLObj() first argument must be an array
        $dummy = getItemsFromSimpleXMLObj(array($xml), $tsuiteXML);
        $tsuite = current($dummy);
        $tsuiteID = $parentID;
        // hmmm, not clear
        if ($tsuite['name'] != "") {
            // Check if Test Suite with this name exists on this container
            // if yes -> update instead of create
            $info = $tsuiteMgr->get_by_name($tsuite['name'], $parentID);
            if (is_null($info)) {
                $ret = $tsuiteMgr->create($parentID, $tsuite['name'], $tsuite['details'], $tsuite['node_order']);
                $tsuite['id'] = $ret['id'];
            } else {
                $ret = $tsuiteMgr->update($tsuite['id'] = $info[0]['id'], $tsuite['name'], $tsuite['details'], null, $tsuite['node_order']);
            }
            unset($dummy);
            $tsuiteID = $tsuite['id'];
            // $tsuiteID is needed on more code pieces => DO NOT REMOVE
            if (!$tsuite['id']) {
                return null;
            }
            if ($doCF) {
                $cf = getCustomFieldsFromSimpleXMLObj($xml->custom_fields->custom_field);
                if (!is_null($cf)) {
                    processTestSuiteCF($tsuiteMgr, $xml, $cfSpec, $cf, $tsuite, $tproject_id);
                }
            }
            if ($keywords = getKeywordsFromSimpleXMLObj($xml->keywords->keyword)) {
                $kwIDs = buildKeywordList($kwMap, $keywords);
                $tsuiteMgr->addKeywords($tsuite['id'], $kwIDs);
            }
            unset($tsuite);
        } else {
            if ($importIntoProject) {
                $tsuiteID = intval($tproject_id);
            }
        }
        $childrenNodes = $xml->children();
        $loop2do = sizeof($childrenNodes);
        for ($idx = 0; $idx < $loop2do; $idx++) {
            $target = $childrenNodes[$idx];
            switch ($target->getName()) {
                case 'testcase':
                    // getTestCaseSetFromSimpleXMLObj() first argument must be an array
                    $tcData = getTestCaseSetFromSimpleXMLObj(array($target));
                    $resultMap = array_merge($resultMap, saveImportedTCData($dbHandler, $tcData, $tproject_id, $tsuiteID, $userID, $kwMap, $duplicateLogic));
                    unset($tcData);
                    break;
                case 'testsuite':
                    $resultMap = array_merge($resultMap, $myself($dbHandler, $target, $tsuiteID, $tproject_id, $userID, $kwMap, $importIntoProject, $duplicateLogic));
                    break;
                    // Important Development Notice
                    // Due to XML file structure, while looping
                    // we will find also this children:
                    // node_order,keywords,custom_fields,details
                    //
                    // It's processing to get and save values is done
                    // on other pieces of this code.
                    //
                    // Form a logical point of view seems the better
                    // to consider and process here testcase and testsuite as children.
                    //
            }
        }
    }
    return $resultMap;
}
Esempio n. 4
0
function importTestSuitesFromSimpleXML(&$dbHandler, &$xml, $parentID, $tproject_id, $userID, $kwMap, $importIntoProject = 0)
{
    static $tsuiteXML;
    static $tsuiteMgr;
    static $myself;
    static $callCounter = 0;
    $resultMap = array();
    // $callCounter++;
    if (is_null($tsuiteXML)) {
        $tsuiteXML = array();
        $tsuiteXML['elements'] = array('string' => array("details"), 'integer' => array("node_order"));
        $tsuiteXML['attributes'] = array('string' => array("name"));
        $tsuiteMgr = new testsuite($dbHandler);
        $myself = __FUNCTION__;
    }
    if ($xml->getName() == 'testsuite') {
        // getItemsFromSimpleXMLObj() first argument must be an array
        $dummy = getItemsFromSimpleXMLObj(array($xml), $tsuiteXML);
        $tsuite = current($dummy);
        $tsuiteID = $parentID;
        if ($tsuite['name'] != "") {
            $ret = $tsuiteMgr->create($parentID, $tsuite['name'], $tsuite['details'], $tsuite['node_order']);
            $tsuiteID = $ret['id'];
            unset($tsuite);
            unset($dummy);
            if (!$tsuiteID) {
                return null;
            }
        } else {
            if ($importIntoProject) {
                $tsuiteID = $tproject_id;
            }
        }
        $childrenNodes = $xml->children();
        $loop2do = sizeof($childrenNodes);
        for ($idx = 0; $idx < $loop2do; $idx++) {
            $target = $childrenNodes[$idx];
            switch ($target->getName()) {
                case 'testcase':
                    // getTestCaseSetFromSimpleXMLObj() first argument must be an array
                    $tcData = getTestCaseSetFromSimpleXMLObj(array($target));
                    // TEST
                    $resultMap = array_merge($resultMap, saveImportedTCData($dbHandler, $tcData, $tproject_id, $tsuiteID, $userID, $kwMap));
                    unset($tcData);
                    break;
                case 'testsuite':
                    $resultMap = array_merge($resultMap, $myself($dbHandler, $target, $tsuiteID, $tproject_id, $userID, $kwMap));
                    break;
                    // do not understand why we need to do this particular logic.
                    // Need to understand
                // do not understand why we need to do this particular logic.
                // Need to understand
                case 'details':
                    if (!$importIntoProject) {
                        $keywords = getKeywordsFromSimpleXMLObj($target->xpath("//keyword"));
                        if ($keywords) {
                            $kwIDs = buildKeywordList($kwMap, $keywords);
                            $tsuiteMgr->addKeywords($tsuiteID, $kwIDs);
                        }
                    }
                    break;
            }
        }
    }
    return $resultMap;
}