ref 3007. Updated software profile image entity.

pull/125/head
Manuel Aranda Rosales 2026-07-13 10:38:22 +02:00
parent 8b78296a86
commit 3390734b10
13 changed files with 214 additions and 50 deletions

View File

@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Mueve el perfil de software de Image (entidad lógica) a ImageImageRepository
* (la versión/copia concreta de la imagen en un repositorio), para poder
* conservar y editar el perfil de software por versión.
*/
final class Version20260710114751 extends AbstractMigration
{
public function getDescription(): string
{
return 'Mueve software_profile_id de image a image_image_repository';
}
public function up(Schema $schema): void
{
$this->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)');
}
}

View File

@ -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;
}

View File

@ -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());
}

View File

@ -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();

View File

@ -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);

View File

@ -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)

View File

@ -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'

View File

@ -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;

View File

@ -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;

View File

@ -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,

View File

@ -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(),
];
}

View File

@ -0,0 +1,86 @@
<?php
namespace Functional;
use App\Controller\OgAgent\Webhook\StatusController;
use App\Entity\ImageImageRepository;
use App\Factory\ClientFactory;
use App\Factory\ImageFactory;
use App\Factory\ImageImageRepositoryFactory;
use App\Factory\SoftwareFactory;
use App\Factory\UserFactory;
use App\Model\UserGroupPermissions;
/**
* Verifica que el inventario de software recibido al crear/inventariar una
* imagen se asocia a la versión concreta (ImageImageRepository) y no a la
* imagen lógica (Image), y que tanto el software ya existente como el
* software nuevo detectado quedan enlazados al perfil generado.
*/
class CreateSoftwareProfileFromInventoryTest extends AbstractTest
{
public function testCreateSoftwareProfileLinksExistingAndNewSoftwareToTheVersion(): 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();
$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');
}
}

View File

@ -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