Skip to content

Commit

Permalink
undeclared class variables were throwing errors with PHP 8.2
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusgreen committed Apr 29, 2024
1 parent add1c8f commit 6ffabb7
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 21 deletions.
1 change: 1 addition & 0 deletions question.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class qtype_gapfill_question extends question_graded_automatically_with_countbac
*/
public $answer;


/**
* Display draggables after the body of the question
* @var boolean
Expand Down
6 changes: 3 additions & 3 deletions tests/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@
*/
class qtype_gapfill_test_helper extends question_test_helper {


/**
* must be implemented or class made abstract
*
* @return string
*/
public function get_test_questions() {
return array('catmat');
return ['catmat'];
}

/**
Expand Down Expand Up @@ -218,7 +219,7 @@ public static function make_question($questiontext = "The [cat] sat on the [mat]
'qtype' => 'gapfill',
'length' => '1',
'stamp' => 'tjh238.vledev.open.ac.uk+100708154547+JrHygi',
'version' => 'tjh238.vledev.open.ac.uk+100708154548+a3zh8v',
'version' => 1,
'hidden' => '0',
'timecreated' => '1278603947',
'timemodified' => '1278603947',
Expand All @@ -231,7 +232,6 @@ public static function make_question($questiontext = "The [cat] sat on the [mat]
];

$question = $question->qtype->make_question($questiondata);
$question->gapstofill = count($answerwords);
return $question;
}

Expand Down
10 changes: 10 additions & 0 deletions tests/question_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@
* @coversDefaultClass \question\type\gapfill\question
*/
class question_test extends \advanced_testcase {

/**
*
* @var qtype_gapfill $gapfill
* An instance of the question type
*/
public $qtype;


/**
* Test value returned by questionid_column_name()
*
Expand Down Expand Up @@ -255,6 +264,7 @@ public function test_is_same_response() {
}
public function setUp(): void {
$this->qtype = \question_bank::get_qtype('gapfill');

}

protected function tearDown(): void {
Expand Down
28 changes: 18 additions & 10 deletions tests/questiontype_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,21 @@
class questiontype_test extends \advanced_testcase {

/**
* explained here https://docs.moodle.org/dev/Unit_test_API
*
* @var qtype_gapfill $gapfill
*
* An instance of the question type
*/
public $qtype;

/**
* Explained here https://docs.moodle.org/dev/Unit_test_API
* @var array
*/
protected static $includecoverage = array(
protected static $includecoverage = [
'question/type/questiontypebase.php',
'question/type/gapfill/questiontype.php',
);
];

protected function setUp(): void {
$this->qtype = new \qtype_gapfill();
Expand Down Expand Up @@ -94,7 +102,7 @@ protected function get_test_question_data() {
*
* @covers ::save_question
*/
public function test_save_question() {
public function test_save_question() : void {
$this->resetAfterTest();
global $DB;
$syscontext = \context_system::instance();
Expand Down Expand Up @@ -128,7 +136,7 @@ public function test_save_question() {
*
* @covers ::name()
*/
public function test_name() {
public function test_name() :void {
$this->assertEquals($this->qtype->name(), 'gapfill');
}

Expand All @@ -141,7 +149,7 @@ public function test_name() {
*
* @covers ::can_analyse_responses()
*/
public function test_can_analyse_responses() {
public function test_can_analyse_responses() :void {
$this->assertFalse($this->qtype->can_analyse_responses());
}

Expand All @@ -150,7 +158,7 @@ public function test_can_analyse_responses() {
*
* @covers ::questionid_column_name()
*/
public function test_questionid_column_name() {
public function test_questionid_column_name() : void {
$this->assertEquals($this->qtype->questionid_column_name(), 'question');
}

Expand All @@ -159,9 +167,9 @@ public function test_questionid_column_name() {
*
* @covers ::extra_question_fields()
*/
public function test_extra_question_fields() {
$extraquestionfields = array('question_gapfill', 'answerdisplay', 'delimitchars',
'casesensitive', 'noduplicates', 'disableregex', 'fixedgapsize', 'optionsaftertext', 'letterhints', 'singleuse');
public function test_extra_question_fields() :void {
$extraquestionfields = ['question_gapfill', 'answerdisplay', 'delimitchars',
'casesensitive', 'noduplicates', 'disableregex', 'fixedgapsize', 'optionsaftertext', 'letterhints', 'singleuse'];
$this->assertEquals($this->qtype->extra_question_fields(), $extraquestionfields);
}
}
23 changes: 15 additions & 8 deletions tests/walkthrough_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,8 @@ public function test_disableregex() {
'disableregex' => 1,
];
$gapfill = helper::make_question($questiontext, $options);
$this->start_attempt_at_question($gapfill, 'interactive', $gapfill->gapstofill);
$gapstofill = count($gapfill->answers);
$this->start_attempt_at_question($gapfill, 'interactive', $gapstofill);

$this->check_current_state(\question_state::$todo);
$this->check_current_mark(null);
Expand Down Expand Up @@ -467,8 +468,9 @@ public function test_interactive_discard_duplicates() {
$submission = array('-submit' => 1, 'p1' => 'gold', 'p2' => 'silver', 'p3' => 'silver');

$gapfill = helper::make_question( $questiontext, $options);
$gapstofill = count($gapfill->answers);

$this->start_attempt_at_question($gapfill, 'interactive', $gapfill->gapstofill);
$this->start_attempt_at_question($gapfill, 'interactive', $gapstofill);

// Check the initial state.
$this->check_current_state(\question_state::$todo);
Expand Down Expand Up @@ -498,7 +500,9 @@ public function test_interactive_discard_duplicates() {
public function test_no_duplicate_draggables() {
$qtext = 'Bicycles have [wheels]. Cars have [wheels|engines].';
$gapfill = helper::make_question($qtext);
$this->start_attempt_at_question($gapfill, 'interactive', $gapfill->gapstofill);
$gapstofill = count($gapfill->answers);

$this->start_attempt_at_question($gapfill, 'interactive', $gapstofill);
// Confirm draggables are unique, i.e. wheels appears only once.
$this->assertEquals(2, count($gapfill->allanswers));
}
Expand All @@ -509,11 +513,13 @@ public function test_no_duplicate_draggables() {
*/
public function test_get_letter_hints() {
$gapfill = helper::make_question();
$gapstofill = count($gapfill->answers);

$gapfill->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($gapfill, 'interactive', $gapfill->gapstofill);
$this->start_attempt_at_question($gapfill, 'interactive', $gapstofill);
$this->process_submission([ '-submit' => 1, 'p1' => 'cat', 'p2' => 'cat']);
$this->process_submission(array('-tryagain' => 1));
$qa = $this->quba->get_question_attempt($this->slot);
Expand Down Expand Up @@ -549,8 +555,9 @@ public function test_interactive_grade_for_blank() {
[one] sat on the [two] [!!] ';

$gapfill = helper::make_question( $questiontext);
$gapstofill = count($gapfill->answers);

$this->start_attempt_at_question($gapfill, 'interactive', $gapfill->gapstofill);
$this->start_attempt_at_question($gapfill, 'interactive', $gapstofill);

/* answer with duplicate values, only one of each duplicate should get a mark */
/* save answer */
Expand Down Expand Up @@ -579,7 +586,7 @@ public function test_interactive_grade_for_blank() {
$this->check_current_state(\question_state::$gradedright);

/* start again but put a value in the field expecting a blank */
$this->start_attempt_at_question($gapfill, 'interactive', $gapfill->gapstofill);
$this->start_attempt_at_question($gapfill, 'interactive', $gapstofill);
$submission = array('p1' => 'one', 'p2' => 'two', 'p3' => "three");
$this->process_submission($submission);
$this->check_step_count(2);
Expand Down Expand Up @@ -669,8 +676,9 @@ public function test_deferred_grade_for_blank() {
[one] sat on the [two] [!!] ';

$gapfill = helper::make_question( $questiontext);
$gapstofill = count($gapfill->answers);

$this->start_attempt_at_question($gapfill, 'deferredfeedback', $gapfill->gapstofill);
$this->start_attempt_at_question($gapfill, 'deferredfeedback', $gapstofill);
/* A mark for a blank submission where the gap is [!!] */
$submission = array('p1' => 'one', 'p2' => 'two', 'p3' => '');

Expand Down Expand Up @@ -704,7 +712,6 @@ public function test_immediatefeedback_with_correct() {
$gapfill = helper::make_question();
$maxmark = 2;

$gapfill->showanswers = true;
$this->start_attempt_at_question($gapfill, 'immediatefeedback', $maxmark);

// Check the initial state.
Expand Down

0 comments on commit 6ffabb7

Please sign in to comment.