-
Notifications
You must be signed in to change notification settings - Fork 384
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add trait for mocking admin and super admin user
- Loading branch information
1 parent
0d82ad0
commit de04a69
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' ] ) ); | ||
} | ||
} | ||
} |