forked from statamic/cms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUsersTest.php
More file actions
170 lines (137 loc) · 5.4 KB
/
UsersTest.php
File metadata and controls
170 lines (137 loc) · 5.4 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
namespace Tests\Fieldtypes;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Auth\UserCollection;
use Statamic\Contracts\Auth\User;
use Statamic\Contracts\Query\Builder;
use Statamic\Data\AugmentedCollection;
use Statamic\Facades;
use Statamic\Fields\Field;
use Statamic\Fieldtypes\Users;
use Tests\FakesRoles;
use Tests\Fieldtypes\Concerns\TestsQueryableValueWithMaxItems;
use Tests\PreventSavingStacheItemsToDisk;
use Tests\TestCase;
class UsersTest extends TestCase
{
use FakesRoles;
use PreventSavingStacheItemsToDisk;
use TestsQueryableValueWithMaxItems;
public function setUp(): void
{
parent::setUp();
Facades\User::make()->id('123')->set('name', 'One')->email('one@domain.com')->save();
Facades\User::make()->id('456')->set('name', 'Two')->email('two@domain.com')->save();
Facades\User::make()->id('789')->email('nameless@domain.com')->save();
}
#[Test]
public function it_augments_to_a_query_builder()
{
$augmented = $this->fieldtype()->augment([456, '123']);
$this->assertInstanceOf(Builder::class, $augmented);
$this->assertEveryItemIsInstanceOf(User::class, $augmented->get());
$this->assertEquals(['456', '123'], $augmented->get()->map->id()->all());
}
#[Test]
public function it_augments_to_a_query_builder_when_theres_no_value()
{
$augmented = $this->fieldtype()->augment(null);
$this->assertInstanceOf(Builder::class, $augmented);
$this->assertCount(0, $augmented->get());
}
#[Test]
public function it_augments_to_a_single_user_when_max_items_is_one()
{
$augmented = $this->fieldtype(['max_items' => 1])->augment(['123']);
$this->assertInstanceOf(User::class, $augmented);
$this->assertEquals('one@domain.com', $augmented->email());
}
#[Test]
public function it_shallow_augments_to_a_collection_of_users()
{
$augmented = $this->fieldtype()->shallowAugment(['123', 456]);
$this->assertInstanceOf(Collection::class, $augmented);
$this->assertNotInstanceOf(UserCollection::class, $augmented);
$this->assertEveryItemIsInstanceOf(AugmentedCollection::class, $augmented);
$this->assertEquals([
[
'id' => '123',
'name' => 'One',
'email' => 'one@domain.com',
'api_url' => 'http://localhost/api/users/123',
],
[
'id' => '456',
'name' => 'Two',
'email' => 'two@domain.com',
'api_url' => 'http://localhost/api/users/456',
],
], $augmented->toArray());
}
#[Test]
public function it_shallow_augments_to_a_single_user_when_max_items_is_one()
{
$augmented = $this->fieldtype(['max_items' => 1])->shallowAugment(['123']);
$this->assertInstanceOf(AugmentedCollection::class, $augmented);
$this->assertEquals([
'id' => '123',
'name' => 'One',
'email' => 'one@domain.com',
'api_url' => 'http://localhost/api/users/123',
], $augmented->toArray());
}
#[Test]
public function it_hides_email_from_index_items_without_view_users_permission()
{
$this->actingAs($this->cpUserWithPermissions(['access cp']));
$items = $this->fieldtype()->getIndexItems(new Request(['paginate' => false]));
$namelessUser = $items->firstWhere('id', '789');
$this->assertArrayNotHasKey('email', $namelessUser);
$this->assertEquals('789', $namelessUser['title']);
}
#[Test]
public function it_includes_email_in_index_items_with_view_users_permission()
{
$this->actingAs($this->cpUserWithPermissions(['access cp', 'view users']));
$items = $this->fieldtype()->getIndexItems(new Request(['paginate' => false]));
$namelessUser = $items->firstWhere('id', '789');
$this->assertEquals('nameless@domain.com', $namelessUser['title']);
$this->assertEquals('nameless@domain.com', $namelessUser['email']);
}
#[Test]
public function it_hides_the_email_column_without_view_users_permission()
{
$this->actingAs($this->cpUserWithPermissions(['access cp']));
$columns = $this->getColumns($this->fieldtype());
$this->assertCount(1, $columns);
$this->assertEquals('title', $columns[0]->field);
}
#[Test]
public function it_includes_the_email_column_with_view_users_permission()
{
$this->actingAs($this->cpUserWithPermissions(['access cp', 'view users']));
$columns = $this->getColumns($this->fieldtype());
$this->assertCount(2, $columns);
$this->assertEquals('title', $columns[0]->field);
$this->assertEquals('email', $columns[1]->field);
}
public function fieldtype($config = [])
{
$field = new Field('test', array_merge([
'type' => 'users',
], $config));
return (new Users)->setField($field);
}
private function cpUserWithPermissions(array $permissions)
{
$this->setTestRoles(['test' => $permissions]);
return tap(Facades\User::make()->id(uniqid())->assignRole('test'))->save();
}
private function getColumns(Users $fieldtype): array
{
$method = new \ReflectionMethod($fieldtype, 'getColumns');
return $method->invoke($fieldtype);
}
}