From eac4b571eeb89dccd86762dc9a80bee2037ef08e Mon Sep 17 00:00:00 2001 From: Pedro Jordao Date: Wed, 28 Feb 2024 12:32:16 +0100 Subject: [PATCH] MDL-80279 core: Set new attributes for user profile initials When the user does not have a profile photo and initials are displayed, there is no alt text for the initials which causes accessibility issues, so with this change I added the user's full name to the title and aria-label attributes and I set a role='img' when the element span is rendered. --- lib/outputrenderers.php | 8 +++++++- lib/tests/user_test.php | 9 ++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/outputrenderers.php b/lib/outputrenderers.php index 39a800c79a0..461fae59d76 100644 --- a/lib/outputrenderers.php +++ b/lib/outputrenderers.php @@ -2732,10 +2732,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))); }