diff --git a/src/Entity/Project.php b/src/Entity/Project.php index beb603f0..cf7050c9 100644 --- a/src/Entity/Project.php +++ b/src/Entity/Project.php @@ -257,6 +257,9 @@ class Project implements EntityWithMetaFields, EntityWithBudget * * @var bool * + * @Serializer\Expose() + * @Serializer\Groups({"Default"}) + * * @ORM\Column(name="global_activities", type="boolean", nullable=false, options={"default": true}) * @Assert\NotNull() */ diff --git a/src/Form/API/ProjectApiEditForm.php b/src/Form/API/ProjectApiEditForm.php index d3a941f7..22ab4c64 100644 --- a/src/Form/API/ProjectApiEditForm.php +++ b/src/Form/API/ProjectApiEditForm.php @@ -10,6 +10,7 @@ namespace App\Form\API; use App\Form\ProjectEditForm; +use App\Form\Type\APITrueFalseType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -23,6 +24,11 @@ class ProjectApiEditForm extends ProjectEditForm parent::buildForm($builder, $options); $builder->remove('metaFields'); + $builder->remove('globalActivities'); + $builder->add('globalActivities', APITrueFalseType::class, [ + 'label' => 'label.globalActivities', + 'empty_data' => '1', + ]); } /** diff --git a/src/Form/Type/APITrueFalseType.php b/src/Form/Type/APITrueFalseType.php new file mode 100644 index 00000000..83001db0 --- /dev/null +++ b/src/Form/Type/APITrueFalseType.php @@ -0,0 +1,30 @@ +setDefaults([ + 'false_values' => [0, '0', false], + 'required' => false, + ]); + } + + public function getParent(): string + { + return CheckboxType::class; + } +} diff --git a/tests/API/APIControllerBaseTest.php b/tests/API/APIControllerBaseTest.php index 7751f7de..8b07606e 100644 --- a/tests/API/APIControllerBaseTest.php +++ b/tests/API/APIControllerBaseTest.php @@ -433,6 +433,7 @@ abstract class APIControllerBaseTest extends ControllerBaseTest 'billable' => 'bool', 'color' => '@string', 'customer' => 'int', + 'globalActivities' => 'bool', 'comment' => '@string', ]; @@ -445,6 +446,7 @@ abstract class APIControllerBaseTest extends ControllerBaseTest 'billable' => 'bool', 'color' => '@string', 'customer' => ['result' => 'object', 'type' => 'Customer'], + 'globalActivities' => 'bool', 'comment' => '@string', ]; @@ -461,6 +463,7 @@ abstract class APIControllerBaseTest extends ControllerBaseTest 'parentTitle' => 'string', 'start' => '@datetime', 'end' => '@datetime', + 'globalActivities' => 'bool', 'teams' => ['result' => 'array', 'type' => 'Team'], 'comment' => '@string', ]; @@ -478,6 +481,7 @@ abstract class APIControllerBaseTest extends ControllerBaseTest 'parentTitle' => 'string', 'start' => '@datetime', 'end' => '@datetime', + 'globalActivities' => 'bool', 'teams' => ['result' => 'array', 'type' => 'Team'], 'comment' => '@string', 'budget' => 'float', diff --git a/tests/API/ProjectControllerTest.php b/tests/API/ProjectControllerTest.php index 2d1d5c00..e224837e 100644 --- a/tests/API/ProjectControllerTest.php +++ b/tests/API/ProjectControllerTest.php @@ -298,12 +298,51 @@ class ProjectControllerTest extends APIControllerBaseTest $this->assertIsArray($result); self::assertApiResponseTypeStructure('ProjectEntity', $result); $this->assertNotEmpty($result['id']); + self::assertTrue($result['globalActivities']); self::assertEquals('2018-02-08T13:02:54+0000', $result['orderDate']); self::assertEquals('2019-02-01T19:32:17+0000', $result['start']); self::assertEquals('2020-02-08T21:11:42+0000', $result['end']); self::assertEquals('1234567890/WXYZ/SUBPROJECT/1234/CONTRACT/EMPLOYEE1', $result['orderNumber']); } + public function testPostActionWithOtherFields() + { + $client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN); + $data = [ + 'name' => 'foo', + 'customer' => 1, + 'globalActivities' => '0', + ]; + $this->request($client, '/api/projects', 'POST', [], json_encode($data)); + $this->assertTrue($client->getResponse()->isSuccessful()); + + $result = json_decode($client->getResponse()->getContent(), true); + $this->assertIsArray($result); + self::assertApiResponseTypeStructure('ProjectEntity', $result); + $this->assertNotEmpty($result['id']); + self::assertEquals('foo', $result['name']); + self::assertFalse($result['globalActivities']); + } + + public function testPostActionWithOtherFields2() + { + $client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN); + $data = [ + 'name' => 'foo', + 'customer' => 1, + 'globalActivities' => '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); + self::assertApiResponseTypeStructure('ProjectEntity', $result); + $this->assertNotEmpty($result['id']); + self::assertEquals('foo', $result['name']); + self::assertTrue($result['globalActivities']); + } + public function testPostActionWithLeastFields() { $client = $this->getClientForAuthenticatedUser(User::ROLE_ADMIN);