-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathControllingControllerTest.php
More file actions
322 lines (275 loc) · 11.8 KB
/
Copy pathControllingControllerTest.php
File metadata and controls
322 lines (275 loc) · 11.8 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<?php
/*
* Copyright (c) 2025-2026 Netresearch DTT GmbH
* SPDX-License-Identifier: AGPL-3.0-only
*/
declare(strict_types=1);
namespace Tests\Controller;
use App\Service\ExportService as Export;
use DateTime;
use Tests\AbstractWebTestCase;
/**
* @internal
*
* @coversNothing
*/
final class ControllingControllerTest extends AbstractWebTestCase
{
public function testExportActionBasicResponse(): void
{
$this->logInSession('unittest');
$this->client->request(\Symfony\Component\HttpFoundation\Request::METHOD_GET, '/controlling/export', [
'year' => 2020,
'month' => 2,
'userid' => 2,
'project' => 0,
]);
$this->assertStatusCode(200);
$response = $this->client->getResponse();
$contentDisposition = $response->headers->get('Content-disposition');
self::assertStringStartsWith('attachment;', (string) $contentDisposition);
self::assertStringContainsString('02_developer', (string) $contentDisposition); // userid 2 = 'developer'
}
/**
* The export ships any user's entries via ?userid=, so it must be denied to
* non-privileged roles (DEV/USER). 'developer' (fixture id 2) is a DEV and
* must not be able to export anyone's data — the IDOR guard.
*/
public function testExportActionForbiddenForNonAdmin(): void
{
$this->logInSession('developer');
// Accept JSON so the ExceptionSubscriber renders the AccessDenied as a
// 403 response rather than an HTML error page; the IsGranted gate itself
// blocks the request regardless of the requested format.
$this->client->request(\Symfony\Component\HttpFoundation\Request::METHOD_GET, '/controlling/export', [
'year' => 2020,
'month' => 2,
'userid' => 1,
'project' => 0,
], [], ['HTTP_ACCEPT' => 'application/json']);
$this->assertStatusCode(403);
}
public function testExportActionInvalidMonth(): void
{
$this->logInSession('unittest');
$this->client->request(\Symfony\Component\HttpFoundation\Request::METHOD_GET, '/controlling/export', [
'year' => 2020,
'month' => 666,
'userid' => 2,
'project' => 0,
]);
$this->assertStatusCode(422);
}
public function testExportActionInvalidYear(): void
{
$this->logInSession('unittest');
$this->client->request(\Symfony\Component\HttpFoundation\Request::METHOD_GET, '/controlling/export', [
'year' => 666,
'month' => 1,
'userid' => 2,
'project' => 0,
]);
$this->assertStatusCode(422);
}
/**
* Test legacy URL format with path parameters.
*
* Bug: /controlling/export/{userid}/{year}/{month}/{project}/{customer}/{billable}
* was returning "Year must be between 1900 and 2100" for valid year 2025
* because path parameters weren't being mapped to the DTO.
*/
public function testExportActionLegacyUrlFormat(): void
{
$this->logInSession('unittest');
// Legacy URL: /controlling/export/{userid}/{year}/{month}/{project}/{customer}/{billable}
$this->client->request(
\Symfony\Component\HttpFoundation\Request::METHOD_GET,
'/controlling/export/0/2025/12/0/0/0',
);
// Should NOT return 422 for valid year 2025
$response = $this->client->getResponse();
$content = (string) $response->getContent();
// The bug caused: "Year must be between 1900 and 2100" for year=2025
self::assertStringNotContainsString(
'Year must be between 1900 and 2100',
$content,
'Valid year 2025 in path parameters should not trigger validation error',
);
// Should return 200 (successful export)
$this->assertStatusCode(200);
}
/**
* Test that various valid years work with legacy URL format.
*/
public function testExportActionLegacyUrlFormatVariousYears(): void
{
$this->logInSession('unittest');
foreach ([2020, 2024, 2025, 2026] as $year) {
$this->client->request(
\Symfony\Component\HttpFoundation\Request::METHOD_GET,
"/controlling/export/0/{$year}/1/0/0/0",
);
$response = $this->client->getResponse();
$content = (string) $response->getContent();
self::assertStringNotContainsString(
'Year must be between 1900 and 2100',
$content,
"Year {$year} should be valid in legacy URL format",
);
}
}
public function testExportActionWithBillableAndTicketTitles(): void
{
$_ENV['APP_SHOW_BILLABLE_FIELD_IN_EXPORT'] = 'true';
// 1. Create export service mock
$exportServiceMock = $this->createMock(Export::class);
// --- Real entities for export ---
$user = new \App\Entity\User()->setId(1)->setUsername('unittest');
$customer = new \App\Entity\Customer()->setId(1)->setName('Test Customer');
$project = new \App\Entity\Project()->setId(1)->setName('Test Project');
$entry1 = new \App\Entity\Entry()
->setId(4)
->setDay(new DateTime('2023-10-15'))
->setStart(new DateTime('2023-10-15 09:00:00'))
->setEnd(new DateTime('2023-10-15 10:30:00'))
->setUser($user)
->setCustomer($customer)
->setProject($project)
->setTicket('TKT-1')
->setDescription('Real Desc 1');
// ->setActivity(null) // Assuming default is fine or set if needed
$entry2 = new \App\Entity\Entry()
->setId(5)
->setDay(new DateTime('2023-10-20'))
->setStart(new DateTime('2023-10-20 11:00:00'))
->setEnd(new DateTime('2023-10-20 12:30:00'))
->setUser($user)
->setCustomer($customer)
->setProject($project)
->setTicket('TKT-2')
->setDescription('Real Desc 2');
// --- End of real entity prep --- //
// Mock exportEntries - return the REAL entries
$exportServiceMock->expects(self::once())
->method('exportEntries')
->willReturn([$entry1, $entry2]);
// Mock enrichEntries - expect it called with correct parameters, use callback to call setters on REAL entries
$exportServiceMock->expects(self::once())
->method('enrichEntriesWithTicketInformation')
->willReturnCallback(static function ($userId, array $entries, $includeBillable, $includeTicketTitle): array {
foreach ($entries as $entry) {
// Use setters ON THE REAL Entry objects
if ($entry instanceof \App\Entity\Entry) {
if ($includeBillable) {
$entry->setBillable(true);
}
if ($includeTicketTitle) {
$entry->setTicketTitle('Mocked Title for ' . $entry->getTicket());
}
}
}
return $entries;
});
// Mock getUsername method used for filename generation - matches current service interface
$exportServiceMock->expects(self::once())
->method('getUsername')
->with(1) // userid from the request
->willReturn('unittest');
// Ensure kernel is shut down before creating client
self::ensureKernelShutdown();
// 2. Create client (boots kernel)
$client = self::createClient();
$this->client = $client;
$this->serviceContainer = $this->client->getContainer();
// 3. Replace service in the container obtained from the client
$testContainer = $this->client->getContainer(); // Get container from client
$testContainer->set(Export::class, $exportServiceMock); // Replace service
// 4. Prepare and make request
$this->logInSession('unittest');
$this->client->request(
\Symfony\Component\HttpFoundation\Request::METHOD_GET,
'/controlling/export',
[
'year' => 2023,
'month' => 10,
'userid' => 1,
'project' => 0,
],
);
// 5. Assertions
$this->assertStatusCode(200);
$response = $this->client->getResponse();
// ... (other header assertions)
$contentDisposition = $response->headers->get('Content-disposition');
self::assertStringStartsWith('attachment;', (string) $contentDisposition);
self::assertStringContainsString(
'attachment;filename=2023_10_unittest.xlsx', // Expect mocked username
(string) $contentDisposition,
);
// ... (rest of assertions)
}
/**
* Test that the billable column (N) is NOT present when APP_SHOW_BILLABLE_FIELD_IN_EXPORT is false/unset.
*/
public function testExportActionHidesBillableFieldWhenNotConfigured(): void
{
$_ENV['APP_SHOW_BILLABLE_FIELD_IN_EXPORT'] = 'false';
// 1. Create export service mock
$exportServiceMock = $this->createMock(Export::class);
// --- Mock data for export ---
$user = new \App\Entity\User()->setId(1)->setUsername('testuser');
$customer = new \App\Entity\Customer()->setId(1)->setName('Test Customer');
$project = new \App\Entity\Project()->setId(1)->setName('Test Project');
$entry1 = new \App\Entity\Entry()
->setId(6)
->setDay(new DateTime('2023-11-05'))
->setStart(new DateTime('2023-11-05 08:00:00'))
->setEnd(new DateTime('2023-11-05 09:30:00'))
->setUser($user)
->setCustomer($customer)
->setProject($project)
->setTicket('TKT-3')
->setDescription('Test Desc 3');
// Mock exportEntries - return the mock entry
$exportServiceMock->expects(self::once())
->method('exportEntries')
->willReturn([$entry1]);
// Since showBillableField=false and tickettitles is not requested,
// enrichEntriesWithTicketInformation should NOT be called
$exportServiceMock->expects(self::never())
->method('enrichEntriesWithTicketInformation');
// Mock getUsername method used for filename generation
$exportServiceMock->expects(self::once())
->method('getUsername')
->with(1) // userid from the request
->willReturn('unittest');
// Ensure kernel is shut down before creating client
self::ensureKernelShutdown();
// 2. Create client (boots kernel)
$client = self::createClient();
$this->client = $client;
// Update service container reference after creating new client
$this->serviceContainer = $this->client->getContainer();
// 3. Replace service in the container
$testContainer = $this->client->getContainer();
$testContainer->set(Export::class, $exportServiceMock);
// 4. Load test data and make request
$this->logInSession('unittest');
$this->client->request(
\Symfony\Component\HttpFoundation\Request::METHOD_GET,
'/controlling/export',
[
'year' => 2023,
'month' => 11,
'userid' => 1,
'project' => 0,
],
);
// 5. Assertions - verify response is 200 and doesn't contain billable column
$this->assertStatusCode(200);
// Note: Since we're mocking the service, the actual spreadsheet content
// can't be easily tested without complex mocking of PhpSpreadsheet objects.
// This test primarily ensures the correct methods are called on the service
// with the correct parameters (showBillable = false).
}
}