Skip to content

Commit 9f0e61e

Browse files
[6.x] Fix current augmentation handling in users fieldtype (#14724)
Co-authored-by: Jason Varga <jason@pixelfear.com>
1 parent a9ca149 commit 9f0e61e

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

src/Fieldtypes/Users.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,11 @@ public function augment($values)
254254
{
255255
$single = $this->config('max_items') === 1;
256256

257-
$ids = Arr::wrap($values);
257+
$ids = collect(Arr::wrap($values))
258+
->map(fn ($id) => $id === 'current' ? User::current()?->id() : $id)
259+
->filter()
260+
->values()
261+
->all();
258262

259263
$query = (new OrderedQueryBuilder(User::query(), $ids))->whereIn('id', $ids);
260264

tests/Fieldtypes/UsersTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,36 @@ public function it_augments_to_a_single_user_when_max_items_is_one()
6060
$this->assertEquals('one@domain.com', $augmented->email());
6161
}
6262

63+
#[Test]
64+
public function it_augments_the_current_user_when_value_is_the_current_string()
65+
{
66+
$this->actingAs(Facades\User::find('123'));
67+
68+
$augmented = $this->fieldtype(['max_items' => 1])->augment('current');
69+
70+
$this->assertInstanceOf(User::class, $augmented);
71+
$this->assertEquals('123', $augmented->id());
72+
}
73+
74+
#[Test]
75+
public function it_augments_the_current_user_to_a_query_builder_when_value_is_the_current_string()
76+
{
77+
$this->actingAs(Facades\User::find('123'));
78+
79+
$augmented = $this->fieldtype()->augment('current');
80+
81+
$this->assertInstanceOf(Builder::class, $augmented);
82+
$this->assertEquals(['123'], $augmented->get()->map->id()->all());
83+
}
84+
85+
#[Test]
86+
public function it_augments_to_null_when_value_is_the_current_string_and_no_user_is_authenticated()
87+
{
88+
$augmented = $this->fieldtype(['max_items' => 1])->augment('current');
89+
90+
$this->assertNull($augmented);
91+
}
92+
6393
#[Test]
6494
public function it_shallow_augments_to_a_collection_of_users()
6595
{

0 commit comments

Comments
 (0)