public function test_check_external_forbidden_words_literal() { $cases = array(array('3+5', '+', true), array('sin(a)', 'a', true), array('sin(a)', 'i', true), array('sin(a)', 'b', false), array('sin(a)', 'b,\\,,c', false), array('[x,y,z]', 'b,\\,,c', true), array('diff(x^2,x)', '[[BASIC-CALCULUS]]', true), array('solve((x-6)^4,x)', '[[BASIC-ALGEBRA]]', true)); foreach ($cases as $case) { $cs = new stack_cas_casstring($case[0]); $this->assertEquals($case[2], $cs->check_external_forbidden_words_literal($case[1])); } }
/** * This is the basic validation of the student's "answer". * This method is only called if the input is not blank. * * Only a few input methods need to modify this method. * For example, Matrix types have two dimensional contents arrays to loop over. * * @param array $contents the content array of the student's input. * @return array of the validity, errors strings and modified contents. */ protected function validate_contents($contents, $forbiddenkeys) { $errors = $this->extra_validation($contents); $valid = !$errors; // Now validate the input as CAS code. $modifiedcontents = array(); $allowwords = $this->get_parameter('allowWords', ''); foreach ($contents as $val) { $answer = new stack_cas_casstring($val); $answer->get_valid('s', $this->get_parameter('strictSyntax', true), $this->get_parameter('insertStars', 0), $allowwords); // Ensure student hasn't used a variable name used by the teacher. if ($forbiddenkeys) { $answer->check_external_forbidden_words($forbiddenkeys); } $forbiddenwords = $this->get_parameter('forbidWords', ''); if ($forbiddenwords) { $answer->check_external_forbidden_words_literal($forbiddenwords); } $modifiedcontents[] = $answer->get_casstring(); $valid = $valid && $answer->get_valid(); $errors .= $answer->get_errors(); } return array($valid, $errors, $modifiedcontents); }