From 3c511df7f7cf056fd91f6c1df28c634d38c1231b Mon Sep 17 00:00:00 2001 From: Kevin Papst Date: Tue, 19 May 2020 11:24:30 +0200 Subject: [PATCH] fix required fields in API (#1718) --- src/API/ActivityController.php | 2 +- src/API/BaseApiController.php | 4 ++++ src/API/TimesheetController.php | 2 +- src/Form/EntityFormTrait.php | 2 ++ tests/API/ActivityControllerTest.php | 15 +++++++++++++++ tests/API/CustomerControllerTest.php | 18 ++++++++++++++++++ tests/API/ProjectControllerTest.php | 17 +++++++++++++++++ 7 files changed, 58 insertions(+), 2 deletions(-) diff --git a/src/API/ActivityController.php b/src/API/ActivityController.php index 43120467..7a108330 100644 --- a/src/API/ActivityController.php +++ b/src/API/ActivityController.php @@ -336,7 +336,7 @@ class ActivityController extends BaseApiController $this->repository->saveActivity($activity); $view = new View($activity, 200); - $view->getContext()->setGroups(['Default', 'Entity', 'Project']); + $view->getContext()->setGroups(['Default', 'Entity', 'Activity']); return $this->viewHandler->handle($view); } diff --git a/src/API/BaseApiController.php b/src/API/BaseApiController.php index 03afd668..e9d69218 100644 --- a/src/API/BaseApiController.php +++ b/src/API/BaseApiController.php @@ -11,9 +11,13 @@ declare(strict_types=1); namespace App\API; +use App\Entity\User; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Form\Extension\Core\Type\DateTimeType; +/** + * @method null|User getUser() + */ abstract class BaseApiController extends AbstractController { public const DATE_FORMAT = DateTimeType::HTML5_FORMAT; diff --git a/src/API/TimesheetController.php b/src/API/TimesheetController.php index 278a86ac..48eced1d 100644 --- a/src/API/TimesheetController.php +++ b/src/API/TimesheetController.php @@ -143,7 +143,7 @@ class TimesheetController extends BaseApiController * @Rest\QueryParam(name="end", requirements=@Constraints\DateTime(format="Y-m-d\TH:i:s"), strict=true, nullable=true, description="Only records before this date will be included (format: HTML5)") * @Rest\QueryParam(name="exported", requirements="0|1", strict=true, nullable=true, description="Use this flag if you want to filter for export state. Allowed values: 0=not exported, 1=exported (default: all)") * @Rest\QueryParam(name="active", requirements="0|1", strict=true, nullable=true, description="Filter for running/active records. Allowed values: 0=stopped, 1=active (default: all)") - * @Rest\QueryParam(name="full", requirements="true", strict=true, nullable=true, description="Allows to fetch fully serialized objects including subresources (TimesheetSubCollection). Allowed values: true (default: false)") + * @Rest\QueryParam(name="full", requirements="true", strict=true, nullable=true, description="Allows to fetch fully serialized objects including subresources. Allowed values: true (default: false)") * @Rest\QueryParam(name="term", description="Free search term") * * @Security("is_granted('view_own_timesheet') or is_granted('view_other_timesheet')") diff --git a/src/Form/EntityFormTrait.php b/src/Form/EntityFormTrait.php index ca293356..0b9860a0 100644 --- a/src/Form/EntityFormTrait.php +++ b/src/Form/EntityFormTrait.php @@ -29,11 +29,13 @@ trait EntityFormTrait if ($options['include_budget']) { $builder ->add('budget', MoneyType::class, [ + 'empty_data' => '0.00', 'label' => 'label.budget', 'required' => false, 'currency' => $currency, ]) ->add('timeBudget', DurationType::class, [ + 'empty_data' => 0, 'label' => 'label.timeBudget', 'icon' => 'clock', 'required' => false, diff --git a/tests/API/ActivityControllerTest.php b/tests/API/ActivityControllerTest.php index 8fecfbfc..08e19f3d 100644 --- a/tests/API/ActivityControllerTest.php +++ b/tests/API/ActivityControllerTest.php @@ -201,6 +201,21 @@ class ActivityControllerTest extends APIControllerBaseTest $this->assertNotEmpty($result['id']); } + public function testPostActionWithLeastFields() + { + $client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN); + $data = [ + 'name' => 'foo', + ]; + $this->request($client, '/api/activities', 'POST', [], json_encode($data)); + $this->assertTrue($client->getResponse()->isSuccessful()); + + $result = json_decode($client->getResponse()->getContent(), true); + $this->assertIsArray($result); + $this->assertStructure($result); + $this->assertNotEmpty($result['id']); + } + public function testPostActionWithInvalidUser() { $client = $this->getClientForAuthenticatedUser(User::ROLE_USER); diff --git a/tests/API/CustomerControllerTest.php b/tests/API/CustomerControllerTest.php index cba12602..82379062 100644 --- a/tests/API/CustomerControllerTest.php +++ b/tests/API/CustomerControllerTest.php @@ -136,6 +136,24 @@ class CustomerControllerTest extends APIControllerBaseTest $this->assertNotEmpty($result['id']); } + public function testPostActionWithLeastFields() + { + $client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN); + $data = [ + 'name' => 'foo', + 'country' => 'DE', + 'currency' => 'EUR', + 'timezone' => 'Europe/Berlin', + ]; + $this->request($client, '/api/customers', 'POST', [], json_encode($data)); + $this->assertTrue($client->getResponse()->isSuccessful()); + + $result = json_decode($client->getResponse()->getContent(), true); + $this->assertIsArray($result); + $this->assertStructure($result); + $this->assertNotEmpty($result['id']); + } + public function testPostActionWithInvalidUser() { $client = $this->getClientForAuthenticatedUser(User::ROLE_USER); diff --git a/tests/API/ProjectControllerTest.php b/tests/API/ProjectControllerTest.php index 2086b13b..620cec03 100644 --- a/tests/API/ProjectControllerTest.php +++ b/tests/API/ProjectControllerTest.php @@ -202,6 +202,23 @@ class ProjectControllerTest extends APIControllerBaseTest self::assertEquals('2020-02-08T21:11:42+0000', $result['end']); } + public function testPostActionWithLeastFields() + { + $client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN); + $data = [ + 'name' => 'foo', + 'customer' => 1 + ]; + $this->request($client, '/api/projects', 'POST', [], json_encode($data)); + $this->assertTrue($client->getResponse()->isSuccessful()); + + $result = json_decode($client->getResponse()->getContent(), true); + $this->assertIsArray($result); + $this->assertStructure($result); + $this->assertNotEmpty($result['id']); + self::assertEquals('foo', $result['name']); + } + public function testPostActionWithInvalidUser() { $client = $this->getClientForAuthenticatedUser(User::ROLE_USER);