PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/classes/db/QueryTester.class.php

http://xe-core.googlecode.com/
PHP | 320 lines | 255 code | 46 blank | 19 comment | 30 complexity | 8846df03743de4e0e09ab90ba1222644 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause
  1. <?php
  2. // Only supports queries inside modules for now
  3. class QueryTester {
  4. function QueryTester(){
  5. }
  6. function getQueryPath($type, $name, $query_name){
  7. return _XE_PATH_ . $type ."/".$name."/queries/" . $query_name . ".xml";
  8. }
  9. function getNewParserOutput($xml_file){
  10. $newXmlQueryParser = new XmlQueryParser();
  11. $xml_obj = $newXmlQueryParser->getXmlFileContent($xml_file);
  12. $parser = new QueryParser($xml_obj->query);
  13. return $parser->toString();
  14. }
  15. function getOldParserOutput($query_id, $xml_file){
  16. $cache_file = _TEST_PATH_ . "cache/".$query_id.'.cache.php';
  17. $parser = new OldXmlQueryParser();
  18. $parser->parse($query_id, $xml_file, $cache_file);
  19. $buff = FileHandler::readFile($cache_file);
  20. return $buff;
  21. }
  22. function cleanOutputAndAddArgs($outputString, $argsString = ''){
  23. $outputString = str_replace("<?php if(!defined('__ZBXE__')) exit();", "", $outputString);
  24. $outputString = str_replace("?>", "", $outputString);
  25. $outputString = $argsString . $outputString;
  26. return $outputString;
  27. }
  28. function getXmlFileContent($xml_file){
  29. return FileHandler::readFile($xml_file);
  30. }
  31. function printOutput($output){
  32. if(is_object($output)) {
  33. var_dump($output); return;
  34. }
  35. $output = htmlspecialchars($output);
  36. $output = preg_replace('/select/i', 'SELECT', $output);
  37. $output = preg_replace('/from/i', '<br/>FROM', $output);
  38. $output = preg_replace('/where/i', '<br/>WHERE', $output);
  39. $output = preg_replace('/group by/i', '<br/>GROUP BY', $output);
  40. $output = preg_replace('/order by/i', '<br/>ORDER BY', $output);
  41. $output = str_replace("\n", "<br/>", $output);
  42. echo '<pre class=prettyprint>'
  43. .$output
  44. .'</pre>';
  45. }
  46. function getNewParserOutputString($xml_file, $argsString){
  47. $outputString = '';
  48. $outputString = $this->getNewParserOutput($xml_file);
  49. $outputString = $this->cleanOutputAndAddArgs($outputString, $argsString);
  50. return $outputString;
  51. }
  52. function getNewParserQuery($outputString){
  53. //echo $outputString;
  54. //exit(0);
  55. $output = eval($outputString);
  56. if(is_a($output, 'Object'))
  57. if(!$output->toBool()) return("Date incorecte! Query-ul nu a putut fi executat.");
  58. $db = new DBCubrid();
  59. if($output->getAction() == 'select')
  60. return $db->getSelectSql($output);
  61. else if($output->getAction() == 'insert')
  62. return $db->getInsertSql($output);
  63. else if($output->getAction() == 'update')
  64. return $db->getUpdateSql($output);
  65. else if($output->getAction() == 'delete')
  66. return $db->getDeleteSql($output);
  67. }
  68. function testNewParser($xml_file, $escape_char, $argsString, $show_output_string){
  69. $outputString = $this->getNewParserOutputString($xml_file, $escape_char, $argsString);
  70. $query = $this->getNewParserQuery($outputString);
  71. echo '<tr>';
  72. if($show_output_string){
  73. echo '<td>';
  74. $this->printOutput($outputString);
  75. echo '</td>';
  76. }
  77. echo '<td>';
  78. $this->printOutput($query);
  79. echo '</td>';
  80. echo '</tr>';
  81. }
  82. function getOldParserOutputString($query_id, $xml_file, $argsString){
  83. $outputString = $this->getOldParserOutput($query_id, $xml_file);
  84. $outputString = $this->cleanOutputAndAddArgs($outputString, $argsString);
  85. return $outputString;
  86. }
  87. function getOldParserQuery($outputString){
  88. $output = eval($outputString);
  89. if(is_a($output, 'Object'))
  90. if(!$output->toBool()) exit("Date incorecte! Query-ul nu a putut fi executat.");
  91. /* SQL Server
  92. *
  93. $db = new DBMssql(false);
  94. if($output->action == "select")
  95. return $db->_executeSelectAct($output);
  96. else if($output->action == "insert")
  97. return $db->_executeInsertAct($output);
  98. else if($output->action == "delete")
  99. return $db->_executeDeleteAct($output);
  100. else if($output->action == "update")
  101. return $db->_executeUpdateAct($output);
  102. */
  103. /*
  104. * Mysql
  105. */
  106. $db = new DBMysql(false);
  107. if($output->action == "select")
  108. $db->_executeSelectAct($output);
  109. else if($output->action == "insert")
  110. $db->_executeInsertAct($output);
  111. else if($output->action == "delete")
  112. $db->_executeDeleteAct($output);
  113. else if($output->action == "update")
  114. $db->_executeUpdateAct($output);
  115. return $db->getLatestQuery();
  116. }
  117. function testOldParser($query_id, $xml_file, $argsString, $show_output_string){
  118. $outputString = $this->getOldParserOutputString($query_id, $xml_file, $argsString);
  119. $query = $this->getOldParserQuery($outputString);
  120. echo '<tr>';
  121. if($show_output_string){
  122. echo '<td>';
  123. $this->printOutput($outputString);
  124. echo '</td>';
  125. }
  126. echo '<td>';
  127. $this->printOutput($query);
  128. echo '</td>';
  129. echo '</tr>';
  130. }
  131. function showXmlInputFile($xml_file){
  132. echo '<tr colspan=2>';
  133. echo '<td>';
  134. $xml_file_content = $this->getXmlFileContent($xml_file);
  135. $this->printOutput($xml_file_content);
  136. echo '</td></tr>';
  137. }
  138. function test($query_id, $xml_file, $argsString, $show_output_string, $escape_char = '"'){
  139. echo "<h3>$query_id</h3>";
  140. echo '<table border=1 cellpadding=5 cellspacing=0 width=50%>';
  141. $this->showXmlInputFile($xml_file);
  142. $this->testNewParser($xml_file, $escape_char, $argsString, $show_output_string);
  143. //$this->testOldParser($query_id, $xml_file, $argsString, $show_output_string);
  144. echo '</table>';
  145. }
  146. function test_addon_getAddonInfo($show_output_string = false){
  147. $argsString = '$args->addon = "captcha";';
  148. $this->test("modules.addon.getAddonInfo"
  149. , $this->getQueryPath("modules", "addon", "getAddonInfo")
  150. , $argsString
  151. , $show_output_string);
  152. }
  153. function test_addon_getAddons($show_output_string = false){
  154. $argsString = '';
  155. $this->test("modules.addon.getAddons"
  156. , $this->getQueryPath("modules", "addon", "getAddons")
  157. , $argsString
  158. , $show_output_string);
  159. }
  160. function test_admin_getCommentCount($show_output_string = false){
  161. $argsString = '';
  162. $this->test("modules.admin.getCommentCount"
  163. , $this->getQueryPath("modules", "admin", "getCommentCount")
  164. , $argsString
  165. , $show_output_string);
  166. }
  167. function test_admin_getCommentDeclaredStatus($show_output_string = false){
  168. $argsString = '$args->date = "20110411";';
  169. $this->test("modules.admin.getCommentDeclaredStatus"
  170. , $this->getQueryPath("modules", "admin", "getCommentDeclaredStatus")
  171. , $argsString
  172. , $show_output_string);
  173. }
  174. function test_module_getDefaultModules($show_output_string = false){
  175. $argsString = '';
  176. $this->test("modules.module.getDefaultModules"
  177. , $this->getQueryPath("modules", "module", "getDefaultModules")
  178. , $argsString
  179. , $show_output_string);
  180. }
  181. function test_module_getModuleCategories($show_output_string = false){
  182. $argsString = '';
  183. $this->test("modules.module.getModuleCategories"
  184. , $this->getQueryPath("modules", "module", "getModuleCategories")
  185. , $argsString
  186. , $show_output_string);
  187. }
  188. function test_module_getNonuniqueDomains($show_output_string = false){
  189. $argsString = '';
  190. $this->test("modules.module.getNonuniqueDomains"
  191. , $this->getQueryPath("modules", "module", "getNonuniqueDomains")
  192. , $argsString
  193. , $show_output_string);
  194. }
  195. function test_module_getAdminId($show_output_string = false){
  196. $argsString = '$args->module_srl = 23;';
  197. $this->test("modules.module.getAdminId"
  198. , $this->getQueryPath("modules", "module", "getAdminId")
  199. , $argsString
  200. , $show_output_string);
  201. }
  202. function test_module_getSiteInfo($show_output_string = false){
  203. $argsString = '$args->site_srl = 0;';
  204. $this->test("modules.module.getSiteInfo"
  205. , $this->getQueryPath("modules", "module", "getSiteInfo")
  206. , $argsString
  207. , $show_output_string);
  208. }
  209. function test_module_insertModule($show_output_string = false){
  210. $argsString = ' $args->module_category_srl = 0;
  211. $args->browser_title = "test";
  212. $args->layout_srl = 0;
  213. $args->mlayout_srl = 0;
  214. $args->module = "page";
  215. $args->mid = "test";
  216. $args->site_srl = 0;
  217. $args->module_srl = 47374;';
  218. $this->test("modules.module.insertModule"
  219. , $this->getQueryPath("modules", "module", "insertModule")
  220. , $argsString
  221. , $show_output_string);
  222. }
  223. function test_module_updateModule($show_output_string = false){
  224. $argsString = ' $args->module_category_srl = 0;
  225. $args->browser_title = "test";
  226. $args->layout_srl = 0;
  227. $args->mlayout_srl = 0;
  228. $args->module = "page";
  229. $args->mid = "test";
  230. $args->use_mobile = "";
  231. $args->site_srl = 0;
  232. $args->module_srl = 47374;';
  233. $this->test("modules.module.updateModule"
  234. , $this->getQueryPath("modules", "module", "updateModule")
  235. , $argsString
  236. , $show_output_string);
  237. }
  238. function test_admin_deleteActionForward($show_output_string = false){
  239. $argsString = '$args->module = "page";
  240. $args->type = "page";
  241. $args->act = "tata";';
  242. $this->test("modules.admin.deleteActionForward"
  243. , $this->getQueryPath("modules", "module", "deleteActionForward")
  244. , $argsString
  245. , $show_output_string);
  246. }
  247. function test_member_getAutologin($show_output_string = false){
  248. $argsString = '$args->autologin_key = 10;';
  249. $this->test("modules.member.getAutologin"
  250. , $this->getQueryPath("modules", "member", "getAutologin")
  251. , $argsString
  252. , $show_output_string);
  253. }
  254. function test_opage_getOpageList($show_output_string = false){
  255. $argsString = '$args->s_title = "yuhuu";
  256. $args->module = 12;';
  257. $this->test("modules.opage.getOpageList"
  258. , $this->getQueryPath("modules", "opage", "getOpageList")
  259. , $argsString
  260. , $show_output_string);
  261. }
  262. function test_getPageList($show_output_string = false){
  263. $argsString = '$args->sort_index = "module_srl";
  264. $args->page_count = 10;
  265. $args->s_module_category_srl = 0;
  266. $args->s_mid = "test";
  267. $args->s_browser_title = "caca";';
  268. $this->test("modules.page.getPageList"
  269. , $this->getQueryPath("modules", "page", "getPageList")
  270. , $argsString
  271. , $show_output_string);
  272. }
  273. }
  274. ?>