testcase.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * The testcase entry point of ZenTaoPMS.
  4. *
  5. * @copyright Copyright 2009-2023 禅道软件(青岛)有限公司(ZenTao Software (Qingdao) Co., Ltd. www.cnezsoft.com)
  6. * @license ZPL(http://zpl.pub/page/zplv12.html) or AGPL(https://www.gnu.org/licenses/agpl-3.0.en.html)
  7. * @author Chunsheng Wang <chunsheng@cnezsoft.com>
  8. * @package entries
  9. * @version 1
  10. * @link https://www.zentao.net
  11. */
  12. class testcaseEntry extends entry
  13. {
  14. /**
  15. * GET method.
  16. *
  17. * @param int $testcaseID
  18. * @access public
  19. * @return string
  20. */
  21. public function get($testcaseID)
  22. {
  23. $control = $this->loadController('testcase', 'view');
  24. $control->view($testcaseID, $this->param('version', 0));
  25. $data = $this->getData();
  26. if(!$data or (isset($data->message) and $data->message == '404 Not found')) return $this->send404();
  27. if(isset($data->status) and $data->status == 'fail') return $this->sendError(zget($data, 'code', 400), $data->message);
  28. if(!isset($data->case)) $this->sendError(400, 'error');
  29. $case = $data->data->case;
  30. $case->steps = (isset($case->steps) and !empty($case->steps)) ? array_values(get_object_vars((object)$case->steps)) : array();
  31. if(!empty($case->steps))
  32. {
  33. foreach($case->steps as &$step)
  34. {
  35. foreach(array('step', 'desc', 'expect') as $field)
  36. {
  37. if(isset($step->$field) && $step->$field != '')
  38. {
  39. $step->$field = str_replace(array("&amp;quot;", "&amp;#039;", "&amp;lt;", "&amp;gt;"), array('"', "'", "<", ">"), $step->$field);
  40. }
  41. }
  42. }
  43. }
  44. return $this->send(200, $this->format($case, 'openedBy:user,openedDate:time,lastEditedBy:user,lastEditedDate:time,lastRunDate:time,scriptedDate:date,reviewedBy:user,reviewedDate:date,steps:array,deleted:bool'));
  45. }
  46. /**
  47. * PUT method.
  48. *
  49. * @param int $caseID
  50. * @access public
  51. * @return string
  52. */
  53. public function put($caseID)
  54. {
  55. $control = $this->loadController('testcase', 'edit');
  56. $oldCase = $this->loadModel('testcase')->getByID($caseID);
  57. /* Set $_POST variables. */
  58. $fields = 'title,pri,story,type,stage,product,module,branch,precondition,script';
  59. $this->batchSetPost($fields, $oldCase);
  60. $this->setPost('uid', $this->request('uid', ''));
  61. if(isset($this->requestBody->script)) $this->setPost('auto', 'auto');
  62. /* Set steps and expects. */
  63. $steps = array();
  64. $expects = array();
  65. $stepType = array();
  66. if(isset($this->requestBody->steps) && !isset($this->requestBody->stepType))
  67. {
  68. foreach($this->requestBody->steps as $key => $step)
  69. {
  70. if(empty($step->type)) $step->type = 'step';
  71. if(!in_array($step->type, array('step', 'item', 'group'))) $step->type = 'step';
  72. if($step->type == 'group' && (empty($this->requestBody->steps[$key + 1]->type) || $this->requestBody->steps[$key + 1]->type != 'step')) $step->type = 'step';
  73. $stepID = zget($step, 'id', $key);
  74. $steps[$stepID] = $step->desc;
  75. $expects[$stepID] = $step->expect;
  76. $stepType[$stepID] = $step->type;
  77. }
  78. }
  79. $this->setPost('steps', isset($this->requestBody->stepType) ? $this->requestBody->steps : $steps);
  80. $this->setPost('expects', isset($this->requestBody->expects) ? $this->requestBody->expects : $expects);
  81. $this->setPost('stepType', isset($this->requestBody->stepType) ? $this->requestBody->stepType : $stepType);
  82. $control->edit($caseID);
  83. $this->getData();
  84. $case = $this->testcase->getByID($caseID);
  85. return $this->send(200, $this->format($case, 'openedBy:user,openedDate:time,lastEditedBy:user,lastEditedDate:time,lastRunDate:time,scriptedDate:date,reviewedBy:user,reviewedDate:date,steps:array,deleted:bool'));
  86. }
  87. /**
  88. * DELETE method.
  89. *
  90. * @param int $testcaseID
  91. * @access public
  92. * @return string
  93. */
  94. public function delete($testcaseID)
  95. {
  96. $control = $this->loadController('testcase', 'delete');
  97. $control->delete($testcaseID, 'yes');
  98. $this->getData();
  99. return $this->sendSuccess(200, 'success');
  100. }
  101. }