MDL-67168 forum: Correct behaviour of viewfullnames

This commit is contained in:
Andrew Nicols 2019-11-21 15:08:33 +08:00
parent d547735f2f
commit 916d611684
2 changed files with 96 additions and 1 deletions

View File

@ -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.

View File

@ -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