PageRenderTime 100ms CodeModel.GetById 23ms RepoModel.GetById 18ms app.codeStats 0ms

/testing/phpunit/test-courses.php

https://code.google.com/p/classroom-presenter/
PHP | 420 lines | 216 code | 106 blank | 98 comment | 28 complexity | 771eb617f9292f8355778b2fd664866d MD5 | raw file
Possible License(s): Apache-2.0, IPL-1.0
  1. <?php
  2. class testCourses extends PHPUnit_Framework_TestCase
  3. {
  4. // This function creates a database connection
  5. public function connectToDatabase() {
  6. include "../../db_credentials.php";
  7. // Connect to the database and insert an arbitrary question
  8. $db_conn = mysql_connect("cubist.cs.washington.edu", $username, $password);
  9. if (!$db_conn) {
  10. die("Could not connect");
  11. }
  12. mysql_select_db($db_name, $db_conn);
  13. return $db_conn;
  14. }
  15. // This function initializes the database given a test instructor
  16. // and a test student. Returns an array where the first index is the
  17. // uid of the instructor and the second is the uid of the student
  18. public function init($instructor, $student) {
  19. $db_conn = $this->connectToDatabase();
  20. // Now insert the instructor into the user table and the instructor
  21. // table
  22. $query = sprintf("INSERT INTO User (email) VALUES ('%s');", $instructor);
  23. $results = mysql_query($query, $db_conn);
  24. // Error Check
  25. if (!$results) {
  26. die ("Error: " . mysql_error($db_conn));
  27. }
  28. // Get the uid for the instructor
  29. $query = sprintf("SELECT uid FROM User WHERE email = '%s';", $instructor);
  30. $results = mysql_query($query, $db_conn);
  31. // Error Check
  32. if (!$results) {
  33. die ("Error: " . mysql_error($db_conn));
  34. }
  35. $row = mysql_fetch_row($results);
  36. $iuid = $row[0];
  37. $query = sprintf("INSERT INTO Instructor VALUES (%d);", $iuid);
  38. $results = mysql_query($query, $db_conn);
  39. // Error Check
  40. if (!$results) {
  41. die ("Error: " . mysql_error($db_conn));
  42. }
  43. // Now do the same for the student
  44. $query = sprintf("INSERT INTO User (email) VALUES ('%s');", $student);
  45. $results = mysql_query($query, $db_conn);
  46. // Error check
  47. if (!$results) {
  48. die ("Error: " . mysql_error($db_conn));
  49. }
  50. // Get the uid
  51. $query = sprintf("SELECT uid FROM User WHERE email = '%s';", $student);
  52. $results = mysql_query($query, $db_conn);
  53. // Error check
  54. if (!$results) {
  55. die ("Error: " . mysql_error($db_conn));
  56. }
  57. $row = mysql_fetch_row($results);
  58. $suid = $row[0];
  59. $query = sprintf("INSERT INTO Student (uid) VALUES (%d);", $suid);
  60. $results = mysql_query($query, $db_conn);
  61. // Error Check
  62. if (!$results) {
  63. die ("Error: " . mysql_error($db_conn));
  64. }
  65. $uids[0] = $iuid;
  66. $uids[1] = $suid;
  67. return $uids;
  68. }
  69. // This function adds a course to the database given a course name,
  70. // an instructor uid, and a flag indicating whether the uid is in fact
  71. // an instructor
  72. public function addCourse($courseName, $iuid) {
  73. $_POST['uid'] = $iuid;
  74. $_POST['name'] = $courseName;
  75. $_POST['mailinglist'] = 'test';
  76. include "../../Incognito/instr/scripts/create_course.php";
  77. // Make sure that we actually got a cid back
  78. $num_results = 0;
  79. if (isset($cid)) {
  80. // Check to make sure that the course was in fact inserted
  81. $db_conn = $this->connectToDatabase();
  82. $query = sprintf("SELECT * FROM Course WHERE cid = %d;", $cid);
  83. $results = mysql_query($query, $db_conn);
  84. // Error check
  85. if (!$results) {
  86. die ("Error: " . mysql_error($db_conn));
  87. }
  88. $num_results = mysql_num_rows($results);
  89. }
  90. // Check if we are do the test on an instructor a student
  91. $this->assertEquals(1, $num_results);
  92. return $cid;
  93. }
  94. // This function adds a series of courses and returns the array of courses created
  95. // given an instructor uid, and given a student id, tests that a student cannot
  96. // create a course.
  97. public function addCourses($iuid, $suid) {
  98. // Define the number of courses we will create
  99. $NUM_COURSES = 20;
  100. $courses;
  101. for ($i = 0; $i < $NUM_COURSES; $i++) {
  102. $courses[$i] = $this->addCourse(rand(1000, 9999), $iuid);
  103. }
  104. return $courses;
  105. }
  106. // This function adds a student to a course. Like I mentioned before, since
  107. // I have other unit tests for the add student script, I will assume that it
  108. // is working.
  109. public function addStudent($uid, $cid) {
  110. $_POST['uid'] = $uid;
  111. $_POST['cid'] = $cid;
  112. include "../../Incognito/instr/scripts/addStudent.php";
  113. }
  114. // This function opens a session for a paritcular course given
  115. // a course id and an instructor id
  116. public function openSession($cid, $uid) {
  117. $_POST['uid'] = $uid;
  118. $_POST['cid'] = $cid;
  119. include "../../Incognito/instr/scripts/start_session.php";
  120. // First see if we got a result back
  121. $num_results = 0;
  122. $sid = 0;
  123. if (isset($row[0])) {
  124. // Then query the database to see if the session is in the db
  125. $db_conn = $this->connectToDatabase();
  126. $sid = $row[0];
  127. $query = sprintf("SELECT * FROM Session WHERE sid = %d;", $sid);
  128. $results = mysql_query($query, $db_conn);
  129. // Error check
  130. if (!$results) {
  131. die ("Error: " . mysql_error($db_conn));
  132. }
  133. $num_results = mysql_num_rows($results);
  134. }
  135. // Should be a session that we just created
  136. $this->assertEquals(1, $num_results);
  137. return $sid;
  138. }
  139. // This function has a student join a session and checks to make sure
  140. // that the student actually joined the session
  141. public function joinSession($sid, $uid) {
  142. $_POST['uid'] = $uid;
  143. $_POST['sid'] = $sid;
  144. include "../../Incognito/students/scripts/join_session.php";
  145. // Now query the database to see if the student made it into the Joined table
  146. $db_conn = $this->connectToDatabase();
  147. $query = sprintf("SELECT * FROM Joined WHERE sid = %d AND uid = %d;", $sid, $uid);
  148. $results = mysql_query($query, $db_conn);
  149. // Error check
  150. if (!$results) {
  151. die ("Error: " . mysql_error($db_conn));
  152. }
  153. // If we an entry in the Joined table then the script passes
  154. $this->assertEquals(1, mysql_num_rows($results));
  155. }
  156. // This function has a student leave the session and test whether
  157. // the student successfully leaves the session
  158. public function exitSession($sid, $uid) {
  159. // Call the script
  160. $_POST['sid'] = $sid;
  161. $_POST['uid'] = $uid;
  162. include "../../Incognito/students/scripts/exit_session.php";
  163. // Now see if the sid, uid is still in the Joined table
  164. $db_conn = $this->connectToDatabase();
  165. $query = sprintf("SELECT * FROM Joined WHERE sid = %d AND uid = %d;", $sid, $uid);
  166. $results = mysql_query($query, $db_conn);
  167. // Error check
  168. if (!$results) {
  169. die ("Error: " . mysql_error($db_conn));
  170. }
  171. // We know that something went wrong if the number of rows is != 0
  172. $this->assertEquals(0, mysql_num_rows($results));
  173. }
  174. // This function attempts to close down the session associated with
  175. // a course. In addition, this function checks the database to make
  176. // sure the closure of the session is reflected.
  177. public function closeSession($cid) {
  178. // Call the end session php script
  179. $_POST['cid'] = $cid;
  180. include "../../Incognito/instr/scripts/end_session.php";
  181. // Now make sure that the database reflects theses changes
  182. $db_conn = $this->connectToDatabase();
  183. $query = sprintf("SELECT * FROM Session WHERE cid = %d;", $cid);
  184. $results = mysql_query($query, $db_conn);
  185. // Error Check
  186. if (!$results) {
  187. die ("Error: " . mysql_error($db_conn));
  188. }
  189. // We know that there should no longer be a session associated with that
  190. // cid, so there should 0 zero rows in the result
  191. $this->assertEquals(0, mysql_num_rows($results));
  192. }
  193. // This function calls the script that removes allows a student to remove
  194. // themselves from the course. In addition, this function tests that the
  195. // database reflects this change
  196. public function removeCourse($cid, $uid) {
  197. // Call the delete course php script on the student side
  198. $_POST['cid'] = $cid;
  199. $_POST['uid'] = $uid;
  200. include "../../Incognito/students/scripts/delete_course.php";
  201. // Now test to make sure the cid, uid pair is no longer in the Joined
  202. // table
  203. $db_conn = $this->connectToDatabase();
  204. $query = sprintf("SELECT * FROM Attends WHERE cid = %d AND uid = %d;", $cid, $uid);
  205. $results = mysql_query($query, $db_conn);
  206. // Error check
  207. if (!$results) {
  208. die ("Error: " . mysql_error($db_conn));
  209. }
  210. // We expect that there should no longer be the pair, so the number of
  211. // rows should be zero
  212. $this->assertEquals(0, mysql_num_rows($results));
  213. }
  214. // This function calls the delete course php script on the instructor side
  215. // thatdeletes a course completely from the database. In adddition,
  216. // this function checks to make sure that this change in the database is
  217. // actually made.
  218. public function deleteCourse($cid, $uid) {
  219. // First call the delete course on the instructor side
  220. $_POST['cid'] = $cid;
  221. $_POST['uid'] = $uid;
  222. include "../../Incognito/instr/scripts/delete_course.php";
  223. // Now run queries to check both the Teaches table and Course table
  224. // to make sure the appropriate changes are reflected
  225. $db_conn = $this->connectToDatabase();
  226. $query = sprintf("SELECT * FROM Teaches WHERE cid = %d AND uid = %d;", $cid, $uid);
  227. $results = mysql_query($query, $db_conn);
  228. // Error check
  229. if (!$results) {
  230. die ("Error: " . mysql_error($db_conn));
  231. }
  232. // We expect that the number of rows of the result are zero since the
  233. // instructor should no longer be teaching the course
  234. $this->assertEquals(0, mysql_num_rows($results));
  235. $query = sprintf("SELECT * FROM Course WHERE cid = %d;", $cid);
  236. $results = mysql_query($query, $db_conn);
  237. // Error check
  238. if (!$results) {
  239. die ("Error: " . mysql_error($db_conn));
  240. }
  241. // Similar to above, the course should be deleted so the number
  242. // of rows in the result should be zero
  243. $this->assertEquals(0, mysql_num_rows($results));
  244. }
  245. // This function is the teardown of the test and just deletes the
  246. // instructor and student from the database where uids is an array
  247. // where the first item is the instructor uid and the second is the
  248. // student uid.
  249. public function destoryTestValues($uids) {
  250. $db_conn = $this->connectToDatabase();
  251. $query = sprintf("DELETE FROM Instructor WHERE uid = %d;", $uids[0]);
  252. $results = mysql_query($query, $db_conn);
  253. // Error check
  254. if (!$results) {
  255. die ("Error: " . mysql_error($db_conn));
  256. }
  257. $query = sprintf("DELETE FROM Student WHERE uid = %d;", $uids[1]);
  258. $results = mysql_query($query, $db_conn);
  259. // Error check
  260. if (!$results) {
  261. die ("Error: " . mysql_error($db_conn));
  262. }
  263. $query = sprintf("DELETE FROM User WHERE uid = %d OR uid = %d;", $uids[0], $uids[1]);
  264. $results = mysql_query($query, $db_conn);
  265. // Error check
  266. if (!$results) {
  267. die ("Error: " . mysql_error($db_conn));
  268. }
  269. }
  270. // This test function goes through the entire sequence of events for a
  271. // proper use and initialization of courses
  272. public function testUseCourses() {
  273. // Make instructors and students
  274. $instructor = "test-inst";
  275. $student = "test-stdnt";
  276. $uids = $this->init($instructor, $student);
  277. $courses = $this->addCourses($uids[0], $uids[1]);
  278. // Next we want to add the student to the courses (Note,
  279. // I already created a unit test for this script, so I am
  280. // going to assume it works)
  281. for ($i = 0; $i < sizeof($courses); $i++) {
  282. $this->addStudent($uids[1], $courses[$i]);
  283. }
  284. // Now that we have added the student to all of the courses we can
  285. // open a session for half of the courses
  286. $sessions;
  287. for ($i = 0; $i < sizeof($courses); $i++) {
  288. $sessions[$i] = $this->openSession($courses[$i], $uids[0]);
  289. }
  290. // Now have the student join the sessions for each of the courses
  291. for ($i = 0; $i < sizeof($sessions); $i++) {
  292. $this->joinSession($sessions[$i], $uids[1]);
  293. }
  294. // Now we will test the student exiting the session
  295. for ($i = 0; $i < sizeof($sessions); $i++) {
  296. $this->exitSession($sessions[$i], $uids[1]);
  297. }
  298. // Now test closing the session on the instructor's side
  299. for ($i = 0; $i < sizeof($courses); $i++) {
  300. $this->closeSession($courses[$i]);
  301. }
  302. // Now test the removal of a course by a student
  303. for ($i = 0; $i < sizeof($courses); $i++) {
  304. $this->removeCourse($courses[$i], $uids[1]);
  305. }
  306. // Now test the instructor removing the course from the database
  307. for ($i = 0; $i < sizeof($courses); $i++) {
  308. $this->deleteCourse($courses[$i], $uids[0]);
  309. }
  310. // Now do a teardown of the tests
  311. $this->destoryTestValues($uids);
  312. }
  313. }
  314. ?>