/**
 * @param object dbHandler reference to db handler
 *
 */
function doImport(&$dbHandler)
{
    $import_msg = array('ok' => array(), 'ko' => array());
    $file_check = array('show_results' => 0, 'status_ok' => 0, 'msg' => '', 'filename' => '', 'import_msg' => $import_msg);
    $key = 'targetFilename';
    $dest = TL_TEMP_PATH . session_id() . "-import_cfields.tmp";
    $source = isset($_FILES[$key]['tmp_name']) ? $_FILES[$key]['tmp_name'] : null;
    if ($source != 'none' && $source != '') {
        $file_check['filename'] = $_FILES[$key]['name'];
        $file_check['status_ok'] = 1;
        if (move_uploaded_file($source, $dest)) {
            $file_check['status_ok'] = !(($xml = @simplexml_load_file_wrapper($dest)) === FALSE);
        }
        if ($file_check['status_ok']) {
            $file_check['show_results'] = 1;
            $cfield_mgr = new cfield_mgr($dbHandler);
            foreach ($xml as $cf) {
                if (is_null($cfield_mgr->get_by_name($cf->name))) {
                    $cfield_mgr->create((array) $cf);
                    $import_msg['ok'][] = sprintf(lang_get('custom_field_imported'), $cf->name);
                } else {
                    $import_msg['ko'][] = sprintf(lang_get('custom_field_already_exists'), $cf->name);
                }
            }
        } else {
            $file_check['msg'] = lang_get('problems_loading_xml_content');
        }
    } else {
        $file_check = array('show_results' => 0, 'status_ok' => 0, 'msg' => lang_get('please_choose_file_to_import'));
    }
    $file_check['import_msg'] = $import_msg;
    return $file_check;
}
Exemple #2
0
/**
 * doExecuteImport
 *
 */
function doExecuteImport($fileName, &$argsObj, &$reqSpecMgr, &$reqMgr)
{
    $retval = new stdClass();
    $retval->items = array();
    $retval->msg = '';
    $retval->file_check = array('status_ok' => 1, 'msg' => 'ok');
    $retval->userFeedback = null;
    $context = new stdClass();
    $context->tproject_id = $argsObj->tproject_id;
    $context->req_spec_id = $argsObj->req_spec_id;
    $context->user_id = $argsObj->user_id;
    $context->importType = $argsObj->importType;
    $opts = array();
    $opts['skipFrozenReq'] = $argsObj->skip_frozen_req ? true : false;
    $opts['hitCriteria'] = $argsObj->hitCriteria;
    $opts['actionOnHit'] = $argsObj->actionOnHit;
    // manage file upload process
    $source = isset($_FILES['uploadedFile']['tmp_name']) ? $_FILES['uploadedFile']['tmp_name'] : null;
    if ($source != 'none' && $source != '') {
        if (move_uploaded_file($source, $fileName)) {
            if ($argsObj->importType == 'XML') {
                $retval->file_check['status_ok'] = !(($xml = simplexml_load_file_wrapper($fileName)) === FALSE);
            }
        }
    } else {
        $retval->file_check = array('status_ok' => 0, 'msg' => lang_get('please_choose_req_file'));
    }
    // ----------------------------------------------------------------------------------------------
    /*
    if($retval->file_check['status_ok']) 
    {
      $isReqSpec = property_exists($xml,'req_spec');
      if(!$isReqSpec && $argsObj->req_spec_id <= 0) 
      {
        $retval->file_check = array('status_ok' => FALSE, 'msg' => lang_get('please_create_req_spec_first'));
      }
    }
    */
    if ($retval->file_check['status_ok']) {
        if ($argsObj->importType == 'XML') {
            // If there is no req_spec in XML, and req_spec_id
            // from context is null, we must raise an error, to avoid ghots requirements in DB
            $isReqSpec = property_exists($xml, 'req_spec');
            if (!$isReqSpec && $argsObj->req_spec_id <= 0) {
                $retval->file_check = array('status_ok' => FALSE, 'msg' => lang_get('please_create_req_spec_first'));
            } else {
                $retval->items = doReqImportFromXML($reqSpecMgr, $reqMgr, $xml, $context, $opts);
            }
        } else {
            $dummy = doReqImportOther($reqMgr, $fileName, $context, $opts);
            $retval->items = $dummy['items'];
            $retval->userFeedback = $dummy['userFeedback'];
        }
        unlink($fileName);
        $retval->msg = lang_get('req_import_finished');
    }
    return $retval;
}
/**
 * @param object dbHandler reference to db handler
 *
 */
function doImport(&$dbHandler, $testproject_id)
{
    $import_msg = array('ok' => array(), 'ko' => array());
    $file_check = array('show_results' => 0, 'status_ok' => 0, 'msg' => '', 'filename' => '', 'import_msg' => $import_msg);
    $key = 'targetFilename';
    $dest = TL_TEMP_PATH . session_id() . "-import_platforms.tmp";
    $fInfo = $_FILES[$key];
    $source = isset($fInfo['tmp_name']) ? $fInfo['tmp_name'] : null;
    if ($source != 'none' && $source != '') {
        $file_check['filename'] = $fInfo['name'];
        $xml = false;
        if (move_uploaded_file($source, $dest)) {
            // http://websec.io/2012/08/27/Preventing-XXE-in-PHP.html
            $xml = @simplexml_load_file_wrapper($dest);
        }
        if ($xml !== FALSE) {
            $file_check['status_ok'] = 1;
            $file_check['show_results'] = 1;
            $platform_mgr = new tlPlatform($dbHandler, $testproject_id);
            $platformsOnSystem = $platform_mgr->getAllAsMap('name', 'rows');
            foreach ($xml as $platform) {
                if (property_exists($platform, 'name')) {
                    // Check if platform with this name already exists on test Project
                    // if answer is yes => update fields
                    $name = trim($platform->name);
                    if (isset($platformsOnSystem[$name])) {
                        $import_msg['ok'][] = sprintf(lang_get('platform_updated'), $platform->name);
                        $platform_mgr->update($platformsOnSystem[$name]['id'], $name, $platform->notes);
                    } else {
                        $import_msg['ok'][] = sprintf(lang_get('platform_imported'), $platform->name);
                        $platform_mgr->create($name, $platform->notes);
                    }
                } else {
                    $import_msg['ko'][] = lang_get('bad_line_skipped');
                }
            }
        } else {
            $file_check['msg'] = lang_get('problems_loading_xml_content');
        }
    } else {
        $msg = getFileUploadErrorMessage($fInfo);
        $file_check = array('show_results' => 0, 'status_ok' => 0, 'msg' => $msg);
    }
    if (count($import_msg['ko']) == 0) {
        $import_msg['ko'] = null;
    }
    $file_check['import_msg'] = $import_msg;
    return $file_check;
}
Exemple #4
0
function check_xml_execution_results($fileName)
{
    $file_check = array('status_ok' => 0, 'msg' => 'xml_ko');
    $xml = @simplexml_load_file_wrapper($fileName);
    if ($xml !== FALSE) {
        $file_check = array('status_ok' => 1, 'msg' => 'ok');
        $elementName = $xml->getName();
        if ($elementName != 'results') {
            $file_check = array('status_ok' => 0, 'msg' => lang_get('wrong_results_import_format'));
        }
    }
    return $file_check;
}
function importIssueFromXML(&$db, $fileName, $parentID, $tproject_id, $userID, $options = null)
{
    $xmlTCs = null;
    $resultMap = null;
    $my = array();
    $my['options'] = array('useRecursion' => false, 'importIntoProject' => 0, 'duplicateLogic' => array('hitCriteria' => 'name', 'actionOnHit' => null));
    $my['options'] = array_merge($my['options'], (array) $options);
    foreach ($my['options'] as $varname => $value) {
        ${$varname} = $value;
    }
    if (file_exists($fileName)) {
        $xml = @simplexml_load_file_wrapper($fileName);
        if ($xml !== FALSE) {
            $resultMap = importTestCasesFromIssueSimpleXML($db, $xml, $parentID, $tproject_id, $userID, null, $duplicateLogic);
        }
    }
    return $resultMap;
}
Exemple #6
0
/**
 * @internal revisions
 */
function importTestPlanLinksFromXML(&$dbHandler, &$tplanMgr, $targetFile, $contextObj)
{
    //   <testplan>
    //     <name></name>
    //     <platforms>
    //       <platform>
    //         <name> </name>
    //         <internal_id></internal_id>
    //       </platform>
    //       <platform>
    //       ...
    //       </platform>
    //     </platforms>
    //     <executables>
    //       <link>
    //         <platform>
    //           <name> </name>
    //         </platform>
    //         <testcase>
    //           <name> </name>
    //           <externalid> </externalid>
    //           <version> </version>
    //           <execution_order> </execution_order>
    //         </testcase>
    //       </link>
    //       <link>
    //       ...
    //       </link>
    //     </executables>
    //   </testplan>
    // </xml>
    $msg = array();
    $labels = init_labels(array('link_without_required_platform' => null, 'ok' => null, 'link_without_platform_element' => null, 'no_platforms_on_tproject' => null, 'tcase_link_updated' => null, 'link_with_platform_not_needed' => null, 'tproject_has_zero_testcases' => null, 'platform_not_on_tproject' => null, 'platform_linked' => null, 'platform_not_linked' => null, 'tcase_doesnot_exist' => null, 'tcversion_doesnot_exist' => null, 'not_imported' => null, 'link_to_tplan_feedback' => null, 'link_to_platform' => null));
    // Double Check
    // Check if Test Plan Parent (Test Project) has testcases, if not abort
    $tprojectMgr = new testproject($dbHandler);
    $tprojectInfo = $tprojectMgr->get_by_id($contextObj->tproject_id);
    $tcasePrefix = $tprojectInfo['prefix'] . config_get('testcase_cfg')->glue_character;
    $tprojectHasTC = $tprojectMgr->count_testcases($contextObj->tproject_id) > 0;
    if (!$tprojectHasTC) {
        $msg[] = array(sprintf($labels['tproject_has_zero_testcases'], $tprojectInfo['name']), $labels['not_imported']);
        return $msg;
        // >>>-----> Bye
    }
    $xml = @simplexml_load_file_wrapper($targetFile);
    if ($xml !== FALSE) {
        $tcaseMgr = new testcase($dbHandler);
        $tcaseSet = array();
        $tprojectMgr->get_all_testcases_id($contextObj->tproject_id, $tcaseSet, array('output' => 'external_id'));
        $tcaseSet = array_flip($tcaseSet);
        // Test Plan name will not be used
        // <testplan>  <name></name>
        //
        // Platform definition info will not be used
        //
        // I will try to link the platforms if are defined
        $status_ok = true;
        if (property_exists($xml, 'platforms')) {
            $platformMgr = new tlPlatform($dbHandler, $contextObj->tproject_id);
            $platformUniverse = $platformMgr->getAllAsMap();
            if (is_null($platformUniverse)) {
                $status_ok = false;
                $msg[] = array($labels['no_platforms_on_tproject'], $labels['not_imported']);
            } else {
                $platformUniverse = array_flip($platformUniverse);
                $op = processPlatforms($platformMgr, $tplanMgr, $platformUniverse, $xml->platforms, $labels, $contextObj->tplan_id);
                $status_ok = $op['status_ok'];
                $msg = $op['msg'];
            }
        }
        if ($status_ok && $xml->xpath('//executables')) {
            $tables = tlObjectWithDB::getDBTables(array('testplan_tcversions'));
            $platformSet = $tplanMgr->getPlatforms($contextObj->tplan_id, array('outputFormat' => 'mapAccessByName'));
            $targetHasPlatforms = count($platformSet) > 0;
            $xmlLinks = $xml->executables->children();
            $loops2do = count($xmlLinks);
            // new dBug($platformSet);
            for ($idx = 0; $idx < $loops2do; $idx++) {
                // if Target Test Plan has platforms and importing file NO => Fatal Error
                $targetName = null;
                $platformID = -1;
                $linkWithPlatform = false;
                $status_ok = false;
                $dummy_msg = null;
                $import_status = $labels['ok'];
                if ($platformElementExists = property_exists($xmlLinks[$idx], 'platform')) {
                    $targetName = trim((string) $xmlLinks[$idx]->platform->name);
                    $linkWithPlatform = $targetName != '';
                }
                // echo "\$targetHasPlatforms:$targetHasPlatforms<br>";
                // echo "\$linkWithPlatform:$linkWithPlatform<br>";
                if ($targetHasPlatforms) {
                    // each link need to have platform or will not be imported
                    if ($linkWithPlatform && isset($platformSet[$targetName])) {
                        $platformID = $platformSet[$targetName]['id'];
                        $status_ok = true;
                        $dummy_msg = null;
                    } else {
                        $import_status = $labels['not_imported'];
                        if (!$platformElementExists) {
                            $dummy_msg = sprintf($labels['link_without_platform_element'], $idx + 1);
                        } else {
                            if (!$linkWithPlatform) {
                                $dummy_msg = sprintf($labels['link_without_required_platform'], $idx + 1);
                            } else {
                                $dummy_msg = sprintf($labels['platform_not_linked'], $idx + 1, $targetName, $contextObj->tplan_name);
                            }
                        }
                    }
                } else {
                    if ($linkWithPlatform) {
                        $import_status = $labels['not_imported'];
                        $dummy_msg = sprintf($labels['link_with_platform_not_needed'], $idx + 1);
                    } else {
                        $platformID = 0;
                        $status_ok = true;
                    }
                }
                if (!is_null($dummy_msg)) {
                    $msg[] = array($dummy_msg, $import_status);
                }
                // echo '$status_ok' . $status_ok . ' ' . __LINE__ . '<br>' ;
                if ($status_ok) {
                    $createLink = false;
                    $updateLink = false;
                    // Link passed ok check on platform
                    // Now we need to understand if requested Test case is present on Test Project
                    $externalID = (int) $xmlLinks[$idx]->testcase->externalid;
                    $tcaseName = (string) $xmlLinks[$idx]->testcase->name;
                    $execOrder = (int) $xmlLinks[$idx]->testcase->execution_order;
                    $version = (int) $xmlLinks[$idx]->testcase->version;
                    if (isset($tcaseSet[$externalID])) {
                        // now need to check if requested version exists
                        $dummy = $tcaseMgr->get_basic_info($tcaseSet[$externalID], array('number' => $version));
                        if (count($dummy) > 0) {
                            // Check :
                            // for same test plan there is a different version already linked ?
                            // if YES => error.
                            //
                            $lvFilters = array('tplan_id' => $contextObj->tplan_id);
                            $linkedVersions = $tcaseMgr->get_linked_versions($dummy[0]['id'], $lvFilters);
                            $updateLink = false;
                            $doUpdateFeedBack = true;
                            // TICKET 5189: Import a test plan does not import test cases execution order
                            // new dBug($linkedVersions);
                            if (!($createLink = is_null($linkedVersions))) {
                                // Now need to understand if is already linked with this signature.
                                if (!isset($linkedVersions[$dummy[0]['tcversion_id']])) {
                                    //echo 'CREATE';
                                    $createLink = true;
                                } else {
                                    // linked platforms
                                    $createLink = false;
                                    $updateLink = false;
                                    $plat_keys = array_keys($linkedVersions[$dummy[0]['tcversion_id']][$contextObj->tplan_id]);
                                    $plat_keys = array_flip($plat_keys);
                                    if (isset($plat_keys[$platformID])) {
                                        $updateLink = true;
                                    } else {
                                        if ($platformID == 0) {
                                            // User request to add without platform, but platforms exist => SKIP
                                            $msg[] = array('platform 0 missing messages', $labels['not_imported']);
                                        } else {
                                            $createLink = true;
                                        }
                                    }
                                }
                            }
                            if ($createLink) {
                                // Create link
                                // function link_tcversions($id,&$items_to_link,$userId)
                                $item2link['items'] = array($dummy[0]['id'] => array($platformID => $dummy[0]['tcversion_id']));
                                $item2link['tcversion'] = array($dummy[0]['id'] => $dummy[0]['tcversion_id']);
                                $tplanMgr->link_tcversions($contextObj->tplan_id, $item2link, $contextObj->userID);
                                $dummy_msg = sprintf($labels['link_to_tplan_feedback'], $externalID, $version);
                                if ($platformID > 0) {
                                    $dummy_msg .= sprintf($labels['link_to_platform'], $targetName);
                                }
                                $msg[] = array($dummy_msg, $labels['ok']);
                                // TICKET 5189: Import a test plan does not import test cases execution order
                                $updateLink = true;
                                $doUpdateFeedBack = false;
                            }
                            if ($updateLink) {
                                $newOrder = array($dummy[0]['tcversion_id'] => $execOrder);
                                $tplanMgr->setExecutionOrder($contextObj->tplan_id, $newOrder);
                                if ($doUpdateFeedBack) {
                                    $dummy_msg = sprintf($labels['tcase_link_updated'], $tcasePrefix . $externalID . ' ' . $tcaseName, $version);
                                    $msg[] = array($dummy_msg, $labels['ok']);
                                }
                            }
                        } else {
                            $msg[] = array(sprintf($labels['tcversion_doesnot_exist'], $externalID, $version, $tprojectInfo['name']));
                        }
                    } else {
                        $msg[] = array(sprintf($labels['tcase_doesnot_exist'], $externalID, $tprojectInfo['name']));
                    }
                    //$tcaseMgr->get_by_external
                    // echo '<pre><xmp>';
                    // var_dump($xmlLinks[$idx]->testcase);
                    // echo 'TCBAME' . (string)$xmlLinks[$idx]->testcase->name;
                    // echo '</xmp></pre>';
                }
            }
        }
    }
    return $msg;
}
/**
 * doExecuteImport
 *
 */
function doExecuteImport($fileName, &$argsObj, &$reqSpecMgr, &$reqMgr)
{
    $retval = new stdClass();
    $retval->items = array();
    $retval->msg = '';
    $retval->file_check = array('status_ok' => 1, 'msg' => 'ok');
    $retval->userFeedback = null;
    $context = new stdClass();
    $context->tproject_id = $argsObj->tproject_id;
    $context->req_spec_id = $argsObj->req_spec_id;
    $context->user_id = $argsObj->userID;
    $context->importType = $argsObj->importType;
    new dBug($context);
    // manage file upload process
    $source = isset($_FILES['uploadedFile']['tmp_name']) ? $_FILES['uploadedFile']['tmp_name'] : null;
    if ($source != 'none' && $source != '') {
        if (move_uploaded_file($source, $fileName)) {
            if (strcasecmp($argsObj->importType, 'XML') == 0) {
                $retval->file_check['status_ok'] = !(($xml = simplexml_load_file_wrapper($fileName)) === FALSE);
            }
        }
    } else {
        $retval->file_check = array('status_ok' => 0, 'msg' => lang_get('please_choose_req_file'));
    }
    // ----------------------------------------------------------------------------------------------
    if ($retval->file_check['status_ok']) {
        //var_dump($xml);
        //die();
        $retval->items = doReqImportFromMantisXML($reqMgr, $xml, $context);
        $retval->msg = lang_get('req_import_finished');
        unlink($fileName);
    }
    return $retval;
}
function check_xml_tc_tsuite($fileName, $recursiveMode)
{
    $xml = @simplexml_load_file_wrapper($fileName);
    $file_check = array('status_ok' => 0, 'msg' => 'xml_load_ko');
    if ($xml !== FALSE) {
        $file_check = array('status_ok' => 1, 'msg' => 'ok');
        $elementName = $xml->getName();
        if ($recursiveMode) {
            if ($elementName != 'testsuite') {
                $file_check = array('status_ok' => 0, 'msg' => lang_get('wrong_xml_tsuite_file'));
            }
        } else {
            if ($elementName != 'testcases' && $elementName != 'testcase') {
                $file_check = array('status_ok' => 0, 'msg' => lang_get('wrong_xml_tcase_file'));
            }
        }
    }
    return $file_check;
}