Skip to content

Commit

Permalink
Minor test helper method refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
joshcanhelp committed Dec 6, 2018
1 parent 9b4b7ec commit f2e75c2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 24 deletions.
12 changes: 6 additions & 6 deletions tests/testProfileChangeEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function testThatHooksAreLoaded() {
* Test that an email update works.
*/
public function testSuccessfulEmailUpdate() {
$user = $this->createUser( null, null, false );
$user = $this->createUser( [], false );
$new_email = $user->data->user_email;
$old_user = clone $user;
$old_user->data->user_email = 'OLD-' . $new_email;
Expand All @@ -88,7 +88,7 @@ public function testSuccessfulEmailUpdate() {
* Test that a non-Auth0 user will skip the email update.
*/
public function testThatNonAuth0UserSkipsUpdate() {
$user = $this->createUser( null, null, false );
$user = $this->createUser( [], false );
$old_user = clone $user;
$old_user->data->user_email = 'OLD-' . $old_user->data->user_email;

Expand All @@ -102,7 +102,7 @@ public function testThatNonAuth0UserSkipsUpdate() {
* Test that a non-DB strategy user will skip the email update.
*/
public function testThatNonDbUserSkipsUpdate() {
$user = $this->createUser( null, null, false );
$user = $this->createUser( [], false );
$old_user = clone $user;
$old_user->data->user_email = 'OLD-' . $old_user->data->user_email;

Expand All @@ -119,7 +119,7 @@ public function testThatNonDbUserSkipsUpdate() {
* Test that a user change without an email update will skip the email update.
*/
public function testThatSameEmailSkipsUpdate() {
$user = $this->createUser( null, null, false );
$user = $this->createUser( [], false );
$old_user = clone $user;

// API call mocked to succeed.
Expand All @@ -135,7 +135,7 @@ public function testThatSameEmailSkipsUpdate() {
* Test that a failed API call changes the email address back.
*/
public function testThatFailedApiCallStopsEmailUpdate() {
$user = $this->createUser( null, null, false );
$user = $this->createUser( [], false );
$old_user = clone $user;
$old_user->data->user_email = 'OLD-' . $old_user->data->user_email;

Expand Down Expand Up @@ -163,7 +163,7 @@ public function testThatFailedApiCallStopsEmailUpdate() {
public function testThatFailedApiRedirectsOnUserEditPage() {
$this->startRedirectHalting();

$user = $this->createUser( null, null, false );
$user = $this->createUser( [], false );
$old_user = clone $user;
$old_user->data->user_email = 'OLD-' . $old_user->data->user_email;

Expand Down
7 changes: 6 additions & 1 deletion tests/testRoutesLogin.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,12 @@ public function testThatLoginRouteReturnsUserIfSuccessful() {
$token_id = '__test_token_id__';
$_POST['username'] = uniqid() . '@' . uniqid() . '.com';
$_POST['password'] = uniqid();
$user = $this->createUser( $_POST['username'], $_POST['password'] );
$user = $this->createUser(
[
'user_email' => $_POST['username'],
'user_pass' => $_POST['password'],
]
);
self::$opts->set( 'migration_ws', 1 );
self::$opts->set( 'client_secret', $client_secret );
self::$opts->set( 'migration_token_id', $token_id );
Expand Down
10 changes: 5 additions & 5 deletions tests/testUserRepoCreate.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static function setUpBeforeClass() {
*/
public function testRequiredEmailRejected() {
$userinfo = $this->getUserinfo( 'auth0' );
$this->createUser( $userinfo->email );
$this->createUser( [ 'user_email' => $userinfo->email ] );

// Require a verified email.
self::$opts->set( 'requires_verified_email', 1 );
Expand All @@ -75,7 +75,7 @@ public function testRequiredEmailRejected() {
*/
public function testUserAlreadyExistsRejected() {
$userinfo = $this->getUserinfo();
$user = $this->createUser( $userinfo->email );
$user = $this->createUser( [ 'user_email' => $userinfo->email ] );
self::$repo->update_auth0_object( $user->ID, $userinfo );

// Require a verified email.
Expand Down Expand Up @@ -160,7 +160,7 @@ function() {
*/
public function testJoinUserEmailVerificationOff() {
$userinfo = $this->getUserinfo();
$user = $this->createUser( $userinfo->email );
$user = $this->createUser( [ 'user_email' => $userinfo->email ] );

// Require a verified email.
self::$opts->set( 'requires_verified_email', 0 );
Expand All @@ -178,7 +178,7 @@ public function testJoinUserEmailVerificationOff() {
*/
public function testJoinUserEmailVerified() {
$userinfo = $this->getUserinfo();
$user = $this->createUser( $userinfo->email );
$user = $this->createUser( [ 'user_email' => $userinfo->email ] );

// Require a verified email.
self::$opts->set( 'requires_verified_email', 1 );
Expand All @@ -199,7 +199,7 @@ public function testJoinUserEmailVerified() {
*/
public function testJoinUserSkipStrategy() {
$userinfo = $this->getUserinfo( 'auth0' );
$user = $this->createUser( $userinfo->email );
$user = $this->createUser( [ 'user_email' => $userinfo->email ] );

// Require a verified email.
self::$opts->set( 'requires_verified_email', 1 );
Expand Down
22 changes: 10 additions & 12 deletions tests/traits/usersHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,19 @@ trait UsersHelper {
/**
* Create a new User.
*
* @param null $email - Email to use, default is used if none provided.
* @param null|string $password - Password to use, default is used if empty.
* @param bool $should_return_data - True to return data only, false to return WP_User.
* @param array $user_data - User data to use.
* @param bool $should_return_data - True to return data only, false to return WP_User.
*
* @return null|object|stdClass|WP_User
*/
public function createUser( $email = null, $password = null, $should_return_data = true ) {
$username = 'test_new_user' . uniqid();
$user_id = wp_insert_user(
[
'user_login' => $username,
'user_email' => $email ? $email : $username . '@example.com',
'user_pass' => $password ? $password : uniqid(),
]
);
public function createUser( array $user_data = [], $should_return_data = true ) {
$username = 'test_new_user' . uniqid();
$default_data = [
'user_login' => 'test_new_user' . uniqid(),
'user_email' => $username . '@example.com',
'user_pass' => uniqid() . uniqid() . uniqid(),
];
$user_id = wp_insert_user( array_merge( $default_data, $user_data ) );

if ( is_wp_error( $user_id ) ) {
return null;
Expand Down

0 comments on commit f2e75c2

Please sign in to comment.