diff --git a/mod/forum/discuss.php b/mod/forum/discuss.php
index eb05d32907b..01c536548f0 100644
--- a/mod/forum/discuss.php
+++ b/mod/forum/discuss.php
@@ -230,7 +230,7 @@
echo $OUTPUT->header();
$headingvalue = format_string($forum->name);
- if (has_capability('mod/forum:viewdiscussion', $modcontext)) {
+ if ((!isguestuser() && isloggedin()) && has_capability('mod/forum:viewdiscussion', $modcontext)) {
// Discussion subscription.
if (\mod_forum\subscriptions::is_subscribable($forum)) {
$headingvalue .= ' ';
diff --git a/mod/forum/lib.php b/mod/forum/lib.php
index b3a63b1a099..6d114281ff8 100644
--- a/mod/forum/lib.php
+++ b/mod/forum/lib.php
@@ -3717,7 +3717,7 @@ function forum_print_discussion_header(&$post, $forum, $group=-1, $datestring=""
userdate($usedate, $datestring).'';
echo "\n";
- if (has_capability('mod/forum:viewdiscussion', $modcontext)) {
+ if ((!isguestuser() && isloggedin()) && has_capability('mod/forum:viewdiscussion', $modcontext)) {
// Discussion subscription.
if (\mod_forum\subscriptions::is_subscribable($forum)) {
echo '
';
diff --git a/mod/forum/tests/behat/discussion_subscriptions.feature b/mod/forum/tests/behat/discussion_subscriptions.feature
index d05a83dd1bf..eccf524ac05 100644
--- a/mod/forum/tests/behat/discussion_subscriptions.feature
+++ b/mod/forum/tests/behat/discussion_subscriptions.feature
@@ -269,3 +269,38 @@ Feature: A user can control their own subscription preferences for a discussion
And I follow "Test post subject two"
And I follow "Reply"
And the field "Discussion subscription" matches value "I don't want email copies of posts to this discussion"
+
+ Scenario: A guest should not be able to subscribe to a discussion
+ Given I click on "Home" "link" in the "Navigation" "block"
+ And I add a "Forum" to section "1" and I fill the form with:
+ | Forum name | Test forum name |
+ | Forum type | Standard forum for general use |
+ | Description | Test forum description |
+ And I add a new discussion to "Test forum name" forum with:
+ | Subject | Test post subject one |
+ | Message | Test post message one |
+ And I log out
+ When I log in as "guest"
+ And I follow "Test forum name"
+ Then "Not subscribed. Click to subscribe." "link" should not exist in the "Test post subject one" "table_row"
+ And "You are subscribed to this discussion. Click to unsubscribe." "link" should not exist in the "Test post subject one" "table_row"
+ And I follow "Test post subject one"
+ And "Not subscribed. Click to subscribe." "link" should not exist
+ And "You are subscribed to this discussion. Click to unsubscribe." "link" should not exist
+
+ Scenario: A user who is not logged in should not be able to subscribe to a discussion
+ Given I click on "Home" "link" in the "Navigation" "block"
+ And I add a "Forum" to section "1" and I fill the form with:
+ | Forum name | Test forum name |
+ | Forum type | Standard forum for general use |
+ | Description | Test forum description |
+ And I add a new discussion to "Test forum name" forum with:
+ | Subject | Test post subject one |
+ | Message | Test post message one |
+ And I log out
+ When I follow "Test forum name"
+ Then "Not subscribed. Click to subscribe." "link" should not exist in the "Test post subject one" "table_row"
+ And "You are subscribed to this discussion. Click to unsubscribe." "link" should not exist in the "Test post subject one" "table_row"
+ And I follow "Test post subject one"
+ And "Not subscribed. Click to subscribe." "link" should not exist
+ And "You are subscribed to this discussion. Click to unsubscribe." "link" should not exist
|