diff --git a/mod/forum/classes/post_form.php b/mod/forum/classes/post_form.php
index 523b79c1b9c..6d53e6aceb0 100644
--- a/mod/forum/classes/post_form.php
+++ b/mod/forum/classes/post_form.php
@@ -159,31 +159,67 @@ class mod_forum_post_form extends moodleform {
             $mform->setConstants(array('timestart'=> 0, 'timeend'=>0));
         }
 
-        if ($groupmode = groups_get_activity_groupmode($cm, $course)) { // hack alert
+        if ($groupmode = groups_get_activity_groupmode($cm, $course)) {
             $groupdata = groups_get_activity_allowed_groups($cm);
-            $groupcount = count($groupdata);
-            $groupinfo = array();
-            $modulecontext = context_module::instance($cm->id);
 
-            // Check whether the user has access to all groups in this forum from the accessallgroups cap.
-            if ($groupmode == VISIBLEGROUPS || has_capability('moodle/site:accessallgroups', $modulecontext)) {
-                // Only allow posting to all groups if the user has access to all groups.
-                $groupinfo = array('0' => get_string('allparticipants'));
+            $groupinfo = array();
+            foreach ($groupdata as $groupid => $group) {
+                // Check whether this user can post in this group.
+                // We must make this check because all groups are returned for a visible grouped activity.
+                if (forum_user_can_post_discussion($forum, $groupid, null, $cm, $modcontext)) {
+                    // Build the data for the groupinfo select.
+                    $groupinfo[$groupid] = $group->name;
+                } else {
+                    unset($groupdata[$groupid]);
+                }
+            }
+            $groupcount = count($groupinfo);
+
+            // Check whether a user can post to all of their own groups.
+
+            // Posts to all of my groups are copied to each group that the user is a member of. Certain conditions must be met.
+            // 1) It only makes sense to allow this when a user is in more than one group.
+            // Note: This check must come before we consider adding accessallgroups, because that is not a real group.
+            $canposttoowngroups = empty($post->edit) && $groupcount > 1;
+
+            // 2) Important: You can *only* post to multiple groups for a top level post. Never any reply.
+            $canposttoowngroups = $canposttoowngroups && empty($post->parent);
+
+            // 3) You also need the canposttoowngroups capability.
+            $canposttoowngroups = $canposttoowngroups && has_capability('mod/forum:canposttomygroups', $modcontext);
+            if ($canposttoowngroups) {
+                // This user is in multiple groups, and can post to all of their own groups.
+                // Note: This is not the same as accessallgroups. This option will copy a post to all groups that a
+                // user is a member of.
+                $mform->addElement('checkbox', 'posttomygroups', get_string('posttomygroups', 'forum'));
+                $mform->addHelpButton('posttomygroups', 'posttomygroups', 'forum');
+                $mform->disabledIf('groupinfo', 'posttomygroups', 'checked');
+            }
+
+            // Check whether this user can post to all groups.
+            // Posts to the 'All participants' group go to all groups, not to each group in a list.
+            // It makes sense to allow this, even if there currently aren't any groups because there may be in the future.
+            if (forum_user_can_post_discussion($forum, -1, null, $cm, $modcontext)) {
+                // Note: We must reverse in this manner because array_unshift renumbers the array.
+                $groupinfo = array_reverse($groupinfo, true );
+                $groupinfo[-1] = get_string('allparticipants');
+                $groupinfo = array_reverse($groupinfo, true );
                 $groupcount++;
             }
 
-            $contextcheck = has_capability('mod/forum:movediscussions', $modulecontext) && empty($post->parent) && $groupcount > 1;
-            if ($contextcheck) {
-                if (has_capability('mod/forum:canposttomygroups', $modulecontext)
-                            && !isset($post->edit)) {
-                    $mform->addElement('checkbox', 'posttomygroups', get_string('posttomygroups', 'forum'));
-                    $mform->addHelpButton('posttomygroups', 'posttomygroups', 'forum');
-                    $mform->disabledIf('groupinfo', 'posttomygroups', 'checked');
-                }
+            // Determine whether the user can select a group from the dropdown. The dropdown is available for several reasons.
+            // 1) This is a new post (not an edit), and there are at least two groups to choose from.
+            $canselectgroupfornew = empty($post->edit) && $groupcount > 1;
 
-                foreach ($groupdata as $grouptemp) {
-                    $groupinfo[$grouptemp->id] = $grouptemp->name;
-                }
+            // 2) This is editing of an existing post and the user is allowed to movediscussions.
+            // We allow this because the post may have been moved from another forum where groups are not available.
+            // We show this even if no groups are available as groups *may* have been available but now are not.
+            $canselectgroupformove = $groupcount && !empty($post->edit) && has_capability('mod/forum:movediscussions', $modcontext);
+
+            // Important: You can *only* change the group for a top level post. Never any reply.
+            $canselectgroup = empty($post->parent) && ($canselectgroupfornew || $canselectgroupformove);
+
+            if ($canselectgroup) {
                 $mform->addElement('select','groupinfo', get_string('group'), $groupinfo);
                 $mform->setDefault('groupinfo', $post->groupid);
                 $mform->setType('groupinfo', PARAM_INT);
diff --git a/mod/forum/post.php b/mod/forum/post.php
index bd6bc18235d..6e3f5e6517d 100644
--- a/mod/forum/post.php
+++ b/mod/forum/post.php
@@ -697,8 +697,6 @@ if ($mform_post->is_cancelled()) {
     // WARNING: the $fromform->message array has been overwritten, do not use it anymore!
     $fromform->messagetrust  = trusttext_trusted($modcontext);
 
-    $contextcheck = isset($fromform->groupinfo) && has_capability('mod/forum:movediscussions', $modcontext);
-
     if ($fromform->edit) {           // Updating a post
         unset($fromform->groupid);
         $fromform->id = $fromform->edit;
@@ -722,10 +720,15 @@ if ($mform_post->is_cancelled()) {
         }
 
         // If the user has access to all groups and they are changing the group, then update the post.
-        if ($contextcheck) {
+        if (isset($fromform->groupinfo) && has_capability('mod/forum:movediscussions', $modcontext)) {
             if (empty($fromform->groupinfo)) {
                 $fromform->groupinfo = -1;
             }
+
+            if (!forum_user_can_post_discussion($forum, $fromform->groupinfo, null, $cm, $modcontext)) {
+                print_error('cannotupdatepost', 'forum');
+            }
+
             $DB->set_field('forum_discussions' ,'groupid' , $fromform->groupinfo, array('firstpost' => $fromform->id));
         }
 
@@ -853,6 +856,9 @@ if ($mform_post->is_cancelled()) {
         exit;
 
     } else { // Adding a new discussion.
+        // The location to redirect to after successfully posting.
+        $redirectto = new moodle_url('view.php', array('f' => $fromform->forum));
+
         $fromform->mailnow = empty($fromform->mailnow) ? 0 : 1;
 
         $discussion = $fromform;
@@ -870,17 +876,28 @@ if ($mform_post->is_cancelled()) {
 
         // If we are posting a copy to all groups the user has access to.
         if (isset($fromform->posttomygroups)) {
+            // Post to each of my groups.
             require_capability('mod/forum:canposttomygroups', $modcontext);
+
+            // Fetch all of this user's groups.
+            // Note: all groups are returned when in visible groups mode so we must manually filter.
             $allowedgroups = groups_get_activity_allowed_groups($cm);
-            $groupstopostto = array_keys($allowedgroups);
+            foreach ($allowedgroups as $groupid => $group) {
+                if (forum_user_can_post_discussion($forum, $groupid, -1, $cm, $modcontext)) {
+                    $groupstopostto[] = $groupid;
+                }
+            }
+        } else if (isset($fromform->groupinfo)) {
+            // Use the value provided in the dropdown group selection.
+            $groupstopostto[] = $fromform->groupinfo;
+            $redirectto->param('group', $fromform->groupinfo);
+        } else if (isset($fromform->groupid) && !empty($fromform->groupid)) {
+            // Use the value provided in the hidden form element instead.
+            $groupstopostto[] = $fromform->groupid;
+            $redirectto->param('group', $fromform->groupid);
         } else {
-            if ($contextcheck) {
-                $fromform->groupid = $fromform->groupinfo;
-            }
-            if (empty($fromform->groupid)) {
-                $fromform->groupid = -1;
-            }
-            $groupstopostto = array($fromform->groupid);
+            // Use the value for all participants instead.
+            $groupstopostto[] = -1;
         }
 
         // Before we post this we must check that the user will not exceed the blocking threshold.
@@ -934,9 +951,8 @@ if ($mform_post->is_cancelled()) {
             $completion->update_state($cm, COMPLETION_COMPLETE);
         }
 
-        redirect(forum_go_back_to("view.php?f=$fromform->forum"), $message.$subscribemessage, $timemessage);
-
-        exit;
+        // Redirect back to the discussion.
+        redirect(forum_go_back_to($redirectto->out()), $message . $subscribemessage, $timemessage);
     }
 }
 
diff --git a/mod/forum/tests/behat/groups_in_course_no_groups_in_forum.feature b/mod/forum/tests/behat/groups_in_course_no_groups_in_forum.feature
new file mode 100644
index 00000000000..ff4e5e3714b
--- /dev/null
+++ b/mod/forum/tests/behat/groups_in_course_no_groups_in_forum.feature
@@ -0,0 +1,62 @@
+@mod @mod_forum
+Feature: Forums in 'No groups' mode allow posting to All participants for all users
+  In order to post to a forum in 'No groups' mode, which is in course which has groups
+  As any user
+  I need to post
+
+  Background:
+    Given the following "users" exist:
+      | username | firstname | lastname | email |
+      | teacher1 | Teacher | 1 | teacher1@example.com |
+      | student1 | Student | 1 | student1@example.com |
+    And the following "courses" exist:
+      | fullname | shortname | category |
+      | Course 1 | C1 | 0 |
+    And the following "course enrolments" exist:
+      | user | course | role |
+      | teacher1 | C1 | editingteacher |
+      | student1 | C1 | student |
+    And the following "groups" exist:
+      | name | course | idnumber |
+      | Group A | C1 | G1 |
+      | Group B | C1 | G2 |
+    And the following "group members" exist:
+      | user | group |
+      | teacher1 | G1 |
+      | teacher1 | G2 |
+      | student1 | G1 |
+    And the following "activities" exist:
+      | activity   | name                   | intro                         | course | idnumber     | groupmode |
+      | forum      | Standard forum name    | Standard forum description    | C1     | nogroups     | 0         |
+
+  Scenario: Teacher can post
+    Given I log in as "teacher1"
+    And I follow "Course 1"
+    And I follow "Standard forum name"
+    And I should not see "Group A"
+    And I should not see "Group B"
+    When I click on "Add a new discussion topic" "button"
+    Then I should not see "Post a copy to all groups"
+    And I should not see "Group" in the "form" "css_element"
+    And I set the following fields to these values:
+      | Subject | Teacher 1 -> Forum  |
+      | Message | Teacher 1 -> Forum  |
+    And I press "Post to forum"
+    And I wait to be redirected
+    And I should see "Teacher 1 -> Forum"
+
+  Scenario: Student can post
+    Given I log in as "student1"
+    And I follow "Course 1"
+    And I follow "Standard forum name"
+    And I should not see "Group A"
+    And I should not see "Group B"
+    When I click on "Add a new discussion topic" "button"
+    Then I should not see "Post a copy to all groups"
+    And I should not see "Group" in the "form" "css_element"
+    And I set the following fields to these values:
+      | Subject | Student 1 -> Forum  |
+      | Message | Student 1 -> Forum  |
+    And I press "Post to forum"
+    And I wait to be redirected
+    And I should see "Student 1 -> Forum"
diff --git a/mod/forum/tests/behat/no_groups_in_course.feature b/mod/forum/tests/behat/no_groups_in_course.feature
new file mode 100644
index 00000000000..96b672f5c30
--- /dev/null
+++ b/mod/forum/tests/behat/no_groups_in_course.feature
@@ -0,0 +1,86 @@
+@mod @mod_forum
+Feature: Posting to forums in a course with no groups behaves correctly
+
+  Background:
+    Given the following "users" exist:
+      | username | firstname | lastname | email |
+      | teacher1 | Teacher | 1 | teacher1@example.com |
+      | student1 | Student | 1 | student1@example.com |
+    And the following "courses" exist:
+      | fullname | shortname | category |
+      | Course 1 | C1 | 0 |
+    And the following "course enrolments" exist:
+      | user | course | role |
+      | teacher1 | C1 | editingteacher |
+      | student1 | C1 | student |
+    And the following "activities" exist:
+      | activity   | name                   | intro                         | course | idnumber     | groupmode |
+      | forum      | Standard forum         | Standard forum description    | C1     | nogroups     | 0         |
+      | forum      | Visible forum          | Visible forum description     | C1     | visgroups    | 2         |
+      | forum      | Separate forum         | Separate forum description    | C1     | sepgroups    | 1         |
+
+  Scenario: Teachers can post in standard forum
+    Given I log in as "teacher1"
+    And I follow "Course 1"
+    And I follow "Standard forum"
+    When I click on "Add a new discussion topic" "button"
+    Then I should not see "Post a copy to all groups"
+    And I set the following fields to these values:
+      | Subject | Teacher -> All participants |
+      | Message | Teacher -> All participants |
+    And I press "Post to forum"
+    And I wait to be redirected
+    And I should see "Teacher -> All participants"
+
+  Scenario: Teachers can post in forum with separate groups
+    Given I log in as "teacher1"
+    And I follow "Course 1"
+    And I follow "Separate forum"
+    When I click on "Add a new discussion topic" "button"
+    Then I should not see "Post a copy to all groups"
+    And I set the following fields to these values:
+      | Subject | Teacher -> All participants |
+      | Message | Teacher -> All participants |
+    And I press "Post to forum"
+    And I wait to be redirected
+    And I should see "Teacher -> All participants"
+
+  Scenario: Teachers can post in forum with visible groups
+    Given I log in as "teacher1"
+    And I follow "Course 1"
+    And I follow "Visible forum"
+    When I click on "Add a new discussion topic" "button"
+    Then I should not see "Post a copy to all groups"
+    And I set the following fields to these values:
+      | Subject | Teacher -> All participants |
+      | Message | Teacher -> All participants |
+    And I press "Post to forum"
+    And I wait to be redirected
+    And I should see "Teacher -> All participants"
+
+  Scenario: Students can post in standard forum
+    Given I log in as "student1"
+    And I follow "Course 1"
+    And I follow "Standard forum"
+    When I click on "Add a new discussion topic" "button"
+    Then I should not see "Post a copy to all groups"
+    And I set the following fields to these values:
+      | Subject | Student -> All participants |
+      | Message | Student -> All participants |
+    And I press "Post to forum"
+    And I wait to be redirected
+    And I should see "Student -> All participants"
+
+  Scenario: Students cannot post in forum with separate groups
+    Given I log in as "student1"
+    And I follow "Course 1"
+    When I follow "Separate forum"
+    Then I should see "You do not have permission to add a new discussion topic for all participants."
+    And I should not see "Add a new discussion topic"
+
+  Scenario: Teachers can post in forum with visible groups
+    Given I log in as "student1"
+    And I follow "Course 1"
+    When I follow "Visible forum"
+    Then I should see "You do not have permission to add a new discussion topic for all participants."
+    And I should not see "Add a new discussion topic"
diff --git a/mod/forum/tests/behat/separate_group_discussions.feature b/mod/forum/tests/behat/separate_group_discussions.feature
index 86d4e08645b..2a99b8c4ef9 100644
--- a/mod/forum/tests/behat/separate_group_discussions.feature
+++ b/mod/forum/tests/behat/separate_group_discussions.feature
@@ -2,7 +2,7 @@
 Feature: Posting to all groups in a separate group discussion is restricted to users with access to all groups
   In order to post to all groups in a forum with separate groups
   As a teacher
-  I need to have the accessallgroups capability or be a member of all of the groups
+  I need to have the accessallgroups capability
 
   Background:
     Given the following "users" exist:
@@ -11,6 +11,7 @@ Feature: Posting to all groups in a separate group discussion is restricted to u
       | noneditor1 | Non-editing teacher | 1 | noneditor1@example.com |
       | noneditor2 | Non-editing teacher | 2 | noneditor2@example.com |
       | student1 | Student | 1 | student1@example.com |
+      | student2 | Student | 2 | student2@example.com |
     And the following "courses" exist:
       | fullname | shortname | category |
       | Course 1 | C1 | 0 |
@@ -20,6 +21,7 @@ Feature: Posting to all groups in a separate group discussion is restricted to u
       | noneditor1 | C1 | teacher |
       | noneditor2 | C1 | teacher |
       | student1 | C1 | student |
+      | student2 | C1 | student |
     And the following "groups" exist:
       | name | course | idnumber |
       | Group A | C1 | G1 |
@@ -35,18 +37,22 @@ Feature: Posting to all groups in a separate group discussion is restricted to u
       | noneditor2 | G1 |
       | noneditor2 | G2 |
       | student1 | G1 |
-      | student1 | G2 |
-    And I log in as "teacher1"
-    And I follow "Course 1"
-    And I turn editing mode on
-    And I add a "Forum" to section "1" and I fill the form with:
-      | Forum name | Standard forum name |
-      | Forum type | Standard forum for general use |
-      | Description | Standard forum description |
-      | Group mode | Separate groups |
-    And I log out
+      | student2 | G1 |
+      | student2 | G2 |
+    And the following "activities" exist:
+      | activity   | name                   | intro                         | course | idnumber     | groupmode |
+      | forum      | Standard forum name    | Standard forum description    | C1     | sepgroups    | 1         |
 
-  Scenario: Teacher with accessallgroups can post in all groups
+  Scenario: Teacher with accessallgroups can view all groups
+    Given I log in as "teacher1"
+    And I follow "Course 1"
+    When I follow "Standard forum name"
+    Then the "Separate groups" select box should contain "All participants"
+    Then the "Separate groups" select box should contain "Group A"
+    Then the "Separate groups" select box should contain "Group B"
+    Then the "Separate groups" select box should contain "Group C"
+
+  Scenario: Teacher with accessallgroups can select any group when posting
     Given I log in as "teacher1"
     And I follow "Course 1"
     And I follow "Standard forum name"
@@ -54,8 +60,158 @@ Feature: Posting to all groups in a separate group discussion is restricted to u
     Then the "Group" select box should contain "All participants"
     And the "Group" select box should contain "Group A"
     And the "Group" select box should contain "Group B"
+    And the "Group" select box should contain "Group C"
     And I should see "Post a copy to all groups"
 
+  Scenario: Teacher with accessallgroups can post in groups they are a member of
+    Given I log in as "teacher1"
+    And I follow "Course 1"
+    And I follow "Standard forum name"
+    And I select "Group A" from the "Separate groups" singleselect
+    When I click on "Add a new discussion topic" "button"
+    Then I should see "Post a copy to all groups"
+    And I set the following fields to these values:
+      | Subject | Teacher 1 -> Group B  |
+      | Message | Teacher 1 -> Group B  |
+      # Change the group in the post form.
+      | Group   | Group B               |
+    And I press "Post to forum"
+    And I wait to be redirected
+    # We should be redirected to the group that we selected when posting.
+    And the field "Separate groups" matches value "Group B"
+    And I should see "Group B" in the "Teacher 1 -> Group B" "table_row"
+    And I should not see "Group A" in the "Teacher 1 -> Group B" "table_row"
+    And I should not see "Group C" in the "Teacher 1 -> Group B" "table_row"
+    # It should also be displayed under All participants
+    And I select "All participants" from the "Separate groups" singleselect
+    And I should see "Group B" in the "Teacher 1 -> Group B" "table_row"
+    And I should not see "Group A" in the "Teacher 1 -> Group B" "table_row"
+    And I should not see "Group C" in the "Teacher 1 -> Group B" "table_row"
+    # It should not be displayed in Groups A, or C.
+    And I select "Group A" from the "Separate groups" singleselect
+    And I should not see "Teacher 1 -> Group B"
+    And I select "Group C" from the "Separate groups" singleselect
+    And I should not see "Teacher 1 -> Group B"
+
+  Scenario: Teacher with accessallgroups can post in groups they are not a member of
+    Given I log in as "teacher1"
+    And I follow "Course 1"
+    And I follow "Standard forum name"
+    And I select "Group A" from the "Separate groups" singleselect
+    When I click on "Add a new discussion topic" "button"
+    Then I should see "Post a copy to all groups"
+    And I set the following fields to these values:
+      | Subject | Teacher 1 -> Group C  |
+      | Message | Teacher 1 -> Group C  |
+      | Group   | Group C               |
+    And I press "Post to forum"
+    And I wait to be redirected
+    # We should be redirected to the group that we selected when posting.
+    And the field "Separate groups" matches value "Group C"
+    # We redirect to the group posted in automatically.
+    And I should see "Group C" in the "Teacher 1 -> Group C" "table_row"
+    And I should not see "Group A" in the "Teacher 1 -> Group C" "table_row"
+    And I should not see "Group B" in the "Teacher 1 -> Group C" "table_row"
+    # It should also be displayed under All participants
+    And I select "All participants" from the "Separate groups" singleselect
+    And I should see "Group C" in the "Teacher 1 -> Group C" "table_row"
+    And I should not see "Group A" in the "Teacher 1 -> Group C" "table_row"
+    And I should not see "Group B" in the "Teacher 1 -> Group C" "table_row"
+    # It should not be displayed in Groups A, or B.
+    And I select "Group A" from the "Separate groups" singleselect
+    And I should not see "Teacher 1 -> Group C"
+    And I select "Group B" from the "Separate groups" singleselect
+    And I should not see "Teacher 1 -> Group C"
+
+  Scenario: Teacher with accessallgroups can post to all groups
+    Given I log in as "teacher1"
+    And I follow "Course 1"
+    And I follow "Standard forum name"
+    When I click on "Add a new discussion topic" "button"
+    And I set the following fields to these values:
+      | Subject                   | Teacher 1 -> Post to all  |
+      | Message                   | Teacher 1 -> Post to all  |
+      | Post a copy to all groups | 1                       |
+    And I press "Post to forum"
+    And I wait to be redirected
+    # Posting to all groups means that we should be redirected to the page we started from.
+    And the field "Separate groups" matches value "All participants"
+    And I select "Group A" from the "Separate groups" singleselect
+    Then I should see "Group A" in the "Teacher 1 -> Post to all" "table_row"
+    And I should not see "Group B" in the "Teacher 1 -> Post to all" "table_row"
+    And I should not see "Group C" in the "Teacher 1 -> Post to all" "table_row"
+    And I select "Group B" from the "Separate groups" singleselect
+    And I should see "Group B" in the "Teacher 1 -> Post to all" "table_row"
+    And I should not see "Group A" in the "Teacher 1 -> Post to all" "table_row"
+    And I should not see "Group C" in the "Teacher 1 -> Post to all" "table_row"
+    And I select "Group C" from the "Separate groups" singleselect
+    And I should see "Group C" in the "Teacher 1 -> Post to all" "table_row"
+    And I should not see "Group A" in the "Teacher 1 -> Post to all" "table_row"
+    And I should not see "Group B" in the "Teacher 1 -> Post to all" "table_row"
+    # No point testing the "All participants".
+
+  Scenario: Students in one group can only post in their group
+    Given I log in as "student1"
+    And I follow "Course 1"
+    When I follow "Standard forum name"
+    Then I should see "Group A"
+    And I click on "Add a new discussion topic" "button"
+    And I should see "Group A"
+    And I should not see "Group B"
+    And I should not see "Group C"
+    And I should not see "Post a copy to all groups"
+    And I set the following fields to these values:
+      | Subject | Student -> B |
+      | Message | Student -> B |
+    And I press "Post to forum"
+    And I wait to be redirected
+    And I should see "Group A" in the "Student -> B" "table_row"
+    And I should not see "Group B" in the "Student -> B" "table_row"
+
+  Scenario: Students in multiple group can post in all of their group individually
+    Given I log in as "student2"
+    And I follow "Course 1"
+    When I follow "Standard forum name"
+    And I select "Group A" from the "Separate groups" singleselect
+    And I click on "Add a new discussion topic" "button"
+    And the "Group" select box should not contain "All participants"
+    And the "Group" select box should contain "Group A"
+    And the "Group" select box should contain "Group B"
+    And the "Group" select box should not contain "Group C"
+    And I should not see "Post a copy to all groups"
+    And I set the following fields to these values:
+      | Subject | Student -> B  |
+      | Message | Student -> B  |
+      | Group   | Group B       |
+    And I press "Post to forum"
+    And I wait to be redirected
+    # We should be redirected to the group that we selected when posting.
+    And the field "Separate groups" matches value "Group B"
+    And I should see "Group B" in the "Student -> B" "table_row"
+    And I should not see "Group A" in the "Student -> B" "table_row"
+    And I select "Group A" from the "Separate groups" singleselect
+    And I should not see "Student -> B"
+    # Now try posting in Group A (starting at Group B)
+    And I select "Group B" from the "Separate groups" singleselect
+    And I click on "Add a new discussion topic" "button"
+    And the "Group" select box should not contain "All participants"
+    And the "Group" select box should contain "Group A"
+    And the "Group" select box should contain "Group B"
+    And the "Group" select box should not contain "Group C"
+    And I should not see "Post a copy to all groups"
+    And I set the following fields to these values:
+      | Subject | Student -> A  |
+      | Message | Student -> A  |
+      | Group   | Group A       |
+    And I press "Post to forum"
+    And I wait to be redirected
+    # We should be redirected to the group that we selected when posting.
+    And the field "Separate groups" matches value "Group A"
+    And I should see "Group A" in the "Student -> A" "table_row"
+    And I should not see "Group B" in the "Student -> A" "table_row"
+    And I select "Group B" from the "Separate groups" singleselect
+    And I should not see "Student -> A"
+
   Scenario: Teacher in all groups but without accessallgroups can only post in their groups
     And I log in as "admin"
     And I set the following system permissions of "Non-editing teacher" role:
diff --git a/mod/forum/tests/behat/separate_group_single_group_discussions.feature b/mod/forum/tests/behat/separate_group_single_group_discussions.feature
index 7fda1f22d68..71e81a6a83f 100644
--- a/mod/forum/tests/behat/separate_group_single_group_discussions.feature
+++ b/mod/forum/tests/behat/separate_group_single_group_discussions.feature
@@ -64,7 +64,7 @@ Feature: Posting to groups in a separate group discussion when restricted to gro
     And I click on "Add a new discussion topic" "button"
     And the "Group" select box should contain "All participants"
     And the "Group" select box should contain "G2G1"
-    And I should see "Post a copy to all groups"
+    And I should not see "Post a copy to all groups"
 
   Scenario: Teacher in all groups but without accessallgroups can post in either group but not to All Participants
     And I log in as "admin"
diff --git a/mod/forum/tests/behat/visible_group_discussions.feature b/mod/forum/tests/behat/visible_group_discussions.feature
new file mode 100644
index 00000000000..45cb568c475
--- /dev/null
+++ b/mod/forum/tests/behat/visible_group_discussions.feature
@@ -0,0 +1,213 @@
+@mod @mod_forum
+Feature: Posting to all groups in a visible group discussion is restricted to users with access to all groups
+  In order to post to all groups in a forum with visible groups
+  As a teacher
+  I need to have the accessallgroups capability
+
+  Background:
+    Given the following "users" exist:
+      | username | firstname | lastname | email |
+      | teacher1 | Teacher | 1 | teacher1@example.com |
+      | student1 | Student | 1 | student1@example.com |
+      | student2 | Student | 2 | student2@example.com |
+    And the following "courses" exist:
+      | fullname | shortname | category |
+      | Course 1 | C1 | 0 |
+    And the following "course enrolments" exist:
+      | user | course | role |
+      | teacher1 | C1 | editingteacher |
+      | student1 | C1 | student |
+      | student2 | C1 | student |
+    And the following "groups" exist:
+      | name | course | idnumber |
+      | Group A | C1 | G1 |
+      | Group B | C1 | G2 |
+      | Group C | C1 | G3 |
+    And the following "group members" exist:
+      | user | group |
+      | teacher1 | G1 |
+      | teacher1 | G2 |
+      | student1 | G1 |
+      | student2 | G1 |
+      | student2 | G2 |
+    And the following "activities" exist:
+      | activity   | name                   | intro                         | course | idnumber     | groupmode |
+      | forum      | Standard forum name    | Standard forum description    | C1     | groups       | 2         |
+
+  Scenario: Teacher with accessallgroups can view all groups
+    Given I log in as "teacher1"
+    And I follow "Course 1"
+    When I follow "Standard forum name"
+    Then the "Visible groups" select box should contain "All participants"
+    Then the "Visible groups" select box should contain "Group A"
+    Then the "Visible groups" select box should contain "Group B"
+    Then the "Visible groups" select box should contain "Group C"
+
+  Scenario: Teacher with accessallgroups can select any group when posting
+    Given I log in as "teacher1"
+    And I follow "Course 1"
+    And I follow "Standard forum name"
+    When I click on "Add a new discussion topic" "button"
+    Then the "Group" select box should contain "All participants"
+    And the "Group" select box should contain "Group A"
+    And the "Group" select box should contain "Group B"
+    And the "Group" select box should contain "Group C"
+    And I should see "Post a copy to all groups"
+
+  Scenario: Teacher with accessallgroups can post in groups they are a member of
+    Given I log in as "teacher1"
+    And I follow "Course 1"
+    And I follow "Standard forum name"
+    And I select "Group A" from the "Visible groups" singleselect
+    When I click on "Add a new discussion topic" "button"
+    Then I should see "Post a copy to all groups"
+    And I set the following fields to these values:
+      | Subject | Teacher 1 -> Group B  |
+      | Message | Teacher 1 -> Group B  |
+      # Change the group in the post form.
+      | Group   | Group B               |
+    And I press "Post to forum"
+    And I wait to be redirected
+    # We should be redirected to the group that we selected when posting.
+    And the field "Visible groups" matches value "Group B"
+    And I should see "Group B" in the "Teacher 1 -> Group B" "table_row"
+    And I should not see "Group A" in the "Teacher 1 -> Group B" "table_row"
+    And I should not see "Group C" in the "Teacher 1 -> Group B" "table_row"
+    # It should also be displayed under All participants
+    And I select "All participants" from the "Visible groups" singleselect
+    And I should see "Group B" in the "Teacher 1 -> Group B" "table_row"
+    And I should not see "Group A" in the "Teacher 1 -> Group B" "table_row"
+    And I should not see "Group C" in the "Teacher 1 -> Group B" "table_row"
+    # It should not be displayed in Groups A, or C.
+    And I select "Group A" from the "Visible groups" singleselect
+    And I should not see "Teacher 1 -> Group B"
+    And I select "Group C" from the "Visible groups" singleselect
+    And I should not see "Teacher 1 -> Group B"
+
+  Scenario: Teacher with accessallgroups can post in groups they are not a member of
+    Given I log in as "teacher1"
+    And I follow "Course 1"
+    And I follow "Standard forum name"
+    And I select "Group A" from the "Visible groups" singleselect
+    When I click on "Add a new discussion topic" "button"
+    Then I should see "Post a copy to all groups"
+    And I set the following fields to these values:
+      | Subject | Teacher 1 -> Group C  |
+      | Message | Teacher 1 -> Group C  |
+      | Group   | Group C               |
+    And I press "Post to forum"
+    And I wait to be redirected
+    # We should be redirected to the group that we selected when posting.
+    And the field "Visible groups" matches value "Group C"
+    # We redirect to the group posted in automatically.
+    And I should see "Group C" in the "Teacher 1 -> Group C" "table_row"
+    And I should not see "Group A" in the "Teacher 1 -> Group C" "table_row"
+    And I should not see "Group B" in the "Teacher 1 -> Group C" "table_row"
+    # It should also be displayed under All participants
+    And I select "All participants" from the "Visible groups" singleselect
+    And I should see "Group C" in the "Teacher 1 -> Group C" "table_row"
+    And I should not see "Group A" in the "Teacher 1 -> Group C" "table_row"
+    And I should not see "Group B" in the "Teacher 1 -> Group C" "table_row"
+    # It should not be displayed in Groups A, or B.
+    And I select "Group A" from the "Visible groups" singleselect
+    And I should not see "Teacher 1 -> Group C"
+    And I select "Group B" from the "Visible groups" singleselect
+    And I should not see "Teacher 1 -> Group C"
+
+  Scenario: Teacher with accessallgroups can post to all groups
+    Given I log in as "teacher1"
+    And I follow "Course 1"
+    And I follow "Standard forum name"
+    When I click on "Add a new discussion topic" "button"
+    And I set the following fields to these values:
+      | Subject                   | Teacher 1 -> Post to all  |
+      | Message                   | Teacher 1 -> Post to all  |
+      | Post a copy to all groups | 1                       |
+    And I press "Post to forum"
+    And I wait to be redirected
+    # Posting to all groups means that we should be redirected to the page we started from.
+    And the field "Visible groups" matches value "All participants"
+    And I select "Group A" from the "Visible groups" singleselect
+    Then I should see "Group A" in the "Teacher 1 -> Post to all" "table_row"
+    And I should not see "Group B" in the "Teacher 1 -> Post to all" "table_row"
+    And I should not see "Group C" in the "Teacher 1 -> Post to all" "table_row"
+    And I select "Group B" from the "Visible groups" singleselect
+    And I should see "Group B" in the "Teacher 1 -> Post to all" "table_row"
+    And I should not see "Group A" in the "Teacher 1 -> Post to all" "table_row"
+    And I should not see "Group C" in the "Teacher 1 -> Post to all" "table_row"
+    And I select "Group C" from the "Visible groups" singleselect
+    And I should see "Group C" in the "Teacher 1 -> Post to all" "table_row"
+    And I should not see "Group A" in the "Teacher 1 -> Post to all" "table_row"
+    And I should not see "Group B" in the "Teacher 1 -> Post to all" "table_row"
+    # No point testing the "All participants".
+
+  Scenario: Students can view all groups
+    Given I log in as "student1"
+    And I follow "Course 1"
+    When I follow "Standard forum name"
+    Then the "Visible groups" select box should contain "All participants"
+    Then the "Visible groups" select box should contain "Group A"
+    Then the "Visible groups" select box should contain "Group B"
+    Then the "Visible groups" select box should contain "Group C"
+
+  Scenario: Students in one group can only post in their group
+    Given I log in as "student1"
+    And I follow "Course 1"
+    When I follow "Standard forum name"
+    Then I should see "Group A"
+    And I click on "Add a new discussion topic" "button"
+    And I should see "Group A"
+    And I should not see "Group B"
+    And I should not see "Group C"
+    And I should not see "Post a copy to all groups"
+    And I set the following fields to these values:
+      | Subject | Student -> B |
+      | Message | Student -> B |
+    And I press "Post to forum"
+    And I wait to be redirected
+    And I should see "Group A" in the "Student -> B" "table_row"
+    And I should not see "Group B" in the "Student -> B" "table_row"
+
+  Scenario: Students in multiple group can post in all of their group individually
+    Given I log in as "student2"
+    And I follow "Course 1"
+    When I follow "Standard forum name"
+    And I select "Group A" from the "Visible groups" singleselect
+    And I click on "Add a new discussion topic" "button"
+    And the "Group" select box should not contain "All participants"
+    And the "Group" select box should contain "Group A"
+    And the "Group" select box should contain "Group B"
+    And the "Group" select box should not contain "Group C"
+    And I should not see "Post a copy to all groups"
+    And I set the following fields to these values:
+      | Subject | Student -> B  |
+      | Message | Student -> B  |
+      | Group   | Group B       |
+    And I press "Post to forum"
+    And I wait to be redirected
+    # We should be redirected to the group that we selected when posting.
+    And the field "Visible groups" matches value "Group B"
+    And I should see "Group B" in the "Student -> B" "table_row"
+    And I should not see "Group A" in the "Student -> B" "table_row"
+    And I select "Group A" from the "Visible groups" singleselect
+    And I should not see "Student -> B"
+    # Now try posting in Group A (starting at Group B)
+    And I select "Group B" from the "Visible groups" singleselect
+    And I click on "Add a new discussion topic" "button"
+    And the "Group" select box should not contain "All participants"
+    And the "Group" select box should contain "Group A"
+    And the "Group" select box should contain "Group B"
+    And the "Group" select box should not contain "Group C"
+    And I should not see "Post a copy to all groups"
+    And I set the following fields to these values:
+      | Subject | Student -> A  |
+      | Message | Student -> A  |
+      | Group   | Group A       |
+    And I press "Post to forum"
+    And I wait to be redirected
+    # We should be redirected to the group that we selected when posting.
+    And the field "Visible groups" matches value "Group A"
+    And I should see "Group A" in the "Student -> A" "table_row"
+    And I should not see "Group B" in the "Student -> A" "table_row"
+    And I select "Group B" from the "Visible groups" singleselect
+    And I should not see "Student -> A"