6
6
use App \DataFixtures \Test \LockedContestFixture ;
7
7
use App \Entity \Problem ;
8
8
use Doctrine \ORM \EntityManagerInterface ;
9
+ use Generator ;
9
10
use Symfony \Component \HttpFoundation \File \UploadedFile ;
10
11
11
12
class ProblemControllerAdminTest extends ProblemControllerTest
@@ -21,7 +22,10 @@ protected function setUp(): void
21
22
parent ::setUp ();
22
23
}
23
24
24
- public function testAddJson (): void
25
+ /**
26
+ * @dataProvider provideAllowedUsers
27
+ */
28
+ public function testAddJson (string $ user , array $ newRoles ): void
25
29
{
26
30
$ json = <<<EOF
27
31
[
@@ -61,6 +65,8 @@ public function testAddJson(): void
61
65
]
62
66
EOF ;
63
67
68
+ $ this ->roles = $ newRoles ;
69
+ self ::setUp ();
64
70
$ url = $ this ->helperGetEndpointURL ($ this ->apiEndpoint ) . '/add-data ' ;
65
71
$ tempJsonFile = tempnam (sys_get_temp_dir (), "/problems-json- " );
66
72
file_put_contents ($ tempJsonFile , $ json );
@@ -86,8 +92,13 @@ public function testAddJson(): void
86
92
self ::assertEquals ($ expectedProblems , $ addedProblems );
87
93
}
88
94
89
- public function testDelete (): void
95
+ /**
96
+ * @dataProvider provideAllowedUsers
97
+ */
98
+ public function testDelete (string $ user , array $ newRoles ): void
90
99
{
100
+ $ this ->roles = $ newRoles ;
101
+ self ::setUp ();
91
102
// Check that we can delete the problem
92
103
$ url = $ this ->helperGetEndpointURL ($ this ->apiEndpoint ) . '/fltcmp ' ;
93
104
$ this ->verifyApiJsonResponse ('DELETE ' , $ url , 204 , $ this ->apiUser );
@@ -98,15 +109,25 @@ public function testDelete(): void
98
109
self ::assertCount (2 , $ problems );
99
110
}
100
111
101
- public function testDeleteNotFound (): void
112
+ /**
113
+ * @dataProvider provideAllowedUsers
114
+ */
115
+ public function testDeleteNotFound (string $ user , array $ newRoles ): void
102
116
{
117
+ $ this ->roles = $ newRoles ;
118
+ self ::setUp ();
103
119
// Check that we can delete the problem
104
120
$ url = $ this ->helperGetEndpointURL ($ this ->apiEndpoint ) . '/4 ' ;
105
121
$ this ->verifyApiJsonResponse ('DELETE ' , $ url , 404 , $ this ->apiUser );
106
122
}
107
123
108
- public function testAdd (): void
124
+ /**
125
+ * @dataProvider provideAllowedUsers
126
+ */
127
+ public function testAdd (string $ user , array $ newRoles ): void
109
128
{
129
+ $ this ->roles = $ newRoles ;
130
+ self ::setUp ();
110
131
$ this ->loadFixture (DummyProblemFixture::class);
111
132
112
133
$ body = [
@@ -145,16 +166,26 @@ public function testAdd(): void
145
166
self ::assertCount (4 , $ problems );
146
167
}
147
168
148
- public function testAddNotFound (): void
169
+ /**
170
+ * @dataProvider provideAllowedUsers
171
+ */
172
+ public function testAddNotFound (string $ user , array $ newRoles ): void
149
173
{
174
+ $ this ->roles = $ newRoles ;
175
+ self ::setUp ();
150
176
// Check that we can delete the problem
151
177
$ url = $ this ->helperGetEndpointURL ($ this ->apiEndpoint ) . '/notfound ' ;
152
178
$ response = $ this ->verifyApiJsonResponse ('PUT ' , $ url , 404 , $ this ->apiUser , ['label ' => 'dummy ' ]);
153
179
self ::assertEquals ("Object with ID 'notfound' not found " , $ response ['message ' ]);
154
180
}
155
181
156
- public function testAddExisting (): void
182
+ /**
183
+ * @dataProvider provideAllowedUsers
184
+ */
185
+ public function testAddExisting (string $ user , array $ newRoles ): void
157
186
{
187
+ $ this ->roles = $ newRoles ;
188
+ self ::setUp ();
158
189
$ this ->loadFixture (DummyProblemFixture::class);
159
190
160
191
// Check that we can not add a problem that is already added
@@ -163,8 +194,13 @@ public function testAddExisting(): void
163
194
self ::assertEquals ('Problem already linked to contest ' , $ response ['message ' ]);
164
195
}
165
196
166
- public function testAddToLocked (): void
197
+ /**
198
+ * @dataProvider provideAllowedUsers
199
+ */
200
+ public function testAddToLocked (string $ user , array $ newRoles ): void
167
201
{
202
+ $ this ->roles = $ newRoles ;
203
+ self ::setUp ();
168
204
$ this ->loadFixture (LockedContestFixture::class);
169
205
$ this ->loadFixture (DummyProblemFixture::class);
170
206
@@ -184,13 +220,24 @@ public function testAddToLocked(): void
184
220
self ::assertStringContainsString ('Contest is locked ' , $ problemResponse ['message ' ]);
185
221
}
186
222
187
- public function testDeleteFromLocked (): void
223
+ /**
224
+ * @dataProvider provideAllowedUsers
225
+ */
226
+ public function testDeleteFromLocked (string $ user , array $ newRoles ): void
188
227
{
189
228
$ this ->loadFixture (LockedContestFixture::class);
229
+ $ this ->roles = $ newRoles ;
230
+ self ::setUp ();
190
231
191
232
// Check that we cannot delete the problem.
192
233
$ url = $ this ->helperGetEndpointURL ($ this ->apiEndpoint ) . '/fltcmp ' ;
193
234
$ problemResponse = $ this ->verifyApiJsonResponse ('DELETE ' , $ url , 403 , $ this ->apiUser );
194
235
self ::assertStringContainsString ('Contest is locked ' , $ problemResponse ['message ' ]);
195
236
}
237
+
238
+ private function provideAllowedUsers (): Generator
239
+ {
240
+ yield ['admin ' , ['admin ' ]];
241
+ yield ['team ' , ['api_problem_change ' ]];
242
+ }
196
243
}
0 commit comments