[wp-trac] [WordPress Trac] #27205: Unit tests: super admin privledges leak to other tests

WordPress Trac noreply at wordpress.org
Tue Feb 25 14:01:40 UTC 2014


#27205: Unit tests: super admin privledges leak to other tests
------------------------------+-----------------------------
 Reporter:  jdgrimes          |      Owner:
     Type:  defect (bug)      |     Status:  new
 Priority:  normal            |  Milestone:  Awaiting Review
Component:  Build/Test Tools  |    Version:  trunk
 Severity:  normal            |   Keywords:
  Focuses:                    |
------------------------------+-----------------------------
 If you create a user during a test and grant them super admin privileges,
 users created in the following tests will also be super admins.

 Example:

 {{{
 class Example_Super_Admin_Test extends WP_UnitTestCase {

         /**
          * This will work as expected.
          */
         public function test_one() {
                 $user_id = $this->factory->user->create();
                 grant_super_admin( $user_id );
                 $this->assertTrue( is_super_admin( $user_id ) );
         }

         /**
          * This will fail.
          */
         public function test_two() {
                 $user_id = $this->factory->user->create();
                 $this->assertFalse( is_super_admin( $user_id ) );
         }
 }
 }}}

 The reason is because the factory assigns these two users the same
 `user_login`, `'user 1'`. But the `$super_admins` global is not cleared at
 the end of each test, and so once `'user 1'` is added as a super admin in
 the first test, all users created with that login in the following tests
 will also have super admin privileges.

 The solution is to reset the `$super_admins` global in our `tearDown()`,
 or change the behavior of `get_super_admins()` to call the database/cache
 each time instead of relying on the global.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/27205>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list