22
33namespace Tests \Fieldtypes ;
44
5+ use Illuminate \Http \Request ;
56use Illuminate \Support \Collection ;
67use PHPUnit \Framework \Attributes \Test ;
78use Statamic \Auth \UserCollection ;
1112use Statamic \Facades ;
1213use Statamic \Fields \Field ;
1314use Statamic \Fieldtypes \Users ;
15+ use Tests \FakesRoles ;
1416use Tests \Fieldtypes \Concerns \TestsQueryableValueWithMaxItems ;
1517use Tests \PreventSavingStacheItemsToDisk ;
1618use Tests \TestCase ;
1719
1820class UsersTest extends TestCase
1921{
22+ use FakesRoles;
2023 use PreventSavingStacheItemsToDisk;
2124 use TestsQueryableValueWithMaxItems;
2225
@@ -26,6 +29,7 @@ public function setUp(): void
2629
2730 Facades \User::make ()->id ('123 ' )->set ('name ' , 'One ' )->email ('one@domain.com ' )->save ();
2831 Facades \User::make ()->id ('456 ' )->set ('name ' , 'Two ' )->email ('two@domain.com ' )->save ();
32+ Facades \User::make ()->id ('789 ' )->email ('nameless@domain.com ' )->save ();
2933 }
3034
3135 #[Test]
@@ -94,6 +98,53 @@ public function it_shallow_augments_to_a_single_user_when_max_items_is_one()
9498 ], $ augmented ->toArray ());
9599 }
96100
101+ #[Test]
102+ public function it_hides_email_from_index_items_without_view_users_permission ()
103+ {
104+ $ this ->actingAs ($ this ->cpUserWithPermissions (['access cp ' ]));
105+
106+ $ items = $ this ->fieldtype ()->getIndexItems (new Request (['paginate ' => false ]));
107+ $ namelessUser = $ items ->firstWhere ('id ' , '789 ' );
108+
109+ $ this ->assertArrayNotHasKey ('email ' , $ namelessUser );
110+ $ this ->assertEquals ('789 ' , $ namelessUser ['title ' ]);
111+ }
112+
113+ #[Test]
114+ public function it_includes_email_in_index_items_with_view_users_permission ()
115+ {
116+ $ this ->actingAs ($ this ->cpUserWithPermissions (['access cp ' , 'view users ' ]));
117+
118+ $ items = $ this ->fieldtype ()->getIndexItems (new Request (['paginate ' => false ]));
119+ $ namelessUser = $ items ->firstWhere ('id ' , '789 ' );
120+
121+ $ this ->assertEquals ('nameless@domain.com ' , $ namelessUser ['title ' ]);
122+ $ this ->assertEquals ('nameless@domain.com ' , $ namelessUser ['email ' ]);
123+ }
124+
125+ #[Test]
126+ public function it_hides_the_email_column_without_view_users_permission ()
127+ {
128+ $ this ->actingAs ($ this ->cpUserWithPermissions (['access cp ' ]));
129+
130+ $ columns = $ this ->getColumns ($ this ->fieldtype ());
131+
132+ $ this ->assertCount (1 , $ columns );
133+ $ this ->assertEquals ('title ' , $ columns [0 ]->field );
134+ }
135+
136+ #[Test]
137+ public function it_includes_the_email_column_with_view_users_permission ()
138+ {
139+ $ this ->actingAs ($ this ->cpUserWithPermissions (['access cp ' , 'view users ' ]));
140+
141+ $ columns = $ this ->getColumns ($ this ->fieldtype ());
142+
143+ $ this ->assertCount (2 , $ columns );
144+ $ this ->assertEquals ('title ' , $ columns [0 ]->field );
145+ $ this ->assertEquals ('email ' , $ columns [1 ]->field );
146+ }
147+
97148 public function fieldtype ($ config = [])
98149 {
99150 $ field = new Field ('test ' , array_merge ([
@@ -102,4 +153,19 @@ public function fieldtype($config = [])
102153
103154 return (new Users )->setField ($ field );
104155 }
156+
157+ private function cpUserWithPermissions (array $ permissions )
158+ {
159+ $ this ->setTestRoles (['test ' => $ permissions ]);
160+
161+ return tap (Facades \User::make ()->id (uniqid ())->assignRole ('test ' ))->save ();
162+ }
163+
164+ private function getColumns (Users $ fieldtype ): array
165+ {
166+ $ method = new \ReflectionMethod ($ fieldtype , 'getColumns ' );
167+ $ method ->setAccessible (true );
168+
169+ return $ method ->invoke ($ fieldtype );
170+ }
105171}
0 commit comments