Merge branch 'MDL-86432-main' of https://github.com/timhunt/moodle

This commit is contained in:
Mihail Geshoski
2025-09-25 14:13:46 +08:00
4 changed files with 199 additions and 26 deletions
+4
View File
@@ -30,6 +30,10 @@
uncomment and alter to fetch external test files from alternative location-->
</php>
<extensions>
<bootstrap class="core\tests\phpunit\moodle_extension"/>
</extensions>
<!--All core suites need to be manually added here-->
<testsuites>
@@ -0,0 +1,116 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
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 <[email protected]>
* @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,
);
}
}
@@ -0,0 +1,44 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
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 <[email protected]>
* @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());
}
}
@@ -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',
],
],
],