From 002ed2d8670a450101e4dbd3f554157c570dbf4f Mon Sep 17 00:00:00 2001 From: Julien Boulen Date: Fri, 10 Feb 2023 11:07:38 +0100 Subject: [PATCH] MDL-76314 forum: add form check to prevent combining wrong parameters Form validation prevents combining the forum type "single discussion" and the group mode "separate groups". --- mod/forum/lang/en/forum.php | 2 ++ mod/forum/mod_form.php | 5 +++++ mod/forum/tests/behat/add_forum.feature | 15 +++++++++++++++ 3 files changed, 22 insertions(+) diff --git a/mod/forum/lang/en/forum.php b/mod/forum/lang/en/forum.php index 248d30907085c..388758cb59b56 100644 --- a/mod/forum/lang/en/forum.php +++ b/mod/forum/lang/en/forum.php @@ -95,6 +95,8 @@ $string['cannottrack'] = 'Could not stop tracking that forum'; $string['cannotunsubscribe'] = 'Could not unsubscribe you from that forum'; $string['cannotupdatepost'] = 'You can not update this post'; +$string['cannotuseseperategroupsandsingletopic'] = 'Separate groups cannot be used with single discussion topic'; +$string['cannotusesingletopicandseperategroups'] = 'Single discussion topic cannot be used with separate groups'; $string['cannotviewpostyet'] = 'You can\'t read other students\' posts in this discussion yet because you haven\'t posted.'; $string['cannotviewusersposts'] = 'There are no posts made by this user that you are able to view.'; $string['cleanreadtime'] = 'Mark old posts as read hour'; diff --git a/mod/forum/mod_form.php b/mod/forum/mod_form.php index 65cd77a75ea23..3717484184828 100644 --- a/mod/forum/mod_form.php +++ b/mod/forum/mod_form.php @@ -341,6 +341,11 @@ function definition_after_data() { public function validation($data, $files) { $errors = parent::validation($data, $files); + if ($data['type'] === 'single' && $data['groupmode'] == SEPARATEGROUPS) { + $errors['type'] = get_string('cannotusesingletopicandseperategroups', 'forum'); + $errors['groupmode'] = get_string('cannotuseseperategroupsandsingletopic', 'forum'); + } + if ($data['duedate'] && $data['cutoffdate']) { if ($data['duedate'] > $data['cutoffdate']) { $errors['cutoffdate'] = get_string('cutoffdatevalidation', 'forum'); diff --git a/mod/forum/tests/behat/add_forum.feature b/mod/forum/tests/behat/add_forum.feature index f3cfed05696bd..885a29335ca10 100644 --- a/mod/forum/tests/behat/add_forum.feature +++ b/mod/forum/tests/behat/add_forum.feature @@ -44,3 +44,18 @@ Feature: Add forum activities and discussions And I should see "empty.txt" And I follow "Edit" And the field "Attachment" matches value "empty.txt" + + @javascript + Scenario: Test forum settings validation + Given the following "courses" exist: + | fullname | shortname | category | + | Course 1 | C1 | 0 | + And I log in as "admin" + And I am on "Course 1" course homepage with editing mode on + And I add a "Forum" to section "0" and I fill the form with: + | Forum name | Test forum | + | Forum type | single | + | Group mode | 1 | + When I press "Save and display" + Then I should see "Separate groups cannot be used with single discussion topic" + And I should see "Single discussion topic cannot be used with separate groups"