66use App \DataFixtures \Test \LockedContestFixture ;
77use App \Entity \Problem ;
88use Doctrine \ORM \EntityManagerInterface ;
9+ use Generator ;
910use Symfony \Component \HttpFoundation \File \UploadedFile ;
1011
1112class ProblemControllerAdminTest extends ProblemControllerTest
@@ -21,7 +22,10 @@ protected function setUp(): void
2122 parent ::setUp ();
2223 }
2324
24- public function testAddJson (): void
25+ /**
26+ * @dataProvider provideAllowedUsers
27+ */
28+ public function testAddJson (string $ user , array $ newRoles ): void
2529 {
2630 $ json = <<<EOF
2731[
@@ -61,6 +65,8 @@ public function testAddJson(): void
6165]
6266EOF ;
6367
68+ $ this ->roles = $ newRoles ;
69+ self ::setUp ();
6470 $ url = $ this ->helperGetEndpointURL ($ this ->apiEndpoint ) . '/add-data ' ;
6571 $ tempJsonFile = tempnam (sys_get_temp_dir (), "/problems-json- " );
6672 file_put_contents ($ tempJsonFile , $ json );
@@ -86,8 +92,13 @@ public function testAddJson(): void
8692 self ::assertEquals ($ expectedProblems , $ addedProblems );
8793 }
8894
89- public function testDelete (): void
95+ /**
96+ * @dataProvider provideAllowedUsers
97+ */
98+ public function testDelete (string $ user , array $ newRoles ): void
9099 {
100+ $ this ->roles = $ newRoles ;
101+ self ::setUp ();
91102 // Check that we can delete the problem
92103 $ url = $ this ->helperGetEndpointURL ($ this ->apiEndpoint ) . '/fltcmp ' ;
93104 $ this ->verifyApiJsonResponse ('DELETE ' , $ url , 204 , $ this ->apiUser );
@@ -98,15 +109,25 @@ public function testDelete(): void
98109 self ::assertCount (2 , $ problems );
99110 }
100111
101- public function testDeleteNotFound (): void
112+ /**
113+ * @dataProvider provideAllowedUsers
114+ */
115+ public function testDeleteNotFound (string $ user , array $ newRoles ): void
102116 {
117+ $ this ->roles = $ newRoles ;
118+ self ::setUp ();
103119 // Check that we can delete the problem
104120 $ url = $ this ->helperGetEndpointURL ($ this ->apiEndpoint ) . '/4 ' ;
105121 $ this ->verifyApiJsonResponse ('DELETE ' , $ url , 404 , $ this ->apiUser );
106122 }
107123
108- public function testAdd (): void
124+ /**
125+ * @dataProvider provideAllowedUsers
126+ */
127+ public function testAdd (string $ user , array $ newRoles ): void
109128 {
129+ $ this ->roles = $ newRoles ;
130+ self ::setUp ();
110131 $ this ->loadFixture (DummyProblemFixture::class);
111132
112133 $ body = [
@@ -145,16 +166,26 @@ public function testAdd(): void
145166 self ::assertCount (4 , $ problems );
146167 }
147168
148- public function testAddNotFound (): void
169+ /**
170+ * @dataProvider provideAllowedUsers
171+ */
172+ public function testAddNotFound (string $ user , array $ newRoles ): void
149173 {
174+ $ this ->roles = $ newRoles ;
175+ self ::setUp ();
150176 // Check that we can delete the problem
151177 $ url = $ this ->helperGetEndpointURL ($ this ->apiEndpoint ) . '/notfound ' ;
152178 $ response = $ this ->verifyApiJsonResponse ('PUT ' , $ url , 404 , $ this ->apiUser , ['label ' => 'dummy ' ]);
153179 self ::assertEquals ("Object with ID 'notfound' not found " , $ response ['message ' ]);
154180 }
155181
156- public function testAddExisting (): void
182+ /**
183+ * @dataProvider provideAllowedUsers
184+ */
185+ public function testAddExisting (string $ user , array $ newRoles ): void
157186 {
187+ $ this ->roles = $ newRoles ;
188+ self ::setUp ();
158189 $ this ->loadFixture (DummyProblemFixture::class);
159190
160191 // Check that we can not add a problem that is already added
@@ -163,8 +194,13 @@ public function testAddExisting(): void
163194 self ::assertEquals ('Problem already linked to contest ' , $ response ['message ' ]);
164195 }
165196
166- public function testAddToLocked (): void
197+ /**
198+ * @dataProvider provideAllowedUsers
199+ */
200+ public function testAddToLocked (string $ user , array $ newRoles ): void
167201 {
202+ $ this ->roles = $ newRoles ;
203+ self ::setUp ();
168204 $ this ->loadFixture (LockedContestFixture::class);
169205 $ this ->loadFixture (DummyProblemFixture::class);
170206
@@ -184,13 +220,24 @@ public function testAddToLocked(): void
184220 self ::assertStringContainsString ('Contest is locked ' , $ problemResponse ['message ' ]);
185221 }
186222
187- public function testDeleteFromLocked (): void
223+ /**
224+ * @dataProvider provideAllowedUsers
225+ */
226+ public function testDeleteFromLocked (string $ user , array $ newRoles ): void
188227 {
189228 $ this ->loadFixture (LockedContestFixture::class);
229+ $ this ->roles = $ newRoles ;
230+ self ::setUp ();
190231
191232 // Check that we cannot delete the problem.
192233 $ url = $ this ->helperGetEndpointURL ($ this ->apiEndpoint ) . '/fltcmp ' ;
193234 $ problemResponse = $ this ->verifyApiJsonResponse ('DELETE ' , $ url , 403 , $ this ->apiUser );
194235 self ::assertStringContainsString ('Contest is locked ' , $ problemResponse ['message ' ]);
195236 }
237+
238+ private function provideAllowedUsers (): Generator
239+ {
240+ yield ['admin ' , ['admin ' ]];
241+ yield ['team ' , ['api_problem_change ' ]];
242+ }
196243}
0 commit comments