diff --git a/Inventory_frontend b/Inventory_frontend index 165e0a6..98f5d98 160000 --- a/Inventory_frontend +++ b/Inventory_frontend @@ -1 +1 @@ -Subproject commit 165e0a634120bcc0254a3c3b7187dcd8032f764d +Subproject commit 98f5d983b38945babfc24be8ac11c8ef3d1a9872 diff --git a/src/Controller/MachineStructureController.php b/src/Controller/MachineStructureController.php index 50361d4..00d39ba 100644 --- a/src/Controller/MachineStructureController.php +++ b/src/Controller/MachineStructureController.php @@ -573,7 +573,7 @@ class MachineStructureController extends AbstractController 'constructeurs' => $this->normalizeConstructeurs($machine->getConstructeurs()), 'customFields' => $this->normalizeCustomFields($machine->getCustomFields()), 'documents' => null, - 'customFieldValues' => null, + 'customFieldValues' => $this->normalizeCustomFieldValues($machine->getCustomFieldValues()), ]; } diff --git a/tests/Api/Controller/MachineCustomFieldDefinitionTest.php b/tests/Api/Controller/MachineCustomFieldDefinitionTest.php new file mode 100644 index 0000000..bc5f1c7 --- /dev/null +++ b/tests/Api/Controller/MachineCustomFieldDefinitionTest.php @@ -0,0 +1,122 @@ +createMachine('Machine CF Create'); + + $client = $this->createGestionnaireClient(); + $client->request('POST', '/api/custom_fields', [ + 'headers' => ['Content-Type' => 'application/ld+json'], + 'json' => [ + 'name' => 'Tension', + 'type' => 'number', + 'machine' => self::iri('machines', $machine->getId()), + ], + ]); + + $this->assertResponseStatusCodeSame(201); + $this->assertJsonContains(['name' => 'Tension', 'type' => 'number']); + + // Verify via structure endpoint + $client->request('GET', sprintf('/api/machines/%s/structure', $machine->getId())); + $this->assertResponseIsSuccessful(); + + $data = $client->getResponse()->toArray(); + $this->assertArrayHasKey('customFields', $data['machine']); + $this->assertNotEmpty($data['machine']['customFields'], 'customFields should contain the new field'); + + $found = false; + foreach ($data['machine']['customFields'] as $cf) { + if ('Tension' === $cf['name'] && 'number' === $cf['type']) { + $found = true; + + break; + } + } + $this->assertTrue($found, 'Structure should contain the created custom field "Tension"'); + } + + public function testUpdateCustomFieldForMachine(): void + { + $machine = $this->createMachine('Machine CF Update'); + $cf = $this->createCustomField('OldName', 'text', $machine); + + $client = $this->createGestionnaireClient(); + $client->request('PATCH', self::iri('custom_fields', $cf->getId()), [ + 'headers' => ['Content-Type' => 'application/merge-patch+json'], + 'json' => ['name' => 'NewName'], + ]); + + $this->assertResponseIsSuccessful(); + $this->assertJsonContains(['name' => 'NewName']); + + // Verify via structure endpoint + $client->request('GET', sprintf('/api/machines/%s/structure', $machine->getId())); + $this->assertResponseIsSuccessful(); + + $data = $client->getResponse()->toArray(); + $this->assertNotEmpty($data['machine']['customFields']); + + $names = array_column($data['machine']['customFields'], 'name'); + $this->assertContains('NewName', $names, 'Structure should reflect the updated custom field name'); + $this->assertNotContains('OldName', $names, 'Old name should no longer appear'); + } + + public function testDeleteCustomFieldForMachine(): void + { + $machine = $this->createMachine('Machine CF Delete'); + $cf = $this->createCustomField('ToDelete', 'text', $machine); + + $client = $this->createGestionnaireClient(); + $client->request('DELETE', self::iri('custom_fields', $cf->getId())); + + $this->assertResponseStatusCodeSame(204); + + // Verify via structure endpoint + $client->request('GET', sprintf('/api/machines/%s/structure', $machine->getId())); + $this->assertResponseIsSuccessful(); + + $data = $client->getResponse()->toArray(); + $this->assertSame([], $data['machine']['customFields'], 'customFields should be empty after deletion'); + } + + public function testAddCustomFieldsEndpointInitializesValues(): void + { + $machine = $this->createMachine('Machine CF Values'); + $cf = $this->createCustomField('Voltage', 'number', $machine); + $cf->setDefaultValue('220'); + $em = $this->getEntityManager(); + $em->persist($cf); + $em->flush(); + + $client = $this->createGestionnaireClient(); + $client->request('POST', sprintf('/api/machines/%s/add-custom-fields', $machine->getId())); + + $this->assertResponseIsSuccessful(); + $response = $client->getResponse()->toArray(); + $this->assertTrue($response['success']); + $this->assertNotEmpty($response['customFieldValues'], 'customFieldValues should not be empty after add-custom-fields'); + + // Verify via structure endpoint + $client->request('GET', sprintf('/api/machines/%s/structure', $machine->getId())); + $this->assertResponseIsSuccessful(); + + $data = $client->getResponse()->toArray(); + $this->assertNotEmpty($data['machine']['customFieldValues'], 'Structure should contain initialized custom field values'); + + $cfv = $data['machine']['customFieldValues'][0]; + $this->assertSame('220', $cfv['value'], 'Value should match the default value'); + $this->assertSame('Voltage', $cfv['customField']['name']); + } +} diff --git a/tests/Api/Controller/MachineStructureCustomFieldValuesTest.php b/tests/Api/Controller/MachineStructureCustomFieldValuesTest.php new file mode 100644 index 0000000..b5a898e --- /dev/null +++ b/tests/Api/Controller/MachineStructureCustomFieldValuesTest.php @@ -0,0 +1,51 @@ +createMachine('Machine CFV'); + $cf = $this->createCustomField('Tension', 'number', $machine); + $this->createCustomFieldValue($cf, '220', $machine); + + $client = $this->createViewerClient(); + $client->request('GET', sprintf('/api/machines/%s/structure', $machine->getId())); + + $this->assertResponseIsSuccessful(); + $data = $client->getResponse()->toArray(); + + $this->assertArrayHasKey('machine', $data); + $this->assertArrayHasKey('customFieldValues', $data['machine']); + $this->assertIsArray($data['machine']['customFieldValues']); + $this->assertNotEmpty($data['machine']['customFieldValues'], 'customFieldValues should not be empty'); + + $cfv = $data['machine']['customFieldValues'][0]; + $this->assertSame('220', $cfv['value']); + $this->assertArrayHasKey('customField', $cfv); + $this->assertSame('Tension', $cfv['customField']['name']); + $this->assertSame('number', $cfv['customField']['type']); + } + + public function testStructureReturnsEmptyArrayWhenNoCustomFieldValues(): void + { + $machine = $this->createMachine('Machine sans CFV'); + + $client = $this->createViewerClient(); + $client->request('GET', sprintf('/api/machines/%s/structure', $machine->getId())); + + $this->assertResponseIsSuccessful(); + $data = $client->getResponse()->toArray(); + + $this->assertArrayHasKey('customFieldValues', $data['machine']); + $this->assertSame([], $data['machine']['customFieldValues']); + } +}