<!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>[50490] trunk: Roles/Caps: Return same result from `current_user_can` and `user_can()`.</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/50490">50490</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/50490","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>peterwilsoncc</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2021-03-04 00:12:55 +0000 (Thu, 04 Mar 2021)</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'>Roles/Caps: Return same result from `current_user_can` and `user_can()`.
Ensure `current_user_can()` and `user_can()` return the same results for logged out users. For core capabilities this changes `user_can( 0, 'exist' )` to return `true` rather than `false` in line with `current_user_can( 'exist' )` for logged out users.
Convert `current_user_can()` and `current_user_can_for_blog()` to wrapper functions ultimately calling `user_can()`.
Add anonymous user to primitive capability checks as appropriate. Convert `Tests_User_Capabilities::test_other_caps_for_all_roles()` to use a data provider and add tests to check whether user exists in the database (`WP_User::exists()`) as that intentionally differs from the `exist` capability.
Props jjj, johnbillion, peterwilsoncc, SergeyBiryukov, TimothyBlynJacobs.
Fixes <a href="https://core.trac.wordpress.org/ticket/52076">#52076</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludescapabilitiesphp">trunk/src/wp-includes/capabilities.php</a></li>
<li><a href="#trunksrcwpincludesclasswpuserphp">trunk/src/wp-includes/class-wp-user.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 2021-03-03 23:31:26 UTC (rev 50489)
+++ trunk/src/wp-includes/capabilities.php 2021-03-04 00:12:55 UTC (rev 50490)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -679,6 +679,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 2.0.0
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 5.3.0 Formalized the existing and already documented `...$args` parameter
</span><span class="cx" style="display: block; padding: 0 10px"> * by adding it to the function signature.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 5.8.0 Converted to wrapper for the user_can() function.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @see WP_User::has_cap()
</span><span class="cx" style="display: block; padding: 0 10px"> * @see map_meta_cap()
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -689,13 +690,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * passed, whether the current user has the given meta capability for the given object.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function current_user_can( $capability, ...$args ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $current_user = wp_get_current_user();
-
- if ( empty( $current_user ) ) {
- return false;
- }
-
- return $current_user->has_cap( $capability, ...$args );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return user_can( wp_get_current_user(), $capability, ...$args );
</ins><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><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -714,6 +709,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 3.0.0
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 5.3.0 Formalized the existing and already documented `...$args` parameter
</span><span class="cx" style="display: block; padding: 0 10px"> * by adding it to the function signature.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 5.8.0 Wraps current_user_can() after switching to blog.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param int $blog_id Site ID.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $capability Capability name.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -723,17 +719,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> function current_user_can_for_blog( $blog_id, $capability, ...$args ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $switched = is_multisite() ? switch_to_blog( $blog_id ) : false;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $current_user = wp_get_current_user();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $can = current_user_can( $capability, ...$args );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( empty( $current_user ) ) {
- if ( $switched ) {
- restore_current_blog();
- }
- return false;
- }
-
- $can = $current_user->has_cap( $capability, ...$args );
-
</del><span class="cx" style="display: block; padding: 0 10px"> if ( $switched ) {
</span><span class="cx" style="display: block; padding: 0 10px"> restore_current_blog();
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -805,8 +792,10 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $user = get_userdata( $user );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( ! $user || ! $user->exists() ) {
- return false;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( empty( $user ) ) {
+ // User is logged out, create anonymous user object.
+ $user = new WP_User( 0 );
+ $user->init( new stdClass );
</ins><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"> return $user->has_cap( $capability, ...$args );
</span></span></pre></div>
<a id="trunksrcwpincludesclasswpuserphp"></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-user.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-user.php 2021-03-03 23:31:26 UTC (rev 50489)
+++ trunk/src/wp-includes/class-wp-user.php 2021-03-04 00:12:55 UTC (rev 50490)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -167,6 +167,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @param int $site_id Optional. The site ID to initialize for.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function init( $data, $site_id = '' ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! isset( $data->ID ) ) {
+ $data->ID = 0;
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->data = $data;
</span><span class="cx" style="display: block; padding: 0 10px"> $this->ID = (int) $data->ID;
</span><span class="cx" style="display: block; padding: 0 10px">
</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 2021-03-03 23:31:26 UTC (rev 50489)
+++ trunk/tests/phpunit/tests/user/capabilities.php 2021-03-04 00:12:55 UTC (rev 50490)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -12,6 +12,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @var WP_User[] $users
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> protected static $users = array(
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'anonymous' => null,
</ins><span class="cx" style="display: block; padding: 0 10px"> 'administrator' => null,
</span><span class="cx" style="display: block; padding: 0 10px"> 'editor' => null,
</span><span class="cx" style="display: block; padding: 0 10px"> 'author' => null,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -31,6 +32,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
</span><span class="cx" style="display: block; padding: 0 10px"> self::$users = array(
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'anonymous' => new WP_User( 0 ),
</ins><span class="cx" style="display: block; padding: 0 10px"> 'administrator' => $factory->user->create_and_get( array( 'role' => 'administrator' ) ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'editor' => $factory->user->create_and_get( array( 'role' => 'editor' ) ),
</span><span class="cx" style="display: block; padding: 0 10px"> 'author' => $factory->user->create_and_get( array( 'role' => 'author' ) ),
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -342,6 +344,26 @@
</span><span class="cx" style="display: block; padding: 0 10px"> return $data;
</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">+ /**
+ * Data provider for testing a single site install's roles.
+ *
+ * @return array[] {
+ * Arguments for test.
+ *
+ * @type string $role The role to test for.
+ * }
+ */
+ function data_single_site_roles_to_check() {
+ return array(
+ array( 'anonymous' ),
+ array( 'administrator' ),
+ array( 'editor' ),
+ array( 'author' ),
+ array( 'contributor' ),
+ array( 'subscriber' ),
+ );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> protected function getAllCapsAndRoles() {
</span><span class="cx" style="display: block; padding: 0 10px"> return $this->getPrimitiveCapsAndRoles() + $this->getMetaCapsAndRoles();
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -391,10 +413,14 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertFalse( defined( 'ALLOW_UNFILTERED_UPLOADS' ) );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> foreach ( self::$users as $role => $user ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( 'anonymous' === $role ) {
+ // The anonymous role does not exist.
+ $this->assertFalse( $user->exists(), "User with {$role} role should not exist" );
+ } else {
+ // Make sure the user is valid.
+ $this->assertTrue( $user->exists(), "User with {$role} role does not exist" );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Make sure the user is valid.
- $this->assertTrue( $user->exists(), "User with {$role} role does not exist" );
-
</del><span class="cx" style="display: block; padding: 0 10px"> $user_caps = $user->allcaps;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> unset(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -563,24 +589,54 @@
</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"> * Test miscellaneous capabilities of all user roles.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ *
+ * @dataProvider data_single_site_roles_to_check
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- function test_other_caps_for_all_roles() {
- foreach ( self::$users as $role => $user ) {
- // Make sure the user is valid.
- $this->assertTrue( $user->exists(), "User with {$role} role does not exist" );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ function test_other_caps_for_all_roles( $role ) {
+ $user = self::$users[ $role ];
+ $old_id = wp_get_current_user()->ID;
+ wp_set_current_user( $user->ID );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Make sure the role name is correct.
- $this->assertSame( array( $role ), $user->roles, "User should only have the {$role} role" );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Make sure the role name is correct.
+ $expected_roles = array( $role );
+ if ( 'anonymous' === $role ) {
+ // Anonymous role does not exist, user roles should be empty.
+ $expected_roles = array();
+ }
+ $this->assertSame( $expected_roles, $user->roles, "User should only have the {$role} role" );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertFalse( $user->has_cap( 'start_a_fire' ), "User with the {$role} role should not have a custom capability" );
- $this->assertFalse( user_can( $user, 'start_a_fire' ), "User with the {$role} role should not have a custom capability" );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertFalse( $user->has_cap( 'start_a_fire' ), "User with the {$role} role should not have a custom capability (test via WP_User->has_cap() method)." );
+ $this->assertFalse( user_can( $user, 'start_a_fire' ), "User with the {$role} role should not have a custom capability (test by user object)." );
+ $this->assertFalse( user_can( $user->ID, 'start_a_fire' ), "User with the {$role} role should not have a custom capability (test by user ID)." );
+ $this->assertFalse( current_user_can( 'start_a_fire' ), "User with the {$role} role should not have a custom capability (test by current user)." );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertFalse( $user->has_cap( 'do_not_allow' ), "User with the {$role} role should not have the do_not_allow capability" );
- $this->assertFalse( user_can( $user, 'do_not_allow' ), "User with the {$role} role should not have the do_not_allow capability" );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertFalse( $user->has_cap( 'do_not_allow' ), "User with the {$role} role should not have the do_not_allow capability (test via WP_User->has_cap() method)." );
+ $this->assertFalse( user_can( $user, 'do_not_allow' ), "User with the {$role} role should not have the do_not_allow capability (test by user object)." );
+ $this->assertFalse( user_can( $user->ID, 'do_not_allow' ), "User with the {$role} role should not have the do_not_allow capability (test by user ID)." );
+ $this->assertFalse( current_user_can( 'do_not_allow' ), "User with the {$role} role should not have the do_not_allow capability (test by current user)." );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertTrue( $user->has_cap( 'exist' ), "User with the {$role} role should have the exist capability" );
- $this->assertTrue( user_can( $user, 'exist' ), "User with the {$role} role should have the exist capability" );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertTrue( $user->has_cap( 'exist' ), "User with the {$role} role should have the exist capability (test via WP_User->has_cap() method)." );
+ $this->assertTrue( user_can( $user, 'exist' ), "User with the {$role} role should have the exist capability (test by user object)." );
+ $this->assertTrue( user_can( $user->ID, 'exist' ), "User with the {$role} role should have the exist capability (test by user ID)." );
+ $this->assertTrue( current_user_can( 'exist' ), "User with the {$role} role should have the exist capability (test by current user)." );
+
+ wp_set_current_user( $old_id );
+ }
+
+ /**
+ * Test user exists/does not exist as expected.
+ *
+ * @dataProvider data_single_site_roles_to_check
+ */
+ function test_user_exists_in_database( $role ) {
+ $user = self::$users[ $role ];
+ $expected = true;
+
+ if ( 'anonymous' === $role ) {
+ $expected = false;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ $this->assertSame( $expected, $user->exists() );
</ins><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><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -595,6 +651,17 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $user->remove_cap( 'do_not_allow' );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertFalse( $has_cap, "User with the {$role} role should not have the do_not_allow capability" );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ # Test adding the cap via a filter
+ add_filter( 'user_has_cap', array( $this, 'grant_do_not_allow' ), 10, 4 );
+ $has_cap = $user->has_cap( 'do_not_allow' );
+ remove_filter( 'user_has_cap', array( $this, 'grant_do_not_allow' ), 10, 4 );
+ $this->assertFalse( $has_cap, "User with the {$role} role should not have the do_not_allow capability" );
+
+ if ( 'anonymous' === $role ) {
+ // The anonymous role does not exist.
+ continue;
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> # Test adding the cap to the user's role
</span><span class="cx" style="display: block; padding: 0 10px"> $role_obj = get_role( $role );
</span><span class="cx" style="display: block; padding: 0 10px"> $role_obj->add_cap( 'do_not_allow' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -601,13 +668,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $has_cap = $user->has_cap( 'do_not_allow' );
</span><span class="cx" style="display: block; padding: 0 10px"> $role_obj->remove_cap( 'do_not_allow' );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertFalse( $has_cap, "User with the {$role} role should not have the do_not_allow capability" );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
- # Test adding the cap via a filter
- add_filter( 'user_has_cap', array( $this, 'grant_do_not_allow' ), 10, 4 );
- $has_cap = $user->has_cap( 'do_not_allow' );
- remove_filter( 'user_has_cap', array( $this, 'grant_do_not_allow' ), 10, 4 );
- $this->assertFalse( $has_cap, "User with the {$role} role should not have the do_not_allow capability" );
-
</del><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></span></pre>
</div>
</div>
</body>
</html>