From ff4230d88ecd24d1f68e1efec3d221fbc8cdfe67 Mon Sep 17 00:00:00 2001 From: Jake Dallimore Date: Wed, 2 Nov 2016 15:11:22 +0800 Subject: [PATCH] MDL-54751 behat: Support for adhoc module and section deletion in behat Introduced new behat step for running all pending adhoc tasks and modified the relavant behat tests. --- .../tests/behat/backup_user_data.feature | 1 + .../tests/behat/basic_functionality.feature | 2 + backup/util/loggers/base_logger.class.php | 3 +- .../tests/behat/structural_changes.feature | 1 + lib/tests/behat/behat_general.php | 37 +++++++++++++++++++ 5 files changed, 43 insertions(+), 1 deletion(-) diff --git a/admin/tool/recyclebin/tests/behat/backup_user_data.feature b/admin/tool/recyclebin/tests/behat/backup_user_data.feature index 500a2730b2f28..c0bc961a89ed6 100644 --- a/admin/tool/recyclebin/tests/behat/backup_user_data.feature +++ b/admin/tool/recyclebin/tests/behat/backup_user_data.feature @@ -58,6 +58,7 @@ Feature: Backup user data And I follow "Course 1" And I turn editing mode on And I delete "Quiz 1" activity + And I run all adhoc tasks And I navigate to "Recycle bin" node in "Course administration" And I should see "Quiz 1" And I click on "Restore" "link" in the "region-main" "region" diff --git a/admin/tool/recyclebin/tests/behat/basic_functionality.feature b/admin/tool/recyclebin/tests/behat/basic_functionality.feature index ded5e99f349b6..b10af91f9e454 100644 --- a/admin/tool/recyclebin/tests/behat/basic_functionality.feature +++ b/admin/tool/recyclebin/tests/behat/basic_functionality.feature @@ -69,6 +69,7 @@ Feature: Basic recycle bin functionality | Assignment name | Test assign | | Description | Test | And I delete "Test assign" activity + And I run all adhoc tasks And I navigate to "Recycle bin" node in "Course administration" When I click on "Delete" "link" Then I should see "Are you sure you want to delete the selected item from the recycle bin?" @@ -92,6 +93,7 @@ Feature: Basic recycle bin functionality | Description | Test 2 | And I delete "Test assign 1" activity And I delete "Test assign 2" activity + And I run all adhoc tasks And I navigate to "Recycle bin" node in "Course administration" And I should see "Test assign 1" And I should see "Test assign 2" diff --git a/backup/util/loggers/base_logger.class.php b/backup/util/loggers/base_logger.class.php index 32b0c06c9394f..26bb05cec4afe 100644 --- a/backup/util/loggers/base_logger.class.php +++ b/backup/util/loggers/base_logger.class.php @@ -114,7 +114,8 @@ abstract protected function action($message, $level, $options = null); // To imp public final function process($message, $level, $options = null) { $result = true; - if ($this->level != backup::LOG_NONE && $this->level >= $level) { // Perform action conditionally + if ($this->level != backup::LOG_NONE && $this->level >= $level + && !(defined('BEHAT_TEST') && BEHAT_TEST)) { // Perform action conditionally. $result = $this->action($message, $level, $options); } if ($result === false) { // Something was wrong, stop the chain diff --git a/blocks/recent_activity/tests/behat/structural_changes.feature b/blocks/recent_activity/tests/behat/structural_changes.feature index d66978a4be685..5914281d57b21 100644 --- a/blocks/recent_activity/tests/behat/structural_changes.feature +++ b/blocks/recent_activity/tests/behat/structural_changes.feature @@ -194,6 +194,7 @@ Feature: View structural changes in recent activity block And I follow "Course 1" And I turn editing mode on And I delete "ForumUpdated" activity + And I run all adhoc tasks And I log out And I wait "1" seconds # Students 1 and 2 see that forum was deleted diff --git a/lib/tests/behat/behat_general.php b/lib/tests/behat/behat_general.php index edc4cca71a10c..a6be6f4a3b88e 100644 --- a/lib/tests/behat/behat_general.php +++ b/lib/tests/behat/behat_general.php @@ -1001,6 +1001,43 @@ public function i_run_the_scheduled_task($taskname) { } } + /** + * Runs all ad-hoc tasks in the queue. + * + * This is faster and more reliable than running cron (running cron won't + * work more than once in the same test, for instance). However it is + * a little less 'realistic'. + * + * While the task is running, we suppress mtrace output because it makes + * the Behat result look ugly. + * + * @Given /^I run all adhoc tasks$/ + * @throws DriverException + */ + public function i_run_all_adhoc_tasks() { + // Do setup for cron task. + cron_setup_user(); + + // Run tasks. Locking is handled by get_next_adhoc_task. + $now = time(); + ob_start(); // Discard task output as not appropriate for Behat output! + while (($task = \core\task\manager::get_next_adhoc_task($now)) !== null) { + + try { + $task->execute(); + + // Mark task complete. + \core\task\manager::adhoc_task_complete($task); + } catch (Exception $e) { + // Mark task failed and throw exception. + \core\task\manager::adhoc_task_failed($task); + ob_end_clean(); + throw new DriverException('An adhoc task failed', 0, $e); + } + } + ob_end_clean(); + } + /** * Checks that an element and selector type exists in another element and selector type on the current page. *