Files
crm_ecosplay/tests/Service/RgpdServiceTest.php

294 lines
11 KiB
PHP
Raw Normal View History

<?php
namespace App\Tests\Service;
use App\Entity\AnalyticsEvent;
use App\Entity\AnalyticsUniqId;
use App\Entity\Attestation;
use App\Service\DocuSealService;
2026-04-07 23:50:19 +02:00
use App\Service\MailerService;
use App\Service\RgpdService;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Twig\Environment;
class RgpdServiceTest extends TestCase
{
private EntityManagerInterface $em;
private Environment $twig;
private DocuSealService $docuSealService;
2026-04-07 23:50:19 +02:00
private MailerService $mailer;
private UrlGeneratorInterface $urlGenerator;
private string $projectDir;
private RgpdService $service;
protected function setUp(): void
{
$this->em = $this->createStub(EntityManagerInterface::class);
$this->twig = $this->createStub(Environment::class);
$this->docuSealService = $this->createStub(DocuSealService::class);
2026-04-07 23:50:19 +02:00
$this->mailer = $this->createStub(MailerService::class);
$this->urlGenerator = $this->createStub(UrlGeneratorInterface::class);
$this->projectDir = sys_get_temp_dir() . '/rgpd_test_' . uniqid();
mkdir($this->projectDir);
mkdir($this->projectDir . '/public');
touch($this->projectDir . '/public/logo_facture.png');
$this->service = new RgpdService(
$this->em,
$this->twig,
$this->docuSealService,
2026-04-07 23:50:19 +02:00
$this->mailer,
$this->urlGenerator,
$this->projectDir,
'secret'
);
}
protected function tearDown(): void
{
$this->removeDir($this->projectDir);
}
private function removeDir(string $dir): void
{
if (!is_dir($dir)) return;
$files = array_diff(scandir($dir), ['.', '..']);
foreach ($files as $file) {
$path = $dir . '/' . $file;
is_dir($path) ? $this->removeDir($path) : unlink($path);
}
rmdir($dir);
}
public function testHandleAccessRequestNoData(): void
{
$ip = '127.0.0.1';
$email = 'test@example.com';
$repository = $this->createStub(EntityRepository::class);
$repository->method('findBy')->willReturn([]);
$em = $this->createStub(EntityManagerInterface::class);
$em->method('getRepository')->willReturn($repository);
2026-04-07 23:50:19 +02:00
$service = new RgpdService($em, $this->twig, $this->docuSealService, $this->mailer, $this->urlGenerator, $this->projectDir, 's');
$result = $service->handleAccessRequest($ip, $email);
$this->assertFalse($result['found']);
$this->assertEquals(0, $result['count']);
}
public function testHandleAccessRequestWithData(): void
{
$ip = '127.0.0.1';
$email = 'test@example.com';
$visitor = new AnalyticsUniqId();
$visitorRepository = $this->createStub(EntityRepository::class);
$visitorRepository->method('findBy')->willReturn([$visitor]);
$eventRepository = $this->createStub(EntityRepository::class);
$eventRepository->method('findBy')->willReturn([]);
$em = $this->createStub(EntityManagerInterface::class);
$em->method('getRepository')->willReturnMap([
[AnalyticsUniqId::class, $visitorRepository],
[AnalyticsEvent::class, $eventRepository],
]);
2026-04-07 23:50:19 +02:00
$service = new RgpdService($em, $this->twig, $this->docuSealService, $this->mailer, $this->urlGenerator, $this->projectDir, 's');
$result = $service->handleAccessRequest($ip, $email);
$this->assertTrue($result['found']);
$this->assertEquals(1, $result['count']);
}
public function testHandleDeletionRequest(): void
{
$ip = '127.0.0.1';
$email = 'test@example.com';
$visitor = new AnalyticsUniqId();
$visitorRepository = $this->createStub(EntityRepository::class);
$visitorRepository->method('findBy')->willReturn([$visitor]);
$em = $this->createMock(EntityManagerInterface::class);
$em->method('getRepository')->willReturn($visitorRepository);
$em->expects($this->once())->method('remove')->with($visitor);
2026-04-07 23:50:19 +02:00
$service = new RgpdService($em, $this->twig, $this->docuSealService, $this->mailer, $this->urlGenerator, $this->projectDir, 's');
$result = $service->handleDeletionRequest($ip, $email);
$this->assertTrue($result['found']);
$this->assertEquals(1, $result['deleted']);
}
public function testHandleDeletionRequestNoData(): void
{
$ip = '127.0.0.1';
$email = 'test@example.com';
$repository = $this->createStub(EntityRepository::class);
$repository->method('findBy')->willReturn([]);
$em = $this->createStub(EntityManagerInterface::class);
$em->method('getRepository')->willReturn($repository);
2026-04-07 23:50:19 +02:00
$service = new RgpdService($em, $this->twig, $this->docuSealService, $this->mailer, $this->urlGenerator, $this->projectDir, 's');
$result = $service->handleDeletionRequest($ip, $email);
$this->assertFalse($result['found']);
$this->assertEquals(0, $result['deleted']);
}
public function testHandleAccessRequestWithoutLogo(): void
{
unlink($this->projectDir . '/public/logo_facture.png');
$ip = '127.0.0.1';
$email = 'test@example.com';
$repository = $this->createStub(EntityRepository::class);
$repository->method('findBy')->willReturn([]);
$em = $this->createStub(EntityManagerInterface::class);
$em->method('getRepository')->willReturn($repository);
2026-04-07 23:50:19 +02:00
$service = new RgpdService($em, $this->twig, $this->docuSealService, $this->mailer, $this->urlGenerator, $this->projectDir, 's');
$result = $service->handleAccessRequest($ip, $email);
$this->assertFalse($result['found']);
}
public function testHandleAccessRequestWithExistingDir(): void
{
mkdir($this->projectDir . '/var/rgpd', 0777, true);
$ip = '127.0.0.1';
$email = 'test@example.com';
$repository = $this->createStub(EntityRepository::class);
$repository->method('findBy')->willReturn([]);
test: couverture 83% methodes (1046 tests, 2135 assertions) Entites completes a 100% : - AdvertTest : 12 nouveaux (state, customer, totals, hmac, lines, payments) - CustomerTest : 3 nouveaux (isPendingDelete, revendeurCode, updatedAt) - DevisTest : 6 nouveaux (customer, submissionId, lines, state constants) - FactureTest : 10 nouveaux (state, totals, isPaid, lines, hmac, splitIndex) - OrderNumberTest : 1 nouveau (markAsUnused) - WebsiteTest : 1 nouveau (revendeurCode) Services completes/ameliores : - DocuSealServiceTest : 30 nouveaux (sendDevis, resendDevis, download, compta) - AdvertServiceTest : 6 nouveaux (isTvaEnabled, getTvaRate, computeTotals) - DevisServiceTest : 6 nouveaux (idem) - FactureServiceTest : 8 nouveaux (idem + createPaidFactureFromAdvert) - MailerServiceTest : 7 nouveaux (unsubscribe headers, VCF, formatFileSize) - MeilisearchServiceTest : 42 nouveaux (index/remove/search tous types) - RgpdServiceTest : 6 nouveaux (sendVerificationCode, verifyCode) - OrderNumberServiceTest : 2 nouveaux (preview/generate unused) - TarificationServiceTest : 1 nouveau (stripe error logger) - ComptaPdfTest : 4 nouveaux (totaux, colonnes numeriques, signature) - FacturePdfTest : 6 nouveaux (QR code, RIB, CGV Twig, footer skip) Controllers ameliores : - ComptabiliteControllerTest : 13 nouveaux (JSON, PDF, sign, callback) - StatsControllerTest : 2 nouveaux (rich data, 6-month evolution) - SyncControllerTest : 13 nouveaux (sync 6 types + purge) - ClientsControllerTest : 7 nouveaux (show, delete, resendWelcome) - FactureControllerTest : 2 nouveaux (generatePdf 404, send success) - LegalControllerTest : 6 nouveaux (rgpdVerify GET/POST) - TarificationControllerTest : 3 nouveaux (purge paths) - AdminControllersTest : 9 nouveaux (dashboard search, services) - WebhookStripeControllerTest : 3 nouveaux (invalid signatures) - KeycloakAuthenticatorTest : 4 nouveaux (groups, domain check) Commands : - PaymentReminderCommandTest : 1 nouveau (formalNotice step) - TestMailCommandTest : 2 nouveaux (force-dsn success/failure) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-08 00:13:00 +02:00
$em = $this->createStub(EntityManagerInterface::class);
$em->method('getRepository')->willReturn($repository);
test: couverture 83% methodes (1046 tests, 2135 assertions) Entites completes a 100% : - AdvertTest : 12 nouveaux (state, customer, totals, hmac, lines, payments) - CustomerTest : 3 nouveaux (isPendingDelete, revendeurCode, updatedAt) - DevisTest : 6 nouveaux (customer, submissionId, lines, state constants) - FactureTest : 10 nouveaux (state, totals, isPaid, lines, hmac, splitIndex) - OrderNumberTest : 1 nouveau (markAsUnused) - WebsiteTest : 1 nouveau (revendeurCode) Services completes/ameliores : - DocuSealServiceTest : 30 nouveaux (sendDevis, resendDevis, download, compta) - AdvertServiceTest : 6 nouveaux (isTvaEnabled, getTvaRate, computeTotals) - DevisServiceTest : 6 nouveaux (idem) - FactureServiceTest : 8 nouveaux (idem + createPaidFactureFromAdvert) - MailerServiceTest : 7 nouveaux (unsubscribe headers, VCF, formatFileSize) - MeilisearchServiceTest : 42 nouveaux (index/remove/search tous types) - RgpdServiceTest : 6 nouveaux (sendVerificationCode, verifyCode) - OrderNumberServiceTest : 2 nouveaux (preview/generate unused) - TarificationServiceTest : 1 nouveau (stripe error logger) - ComptaPdfTest : 4 nouveaux (totaux, colonnes numeriques, signature) - FacturePdfTest : 6 nouveaux (QR code, RIB, CGV Twig, footer skip) Controllers ameliores : - ComptabiliteControllerTest : 13 nouveaux (JSON, PDF, sign, callback) - StatsControllerTest : 2 nouveaux (rich data, 6-month evolution) - SyncControllerTest : 13 nouveaux (sync 6 types + purge) - ClientsControllerTest : 7 nouveaux (show, delete, resendWelcome) - FactureControllerTest : 2 nouveaux (generatePdf 404, send success) - LegalControllerTest : 6 nouveaux (rgpdVerify GET/POST) - TarificationControllerTest : 3 nouveaux (purge paths) - AdminControllersTest : 9 nouveaux (dashboard search, services) - WebhookStripeControllerTest : 3 nouveaux (invalid signatures) - KeycloakAuthenticatorTest : 4 nouveaux (groups, domain check) Commands : - PaymentReminderCommandTest : 1 nouveau (formalNotice step) - TestMailCommandTest : 2 nouveaux (force-dsn success/failure) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-08 00:13:00 +02:00
2026-04-07 23:50:19 +02:00
$service = new RgpdService($em, $this->twig, $this->docuSealService, $this->mailer, $this->urlGenerator, $this->projectDir, 's');
$result = $service->handleAccessRequest($ip, $email);
$this->assertFalse($result['found']);
}
test: couverture 83% methodes (1046 tests, 2135 assertions) Entites completes a 100% : - AdvertTest : 12 nouveaux (state, customer, totals, hmac, lines, payments) - CustomerTest : 3 nouveaux (isPendingDelete, revendeurCode, updatedAt) - DevisTest : 6 nouveaux (customer, submissionId, lines, state constants) - FactureTest : 10 nouveaux (state, totals, isPaid, lines, hmac, splitIndex) - OrderNumberTest : 1 nouveau (markAsUnused) - WebsiteTest : 1 nouveau (revendeurCode) Services completes/ameliores : - DocuSealServiceTest : 30 nouveaux (sendDevis, resendDevis, download, compta) - AdvertServiceTest : 6 nouveaux (isTvaEnabled, getTvaRate, computeTotals) - DevisServiceTest : 6 nouveaux (idem) - FactureServiceTest : 8 nouveaux (idem + createPaidFactureFromAdvert) - MailerServiceTest : 7 nouveaux (unsubscribe headers, VCF, formatFileSize) - MeilisearchServiceTest : 42 nouveaux (index/remove/search tous types) - RgpdServiceTest : 6 nouveaux (sendVerificationCode, verifyCode) - OrderNumberServiceTest : 2 nouveaux (preview/generate unused) - TarificationServiceTest : 1 nouveau (stripe error logger) - ComptaPdfTest : 4 nouveaux (totaux, colonnes numeriques, signature) - FacturePdfTest : 6 nouveaux (QR code, RIB, CGV Twig, footer skip) Controllers ameliores : - ComptabiliteControllerTest : 13 nouveaux (JSON, PDF, sign, callback) - StatsControllerTest : 2 nouveaux (rich data, 6-month evolution) - SyncControllerTest : 13 nouveaux (sync 6 types + purge) - ClientsControllerTest : 7 nouveaux (show, delete, resendWelcome) - FactureControllerTest : 2 nouveaux (generatePdf 404, send success) - LegalControllerTest : 6 nouveaux (rgpdVerify GET/POST) - TarificationControllerTest : 3 nouveaux (purge paths) - AdminControllersTest : 9 nouveaux (dashboard search, services) - WebhookStripeControllerTest : 3 nouveaux (invalid signatures) - KeycloakAuthenticatorTest : 4 nouveaux (groups, domain check) Commands : - PaymentReminderCommandTest : 1 nouveau (formalNotice step) - TestMailCommandTest : 2 nouveaux (force-dsn success/failure) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-08 00:13:00 +02:00
// --- sendVerificationCode ---
public function testSendVerificationCodeCallsMailer(): void
{
$this->twig->method('render')->willReturn('<html>Code: 123456</html>');
$mailer = $this->createMock(MailerService::class);
$mailer->expects($this->once())->method('sendEmail');
$service = new RgpdService($this->em, $this->twig, $this->docuSealService, $mailer, $this->urlGenerator, $this->projectDir, 'secret');
$service->sendVerificationCode('test@example.com', '127.0.0.1', 'access');
// Code file should be created
$codesDir = $this->projectDir . '/var/rgpd/codes';
$this->assertDirectoryExists($codesDir);
}
public function testSendVerificationCodeForDeletion(): void
{
$this->twig->method('render')->willReturn('<html>Code: 654321</html>');
$mailer = $this->createMock(MailerService::class);
$mailer->expects($this->once())->method('sendEmail');
$service = new RgpdService($this->em, $this->twig, $this->docuSealService, $mailer, $this->urlGenerator, $this->projectDir, 'secret');
$service->sendVerificationCode('test@example.com', '127.0.0.1', 'deletion');
$this->addToAssertionCount(1);
}
// --- verifyCode ---
public function testVerifyCodeReturnsFalseIfNoFile(): void
{
$service = new RgpdService($this->em, $this->twig, $this->docuSealService, $this->mailer, $this->urlGenerator, $this->projectDir, 'secret');
$result = $service->verifyCode('test@example.com', '127.0.0.1', 'access', '123456');
$this->assertFalse($result);
}
public function testVerifyCodeReturnsTrueForCorrectCode(): void
{
$this->twig->method('render')->willReturn('<html>ok</html>');
$service = new RgpdService($this->em, $this->twig, $this->docuSealService, $this->mailer, $this->urlGenerator, $this->projectDir, 'secret');
// Send a code to create the file; we'll intercept the actual code from the file
$service->sendVerificationCode('verify@example.com', '10.0.0.1', 'access');
// Read the created code file to get the actual code
$codesDir = $this->projectDir . '/var/rgpd/codes';
$files = glob($codesDir . '/*.json');
$this->assertNotEmpty($files);
$data = json_decode(file_get_contents($files[0]), true);
$code = $data['code'];
$result = $service->verifyCode('verify@example.com', '10.0.0.1', 'access', $code);
$this->assertTrue($result);
}
public function testVerifyCodeReturnsFalseForWrongCode(): void
{
$this->twig->method('render')->willReturn('<html>ok</html>');
$service = new RgpdService($this->em, $this->twig, $this->docuSealService, $this->mailer, $this->urlGenerator, $this->projectDir, 'secret');
$service->sendVerificationCode('wrong@example.com', '10.0.0.2', 'access');
$result = $service->verifyCode('wrong@example.com', '10.0.0.2', 'access', '000000');
$this->assertFalse($result);
}
public function testVerifyCodeReturnsFalseIfExpired(): void
{
$codesDir = $this->projectDir . '/var/rgpd/codes';
if (!is_dir($codesDir)) {
mkdir($codesDir, 0755, true);
}
// Write an already-expired code file
$codeHash = hash('sha256', 'expired@example.com|127.0.0.1|access|secret');
$filePath = $codesDir . '/' . $codeHash . '.json';
file_put_contents($filePath, json_encode([
'code' => '999999',
'hash' => 'ignored',
'expires' => time() - 1, // expired 1 second ago
]));
$service = new RgpdService($this->em, $this->twig, $this->docuSealService, $this->mailer, $this->urlGenerator, $this->projectDir, 'secret');
$result = $service->verifyCode('expired@example.com', '127.0.0.1', 'access', '999999');
$this->assertFalse($result);
}
public function testVerifyCodeReturnsFalseForInvalidJson(): void
{
$codesDir = $this->projectDir . '/var/rgpd/codes';
if (!is_dir($codesDir)) {
mkdir($codesDir, 0755, true);
}
$codeHash = hash('sha256', 'bad@example.com|127.0.0.1|access|secret');
$filePath = $codesDir . '/' . $codeHash . '.json';
file_put_contents($filePath, 'not valid json');
$service = new RgpdService($this->em, $this->twig, $this->docuSealService, $this->mailer, $this->urlGenerator, $this->projectDir, 'secret');
$result = $service->verifyCode('bad@example.com', '127.0.0.1', 'access', '000000');
$this->assertFalse($result);
}
}