From 20aceab916c32b0e1bf9c226583f395b62ec820a Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Tue, 20 Feb 2024 11:45:00 +0800 Subject: [PATCH 1/1] NOCOMMIT: Test scenario for MDL-80862 --- lib/classes/task/fail.php | 33 ++++++++++++++++++++ lib/classes/task/pass.php | 34 ++++++++++++++++++++ lib/tests/behat/behat_tasks.php | 55 +++++++++++++++++++++++++++++++++ lib/tests/behat/task.feature | 20 ++++++++++++ 4 files changed, 142 insertions(+) create mode 100644 lib/classes/task/fail.php create mode 100644 lib/classes/task/pass.php create mode 100644 lib/tests/behat/behat_tasks.php create mode 100644 lib/tests/behat/task.feature diff --git a/lib/classes/task/fail.php b/lib/classes/task/fail.php new file mode 100644 index 0000000000..a57451c756 --- /dev/null +++ b/lib/classes/task/fail.php @@ -0,0 +1,33 @@ +. + +namespace core\task; + +/** + * Task designd to fail. + * + * @package core + * @copyright 2024 Andrew Lyons + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class fail extends adhoc_task { + /** + * Run the task to clean up deleted search are data. + */ + public function execute() { + throw new \coding_exception('Oops, I did it again!'); + } +} diff --git a/lib/classes/task/pass.php b/lib/classes/task/pass.php new file mode 100644 index 0000000000..d2441b8071 --- /dev/null +++ b/lib/classes/task/pass.php @@ -0,0 +1,34 @@ +. + +namespace core\task; + +/** + * Task designd to fail. + * + * @package core + * @copyright 2024 Andrew Lyons + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class pass extends adhoc_task { + /** + * Run the task to clean up deleted search are data. + */ + public function execute() { + mtrace("I am a passing task."); + set_config('pass', 'yes', 'core_task'); + } +} diff --git a/lib/tests/behat/behat_tasks.php b/lib/tests/behat/behat_tasks.php new file mode 100644 index 0000000000..55b1963e82 --- /dev/null +++ b/lib/tests/behat/behat_tasks.php @@ -0,0 +1,55 @@ +. + +// NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php. + +require_once(__DIR__ . '/../../behat/behat_base.php'); + +/** + * Behat tasks test. + * + * @package core + * @category test + * @copyright 2024 Andrew Lyons + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class behat_tasks extends behat_base { + /** + * @Given I queue a task with class name :taskname + * @param string $taskname + */ + public function queue_task(string $taskname): void { + if (!class_exists($taskname)) { + throw new \coding_exception("Unable to find a task named {$taskname}"); + } + $task = new $taskname(); + \core\task\manager::queue_adhoc_task($task); + } + + /** + * @Then the task should not have set the magic config value + */ + public function it_did_not_run(): void { + assert(get_config('core_task', 'pass') !== 'yes'); + } + + /** + * @Then the task should have set the magic config value + */ + public function it_ran(): void { + assert(get_config('core_task', 'pass') === 'yes'); + } +} diff --git a/lib/tests/behat/task.feature b/lib/tests/behat/task.feature new file mode 100644 index 0000000000..7300c987c7 --- /dev/null +++ b/lib/tests/behat/task.feature @@ -0,0 +1,20 @@ +@core +Feature: View timezone defaults + In order to run tests properly + I need to be able to run adhoc tasks + After a previous test fails running a task + + Scenario: Run a failing task + Given I queue a task with class name "\core\task\fail" + And I queue a task with class name "\core\task\fail" + And I queue a task with class name "\core\task\fail" + And I queue a task with class name "\core\task\fail" + And I queue a task with class name "\core\task\fail" + And I run all adhoc tasks + Then I should see "Task failed: \core\task\fail" + + Scenario: Run a passing task + Given I queue a task with class name "\core\task\pass" + And the task should not have set the magic config value + When I run all adhoc tasks + Then the task should have set the magic config value -- 2.42.0