55use PHPUnit \Framework \Attributes \Test ;
66use Statamic \Facades \Collection ;
77use Statamic \Facades \Entry ;
8+ use Statamic \Facades \Taxonomy ;
9+ use Statamic \Facades \Term ;
810use Statamic \Facades \User ;
911use Statamic \Query \Scopes \Scope ;
1012use Tests \FakesRoles ;
@@ -35,7 +37,7 @@ public function it_filters_entries_by_query_scopes()
3537 Entry::make ()->collection ('test ' )->slug ('cherry ' )->data (['title ' => 'Cherry ' ])->save ();
3638 Entry::make ()->collection ('test ' )->slug ('banana ' )->data (['title ' => 'Banana ' ])->save ();
3739
38- $ this ->setTestRoles (['test ' => ['access cp ' ]]);
40+ $ this ->setTestRoles (['test ' => ['access cp ' , ' view test entries ' ]]);
3941 $ user = User::make ()->assignRole ('test ' )->save ();
4042
4143 $ config = base64_encode (json_encode ([
@@ -46,7 +48,7 @@ public function it_filters_entries_by_query_scopes()
4648
4749 $ response = $ this
4850 ->actingAs ($ user )
49- ->get ("/cp/fieldtypes/relationship?config= {$ config }&collections[0]=test " )
51+ ->get ("/cp/fieldtypes/relationship?config= {$ config }" )
5052 ->assertOk ();
5153
5254 $ titles = collect ($ response ->json ('data ' ))->pluck ('title ' )->all ();
@@ -57,6 +59,96 @@ public function it_filters_entries_by_query_scopes()
5759 $ this ->assertNotContains ('Apple ' , $ titles );
5860 $ this ->assertNotContains ('Banana ' , $ titles );
5961 }
62+
63+ #[Test]
64+ public function it_denies_access_to_entries_when_theres_a_collection_the_user_cannot_view ()
65+ {
66+ Collection::make ('secret ' )->save ();
67+ Entry::make ()->collection ('secret ' )->slug ('secret-one ' )->data (['title ' => 'Secret One ' ])->save ();
68+
69+ $ this ->setTestRoles (['test ' => ['access cp ' ]]);
70+ $ user = User::make ()->assignRole ('test ' )->save ();
71+
72+ $ config = base64_encode (json_encode ([
73+ 'type ' => 'entries ' ,
74+ 'collections ' => ['secret ' ],
75+ ]));
76+
77+ $ this
78+ ->actingAs ($ user )
79+ ->getJson ("/cp/fieldtypes/relationship?config= {$ config }" )
80+ ->assertForbidden ();
81+ }
82+
83+ #[Test]
84+ public function it_forbids_access_to_entries_when_filters_target_a_collection_the_user_cannot_view ()
85+ {
86+ Collection::make ('secret ' )->save ();
87+ Entry::make ()->collection ('test ' )->slug ('apple ' )->data (['title ' => 'Apple ' ])->save ();
88+ Entry::make ()->collection ('secret ' )->slug ('secret-one ' )->data (['title ' => 'Secret One ' ])->save ();
89+
90+ $ this ->setTestRoles ([
91+ 'test ' => ['access cp ' , 'view test entries ' ],
92+ ]);
93+ $ user = User::make ()->assignRole ('test ' )->save ();
94+
95+ $ config = base64_encode (json_encode ([
96+ 'type ' => 'entries ' ,
97+ 'collections ' => ['test ' ],
98+ ]));
99+ $ filters = base64_encode (json_encode ([
100+ 'collection ' => ['collections ' => ['secret ' ]],
101+ ]));
102+
103+ $ this
104+ ->actingAs ($ user )
105+ ->getJson ("/cp/fieldtypes/relationship?config= {$ config }&filters= {$ filters }" )
106+ ->assertForbidden ();
107+ }
108+
109+ #[Test]
110+ public function it_forbids_access_to_terms_when_config_contains_a_taxonomy_the_user_cannot_view ()
111+ {
112+ Taxonomy::make ('secret ' )->save ();
113+ Term::make ('internal ' )->taxonomy ('secret ' )->data ([])->save ();
114+
115+ $ this ->setTestRoles (['test ' => ['access cp ' ]]);
116+ $ user = User::make ()->assignRole ('test ' )->save ();
117+
118+ $ config = base64_encode (json_encode ([
119+ 'type ' => 'terms ' ,
120+ 'taxonomies ' => ['secret ' ],
121+ ]));
122+
123+ $ this
124+ ->actingAs ($ user )
125+ ->getJson ("/cp/fieldtypes/relationship?config= {$ config }&taxonomies[0]=secret " )
126+ ->assertForbidden ();
127+ }
128+
129+ #[Test]
130+ public function it_forbids_access_to_terms_when_requested_taxonomy_is_forbidden ()
131+ {
132+ Taxonomy::make ('topics ' )->save ();
133+ Taxonomy::make ('secret ' )->save ();
134+ Term::make ('public ' )->taxonomy ('topics ' )->data ([])->save ();
135+ Term::make ('internal ' )->taxonomy ('secret ' )->data ([])->save ();
136+
137+ $ this ->setTestRoles ([
138+ 'test ' => ['access cp ' , 'view topics terms ' ],
139+ ]);
140+ $ user = User::make ()->assignRole ('test ' )->save ();
141+
142+ $ config = base64_encode (json_encode ([
143+ 'type ' => 'terms ' ,
144+ 'taxonomies ' => ['topics ' ],
145+ ]));
146+
147+ $ this
148+ ->actingAs ($ user )
149+ ->getJson ("/cp/fieldtypes/relationship?config= {$ config }&taxonomies[0]=secret " )
150+ ->assertForbidden ();
151+ }
60152}
61153
62154class StartsWithC extends Scope
0 commit comments