Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Randomize testing user emails and passwords #2255

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions tests/Feature/RouteAccessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ protected function setUp(): void
URL::forceRootUrl('http://localhost');

$this->normal_user = $this->makeNormalUser();
$this->assertDatabaseHas('user', ['email' => 'jane@smith']);
$this->assertDatabaseHas('user', ['email' => $this->normal_user->email]);

$this->admin_user = $this->makeAdminUser();
$this->assertDatabaseHas('user', ['email' => 'admin@user', 'admin' => '1']);
$this->assertDatabaseHas('user', ['email' => $this->admin_user->email, 'admin' => '1']);

$this->public_project = $this->makePublicProject();
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Feature/UniqueEmailsMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public function testUniqueEmailsMigration()
'--force' => true]);

// Create two users with the same email address.
$user1 = $this->makeNormalUser();
$user2 = $this->makeNormalUser();
$user1 = $this->makeNormalUser('Jane', 'Smith', 'jane@smith');
$user2 = $this->makeNormalUser('Jane', 'Smith', 'jane@smith');

// Verify that they both exist and have different ids.
self::assertIsNumeric($user1->id);
Expand Down
83 changes: 61 additions & 22 deletions tests/Traits/CreatesUsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,72 @@
namespace Tests\Traits;

use App\Models\User;
use Illuminate\Support\Str;

trait CreatesUsers
{
public function makeAdminUser() : User
{
// Create an admin user.
$admin = new User();
$admin->firstname = 'Admin';
$admin->lastname = 'User';
$admin->email = 'admin@user';
$admin->password = '45678';
$admin->institution = 'me';
$admin->admin = true;
$admin->save();
return $admin;
/**
* Create an admin user.
*/
public function makeAdminUser(
?string $firstname = null,
?string $lastname = null,
?string $email = null,
?string $password = null,
?string $institution = null
): User {


return $this->makeUser(
$firstname ?? 'Admin',
$lastname ?? 'User',
$email ?? 'admin_' . Str::uuid()->toString() . '@example.com',
$password ?? Str::uuid()->toString(),
$institution ?? 'institution placeholder',
true
);
}

/**
* Create a non-administrator user.
*/
public function makeNormalUser(
?string $firstname = null,
?string $lastname = null,
?string $email = null,
?string $password = null,
?string $institution = null
): User {
return $this->makeUser(
$firstname ?? 'Normal',
$lastname ?? 'User',
$email ?? 'user_' . Str::uuid()->toString() . '@example.com',
$password ?? Str::uuid()->toString(),
$institution ?? 'institution placeholder',
false
);
}

public function makeNormalUser() : User
{
// Create a non-administrator user.
$user = new User();
$user->firstname = 'Jane';
$user->lastname = 'Smith';
$user->email = 'jane@smith';
$user->password = '12345';
$user->institution = 'me';
$user->admin = false;
/**
* Developers are encouraged to use the makeNormalUser() and makeAdminUser() helpers unless
* special functionality is required.
*/
public function makeUser(
string $firstname,
string $lastname,
string $email,
string $password,
string $institution,
bool $admin
): User {
$user = new User([
'firstname' => $firstname,
'lastname' => $lastname,
'email' => $email,
'institution' => $institution,
]);
$user->password = $password;
$user->admin = $admin;
$user->save();
return $user;
}
Expand Down