From 87f196af93e71cff6e0d5a518fc9ebafa015a378 Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Wed, 3 Apr 2019 17:09:05 +0200 Subject: [PATCH] MDL-65204 phpunit: more isEqual() strict string fixes This is a followup of 85f47ba, where we were relaxing the (new since phpunit 7.x) strict (===) isEqual() comparison for strings. Copying the explanations for easier understanding. Link: https://github.com/sebastianbergmann/phpunit/issues/3185 Solution here is one of: a) Return to the previous situation, making the comparison softer. That can achieved by forcing different types, so float == string works. b) Changing APIs (both forms and database return strings) to perform some conversion to floats. That would make float comparison (with floats or strings) to work too. The patch here follows the a) approach. Changing all the internals for proper float handling sounds excesive when it has been working perfectly since ever. So we went the easier route, just getting rid of the new === comparisons when needed by changing expectation types to float. --- lib/dml/tests/dml_test.php | 2 +- .../tests/datalib_reporting_queries_test.php | 16 ++++++++-------- question/type/calculatedsimple/tests/helper.php | 4 ++-- question/type/numerical/tests/helper.php | 12 ++++++------ question/type/truefalse/tests/helper.php | 4 ++-- 5 files changed, 19 insertions(+), 19 deletions(-) diff --git a/lib/dml/tests/dml_test.php b/lib/dml/tests/dml_test.php index fb144e9c736..a87c36239af 100644 --- a/lib/dml/tests/dml_test.php +++ b/lib/dml/tests/dml_test.php @@ -2321,7 +2321,7 @@ class core_dml_testcase extends database_driver_testcase { $record->id = '1'; $record->course = '1'; $record->oneint = null; - $record->onenum = '1.00'; + $record->onenum = 1.0; $record->onechar = 'a'; $record->onetext = 'aaa'; diff --git a/question/engine/tests/datalib_reporting_queries_test.php b/question/engine/tests/datalib_reporting_queries_test.php index 8bbe0c81340..06ccce4e651 100644 --- a/question/engine/tests/datalib_reporting_queries_test.php +++ b/question/engine/tests/datalib_reporting_queries_test.php @@ -146,7 +146,7 @@ class question_engine_data_mapper_reporting_testcase extends qbehaviour_walkthro $state = $states[$this->usageids[0]][$this->allslots[0]]; $this->assertEquals((object) array( 'questionid' => $this->sa->id, - 'maxmark' => '5.0000000', + 'maxmark' => 5.0, 'sequencenumber' => 2, 'state' => (string) question_state::$gradedright, ), $state); @@ -154,7 +154,7 @@ class question_engine_data_mapper_reporting_testcase extends qbehaviour_walkthro $state = $states[$this->usageids[0]][$this->allslots[1]]; $this->assertEquals((object) array( 'questionid' => $this->essay->id, - 'maxmark' => '10.0000000', + 'maxmark' => 10.0, 'sequencenumber' => 2, 'state' => (string) question_state::$needsgrading, ), $state); @@ -162,7 +162,7 @@ class question_engine_data_mapper_reporting_testcase extends qbehaviour_walkthro $state = $states[$this->usageids[1]][$this->allslots[0]]; $this->assertEquals((object) array( 'questionid' => $this->sa->id, - 'maxmark' => '5.0000000', + 'maxmark' => 5.0, 'sequencenumber' => 2, 'state' => (string) question_state::$gradedwrong, ), $state); @@ -170,7 +170,7 @@ class question_engine_data_mapper_reporting_testcase extends qbehaviour_walkthro $state = $states[$this->usageids[1]][$this->allslots[1]]; $this->assertEquals((object) array( 'questionid' => $this->essay->id, - 'maxmark' => '10.0000000', + 'maxmark' => 10.0, 'sequencenumber' => 1, 'state' => (string) question_state::$gaveup, ), $state); @@ -295,7 +295,7 @@ class question_engine_data_mapper_reporting_testcase extends qbehaviour_walkthro $state = $states[$this->usageids[0]][$this->allslots[0]]; $this->assertEquals((object) array( 'questionid' => $this->sa->id, - 'maxmark' => '5.0000000', + 'maxmark' => 5.0, 'sequencenumber' => 2, 'state' => (string) question_state::$gradedright, ), $state); @@ -303,7 +303,7 @@ class question_engine_data_mapper_reporting_testcase extends qbehaviour_walkthro $state = $states[$this->usageids[0]][$this->allslots[1]]; $this->assertEquals((object) array( 'questionid' => $this->essay->id, - 'maxmark' => '10.0000000', + 'maxmark' => 10.0, 'sequencenumber' => 2, 'state' => (string) question_state::$needsgrading, ), $state); @@ -311,7 +311,7 @@ class question_engine_data_mapper_reporting_testcase extends qbehaviour_walkthro $state = $states[$this->usageids[1]][$this->allslots[0]]; $this->assertEquals((object) array( 'questionid' => $this->sa->id, - 'maxmark' => '5.0000000', + 'maxmark' => 5.0, 'sequencenumber' => 2, 'state' => (string) question_state::$gradedwrong, ), $state); @@ -319,7 +319,7 @@ class question_engine_data_mapper_reporting_testcase extends qbehaviour_walkthro $state = $states[$this->usageids[1]][$this->allslots[1]]; $this->assertEquals((object) array( 'questionid' => $this->essay->id, - 'maxmark' => '10.0000000', + 'maxmark' => 10.0, 'sequencenumber' => 1, 'state' => (string) question_state::$gaveup, ), $state); diff --git a/question/type/calculatedsimple/tests/helper.php b/question/type/calculatedsimple/tests/helper.php index 5218994a2ae..2be6acd6401 100644 --- a/question/type/calculatedsimple/tests/helper.php +++ b/question/type/calculatedsimple/tests/helper.php @@ -240,7 +240,7 @@ class qtype_calculatedsimple_test_helper extends question_test_helper { $q->options->answers[0]->question = '3379'; $q->options->answers[0]->answer = '{a} + {b}'; $q->options->answers[0]->answerformat = '0'; - $q->options->answers[0]->fraction = '1.0000000'; + $q->options->answers[0]->fraction = 1.0; $q->options->answers[0]->feedback = ''; $q->options->answers[0]->feedbackformat = '1'; $q->options->answers[0]->tolerance = '0.01'; @@ -251,7 +251,7 @@ class qtype_calculatedsimple_test_helper extends question_test_helper { $q->options->units = array(); $q->options->unitgradingtype = '0'; - $q->options->unitpenalty = '0.1000000'; + $q->options->unitpenalty = 0.1; $q->options->showunits = '3'; $q->options->unitsleft = '0'; diff --git a/question/type/numerical/tests/helper.php b/question/type/numerical/tests/helper.php index 650674b423c..c039098dd61 100644 --- a/question/type/numerical/tests/helper.php +++ b/question/type/numerical/tests/helper.php @@ -199,21 +199,21 @@ class qtype_numerical_test_helper extends question_test_helper { $q->options->answers = array(); $q->options->answers[0] = new stdClass(); $q->options->answers[0]->answer = '3.14'; - $q->options->answers[0]->fraction = '1.0000000'; + $q->options->answers[0]->fraction = 1.0; $q->options->answers[0]->feedback = 'Very good.'; $q->options->answers[0]->feedbackformat = FORMAT_HTML; $q->options->answers[0]->tolerance = '0'; $q->options->answers[1] = new stdClass(); $q->options->answers[1]->answer = '3.142'; - $q->options->answers[1]->fraction = '0.0000000'; + $q->options->answers[1]->fraction = 0.0; $q->options->answers[1]->feedback = 'Too accurate.'; $q->options->answers[1]->feedbackformat = FORMAT_HTML; $q->options->answers[1]->tolerance = '0'; $q->options->answers[2] = new stdClass(); $q->options->answers[2]->answer = '3.1'; - $q->options->answers[2]->fraction = '0.0000000'; + $q->options->answers[2]->fraction = 0.0; $q->options->answers[2]->feedback = 'Not accurate enough.'; $q->options->answers[2]->feedbackformat = FORMAT_HTML; $q->options->answers[2]->tolerance = '0'; @@ -221,7 +221,7 @@ class qtype_numerical_test_helper extends question_test_helper { $q->options->answers[3] = new stdClass(); $q->options->answers[3]->answer = '3'; $q->options->answers[3]->answerformat = '0'; - $q->options->answers[3]->fraction = '0.0000000'; + $q->options->answers[3]->fraction = 0.0; $q->options->answers[3]->feedback = 'Not accurate enough.'; $q->options->answers[3]->feedbackformat = FORMAT_HTML; $q->options->answers[3]->tolerance = '0'; @@ -229,7 +229,7 @@ class qtype_numerical_test_helper extends question_test_helper { $q->options->answers[4] = new stdClass(); $q->options->answers[4]->answer = '*'; $q->options->answers[4]->answerformat = '0'; - $q->options->answers[4]->fraction = '0.0000000'; + $q->options->answers[4]->fraction = 0.0; $q->options->answers[4]->feedback = 'Completely wrong.'; $q->options->answers[4]->feedbackformat = FORMAT_HTML; $q->options->answers[4]->tolerance = '0'; @@ -237,7 +237,7 @@ class qtype_numerical_test_helper extends question_test_helper { $q->options->units = array(); $q->options->unitgradingtype = '0'; - $q->options->unitpenalty = '0.1000000'; + $q->options->unitpenalty = 0.1; $q->options->showunits = '3'; $q->options->unitsleft = '0'; diff --git a/question/type/truefalse/tests/helper.php b/question/type/truefalse/tests/helper.php index 359ade0b9ee..78850acb938 100644 --- a/question/type/truefalse/tests/helper.php +++ b/question/type/truefalse/tests/helper.php @@ -111,13 +111,13 @@ class qtype_truefalse_test_helper extends question_test_helper { $q->options->answers = array(); $q->options->answers[0] = new stdClass(); $q->options->answers[0]->answer = 'True'; - $q->options->answers[0]->fraction = '1.0000000'; + $q->options->answers[0]->fraction = 1.0; $q->options->answers[0]->feedback = 'This is the right answer.'; $q->options->answers[0]->feedbackformat = FORMAT_HTML; $q->options->answers[1] = new stdClass(); $q->options->answers[1]->answer = 'False'; - $q->options->answers[1]->fraction = '0.0000000'; + $q->options->answers[1]->fraction = 0.0; $q->options->answers[1]->feedback = 'This is the wrong answer.'; $q->options->answers[1]->feedbackformat = FORMAT_HTML;