<!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>[32812] trunk/tests/phpunit/tests/user/capabilities.php: Comprehensive unit tests for every available capability, and for all roles.</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 { 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/32812">32812</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/32812","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>johnbillion</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2015-06-17 00:04:08 +0000 (Wed, 17 Jun 2015)</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'>Comprehensive unit tests for every available capability, and for all roles.
Fixes <a href="https://core.trac.wordpress.org/ticket/32394">#32394</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestsusercapabilitiesphp">trunk/tests/phpunit/tests/user/capabilities.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<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 2015-06-17 00:02:08 UTC (rev 32811)
+++ trunk/tests/phpunit/tests/user/capabilities.php 2015-06-17 00:04:08 UTC (rev 32812)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -38,204 +38,389 @@
</span><span class="cx" style="display: block; padding: 0 10px"> return $meta_value;
</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">- // test the default roles
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ protected function _getSingleSiteCaps() {
+ return array(
</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_user_administrator() {
- $id = $this->factory->user->create( array( 'role' => 'administrator' ) );
- $user = new WP_User($id);
- $this->assertTrue($user->exists(), "Problem getting user $id");
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'unfiltered_html' => array( 'administrator', 'editor' ),
</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->assertEquals(array('administrator'), $user->roles);
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'manage_network' => array(),
+ 'manage_sites' => array(),
+ 'manage_network_users' => array(),
+ 'manage_network_plugins' => array(),
+ 'manage_network_themes' => array(),
+ 'manage_network_options' => array(),
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // check a few of the main capabilities
- $this->assertTrue($user->has_cap('switch_themes'));
- $this->assertTrue($user->has_cap('list_users'));
- $this->assertTrue($user->has_cap('manage_options'));
- $this->assertTrue($user->has_cap('level_10'));
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'activate_plugins' => array( 'administrator' ),
+ 'create_users' => array( 'administrator' ),
+ 'delete_plugins' => array( 'administrator' ),
+ 'delete_themes' => array( 'administrator' ),
+ 'delete_users' => array( 'administrator' ),
+ 'edit_files' => array( 'administrator' ),
+ 'edit_plugins' => array( 'administrator' ),
+ 'edit_themes' => array( 'administrator' ),
+ 'edit_users' => array( 'administrator' ),
+ 'install_plugins' => array( 'administrator' ),
+ 'install_themes' => array( 'administrator' ),
+ 'update_core' => array( 'administrator' ),
+ 'update_plugins' => array( 'administrator' ),
+ 'update_themes' => array( 'administrator' ),
+ 'edit_theme_options' => array( 'administrator' ),
+ 'export' => array( 'administrator' ),
+ 'import' => array( 'administrator' ),
+ 'list_users' => array( 'administrator' ),
+ 'manage_options' => array( 'administrator' ),
+ 'promote_users' => array( 'administrator' ),
+ 'remove_users' => array( 'administrator' ),
+ 'switch_themes' => array( 'administrator' ),
+ 'edit_dashboard' => array( 'administrator' ),
+
+ 'moderate_comments' => array( 'administrator', 'editor' ),
+ 'manage_categories' => array( 'administrator', 'editor' ),
+ 'edit_others_posts' => array( 'administrator', 'editor' ),
+ 'edit_pages' => array( 'administrator', 'editor' ),
+ 'edit_others_pages' => array( 'administrator', 'editor' ),
+ 'edit_published_pages' => array( 'administrator', 'editor' ),
+ 'publish_pages' => array( 'administrator', 'editor' ),
+ 'delete_pages' => array( 'administrator', 'editor' ),
+ 'delete_others_posts' => array( 'administrator', 'editor' ),
+ 'delete_published_pages' => array( 'administrator', 'editor' ),
+ 'delete_others_posts' => array( 'administrator', 'editor' ),
+ 'delete_private_posts' => array( 'administrator', 'editor' ),
+ 'edit_private_posts' => array( 'administrator', 'editor' ),
+ 'read_private_posts' => array( 'administrator', 'editor' ),
+ 'delete_private_pages' => array( 'administrator', 'editor' ),
+ 'edit_private_pages' => array( 'administrator', 'editor' ),
+ 'read_private_pages' => array( 'administrator', 'editor' ),
+
+ 'edit_published_posts' => array( 'administrator', 'editor', 'author' ),
+ 'upload_files' => array( 'administrator', 'editor', 'author' ),
+ 'publish_posts' => array( 'administrator', 'editor', 'author' ),
+ 'delete_published_posts' => array( 'administrator', 'editor', 'author' ),
+
+ 'edit_posts' => array( 'administrator', 'editor', 'author', 'contributor' ),
+ 'delete_posts' => array( 'administrator', 'editor', 'author', 'contributor' ),
+
+ 'read' => array( 'administrator', 'editor', 'author', 'contributor', 'subscriber' ),
+
+ 'level_10' => array( 'administrator' ),
+ 'level_9' => array( 'administrator' ),
+ 'level_8' => array( 'administrator' ),
+ 'level_7' => array( 'administrator', 'editor' ),
+ 'level_6' => array( 'administrator', 'editor' ),
+ 'level_5' => array( 'administrator', 'editor' ),
+ 'level_4' => array( 'administrator', 'editor' ),
+ 'level_3' => array( 'administrator', 'editor' ),
+ 'level_2' => array( 'administrator', 'editor', 'author' ),
+ 'level_1' => array( 'administrator', 'editor', 'author', 'contributor' ),
+ 'level_0' => array( 'administrator', 'editor', 'author', 'contributor', 'subscriber' ),
+
+ );
+
</ins><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">- function test_user_editor() {
- $id = $this->factory->user->create( array( 'role' => 'editor' ) );
- $user = new WP_User($id);
- $this->assertTrue($user->exists(), "Problem getting user $id");
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ protected function _getMultiSiteCaps() {
+ return array(
</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->assertEquals(array('editor'), $user->roles);
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'unfiltered_html' => array(),
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // check a few of the main capabilities
- $this->assertTrue($user->has_cap('moderate_comments'));
- $this->assertTrue($user->has_cap('manage_categories'));
- $this->assertTrue($user->has_cap('upload_files'));
- $this->assertTrue($user->has_cap('level_7'));
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'manage_network' => array(),
+ 'manage_sites' => array(),
+ 'manage_network_users' => array(),
+ 'manage_network_plugins' => array(),
+ 'manage_network_themes' => array(),
+ 'manage_network_options' => array(),
+ 'activate_plugins' => array(),
+ 'create_users' => array(),
+ 'delete_plugins' => array(),
+ 'delete_themes' => array(),
+ 'delete_users' => array(),
+ 'edit_files' => array(),
+ 'edit_plugins' => array(),
+ 'edit_themes' => array(),
+ 'edit_users' => array(),
+ 'install_plugins' => array(),
+ 'install_themes' => array(),
+ 'update_core' => array(),
+ 'update_plugins' => array(),
+ 'update_themes' => array(),
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // and a few capabilities this user doesn't have
- $this->assertFalse($user->has_cap('switch_themes'));
- $this->assertFalse($user->has_cap('edit_users'));
- $this->assertFalse($user->has_cap('level_8'));
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'edit_theme_options' => array( 'administrator' ),
+ 'export' => array( 'administrator' ),
+ 'import' => array( 'administrator' ),
+ 'list_users' => array( 'administrator' ),
+ 'manage_options' => array( 'administrator' ),
+ 'promote_users' => array( 'administrator' ),
+ 'remove_users' => array( 'administrator' ),
+ 'switch_themes' => array( 'administrator' ),
+ 'edit_dashboard' => array( 'administrator' ),
+
+ 'moderate_comments' => array( 'administrator', 'editor' ),
+ 'manage_categories' => array( 'administrator', 'editor' ),
+ 'edit_others_posts' => array( 'administrator', 'editor' ),
+ 'edit_pages' => array( 'administrator', 'editor' ),
+ 'edit_others_pages' => array( 'administrator', 'editor' ),
+ 'edit_published_pages' => array( 'administrator', 'editor' ),
+ 'publish_pages' => array( 'administrator', 'editor' ),
+ 'delete_pages' => array( 'administrator', 'editor' ),
+ 'delete_others_posts' => array( 'administrator', 'editor' ),
+ 'delete_published_pages' => array( 'administrator', 'editor' ),
+ 'delete_others_posts' => array( 'administrator', 'editor' ),
+ 'delete_private_posts' => array( 'administrator', 'editor' ),
+ 'edit_private_posts' => array( 'administrator', 'editor' ),
+ 'read_private_posts' => array( 'administrator', 'editor' ),
+ 'delete_private_pages' => array( 'administrator', 'editor' ),
+ 'edit_private_pages' => array( 'administrator', 'editor' ),
+ 'read_private_pages' => array( 'administrator', 'editor' ),
+
+ 'edit_published_posts' => array( 'administrator', 'editor', 'author' ),
+ 'upload_files' => array( 'administrator', 'editor', 'author' ),
+ 'publish_posts' => array( 'administrator', 'editor', 'author' ),
+ 'delete_published_posts' => array( 'administrator', 'editor', 'author' ),
+
+ 'edit_posts' => array( 'administrator', 'editor', 'author', 'contributor' ),
+ 'delete_posts' => array( 'administrator', 'editor', 'author', 'contributor' ),
+
+ 'read' => array( 'administrator', 'editor', 'author', 'contributor', 'subscriber' ),
+
+ 'level_10' => array( 'administrator' ),
+ 'level_9' => array( 'administrator' ),
+ 'level_8' => array( 'administrator' ),
+ 'level_7' => array( 'administrator', 'editor' ),
+ 'level_6' => array( 'administrator', 'editor' ),
+ 'level_5' => array( 'administrator', 'editor' ),
+ 'level_4' => array( 'administrator', 'editor' ),
+ 'level_3' => array( 'administrator', 'editor' ),
+ 'level_2' => array( 'administrator', 'editor', 'author' ),
+ 'level_1' => array( 'administrator', 'editor', 'author', 'contributor' ),
+ 'level_0' => array( 'administrator', 'editor', 'author', 'contributor', 'subscriber' ),
+
+ );
+
</ins><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">- function test_user_author() {
- $id = $this->factory->user->create( array( 'role' => 'author' ) );
- $user = new WP_User($id);
- $this->assertTrue($user->exists(), "Problem getting user $id");
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ protected function getCapsAndRoles() {
+ if ( is_multisite() ) {
+ return $this->_getMultiSiteCaps();
+ } else {
+ return $this->_getSingleSiteCaps();
+ }
+ }
</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->assertEquals(array('author'), $user->roles);
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // test the tests
+ function test_single_and_multisite_cap_tests_match() {
+ $single = $this->_getSingleSiteCaps();
+ $multi = $this->_getMultiSiteCaps();
+ $this->assertEquals( array_keys( $single ), array_keys( $multi ) );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // check a few of the main capabilities
- $this->assertTrue($user->has_cap('edit_posts'));
- $this->assertTrue($user->has_cap('edit_published_posts'));
- $this->assertTrue($user->has_cap('upload_files'));
- $this->assertTrue($user->has_cap('level_2'));
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // test the default roles and caps
+ function test_all_roles_and_caps() {
+ $users = array(
+ 'administrator' => $this->factory->user->create_and_get( array( 'role' => 'administrator' ) ),
+ 'editor' => $this->factory->user->create_and_get( array( 'role' => 'editor' ) ),
+ 'author' => $this->factory->user->create_and_get( array( 'role' => 'author' ) ),
+ 'contributor' => $this->factory->user->create_and_get( array( 'role' => 'contributor' ) ),
+ 'subscriber' => $this->factory->user->create_and_get( array( 'role' => 'subscriber' ) ),
+ );
+ $caps = $this->getCapsAndRoles();
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // and a few capabilities this user doesn't have
- $this->assertFalse($user->has_cap('moderate_comments'));
- $this->assertFalse($user->has_cap('manage_categories'));
- $this->assertFalse($user->has_cap('level_3'));
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ foreach ( $users as $role => $user ) {
+
+ // make sure the user is valid
+ $this->assertTrue( $user->exists(), "User with {$role} role does not exist" );
+
+ // make sure the role name is correct
+ $this->assertEquals( array( $role ), $user->roles, "User should only have the {$role} role" );
+
+ foreach ( $caps as $cap => $roles ) {
+ if ( in_array( $role, $roles, true ) ) {
+ $this->assertTrue( $user->has_cap( $cap ), "User with the {$role} role should have the {$cap} capability" );
+ $this->assertTrue( user_can( $user, $cap ), "User with the {$role} role should have the {$cap} capability" );
+ } else {
+ $this->assertFalse( $user->has_cap( $cap ), "User with the {$role} role should not have the {$cap} capability" );
+ $this->assertFalse( user_can( $user, $cap ), "User with the {$role} role should not have the {$cap} capability" );
+ }
+ }
+
+ }
</ins><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">- function test_user_contributor() {
- $id = $this->factory->user->create( array( 'role' => 'contributor' ) );
- $user = new WP_User($id);
- $this->assertTrue($user->exists(), "Problem getting user $id");
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // special case for the link manager
+ function test_link_manager_caps() {
+ $users = array(
+ 'administrator' => $this->factory->user->create_and_get( array( 'role' => 'administrator' ) ),
+ 'editor' => $this->factory->user->create_and_get( array( 'role' => 'editor' ) ),
+ 'author' => $this->factory->user->create_and_get( array( 'role' => 'author' ) ),
+ 'contributor' => $this->factory->user->create_and_get( array( 'role' => 'contributor' ) ),
+ 'subscriber' => $this->factory->user->create_and_get( array( 'role' => 'subscriber' ) ),
+ );
+ $caps = array(
+ 'manage_links' => array( 'administrator', 'editor' ),
+ );
</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->assertEquals(array('contributor'), $user->roles);
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertSame( '0', get_option( 'link_manager_enabled' ) );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // check a few of the main capabilities
- $this->assertTrue($user->has_cap('edit_posts'));
- $this->assertTrue($user->has_cap('read'));
- $this->assertTrue($user->has_cap('level_1'));
- $this->assertTrue($user->has_cap('level_0'));
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // no-one should have access to the link manager by default
+ foreach ( $users as $role => $user ) {
+ foreach ( $caps as $cap => $roles ) {
+ $this->assertFalse( $user->has_cap( $cap ), "User with the {$role} role should not have the {$cap} capability" );
+ $this->assertFalse( user_can( $user, $cap ), "User with the {$role} role should not have the {$cap} capability" );
+ }
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // and a few capabilities this user doesn't have
- $this->assertFalse($user->has_cap('upload_files'));
- $this->assertFalse($user->has_cap('edit_published_posts'));
- $this->assertFalse($user->has_cap('level_2'));
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ update_option( 'link_manager_enabled', '1' );
+ $this->assertSame( '1', get_option( 'link_manager_enabled' ) );
+
+ foreach ( $users as $role => $user ) {
+ foreach ( $caps as $cap => $roles ) {
+ if ( in_array( $role, $roles, true ) ) {
+ $this->assertTrue( $user->has_cap( $cap ), "User with the {$role} role should have the {$cap} capability" );
+ $this->assertTrue( user_can( $user, $cap ), "User with the {$role} role should have the {$cap} capability" );
+ } else {
+ $this->assertFalse( $user->has_cap( $cap ), "User with the {$role} role should not have the {$cap} capability" );
+ $this->assertFalse( user_can( $user, $cap ), "User with the {$role} role should not have the {$cap} capability" );
+ }
+ }
+ }
+
+ update_option( 'link_manager_enabled', '0' );
+ $this->assertSame( '0', get_option( 'link_manager_enabled' ) );
+
</ins><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">- function test_user_subscriber() {
- $id = $this->factory->user->create( array( 'role' => 'subscriber' ) );
- $user = new WP_User($id);
- $this->assertTrue($user->exists(), "Problem getting user $id");
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ function test_super_admin_caps() {
+ if ( ! is_multisite() ) {
+ $this->markTestSkipped( 'Test only runs in multisite' );
+ return;
+ }
+ $caps = $this->getCapsAndRoles();
</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->assertEquals(array('subscriber'), $user->roles);
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $user = $this->factory->user->create_and_get( array( 'role' => 'administrator' ) );
+ grant_super_admin( $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">- // check a few of the main capabilities
- $this->assertTrue($user->has_cap('read'));
- $this->assertTrue($user->has_cap('level_0'));
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertTrue( is_super_admin( $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">- // and a few capabilities this user doesn't have
- $this->assertFalse($user->has_cap('upload_files'));
- $this->assertFalse($user->has_cap('edit_posts'));
- $this->assertFalse($user->has_cap('level_1'));
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ foreach ( $caps as $cap => $roles ) {
+ $this->assertTrue( $user->has_cap( $cap ), "Super Admins should have the {$cap} capability" );
+ $this->assertTrue( user_can( $user, $cap ), "Super Admins should have the {$cap} capability" );
+ }
</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"> // a role that doesn't exist
</span><span class="cx" style="display: block; padding: 0 10px"> function test_bogus_role() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- _disable_wp_die();
- $id = $this->factory->user->create( array( 'role' => rand_str() ) );
- $user = new WP_User($id);
- $this->assertTrue($user->exists(), "Problem getting user $id");
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $user = $this->factory->user->create_and_get( array( 'role' => 'invalid_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">- // user has no role and no capabilities
- $this->assertEquals(array(), $user->roles);
- $this->assertFalse($user->has_cap('level_0'));
- _enable_wp_die();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // make sure the user is valid
+ $this->assertTrue( $user->exists(), "User does not exist" );
+
+ // make sure the role name is correct
+ $this->assertEquals( array(), $user->roles, "User should not have any roles" );
+
+ $caps = $this->getCapsAndRoles();
+
+ foreach ( $caps as $cap => $roles ) {
+ $this->assertFalse( $user->has_cap( $cap ), "User with an invalid role should not have the {$cap} capability" );
+ $this->assertFalse( user_can( $user, $cap ), "User with an invalid role should not have the {$cap} capability" );
+ }
</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"> // a user with multiple roles
</span><span class="cx" style="display: block; padding: 0 10px"> function test_user_subscriber_contributor() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $id = $this->factory->user->create( array( 'role' => 'subscriber' ) );
- $user = new WP_User($id);
- $this->assertTrue($user->exists(), "Problem getting user $id");
- $user->add_role('contributor');
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $user = $this->factory->user->create_and_get( array( 'role' => 'subscriber' ) );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // nuke and re-fetch the object to make sure it was stored
- $user = NULL;
- $user = new WP_User($id);
- $this->assertTrue($user->exists(), "Problem getting user $id");
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // make sure the user is valid
+ $this->assertTrue( $user->exists(), "User does not exist" );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $user->add_role( 'contributor' );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> // user should have two roles now
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertEquals(array('subscriber', 'contributor'), $user->roles);
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertEquals( array( 'subscriber', 'contributor' ), $user->roles );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // with contributor capabilities
- $this->assertTrue($user->has_cap('edit_posts'));
- $this->assertTrue($user->has_cap('read'));
- $this->assertTrue($user->has_cap('level_1'));
- $this->assertTrue($user->has_cap('level_0'));
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $caps = $this->getCapsAndRoles();
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // but not these
- $this->assertFalse($user->has_cap('upload_files'));
- $this->assertFalse($user->has_cap('edit_published_posts'));
- $this->assertFalse($user->has_cap('level_2'));
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ foreach ( $caps as $cap => $roles ) {
+ if ( array_intersect( $user->roles, $roles ) ) {
+ $this->assertTrue( $user->has_cap( $cap ), "User should have the {$cap} capability" );
+ $this->assertTrue( user_can( $user, $cap ), "User should have the {$cap} capability" );
+ } else {
+ $this->assertFalse( $user->has_cap( $cap ), "User should not have the {$cap} capability" );
+ $this->assertFalse( user_can( $user, $cap ), "User should not have the {$cap} capability" );
+ }
+ }
</ins><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">+ // newly added empty role
</ins><span class="cx" style="display: block; padding: 0 10px"> function test_add_empty_role() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // add_role($role, $display_name, $capabilities = '')
- // randomly named role with no capabilities
</del><span class="cx" style="display: block; padding: 0 10px"> global $wp_roles;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $role_name = rand_str();
- add_role($role_name, 'Janitor', array());
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ $role_name = 'janitor';
+ add_role( $role_name, 'Janitor', array() );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->_flush_roles();
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertTrue($wp_roles->is_role($role_name));
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertTrue( $wp_roles->is_role( $role_name ) );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $id = $this->factory->user->create( array( 'role' => $role_name ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $user = $this->factory->user->create_and_get( array( 'role' => $role_name ) );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $user = new WP_User($id);
- $this->assertTrue($user->exists(), "Problem getting user $id");
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // make sure the user is valid
+ $this->assertTrue( $user->exists(), "User 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">- $this->assertEquals(array($role_name), $user->roles);
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // make sure the role name is correct
+ $this->assertEquals( array( $role_name ), $user->roles );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // user shouldn't have any capabilities; test a quick sample
- $this->assertFalse($user->has_cap('upload_files'));
- $this->assertFalse($user->has_cap('edit_published_posts'));
- $this->assertFalse($user->has_cap('level_1'));
- $this->assertFalse($user->has_cap('level_0'));
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $caps = $this->getCapsAndRoles();
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ foreach ( $caps as $cap => $roles ) {
+ $this->assertFalse( $user->has_cap( $cap ), "User should not have the {$cap} capability" );
+ $this->assertFalse( user_can( $user, $cap ), "User should not have the {$cap} capability" );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> // clean up
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- remove_role($role_name);
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ remove_role( $role_name );
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->_flush_roles();
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertFalse($wp_roles->is_role($role_name));
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertFalse( $wp_roles->is_role( $role_name ) );
</ins><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">-
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // newly added role
</ins><span class="cx" style="display: block; padding: 0 10px"> function test_add_role() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // add_role($role, $display_name, $capabilities = '')
- // randomly named role with a few capabilities
</del><span class="cx" style="display: block; padding: 0 10px"> global $wp_roles;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $role_name = rand_str();
- add_role($role_name, 'Janitor', array('edit_posts'=>true, 'edit_pages'=>true, 'level_0'=>true, 'level_1'=>true, 'level_2'=>true));
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ $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 );
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->_flush_roles();
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertTrue($wp_roles->is_role($role_name));
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertTrue( $wp_roles->is_role( $role_name ) );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $id = $this->factory->user->create( array( 'role' => $role_name ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $user = $this->factory->user->create_and_get( array( 'role' => $role_name ) );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $user = new WP_User($id);
- $this->assertTrue($user->exists(), "Problem getting user $id");
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // make sure the user is valid
+ $this->assertTrue( $user->exists(), "User 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">- $this->assertEquals(array($role_name), $user->roles);
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // make sure the role name is correct
+ $this->assertEquals( array( $role_name ), $user->roles );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // the user should have all the above caps
- $this->assertTrue($user->has_cap($role_name));
- $this->assertTrue($user->has_cap('edit_posts'));
- $this->assertTrue($user->has_cap('edit_pages'));
- $this->assertTrue($user->has_cap('level_0'));
- $this->assertTrue($user->has_cap('level_1'));
- $this->assertTrue($user->has_cap('level_2'));
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $caps = $this->getCapsAndRoles();
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // shouldn't have any other caps
- $this->assertFalse($user->has_cap('upload_files'));
- $this->assertFalse($user->has_cap('edit_published_posts'));
- $this->assertFalse($user->has_cap('upload_files'));
- $this->assertFalse($user->has_cap('level_3'));
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ foreach ( $caps as $cap => $roles ) {
+ // the user should have all the above caps
+ if ( isset( $expected_caps[ $cap ] ) ) {
+ $this->assertTrue( $user->has_cap( $cap ), "User should have the {$cap} capability" );
+ $this->assertTrue( user_can( $user, $cap ), "User should have the {$cap} capability" );
+ } else {
+ $this->assertFalse( $user->has_cap( $cap ), "User should not have the {$cap} capability" );
+ $this->assertFalse( user_can( $user, $cap ), "User should not have the {$cap} capability" );
+ }
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // clean up
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- remove_role($role_name);
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ remove_role( $role_name );
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->_flush_roles();
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertFalse($wp_roles->is_role($role_name));
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertFalse( $wp_roles->is_role( $role_name ) );
</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"> function test_role_add_cap() {
</span></span></pre>
</div>
</div>
</body>
</html>