Skip to content

Commit

Permalink
Add trait for mocking admin and super admin user
Browse files Browse the repository at this point in the history
  • Loading branch information
thelovekesh committed Nov 23, 2022
1 parent 0d82ad0 commit de04a69
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions tests/php/src/Helpers/MockAdminUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* Trait MockAdminUser.
*
* @package AmpProject\AmpWP
*/

namespace AmpProject\AmpWP\Tests\Helpers;

/**
* Helper trait for mocking an user:
* - Admin user in single site.
* - Super admin user in multisite.
*
* @package AmpProject\AmpWP
*/
trait MockAdminUser {

/**
* Mock an admin or super admin user.
*/
public function mock_admin_user() {
if ( is_multisite() ) {
$user_id = self::factory()->user->create(
[
'role' => 'administrator',
]
);

grant_super_admin( $user_id );
wp_set_current_user( $user_id );
} else {
wp_set_current_user( self::factory()->user->create( [ 'role' => 'administrator' ] ) );
}
}
}

0 comments on commit de04a69

Please sign in to comment.