survey.lib.test.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. <?php
  2. Mock::generate('Database');
  3. Mock::generate('Display');
  4. $config['survey']['debug'] = false;
  5. require_once(api_get_path(LIBRARY_PATH).'add_course.lib.inc.php');
  6. class TestSurvey extends UnitTestCase {
  7. public $smanager;
  8. public $squestion;
  9. public $syesno;
  10. public $multiplechoice;
  11. public $personality;
  12. public $multipleresponse;
  13. public function TestSurvey() {
  14. $this->UnitTestCase('');
  15. }
  16. public function setUp() {
  17. $this->smanager = new survey_manager();
  18. $this->squestion = new question();
  19. $this->syesno = new yesno();
  20. $this->smultiplechoice = new multiplechoice();
  21. $this->spersonality = new personality();
  22. $this->smultipleresponse = new multipleresponse();
  23. }
  24. public function tearDown() {
  25. $this-> smanager = null;
  26. $this-> squestion = null;
  27. $this-> syesno = null;
  28. $this->smultiplechoice = null;
  29. $this->personality = null;
  30. $this->multipleresponse = null;
  31. }
  32. /*
  33. public function testGetSurvey() {
  34. $instans = new MockDatabase();
  35. global $_course;
  36. $survey_id=1;
  37. $shared=1;
  38. $my_course_id=$_GET['course'];
  39. $res = $this->smanager->get_survey($survey_id,$shared);
  40. $my_course_info=api_get_course_info($my_course_id);
  41. $table_survey = Database :: get_course_table(TABLE_SURVEY, $my_course_info['dbName']);
  42. $sql = "SELECT * FROM $table_survey WHERE survey_id='".Database::escape_string($survey_id)."'";
  43. $result = Database::query($sql, __FILE__, __LINE__);
  44. $instans->expectCallCount($table_survey);
  45. $this->assertTrue(is_array($res));
  46. $this->assertFalse($result);
  47. //var_dump($table_survey);
  48. //var_dump($result);
  49. //var_dump($res);
  50. }
  51. public function testStoreSurvey(){
  52. $instans = new MockDatabase();
  53. global $_user;
  54. $values=array('002','003','003');
  55. $table_survey=Database::get_course_table(TABLE_SURVEY);
  56. $shared_survey_id=0;
  57. $instans->expectCallCount($table_survey);
  58. //if(!$values['survey_id'] OR !is_numeric($values['survey_id']))
  59. $res = $this->smanager->store_survey($values);
  60. $this->assertTrue($res);
  61. $this->assertTrue(is_array($res));
  62. $this->assertTrue($res);
  63. $this->assertTrue($table_survey);
  64. //var_dump($res);
  65. //var_dump($table_survey);
  66. }
  67. /*
  68. public function testStoreSharedSurvey($values){
  69. $instans = new MockDatabase();
  70. $values=array('');
  71. global $_user;
  72. global $_course;
  73. $table_survey=Database::get_main_table(TABLE_MAIN_SHARED_SURVEY);
  74. $instans->expectCallCount(Database::get_main_table(TABLE_MAIN_SHARED_SURVEY));
  75. if(!$values['survey_id'] OR !is_numeric($values['survey_id']) OR $values['survey_share']['survey_share'] == 'true') {
  76. $sql = "INSERT INTO $table_survey (code, title, subtitle, author, lang, template, intro, surveythanks, creation_date, course_code)";
  77. $result = Database::query($sql, __FILE__, __LINE__);
  78. $return = Database::insert_id();
  79. }else{
  80. $sql = "UPDATE $table_survey SET";
  81. $result = Database::query($sql, __FILE__, __LINE__);
  82. $return = $values['survey_share']['survey_share'];
  83. }
  84. $res = $this->smanager->store_shared_survey($values);
  85. $this->assertTrue($res);
  86. //var_dump($res);
  87. //var_dump($table_survey);
  88. //var_dump($table_survey);
  89. }*/
  90. public function testDeleteSurvey(){
  91. $instans = new MockDatabase();
  92. $survey_id=1;
  93. $shared=false;
  94. $course_code=001;
  95. $table_survey= Database :: get_course_table(TABLE_SURVEY,$course_code);
  96. $table_survey_question_group = Database :: get_course_table(TABLE_SURVEY_QUESTION_GROUP,$course_code);
  97. $instans->expectOnce($table_survey);
  98. $res = $this->smanager->delete_survey($survey_id, $shared, $course_code);
  99. $this->assertTrue($res);
  100. $this->assertTrue(is_string($res));
  101. $this->assertTrue($table_survey);
  102. //var_dump($table_survey_question_group);
  103. //var_dump($res);
  104. }
  105. public function testCopySurvey(){
  106. $instans = new MockDatabase();
  107. $parent_survey=null;
  108. $new_survey_id=null;
  109. $instans->expectCallCount(Database::get_course_table(TABLE_SURVEY));
  110. $sql = "SELECT * from $table_survey_question_group " .
  111. "WHERE survey_id='".$parent_survey."'";
  112. $result = Database::query($sql, __FILE__, __LINE__);
  113. $res = $this->smanager->copy_survey($parent_survey,$new_survey_id);
  114. $this->assertTrue(is_bool($res));
  115. $this->assertTrue($res);
  116. $this->assertTrue($instans);
  117. //var_dump($res);
  118. //var_dump($result);
  119. }
  120. public function testEmpty_survey(){
  121. $instans = new MockDatabase();
  122. $table_survey_invitation = Database :: get_course_table(TABLE_SURVEY_INVITATION);
  123. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER);
  124. $table_survey = Database :: get_course_table(TABLE_SURVEY);
  125. $survey_id=null;
  126. $instans->expectOnce($table_survey);
  127. $instans->expectCallCount(count($table_survey));
  128. $instans->expectCallCount($table_survey);
  129. $res = $this->smanager->empty_survey($survey_id);
  130. $this->assertTrue($res);
  131. $this->assertTrue(is_object($instans));
  132. //var_dump($res);
  133. //var_dump($table_survey);
  134. }
  135. public function testUpdateSurveyAnswered(){
  136. $instans = new MockDatabase();
  137. global $_course;
  138. $survey_id=1;
  139. $user=001;
  140. $survey_code=001;
  141. $table_survey= Database :: get_course_table(TABLE_SURVEY, $_course['db_name']);
  142. $table_survey_invitation = Database :: get_course_table(TABLE_SURVEY_INVITATION, $_course['db_name']);
  143. $instans->expectCallCount($table_survey);
  144. $instans->expectCallCount(count($table_survey_invitation));
  145. $sql = "UPDATE $table_survey_invitation SET answered='1' WHERE session_id='".api_get_session_id()."' AND user='".Database::escape_string($user)."' AND survey_code='".Database::escape_string($survey_code)."'";
  146. $res = Database::query($sql, __FILE__, __LINE__);
  147. $result = $this->smanager->update_survey_answered($survey_id, $user, $survey_code);
  148. $this->assertTrue(is_bool($res));
  149. $this->assertTrue($res === false);
  150. //var_dump($result);
  151. //var_dump($res);
  152. }
  153. public function testGetCompleteSurveyStructure(){
  154. $survey_id='';
  155. $shared=0;
  156. $res = $this->smanager->get_complete_survey_structure($survey_id, $shared);
  157. $this->assertNull($res);
  158. $this->assertTrue($res=== null);
  159. //var_dump($res);
  160. }
  161. public function testIconQuestion(){
  162. $type='open';
  163. $res = $this->smanager->icon_question($type);
  164. if(is_bool($res)) {
  165. $this->assertTrue($res ===false);
  166. $this->assertTrue(is_bool($res));
  167. }else{
  168. $this->assertTrue($res);
  169. $this->assertTrue(is_string($res));
  170. }
  171. //var_dump($res);
  172. }
  173. public function testGetQuestion(){
  174. $instans = new MockDatabase();
  175. $question_id=01;
  176. $shared=false;
  177. $tbl_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  178. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  179. $sql = "SELECT * FROM $tbl_survey_question WHERE question_id='".Database::escape_string($question_id)."' ORDER BY `sort`";
  180. $result = Database::query($sql, __FILE__, __LINE__);
  181. $row = Database::fetch_array($result,'ASSOC');
  182. $res = $this->smanager->get_question($question_id,$shared);
  183. $this->assertTrue($res);
  184. $instans->expectOnce(count($res));
  185. //var_dump($res);
  186. //var_dump($result);
  187. }
  188. public function testGetQuestions(){
  189. $get_questions = new MockDatabase();
  190. $survey_id =1;
  191. $tbl_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  192. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  193. $sql = "SELECT * FROM $tbl_survey_question WHERE survey_id='".Database::escape_string($survey_id)."'";
  194. $result = Database::query($sql, __FILE__, __LINE__);
  195. $row = Database::fetch_array($result,'ASSOC');
  196. $res= $this->smanager->get_questions($survey_id);
  197. $get_questions->expectCallCount($result);
  198. $this->assertNull($res);
  199. //var_dump($res);
  200. //var_dump($row);
  201. //var_dump($get_questions);
  202. }
  203. public function testSaveQuestion(){
  204. global $survey_data;
  205. global $_course;
  206. $form_content=array('question'=>'121212');
  207. $res = $this->smanager->save_question($form_content['question']);
  208. $this->assertTrue($res);
  209. $this->assertTrue(is_string($res));
  210. //var_dump($res);
  211. }
  212. public function testSaveSharedQuestion(){
  213. $instans = new MockDatabase();
  214. global $_course;
  215. $form_content=array('');
  216. $survey_data=array('');
  217. $tbl_survey_question= Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION);
  218. $res = $this->smanager->save_shared_question($form_content,$survey_data);
  219. $instans->expectOnce($tbl_survey_question);
  220. $this->assertTrue($res);
  221. $this->assertTrue($instans);
  222. //var_dump($res);
  223. //var_dump($instans);
  224. }
  225. public function testMoveSurveyQuestion(){
  226. $instans = new MockDatabase();
  227. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  228. $direction='moveup';
  229. $survey_question_id=01;
  230. $survey_id=1;
  231. /*$sql = "SELECT * FROM $table_survey_question WHERE survey_id='".Database::escape_string($survey_id)."' ORDER BY sort $sort";
  232. $result = Database::query($sql, __FILE__, __LINE__);
  233. */
  234. $res = $this->smanager->move_survey_question($direction,$survey_question_id,$survey_id);
  235. $this->assertTrue(is_null($res));
  236. //var_dump($res);
  237. }
  238. public function testDeleteAllSurveyQuestions(){
  239. $instans = new MockDatabase();
  240. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  241. $survey_id=1;
  242. $shared=false;
  243. $sql = "DELETE from $table_survey_question WHERE survey_id='".Database::escape_string($survey_id)."'";
  244. $res = Database::query($sql, __FILE__, __LINE__);
  245. $result = $this->smanager->delete_all_survey_questions($survey_id,$shared);
  246. $instans->expectOnce($res);
  247. $this->assertTrue(is_null($result));
  248. $this->assertTrue(is_bool($res));
  249. $this->assertTrue($res === true || $res === false);
  250. //var_dump($result);
  251. //var_dump($res);
  252. }
  253. public function testDeleteSurveyQuestion(){
  254. $instans = new MockDatabase();
  255. $table_survey_question = Database :: get_course_table(TABLE_SURVEY_QUESTION);
  256. $survey_id =1;
  257. $question_id=01;
  258. $shared=false;
  259. $sql = "DELETE from $table_survey_question WHERE survey_id='".Database::escape_string($survey_id)."' AND question_id='".Database::escape_string($question_id)."'";
  260. $res = Database::query($sql, __FILE__, __LINE__);
  261. $instans->expectOnce($res);
  262. $result = $this->smanager->delete_survey_question($survey_id,$question_id);
  263. $this->assertTrue(is_null($result));
  264. $this->assertTrue(is_object($instans));
  265. $this->assertFalse($res);
  266. //var_dump($result);
  267. //var_dump($res);
  268. }
  269. /*
  270. public function testDeleteSharedSurveyQuestion(){
  271. $instans = new MockDatabase();
  272. $survey_id=1;
  273. $question_id=01;
  274. $table_survey_question = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION);
  275. $table_survey_question_option = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION_OPTION);
  276. $res = $this->smanager->delete_shared_survey_question($survey_id,$question_id);
  277. $instans->expectOnce($table_survey_question);
  278. $this->assertTrue(is_null($res));
  279. $this->assertTrue(is_object($instans));
  280. //var_dump($res);
  281. }
  282. public function testSaveQuestionOptions(){
  283. $instans = new MockDatabase();
  284. $form_content=array('percentage');
  285. $survey_data=array('survey_share');
  286. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  287. $instans->expectOnce($table_survey_question_option);
  288. $res = $this->smanager->save_question_options($form_content,$survey_data);
  289. $this->assertTrue(is_null($res));
  290. $this->assertTrue(is_object($instans));
  291. //var_dump($res);
  292. //var_dump($table_survey_question_option);
  293. }
  294. public function testSaveSharedQuestionOptions(){
  295. $instans = new MockDatabase();
  296. $form_content=array('answers');
  297. $survey_data=array('');
  298. $table_survey_question_option = Database :: get_main_table(TABLE_MAIN_SHARED_SURVEY_QUESTION_OPTION);
  299. $sql = "DELETE FROM $table_survey_question_option WHERE question_id = '".Database::escape_string($form_content['shared_question_id'])."'";
  300. $result = Database::query($sql, __FILE__, __LINE__);
  301. $instans->expectCallCount($result);
  302. $res = $this->smanager->save_shared_question_options($form_content,$survey_data);
  303. $this->assertTrue(is_bool($result));
  304. $this->assertTrue($result === true || $result === false);
  305. $this->assertTrue(is_null($res));
  306. $this->assertNull($res);
  307. //var_dump($res);
  308. //var_dump($result);
  309. }
  310. public function testDeleteAllSurveyQuestionsOptions(){
  311. $instans = new MockDatabase();
  312. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  313. $survey_id=1;
  314. $shared=false;
  315. $sql = "DELETE from $table_survey_question_option WHERE survey_id='".Database::escape_string($survey_id)."'";
  316. $res = Database::query($sql, __FILE__, __LINE__);
  317. $result = $this->smanager->delete_all_survey_questions_options($survey_id,$shared);
  318. $instans->expectCallCount($res);
  319. $this->assertTrue($result);
  320. $this->assertFalse($res);
  321. $this->assertTrue(is_bool($res));
  322. //var_dump($result);
  323. //var_dump($res);
  324. }
  325. public function testDeleteSurveyQuestionOption(){
  326. $instans = new MockDatabase();
  327. $survey_id=1;
  328. $question_id=01;
  329. $shared=false;
  330. $table_survey_question_option = Database :: get_course_table(TABLE_SURVEY_QUESTION_OPTION);
  331. $sql = "DELETE from $table_survey_question_option WHERE survey_id='".Database::escape_string($survey_id)."' AND question_id='".Database::escape_string($question_id)."'";
  332. $res = Database::query($sql, __FILE__, __LINE__);
  333. $instans->expectOnce($res);
  334. $instans->expectCallCount($res);
  335. $result = $this->smanager->delete_survey_question_option($survey_id,$question_id,$shared);
  336. if(is_bool($result))
  337. $this->assertTrue(is_bool($result));
  338. $this->assertTrue($result === true || $result===false);
  339. $this->assertTrue($result);
  340. if(is_bool($res))
  341. $this->assertTrue(is_bool($res));
  342. $this->assertTrue($res === true || $res===false);
  343. $this->assertFalse($res);
  344. //var_dump($result);
  345. //var_dump($res);
  346. }
  347. public function testDeleteAllSurveyAnswers(){
  348. $instans = new MockDatabase();
  349. $survey_id=1;
  350. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER);
  351. $instans->expectCallCount($table_survey_answer);
  352. $res = $this->smanager->delete_all_survey_answers($survey_id);
  353. $this->assertTrue(is_bool($res));
  354. $this->assertTrue($res);
  355. $this->assertTrue($res === true || $res === false);
  356. $this->assertTrue($table_survey_answer);
  357. //var_dump($res);
  358. //var_dump($table_survey_answer);
  359. }
  360. public function testGetPeopleWhoFilledSurvey(){
  361. $instans = new MockDatabase();
  362. $survey_id=1;
  363. $all_user_info=false;
  364. global $_course;
  365. $table_survey_answer = Database :: get_course_table(TABLE_SURVEY_ANSWER, $_course['db_name']);
  366. $table_user = Database :: get_main_table('user');
  367. $survey_data = survey_manager::get_survey($survey_id);
  368. $sql = "SELECT DISTINCT user FROM $table_survey_answer WHERE survey_id= '".Database::escape_string($survey_data['survey_id'])."'";
  369. $res = Database::query($sql, __FILE__, __LINE__);
  370. $instans->expectCallCount($table_user);
  371. $result = $this->smanager->get_people_who_filled_survey($survey_id,$all_user_info);
  372. $this->assertTrue(is_array($result));
  373. $this->assertNotNull($res);
  374. $this->assertFalse($res);
  375. $this->assertTrue(is_bool($res));
  376. $this->assertTrue($res=== true || $res === false);
  377. $this->assertTrue($sql);
  378. //var_dump($res);
  379. //var_dump($result);
  380. //var_dump($sql);
  381. }
  382. */
  383. public function testCreateForm(){
  384. $instans = new MockDisplay();
  385. global $charset;
  386. global $survey_data;
  387. $tool_name = 'AddQuestion';
  388. $tool_name = Display::return_icon(survey_manager::icon_question(Security::remove_XSS($_GET['type'])),get_lang(ucfirst(Security::remove_XSS($_GET['type']))),array('align'=>'middle', 'height'=>'22px')).' ';
  389. $form_content=array('');
  390. $res = $this->squestion->create_form($form_content);
  391. $instans->expectCallCount($tool_name);
  392. $this->assertTrue($res);
  393. $this->assertTrue($tool_name);
  394. //var_dump($res);
  395. //var_dump($tool_name);
  396. }
  397. public function testRenderForm(){
  398. ob_start();
  399. $this->squestion->render_form();
  400. ob_end_clean();
  401. $this->assertNotNull($this->squestion->html);
  402. //var_dump($res);
  403. }
  404. public function testHandleAction(){
  405. global $config;
  406. $form_content['answers']=array('');
  407. $message = 'PleaseEnterAQuestion';
  408. $max_answer = count($form_content['answers']);
  409. foreach ($_POST['delete_answer'] as $key=>$value) {
  410. $delete = $key;
  411. }
  412. $res = $this->squestion->handle_action($form_content);
  413. $this->assertTrue(is_array($res));
  414. $this->assertTrue(isset($form_content['answers'][$max_answer-1]));
  415. //var_dump($res);
  416. }
  417. public function testAddRemoveButtons(){
  418. $form_content['answers'] =array();
  419. $res = $this->squestion->add_remove_buttons($form_content);
  420. $this->assertTrue($res);
  421. //var_dump($res);
  422. }
  423. public function testRenderQuestion(){
  424. $form_content=array('');
  425. $res = $this->squestion->render_question($form_content);
  426. $this->assertNull($res);
  427. $this->assertTrue(is_null($res));
  428. //var_dump($res);
  429. }
  430. public function testCreateForm1(){
  431. $form_content=array('');
  432. $this->syesno->create_form($form_content);
  433. $this->assertNotNull($this->syesno->html);
  434. }
  435. public function testRenderQuestion1(){
  436. ob_start();
  437. $form_content['options']=array('');
  438. $answers=array();
  439. $this->syesno->render_question($form_content,$answers);
  440. ob_end_clean();
  441. $this->assertNotNull($this->syesno->html);
  442. }
  443. public function testCreateForm2(){
  444. global $charset;
  445. $form_content=array('');
  446. $this->smultiplechoice->create_form($form_content);
  447. $this->assertNotNull($this->smultiplechoice->html);
  448. $this->assertTrue(!is_null(html));
  449. }
  450. /**
  451. * @todo it would make more sense to consider yesno as a
  452. * special case of multiplechoice and not the other way
  453. * around
  454. */
  455. public function testRenderQuestion2(){
  456. $form_content= array('');
  457. $answers=array('');
  458. $this->smultiplechoice->render_question($form_content, $answers);
  459. $this->assertNull($this->smultiplechoice->html);
  460. }
  461. public function testCreateForm3(){
  462. $form_content['answers']=array();
  463. $count= 0;
  464. $this->spersonality;
  465. $this->spersonality->create_form($form_content);
  466. $this->assertNotNull($this->spersonality->html);
  467. $this->assertTrue($this->spersonality->html);
  468. }
  469. public function testRenderQuestion3(){
  470. $form_content=array();
  471. $answers=array();
  472. $this->spersonality->render_question($form_content,$answers);
  473. $this->assertNull($this->spersonality->html);
  474. $this->assertFalse($this->spersonality->html);
  475. }
  476. public function testCreateForm4(){
  477. global $charset;
  478. $form_content=array();
  479. $this->smultipleresponse->create_form($form_content);
  480. $this->assertNotNull($this->smultipleresponse->html);
  481. $this->assertTrue($this->smultipleresponse->html);
  482. }
  483. }
  484. ?>