<!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>[510] wp-testcase/test_user_capabilities.php: Make sure we have a user object before trying to do anything with it, avoiding misdirection in later assertions.</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, #logmsg > ol { margin-left: 0; 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">
<dt>Revision</dt> <dd><a href="http://unit-tests.trac.wordpress.org/changeset/510">510</a></dd>
<dt>Author</dt> <dd>convissor</dd>
<dt>Date</dt> <dd>2012-02-01 23:07:35 +0000 (Wed, 01 Feb 2012)</dd>
</dl>
<h3>Log Message</h3>
<pre>Make sure we have a user object before trying to do anything with it, avoiding misdirection in later assertions.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#wptestcasetest_user_capabilitiesphp">wp-testcase/test_user_capabilities.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="wptestcasetest_user_capabilitiesphp"></a>
<div class="modfile"><h4>Modified: wp-testcase/test_user_capabilities.php (509 => 510)</h4>
<pre class="diff"><span>
<span class="info">--- wp-testcase/test_user_capabilities.php        2012-02-01 22:48:03 UTC (rev 509)
+++ wp-testcase/test_user_capabilities.php        2012-02-01 23:07:35 UTC (rev 510)
</span><span class="lines">@@ -46,6 +46,7 @@
</span><span class="cx">         function test_user_administrator() {
</span><span class="cx">                 $id = $this->_make_user('administrator');
</span><span class="cx">                 $user = new WP_User($id);
</span><ins>+                $this->assertInstanceOf('WP_User', $user, "Problem getting user $id");
</ins><span class="cx">
</span><span class="cx">                 // make sure the role name is correct
</span><span class="cx">                 $this->assertEquals(array('administrator'), $user->roles);
</span><span class="lines">@@ -60,6 +61,7 @@
</span><span class="cx">         function test_user_editor() {
</span><span class="cx">                 $id = $this->_make_user('editor');
</span><span class="cx">                 $user = new WP_User($id);
</span><ins>+                $this->assertInstanceOf('WP_User', $user, "Problem getting user $id");
</ins><span class="cx">
</span><span class="cx">                 // make sure the role name is correct
</span><span class="cx">                 $this->assertEquals(array('editor'), $user->roles);
</span><span class="lines">@@ -79,6 +81,7 @@
</span><span class="cx">         function test_user_author() {
</span><span class="cx">                 $id = $this->_make_user('author');
</span><span class="cx">                 $user = new WP_User($id);
</span><ins>+                $this->assertInstanceOf('WP_User', $user, "Problem getting user $id");
</ins><span class="cx">
</span><span class="cx">                 // make sure the role name is correct
</span><span class="cx">                 $this->assertEquals(array('author'), $user->roles);
</span><span class="lines">@@ -98,6 +101,7 @@
</span><span class="cx">         function test_user_contributor() {
</span><span class="cx">                 $id = $this->_make_user('contributor');
</span><span class="cx">                 $user = new WP_User($id);
</span><ins>+                $this->assertInstanceOf('WP_User', $user, "Problem getting user $id");
</ins><span class="cx">
</span><span class="cx">                 // make sure the role name is correct
</span><span class="cx">                 $this->assertEquals(array('contributor'), $user->roles);
</span><span class="lines">@@ -117,6 +121,7 @@
</span><span class="cx">         function test_user_subscriber() {
</span><span class="cx">                 $id = $this->_make_user('subscriber');
</span><span class="cx">                 $user = new WP_User($id);
</span><ins>+                $this->assertInstanceOf('WP_User', $user, "Problem getting user $id");
</ins><span class="cx">
</span><span class="cx">                 // make sure the role name is correct
</span><span class="cx">                 $this->assertEquals(array('subscriber'), $user->roles);
</span><span class="lines">@@ -136,6 +141,7 @@
</span><span class="cx">                 _disable_wp_die();
</span><span class="cx">                 $id = $this->_make_user(rand_str());
</span><span class="cx">                 $user = new WP_User($id);
</span><ins>+                $this->assertInstanceOf('WP_User', $user, "Problem getting user $id");
</ins><span class="cx">
</span><span class="cx">                 // user has no role and no capabilities
</span><span class="cx">                 $this->assertEquals(array(), $user->roles);
</span><span class="lines">@@ -147,11 +153,13 @@
</span><span class="cx">         function test_user_subscriber_contributor() {
</span><span class="cx">                 $id = $this->_make_user('subscriber');
</span><span class="cx">                 $user = new WP_User($id);
</span><ins>+                $this->assertInstanceOf('WP_User', $user, "Problem getting user $id");
</ins><span class="cx">                 $user->add_role('contributor');
</span><span class="cx">
</span><span class="cx">                 // nuke and re-fetch the object to make sure it was stored
</span><span class="cx">                 $user = NULL;
</span><span class="cx">                 $user = new WP_User($id);
</span><ins>+                $this->assertInstanceOf('WP_User', $user, "Problem getting user $id");
</ins><span class="cx">
</span><span class="cx">                 // user should have two roles now
</span><span class="cx">                 $this->assertEquals(array('subscriber', 'contributor'), $user->roles);
</span><span class="lines">@@ -180,6 +188,7 @@
</span><span class="cx">                 $id = $this->_make_user($role_name);
</span><span class="cx">
</span><span class="cx">                 $user = new WP_User($id);
</span><ins>+                $this->assertInstanceOf('WP_User', $user, "Problem getting user $id");
</ins><span class="cx">
</span><span class="cx">                 $this->assertEquals(array($role_name), $user->roles);
</span><span class="cx">
</span><span class="lines">@@ -208,6 +217,7 @@
</span><span class="cx">                 $id = $this->_make_user($role_name);
</span><span class="cx">
</span><span class="cx">                 $user = new WP_User($id);
</span><ins>+                $this->assertInstanceOf('WP_User', $user, "Problem getting user $id");
</ins><span class="cx">
</span><span class="cx">                 $this->assertEquals(array($role_name), $user->roles);
</span><span class="cx">
</span><span class="lines">@@ -248,6 +258,7 @@
</span><span class="cx">                 $this->_flush_roles();
</span><span class="cx">
</span><span class="cx">                 $user = new WP_User($id);
</span><ins>+                $this->assertInstanceOf('WP_User', $user, "Problem getting user $id");
</ins><span class="cx">                 $this->assertEquals(array($role_name), $user->roles);
</span><span class="cx">
</span><span class="cx">                 // the user should have all the above caps
</span><span class="lines">@@ -285,6 +296,7 @@
</span><span class="cx">                 $this->_flush_roles();
</span><span class="cx">
</span><span class="cx">                 $user = new WP_User($id);
</span><ins>+                $this->assertInstanceOf('WP_User', $user, "Problem getting user $id");
</ins><span class="cx">                 $this->assertEquals(array($role_name), $user->roles);
</span><span class="cx">
</span><span class="cx">                 // the user should have all the above caps
</span><span class="lines">@@ -311,11 +323,14 @@
</span><span class="cx">
</span><span class="cx">                 // user 1 has an extra capability
</span><span class="cx">                 $user_1 = new WP_User($id_1);
</span><ins>+                $this->assertInstanceOf('WP_User', $user_1, "Problem getting user $id_1");
</ins><span class="cx">                 $user_1->add_cap('publish_posts');
</span><span class="cx">
</span><span class="cx">                 // re-fetch both users from the db
</span><span class="cx">                 $user_1 = new WP_User($id_1);
</span><ins>+                $this->assertInstanceOf('WP_User', $user_1, "Problem getting user $id_1");
</ins><span class="cx">                 $user_2 = new WP_User($id_2);
</span><ins>+                $this->assertInstanceOf('WP_User', $user_2, "Problem getting user $id_2");
</ins><span class="cx">
</span><span class="cx">                 // make sure they're both still contributors
</span><span class="cx">                 $this->assertEquals(array('contributor'), $user_1->roles);
</span><span class="lines">@@ -345,6 +360,7 @@
</span><span class="cx">
</span><span class="cx">                 // user 1 has an extra capability
</span><span class="cx">                 $user_1 = new WP_User($id_1);
</span><ins>+                $this->assertInstanceOf('WP_User', $user_1, "Problem getting user $id_1");
</ins><span class="cx">                 $user_1->add_cap('publish_posts');
</span><span class="cx">
</span><span class="cx">                 // now remove the extra cap
</span><span class="lines">@@ -352,7 +368,9 @@
</span><span class="cx">
</span><span class="cx">                 // re-fetch both users from the db
</span><span class="cx">                 $user_1 = new WP_User($id_1);
</span><ins>+                $this->assertInstanceOf('WP_User', $user_1, "Problem getting user $id_1");
</ins><span class="cx">                 $user_2 = new WP_User($id_2);
</span><ins>+                $this->assertInstanceOf('WP_User', $user_2, "Problem getting user $id_2");
</ins><span class="cx">
</span><span class="cx">                 // make sure they're both still contributors
</span><span class="cx">                 $this->assertEquals(array('contributor'), $user_1->roles);
</span><span class="lines">@@ -370,6 +388,7 @@
</span><span class="cx">                 // user starts as an author
</span><span class="cx">                 $id = $this->_make_user('author');
</span><span class="cx">                 $user = new WP_User($id);
</span><ins>+                $this->assertInstanceOf('WP_User', $user, "Problem getting user $id");
</ins><span class="cx">
</span><span class="cx">                 // author = user level 2
</span><span class="cx">                 $this->assertEquals( 2, $user->user_level );
</span><span class="lines">@@ -392,6 +411,7 @@
</span><span class="cx">                 // user starts as an author
</span><span class="cx">                 $id = $this->_make_user('author');
</span><span class="cx">                 $user = new WP_User($id);
</span><ins>+                $this->assertInstanceOf('WP_User', $user, "Problem getting user $id");
</ins><span class="cx">
</span><span class="cx">                 // add some extra capabilities
</span><span class="cx">                 $user->add_cap('make_coffee');
</span><span class="lines">@@ -399,6 +419,7 @@
</span><span class="cx">
</span><span class="cx">                 // re-fetch
</span><span class="cx">                 $user = new WP_User($id);
</span><ins>+                $this->assertInstanceOf('WP_User', $user, "Problem getting user $id");
</ins><span class="cx">
</span><span class="cx">                 $this->assertTrue($user->has_cap('make_coffee'));
</span><span class="cx">                 $this->assertTrue($user->has_cap('drink_coffee'));
</span><span class="lines">@@ -408,6 +429,7 @@
</span><span class="cx">
</span><span class="cx">                 // re-fetch
</span><span class="cx">                 $user = new WP_User($id);
</span><ins>+                $this->assertInstanceOf('WP_User', $user, "Problem getting user $id");
</ins><span class="cx">
</span><span class="cx">                 // capabilities for the author role should be gone
</span><span class="cx"> #                $this->assertFalse($user->has_cap('edit_posts'));
</span><span class="lines">@@ -434,6 +456,7 @@
</span><span class="cx">
</span><span class="cx">                 // the author of the post
</span><span class="cx">                 $author = new WP_User($this->author->ID);
</span><ins>+                $this->assertInstanceOf('WP_User', $author, "Problem getting user " . $this->author->ID);
</ins><span class="cx">                 $author->set_role('author');
</span><span class="cx">
</span><span class="cx">                 // add some other users
</span><span class="lines">@@ -493,6 +516,7 @@
</span><span class="cx">
</span><span class="cx">                 // the author of the page
</span><span class="cx">                 $author = new WP_User($this->author->ID);
</span><ins>+                $this->assertInstanceOf('WP_User', $author, "Problem getting user " . $this->author->ID);
</ins><span class="cx">                 $author->set_role('author');
</span><span class="cx">
</span><span class="cx">                 // add some other users
</span><span class="lines">@@ -526,6 +550,7 @@
</span><span class="cx">
</span><span class="cx">                 $id = $this->_make_user('author');
</span><span class="cx">                 $user = new WP_User($id);
</span><ins>+                $this->assertInstanceOf('WP_User', $user, "Problem getting user $id");
</ins><span class="cx">
</span><span class="cx">                 global $wpdb;
</span><span class="cx">                 if (!empty($wpdb->user_role))
</span><span class="lines">@@ -534,6 +559,7 @@
</span><span class="cx">                 update_usermeta($id, $user->cap_key, array('editor' => true));
</span><span class="cx">
</span><span class="cx">                 $user = new WP_User($id);
</span><ins>+                $this->assertInstanceOf('WP_User', $user, "Problem getting user $id");
</ins><span class="cx">
</span><span class="cx">                 // check a few of the main capabilities
</span><span class="cx">                 $this->assertEquals(array('editor'), $user->roles);
</span><span class="lines">@@ -567,6 +593,7 @@
</span><span class="cx">
</span><span class="cx">                 // regular users
</span><span class="cx">                 $user = new WP_User($id[0]);
</span><ins>+                $this->assertInstanceOf('WP_User', $user, "Problem getting user $id[0]");
</ins><span class="cx">                 update_usermeta($id[0], $user->cap_key, array('administrator' => true));
</span><span class="cx">                 update_usermeta($id[1], $user->cap_key, array('editor' => true));
</span><span class="cx">                 update_usermeta($id[2], $user->cap_key, array('subscriber' => true));
</span><span class="lines">@@ -584,6 +611,7 @@
</span><span class="cx">
</span><span class="cx">                 // test each user's role and capabilities, and make sure the usermeta data is what we expect
</span><span class="cx">                 $user_0 = new WP_User($id[0]);
</span><ins>+                $this->assertInstanceOf('WP_User', $user_0, "Problem getting user $id[0]");
</ins><span class="cx">                 $this->assertEquals(array('administrator'), $user_0->roles);
</span><span class="cx">                 $this->assertTrue($user_0->has_cap('switch_themes'));
</span><span class="cx">                 $this->assertTrue($user_0->has_cap('edit_users'));
</span><span class="lines">@@ -593,6 +621,7 @@
</span><span class="cx">                 $this->assertEquals( array(), $old_caps );
</span><span class="cx">
</span><span class="cx">                 $user_1 = new WP_User($id[1]);
</span><ins>+                $this->assertInstanceOf('WP_User', $user_1, "Problem getting user $id[1]");
</ins><span class="cx">                 $this->assertEquals(array('editor'), $user_1->roles);
</span><span class="cx">                 $this->assertTrue($user_1->has_cap('moderate_comments'));
</span><span class="cx">                 $this->assertTrue($user_1->has_cap('manage_categories'));
</span><span class="lines">@@ -605,6 +634,7 @@
</span><span class="cx">                 $this->assertEquals( array(), $old_caps );
</span><span class="cx">
</span><span class="cx">                 $user_2 = new WP_User($id[2]);
</span><ins>+                $this->assertInstanceOf('WP_User', $user_2, "Problem getting user $id[2]");
</ins><span class="cx">                 $this->assertEquals(array('subscriber'), $user_2->roles);
</span><span class="cx">                 $this->assertTrue($user_2->has_cap('read'));
</span><span class="cx">                 $this->assertTrue($user_2->has_cap('level_0'));
</span><span class="lines">@@ -616,6 +646,7 @@
</span><span class="cx">
</span><span class="cx">                 // user 3 has two roles
</span><span class="cx">                 $user_3 = new WP_User($id[3]);
</span><ins>+                $this->assertInstanceOf('WP_User', $user_3, "Problem getting user $id[3]");
</ins><span class="cx">                 $this->assertEquals(array('contributor', 'author'), $user_3->roles);
</span><span class="cx">                 $this->assertTrue($user_3->has_cap('edit_posts'));
</span><span class="cx">                 $this->assertTrue($user_3->has_cap('edit_published_posts'));
</span><span class="lines">@@ -629,6 +660,7 @@
</span><span class="cx">
</span><span class="cx">                 // user 4 is a subscriber with some extra per-user caps
</span><span class="cx">                 $user_4 = new WP_User($id[4]);
</span><ins>+                $this->assertInstanceOf('WP_User', $user_4, "Problem getting user $id[4]");
</ins><span class="cx">                 $this->assertEquals(array('subscriber'), $user_4->roles);
</span><span class="cx">                 $this->assertTrue($user_4->has_cap('read'));
</span><span class="cx">                 $this->assertTrue($user_4->has_cap('level_0'));
</span></span></pre>
</div>
</div>
</body>
</html>