<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[54213] trunk: Role/Capability: Add a new `update_role` function.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/54213">54213</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/54213","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>davidbaumwald</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2022-09-19 20:47:22 +0000 (Mon, 19 Sep 2022)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Role/Capability: Add a new `update_role` function.

Until now, changing a user's role involved deleting a user's role then re-adding.  This change creates a new `update_role` function and associated method in `WP_Roles` to consolidate this process.

This commit also introduces new unit tests around `update_role` and adds additional "unhappy path" tests for roles and capabilities in general.

Props maksimkuzmin, peterwilsoncc, NomNom99, costdev, SergeyBiryukov.
Fixes <a href="https://core.trac.wordpress.org/ticket/54572">#54572</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludescapabilitiesphp">trunk/src/wp-includes/capabilities.php</a></li>
<li><a href="#trunksrcwpincludesclasswprolesphp">trunk/src/wp-includes/class-wp-roles.php</a></li>
<li><a href="#trunktestsphpunittestsusercapabilitiesphp">trunk/tests/phpunit/tests/user/capabilities.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludescapabilitiesphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/capabilities.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/capabilities.php    2022-09-19 20:26:09 UTC (rev 54212)
+++ trunk/src/wp-includes/capabilities.php      2022-09-19 20:47:22 UTC (rev 54213)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1031,6 +1031,30 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Updates an existing role. Creates a new role if it doesn't exist.
+ *
+ * Modifies the display name and/or capabilities for an existing role.
+ * If the role does not exist then a new role is created.
+ *
+ * The capabilities are defined in the following format: `array( 'read' => true )`.
+ * To explicitly deny the role a capability, set the value for that capability to false.
+ *
+ * @since 6.1.0
+ *
+ * @param string      $role         Role name.
+ * @param string|null $display_name Optional. Role display name. If null, the display name
+ *                                  is not modified. Default null.
+ * @param bool[]|null $capabilities Optional. List of capabilities keyed by the capability name,
+ *                                  e.g. `array( 'edit_posts' => true, 'delete_posts' => false )`.
+ *                                  If null, don't alter capabilities for the existing role and make
+ *                                  empty capabilities for the new one. Default null.
+ * @return WP_Role|void WP_Role object, if the role is updated.
+ */
+function update_role( $role, $display_name = null, $capabilities = null ) {
+       return wp_roles()->update_role( $role, $display_name, $capabilities );
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px">  * Removes a role, if it exists.
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 2.0.0
</span></span></pre></div>
<a id="trunksrcwpincludesclasswprolesphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-roles.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-roles.php  2022-09-19 20:26:09 UTC (rev 54212)
+++ trunk/src/wp-includes/class-wp-roles.php    2022-09-19 20:47:22 UTC (rev 54213)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -173,6 +173,76 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Updates an existing role. Creates a new role if it doesn't exist.
+        *
+        * Modifies the display name and/or capabilities for an existing role.
+        * If the role does not exist then a new role is created.
+        *
+        * The capabilities are defined in the following format: `array( 'read' => true )`.
+        * To explicitly deny the role a capability, set the value for that capability to false.
+        *
+        * @since 6.1.0
+        *
+        * @param string      $role         Role name.
+        * @param string|null $display_name Optional. Role display name. If null, the display name
+        *                                  is not modified. Default null.
+        * @param bool[]|null $capabilities Optional. List of capabilities keyed by the capability name,
+        *                                  e.g. `array( 'edit_posts' => true, 'delete_posts' => false )`.
+        *                                  If null, don't alter capabilities for the existing role and make
+        *                                  empty capabilities for the new one. Default null.
+        * @return WP_Role|void WP_Role object, if the role is updated.
+        */
+       public function update_role( $role, $display_name = null, $capabilities = null ) {
+               if ( ! is_string( $role ) || '' === trim( $role ) ) {
+                       return;
+               }
+
+               if ( null !== $display_name && ( ! is_string( $display_name ) || '' === trim( $display_name ) ) ) {
+                       return;
+               }
+
+               if ( null !== $capabilities && ! is_array( $capabilities ) ) {
+                       return;
+               }
+
+               if ( null === $display_name && null === $capabilities ) {
+                       if ( isset( $this->role_objects[ $role ] ) ) {
+                               return $this->role_objects[ $role ];
+                       }
+                       return;
+               }
+
+               if ( null === $display_name ) {
+                       if ( ! isset( $this->role_objects[ $role ] ) ) {
+                               return;
+                       }
+
+                       $display_name = $this->roles[ $role ]['name'];
+               }
+
+               if ( null === $capabilities ) {
+                       if ( isset( $this->role_objects[ $role ] ) ) {
+                               $capabilities = $this->role_objects[ $role ]->capabilities;
+                       } else {
+                               $capabilities = array();
+                       }
+               }
+
+               if ( isset( $this->roles[ $role ] ) ) {
+                       if ( null === $capabilities ) {
+                               $capabilities = $this->role_objects[ $role ]->capabilities;
+                       }
+
+                       unset( $this->role_objects[ $role ] );
+                       unset( $this->role_names[ $role ] );
+                       unset( $this->roles[ $role ] );
+               }
+
+               // The roles database option will be updated in ::add_role().
+               return $this->add_role( $role, $display_name, $capabilities );
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Removes a role by name.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 2.0.0
</span></span></pre></div>
<a id="trunktestsphpunittestsusercapabilitiesphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/user/capabilities.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/user/capabilities.php   2022-09-19 20:26:09 UTC (rev 54212)
+++ trunk/tests/phpunit/tests/user/capabilities.php     2022-09-19 20:47:22 UTC (rev 54213)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -999,6 +999,461 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @dataProvider data_update_role_unhappy_paths
+        *
+        * @ticket 54572
+        *
+        * @covers WP_Roles::update_role
+        * @covers ::update_role
+        *
+        * @param mixed $role         The role to update.
+        * @param mixed $display_name The display name for the role.
+        * @param mixed $capabilities The capabilities for the role.
+        */
+       public function test_update_role_unhappy_paths( $role, $display_name, $capabilities ) {
+               global $wp_roles;
+
+               // Create role if it does not exist.
+               $role_name     = 'janitor';
+               $expected_caps = array(
+                       'edit_posts' => true,
+                       'edit_pages' => true,
+                       'level_0'    => true,
+                       'level_1'    => true,
+                       'level_2'    => true,
+               );
+               add_role( $role_name, 'Janitor', $expected_caps );
+               $this->flush_roles();
+
+               $this->assertTrue(
+                       $wp_roles->is_role( $role_name ),
+                       "The $role_name role was not created"
+               );
+
+               $this->assertNotInstanceOf(
+                       'WP_Role',
+                       update_role( $role, $display_name, $capabilities ),
+                       "The $role_name role was updated"
+               );
+
+               // Clean up.
+               remove_role( $role_name );
+               $this->flush_roles();
+               $this->assertFalse(
+                       $wp_roles->is_role( $role_name ),
+                       "The $role_name role was not removed"
+               );
+       }
+
+       /**
+        * Data provider.
+        *
+        * @return array
+        */
+       public function data_update_role_unhappy_paths() {
+               return array(
+                       'true as the role'                       => array(
+                               'role'         => true,
+                               'display_name' => 'Janitor',
+                               'capabilities' => array(
+                                       'level_1' => true,
+                               ),
+                       ),
+                       'false as the role'                      => array(
+                               'role'         => false,
+                               'display_name' => 'Janitor',
+                               'capabilities' => array(
+                                       'level_1' => true,
+                               ),
+                       ),
+                       'null as the role'                       => array(
+                               'role'         => null,
+                               'display_name' => 'Janitor',
+                               'capabilities' => array(
+                                       'level_1' => true,
+                               ),
+                       ),
+                       '(int) 1 as the role'                    => array(
+                               'role'         => 1,
+                               'display_name' => 'Janitor',
+                               'capabilities' => array(
+                                       'level_1' => true,
+                               ),
+                       ),
+                       '(float) 1.0 as the role'                => array(
+                               'role'         => 1.0,
+                               'display_name' => 'Janitor',
+                               'capabilities' => array(
+                                       'level_1' => true,
+                               ),
+                       ),
+                       '(int) 0 as the role'                    => array(
+                               'role'         => 0,
+                               'display_name' => 'Janitor',
+                               'capabilities' => array(
+                                       'level_1' => true,
+                               ),
+                       ),
+                       '(float) 0.0 as the role'                => array(
+                               'role'         => 0.0,
+                               'display_name' => 'Janitor',
+                               'capabilities' => array(
+                                       'level_1' => true,
+                               ),
+                       ),
+                       'an empty string as the role'            => array(
+                               'role'         => '',
+                               'display_name' => 'Janitor',
+                               'capabilities' => array(
+                                       'level_1' => true,
+                               ),
+                       ),
+                       'a string with only a space as the role' => array(
+                               'role'         => ' ',
+                               'display_name' => 'Janitor',
+                               'capabilities' => array(
+                                       'level_1' => true,
+                               ),
+                       ),
+                       'an empty array as the role'             => array(
+                               'role'         => array(),
+                               'display_name' => 'Janitor',
+                               'capabilities' => array(
+                                       'level_1' => true,
+                               ),
+                       ),
+                       'a non-empty array as the role'          => array(
+                               'role'         => array( 'janitor' ),
+                               'display_name' => 'Janitor',
+                               'capabilities' => array(
+                                       'level_1' => true,
+                               ),
+                       ),
+                       'an object as the role'                  => array(
+                               'role'         => (object) array( 'janitor' ),
+                               'display_name' => 'Janitor',
+                               'capabilities' => array(
+                                       'level_1' => true,
+                               ),
+                       ),
+                       'true as the display name'               => array(
+                               'role'         => 'janitor',
+                               'display_name' => true,
+                               'capabilities' => array(
+                                       'level_1' => true,
+                               ),
+                       ),
+                       'false as the display name'              => array(
+                               'role'         => 'janitor',
+                               'display_name' => false,
+                               'capabilities' => array(
+                                       'level_1' => true,
+                               ),
+                       ),
+                       '(int) 1 as the display name'            => array(
+                               'role'         => 'janitor',
+                               'display_name' => 1,
+                               'capabilities' => array(
+                                       'level_1' => true,
+                               ),
+                       ),
+                       '(float) 1.0 as the display name'        => array(
+                               'role'         => 'janitor',
+                               'display_name' => 1.0,
+                               'capabilities' => array(
+                                       'level_1' => true,
+                               ),
+                       ),
+                       '(int) 0 as the display name'            => array(
+                               'role'         => 'janitor',
+                               'display_name' => 0,
+                               'capabilities' => array(
+                                       'level_1' => true,
+                               ),
+                       ),
+                       '(float) 0.0 as the display name'        => array(
+                               'role'         => 'janitor',
+                               'display_name' => 0.0,
+                               'capabilities' => array(
+                                       'level_1' => true,
+                               ),
+                       ),
+                       'an empty string as the display name'    => array(
+                               'role'         => 'janitor',
+                               'display_name' => '',
+                               'capabilities' => array(
+                                       'level_1' => true,
+                               ),
+                       ),
+                       'a string with only a space as the display name' => array(
+                               'role'         => 'janitor',
+                               'display_name' => ' ',
+                               'capabilities' => array(
+                                       'level_1' => true,
+                               ),
+                       ),
+                       'an empty array as the display name'     => array(
+                               'role'         => 'janitor',
+                               'display_name' => array(),
+                               'capabilities' => array(
+                                       'level_1' => true,
+                               ),
+                       ),
+                       'a non-empty array as the display name'  => array(
+                               'role'         => 'janitor',
+                               'display_name' => array( 'Janitor' ),
+                               'capabilities' => array(
+                                       'level_1' => true,
+                               ),
+                       ),
+                       'an object as the display name'          => array(
+                               'role'         => 'janitor',
+                               'display_name' => (object) array( 'Janitor' ),
+                               'capabilities' => array(
+                                       'level_1' => true,
+                               ),
+                       ),
+                       'true as the capabilities'               => array(
+                               'role'         => 'janitor',
+                               'display_name' => 'Janitor',
+                               'capabilities' => true,
+                       ),
+                       'false as the capabilities'              => array(
+                               'role'         => (object) array( 'janitor' ),
+                               'display_name' => 'Janitor',
+                               'capabilities' => false,
+                       ),
+                       '(int) 1 as the capabilities'            => array(
+                               'role'         => 'janitor',
+                               'display_name' => 'Janitor',
+                               'capabilities' => 1,
+                       ),
+                       '(float) 1.0 as the capabilities'        => array(
+                               'role'         => 'janitor',
+                               'display_name' => 'Janitor',
+                               'capabilities' => 1.0,
+                       ),
+                       '(int) 0 as the capabilities'            => array(
+                               'role'         => 'janitor',
+                               'display_name' => 'Janitor',
+                               'capabilities' => 0,
+                       ),
+                       '(float) 0.0 as the capabilities'        => array(
+                               'role'         => 'janitor',
+                               'display_name' => 'Janitor',
+                               'capabilities' => 0.0,
+                       ),
+                       'an empty string as the capabilities'    => array(
+                               'role'         => 'janitor',
+                               'display_name' => 'Janitor',
+                               'capabilities' => '',
+                       ),
+                       'a string with only a space as the capabilities' => array(
+                               'role'         => 'janitor',
+                               'display_name' => 'Janitor',
+                               'capabilities' => ' ',
+                       ),
+                       'an object as the capabilities'          => array(
+                               'role'         => 'janitor',
+                               'display_name' => 'Janitor',
+                               'capabilities' => (object) array( 'level_1' => true ),
+                       ),
+               );
+       }
+
+       /**
+        * @ticket 54572
+        *
+        * @covers WP_Roles::update_role
+        * @covers ::update_role
+        */
+       public function test_update_role_should_create_a_role_if_it_does_not_exist() {
+               global $wp_roles;
+
+               // Create role if it does not exist.
+               $role_name     = 'janitor';
+               $expected_caps = array(
+                       'edit_posts' => true,
+                       'edit_pages' => true,
+                       'level_0'    => true,
+                       'level_1'    => true,
+                       'level_2'    => true,
+               );
+               update_role( $role_name, 'Janitor', $expected_caps );
+               $this->flush_roles();
+               $this->assertTrue( $wp_roles->is_role( $role_name ) );
+       }
+
+       /**
+        * @ticket 54572
+        *
+        * @covers WP_Roles::update_role
+        * @covers ::update_role
+        */
+       public function test_update_role_should_change_the_display_name_of_a_role() {
+               global $wp_roles;
+               $role_name = 'janitor';
+
+               add_role( $role_name, 'Janitor', array( 'level_1' => true ) );
+               $this->flush_roles();
+
+               $expected_display_name = 'Janitor Executive';
+               update_role( $role_name, $expected_display_name );
+               $this->flush_roles();
+
+               $this->assertSame(
+                       $expected_display_name,
+                       $wp_roles->roles[ $role_name ]['name'],
+                       'The expected display name was not correct'
+               );
+               $this->assertSame(
+                       array( 'level_1' => true ),
+                       $wp_roles->roles[ $role_name ]['capabilities'],
+                       'The expected capabilities were not correct'
+               );
+
+               // Clean up.
+               remove_role( $role_name );
+               $this->flush_roles();
+               $this->assertFalse(
+                       $wp_roles->is_role( $role_name ),
+                       "The $role_name role was not removed"
+               );
+       }
+
+       /**
+        * @dataProvider data_update_role_should_change_the_capabilities_of_a_role
+        *
+        * @ticket 54572
+        *
+        * @covers WP_Roles::update_role
+        * @covers ::update_role
+        *
+        * @param array $capabilities An array of capabilities.
+        */
+       public function test_update_role_should_change_the_capabilities_of_a_role( $capabilities ) {
+               global $wp_roles;
+               $role_name = 'janitor';
+
+               add_role( $role_name, 'Janitor', array( 'level_1' => true ) );
+               $this->flush_roles();
+
+               update_role( $role_name, null, $capabilities );
+               $this->flush_roles();
+
+               $this->assertSame(
+                       'Janitor',
+                       $wp_roles->roles[ $role_name ]['name'],
+                       'The display name was changed'
+               );
+               $this->assertSame(
+                       $capabilities,
+                       $wp_roles->roles[ $role_name ]['capabilities'],
+                       'The expected capabilities were not correct'
+               );
+
+               // Clean up.
+               remove_role( $role_name );
+               $this->flush_roles();
+               $this->assertFalse(
+                       $wp_roles->is_role( $role_name ),
+                       "The $role_name role was not removed"
+               );
+       }
+
+       /**
+        * Data provider.
+        *
+        * @return array
+        */
+       public function data_update_role_should_change_the_capabilities_of_a_role() {
+               return array(
+                       'an empty array'           => array( 'capabilities' => array() ),
+                       'an array of capabilities' => array( 'capabilities' => array( 'level_2' => true ) ),
+               );
+       }
+
+       /**
+        * @ticket 54572
+        *
+        * @covers WP_Roles::update_role
+        * @covers ::update_role
+        */
+       public function test_update_role_should_change_display_name_and_capabilities_of_role() {
+               global $wp_roles;
+               $role_name = 'janitor';
+
+               add_role( $role_name, 'Janitor', array( 'level_1' => true ) );
+               $this->flush_roles();
+
+               $expected_display_name = 'Janitor Manager';
+               $new_expected_caps     = array(
+                       'level_3' => true,
+                       'level_4' => true,
+               );
+               update_role( $role_name, $expected_display_name, $new_expected_caps );
+               $this->flush_roles();
+
+               $this->assertSame(
+                       $expected_display_name,
+                       $wp_roles->roles[ $role_name ]['name'],
+                       'The expected display name was not correct'
+               );
+
+               $this->assertSame(
+                       $new_expected_caps,
+                       $wp_roles->roles[ $role_name ]['capabilities'],
+                       'The expected capabilities were not correct'
+               );
+
+               // Clean up.
+               remove_role( $role_name );
+               $this->flush_roles();
+               $this->assertFalse(
+                       $wp_roles->is_role( $role_name ),
+                       "The $role_name role was not removed"
+               );
+       }
+
+       /**
+        * @ticket 54572
+        *
+        * @covers WP_Roles::update_role
+        * @covers ::update_role
+        */
+       public function test_update_role_should_change_capabilities_of_a_user() {
+               global $wp_roles;
+               $role_name = 'janitor';
+
+               add_role( $role_name, 'Janitor', array( 'level_1' => true ) );
+               $this->flush_roles();
+
+               // Assign a user to the role.
+               $id = self::factory()->user->create( array( 'role' => $role_name ) );
+
+               // Update empty capabilities.
+               update_role( $role_name, null, array( 'level_2' => true ) );
+               $this->flush_roles();
+
+               $this->assertTrue(
+                       user_can( $id, 'level_2' ),
+                       'The user does not have level_2 capabilities'
+               );
+               $this->assertFalse(
+                       user_can( $id, 'level_1' ),
+                       'The user has level_1 capabilities'
+               );
+
+               // Clean up.
+               remove_role( $role_name );
+               $this->flush_roles();
+               $this->assertFalse(
+                       $wp_roles->is_role( $role_name ),
+                       "The $role_name role was not removed"
+               );
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Change the capabilites associated with a role and make sure the change
</span><span class="cx" style="display: block; padding: 0 10px">         * is reflected in has_cap().
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span></span></pre>
</div>
</div>

</body>
</html>