From 916d6116845c07deeed2b3fd938bd4c8aeb40f01 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Thu, 21 Nov 2019 15:08:33 +0800 Subject: [PATCH] MDL-67168 forum: Correct behaviour of viewfullnames --- mod/forum/classes/task/send_user_digests.php | 2 +- mod/forum/tests/maildigest_test.php | 95 ++++++++++++++++++++ 2 files changed, 96 insertions(+), 1 deletion(-) diff --git a/mod/forum/classes/task/send_user_digests.php b/mod/forum/classes/task/send_user_digests.php index 148afc5e0e1..a625333b526 100644 --- a/mod/forum/classes/task/send_user_digests.php +++ b/mod/forum/classes/task/send_user_digests.php @@ -200,7 +200,7 @@ class send_user_digests extends \core\task\adhoc_task { } if (!isset($this->viewfullnames[$forum->id])) { - $this->viewfullnames = has_capability('moodle/site:viewfullnames', $modcontext, $this->recipient->id); + $this->viewfullnames[$forum->id] = has_capability('moodle/site:viewfullnames', $modcontext, $this->recipient->id); } // Set the discussion storage values. diff --git a/mod/forum/tests/maildigest_test.php b/mod/forum/tests/maildigest_test.php index 76759b6020a..e725f7570ec 100644 --- a/mod/forum/tests/maildigest_test.php +++ b/mod/forum/tests/maildigest_test.php @@ -360,6 +360,101 @@ class mod_forum_maildigest_testcase extends advanced_testcase { $this->send_digests_and_assert($user, $posts); } + /** + * Send digests to a user who cannot view fullnames + */ + public function test_cron_digest_view_fullnames_off() { + global $DB, $CFG; + + $CFG->fullnamedisplay = 'lastname'; + $this->resetAfterTest(true); + + // Set up a basic user enrolled in a course. + $userhelper = $this->helper_setup_user_in_course(); + $user = $userhelper->user; + $course1 = $userhelper->courses->course1; + $forum1 = $userhelper->forums->forum1; + $posts = []; + + // Add 1 discussions to forum 1. + list($discussion, $post) = $this->helper_post_to_forum($forum1, $user, ['mailnow' => 1]); + $posts[] = $post; + + // Set the tested user's default maildigest setting. + $DB->set_field('user', 'maildigest', 1, array('id' => $user->id)); + + // No digests mails should be sent, but 1 forum mails will be sent. + $expect = [ + (object) [ + 'userid' => $user->id, + 'messages' => 0, + 'digests' => 1, + ], + ]; + $this->queue_tasks_and_assert($expect); + $this->send_digests_and_assert($user, $posts); + + // The user does not, by default, have permission to view the fullname. + $messagecontent = $this->messagesink->get_messages()[0]->fullmessage; + + // Assert that the expected name is present (lastname only). + $this->assertContains(fullname($user, false), $messagecontent); + + // Assert that the full name is not present (firstname lastname only). + $this->assertNotContains(fullname($user, true), $messagecontent); + } + + /** + * Send digests to a user who can view fullnames. + */ + public function test_cron_digest_view_fullnames_on() { + global $DB, $CFG; + + $CFG->fullnamedisplay = 'lastname'; + $this->resetAfterTest(true); + + // Set up a basic user enrolled in a course. + $userhelper = $this->helper_setup_user_in_course(); + $user = $userhelper->user; + $course1 = $userhelper->courses->course1; + $forum1 = $userhelper->forums->forum1; + $posts = []; + assign_capability( + 'moodle/site:viewfullnames', + CAP_ALLOW, + $DB->get_field('role', 'id', ['shortname' => 'student']), + \context_course::instance($course1->id) + ); + + // Add 1 discussions to forum 1. + list($discussion, $post) = $this->helper_post_to_forum($forum1, $user, ['mailnow' => 1]); + $posts[] = $post; + + // Set the tested user's default maildigest setting. + $DB->set_field('user', 'maildigest', 1, array('id' => $user->id)); + + // No digests mails should be sent, but 1 forum mails will be sent. + $expect = [ + (object) [ + 'userid' => $user->id, + 'messages' => 0, + 'digests' => 1, + ], + ]; + $this->queue_tasks_and_assert($expect); + $this->send_digests_and_assert($user, $posts); + + // The user does not, by default, have permission to view the fullname. + // However we have given the user that capability so we expect to see both firstname and lastname. + $messagecontent = $this->messagesink->get_messages()[0]->fullmessage; + + // Assert that the expected name is present (lastname only). + $this->assertContains(fullname($user, false), $messagecontent); + + // Assert that the full name is also present (firstname lastname only). + $this->assertContains(fullname($user, true), $messagecontent); + } + /** * Sends several notifications to one user as: * * daily digests coming from the per-forum setting; and