feat(admin): Ajoute interface d'administration avec Tailwind et Turbo.

🐛 fix(mailer): Corrige l'URL de suivi du mail pour production.

 feat(account): Ajoute la gestion de l'avatar de l'utilisateur.

 feat(account): Ajoute la gestion du premier mot de passe à la connexion.

🗑️ refactor: Supprime les tests unitaires obsolètes.
This commit is contained in:
Serreau Jovann
2025-07-21 09:02:13 +02:00
parent dd7f4730fb
commit 757907821a
31 changed files with 854 additions and 679 deletions

View File

@@ -1,37 +0,0 @@
<?php
namespace App\Tests\Entity;
use App\Entity\Account;
use App\Entity\AccountResetPasswordRequest;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
class AccountResetPasswordRequestTest extends TestCase
{
public function testEntitySettersAndGetters(): void
{
$account = new Account(); // Ou crée un mock si la classe est complexe
$token = 'reset-token-example';
$now = new \DateTimeImmutable();
$expires = $now->modify('+1 hour');
$resetRequest = new AccountResetPasswordRequest();
$resetRequest->setAccount($account);
$resetRequest->setToken($token);
$resetRequest->setRequestedAt($now);
$resetRequest->setExpiresAt($expires);
$refClass = new ReflectionClass($resetRequest);
$idProp = $refClass->getProperty('id');
$idProp->setAccessible(true);
$idProp->setValue($resetRequest, 123);
$this->assertSame($account, $resetRequest->getAccount());
$this->assertSame($token, $resetRequest->getToken());
$this->assertSame($now, $resetRequest->getRequestedAt());
$this->assertSame($expires, $resetRequest->getExpiresAt());
$this->assertSame(123, $resetRequest->getId());
}
}

View File

@@ -1,119 +0,0 @@
<?php
namespace App\Tests\Entity;
use App\Entity\Account;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Validation;
use Symfony\Component\Validator\Validator\ValidatorInterface;
class AccountTest extends TestCase
{
private ValidatorInterface $validator;
protected function setUp(): void
{
// Corrected line: Use enableAttributeMapping() for PHP attributes
$this->validator = Validation::createValidatorBuilder()
->enableAttributeMapping() // Use this for PHP attributes
->getValidator();
}
/**
* Helper to create a valid Account instance.
*/
private function createValidAccount(): Account
{
return (new Account())
->setUsername('testuser')
->setEmail('test@example.com')
->setPassword('securepassword')
->setUuid('1b9d67fe-1b0d-40e9-a417-36e6e2978051');
}
// --- Unit Tests for Getters and Setters ---
public function testGetId(): void
{
$account = new Account();
// ID is typically set by the ORM, so we can't directly test a generated value here.
// We'll rely on functional tests with the database for this.
$this->assertNull($account->getId());
}
public function testSetAndGetUsername(): void
{
$account = new Account();
$account->setUsername('newusername');
$this->assertSame('newusername', $account->getUsername());
}
public function testSetAndGetEmail(): void
{
$account = new Account();
$account->setEmail('newemail@example.com');
$this->assertSame('newemail@example.com', $account->getEmail());
}
public function testSetAndGetPassword(): void
{
$account = new Account();
$account->setPassword('hashedpassword');
$this->assertSame('hashedpassword', $account->getPassword());
}
public function testSetAndGetUuid(): void
{
$account = new Account();
$uuid = 'a1b2c3d4-e5f6-7890-1234-567890abcdef';
$account->setUuid($uuid);
$this->assertSame($uuid, $account->getUuid());
}
public function testSetAndGetRoles(): void
{
$account = new Account();
$account->setRoles(['ROLE_ADMIN', 'ROLE_USER']);
$this->assertContains('ROLE_ADMIN', $account->getRoles());
$this->assertContains('ROLE_USER', $account->getRoles());
$this->assertCount(2, $account->getRoles()); // Because ROLE_USER is guaranteed
}
// --- UserInterface and PasswordAuthenticatedUserInterface Tests ---
public function testGetUserIdentifier(): void
{
$account = $this->createValidAccount();
$this->assertSame('testuser', $account->getUserIdentifier());
}
public function testGetRolesAlwaysIncludesRoleUser(): void
{
$account = new Account();
$this->assertContains('ROLE_USER', $account->getRoles());
$account->setRoles(['ROLE_ADMIN']);
$this->assertContains('ROLE_ADMIN', $account->getRoles());
$this->assertContains('ROLE_USER', $account->getRoles());
$this->assertCount(2, $account->getRoles());
}
public function testEraseCredentials(): void
{
$account = $this->createValidAccount();
// eraseCredentials is deprecated and should not modify password directly in modern Symfony
// It's usually for clearing sensitive data from memory after security operations.
$account->eraseCredentials();
// Assert that password remains, as it's not actually cleared by this method (deprecated behavior)
$this->assertNotNull($account->getPassword());
}
public function testSerializeRemovesSensitiveData(): void
{
$account = $this->createValidAccount();
$serializedAccount = serialize($account);
$this->assertStringContainsString(hash('crc32c', 'securepassword'), $serializedAccount);
$this->assertStringNotContainsString('securepassword', $serializedAccount);
}
}

View File

@@ -1,43 +0,0 @@
<?php
namespace App\Tests\Entity;
use App\Entity\Mail;
use PHPUnit\Framework\TestCase;
class MailTest extends TestCase
{
public function testMailEntity(): void
{
$mail = new Mail();
// Test messageId property
$messageId = 'test_message_id_123';
$mail->setMessageId($messageId);
$this->assertSame($messageId, $mail->getMessageId());
// Test status property
$status = 'sent';
$mail->setStatus($status);
$this->assertSame($status, $mail->getStatus());
// Test dest property
$dest = 'recipient@example.com';
$mail->setDest($dest);
$this->assertSame($dest, $mail->getDest());
// Test subject property
$subject = 'Test Subject';
$mail->setSubject($subject);
$this->assertSame($subject, $mail->getSubject());
// Test content property
$content = 'This is the test email content.';
$mail->setContent($content);
$this->assertSame($content, $mail->getContent());
// Test getId() - should be null initially as it's auto-generated by the database
$this->assertNull($mail->getId());
}
}

View File

@@ -1,208 +0,0 @@
<?php
namespace Tests\App\EventListener;
use App\Attribute\Mainframe;
use App\EventListener\MainframeAttributeListener;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
/**
* Test class for the MainframeAttributeListener.
* This test class has been updated to reflect the removal of logger calls
* from the MainframeAttributeListener.
*/
class MainframeAttributeListenerTest extends TestCase
{
private MainframeAttributeListener $listener;
protected function setUp(): void
{
// No logger mock needed as logger has been removed from the listener
$this->listener = new MainframeAttributeListener();
}
/**
* Helper method to create a ControllerEvent.
*
* @param object $controllerObject The controller instance.
* @param string $methodName The method name to be called.
* @param array $requestAttributes Additional request attributes.
* @return ControllerEvent
*/
private function createControllerEvent(object $controllerObject, string $methodName, array $requestAttributes = []): ControllerEvent
{
$request = new Request([], [], $requestAttributes);
$kernel = $this->createMock(HttpKernelInterface::class);
return new ControllerEvent($kernel, [$controllerObject, $methodName], $request, HttpKernelInterface::MAIN_REQUEST);
}
/**
* Helper method to create a ResponseEvent.
*
* @param Request $request The request object.
* @param Response $response The response object.
* @return ResponseEvent
*/
private function createResponseEvent(Request $request, Response $response): ResponseEvent
{
$kernel = $this->createMock(HttpKernelInterface::class);
return new ResponseEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST, $response);
}
/**
* Tests that the X-Robots-Tag: noindex header is added when index is false on the method.
*/
public function testNoIndexHeaderAddedWhenMethodIndexIsFalse(): void
{
// Dummy controller with Mainframe attribute on method
$controller = new class {
#[Mainframe(index: false, sitemap: true)]
public function testAction(): Response { return new Response(); }
};
// Simulate ControllerEvent
$controllerEvent = $this->createControllerEvent($controller, 'testAction');
$this->listener->onKernelController($controllerEvent);
// Assert that _mainframe_noindex attribute is set to true
$this->assertTrue($controllerEvent->getRequest()->attributes->get('_mainframe_noindex'));
// Simulate ResponseEvent
$response = new Response();
$responseEvent = $this->createResponseEvent($controllerEvent->getRequest(), $response);
$this->listener->onKernelResponse($responseEvent);
// Assert that the X-Robots-Tag header is present and correct
$this->assertTrue($response->headers->has('X-Robots-Tag'));
$this->assertEquals('noindex', $response->headers->get('X-Robots-Tag'));
}
/**
* Tests that the X-Robots-Tag: noindex header is added when index is false on the class
* and not overridden by the method.
*/
public function testNoIndexHeaderAddedWhenClassIndexIsFalse(): void
{
// Dummy controller with Mainframe attribute on class, no attribute on method
$controller = new #[Mainframe(index: false, sitemap: true)] class {
public function testAction(): Response { return new Response(); }
};
// Simulate ControllerEvent
$controllerEvent = $this->createControllerEvent($controller, 'testAction');
$this->listener->onKernelController($controllerEvent);
// Assert that _mainframe_noindex attribute is set to true
$this->assertTrue($controllerEvent->getRequest()->attributes->get('_mainframe_noindex'));
// Simulate ResponseEvent
$response = new Response();
$responseEvent = $this->createResponseEvent($controllerEvent->getRequest(), $response);
$this->listener->onKernelResponse($responseEvent);
// Assert that the X-Robots-Tag header is present and correct
$this->assertTrue($response->headers->has('X-Robots-Tag'));
$this->assertEquals('noindex', $response->headers->get('X-Robots-Tag'));
}
/**
* Tests that no X-Robots-Tag: noindex header is added when index is true on the method.
*/
public function testNoIndexHeaderNotAddedWhenMethodIndexIsTrue(): void
{
// Dummy controller with Mainframe attribute on method (index: true)
$controller = new class {
#[Mainframe(index: true, sitemap: true)]
public function testAction(): Response { return new Response(); }
};
// Simulate ControllerEvent
$controllerEvent = $this->createControllerEvent($controller, 'testAction');
$this->listener->onKernelController($controllerEvent);
// Assert that _mainframe_noindex attribute is set to false
$this->assertFalse($controllerEvent->getRequest()->attributes->get('_mainframe_noindex'));
// Simulate ResponseEvent
$response = new Response();
$responseEvent = $this->createResponseEvent($controllerEvent->getRequest(), $response);
$this->listener->onKernelResponse($responseEvent);
// Assert that the X-Robots-Tag header is NOT present
$this->assertFalse($response->headers->has('X-Robots-Tag'));
}
/**
* Tests that no X-Robots-Tag: noindex header is added when index is true on the class.
*/
public function testNoIndexHeaderNotAddedWhenClassIndexIsTrue(): void
{
// Dummy controller with Mainframe attribute on class (index: true), no attribute on method
$controller = new #[Mainframe(index: true, sitemap: true)] class {
public function testAction(): Response { return new Response(); }
};
// Simulate ControllerEvent
$controllerEvent = $this->createControllerEvent($controller, 'testAction');
$this->listener->onKernelController($controllerEvent);
// Assert that _mainframe_noindex attribute is set to false
$this->assertFalse($controllerEvent->getRequest()->attributes->get('_mainframe_noindex'));
// Simulate ResponseEvent
$response = new Response();
$responseEvent = $this->createResponseEvent($controllerEvent->getRequest(), $response);
$this->listener->onKernelResponse($responseEvent);
// Assert that the X-Robots-Tag header is NOT present
$this->assertFalse($response->headers->has('X-Robots-Tag'));
}
/**
* Tests that no X-Robots-Tag: noindex header is added when no attribute is present.
*/
public function testNoIndexHeaderNotAddedWhenNoAttributePresent(): void
{
// Dummy controller with no Mainframe attribute
$controller = new class {
public function testAction(): Response { return new Response(); }
};
// Simulate ControllerEvent
$controllerEvent = $this->createControllerEvent($controller, 'testAction');
$this->listener->onKernelController($controllerEvent);
// Assert that _mainframe_noindex attribute is set to false (default)
$this->assertFalse($controllerEvent->getRequest()->attributes->get('_mainframe_noindex'));
// Simulate ResponseEvent
$response = new Response();
$responseEvent = $this->createResponseEvent($controllerEvent->getRequest(), $response);
$this->listener->onKernelResponse($responseEvent);
// Assert that the X-Robots-Tag header is NOT present
$this->assertFalse($response->headers->has('X-Robots-Tag'));
}
/**
* Tests that a closure controller is skipped.
*/
public function testClosureControllerIsSkipped(): void
{
$kernel = $this->createMock(HttpKernelInterface::class);
$request = new Request();
$controller = function() { return new Response(); }; // A closure controller
$controllerEvent = new ControllerEvent($kernel, $controller, $request, HttpKernelInterface::MAIN_REQUEST);
$this->listener->onKernelController($controllerEvent);
// Ensure no _mainframe_noindex attribute is set
$this->assertFalse($request->attributes->has('_mainframe_noindex'));
}
}

View File

@@ -1,55 +0,0 @@
<?php
namespace App\Tests\EventListener;
use App\EventListener\SitemapSubscriber;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Presta\SitemapBundle\Event\SitemapPopulateEvent;
use Presta\SitemapBundle\Service\UrlContainerInterface;
use Presta\SitemapBundle\Sitemap\Url\UrlConcrete;
class SitemapSubscriberTest extends TestCase
{
/**
* @var UrlGeneratorInterface|\PHPUnit\Framework\MockObject\MockObject
*/
private $urlGenerator;
/**
* @var UrlContainerInterface|\PHPUnit\Framework\MockObject\MockObject
*/
private $urlContainer;
protected function setUp(): void
{
// Mock the UrlGeneratorInterface
$this->urlGenerator = $this->createMock(UrlGeneratorInterface::class);
// Mock the UrlContainerInterface
$this->urlContainer = $this->createMock(UrlContainerInterface::class);
}
public function testGetSubscribedEvents(): void
{
// Assert that the getSubscribedEvents method returns the correct event.
$expectedEvents = [
SitemapPopulateEvent::class => 'populate',
];
$this->assertEquals($expectedEvents, SitemapSubscriber::getSubscribedEvents());
}
public function testPopulate(): void
{
// Create an instance of the subscriber
$subscriber = new SitemapSubscriber();
// Create a mock for SitemapPopulateEvent
$event = new SitemapPopulateEvent($this->urlContainer, $this->urlGenerator);
// Call the populate method
$subscriber->populate($event);
$this->assertEquals("a","a");
}
}

View File

@@ -1,129 +0,0 @@
<?php
namespace App\Tests\Repository;
use App\Entity\Account;
use App\Repository\AccountRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
/**
* Test class for AccountRepository.
*
* This class extends KernelTestCase to allow access to the Symfony container
* and test the repository methods with a real database connection (or mock it).
*/
class AccountRepositoryTest extends KernelTestCase
{
private ?EntityManagerInterface $entityManager;
private ?AccountRepository $accountRepository;
/**
* Sets up the test environment before each test method.
*
* This method boots the Symfony kernel and retrieves the EntityManager
* and AccountRepository from the service container. It also begins a database transaction
* to ensure a clean state for each test.
*/
protected function setUp(): void
{
// Boot the Symfony kernel to access services
self::bootKernel();
// Get the EntityManager from the container
// Corrected service name from 'doctrine.orm.entity.manager' to 'doctrine.orm.entity_manager'
$this->entityManager = static::getContainer()->get('doctrine.orm.entity_manager');
// Get the AccountRepository from the container
$this->accountRepository = static::getContainer()->get(AccountRepository::class);
// Begin a transaction to ensure a clean database state for each test.
// All changes made during the test will be rolled back in tearDown.
$this->entityManager->beginTransaction();
}
/**
* Tests the upgradePassword method with a valid Account instance.
*
* This test verifies that the password of an Account object is correctly
* updated and persisted when upgradePassword is called.
*/
public function testUpgradePassword(): void
{
// Create a mock Account entity for testing
$account = new Account();
$account->setEmail('test@example.com');
// Add a username as it seems to be a non-nullable field based on previous errors
$account->setUsername('test_user');
// Add a UUID in the correct format. For real applications, consider using a UUID library
// like Ramsey\Uuid (e.g., Uuid::uuid4()->toString()). For testing, a valid string literal is fine.
$account->setUuid('123e4567-e89b-12d3-a456-426614174000'); // Example of a valid UUID format
$account->setPassword('old_hashed_password'); // Set an initial password
$newHashedPassword = 'new_hashed_password';
// Call the upgradePassword method
$this->accountRepository->upgradePassword($account, $newHashedPassword);
// Assert that the account's password has been updated
$this->assertEquals($newHashedPassword, $account->getPassword(), 'The account password should be updated.');
// In a real test, you might want to mock the EntityManager's persist and flush calls
// to avoid actual database interaction if you're unit testing the repository in isolation.
// For an integration test with a real database, you'd check if the change is reflected
// by fetching the entity again.
// Example of how you might assert that persist and flush were called if mocking:
// $mockEntityManager = $this->createMock(EntityManagerInterface::class);
// $mockEntityManager->expects($this->once())->method('persist')->with($account);
// $mockEntityManager->expects($this->once())->method('flush');
// // Then inject this mock into your repository if it were a pure unit test.
}
/**
* Tests the upgradePassword method with an unsupported user type.
*
* This test verifies that an UnsupportedUserException is thrown when
* upgradePassword is called with a user object that is not an instance of Account.
*/
public function testUpgradePasswordWithUnsupportedUser(): void
{
// Expect an UnsupportedUserException to be thrown
$this->expectException(UnsupportedUserException::class);
// The message will contain the class name of the mock object, which implements the interface
// Create a mock object that implements PasswordAuthenticatedUserInterface
// but is NOT an instance of Account. This is crucial to trigger the UnsupportedUserException
// from the repository's internal check, rather than a TypeError from the method signature.
$unsupportedUserMock = $this->createMock(PasswordAuthenticatedUserInterface::class);
// Pass this mock object to the upgradePassword method.
$this->accountRepository->upgradePassword($unsupportedUserMock, 'some_password');
}
/**
* Cleans up the test environment after each test method.
*
* This method rolls back the database transaction started in setUp,
* effectively undoing any changes made during the test, and then closes
* the EntityManager to release database resources.
*/
protected function tearDown(): void
{
parent::tearDown();
// Check if a transaction is active before attempting to roll back
if ($this->entityManager && $this->entityManager->getConnection()->isTransactionActive()) {
$this->entityManager->rollback();
}
// Close the EntityManager to prevent memory leaks
if ($this->entityManager) {
$this->entityManager->close();
$this->entityManager = null; // Avoid memory leaks
}
$this->accountRepository = null;
}
}

View File

@@ -1,36 +0,0 @@
<?php
namespace App\Tests\Repository;
use App\Entity\AccountResetPasswordRequest;
use App\Repository\AccountResetPasswordRequestRepository;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Doctrine\ORM\EntityManagerInterface;
class AccountResetPasswordRequestRepositoryTest extends KernelTestCase
{
private ?EntityManagerInterface $entityManager;
private ?AccountResetPasswordRequestRepository $accountResetPasswordRequestRepository;
protected function setUp(): void
{
self::bootKernel();
$this->entityManager = self::getContainer()->get('doctrine')->getManager();
$this->accountResetPasswordRequestRepository = $this->entityManager->getRepository(AccountResetPasswordRequest::class);
}
public function testRepositoryExistsAndIsCorrectInstance(): void
{
$this->assertInstanceOf(AccountResetPasswordRequestRepository::class, $this->accountResetPasswordRequestRepository);
}
protected function tearDown(): void
{
parent::tearDown();
$this->entityManager->close();
$this->entityManager = null; // Avoid memory leaks
$this->accountResetPasswordRequestRepository = null;
}
}

View File

@@ -1,36 +0,0 @@
<?php
namespace App\Tests\Repository;
use App\Entity\Mail;
use App\Repository\MailRepository;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Doctrine\ORM\EntityManagerInterface;
class MailRepositoryTest extends KernelTestCase
{
private ?EntityManagerInterface $entityManager;
private ?MailRepository $mailRepository;
protected function setUp(): void
{
self::bootKernel();
$this->entityManager = self::getContainer()->get('doctrine')->getManager();
$this->mailRepository = $this->entityManager->getRepository(Mail::class);
}
public function testRepositoryExistsAndIsCorrectInstance(): void
{
$this->assertInstanceOf(MailRepository::class, $this->mailRepository);
}
protected function tearDown(): void
{
parent::tearDown();
$this->entityManager->close();
$this->entityManager = null; // Avoid memory leaks
$this->mailRepository = null;
}
}