diff --git a/lib/outputrenderers.php b/lib/outputrenderers.php
index 5852068f61b..47a42e8230c 100644
--- a/lib/outputrenderers.php
+++ b/lib/outputrenderers.php
@@ -2790,10 +2790,16 @@ class core_renderer extends renderer_base {
// Get the image html output first, auto generated based on initials if one isn't already set.
if ($user->picture == 0 && empty($CFG->enablegravatar) && !defined('BEHAT_SITE_RUNNING')) {
$initials = \core_user::get_initials($user);
+ $fullname = fullname($userpicture->user, $canviewfullnames);
// Don't modify in corner cases where neither the firstname nor the lastname appears.
$output = html_writer::tag(
'span', $initials,
- ['class' => 'userinitials size-' . $size]
+ [
+ 'class' => 'userinitials size-' . $size,
+ 'title' => $fullname,
+ 'aria-label' => $fullname,
+ 'role' => 'img',
+ ]
);
} else {
$output = html_writer::empty_tag('img', $attributes);
diff --git a/lib/tests/user_test.php b/lib/tests/user_test.php
index 60851556cd3..0a82f09f186 100644
--- a/lib/tests/user_test.php
+++ b/lib/tests/user_test.php
@@ -917,7 +917,8 @@ class user_test extends \advanced_testcase {
// Display profile picture.
$context = \context_system::instance();
// No image, show initials.
- $this->assertStringContainsString("JD",
+ $this->assertStringContainsString(
+ "JD",
$OUTPUT->render(\core_user::get_profile_picture($user1, $context)));
// With Image.
$expectedimagesrc = $CFG->wwwroot . '/pluginfile.php/' . \context_user::instance($user2->id)->id .
@@ -927,12 +928,14 @@ class user_test extends \advanced_testcase {
// Display profile picture with options.
$options = ['size' => 50, 'includefullname' => true];
- $this->assertStringContainsString("JDJohn Doe",
+ $this->assertStringContainsString(
+ "JDJohn Doe",
$OUTPUT->render(\core_user::get_profile_picture($user1, $context, $options)));
// Display profile picture with options, no link.
$options = ['link' => false];
- $this->assertEquals("JD",
+ $this->assertEquals(
+ "JD",
$OUTPUT->render(\core_user::get_profile_picture($user1, $context, $options)));
}