testcases.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * The testcases 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 testcasesEntry extends entry
  13. {
  14. /**
  15. * GET method.
  16. *
  17. * @param int $productID
  18. * @access public
  19. * @return string
  20. */
  21. public function get($productID = 0)
  22. {
  23. if(empty($productID)) $productID = $this->param('product', 0);
  24. if(empty($productID)) return $this->sendError(400, 'Need product id.');
  25. $type = $this->param('status', 'all');
  26. $branch = $this->param('branch', '');
  27. $param = 0;
  28. $moduleID = $this->param('module', 0);
  29. if($moduleID)
  30. {
  31. $type = 'byModule';
  32. $param = $moduleID;
  33. }
  34. $this->app->cookie->caseModule = 0;
  35. $this->app->cookie->caseSuite = 0;
  36. $this->app->cookie->preBranch = $branch;
  37. $this->app->cookie->onlyAutoCase = 0;
  38. $control = $this->loadController('testcase', 'browse');
  39. $control->browse($productID, $this->param('branch', ''), $type, $param, $this->param('caseType', ''), $this->param('order', 'id_desc'), 0, $this->param('limit', 20), $this->param('page', 1));
  40. $data = $this->getData();
  41. if(isset($data->status) and $data->status == 'success')
  42. {
  43. $cases = $data->data->cases;
  44. $pager = $data->data->pager;
  45. $result = array();
  46. foreach($cases as $case)
  47. {
  48. $case->statusName = $this->lang->testcase->statusList[$case->status];
  49. $result[] = $this->format($case, 'openedBy:user,openedDate:time,lastEditedBy:user,lastEditedDate:time,lastRunDate:time,scriptedDate:date,reviewedBy:user,reviewedDate:date,deleted:bool');
  50. }
  51. return $this->send(200, array('page' => $pager->pageID, 'total' => $pager->recTotal, 'limit' => $pager->recPerPage, 'testcases' => $result));
  52. }
  53. if(isset($data->status) and $data->status == 'fail') return $this->sendError(zget($data, 'code', 400), $data->message);
  54. return $this->sendError(400, 'error');
  55. }
  56. /**
  57. * POST method.
  58. *
  59. * @param int $productID
  60. * @access public
  61. * @return string
  62. */
  63. public function post($productID = 0)
  64. {
  65. if(!$productID) $productID = $this->param('product');
  66. if(!$productID and isset($this->requestBody->product)) $productID = $this->requestBody->product;
  67. if(!$productID) return $this->sendError(400, 'Need product id.');
  68. $control = $this->loadController('testcase', 'create');
  69. $fields = 'module,type,stage,story,title,precondition,pri';
  70. $this->batchSetPost($fields);
  71. $this->setPost('product', $productID);
  72. /* Set steps and expects. */
  73. if(isset($this->requestBody->steps))
  74. {
  75. $steps = array();
  76. $expects = array();
  77. $stepType = array();
  78. $index = 1;
  79. $useName = is_array($this->requestBody->steps) && count(array_column($this->requestBody->steps, 'name')) == count($this->requestBody->steps);
  80. if(!isset($this->requestBody->stepType))
  81. {
  82. foreach($this->requestBody->steps as $step)
  83. {
  84. $name = $useName ? $step->name : $index;
  85. $type = isset($step->type) ? $step->type : 'step';
  86. $index ++;
  87. $steps[$name] = $step->desc;
  88. $expects[$name] = $type == 'group' ? '' : $step->expect;
  89. $stepType[$name] = $type;
  90. }
  91. }
  92. $this->setPost('steps', isset($this->requestBody->stepType) ? $this->requestBody->steps : $steps);
  93. $this->setPost('expects', isset($this->requestBody->expects) ? $this->requestBody->expects : $expects);
  94. $this->setPost('stepType', isset($this->requestBody->stepType) ? $this->requestBody->stepType : $stepType);
  95. }
  96. $this->requireFields('title,type,pri,steps');
  97. $control->create(0);
  98. $data = $this->getData();
  99. if(isset($data->result) and $data->result == 'fail') return $this->sendError(400, $data->message);
  100. if(isset($data->result) and !isset($data->id)) return $this->sendError(400, $data->message);
  101. $case = $this->loadModel('testcase')->getByID($data->id);
  102. $case->steps = (isset($case->steps) and !empty($case->steps)) ? array_values($case->steps) : array();
  103. return $this->send(200, $this->format($case, 'openedBy:user,openedDate:time,lastEditedBy:user,lastEditedDate:time,lastRunDate:time,scriptedDate:date,reviewedBy:user,reviewedDate:date,deleted:bool'));
  104. }
  105. }