freemind.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. <?php
  2. /**
  3. * The freemind library of zentaopms, can be used to bakup and restore a database.
  4. *
  5. * @copyright Copyright 2009-2015 禅道软件(青岛)有限公司(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 Ke Zhao <zhaoke@cnezsoft.com>
  8. * @package FreeMind
  9. * @version $Id$
  10. * @link http://www.zentao.net
  11. */
  12. class freemind
  13. {
  14. /**
  15. * Create module node.
  16. *
  17. * @param DOMDocument $xmlDoc
  18. * @param array $context
  19. * @param DOMElement $productNode
  20. * @param array $moduleNodes
  21. * @access public
  22. * @return void
  23. */
  24. function createModuleNode($xmlDoc, $context, $productNode, &$moduleNodes)
  25. {
  26. $config = $context['config'];
  27. $moduleList = $context['moduleList'];
  28. $caseModules = array_column($context['caseList'], 'moduleID');
  29. $caseModules = array_filter($caseModules);
  30. $caseModules = array_combine($caseModules, $caseModules);
  31. foreach($moduleList as $key => $name)
  32. {
  33. if(!isset($caseModules[$key])) continue;
  34. $suffix = $config['module'].':'.$key;
  35. $moduleNode = $this->createNode($xmlDoc, $name, $suffix, array('nodeType' => 'module'));
  36. $productNode->appendChild($moduleNode);
  37. $moduleNodes[$key] = $moduleNode;
  38. }
  39. }
  40. /**
  41. * Create scene node.
  42. *
  43. * @param DOMDocument $xmlDoc
  44. * @param array $context
  45. * @param DOMElement $productNode
  46. * @param array $moduleNodes
  47. * @param array $sceneNodes
  48. * @access public
  49. * @return void
  50. */
  51. function createSceneNode($xmlDoc, $context, $productNode, &$moduleNodes, &$sceneNodes)
  52. {
  53. $config = $context['config'];
  54. $topScenes = $context['topScenes'];
  55. $caseScenes = array_column($context['caseList'], 'moduleID');
  56. $caseScenes = array_filter($caseScenes);
  57. $caseScenes = array_combine($caseScenes, $caseScenes);
  58. foreach($topScenes as $key => $scene)
  59. {
  60. if(!isset($caseScenes[$key])) continue;
  61. $suffix = $config['scene'] . ':' . $scene->sceneID;
  62. $sceneNode = $this->createNode($xmlDoc, $scene->sceneName, $suffix, array('nodeType' => 'scene'));
  63. $this->createNextChildScenesNode($scene, $sceneNode, $xmlDoc, $context, $moduleNodes, $sceneNodes);
  64. if(isset($moduleNodes[$scene->moduleID]))
  65. {
  66. $moduleNode = $moduleNodes[$scene->moduleID];
  67. $moduleNode->appendChild($sceneNode);
  68. }
  69. else
  70. {
  71. $productNode->appendChild($sceneNode);
  72. }
  73. $sceneNodes[$scene->sceneID] = $sceneNode;
  74. }
  75. }
  76. /**
  77. * Create next child scene node.
  78. *
  79. * @param object $parentScene
  80. * @param object $parentNode
  81. * @param DOMDocument $xmlDoc
  82. * @param array $context
  83. * @param array $moduleNodes
  84. * @param array $sceneNodes
  85. * @access public
  86. * @return void
  87. */
  88. function createNextChildScenesNode($parentScene,$parentNode, $xmlDoc, $context, &$moduleNodes, &$sceneNodes)
  89. {
  90. $sceneMaps = $context['sceneMaps'];
  91. $config = $context['config'];
  92. foreach($sceneMaps as $key => $scene)
  93. {
  94. if($scene->parentID != $parentScene->sceneID) continue;
  95. $suffix = $config['scene'].':'.$scene->sceneID;
  96. $sceneNode = $this->createNode($xmlDoc, $scene->sceneName, $suffix, array('nodeType'=>'scene'));
  97. $this->createNextChildScenesNode($scene, $sceneNode, $xmlDoc, $context, $moduleNodes, $sceneNodes);
  98. $parentNode->appendChild($sceneNode);
  99. $sceneNodes[$scene->sceneID] = $sceneNode;
  100. }
  101. }
  102. /**
  103. * Create test case node.
  104. *
  105. * @param DOMDocument $xmlDoc
  106. * @param array $context
  107. * @param object $productNode
  108. * @param array $moduleNodes
  109. * @param array $sceneNodes
  110. * @access public
  111. * @return void
  112. */
  113. function createTestcaseNode($xmlDoc, $context, $productNode, &$moduleNodes, &$sceneNodes)
  114. {
  115. $caseList = $context['caseList'];
  116. foreach($caseList as $case)
  117. {
  118. if(empty($case->testcaseID)) continue;
  119. if($case->sceneID && isset($sceneNodes[$case->sceneID]))
  120. {
  121. $this->createOneTestcaseNode($case, $xmlDoc, $context, $sceneNodes[$case->sceneID]);
  122. }
  123. else
  124. {
  125. if($case->moduleID && isset($moduleNodes[$case->moduleID]))
  126. {
  127. $this->createOneTestcaseNode($case, $xmlDoc, $context, $moduleNodes[$case->moduleID]);
  128. }
  129. else
  130. {
  131. $this->createOneTestcaseNode($case, $xmlDoc, $context, $productNode);
  132. }
  133. }
  134. }
  135. }
  136. /**
  137. * Create one test case node.
  138. *
  139. * @param object $case
  140. * @param DOMDocument $xmlDoc
  141. * @param array $context
  142. * @param object $parentNode
  143. * @access public
  144. * @return void
  145. */
  146. function createOneTestcaseNode($case, $xmlDoc, $context, $parentNode)
  147. {
  148. $caseList = $context['caseList'];
  149. $stepList = $context['stepList'];
  150. $config = $context['config'];
  151. $suffix = $config['case'].':'.$case->testcaseID.','.$config['pri'].':'.$case->pri;
  152. $caseNode = $this->createNode($xmlDoc, $case->name, $suffix, array('nodeType'=>'testcase'));
  153. $parentNode->appendChild($caseNode);
  154. $this->createPreconditionNode($xmlDoc, $config, $caseNode, $case->precondition);
  155. $topStepList = $this->findTopStepListByCase($case, $stepList);
  156. $this->createStepNode($xmlDoc, $config, $caseNode, $stepList, $topStepList);
  157. }
  158. /**
  159. * 生成用例前置条件节点。
  160. * Create precondition node.
  161. *
  162. * @param DOMDocument $xmlDoc
  163. * @param array $config
  164. * @param object $parentNode
  165. * @param string $precondition
  166. * @access private
  167. * @return void
  168. */
  169. private function createPreconditionNode($xmlDoc, $config, $parentNode, $precondition)
  170. {
  171. if(empty($precondition)) return false;
  172. $preconditionNode = $this->createNode($xmlDoc, $precondition, $config['precondition'], array('nodeType' => 'precondition'));
  173. $parentNode->appendChild($preconditionNode);
  174. }
  175. /**
  176. * 生成用例步骤节点。
  177. * Create step node.
  178. *
  179. * @param DOMDocument $xmlDoc
  180. * @param array $config
  181. * @param object $parentNode
  182. * @param array $allSteps
  183. * @param array $steps
  184. * @access private
  185. * @return void
  186. */
  187. private function createStepNode($xmlDoc, $config, $parentNode, $allSteps, $steps)
  188. {
  189. foreach($steps as $step)
  190. {
  191. $subSteps = $this->findSubStepListByStep($step, $allSteps);
  192. $suffix = count($subSteps) > 0 ? $config['group'] : '';
  193. $stepNode = $this->createNode($xmlDoc, $step->desc, $suffix, array('nodeType' => 'step'));
  194. $parentNode->appendChild($stepNode);
  195. if($subSteps)
  196. {
  197. $this->createStepNode($xmlDoc, $config, $stepNode, $allSteps, $subSteps);
  198. }
  199. else if(!empty($step->expect))
  200. {
  201. $expectNode = $this->createNode($xmlDoc, $step->expect, '', array('nodeType'=>'expect'));
  202. $stepNode->appendChild($expectNode);
  203. }
  204. }
  205. }
  206. /**
  207. * Find substep list by step.
  208. *
  209. * @param object $step
  210. * @param array $stepList
  211. * @access public
  212. * @return array
  213. */
  214. function findSubStepListByStep($step,$stepList)
  215. {
  216. $subList = array();
  217. foreach($stepList as $one)
  218. {
  219. if($one->parentID == $step->stepID)
  220. {
  221. $subList[] = $one;
  222. }
  223. }
  224. return $subList;
  225. }
  226. /**
  227. * Find top step list by case.
  228. *
  229. * @param object $case
  230. * @param array $stepList
  231. * @access public
  232. * @return array
  233. */
  234. public function findTopStepListByCase($case,$stepList)
  235. {
  236. $topList = array();
  237. foreach($stepList as $step)
  238. {
  239. if($step->parentID == '0' && $step->testcaseID == $case->testcaseID)
  240. {
  241. $topList[] = $step;
  242. }
  243. }
  244. return $topList;
  245. }
  246. /**
  247. * Create freemind node.
  248. *
  249. * @param DOMDocument $xmlDoc
  250. * @param string $text
  251. * @param string $suffix
  252. * @param array $attrs
  253. * @access public
  254. * @return object
  255. */
  256. public function createNode($xmlDoc, $text, $suffix = '', $attrs=array())
  257. {
  258. $node = $xmlDoc->createElement('node');
  259. $textAttr = $xmlDoc->createAttribute('TEXT');
  260. $textAttrValue = $xmlDoc->createTextNode($this->toText($text,$suffix));
  261. $textAttr->appendChild($textAttrValue);
  262. $node->appendChild($textAttr);
  263. $positionAttr = $xmlDoc->createAttribute("POSITION");
  264. $positionAttrValue = $xmlDoc->createTextNode('right');
  265. $positionAttr->appendChild($positionAttrValue);
  266. $node->appendChild($positionAttr);
  267. foreach($attrs as $key => $value)
  268. {
  269. $attr = $xmlDoc->createAttribute($key);
  270. $attrValue = $xmlDoc->createTextNode($value);
  271. $attr->appendChild($attrValue);
  272. $node->appendChild($attr);
  273. }
  274. return $node;
  275. }
  276. /**
  277. * Add suffix before string.
  278. *
  279. * @param string $str
  280. * @param string $suffix
  281. * @access public
  282. * @return string
  283. */
  284. public function toText($str, $suffix)
  285. {
  286. if(empty($suffix)) return $str;
  287. return $str . '['.$suffix.']';
  288. }
  289. /**
  290. * Get substring between mark1 and mark2 from kw.
  291. *
  292. * @param string $str
  293. * @param string $suffix
  294. * @access public
  295. * @return string
  296. */
  297. function getBetween($kw1, $mark1, $mark2)
  298. {
  299. $kw = $kw1;
  300. $kw = '123' . $kw . '123';
  301. $st = strripos($kw, $mark1);
  302. $ed = strripos($kw, $mark2);
  303. if(($st == false || $ed == false) || $st >= $ed) return 0;
  304. $kw = substr($kw, ($st + 1), ($ed - $st - 1));
  305. return $kw;
  306. }
  307. /**
  308. * Judgment ends with a string.
  309. *
  310. * @param string $haystack
  311. * @param string $needle
  312. * @access public
  313. * @return string
  314. */
  315. function endsWith($haystack, $needle)
  316. {
  317. return $needle === '' || substr_compare($haystack, $needle, -strlen($needle)) === 0;
  318. }
  319. }