' .
preg_quote(get_string('yougotnright', 'question', $a), '/') . '/');
}
protected function get_does_not_contain_specific_feedback_expectation() {
return new question_no_pattern_expectation('/class="specificfeedback"/');
}
protected function get_contains_validation_error_expectation() {
return new question_contains_tag_with_attribute('div', 'class', 'validationerror');
}
protected function get_does_not_contain_validation_error_expectation() {
return new question_no_pattern_expectation('/class="validationerror"/');
}
protected function get_contains_mark_summary($mark) {
$a = new stdClass();
$a->mark = format_float($mark, $this->displayoptions->markdp);
$a->max = format_float($this->quba->get_question_max_mark($this->slot),
$this->displayoptions->markdp);
return new question_pattern_expectation('/' .
preg_quote(get_string('markoutofmax', 'question', $a), '/') . '/');
}
protected function get_contains_marked_out_of_summary() {
$max = format_float($this->quba->get_question_max_mark($this->slot),
$this->displayoptions->markdp);
return new question_pattern_expectation('/' .
preg_quote(get_string('markedoutofmax', 'question', $max), '/') . '/');
}
protected function get_does_not_contain_mark_summary() {
return new question_no_pattern_expectation('/
/');
}
protected function get_contains_checkbox_expectation($baseattr, $enabled, $checked) {
$expectedattributes = $baseattr;
$forbiddenattributes = array();
$expectedattributes['type'] = 'checkbox';
if ($enabled === true) {
$forbiddenattributes['disabled'] = 'disabled';
} else if ($enabled === false) {
$expectedattributes['disabled'] = 'disabled';
}
if ($checked === true) {
$expectedattributes['checked'] = 'checked';
} else if ($checked === false) {
$forbiddenattributes['checked'] = 'checked';
}
return new question_contains_tag_with_attributes('input', $expectedattributes, $forbiddenattributes);
}
protected function get_contains_mc_checkbox_expectation($index, $enabled = null,
$checked = null) {
return $this->get_contains_checkbox_expectation(array(
'name' => $this->quba->get_field_prefix($this->slot) . $index,
'value' => 1,
), $enabled, $checked);
}
protected function get_contains_radio_expectation($baseattr, $enabled, $checked) {
$expectedattributes = $baseattr;
$forbiddenattributes = array();
$expectedattributes['type'] = 'radio';
if ($enabled === true) {
$forbiddenattributes['disabled'] = 'disabled';
} else if ($enabled === false) {
$expectedattributes['disabled'] = 'disabled';
}
if ($checked === true) {
$expectedattributes['checked'] = 'checked';
} else if ($checked === false) {
$forbiddenattributes['checked'] = 'checked';
}
return new question_contains_tag_with_attributes('input', $expectedattributes, $forbiddenattributes);
}
protected function get_contains_mc_radio_expectation($index, $enabled = null, $checked = null) {
return $this->get_contains_radio_expectation(array(
'name' => $this->quba->get_field_prefix($this->slot) . 'answer',
'value' => $index,
), $enabled, $checked);
}
protected function get_contains_hidden_expectation($name, $value = null) {
$expectedattributes = array('type' => 'hidden', 'name' => s($name));
if (!is_null($value)) {
$expectedattributes['value'] = s($value);
}
return new question_contains_tag_with_attributes('input', $expectedattributes);
}
protected function get_does_not_contain_hidden_expectation($name, $value = null) {
$expectedattributes = array('type' => 'hidden', 'name' => s($name));
if (!is_null($value)) {
$expectedattributes['value'] = s($value);
}
return new question_does_not_contain_tag_with_attributes('input', $expectedattributes);
}
protected function get_contains_tf_true_radio_expectation($enabled = null, $checked = null) {
return $this->get_contains_radio_expectation(array(
'name' => $this->quba->get_field_prefix($this->slot) . 'answer',
'value' => 1,
), $enabled, $checked);
}
protected function get_contains_tf_false_radio_expectation($enabled = null, $checked = null) {
return $this->get_contains_radio_expectation(array(
'name' => $this->quba->get_field_prefix($this->slot) . 'answer',
'value' => 0,
), $enabled, $checked);
}
protected function get_contains_cbm_radio_expectation($certainty, $enabled = null,
$checked = null) {
return $this->get_contains_radio_expectation(array(
'name' => $this->quba->get_field_prefix($this->slot) . '-certainty',
'value' => $certainty,
), $enabled, $checked);
}
protected function get_contains_button_expectation($name, $value = null, $enabled = null) {
$expectedattributes = array(
'type' => 'submit',
'name' => $name,
);
$forbiddenattributes = array();
if (!is_null($value)) {
$expectedattributes['value'] = $value;
}
if ($enabled === true) {
$forbiddenattributes['disabled'] = 'disabled';
} else if ($enabled === false) {
$expectedattributes['disabled'] = 'disabled';
}
return new question_contains_tag_with_attributes('input', $expectedattributes, $forbiddenattributes);
}
/**
* Returns an epectation that a string contains the HTML of a button with
* name {question-attempt prefix}-submit, and eiter enabled or not.
* @param bool $enabled if not null, check the enabled/disabled state of the button. True = enabled.
* @return question_contains_tag_with_attributes an expectation for use with check_current_output.
*/
protected function get_contains_submit_button_expectation($enabled = null) {
return $this->get_contains_button_expectation(
$this->quba->get_field_prefix($this->slot) . '-submit', null, $enabled);
}
/**
* Returns an epectation that a string does not contain the HTML of a button with
* name {question-attempt prefix}-submit.
* @return question_contains_tag_with_attributes an expectation for use with check_current_output.
*/
protected function get_does_not_contain_submit_button_expectation() {
return new question_no_pattern_expectation('/name="' .
$this->quba->get_field_prefix($this->slot) . '-submit"/');
}
protected function get_tries_remaining_expectation($n) {
return new question_pattern_expectation('/' .
preg_quote(get_string('triesremaining', 'qbehaviour_interactive', $n), '/') . '/');
}
protected function get_invalid_answer_expectation() {
return new question_pattern_expectation('/' .
preg_quote(get_string('invalidanswer', 'question'), '/') . '/');
}
protected function get_contains_try_again_button_expectation($enabled = null) {
$expectedattributes = array(
'type' => 'submit',
'name' => $this->quba->get_field_prefix($this->slot) . '-tryagain',
);
$forbiddenattributes = array();
if ($enabled === true) {
$forbiddenattributes['disabled'] = 'disabled';
} else if ($enabled === false) {
$expectedattributes['disabled'] = 'disabled';
}
return new question_contains_tag_with_attributes('input', $expectedattributes, $forbiddenattributes);
}
protected function get_does_not_contain_try_again_button_expectation() {
return new question_no_pattern_expectation('/name="' .
$this->quba->get_field_prefix($this->slot) . '-tryagain"/');
}
protected function get_contains_select_expectation($name, $choices,
$selected = null, $enabled = null) {
$fullname = $this->quba->get_field_prefix($this->slot) . $name;
return new question_contains_select_expectation($fullname, $choices, $selected, $enabled);
}
protected function get_mc_right_answer_index($mc) {
$order = $mc->get_order($this->get_question_attempt());
foreach ($order as $i => $ansid) {
if ($mc->answers[$ansid]->fraction == 1) {
return $i;
}
}
$this->fail('This multiple choice question does not seem to have a right answer!');
}
protected function get_no_hint_visible_expectation() {
return new question_no_pattern_expectation('/class="hint"/');
}
protected function get_contains_hint_expectation($hinttext) {
// Does not currently verify hint text.
return new question_contains_tag_with_attribute('div', 'class', 'hint');
}
}
/**
* Simple class that implements the {@link moodle_recordset} API based on an
* array of test data.
*
* See the {@link question_attempt_step_db_test} class in
* question/engine/tests/testquestionattemptstep.php for an example of how
* this is used.
*
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class question_test_recordset extends moodle_recordset {
protected $records;
/**
* Constructor
* @param $table as for {@link testing_db_record_builder::build_db_records()}
* but does not need a unique first column.
*/
public function __construct(array $table) {
$columns = array_shift($table);
$this->records = array();
foreach ($table as $row) {
if (count($row) != count($columns)) {
throw new coding_exception("Row contains the wrong number of fields.");
}
$rec = array();
foreach ($columns as $i => $name) {
$rec[$name] = $row[$i];
}
$this->records[] = $rec;
}
reset($this->records);
}
public function __destruct() {
$this->close();
}
public function current() {
return (object) current($this->records);
}
public function key() {
if (is_null(key($this->records))) {
return false;
}
$current = current($this->records);
return reset($current);
}
public function next() {
next($this->records);
}
public function valid() {
return !is_null(key($this->records));
}
public function close() {
$this->records = null;
}
}
/**
* Helper class for tests that help to test core_question_renderer.
*
* @copyright 2018 Huong Nguyen
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class testable_core_question_renderer extends core_question_renderer {
/**
* Test the private number function.
*
* @param null|string $number
* @return HTML
*/
public function number($number) {
return parent::number($number);
}
}