diff --git a/migrations/Version20260710114751.php b/migrations/Version20260710114751.php new file mode 100644 index 0000000..09b7a50 --- /dev/null +++ b/migrations/Version20260710114751.php @@ -0,0 +1,41 @@ +addSql('ALTER TABLE image DROP FOREIGN KEY FK_C53D045FED42A742'); + $this->addSql('DROP INDEX IDX_C53D045FED42A742 ON image'); + $this->addSql('ALTER TABLE image DROP software_profile_id'); + $this->addSql('ALTER TABLE image_image_repository ADD software_profile_id INT DEFAULT NULL'); + $this->addSql('ALTER TABLE image_image_repository ADD CONSTRAINT FK_B7851337ED42A742 FOREIGN KEY (software_profile_id) REFERENCES software_profile (id)'); + $this->addSql('CREATE INDEX IDX_B7851337ED42A742 ON image_image_repository (software_profile_id)'); + } + + public function down(Schema $schema): void + { + $this->addSql('ALTER TABLE image_image_repository DROP FOREIGN KEY FK_B7851337ED42A742'); + $this->addSql('DROP INDEX IDX_B7851337ED42A742 ON image_image_repository'); + $this->addSql('ALTER TABLE image_image_repository DROP software_profile_id'); + $this->addSql('ALTER TABLE image ADD software_profile_id INT DEFAULT NULL'); + $this->addSql('ALTER TABLE image ADD CONSTRAINT FK_C53D045FED42A742 FOREIGN KEY (software_profile_id) REFERENCES software_profile (id)'); + $this->addSql('CREATE INDEX IDX_C53D045FED42A742 ON image (software_profile_id)'); + } +} diff --git a/src/Dto/Input/ImageImageRepositoryInput.php b/src/Dto/Input/ImageImageRepositoryInput.php index 80303e1..ff8f0b0 100644 --- a/src/Dto/Input/ImageImageRepositoryInput.php +++ b/src/Dto/Input/ImageImageRepositoryInput.php @@ -5,6 +5,7 @@ namespace App\Dto\Input; use ApiPlatform\Metadata\ApiProperty; use App\Dto\Output\ImageOutput; use App\Dto\Output\ImageRepositoryOutput; +use App\Dto\Output\SoftwareProfileOutput; use App\Entity\ImageImageRepository; use App\Entity\PxeTemplate; use Symfony\Component\Serializer\Annotation\Groups; @@ -19,6 +20,10 @@ final class ImageImageRepositoryInput #[ApiProperty(description: 'Whether this image is available for Remote PC in this repository', example: false)] public ?bool $remotePc = false; + #[Groups(['image-image-repository:write'])] + #[ApiProperty(description: 'El perfil de software detectado en esta versión de la imagen')] + public ?SoftwareProfileOutput $softwareProfile = null; + public function __construct(?ImageImageRepository $imageImageRepository = null) { if (!$imageImageRepository) { @@ -27,6 +32,10 @@ final class ImageImageRepositoryInput $this->description = $imageImageRepository->getDescription(); $this->remotePc = $imageImageRepository->isRemotePc(); + + if ($imageImageRepository->getSoftwareProfile()) { + $this->softwareProfile = new SoftwareProfileOutput($imageImageRepository->getSoftwareProfile(), false); + } } public function createOrUpdateEntity(?ImageImageRepository $imageImageRepository = null): ImageImageRepository @@ -37,6 +46,7 @@ final class ImageImageRepositoryInput $imageImageRepository->setDescription($this->description); $imageImageRepository->setRemotePc($this->remotePc); + $imageImageRepository->setSoftwareProfile($this->softwareProfile?->getEntity()); return $imageImageRepository; } diff --git a/src/Dto/Input/ImageInput.php b/src/Dto/Input/ImageInput.php index b611f4b..e2807cf 100644 --- a/src/Dto/Input/ImageInput.php +++ b/src/Dto/Input/ImageInput.php @@ -10,7 +10,6 @@ use App\Dto\Output\ImageRepositoryOutput; use App\Dto\Output\OrganizationalUnitOutput; use App\Dto\Output\PartitionOutput; use App\Dto\Output\RemoteCalendarRuleOutput; -use App\Dto\Output\SoftwareProfileOutput; use App\Entity\Image; use App\Entity\ImageImageRepository; use App\Entity\OrganizationalUnit; @@ -33,10 +32,6 @@ final class ImageInput #[ApiProperty(description: 'The optional selected image')] public ?ImageOutput $selectedImage = null; - #[Groups(['image:write'])] - #[ApiProperty(description: 'The software profile of the image')] - public ?SoftwareProfileOutput $softwareProfile = null; - /** * @var ImageRepositoryOutput[] */ @@ -83,10 +78,6 @@ final class ImageInput $this->isGlobal = $image->isGlobal(); $this->version = $image->getVersion(); - if ($image->getSoftwareProfile()) { - $this->softwareProfile = new SoftwareProfileOutput($image->getSoftwareProfile()); - } - if ($image->getClient()) { $this->client = new ClientOutput($image->getClient()); } @@ -109,10 +100,6 @@ final class ImageInput $image->setName($this->name); $image->setType($this->type); - if ($this->softwareProfile) { - $image->setSoftwareProfile($this->softwareProfile->getEntity()); - } - if ($this->client) { $image->setClient($this->client->getEntity()); } diff --git a/src/Dto/Output/ClientOutput.php b/src/Dto/Output/ClientOutput.php index 2391f60..bdf707f 100644 --- a/src/Dto/Output/ClientOutput.php +++ b/src/Dto/Output/ClientOutput.php @@ -94,7 +94,7 @@ final class ClientOutput extends AbstractOutput #[Groups(['client:read'])] #[ApiProperty( - description: 'El perfil de hardware propio del cliente (registro 1:1, gestionado desde /hardware-profiles)', + description: 'El perfil de hardware asignado al cliente, normalmente heredado del aula y compartido con otros clientes de la misma', example: null )] public ?HardwareProfileOutput $hardwareProfile = null; @@ -219,7 +219,7 @@ final class ClientOutput extends AbstractOutput $this->menu = $client->getMenu() ? new MenuOutput($client->getMenu()) : null; $this->hardwareProfile = $client->getHardwareProfile() - ? new HardwareProfileOutput($client->getHardwareProfile(), includeClient: false) + ? new HardwareProfileOutput($client->getHardwareProfile(), includeClients: false) : null; $this->position = $client->getPosition(); diff --git a/src/Dto/Output/ImageImageRepositoryOutput.php b/src/Dto/Output/ImageImageRepositoryOutput.php index a0eda0f..0cbe9d0 100644 --- a/src/Dto/Output/ImageImageRepositoryOutput.php +++ b/src/Dto/Output/ImageImageRepositoryOutput.php @@ -24,6 +24,13 @@ class ImageImageRepositoryOutput extends AbstractOutput )] public ?ImageRepositoryOutput $imageRepository = null; + #[Groups(['image-image-repository:read', 'image:read'])] + #[ApiProperty( + description: 'El perfil de software detectado en esta versión de la imagen', + example: null + )] + public ?SoftwareProfileOutput $softwareProfile = null; + #[Groups(['image-image-repository:read', 'image:read'])] #[ApiProperty( description: 'El estado de la imagen en el repositorio', @@ -115,6 +122,10 @@ class ImageImageRepositoryOutput extends AbstractOutput $this->imageRepository = new ImageRepositoryOutput($imageImageRepository->getRepository()); } + if ($imageImageRepository->getSoftwareProfile()) { + $this->softwareProfile = new SoftwareProfileOutput($imageImageRepository->getSoftwareProfile(), false); + } + $this->name = $imageImageRepository->getName(); $this->version = $imageImageRepository->getVersion(); $this->partitionInfo = json_decode($imageImageRepository->getPartitionInfo(), true); diff --git a/src/Dto/Output/ImageOutput.php b/src/Dto/Output/ImageOutput.php index 48e8284..1c9fb6e 100644 --- a/src/Dto/Output/ImageOutput.php +++ b/src/Dto/Output/ImageOutput.php @@ -34,13 +34,6 @@ final class ImageOutput extends AbstractOutput )] public ?string $type = ''; - #[Groups(['image:read'])] - #[ApiProperty( - description: 'El perfil de software asociado a la imagen', - example: null - )] - public ?SoftwareProfileOutput $softwareProfile = null; - /** * @var ImageRepositoryOutput[]|null */ @@ -84,7 +77,6 @@ final class ImageOutput extends AbstractOutput parent::__construct($image); $this->name = $image->getName(); - $this->softwareProfile = $image->getSoftwareProfile() ? new SoftwareProfileOutput($image->getSoftwareProfile(), false) : null; $this->imageRepositories = $image->getImageImageRepositories()->map( fn(ImageImageRepository $image) => new ImageImageRepositoryOutput($image) diff --git a/src/Dto/Output/SoftwareProfileOutput.php b/src/Dto/Output/SoftwareProfileOutput.php index 715b935..006d854 100644 --- a/src/Dto/Output/SoftwareProfileOutput.php +++ b/src/Dto/Output/SoftwareProfileOutput.php @@ -12,7 +12,7 @@ use Symfony\Component\Serializer\Annotation\Groups; #[Get(shortName: 'SoftwareProfile')] final class SoftwareProfileOutput extends AbstractOutput { - #[Groups(['software-profile:read', 'image:read'])] + #[Groups(['software-profile:read', 'image:read', 'image-image-repository:read'])] #[ApiProperty( description: 'La descripción del perfil de software', example: 'Perfil estándar para aula de informática' diff --git a/src/Entity/Image.php b/src/Entity/Image.php index 09e51fe..332ecdb 100644 --- a/src/Entity/Image.php +++ b/src/Entity/Image.php @@ -15,10 +15,6 @@ class Image extends AbstractEntity { use NameableTrait; - #[ORM\ManyToOne] - #[ORM\JoinColumn(nullable: true)] - private ?SoftwareProfile $softwareProfile = null; - #[ORM\Column(length: 255, nullable: true)] private ?string $partitionInfo = null; @@ -50,18 +46,6 @@ class Image extends AbstractEntity $this->imageImageRepositories = new ArrayCollection(); } - public function getSoftwareProfile(): ?SoftwareProfile - { - return $this->softwareProfile; - } - - public function setSoftwareProfile(?SoftwareProfile $softwareProfile): static - { - $this->softwareProfile = $softwareProfile; - - return $this; - } - public function getPartitionInfo(): ?string { return $this->partitionInfo; diff --git a/src/Entity/ImageImageRepository.php b/src/Entity/ImageImageRepository.php index ea6fc2d..a238b75 100644 --- a/src/Entity/ImageImageRepository.php +++ b/src/Entity/ImageImageRepository.php @@ -17,6 +17,10 @@ class ImageImageRepository extends AbstractEntity #[ORM\JoinColumn(nullable: false)] private ?ImageRepository $repository = null; + #[ORM\ManyToOne] + #[ORM\JoinColumn(nullable: true)] + private ?SoftwareProfile $softwareProfile = null; + #[ORM\Column(length: 255)] private ?string $status = null; @@ -71,6 +75,18 @@ class ImageImageRepository extends AbstractEntity return $this; } + public function getSoftwareProfile(): ?SoftwareProfile + { + return $this->softwareProfile; + } + + public function setSoftwareProfile(?SoftwareProfile $softwareProfile): static + { + $this->softwareProfile = $softwareProfile; + + return $this; + } + public function getStatus(): ?string { return $this->status; diff --git a/src/Factory/ImageFactory.php b/src/Factory/ImageFactory.php index 9a2e2d4..c3d20f0 100644 --- a/src/Factory/ImageFactory.php +++ b/src/Factory/ImageFactory.php @@ -35,7 +35,6 @@ final class ImageFactory extends ModelFactory return [ 'createdAt' => self::faker()->dateTime(), 'name' => self::faker()->text(255), - 'softwareProfile' => SoftwareProfileFactory::new(), 'updatedAt' => self::faker()->dateTime(), 'type' => 'monolithic', 'isGlobal' => false, diff --git a/src/Factory/SoftwareProfileFactory.php b/src/Factory/SoftwareProfileFactory.php index 2f368a8..43b4655 100644 --- a/src/Factory/SoftwareProfileFactory.php +++ b/src/Factory/SoftwareProfileFactory.php @@ -3,7 +3,6 @@ namespace App\Factory; use App\Entity\SoftwareProfile; -use App\Factory\ImageFactory; use App\Repository\SoftwareProfileRepository; use Zenstruck\Foundry\ModelFactory; use Zenstruck\Foundry\Persistence\PersistentProxyObjectFactory; @@ -37,7 +36,6 @@ final class SoftwareProfileFactory extends ModelFactory 'description' => self::faker()->text(255), 'updatedAt' => self::faker()->dateTime(), 'operativeSystem' => OperativeSystemFactory::createOne()->_save(), - 'image' => ImageFactory::createOne()->_save(), ]; } diff --git a/tests/Functional/CreateSoftwareProfileFromInventoryTest.php b/tests/Functional/CreateSoftwareProfileFromInventoryTest.php new file mode 100644 index 0000000..85b7e11 --- /dev/null +++ b/tests/Functional/CreateSoftwareProfileFromInventoryTest.php @@ -0,0 +1,86 @@ + 'ogadmin', 'roles' => [UserGroupPermissions::ROLE_SUPER_ADMIN]]); + + SoftwareFactory::createOne(['name' => 'Firefox']); + + /** @var ImageImageRepository $imageImageRepository */ + $imageImageRepository = ImageImageRepositoryFactory::new([ + 'image' => ImageFactory::new(['client' => ClientFactory::new()]), + ])->create()->_real(); + + $base64Inventory = base64_encode("Firefox\nChrome\n"); + + $statusController = static::getContainer()->get(StatusController::class); + $statusController->createSoftwareProfile($base64Inventory, $imageImageRepository); + + static::getContainer()->get('doctrine')->getManager()->refresh($imageImageRepository); + + $softwareProfile = $imageImageRepository->getSoftwareProfile(); + $this->assertNotNull($softwareProfile, 'El perfil de software debe quedar asignado a la versión (ImageImageRepository)'); + + $softwareNames = $softwareProfile->getSoftwareCollection()->map(fn($software) => $software->getName())->toArray(); + sort($softwareNames); + $this->assertSame(['Chrome', 'Firefox'], $softwareNames); + } + + /** + * Cubre una regresión concreta: al listar /image-image-repositories, el + * softwareProfile embebido debe incluir "description" (el frontend lo + * usa para mostrar el perfil asignado). Se perdía porque la propiedad no + * estaba etiquetada con el grupo de serialización image-image-repository:read. + * + * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface + * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface + * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface + * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface + * @throws \Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface + */ + public function testSoftwareProfileDescriptionIsVisibleWhenListingImageImageRepositories(): void + { + UserFactory::createOne(['username' => 'ogadmin', 'roles' => [UserGroupPermissions::ROLE_SUPER_ADMIN]]); + + SoftwareFactory::createOne(['name' => 'Firefox']); + + /** @var ImageImageRepository $imageImageRepository */ + $imageImageRepository = ImageImageRepositoryFactory::new([ + 'image' => ImageFactory::new(['client' => ClientFactory::new()]), + ])->create()->_real(); + + $statusController = static::getContainer()->get(StatusController::class); + $statusController->createSoftwareProfile(base64_encode("Firefox\n"), $imageImageRepository); + + $iri = $this->findIriBy(ImageImageRepository::class, ['id' => $imageImageRepository->getId()]); + + $client = $this->createClientWithCredentials(); + $client->request('GET', '/image-image-repositories'); + + $this->assertResponseIsSuccessful(); + $data = $client->getResponse()->toArray(); + $member = current(array_filter($data['hydra:member'], fn($item) => $item['@id'] === $iri)); + + $this->assertNotFalse($member, 'La versión creada debe aparecer en el listado'); + $this->assertNotEmpty($member['softwareProfile']['description'] ?? null, 'El listado debe incluir la descripción del perfil de software asignado'); + } +} diff --git a/tests/Functional/SoftwareProfileTest.php b/tests/Functional/SoftwareProfileTest.php index 8d2ac62..f228cac 100644 --- a/tests/Functional/SoftwareProfileTest.php +++ b/tests/Functional/SoftwareProfileTest.php @@ -3,12 +3,11 @@ namespace Functional; -use App\Entity\OrganizationalUnit; +use App\Entity\Software; use App\Entity\SoftwareProfile; -use App\Factory\OrganizationalUnitFactory; +use App\Factory\SoftwareFactory; use App\Factory\SoftwareProfileFactory; use App\Factory\UserFactory; -use App\Model\OrganizationalUnitTypes; use App\Model\UserGroupPermissions; use Symfony\Component\HttpFoundation\Response; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; @@ -59,12 +58,8 @@ class SoftwareProfileTest extends AbstractTest { UserFactory::createOne(['username' => self::USER_ADMIN, 'roles'=> [UserGroupPermissions::ROLE_SUPER_ADMIN]]); - OrganizationalUnitFactory::createOne(['type' => OrganizationalUnitTypes::ORGANIZATIONAL_UNIT]); - $ouIri = $this->findIriBy(OrganizationalUnit::class, ['type' => OrganizationalUnitTypes::ORGANIZATIONAL_UNIT]); - $this->createClientWithCredentials()->request('POST', '/software-profiles',['json' => [ 'description' => self::SW_PROFILE_CREATE, - 'organizationalUnit' => $ouIri ]]); $this->assertResponseStatusCodeSame(201); @@ -76,6 +71,51 @@ class SoftwareProfileTest extends AbstractTest ]); } + /** + * Verifica que el software seleccionado en el formulario se persiste + * (softwareCollection) y que se puede añadir y quitar software después. + * + * @throws RedirectionExceptionInterface + * @throws DecodingExceptionInterface + * @throws ClientExceptionInterface + * @throws TransportExceptionInterface + * @throws ServerExceptionInterface + */ + public function testSoftwareProfileSoftwareCollectionIsPersisted(): void + { + UserFactory::createOne(['username' => self::USER_ADMIN, 'roles'=> [UserGroupPermissions::ROLE_SUPER_ADMIN]]); + + SoftwareFactory::createOne(['name' => 'test-software-a']); + SoftwareFactory::createOne(['name' => 'test-software-b']); + $softwareAIri = $this->findIriBy(Software::class, ['name' => 'test-software-a']); + $softwareBIri = $this->findIriBy(Software::class, ['name' => 'test-software-b']); + + $client = $this->createClientWithCredentials(); + + $client->request('POST', '/software-profiles', ['json' => [ + 'description' => 'test-sw-profile-collection', + 'softwareCollection' => [$softwareAIri, $softwareBIri], + ]]); + $this->assertResponseStatusCodeSame(201); + $iri = $client->getResponse()->toArray()['@id']; + + $client->request('GET', $iri); + $this->assertResponseIsSuccessful(); + $data = $client->getResponse()->toArray(); + $this->assertCount(2, $data['softwareCollection']); + + $client->request('PUT', $iri, ['json' => [ + 'description' => 'test-sw-profile-collection', + 'softwareCollection' => [$softwareAIri], + ]]); + $this->assertResponseIsSuccessful(); + + $client->request('GET', $iri); + $data = $client->getResponse()->toArray(); + $this->assertCount(1, $data['softwareCollection']); + $this->assertSame($softwareAIri, $data['softwareCollection'][0]['@id']); + } + /** * @throws RedirectionExceptionInterface * @throws DecodingExceptionInterface