diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 90b9dea5f03..c9d9458284f 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -30,6 +30,10 @@ uncomment and alter to fetch external test files from alternative location--> + + + + diff --git a/public/lib/tests/classes/phpunit/data_provider_finished_subscriber.php b/public/lib/tests/classes/phpunit/data_provider_finished_subscriber.php new file mode 100644 index 00000000000..162f11e5e05 --- /dev/null +++ b/public/lib/tests/classes/phpunit/data_provider_finished_subscriber.php @@ -0,0 +1,116 @@ +. + +namespace core\tests\phpunit; + +use PHPUnit\Event\Test\DataProviderMethodFinished; +use PHPUnit\Event\Test\DataProviderMethodFinishedSubscriber; + +/** + * PHPUnit Event Subscriber for DataProviderMethodFinished event. + * + * @package core + * @copyright Andrew Lyons + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +final class data_provider_finished_subscriber implements DataProviderMethodFinishedSubscriber { + /** @var ?int The number of DB writes */ + private static ?int $dbwrites = null; + + #[\Override] + public function notify(DataProviderMethodFinished $event): void { + $resetall = false; + + // Note: All of these checks must be extremely lightweight. + // This method is called after every single Data Provider. + if ($this->is_page_set()) { + $this->trigger_notice($event, "has set the theme"); + $resetall = true; + } + + if ($this->is_db_written()) { + $this->trigger_notice($event, "has written to the database"); + $resetall = true; + } + + if ($resetall) { + \phpunit_util::reset_all_data(); + $this->update_db_writes(); + } + } + + /** + * Check whether the page/theme has been initialised. + * + * Data Providers should not rely on the page/theme being set. + * + * @return bool + */ + private function is_page_set(): bool { + global $PAGE; + + return (new \ReflectionProperty(\moodle_page::class, '_theme'))->getValue($PAGE) !== null; + } + + /** + * Check whether the database has been written to. + * + * Data Providers should not rely on the database being written to. + * + * @return bool + */ + private function is_db_written(): bool { + global $DB; + + if (!$DB) { + // DB not initialised yet. + return false; + } + + if (self::$dbwrites === null) { + self::$dbwrites = $DB->perf_get_writes(); + } + + return $DB->perf_get_writes() > self::$dbwrites; + } + + /** + * Update the database write count. + */ + private function update_db_writes(): void { + global $DB; + + self::$dbwrites = $DB->perf_get_writes(); + } + + /** + * Trigger a warning on the CLI. + * + * Note: PHPUnit does not let us actually emit a notice or warning for the DataProviderFinished event. + * + * @param DataProviderMethodFinished $event + * @param string $message + */ + private function trigger_notice(DataProviderMethodFinished $event, string $message): void { + printf( + "Warning: Data provider for %s::%s %s%s", + $event->testMethod()->className(), + $event->testMethod()->methodName(), + $message, + PHP_EOL, + ); + } +} diff --git a/public/lib/tests/classes/phpunit/moodle_extension.php b/public/lib/tests/classes/phpunit/moodle_extension.php new file mode 100644 index 00000000000..98f8b36ed7b --- /dev/null +++ b/public/lib/tests/classes/phpunit/moodle_extension.php @@ -0,0 +1,44 @@ +. + +namespace core\tests\phpunit; + +use PHPUnit\Runner\Extension\Extension; +use PHPUnit\Runner\Extension\Facade; +use PHPUnit\Runner\Extension\ParameterCollection; +use PHPUnit\TextUI\Configuration\Configuration; + +/** + * Moodle PHPUnit Extension Registration. + * + * @package core + * @copyright Andrew Lyons + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +final class moodle_extension implements Extension { + #[\Override] + public function bootstrap( + Configuration $configuration, + Facade $facade, + ParameterCollection $parameters + ): void { + if ($configuration->noOutput()) { + return; + } + + $facade->registerSubscriber(new data_provider_finished_subscriber()); + } +} diff --git a/public/question/type/ordering/tests/output/formulation_and_controls_test.php b/public/question/type/ordering/tests/output/formulation_and_controls_test.php index 6cb1dd2fc46..87f5e218e37 100644 --- a/public/question/type/ordering/tests/output/formulation_and_controls_test.php +++ b/public/question/type/ordering/tests/output/formulation_and_controls_test.php @@ -26,6 +26,7 @@ defined('MOODLE_INTERNAL') || die(); global $CFG; require_once($CFG->dirroot . '/question/engine/tests/helpers.php'); +require_once($CFG->dirroot . '/question/type/ordering/question.php'); /** * A test class used to test formulation_and_controls. @@ -49,7 +50,22 @@ final class formulation_and_controls_test extends advanced_testcase { * @return void */ public function test_export_for_template(array $answeritems, int $gradingtype, string $layouttype, array $expected): void { - global $PAGE; + global $OUTPUT, $PAGE; + + $correct = $OUTPUT->pix_icon('i/grade_correct', get_string('correct', 'question')); + $partiallycorrect = $OUTPUT->pix_icon('i/grade_partiallycorrect', get_string('partiallycorrect', 'question')); + $incorrect = $OUTPUT->pix_icon('i/grade_incorrect', get_string('incorrect', 'question')); + + foreach ($expected['answers'] as $key => $answer) { + if (array_key_exists('feedbackimage', $expected['answers'][$key])) { + $expected['answers'][$key]['feedbackimage'] = match ($expected['answers'][$key]['feedbackimage']) { + 'correct' => $correct, + 'partiallycorrect' => $partiallycorrect, + 'incorrect' => $incorrect, + default => null + }; + } + } $question = test_question_maker::make_question('ordering'); $question->layouttype = $layouttype === 'horizontal' ? qtype_ordering_question::LAYOUT_HORIZONTAL : @@ -93,13 +109,6 @@ final class formulation_and_controls_test extends advanced_testcase { * @return array */ public static function export_for_template_provider(): array { - global $CFG, $OUTPUT; - require_once($CFG->dirroot . '/question/type/ordering/question.php'); - - $correct = $OUTPUT->pix_icon('i/grade_correct', get_string('correct', 'question')); - $partiallycorrect = $OUTPUT->pix_icon('i/grade_partiallycorrect', get_string('partiallycorrect', 'question')); - $incorrect = $OUTPUT->pix_icon('i/grade_incorrect', get_string('incorrect', 'question')); - return [ 'Horizontal, correct and partially correct' => [ [13 => 'Modular', 14 => 'Object', 15 => 'Oriented', 17 => 'Learning', 16 => 'Dynamic', 18 => 'Environment'], @@ -127,37 +136,37 @@ final class formulation_and_controls_test extends advanced_testcase { 'scoreclass' => 'correct', 'id' => 'ordering_item_' . md5('Modular'), 'answertext' => "Modular", - 'feedbackimage' => $correct, + 'feedbackimage' => 'correct', ], [ 'scoreclass' => 'correct', 'id' => 'ordering_item_' . md5('Object'), 'answertext' => "Object", - 'feedbackimage' => $correct, + 'feedbackimage' => 'correct', ], [ 'scoreclass' => 'correct', 'id' => 'ordering_item_' . md5('Oriented'), 'answertext' => "Oriented", - 'feedbackimage' => $correct, + 'feedbackimage' => 'correct', ], [ 'scoreclass' => 'partial66', 'id' => 'ordering_item_' . md5('Learning'), 'answertext' => "Learning", - 'feedbackimage' => $partiallycorrect, + 'feedbackimage' => 'partiallycorrect', ], [ 'scoreclass' => 'partial66', 'id' => 'ordering_item_' . md5('Dynamic'), 'answertext' => "Dynamic", - 'feedbackimage' => $partiallycorrect, + 'feedbackimage' => 'partiallycorrect', ], [ 'scoreclass' => 'correct', 'id' => 'ordering_item_' . md5('Environment'), 'answertext' => "Environment", - 'feedbackimage' => $correct, + 'feedbackimage' => 'correct', ], ], ], @@ -188,37 +197,37 @@ final class formulation_and_controls_test extends advanced_testcase { 'scoreclass' => 'incorrect', 'id' => 'ordering_item_' . md5('Object'), 'answertext' => "Object", - 'feedbackimage' => $incorrect, + 'feedbackimage' => 'incorrect', ], [ 'scoreclass' => 'incorrect', 'id' => 'ordering_item_' . md5('Dynamic'), 'answertext' => "Dynamic", - 'feedbackimage' => $incorrect, + 'feedbackimage' => 'incorrect', ], [ 'scoreclass' => 'incorrect', 'id' => 'ordering_item_' . md5('Modular'), 'answertext' => "Modular", - 'feedbackimage' => $incorrect, + 'feedbackimage' => 'incorrect', ], [ 'scoreclass' => 'incorrect', 'id' => 'ordering_item_' . md5('Learning'), 'answertext' => "Learning", - 'feedbackimage' => $incorrect, + 'feedbackimage' => 'incorrect', ], [ 'scoreclass' => 'incorrect', 'id' => 'ordering_item_' . md5('Environment'), 'answertext' => "Environment", - 'feedbackimage' => $incorrect, + 'feedbackimage' => 'incorrect', ], [ 'scoreclass' => 'incorrect', 'id' => 'ordering_item_' . md5('Oriented'), 'answertext' => "Oriented", - 'feedbackimage' => $incorrect, + 'feedbackimage' => 'incorrect', ], ], ], @@ -249,37 +258,37 @@ final class formulation_and_controls_test extends advanced_testcase { 'scoreclass' => 'correct', 'id' => 'ordering_item_' . md5('Modular'), 'answertext' => "Modular", - 'feedbackimage' => $correct, + 'feedbackimage' => 'correct', ], [ 'scoreclass' => 'correct', 'id' => 'ordering_item_' . md5('Object'), 'answertext' => "Object", - 'feedbackimage' => $correct, + 'feedbackimage' => 'correct', ], [ 'scoreclass' => 'correct', 'id' => 'ordering_item_' . md5('Oriented'), 'answertext' => "Oriented", - 'feedbackimage' => $correct, + 'feedbackimage' => 'correct', ], [ 'scoreclass' => 'correct', 'id' => 'ordering_item_' . md5('Dynamic'), 'answertext' => "Dynamic", - 'feedbackimage' => $correct, + 'feedbackimage' => 'correct', ], [ 'scoreclass' => 'correct', 'id' => 'ordering_item_' . md5('Learning'), 'answertext' => "Learning", - 'feedbackimage' => $correct, + 'feedbackimage' => 'correct', ], [ 'scoreclass' => 'correct', 'id' => 'ordering_item_' . md5('Environment'), 'answertext' => "Environment", - 'feedbackimage' => $correct, + 'feedbackimage' => 'correct', ], ], ],