function checkPostRedirect($client) { global $passed; line_out("Checking to see if there was a POST redirect to a GET"); $method = $client->getRequest()->getMethod(); if ($method == "get") { $passed++; } else { error_out('Expecting POST to Redirect to GET - found ' . $method); } }
} $grade = 0; error_log("ASSN02 " . $url); line_out("Retrieving " . htmlent_utf8($url) . "..."); flush(); // http://symfony.com/doc/current/components/dom_crawler.html $client = new Client(); $crawler = $client->request('GET', $url); $html = $crawler->html(); $OUTPUT->togglePre("Show retrieved page", $html); line_out("Searching for h1 tag..."); $passed = 0; $titlefound = false; try { $h1 = $crawler->filter('h1')->text(); line_out("Found h1 tag..."); } catch (Exception $ex) { error_out("Did not find h1 tag"); $h1 = ""; } if (stripos($h1, 'Hello') !== false) { success_out("Found 'Hello' in the h1 tag"); $passed += 1; } else { error_out("Did not find 'Hello' in the h1 tag"); } if ($USER->displayname && stripos($h1, $USER->displayname) !== false) { success_out("Found ({$USER->displayname}) in the h1 tag"); $passed += 1; } else { if ($USER->displayname) {
/** * Dump a debug array with messages and optional detail * * This kind of debug array comes back from some of the * grade calls. We loop through printing the messages and * put the detail into a togglable pre tag is present. */ function dumpDebugArray($debug_log) { if (!is_array($debug_log)) { return; } foreach ($debug_log as $k => $v) { if (count($v) > 1) { $this->togglePre($v[0], $v[1]); } else { line_out($v[0]); } } }
$crawler = $client->request('GET', $u); $html = $crawler->html(); $OUTPUT->togglePre("Show retrieved page", $html); line_out("Looking for 'Your guess is too high'"); if (stripos($html, 'Your guess is too high') > 0) { $passed++; } else { error_out("Not found"); } // Good guess $u = $url . "?guess=42"; line_out("Retrieving " . htmlent_utf8($u)); $crawler = $client->request('GET', $u); $html = $crawler->html(); $OUTPUT->togglePre("Show retrieved page", $html); line_out("Looking for 'Congratulations - You are right'"); if (stripos($html, 'congratulations') > 0) { $passed++; } else { error_out("Not found"); } } catch (Exception $ex) { error_out("The autograder did not find something it was looking for in your HTML - test ended."); error_log($ex->getMessage()); error_log($ex->getTraceAsString()); $detail = "This indicates the source code line where the test stopped.\n" . "It may not make any sense without looking at the source code for the test.\n" . 'Caught exception: ' . $ex->getMessage() . "\n" . $ex->getTraceAsString() . "\n"; $OUTPUT->togglePre("Internal error detail.", $detail); } $perfect = 10; $score = webauto_compute_effective_score($perfect, $passed, $penalty); if (!$titlefound) {
$crawler = $client->request('GET', $url); $html = $crawler->html(); $OUTPUT->togglePre("Show retrieved page", $html); line_out("Searching for h1 tag..."); try { $h1 = $crawler->filter('h1')->text(); line_out("Found h1 tag..."); } catch (Exception $ex) { error_out("Did not find h1 tag"); $h1 = ""; } if ($displayname && strpos($h1, $displayname) !== false) { success_out("Found ({$displayname}) in the h1 tag"); } else { if ($displayname) { line_out("Warning: Unable to find {$displayname} in the h1 tag"); } } $success = ""; $failure = ""; $grade = 0.0; if (strpos($h1, "Dr. Chuck") !== false) { $failure = "You need to put your own name in the h1 tag - assignment not complete!"; } else { if (strpos($h1, 'Hello') !== false) { $success = "Found 'Hello' in the h1 tag - assignment correct!"; $grade = 1.0; } else { $failure = "Did not find 'Hello' in the h1 tag - assignment not complete!"; } }
function webauto_test_passed($grade, $url) { global $displayname; global $OUTPUT; success_out("Test passed - congratulations"); if ($displayname === false || !isset($_SESSION['lti'])) { line_out('Not setup to return a grade..'); exit; } if (!isset($_GET['grade'])) { line_out('Dry run - grade of (' . intval($grade * 100) . '%) was not sent.'); exit; } gradeUpdateJson(json_encode(array("url" => $url))); $debug_log = array(); $retval = LTIX::gradeSend($grade, false, $debug_log); $OUTPUT->dumpDebugArray($debug_log); if ($retval == true) { $success = "Grade sent to server (" . intval($grade * 100) . "%)"; } else { if (is_string($retval)) { $failure = "Grade not sent: " . $retval; } else { echo "<pre>\n"; var_dump($retval); echo "</pre>\n"; $failure = "Internal error"; } } if (strlen($success) > 0) { success_out($success); error_log($success); } else { if (strlen($failure) > 0) { error_out($failure); error_log($failure); } else { error_log("No status"); } } }
preg_match("/.*location.*=.*'(.*?)'/", $onclick, $matches); if (count($matches) == 2) { $url = $matches[1]; } } catch (Exception $ex) { $url = false; line_out("Looking for Logout Anchor Tag."); $link = $crawler->selectLink('Logout')->link(); $url = $link->getURI(); } line_out("Retrieving " . htmlent_utf8($url) . "..."); $crawler = $client->request('GET', $url); $html = $crawler->html(); $OUTPUT->togglePre("Show retrieved page", $html); $passed++; line_out("Looking for login link."); $link = $crawler->selectLink('Log In')->link(); $url = $link->getURI(); $passed++; } catch (Exception $ex) { error_out("The autograder did not find something it was looking for in your HTML - test ended."); error_log($ex->getMessage()); error_log($ex->getTraceAsString()); $detail = "This indicates the source code line where the test stopped.\n" . "It may not make any sense without looking at the source code for the test.\n" . 'Caught exception: ' . $ex->getMessage() . "\n" . $ex->getTraceAsString() . "\n"; $OUTPUT->togglePre("Internal error detail.", $detail); } // There is a maximum of 20 passes for this test $perfect = 20; $score = webauto_compute_effective_score($perfect, $passed, $penalty); if (!$titlefound) { error_out("These pages do not have proper titles so this grade is not official");
$retval = webauto_check_title($crawler); if ($retval === true) { $titlefound = true; } else { if (!$title_once) { error_out($retval); } $title_once = true; } $matches = array(); preg_match('/Your Play=([^ ]*) Computer Play=([^ ]*) Result=(.*)/', $html, $matches); if (count($matches) != 4) { error_out('Could not find line starting with "Your Play=" (case matters)'); continue; } line_out('Found:' . $matches[0]); if (check($matches)) { success_out('Correct play'); $passed++; } else { error_out('Incorrect play'); } } $perfect = 5; $score = webauto_compute_effective_score($perfect, $passed, $penalty); if (!$titlefound) { error_out("These pages do not have proper titles so this grade was not sent"); return; } if ($score > 0.0) { webauto_test_passed($score, $url);
$pos2 = strpos($html, "delete.php"); if ($pos2 < 1) { break; } $pos3 = strpos($html, '"', $pos2); if ($pos3 < 1) { break; } $editlink = substr($html, $pos2, $pos3 - $pos2); $editlink = str_replace("&", "&", $editlink); line_out("Retrieving " . $editlink . "..."); $crawler = $client->request('GET', $editlink); $html = $crawler->html(); $OUTPUT->togglePre("Show retrieved page", $html); // Do the Delete line_out('Looking for the form with a value="Delete" submit button'); $form = $crawler->selectButton('Delete')->form(); $crawler = $client->submit($form); checkPostRedirect($client); $html = $crawler->html(); $OUTPUT->togglePre("Show retrieved page", $html); $passed--; // Undo post redirect } } catch (Exception $ex) { error_out("The autograder did not find something it was looking for in your HTML - test ended."); error_log($ex->getMessage()); error_log($ex->getTraceAsString()); $detail = "This indicates the source code line where the test stopped.\n" . "It may not make any sense without looking at the source code for the test.\n" . 'Caught exception: ' . $ex->getMessage() . "\n" . $ex->getTraceAsString() . "\n"; $OUTPUT->togglePre("Internal error detail.", $detail); }
$passed++; } catch (Exception $ex) { error_out("The autograder did not find something it was looking for in your HTML - test ended."); error_log($ex->getMessage()); error_log($ex->getTraceAsString()); $detail = "This indicates the source code line where the test stopped.\n" . "It may not make any sense without looking at the source code for the test.\n" . 'Caught exception: ' . $ex->getMessage() . "\n" . $ex->getTraceAsString() . "\n"; $OUTPUT->togglePre("Internal error detail.", $detail); } // There is a maximum of 20 passes for this test $perfect = 20; $score = $passed * (1.0 / $perfect); if ($score < 0) { $score = 0; } if ($score > 1) { $score = 1; } $scorestr = "Score = {$score} ({$passed}/{$perfect})"; if ($penalty === false) { line_out("Score = {$score} ({$passed}/{$perfect})"); } else { $score = $score * (1.0 - $penalty); line_out("Score = {$score} ({$passed}/{$perfect}) penalty={$penalty}"); } if (!$titlepassed) { error_out("These pages do not have proper titles so this grade is not official"); return; } if ($score > 0.0) { webauto_test_passed($score, $url); }
$editlink = substr($html, $pos2, $pos3 - $pos2); line_out("Retrieving " . htmlent_utf8($editlink) . "..."); $crawler = $client->request('GET', $editlink); $html = $crawler->html(); $OUTPUT->togglePre("Show retrieved page", $html); $passed++; if (strpos($html, "AC<DC") > 2) { $passed += 2; } else { if (strpos($html, "&lt;") > 2) { error_out("It looks like you have double-called htmlentities()"); } else { error_out("Found HTML Injection"); } } line_out("Looking for the form with a 'Delete' submit button"); $form = $crawler->selectButton('Delete')->form(); $crawler = $client->submit($form); $html = $crawler->html(); $OUTPUT->togglePre("Show retrieved page", $html); $passed++; checkPostRedirect($client); } catch (Exception $ex) { error_out("The autograder did not find something it was looking for in your HTML - test ended."); error_log($ex->getMessage()); error_log($ex->getTraceAsString()); $detail = "This indicates the source code line where the test stopped.\n" . "It may not make any sense without looking at the source code for the test.\n" . 'Caught exception: ' . $ex->getMessage() . "\n" . $ex->getTraceAsString() . "\n"; $OUTPUT->togglePre("Internal error detail.", $detail); } // There is a maximum of 26 passes for this test $perfect = 26;