-
Notifications
You must be signed in to change notification settings - Fork 690
Expand file tree
/
Copy pathUserFactory.php
More file actions
126 lines (107 loc) · 3.39 KB
/
UserFactory.php
File metadata and controls
126 lines (107 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
declare(strict_types=1);
namespace CraftCms\Cms\Database\Factories;
use CraftCms\Cms\Auth\Models\WebAuthn;
use CraftCms\Cms\Support\Facades\Elements;
use CraftCms\Cms\Support\Facades\UserPermissions;
use CraftCms\Cms\User\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\Enumerable;
use Illuminate\Support\Facades\Hash;
use Override;
use RuntimeException;
class UserFactory extends Factory
{
#[Override]
protected $model = User::class;
#[Override]
public function definition(): array
{
return [
'id' => null,
'fullName' => $this->faker->name(),
'firstName' => $this->faker->firstName(),
'lastName' => $this->faker->lastName(),
'username' => $this->faker->userName(),
'email' => $this->faker->email(),
'password' => Hash::make('password'),
'active' => true,
'pending' => false,
'locked' => false,
'suspended' => false,
];
}
public function admin(bool $admin = true): self
{
return $this->state(fn () => [
'admin' => $admin,
]);
}
public function pending(): self
{
return $this->state(fn () => [
'pending' => true,
]);
}
public function locked(): self
{
return $this->state(fn () => [
'locked' => true,
'invalidLoginCount' => 2,
'lockoutDate' => now(),
]);
}
public function active(): self
{
return $this->state(fn () => [
'active' => true,
]);
}
public function suspended(): self
{
return $this->state(fn () => [
'suspended' => true,
]);
}
public function createElement(array $attributes = [], ?Model $parent = null): \CraftCms\Cms\User\Elements\User
{
return $this->create($attributes, $parent)->asElement();
}
public function withPasskey(string $credentialId): self
{
return $this->afterCreating(fn (User $user) => WebAuthn::factory()->create([
'userId' => $user->id,
'credentialId' => $credentialId,
]));
}
public function withPermissions(array $permissions): self
{
return $this->afterCreating(fn (User $user) => UserPermissions::saveUserPermissions($user->id, $permissions));
}
#[Override]
protected function store(Collection $results): void
{
$results->each(function (User $model) {
foreach ($model->getRelations() as $name => $items) {
if ($items instanceof Enumerable && $items->isEmpty()) {
$model->unsetRelation($name);
}
}
if (! Elements::saveElement($element = $model->asElement())) {
dump($element->errors()->all());
throw new RuntimeException('Could not save user.');
}
$model->id = $element->id;
$model->exists = true;
$model->save();
$this->createChildren($model);
// Ensure any password is set to the element as well.
$element->password = $model->password;
$model->refresh();
$model->uid = $element->uid;
$model->password = $element->password;
});
}
}