API begin and end fields for Admins (#5134)

This commit is contained in:
Kevin Papst
2024-10-25 10:47:58 +02:00
committed by GitHub
parent 31bae44f3c
commit dcc52f1a95
12 changed files with 161 additions and 47 deletions

View File

@@ -211,7 +211,7 @@ abstract class APIControllerBaseTest extends ControllerBaseTest
* @param bool $extraFields test for the error "This form should not contain extra fields"
* @param array<int, string>|array<string, mixed> $globalError
*/
protected function assertApiCallValidationError(Response $response, array $failedFields, bool $extraFields = false, array $globalError = []): void
protected function assertApiCallValidationError(Response $response, array $failedFields, bool $extraFields = false, array $globalError = [], array $expectedFields = [], array $missingFields = []): void
{
self::assertFalse($response->isSuccessful());
$result = json_decode($response->getContent(), true);
@@ -232,6 +232,18 @@ abstract class APIControllerBaseTest extends ControllerBaseTest
self::assertArrayHasKey('children', $result['errors']);
$data = $result['errors']['children'];
if (\count($expectedFields) > 0) {
foreach ($expectedFields as $expectedField) {
$this->assertArrayHasKey($expectedField, $data, 'Expected field is missing: ' . $expectedField);
}
}
if (\count($missingFields) > 0) {
foreach ($missingFields as $missingField) {
$this->assertArrayNotHasKey($missingField, $data, 'Expected missing field is available: ' . $missingField);
}
}
$foundErrors = [];
foreach ($failedFields as $key => $value) {

View File

@@ -752,6 +752,48 @@ class TimesheetControllerTest extends APIControllerBaseTest
$this->assertTrue($result['billable']);
}
public function getTrackingModeTestData(): array
{
return [
['duration_fixed_begin', User::ROLE_USER, false],
['duration_fixed_begin', User::ROLE_ADMIN, true],
['duration_fixed_begin', User::ROLE_SUPER_ADMIN, true],
['punch', User::ROLE_USER, false],
['punch', User::ROLE_ADMIN, true],
['punch', User::ROLE_SUPER_ADMIN, true],
['default', User::ROLE_USER, true],
['default', User::ROLE_ADMIN, true],
['default', User::ROLE_SUPER_ADMIN, true]
];
}
/**
* @dataProvider getTrackingModeTestData
*/
public function testCreateActionWithTrackingModeHasFieldsForUser(string $trackingMode, string $user, bool $showTimes): void
{
$dateTime = new DateTimeFactory(new \DateTimeZone(self::TEST_TIMEZONE));
$client = $this->getClientForAuthenticatedUser($user);
$this->setSystemConfiguration('timesheet.mode', $trackingMode);
$data = [
'activity' => 1,
'project' => 1,
'begin' => ($dateTime->createDateTime('-8 hours'))->format('Y-m-d H:m:0'),
'end' => ($dateTime->createDateTime())->format('Y-m-d H:m:0'),
'description' => 'foo',
];
$json = json_encode($data);
self::assertIsString($json);
$this->request($client, '/api/timesheets', 'POST', [], $json);
$response = $client->getResponse();
if ($showTimes) {
$this->assertTrue($response->isSuccessful());
} else {
$this->assertApiCallValidationError($response, [], true, [], [], ['begin', 'end']);
}
}
public function testPatchAction(): void
{
$dateTime = new DateTimeFactory(new \DateTimeZone(self::TEST_TIMEZONE));