From 5800ab30be0b5c6d4e9e869b9dd08512e476da29 Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Wed, 14 Feb 2024 20:13:17 +0000 Subject: [PATCH] Fix coding style --- .../backup_qtype_varnumunit_plugin.class.php | 28 +++--- .../restore_qtype_varnumunit_plugin.class.php | 6 +- classes/output/mobile.php | 4 +- db/mobile.php | 10 +- edit_varnumunit_form.php | 69 ++++++------- lib.php | 2 +- question.php | 12 +-- questiontype.php | 44 ++++----- renderer.php | 2 +- tests/behat/preview.feature | 2 +- tests/helper.php | 32 +++--- tests/privacy_provider_test.php | 25 ++--- tests/question_test.php | 77 ++++++++------- tests/questiontype_test.php | 97 +++++++++---------- tests/walkthrough_test.php | 94 +++++++++--------- version.php | 4 +- 16 files changed, 254 insertions(+), 254 deletions(-) diff --git a/backup/moodle2/backup_qtype_varnumunit_plugin.class.php b/backup/moodle2/backup_qtype_varnumunit_plugin.class.php index 1777036..0363abf 100644 --- a/backup/moodle2/backup_qtype_varnumunit_plugin.class.php +++ b/backup/moodle2/backup_qtype_varnumunit_plugin.class.php @@ -60,15 +60,15 @@ protected function define_question_plugin_structure() { $this->add_question_qtype_varnumunit_units($pluginwrapper); // Now create the qtype own structures. - $varnumunit = new backup_nested_element('varnumunit', array('id'), array( - 'randomseed', 'recalculateeverytime', 'requirescinotation', 'unitfraction')); + $varnumunit = new backup_nested_element('varnumunit', ['id'], [ + 'randomseed', 'recalculateeverytime', 'requirescinotation', 'unitfraction']); // Now the own qtype tree. $pluginwrapper->add_child($varnumunit); // Set source to populate the data. $varnumunit->set_source_table('qtype_varnumunit', - array('questionid' => backup::VAR_PARENTID)); + ['questionid' => backup::VAR_PARENTID]); // Don't need to annotate ids nor files. @@ -83,8 +83,8 @@ protected function add_question_qtype_varnumunit_vars($element) { // Define the elements. $vars = new backup_nested_element('vars'); - $var = new backup_nested_element('var', array('id'), - array('varno', 'nameorassignment')); + $var = new backup_nested_element('var', ['id'], + ['varno', 'nameorassignment']); $this->add_question_qtype_varnumunit_variants($var); @@ -94,7 +94,7 @@ protected function add_question_qtype_varnumunit_vars($element) { // Set source to populate the data. $var->set_source_table('qtype_varnumunit_vars', - array('questionid' => backup::VAR_PARENTID)); + ['questionid' => backup::VAR_PARENTID]); } protected function add_question_qtype_varnumunit_variants($element) { @@ -106,8 +106,8 @@ protected function add_question_qtype_varnumunit_variants($element) { // Define the elements. $variants = new backup_nested_element('variants'); - $variant = new backup_nested_element('variant', array('id'), - array('varid', 'variantno', 'value')); + $variant = new backup_nested_element('variant', ['id'], + ['varid', 'variantno', 'value']); // Build the tree. $element->add_child($variants); @@ -115,7 +115,7 @@ protected function add_question_qtype_varnumunit_variants($element) { // Set source to populate the data. $variant->set_source_table('qtype_varnumunit_variants', - array('varid' => backup::VAR_PARENTID)); + ['varid' => backup::VAR_PARENTID]); } protected function add_question_qtype_varnumunit_answers($element) { @@ -127,9 +127,9 @@ protected function add_question_qtype_varnumunit_answers($element) { // Define the elements. $answers = new backup_nested_element('varnumunit_answers'); - $answer = new backup_nested_element('varnumunit_answer', array('id'), array( + $answer = new backup_nested_element('varnumunit_answer', ['id'], [ 'answerid', 'error', 'sigfigs', 'checknumerical', 'checkscinotation', - 'checkpowerof10', 'checkrounding', 'syserrorpenalty', 'checkscinotationformat')); + 'checkpowerof10', 'checkrounding', 'syserrorpenalty', 'checkscinotationformat']); // Build the tree. $element->add_child($answers); @@ -142,7 +142,7 @@ protected function add_question_qtype_varnumunit_answers($element) { JOIN {question_answers} ans ON ans.id = vans.answerid WHERE ans.question = :question ORDER BY id', - array('question' => backup::VAR_PARENTID)); + ['question' => backup::VAR_PARENTID]); // Don't need to annotate ids or files. } @@ -164,7 +164,7 @@ protected function add_question_qtype_varnumunit_units($element) { // Set source to populate the data. $unit->set_source_table('qtype_varnumunit_units', - array('questionid' => backup::VAR_PARENTID), 'id ASC'); + ['questionid' => backup::VAR_PARENTID], 'id ASC'); } /** @@ -174,6 +174,6 @@ protected function add_question_qtype_varnumunit_units($element) { * files to be processed both in backup and restore. */ public static function get_qtype_fileareas() { - return array('unitsfeedback' => 'qtype_varnumunit_unit'); + return ['unitsfeedback' => 'qtype_varnumunit_unit']; } } diff --git a/backup/moodle2/restore_qtype_varnumunit_plugin.class.php b/backup/moodle2/restore_qtype_varnumunit_plugin.class.php index 04b527b..7afacc1 100644 --- a/backup/moodle2/restore_qtype_varnumunit_plugin.class.php +++ b/backup/moodle2/restore_qtype_varnumunit_plugin.class.php @@ -34,16 +34,16 @@ class restore_qtype_varnumunit_plugin extends restore_qtype_plugin { */ protected function define_question_plugin_structure() { - $paths = array(); + $paths = []; // This qtype uses question_answers, add them. $this->add_question_question_answers($paths); - $elements = array('qtype_varnumunit' => '/varnumunit', + $elements = ['qtype_varnumunit' => '/varnumunit', 'qtype_varnumunit_answer' => '/varnumunit_answers/varnumunit_answer', 'qtype_varnumunit_var' => '/vars/var', 'qtype_varnumunit_variant' => '/vars/var/variants/variant', - 'qtype_varnumunit_unit' => '/units/unit'); + 'qtype_varnumunit_unit' => '/units/unit']; foreach ($elements as $elename => $path) { $elepath = $this->get_pathfor($path); $paths[] = new restore_path_element($elename, $elepath); diff --git a/classes/output/mobile.php b/classes/output/mobile.php index 047d9ff..20e38aa 100644 --- a/classes/output/mobile.php +++ b/classes/output/mobile.php @@ -36,10 +36,10 @@ public static function varnumunit_view() { 'templates' => [ [ 'id' => 'main', - 'html' => file_get_contents($CFG->dirroot . '/question/type/varnumunit/mobile/varnumunit.html') + 'html' => file_get_contents($CFG->dirroot . '/question/type/varnumunit/mobile/varnumunit.html'), ], ], - 'javascript' => file_get_contents($CFG->dirroot . '/question/type/varnumunit/mobile/varnumunit.js') + 'javascript' => file_get_contents($CFG->dirroot . '/question/type/varnumunit/mobile/varnumunit.js'), ]; } } diff --git a/db/mobile.php b/db/mobile.php index 3a36381..05150c5 100644 --- a/db/mobile.php +++ b/db/mobile.php @@ -32,13 +32,13 @@ 'method' => 'varnumunit_view', 'styles' => [ 'url' => $CFG->wwwroot . '/question/type/varnumericset/mobileapp.css', - 'version' => 2019040200 + 'version' => 2019040200, ], - 'init' => 'varnumunit_view' + 'init' => 'varnumunit_view', ], ], 'lang' => [ // Language strings to be used. - ['err_ousupsubnotsupportedonmobile', 'qtype_varnumericset'] - ] - ] + ['err_ousupsubnotsupportedonmobile', 'qtype_varnumericset'], + ], + ], ]; diff --git a/edit_varnumunit_form.php b/edit_varnumunit_form.php index a56da80..568c761 100644 --- a/edit_varnumunit_form.php +++ b/edit_varnumunit_form.php @@ -22,7 +22,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ - defined('MOODLE_INTERNAL') || die(); require_once($CFG->dirroot . '/question/type/varnumericset/edit_varnumericset_form_base.php'); @@ -43,18 +42,15 @@ public function qtype() { /** * Get list of form elements to repeat for each 'units' block. - * @param object $mform the form being built. - * @param $label the label to use for the header. - * @param $gradeoptions the possible grades for each answer. - * @param $repeatedoptions reference to array of repeated options to fill - * @param $unitoption reference to return the name of $question->options - * field holding an array of units + * @param MoodleQuickForm $mform the form being built. + * @param string $label the label to use for the header. + * @param array $gradeoptions the possible grades for each answer. * @return array of form fields. */ protected function get_per_unit_fields($mform, $label, $gradeoptions) { - $repeated = array(); + $repeated = []; $repeated[] = $mform->createElement('textarea', 'units', $label, - array('rows' => '2', 'cols' => '60', 'class' => 'textareamonospace')); + ['rows' => '2', 'cols' => '60', 'class' => 'textareamonospace']); $spaceinunitoptions = qtype_varnumunit::spaceinunit_options(); $repeated[] = $mform->createElement('select', 'spaceinunit', @@ -67,7 +63,7 @@ protected function get_per_unit_fields($mform, $label, $gradeoptions) { get_string('gradenoun'), $gradeoptions); $repeated[] = $mform->createElement('editor', 'unitsfeedback', get_string('feedback', 'question'), - array('rows' => 5), $this->editoroptions); + ['rows' => 5], $this->editoroptions); return $repeated; } @@ -78,6 +74,7 @@ protected function data_preprocessing($question) { /** * Perform the necessary preprocessing for the unit fields. + * * @param object $question the data being passed to the form. * @return object $question the modified data. */ @@ -86,11 +83,11 @@ protected function data_preprocessing_units($question) { return $question; } - $question->units = array(); - $question->unitsfraction = array(); - $question->spaceinunit = array(); + $question->units = []; + $question->unitsfraction = []; + $question->spaceinunit = []; $question->spacesfeedback = []; - $question->replacedash = array(); + $question->replacedash = []; $key = 0; foreach ($question->options->units as $unitid => $unit) { @@ -129,7 +126,7 @@ protected function data_preprocessing_units($question) { protected function unit_feedback_html_element_preprocess($draftitemidkey, $unitid, $feedback, $feedbackformat, $filearea) { // Feedback field and attached files. - $formelementdata = array(); + $formelementdata = []; $draftitemid = file_get_submitted_draft_itemid($draftitemidkey); $formelementdata['text'] = file_prepare_draft_area( $draftitemid, @@ -149,17 +146,17 @@ protected function unit_feedback_html_element_preprocess($draftitemidkey, $uniti /** * Add a set of form fields, obtained from get_per_answer_fields, to the form, * one for each existing answer, with some blanks for some new ones. - * @param object $mform the form being built. - * @param $label the label to use for each option. - * @param $gradeoptions the possible grades for each answer. - * @param int|\the $minoptions the minimum number of answer blanks to display. + * @param MoodleQuickForm $mform the form being built. + * @param string $label the label to use for each option. + * @param array $gradeoptions the possible grades for each answer. + * @param int $minoptions the minimum number of answer blanks to display. * Default QUESTION_NUMANS_START. - * @param int|\the $addoptions the number of answer blanks to add. Default QUESTION_NUMANS_ADD. + * @param int $addoptions the number of answer blanks to add. Default QUESTION_NUMANS_ADD. * @return void */ - protected function add_per_unit_fields(&$mform, $label, $gradeoptions, - $minoptions = QUESTION_NUMANS_START, $addoptions = QUESTION_NUMANS_ADD) { - global $DB; + protected function add_per_unit_fields($mform, $label, $gradeoptions, + $minoptions = QUESTION_NUMANS_START, $addoptions = QUESTION_NUMANS_ADD) { + $repeated = $this->get_per_unit_fields($mform, $label, $gradeoptions); if (isset($this->question->options)) { @@ -179,15 +176,15 @@ protected function add_per_unit_fields(&$mform, $label, $gradeoptions, $repeatsatstart = $minoptions; } - $repeatedoptions = array(); + $repeatedoptions = []; $repeatedoptions['units']['type'] = PARAM_RAW_TRIMMED; - $repeatedoptions["units"]['helpbutton'] = array('units', 'qtype_varnumunit'); + $repeatedoptions["units"]['helpbutton'] = ['units', 'qtype_varnumunit']; $repeatedoptions["spacesfeedback"]['helpbutton'] = ['spacingfeedback', 'qtype_varnumunit']; // Wating for #257559 Mform disableif does not work on editor element [MDL-29701]. Once this merged, this should work. $repeatedoptions["spacesfeedback"]['disabledif'] = ['spaceinunit', 'neq', qtype_varnumunit::SPACEINUNIT_PRESERVE_SPACE_REQUIRE]; $repeatedoptions["spacesfeedback"]['default'] = [ - 'text' => get_string('spacingfeedback_default', 'qtype_varnumunit') + 'text' => get_string('spacingfeedback_default', 'qtype_varnumunit'), ]; $this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions, 'nounits', 'addunits', $addoptions, @@ -211,7 +208,7 @@ protected function add_other_unit_fields($mform) { get_string('anyotherunit', 'qtype_varnumunit')); $mform->addElement('static', 'otherunitfraction', get_string('gradenoun'), '0%'); $mform->addElement('editor', 'otherunitfeedback', get_string('feedback', 'question'), - array('rows' => 5), $this->editoroptions); + ['rows' => 5], $this->editoroptions); } protected function add_answer_form_part($mform) { @@ -238,16 +235,14 @@ protected function definition() { $mform->removeElement('requirescinotation'); - $requirescinotationoptions = array( + $requirescinotationoptions = [ qtype_varnumunit::SUPERSCRIPT_SCINOTATION_REQUIRED => get_string('superscriptscinotationrequired', 'qtype_varnumunit'), qtype_varnumunit::SUPERSCRIPT_ALLOWED => get_string('superscriptallowed', 'qtype_varnumunit'), - qtype_varnumunit::SUPERSCRIPT_NONE => get_string('superscriptnone', 'qtype_varnumunit') - ); + qtype_varnumunit::SUPERSCRIPT_NONE => get_string('superscriptnone', 'qtype_varnumunit'), + ]; $requirescinotationel = $mform->createElement('select', - 'requirescinotation', - get_string('superscripts', 'qtype_varnumunit'), - $requirescinotationoptions); + 'requirescinotation', get_string('superscripts', 'qtype_varnumunit'), $requirescinotationoptions); $mform->insertElementBefore($requirescinotationel, 'answersinstruct'); } @@ -263,7 +258,7 @@ protected static function grade_weighting() { // b. p/10 for 0 <= p <= 10 // c. 1/q for 1 <= q <= 10 // d. 1/20. - $rawfractions = array( + $rawfractions = [ 1.0000000, 0.9000000, 0.8333333, @@ -285,9 +280,9 @@ protected static function grade_weighting() { 0.1000000, 0.0500000, - ); + ]; - $fractionoptions = array(); + $fractionoptions = []; foreach ($rawfractions as $fraction) { $a = new stdClass(); @@ -304,7 +299,7 @@ public function validation($data, $files) { $units = $data['units']; $unitcount = 0; $maxgrade = false; - $trimmedunits = array(); + $trimmedunits = []; foreach ($units as $key => $unit) { $trimmedunit = trim($unit); if ($trimmedunit !== '') { diff --git a/lib.php b/lib.php index e1395e8..d519ad4 100644 --- a/lib.php +++ b/lib.php @@ -25,7 +25,7 @@ /** * Checks file access for varnumunit questions. */ -function qtype_varnumunit_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) { +function qtype_varnumunit_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options= []) { global $DB, $CFG; require_once($CFG->libdir . '/questionlib.php'); question_pluginfile($course, $context, 'qtype_varnumunit', $filearea, $args, $forcedownload, $options); diff --git a/question.php b/question.php index 393c37d..a03854a 100644 --- a/question.php +++ b/question.php @@ -121,7 +121,7 @@ public function grade_response(array $response) { $gradenumerical = $this->grade_numeric_part_of_response($response); $gradeunit = $this->grade_unit_part_of_response($response); $overallgrade = $this->weight_grades_for_num_and_unit_part($gradenumerical, $gradeunit); - return array($overallgrade, question_state::graded_state_for_fraction($overallgrade)); + return [$overallgrade, question_state::graded_state_for_fraction($overallgrade)]; } protected function weight_grades_for_num_and_unit_part($gradenumerical, $gradeunit) { @@ -157,7 +157,7 @@ protected function split_response_into_num_and_unit($response) { $num = new qtype_varnumericset_number_interpreter_number_with_optional_sci_notation($this->usesupeditor); $num->match($response); $numeric = $num->get_normalised(); - return array($numeric, $num->get_postfix()); + return [$numeric, $num->get_postfix()]; } protected function feedback_for_post_prefix_parts($prefix, $postfix) { @@ -166,10 +166,10 @@ protected function feedback_for_post_prefix_parts($prefix, $postfix) { public function compute_final_grade($responses, $totaltries) { // Remove non numeric part of response to pass numeric part to parent class. - $numericresponses = array(); + $numericresponses = []; foreach ($responses as $responseno => $response) { list($numericpartofresponse, ) = $this->split_response_into_num_and_unit($response['answer']); - $numericresponses[] = array('answer' => $numericpartofresponse); + $numericresponses[] = ['answer' => $numericpartofresponse]; } $numerictotaltries = $totaltries; @@ -230,7 +230,7 @@ public function classify_response(array $response) { $unitclassifiedresponse = new question_classified_response($unit->unit, $unitpart, $unit->fraction); } - return array("unitpart" => $unitclassifiedresponse, - "numericpart" => $numericclassifiedresponse); + return ["unitpart" => $unitclassifiedresponse, + "numericpart" => $numericclassifiedresponse]; } } diff --git a/questiontype.php b/questiontype.php index 56dbe8b..7f6b853 100644 --- a/questiontype.php +++ b/questiontype.php @@ -64,7 +64,7 @@ public function db_table_prefix() { } public function extra_question_fields() { - return array($this->db_table_prefix(), 'randomseed', 'requirescinotation', 'unitfraction'); + return [$this->db_table_prefix(), 'randomseed', 'requirescinotation', 'unitfraction']; } protected function delete_files_in_units($questionid, $contextid) { @@ -72,7 +72,7 @@ protected function delete_files_in_units($questionid, $contextid) { $fs = get_file_storage(); $tablename = $this->db_table_prefix().'_units'; - $unitids = $DB->get_records_menu($tablename, array('questionid' => $questionid), 'id', 'id,1'); + $unitids = $DB->get_records_menu($tablename, ['questionid' => $questionid], 'id', 'id,1'); foreach ($unitids as $unitid => $notused) { $fs->delete_area_files($contextid, $this->db_table_prefix(), 'unitsfeedback', $unitid); } @@ -82,7 +82,7 @@ protected function move_files_in_units($questionid, $oldcontextid, $newcontextid global $DB; $fs = get_file_storage(); $tablename = $this->db_table_prefix().'_units'; - $unitids = $DB->get_records_menu($tablename, array('questionid' => $questionid), 'id', 'id,1'); + $unitids = $DB->get_records_menu($tablename, ['questionid' => $questionid], 'id', 'id,1'); foreach ($unitids as $unitid => $notused) { $fs->move_area_files_to_new_context($oldcontextid, $newcontextid, $this->db_table_prefix(), 'unitsfeedback', $unitid); @@ -92,7 +92,7 @@ protected function move_files_in_units($questionid, $oldcontextid, $newcontextid public function delete_question($questionid, $contextid) { global $DB; $tablename = $this->db_table_prefix().'_units'; - $DB->delete_records($tablename, array('questionid' => $questionid)); + $DB->delete_records($tablename, ['questionid' => $questionid]); parent::delete_question($questionid, $contextid); } @@ -100,9 +100,9 @@ public function save_units($formdata) { global $DB; $context = $formdata->context; $table = $this->db_table_prefix().'_units'; - $oldunits = $DB->get_records($table, array('questionid' => $formdata->id), 'id ASC'); + $oldunits = $DB->get_records($table, ['questionid' => $formdata->id], 'id ASC'); if (empty($oldunits)) { - $oldunits = array(); + $oldunits = []; } if (!empty($formdata->units)) { @@ -151,7 +151,7 @@ public function save_units($formdata) { foreach ($oldunits as $oldunit) { $fs->delete_area_files($context->id, $this->db_table_prefix(), 'unitsfeedback', $oldunit->id); $fs->delete_area_files($context->id, $this->db_table_prefix(), 'spacesfeedback', $oldunit->id); - $DB->delete_records($table, array('id' => $oldunit->id)); + $DB->delete_records($table, ['id' => $oldunit->id]); } } @@ -239,7 +239,7 @@ public function get_possible_responses($questiondata) { $numericresponses = $parentresponses[$questiondata->id]; $matchall = false; - $unitresponses = array(); + $unitresponses = []; foreach ($questiondata->options->units as $unitid => $unit) { if ('*' === $unit->unit) { $matchall = true; @@ -251,8 +251,8 @@ public function get_possible_responses($questiondata) { } $unitresponses[null] = question_possible_response::no_response(); - return array("unitpart" => $unitresponses, - "numericpart" => $numericresponses); + return ["unitpart" => $unitresponses, + "numericpart" => $numericresponses]; } public function get_random_guess_score($questiondata) { @@ -282,42 +282,42 @@ public function import_from_xml($data, $question, qformat_xml $format, $extra=nu $units = $data['#']['unit']; $unitno = 0; foreach ($units as $unit) { - $unitname = $format->getpath($unit, array('#', 'units', 0, '#'), '', true); + $unitname = $format->getpath($unit, ['#', 'units', 0, '#'], '', true); if ('*' !== $unitname) { $qo->units[$unitno] = $unitname; $qo->unitsfeedback[$unitno] = $this->import_html($format, $unit['#']['unitsfeedback'][0], $qo->questiontextformat); // Check for removespace if import from version 2014111200. - $removespace = $format->getpath($unit, array('#', 'removespace', 0, '#'), false); + $removespace = $format->getpath($unit, ['#', 'removespace', 0, '#'], false); if ($removespace !== false) { $qo->spaceinunit[$unitno] = $removespace; $qo->spacesfeedback[$unitno] = $this->import_html($format, '', FORMAT_HTML); } else { - $qo->spaceinunit[$unitno] = $format->getpath($unit, array('#', 'spaceinunit', 0, '#'), false); + $qo->spaceinunit[$unitno] = $format->getpath($unit, ['#', 'spaceinunit', 0, '#'], false); $qo->spacesfeedback[$unitno] = $this->import_html($format, $unit['#']['spacesfeedback'][0], $qo->questiontextformat); } - $qo->replacedash[$unitno] = $format->getpath($unit, array('#', 'replacedash', 0, '#'), false); - $qo->unitsfraction[$unitno] = $format->getpath($unit, array('#', 'unitsfraction', 0, '#'), 0, true); + $qo->replacedash[$unitno] = $format->getpath($unit, ['#', 'replacedash', 0, '#'], false); + $qo->unitsfraction[$unitno] = $format->getpath($unit, ['#', 'unitsfraction', 0, '#'], 0, true); $unitno++; } else { $qo->otherunitfeedback = $this->import_html($format, $unit['#']['unitsfeedback'][0], $qo->questiontextformat); } } if (!isset($qo->otherunitfeedback)) { - $qo->otherunitfeedback = array('text' => '', 'format' => $qo->questiontextformat); + $qo->otherunitfeedback = ['text' => '', 'format' => $qo->questiontextformat]; } } return $qo; } protected function import_html(qformat_xml $format, $data, $defaultformat) { - $text = array(); - $text['text'] = $format->getpath($data, array('#', 'text', 0, '#'), '', true); - $text['format'] = $format->trans_format($format->getpath($data, array('@', 'format'), + $text = []; + $text['text'] = $format->getpath($data, ['#', 'text', 0, '#'], '', true); + $text['format'] = $format->trans_format($format->getpath($data, ['@', 'format'], $format->get_format($defaultformat))); - $text['files'] = $format->import_files($format->getpath($data, array('#', 'file'), array(), false)); + $text['files'] = $format->import_files($format->getpath($data, ['#', 'file'], [], false)); return $text; } @@ -327,12 +327,12 @@ public function export_to_xml($question, qformat_xml $format, $extra=null) { $units = $question->options->units; foreach ($units as $unit) { $expout .= " \n"; - $fields = array( + $fields = [ 'units' => 'unit', 'unitsfraction' => 'fraction', 'spaceinunit' => 'spaceinunit', 'replacedash' => 'replacedash', - ); + ]; foreach ($fields as $xmlfield => $dbfield) { $exportedvalue = $format->xml_escape($unit->$dbfield); $expout .= " <$xmlfield>{$exportedvalue}\n"; diff --git a/renderer.php b/renderer.php index e9fd7da..87e7e7b 100644 --- a/renderer.php +++ b/renderer.php @@ -41,7 +41,7 @@ public function specific_feedback(question_attempt $qa) { $question = $qa->get_question(); - $unit = $question->get_matching_unit(array('answer' => $qa->get_last_qt_var('answer'))); + $unit = $question->get_matching_unit(['answer' => $qa->get_last_qt_var('answer')]); if (!$unit || !$unit->feedback) { return $parentfeedback.''; } diff --git a/tests/behat/preview.feature b/tests/behat/preview.feature index 4ee0230..d744702 100644 --- a/tests/behat/preview.feature +++ b/tests/behat/preview.feature @@ -29,7 +29,7 @@ Feature: Preview a Variable numeric set with units question | Marked out of | 3 | | Question variant | 1 | | Marks | Show mark and max | - And I press "Start again with these options" + And I press "id_saverestart" Then I should see "What is 2 m + 8 m?" And the state of "What is 2 m + 8 m?" question is shown as "Tries remaining: 3" When I set the field "Answer:" to "2" diff --git a/tests/helper.php b/tests/helper.php index a6386c3..b5b7d0f 100644 --- a/tests/helper.php +++ b/tests/helper.php @@ -16,6 +16,8 @@ use qtype_varnumunit\qtype_varnumunit_unit; +defined('MOODLE_INTERNAL') || die(); + global $CFG; require_once($CFG->dirroot . '/question/type/varnumunit/questiontype.php'); @@ -47,7 +49,7 @@ public function make_varnumunit_question_3_sig_figs_with_m_unit() { $vu->unitfraction = '0.2500000'; $vu->qtype = question_bank::get_qtype('varnumunit'); - $vu->answers = array(1 => new qtype_varnumericset_answer( + $vu->answers = [1 => new qtype_varnumericset_answer( '1', // Id. '12345', // Answer. '1', // Fraction. @@ -74,9 +76,9 @@ public function make_varnumunit_question_3_sig_figs_with_m_unit() { '0', // Checkscinotation. '0', // Checkpowerof10. '0', // Checkrounding. - '0')); // Checkscinotationformat. + '0')]; // Checkscinotationformat. - $vu->units = array( + $vu->units = [ 1 => new qtype_varnumunit_unit( '1', 'match(m)', @@ -96,7 +98,7 @@ public function make_varnumunit_question_3_sig_figs_with_m_unit() { 1, '0.0000000', '', - '1')); + '1')]; $calculatorname = $vu->qtype->calculator_name(); $vu->calculator = new $calculatorname(); $vu->calculator->evaluate_variant(0); @@ -118,7 +120,7 @@ public function make_varnumunit_question_3_sig_figs_with_units_meters_per_second $vu->usesupeditor = true; $vu->unitfraction = '0.1000000'; $vu->qtype = question_bank::get_qtype('varnumunit'); - $vu->answers = array(1 => new qtype_varnumericset_answer( + $vu->answers = [1 => new qtype_varnumericset_answer( '1', // Id. '4000', // Answer. '1', // Fraction. @@ -145,9 +147,9 @@ public function make_varnumunit_question_3_sig_figs_with_units_meters_per_second '0', // Checkscinotation. '0', // Checkpowerof10. '0', // Checkrounding. - '0')); // Checkscinotationformat. + '0')]; // Checkscinotationformat. - $vu->units = array( + $vu->units = [ 1 => new qtype_varnumunit_unit( '1', 'match(ms-1)', @@ -177,7 +179,7 @@ public function make_varnumunit_question_3_sig_figs_with_units_meters_per_second 1, '0.0000000', '', - '1')); + '1')]; $calculatorname = $vu->qtype->calculator_name(); $vu->calculator = new $calculatorname(); $vu->calculator->evaluate_variant(0); @@ -199,7 +201,7 @@ public function make_varnumunit_question_simple_1_m() { $vu->usesupeditor = false; $vu->unitfraction = '0.1000000'; $vu->qtype = question_bank::get_qtype('varnumunit'); - $vu->answers = array(1 => new qtype_varnumericset_answer( + $vu->answers = [1 => new qtype_varnumericset_answer( '1', // Id. '1', // Answer. '1', // Fraction. @@ -212,8 +214,8 @@ public function make_varnumunit_question_simple_1_m() { '0', // Checkscinotation. '0', // Checkpowerof10. '0', // Checkrounding. - '0')); // Checkscinotationformat. - $vu->units = array( + '0')]; // Checkscinotationformat. + $vu->units = [ 1 => new qtype_varnumunit_unit( '1', 'match(m)', @@ -234,7 +236,7 @@ public function make_varnumunit_question_simple_1_m() { '0.0000000', '

That is not the right unit.

', '1'), - ); + ]; $calculatorname = $vu->qtype->calculator_name(); $vu->calculator = new $calculatorname(); $vu->calculator->evaluate_variant(0); @@ -315,7 +317,7 @@ public function get_varnumunit_question_form_data_with_variables() { $form->fraction = ['0' => '1.0', '1' => '0.0', '2' => '0.0']; $form->feedback = [ '0' => ['format' => FORMAT_HTML, 'text' => 'Well done!'], - '1' => ['format' => FORMAT_HTML, 'text' => 'Sorry, no.'] + '1' => ['format' => FORMAT_HTML, 'text' => 'Sorry, no.'], ]; $form->otherunitfeedback = ['text' => '', 'format' => FORMAT_HTML]; $form->unit[0] = 'match(m)'; @@ -328,12 +330,12 @@ public function get_varnumunit_question_form_data_with_variables() { $form->spaceinunit[1] = 1; $form->replacedash[1] = true; $form->unitsfraction[1] = '0.0'; - $form->unitsfeedback[0] = ['text' => 'That is the right unit 2.', 'format' => FORMAT_HTML]; + $form->unitsfeedback[1] = ['text' => 'That is the right unit 2.', 'format' => FORMAT_HTML]; $form->spacingfeedback[1] = ['text' => '', 'format' => FORMAT_HTML]; $form->penalty = '0.3333333'; $form->hint = [ ['text' => 'Please try again.', 'format' => FORMAT_HTML], - ['text' => 'You may use a calculator if necessary.', 'format' => FORMAT_HTML] + ['text' => 'You may use a calculator if necessary.', 'format' => FORMAT_HTML], ]; return $form; diff --git a/tests/privacy_provider_test.php b/tests/privacy_provider_test.php index 2e3d5e1..b7eee9f 100644 --- a/tests/privacy_provider_test.php +++ b/tests/privacy_provider_test.php @@ -22,10 +22,10 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -namespace qtype_varnumunit\tests; +namespace qtype_varnumunit; -use qtype_varnumunit\privacy\provider; use core_privacy\local\request\writer; +use qtype_varnumunit\privacy\provider; defined('MOODLE_INTERNAL') || die(); @@ -35,20 +35,21 @@ /** * Privacy provider tests class. * - * @package qtype_varnumunit - * @copyright 2021 The Open university - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @package qtype_varnumunit + * @copyright 2021 The Open university + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @covers \qtype_varnumunit\privacy\provider */ -class qtype_varnumunit_privacy_provider_testcase extends \core_privacy\tests\provider_testcase { +class privacy_provider_test extends \core_privacy\tests\provider_testcase { // Include the privacy helper which has assertions on it. - public function test_get_metadata() { + public function test_get_metadata(): void { $collection = new \core_privacy\local\metadata\collection('qtype_varnumunit'); - $actual = \qtype_varnumunit\privacy\provider::get_metadata($collection); + $actual = provider::get_metadata($collection); $this->assertEquals($collection, $actual); } - public function test_export_user_preferences_no_pref() { + public function test_export_user_preferences_no_pref(): void { $this->resetAfterTest(); $user = $this->getDataGenerator()->create_user(); @@ -65,7 +66,7 @@ public function test_export_user_preferences_no_pref() { * @param string $value The value stored in the database * @param string $expected The expected transformed value */ - public function test_export_user_preferences($name, $value, $expected) { + public function test_export_user_preferences($name, $value, $expected): void { $this->resetAfterTest(); $user = $this->getDataGenerator()->create_user(); set_user_preference("qtype_varnumunit_$name", $value, $user); @@ -89,7 +90,7 @@ public function test_export_user_preferences($name, $value, $expected) { * * @return array Array of valid user preferences. */ - public function user_preference_provider() { + public static function user_preference_provider(): array { return [ 'default mark 1' => ['defaultmark', 1, 1], 'penalty 33.33333%' => ['penalty', 0.3333333, '33.33333%'], @@ -98,7 +99,7 @@ public function user_preference_provider() { 'unitfraction 0.1111111' => ['unitfraction', 0.1111111, 'Value : 88.88889%, Units : 11.11111%'], 'unitfraction 0.1250000' => ['unitfraction', 0.1250000, 'Value : 87.5%, Units : 12.5%'], 'unitfraction 0.1666667' => ['unitfraction', 0.1666667, 'Value : 83.33333%, Units : 16.66667%'], - 'unitfraction 0.2000000' => ['unitfraction', 0.2000000, 'Value : 80%, Units : 20%'] + 'unitfraction 0.2000000' => ['unitfraction', 0.2000000, 'Value : 80%, Units : 20%'], ]; } } diff --git a/tests/question_test.php b/tests/question_test.php index 7bf62bf..5243e74 100644 --- a/tests/question_test.php +++ b/tests/question_test.php @@ -14,15 +14,16 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * Unit tests for the varnumunit question definition class. - * - * @package qtype_varnumunit - * @copyright 2018 The Open University - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ +namespace qtype_varnumunit; -use qtype_varnumunit\qtype_varnumunit_unit; +use advanced_testcase; +use qtype_varnumericset_answer; +use qtype_varnumunit; +use qtype_varnumunit_question; +use question_attempt_step; +use question_classified_response; +use question_testcase; +use test_question_maker; defined('MOODLE_INTERNAL') || die(); global $CFG; @@ -34,23 +35,26 @@ /** * Unit tests for the varnumunit question definition class. * + * @package qtype_varnumunit * @copyright 2018 The Open University - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @covers \qtype_varnumunit_question + * @covers \qtype_varnumunit\qtype_varnumunit_unit */ -class qtype_varnumunit_question_test extends advanced_testcase { +class question_test extends advanced_testcase { /** * Provide data for testing test_question_responses. * * @return array */ - public function space_in_unit_question_providers() { + public static function space_in_unit_question_providers(): array { $data = []; $data[] = [ 'units' => [ - 'match(km)' => ['type' => qtype_varnumunit::SPACEINUNIT_REMOVE_ALL_SPACE, 'fraction' => 1] + 'match(km)' => ['type' => qtype_varnumunit::SPACEINUNIT_REMOVE_ALL_SPACE, 'fraction' => 1], ], 'answers' => [1], 'expects' => [ @@ -60,12 +64,12 @@ public function space_in_unit_question_providers() { '2 km' => ['grade' => 0.1, 'state' => 'question_state_gradedpartial'], '1 cm' => ['grade' => 0.9, 'state' => 'question_state_gradedpartial'], '2cm' => ['grade' => 0, 'state' => 'question_state_gradedwrong'], - '1 k m' => ['grade' => 1, 'state' => 'question_state_gradedright'] - ] + '1 k m' => ['grade' => 1, 'state' => 'question_state_gradedright'], + ], ]; $data[] = [ 'units' => [ - 'match(km)' => ['type' => qtype_varnumunit::SPACEINUNIT_PRESERVE_SPACE_REQUIRE, 'fraction' => 1] + 'match(km)' => ['type' => qtype_varnumunit::SPACEINUNIT_PRESERVE_SPACE_REQUIRE, 'fraction' => 1], ], 'answers' => [1], 'expects' => [ @@ -79,13 +83,13 @@ public function space_in_unit_question_providers() { '1 cm' => ['grade' => 0.9, 'state' => 'question_state_gradedpartial'], '2cm' => ['grade' => 0, 'state' => 'question_state_gradedwrong'], '1 k m' => ['grade' => 0.9, 'state' => 'question_state_gradedpartial'], - '2 k m' => ['grade' => 0, 'state' => 'question_state_gradedwrong'] - ] + '2 k m' => ['grade' => 0, 'state' => 'question_state_gradedwrong'], + ], ]; $data[] = [ 'units' => [ - 'match(km)' => ['type' => qtype_varnumunit::SPACEINUNIT_PRESERVE_SPACE_NOT_REQUIRE, 'fraction' => 1] + 'match(km)' => ['type' => qtype_varnumunit::SPACEINUNIT_PRESERVE_SPACE_NOT_REQUIRE, 'fraction' => 1], ], 'answers' => [1], 'expects' => [ @@ -96,15 +100,15 @@ public function space_in_unit_question_providers() { '1 cm' => ['grade' => 0.9, 'state' => 'question_state_gradedpartial'], '2cm' => ['grade' => 0, 'state' => 'question_state_gradedwrong'], '1 k m' => ['grade' => 0.9, 'state' => 'question_state_gradedpartial'], - '2 k m' => ['grade' => 0, 'state' => 'question_state_gradedwrong'] - ] + '2 k m' => ['grade' => 0, 'state' => 'question_state_gradedwrong'], + ], ]; $data[] = [ 'units' => [ 'match(cm)' => ['type' => qtype_varnumunit::SPACEINUNIT_REMOVE_ALL_SPACE, 'fraction' => 1], 'match(dm)' => ['type' => qtype_varnumunit::SPACEINUNIT_PRESERVE_SPACE_NOT_REQUIRE, 'fraction' => 0.75], - 'match(km)' => ['type' => qtype_varnumunit::SPACEINUNIT_PRESERVE_SPACE_REQUIRE, 'fraction' => 0.5] + 'match(km)' => ['type' => qtype_varnumunit::SPACEINUNIT_PRESERVE_SPACE_REQUIRE, 'fraction' => 0.5], ], 'answers' => [1], 'expects' => [ @@ -131,8 +135,8 @@ public function space_in_unit_question_providers() { '10 cm' => ['grade' => 0.1, 'state' => 'question_state_gradedpartial'], '10 dm' => ['grade' => 0.075, 'state' => 'question_state_gradedpartial'], '10 m' => ['grade' => 0, 'state' => 'question_state_gradedwrong'], - '10 km' => ['grade' => 0, 'state' => 'question_state_gradedwrong'] - ] + '10 km' => ['grade' => 0, 'state' => 'question_state_gradedwrong'], + ], ]; return $data; @@ -146,15 +150,15 @@ public function space_in_unit_question_providers() { * @param $units * @param $answers * @param $expects - * @throws coding_exception */ - public function test_question_responses($units, $answers, $expects) { + public function test_question_responses($units, $answers, $expects): void { $questionanswers = []; $questionunits = []; + /** @var qtype_varnumunit_question $question */ $question = test_question_maker::make_question('varnumunit', 'require_space_between_number_n_unit'); foreach ($answers as $answer) { - $questionanswers[] = new qtype_varnumericset_answer(1, $answer, '1', 'Correct', FORMAT_HTML, '0', '', 1, 0, 0, 0, 0, 0); + $questionanswers[] = new qtype_varnumericset_answer(1, $answer, 1, 'Correct', FORMAT_HTML, '0', '', 1, 0, 0, 0, 0, 0); } foreach ($units as $pattern => $params) { $questionunits[] = new qtype_varnumunit_unit(1, $pattern, $params['type'], '', 1, 1, @@ -177,7 +181,7 @@ public function test_question_responses($units, $answers, $expects) { * * @return array */ - public function question_classify_providers() { + public static function question_classify_providers(): array { $data = []; $data['correct_response'] = [ @@ -197,7 +201,7 @@ public function question_classify_providers() { 1 => new qtype_varnumericset_answer( 1, '1', - '1', + 1, 'Correct', FORMAT_HTML, '0', '', @@ -207,7 +211,7 @@ public function question_classify_providers() { 0, 0, 0), - ] + ], ], 'expects' => [ '1m' => [ @@ -217,8 +221,8 @@ public function question_classify_providers() { '' => [ 'unitpart' => question_classified_response::no_response(), 'numericpart' => question_classified_response::no_response(), - ] - ] + ], + ], ]; $data['correct_response_require_space_between_number_n_unit'] = [ 'qdata' => [ @@ -237,7 +241,7 @@ public function question_classify_providers() { 1 => new qtype_varnumericset_answer( 1, '1', - '1', + 1, 'Correct', FORMAT_HTML, '0', '', @@ -247,7 +251,7 @@ public function question_classify_providers() { 0, 0, 0), - ] + ], ], 'expects' => [ '1m' => [ @@ -265,8 +269,8 @@ public function question_classify_providers() { '' => [ 'unitpart' => question_classified_response::no_response(), 'numericpart' => question_classified_response::no_response(), - ] - ] + ], + ], ]; return $data; @@ -279,9 +283,8 @@ public function question_classify_providers() { * * @param array $qdata Question's data. * @param array $expects Expected responses. - * @throws coding_exception */ - public function test_classify_response_correct_response($qdata, $expects) { + public function test_classify_response_correct_response($qdata, $expects): void { foreach ($expects as $answer => $expect) { // Add units and answers to question. diff --git a/tests/questiontype_test.php b/tests/questiontype_test.php index e79b433..596e25b 100644 --- a/tests/questiontype_test.php +++ b/tests/questiontype_test.php @@ -14,14 +14,14 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * Unit tests for the varnumunit question type class. - * - * @package qtype_varnumunit - * @copyright 2012 The Open University - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ +namespace qtype_varnumunit; +use qformat_xml; +use qtype_varnumunit; +use question_check_specified_fields_expectation; +use question_possible_response; +use question_testcase; +use stdClass; defined('MOODLE_INTERNAL') || die(); global $CFG; @@ -32,15 +32,12 @@ /** * Unit tests for the varnumunit question type class. * + * @package qtype_varnumunit * @copyright 2012 The Open University * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @group qtype_varnumunit + * @covers \qtype_varnumunit */ -class qtype_varnumunit_test extends question_testcase { - public static $includecoverage = array( - 'question/type/questiontype.php', - 'question/type/varnumunit/questiontype.php', - ); +class questiontype_test extends question_testcase { protected $qtype; @@ -48,42 +45,42 @@ protected function setUp(): void { $this->qtype = new qtype_varnumunit(); } - protected function get_test_question_data() { + protected function get_test_question_data(): stdClass { $q = new stdClass(); $q->id = 1; $q->options = new stdClass(); - $q->options->answers[1] = (object) array('answer' => '1.5', 'fraction' => 1); - $q->options->answers[2] = (object) array('answer' => '*', 'fraction' => 0.1); + $q->options->answers[1] = (object) ['answer' => '1.5', 'fraction' => 1]; + $q->options->answers[2] = (object) ['answer' => '*', 'fraction' => 0.1]; $q->options->unitfraction = 0.25; - $q->options->units[1] = (object) array('unit' => 'match(frogs)', 'fraction' => 1); - $q->options->units[2] = (object) array('unit' => '*', 'fraction' => 0.1); + $q->options->units[1] = (object) ['unit' => 'match(frogs)', 'fraction' => 1]; + $q->options->units[2] = (object) ['unit' => '*', 'fraction' => 0.1]; return $q; } - public function test_get_random_guess_score() { + public function test_get_random_guess_score(): void { $q = $this->get_test_question_data(); $this->assertEqualsWithDelta(0.075, $this->qtype->get_random_guess_score($q), question_testcase::GRADE_DELTA); } - public function test_get_possible_responses() { + public function test_get_possible_responses(): void { $q = $this->get_test_question_data(); - $this->assertEquals(array( - 'unitpart' => array( + $this->assertEquals([ + 'unitpart' => [ 'match(frogs)' => new question_possible_response('match(frogs)', 1), '*' => new question_possible_response('*', 0.1), null => question_possible_response::no_response(), - ), - 'numericpart' => array( + ], + 'numericpart' => [ 1 => new question_possible_response('1.5', 1), 2 => new question_possible_response('*', 0.1), null => question_possible_response::no_response(), - ), - ), $this->qtype->get_possible_responses($q)); + ], + ], $this->qtype->get_possible_responses($q)); } - public function test_xml_import() { + public function test_xml_import(): void { $xml = ' Imported variable numeric set with units question @@ -209,7 +206,7 @@ public function test_xml_import() { $importer = new qformat_xml(); $qtype = new qtype_varnumunit(); - $q = $qtype->import_from_xml($xmldata['question'], null, $importer, null); + $q = $qtype->import_from_xml($xmldata['question'], null, $importer); $expectedq = new stdClass(); $expectedq->qtype = 'varnumunit'; @@ -235,7 +232,7 @@ public function test_xml_import() { $expectedq->answer = [ 0 => 'c', 1 => '*']; $expectedq->feedback = [ ['text' => '

Well done!

', 'format' => FORMAT_HTML], - ['text' => '

Sorry, no.

', 'format' => FORMAT_HTML] + ['text' => '

Sorry, no.

', 'format' => FORMAT_HTML], ]; $expectedq->sigfigs = [false, false]; $expectedq->error = [false, false]; @@ -250,7 +247,7 @@ public function test_xml_import() { $expectedq->varname = [ 'a', 'b', - 'c = a + b' + 'c = a + b', ]; $expectedq->vartype = [1, 1, 0]; $expectedq->variant0 = [2, 8, 10]; @@ -261,38 +258,38 @@ public function test_xml_import() { [ 'text' => '

You are required to put a space between the number and the unit.

', 'format' => FORMAT_HTML, - 'files' => [] - ] + 'files' => [], + ], ]; $expectedq->unitsfeedback = [ [ 'text' => '

That is the right unit.

', 'format' => FORMAT_HTML, - 'files' => [] - ] + 'files' => [], + ], ]; $expectedq->otherunitfeedback = [ 'text' => '

That is the wrong unit.

', 'format' => FORMAT_HTML, - 'files' => [] + 'files' => [], ]; $expectedq->units = ['match(m)']; $expectedq->hint = [ 0 => [ 'text' => '

Please try again.

', - 'format' => FORMAT_HTML + 'format' => FORMAT_HTML, ], 1 => [ 'text' => '

You may use a calculator if necessary.

', - 'format' => FORMAT_HTML + 'format' => FORMAT_HTML, - ] + ], ]; $this->assertEquals($expectedq->answer, $q->answer); $this->assert(new question_check_specified_fields_expectation($expectedq), $q); } - public function test_xml_import_legacy() { + public function test_xml_import_legacy(): void { $xml = ' Imported variable numeric set with units question @@ -412,7 +409,7 @@ public function test_xml_import_legacy() { $importer = new qformat_xml(); $qtype = new qtype_varnumunit(); - $q = $qtype->import_from_xml($xmldata['question'], null, $importer, null); + $q = $qtype->import_from_xml($xmldata['question'], null, $importer); $expectedq = new stdClass(); $expectedq->qtype = 'varnumunit'; @@ -438,7 +435,7 @@ public function test_xml_import_legacy() { $expectedq->answer = [ 0 => 'c', 1 => '*']; $expectedq->feedback = [ ['text' => '

Well done!

', 'format' => FORMAT_HTML], - ['text' => '

Sorry, no.

', 'format' => FORMAT_HTML] + ['text' => '

Sorry, no.

', 'format' => FORMAT_HTML], ]; $expectedq->sigfigs = [false, false]; $expectedq->error = [false, false]; @@ -453,7 +450,7 @@ public function test_xml_import_legacy() { $expectedq->varname = [ 'a', 'b', - 'c = a + b' + 'c = a + b', ]; $expectedq->vartype = [1, 1, 0]; $expectedq->variant0 = [2, 8, 10]; @@ -464,32 +461,32 @@ public function test_xml_import_legacy() { [ 'text' => false, 'format' => FORMAT_HTML, - 'files' => [] - ] + 'files' => [], + ], ]; $expectedq->unitsfeedback = [ [ 'text' => '

That is the right unit.

', 'format' => FORMAT_HTML, - 'files' => [] - ] + 'files' => [], + ], ]; $expectedq->otherunitfeedback = [ 'text' => '

That is the wrong unit.

', 'format' => FORMAT_HTML, - 'files' => [] + 'files' => [], ]; $expectedq->units = ['match(m)']; $expectedq->hint = [ 0 => [ 'text' => '

Please try again.

', - 'format' => FORMAT_HTML + 'format' => FORMAT_HTML, ], 1 => [ 'text' => '

You may use a calculator if necessary.

', - 'format' => FORMAT_HTML + 'format' => FORMAT_HTML, - ] + ], ]; $this->assertEquals($expectedq->answer, $q->answer); $this->assert(new question_check_specified_fields_expectation($expectedq), $q); diff --git a/tests/walkthrough_test.php b/tests/walkthrough_test.php index 2265762..b0339e1 100644 --- a/tests/walkthrough_test.php +++ b/tests/walkthrough_test.php @@ -14,14 +14,15 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -/** - * This file contains overall tests of varnumunit questions. - * - * @package qtype_varnumunit - * @copyright 2011 The Open University - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ +namespace qtype_varnumunit; +use qbehaviour_walkthrough_test_base; +use question_hint; +use question_hint_with_parts; +use question_pattern_expectation; +use question_state; +use stdClass; +use test_question_maker; defined('MOODLE_INTERNAL') || die(); @@ -33,25 +34,26 @@ /** * Walk through Unit tests for varnumunit questions. * + * @package qtype_varnumunit * @copyright 2011 The Open University * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * @group qtype_varnumunit + * @covers \qtype_varnumunit_question */ -class qtype_varnumunit_walkthrough_test extends qbehaviour_walkthrough_test_base { +class walkthrough_test extends qbehaviour_walkthrough_test_base { protected function setUp(): void { global $PAGE; parent::setUp(); $PAGE->set_url('/'); } - public function test_validation_and_interactive_with_m_unit_submission_with_no_unit() { + public function test_validation_and_interactive_with_m_unit_submission_with_no_unit(): void { // Create a varnumunit question. $q = test_question_maker::make_question('varnumunit', '3_sig_figs_with_m_unit'); - $q->hints = array( + $q->hints = [ new question_hint(1, 'This is the first hint.', FORMAT_HTML), new question_hint(2, 'This is the second hint.', FORMAT_HTML), - ); + ]; $this->start_attempt_at_question($q, 'interactive', 100); // Check the initial state. @@ -67,7 +69,7 @@ public function test_validation_and_interactive_with_m_unit_submission_with_no_u $this->get_no_hint_visible_expectation()); // Submit blank. - $this->process_submission(array('-submit' => 1, 'answer' => '')); + $this->process_submission(['-submit' => 1, 'answer' => '']); $this->check_current_state(question_state::$invalid); $this->check_current_mark(null); @@ -82,7 +84,7 @@ public function test_validation_and_interactive_with_m_unit_submission_with_no_u $this->get_no_hint_visible_expectation()); // Submit something that does not look like a number. - $this->process_submission(array('-submit' => 1, 'answer' => 'newt')); + $this->process_submission(['-submit' => 1, 'answer' => 'newt']); $this->check_current_state(question_state::$invalid); $this->check_current_mark(null); $this->check_current_output( @@ -96,7 +98,7 @@ public function test_validation_and_interactive_with_m_unit_submission_with_no_u $this->get_no_hint_visible_expectation()); // Submit without unit. - $this->process_submission(array('-submit' => 1, 'answer' => '12300')); + $this->process_submission(['-submit' => 1, 'answer' => '12300']); $this->check_current_state(question_state::$todo); $this->check_current_mark(null); @@ -111,7 +113,7 @@ public function test_validation_and_interactive_with_m_unit_submission_with_no_u $this->quba->get_response_summary($this->slot)); // Submit all and finish without unit. - $this->process_submission(array('-finish' => 1)); + $this->process_submission(['-finish' => 1]); $this->check_current_state(question_state::$gradedpartial); $this->check_current_mark(75); @@ -126,14 +128,14 @@ public function test_validation_and_interactive_with_m_unit_submission_with_no_u $this->quba->get_response_summary($this->slot)); } - public function test_validation_and_interactive_with_m_unit_submission_with_no_unit_then_with_unit() { + public function test_validation_and_interactive_with_m_unit_submission_with_no_unit_then_with_unit(): void { // Create a varnumunit question. $q = test_question_maker::make_question('varnumunit', '3_sig_figs_with_m_unit'); - $q->hints = array( + $q->hints = [ new question_hint(1, 'This is the first hint.', FORMAT_HTML), new question_hint(2, 'This is the second hint.', FORMAT_HTML), - ); + ]; $this->start_attempt_at_question($q, 'interactive', 100); // Check the initial state. @@ -149,7 +151,7 @@ public function test_validation_and_interactive_with_m_unit_submission_with_no_u $this->get_no_hint_visible_expectation()); // Submit without unit. - $this->process_submission(array('-submit' => 1, 'answer' => '12300')); + $this->process_submission(['-submit' => 1, 'answer' => '12300']); $this->check_current_state(question_state::$todo); $this->check_current_mark(null); @@ -164,7 +166,7 @@ public function test_validation_and_interactive_with_m_unit_submission_with_no_u $this->quba->get_response_summary($this->slot)); // Do try again. - $this->process_submission(array('-tryagain' => 1)); + $this->process_submission(['-tryagain' => 1]); $this->check_current_state(question_state::$todo); $this->check_current_mark(null); @@ -179,7 +181,7 @@ public function test_validation_and_interactive_with_m_unit_submission_with_no_u $this->check_current_mark(null); // Now get it right. - $this->process_submission(array('-submit' => 1, 'answer' => '12300m')); + $this->process_submission(['-submit' => 1, 'answer' => '12300m']); $this->check_current_state(question_state::$gradedright); $this->check_current_mark(91.6666675); @@ -194,14 +196,14 @@ public function test_validation_and_interactive_with_m_unit_submission_with_no_u $this->quba->get_response_summary($this->slot)); } - public function test_validation_and_interactive_with_m_unit_submission_with_wrong_unit_and_partially_correct_number() { + public function test_validation_and_interactive_with_m_unit_submission_with_wrong_unit_and_partially_correct_number(): void { // Create a varnumunit question. $q = test_question_maker::make_question('varnumunit', '3_sig_figs_with_m_unit'); - $q->hints = array( + $q->hints = [ new question_hint_with_parts(1, 'This is the first hint.', FORMAT_HTML, '0', '1'), new question_hint_with_parts(2, 'This is the second hint.', FORMAT_HTML, '0', '1'), - ); + ]; $this->start_attempt_at_question($q, 'interactive', 100); // Check the initial state. @@ -217,7 +219,7 @@ public function test_validation_and_interactive_with_m_unit_submission_with_wron $this->get_no_hint_visible_expectation()); // Submit with wrong unit and number off by factor of 10. - $this->process_submission(array('-submit' => 1, 'answer' => '12.300s')); + $this->process_submission(['-submit' => 1, 'answer' => '12.300s']); $this->check_current_state(question_state::$todo); $this->check_current_mark(null); @@ -232,7 +234,7 @@ public function test_validation_and_interactive_with_m_unit_submission_with_wron $this->quba->get_response_summary($this->slot)); // Do try again. - $this->process_submission(array('-tryagain' => 1)); + $this->process_submission(['-tryagain' => 1]); $this->check_current_state(question_state::$todo); $this->check_current_mark(null); @@ -247,7 +249,7 @@ public function test_validation_and_interactive_with_m_unit_submission_with_wron $this->check_current_mark(null); // Now get it right. - $this->process_submission(array('-submit' => 1, 'answer' => '12300m')); + $this->process_submission(['-submit' => 1, 'answer' => '12300m']); $this->check_current_state(question_state::$gradedright); $this->check_current_mark(84.1666675); @@ -262,14 +264,14 @@ public function test_validation_and_interactive_with_m_unit_submission_with_wron $this->quba->get_response_summary($this->slot)); } - public function test_validation_and_interactive_with_simple_1_m_question() { + public function test_validation_and_interactive_with_simple_1_m_question(): void { // Create a varnumunit question. $q = test_question_maker::make_question('varnumunit', 'simple_1_m'); - $q->hints = array( + $q->hints = [ new question_hint_with_parts(1, 'This is the first hint.', FORMAT_HTML, '0', '1'), new question_hint_with_parts(2, 'This is the second hint.', FORMAT_HTML, '0', '1'), - ); + ]; $this->start_attempt_at_question($q, 'interactive', 100); // Check the initial state. @@ -285,7 +287,7 @@ public function test_validation_and_interactive_with_simple_1_m_question() { $this->get_no_hint_visible_expectation()); // Submit with wrong unit. - $this->process_submission(array('-submit' => 1, 'answer' => '1 x')); + $this->process_submission(['-submit' => 1, 'answer' => '1 x']); $this->check_current_state(question_state::$todo); $this->check_current_mark(null); @@ -300,7 +302,7 @@ public function test_validation_and_interactive_with_simple_1_m_question() { $this->quba->get_response_summary($this->slot)); // Do try again. - $this->process_submission(array('-tryagain' => 1)); + $this->process_submission(['-tryagain' => 1]); $this->check_current_state(question_state::$todo); $this->check_current_mark(null); @@ -316,7 +318,7 @@ public function test_validation_and_interactive_with_simple_1_m_question() { $this->check_current_mark(null); // Now get it right. - $this->process_submission(array('-submit' => 1, 'answer' => '1m')); + $this->process_submission(['-submit' => 1, 'answer' => '1m']); $this->check_current_state(question_state::$gradedright); $this->check_current_mark(98); @@ -331,7 +333,7 @@ public function test_validation_and_interactive_with_simple_1_m_question() { $this->quba->get_response_summary($this->slot)); } - public function test_deferred_feedback_with_wrong_unit_and_wrong_number() { + public function test_deferred_feedback_with_wrong_unit_and_wrong_number(): void { // Create a varnumunit question. $q = test_question_maker::make_question('varnumunit', '3_sig_figs_with_units_meters_per_second'); @@ -349,7 +351,7 @@ public function test_deferred_feedback_with_wrong_unit_and_wrong_number() { $this->get_no_hint_visible_expectation()); // Save with wrong unit and number. - $this->process_submission(array('answer' => '5e3 ms ')); + $this->process_submission(['answer' => '5e3 ms ']); $this->check_current_state(question_state::$complete); $this->check_current_mark(null); @@ -362,7 +364,7 @@ public function test_deferred_feedback_with_wrong_unit_and_wrong_number() { $this->assertEquals(null, $this->quba->get_response_summary($this->slot)); // Finish. - $this->process_submission(array('-finish' => 1)); + $this->process_submission(['-finish' => 1]); $this->check_current_state(question_state::$gradedwrong); $this->check_current_mark(0); @@ -378,7 +380,7 @@ public function test_deferred_feedback_with_wrong_unit_and_wrong_number() { $this->assertEquals(get_string('summarise_response', 'qtype_varnumunit', $a), $this->quba->get_response_summary($this->slot)); } - public function test_deferred_feedback_with_wrong_unit_but_correct_number() { + public function test_deferred_feedback_with_wrong_unit_but_correct_number(): void { // Create a varnumunit question. $q = test_question_maker::make_question('varnumunit', '3_sig_figs_with_units_meters_per_second'); @@ -396,7 +398,7 @@ public function test_deferred_feedback_with_wrong_unit_but_correct_number() { $this->get_no_hint_visible_expectation()); // Save with wrong unit and number. - $this->process_submission(array('answer' => '4.000e3 g')); + $this->process_submission(['answer' => '4.000e3 g']); $this->check_current_state(question_state::$complete); $this->check_current_mark(null); @@ -409,7 +411,7 @@ public function test_deferred_feedback_with_wrong_unit_but_correct_number() { $this->assertEquals(null, $this->quba->get_response_summary($this->slot)); // Finish. - $this->process_submission(array('-finish' => 1)); + $this->process_submission(['-finish' => 1]); $this->check_current_state(question_state::$gradedpartial); $this->check_current_mark(90); @@ -426,7 +428,7 @@ public function test_deferred_feedback_with_wrong_unit_but_correct_number() { $this->quba->get_response_summary($this->slot)); } - public function test_deferred_feedback_with_correct_unit_but_wrong_number() { + public function test_deferred_feedback_with_correct_unit_but_wrong_number(): void { // Create a varnumunit question. $q = test_question_maker::make_question('varnumunit', '3_sig_figs_with_units_meters_per_second'); @@ -443,7 +445,7 @@ public function test_deferred_feedback_with_correct_unit_but_wrong_number() { $this->get_no_hint_visible_expectation()); // Save with wrong unit and number. - $this->process_submission(array('answer' => '5.000e3 m/s')); + $this->process_submission(['answer' => '5.000e3 m/s']); $this->check_current_state(question_state::$complete); $this->check_current_mark(null); @@ -456,7 +458,7 @@ public function test_deferred_feedback_with_correct_unit_but_wrong_number() { $this->assertEquals(null, $this->quba->get_response_summary($this->slot)); // Finish. - $this->process_submission(array('-finish' => 1)); + $this->process_submission(['-finish' => 1]); $this->check_current_state(question_state::$gradedpartial); $this->check_current_mark(10); @@ -473,7 +475,7 @@ public function test_deferred_feedback_with_correct_unit_but_wrong_number() { $this->quba->get_response_summary($this->slot)); } - public function test_deferred_feedback_with_correct_unit_and_correct_number() { + public function test_deferred_feedback_with_correct_unit_and_correct_number(): void { // Create a varnumunit question. $q = test_question_maker::make_question('varnumunit', '3_sig_figs_with_units_meters_per_second'); @@ -490,7 +492,7 @@ public function test_deferred_feedback_with_correct_unit_and_correct_number() { $this->get_no_hint_visible_expectation()); // Save with wrong unit and number. - $this->process_submission(array('answer' => '4.000e3 ms-1')); + $this->process_submission(['answer' => '4.000e3 ms-1']); $this->check_current_state(question_state::$complete); $this->check_current_mark(null); @@ -503,7 +505,7 @@ public function test_deferred_feedback_with_correct_unit_and_correct_number() { $this->assertEquals(null, $this->quba->get_response_summary($this->slot)); // Finish. - $this->process_submission(array('-finish' => 1)); + $this->process_submission(['-finish' => 1]); $this->check_current_state(question_state::$gradedright); $this->check_current_mark(100); diff --git a/version.php b/version.php index 06b6895..845efa1 100644 --- a/version.php +++ b/version.php @@ -30,9 +30,9 @@ $plugin->maturity = MATURITY_STABLE; $plugin->release = '1.8 for Moodle 3.11+'; -$plugin->dependencies = array( +$plugin->dependencies = [ 'qtype_varnumericset' => 2019110900, 'qtype_pmatch' => 2019091000, -); +]; $plugin->outestssufficient = true;