Files
e-ticket/tests/Service/MeilisearchServiceTest.php
Serreau Jovann 04927ec988 Complete TASK_CHECKUP: security, UX, tests, coverage, accessibility, config externalization
Billetterie:
- Partial refund support (STATUS_PARTIALLY_REFUNDED, refundedAmount field, migration)
- Race condition fix: PESSIMISTIC_WRITE lock on stock decrement in transaction
- Idempotency key on PaymentIntent::create, reuse existing PI if stripeSessionId set
- Disable checkout when event ended (server 400 + template hide)
- Webhook deduplication via cache (24h TTL on stripe event.id)
- Email validation (filter_var) in OrderController guest flow
- JSON cart validation (structure check before processing)
- Invitation expiration after 7 days (isExpired method + landing page message)
- Stripe Checkout fallback when JS fails to load (noscript + redirect)

Config externalization:
- Move Stripe fees (STRIPE_FEE_RATE, STRIPE_FEE_FIXED) and admin email (ADMIN_EMAIL) to .env/services.yaml
- Replace all hardcoded contact@e-cosplay.fr across 13 files
- MailerService: getAdminEmail()/getAdminFrom(), default $from=null resolves to admin

UX & Accessibility:
- ARIA tabs: role=tablist/tab/tabpanel, aria-selected, keyboard nav (arrows, Home, End)
- aria-label on cart +/- buttons and editor toolbar buttons
- tabindex=0 on editor toolbar buttons for keyboard access
- data-confirm handler in app.js (was only in admin.js)
- Cart error feedback on checkout failure
- Billet designer save feedback (loading/success/error states)
- Stock polling every 30s with rupture/low stock badges
- Back to event link on payment page

Security:
- HTML sanitizer: BLOCKED_TAGS list (script, style, iframe, svg, etc.) - content fully removed
- Stripe polling timeout (15s max) with fallback redirect
- Rate limiting on public order access (20/5min)
- .catch() on all fetch() calls (sortable, billet-designer)

Tests (92% PHP, 100% JS lines):
- PCOV added to dev Dockerfile
- Test DB setup: .env.test with DATABASE_URL, Redis auth, Meilisearch key
- Rate limiter disabled in test env
- Makefile: test_db_setup, test_db_reset, run_test_php, run_test_coverage_php/js
- New tests: InvitationFlowTest (21), AuditServiceTest (4), ExportServiceTest (9), InvoiceServiceTest (4)
- New tests: SuspendedUserSubscriberTest, RateLimiterSubscriberTest, MeilisearchServiceTest
- New tests: Stripe webhook payment_failed (6) + charge.refunded (6)
- New tests: BilletBuyer refund, User suspended, OrganizerInvitation expiration
- JS tests: stock polling (6), data-confirm (2), copy-url restore (1), editor ARIA (2), XSS (9), tabs keyboard (9)
- ESLint + PHP CS Fixer: 0 errors
- SonarQube exclusions aligned with vitest coverage config

Infra:
- Meilisearch consistency command (app:meilisearch:check-consistency --fix) + cron daily 3am
- MeilisearchService: getAllDocumentIds(), listIndexes()

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-23 11:14:06 +01:00

259 lines
9.3 KiB
PHP

<?php
namespace App\Tests\Service;
use App\Message\MeilisearchMessage;
use App\Service\MeilisearchService;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
class MeilisearchServiceTest extends TestCase
{
private HttpClientInterface $httpClient;
private MessageBusInterface $bus;
private MeilisearchService $service;
protected function setUp(): void
{
$this->httpClient = $this->createMock(HttpClientInterface::class);
$this->bus = $this->createMock(MessageBusInterface::class);
$this->service = new MeilisearchService(
$this->httpClient,
$this->bus,
'http://meilisearch:7700',
'test-key',
);
}
public function testIndexExistsReturnsTrue(): void
{
$response = $this->createMock(ResponseInterface::class);
$response->method('getStatusCode')->willReturn(200);
$this->httpClient->method('request')->willReturn($response);
self::assertTrue($this->service->indexExists('events'));
}
public function testIndexExistsReturnsFalseOnException(): void
{
$this->httpClient->method('request')->willThrowException(new \RuntimeException('fail'));
self::assertFalse($this->service->indexExists('events'));
}
public function testCreateIndexIfNotExistsCreatesWhenMissing(): void
{
$this->httpClient->method('request')->willThrowException(new \RuntimeException('not found'));
$this->bus->expects(self::once())
->method('dispatch')
->with(self::callback(fn (MeilisearchMessage $m) => 'createIndex' === $m->action))
->willReturn(new Envelope(new \stdClass()));
$this->service->createIndexIfNotExists('events');
}
public function testCreateIndexIfNotExistsSkipsWhenExists(): void
{
$response = $this->createMock(ResponseInterface::class);
$response->method('getStatusCode')->willReturn(200);
$this->httpClient->method('request')->willReturn($response);
$this->bus->expects(self::never())->method('dispatch');
$this->service->createIndexIfNotExists('events');
}
public function testCreateIndexDispatchesMessage(): void
{
$this->bus->expects(self::once())
->method('dispatch')
->with(self::callback(fn (MeilisearchMessage $m) => 'createIndex' === $m->action && 'events' === $m->index))
->willReturn(new Envelope(new \stdClass()));
$this->service->createIndex('events');
}
public function testDeleteIndexDispatchesMessage(): void
{
$this->bus->expects(self::once())
->method('dispatch')
->with(self::callback(fn (MeilisearchMessage $m) => 'deleteIndex' === $m->action))
->willReturn(new Envelope(new \stdClass()));
$this->service->deleteIndex('events');
}
public function testAddDocumentsDispatchesMessage(): void
{
$docs = [['id' => 1, 'title' => 'Test']];
$this->bus->expects(self::once())
->method('dispatch')
->with(self::callback(fn (MeilisearchMessage $m) => 'addDocuments' === $m->action && $m->payload['documents'] === $docs))
->willReturn(new Envelope(new \stdClass()));
$this->service->addDocuments('events', $docs);
}
public function testUpdateDocumentsDispatchesMessage(): void
{
$docs = [['id' => 1, 'title' => 'Updated']];
$this->bus->expects(self::once())
->method('dispatch')
->with(self::callback(fn (MeilisearchMessage $m) => 'updateDocuments' === $m->action && $m->payload['documents'] === $docs))
->willReturn(new Envelope(new \stdClass()));
$this->service->updateDocuments('events', $docs);
}
public function testDeleteDocumentDispatchesMessage(): void
{
$this->bus->expects(self::once())
->method('dispatch')
->with(self::callback(fn (MeilisearchMessage $m) => 'deleteDocument' === $m->action && 42 === $m->payload['documentId']))
->willReturn(new Envelope(new \stdClass()));
$this->service->deleteDocument('events', 42);
}
public function testDeleteDocumentsDispatchesMessage(): void
{
$ids = [1, 2, 3];
$this->bus->expects(self::once())
->method('dispatch')
->with(self::callback(fn (MeilisearchMessage $m) => 'deleteDocuments' === $m->action && $m->payload['ids'] === $ids))
->willReturn(new Envelope(new \stdClass()));
$this->service->deleteDocuments('events', $ids);
}
public function testUpdateSettingsDispatchesMessage(): void
{
$settings = ['filterableAttributes' => ['status']];
$this->bus->expects(self::once())
->method('dispatch')
->with(self::callback(fn (MeilisearchMessage $m) => 'updateSettings' === $m->action && $m->payload['settings'] === $settings))
->willReturn(new Envelope(new \stdClass()));
$this->service->updateSettings('events', $settings);
}
public function testSearchMakesPostRequest(): void
{
$response = $this->createMock(ResponseInterface::class);
$response->method('getStatusCode')->willReturn(200);
$response->method('toArray')->willReturn(['hits' => []]);
$this->httpClient->method('request')->willReturn($response);
$result = $this->service->search('events', 'test');
self::assertArrayHasKey('hits', $result);
}
public function testGetDocumentReturnsArray(): void
{
$response = $this->createMock(ResponseInterface::class);
$response->method('getStatusCode')->willReturn(200);
$response->method('toArray')->willReturn(['id' => 1, 'title' => 'Event']);
$this->httpClient->method('request')->willReturn($response);
$result = $this->service->getDocument('events', 1);
self::assertSame(1, $result['id']);
}
public function testRequestReturnsEmptyArrayOn204(): void
{
$response = $this->createMock(ResponseInterface::class);
$response->method('getStatusCode')->willReturn(204);
$this->httpClient->method('request')->willReturn($response);
$result = $this->service->request('DELETE', '/indexes/events');
self::assertSame([], $result);
}
public function testGetAllDocumentIdsSinglePage(): void
{
$response = $this->createMock(ResponseInterface::class);
$response->method('getStatusCode')->willReturn(200);
$response->method('toArray')->willReturn([
'results' => [
['id' => 1],
['id' => 2],
['id' => 3],
],
]);
$this->httpClient->method('request')->willReturn($response);
$ids = $this->service->getAllDocumentIds('events');
self::assertSame([1, 2, 3], $ids);
}
public function testGetAllDocumentIdsMultiplePages(): void
{
// First call returns exactly 1000 results (triggers next page)
$firstPageDocs = array_map(fn (int $i) => ['id' => $i], range(1, 1000));
$secondPageDocs = [['id' => 1001], ['id' => 1002]];
$response1 = $this->createMock(ResponseInterface::class);
$response1->method('getStatusCode')->willReturn(200);
$response1->method('toArray')->willReturn(['results' => $firstPageDocs]);
$response2 = $this->createMock(ResponseInterface::class);
$response2->method('getStatusCode')->willReturn(200);
$response2->method('toArray')->willReturn(['results' => $secondPageDocs]);
$this->httpClient->method('request')->willReturnOnConsecutiveCalls($response1, $response2);
$ids = $this->service->getAllDocumentIds('events');
self::assertCount(1002, $ids);
self::assertSame(1, $ids[0]);
self::assertSame(1002, $ids[1001]);
}
public function testGetAllDocumentIdsEmptyIndex(): void
{
$response = $this->createMock(ResponseInterface::class);
$response->method('getStatusCode')->willReturn(200);
$response->method('toArray')->willReturn(['results' => []]);
$this->httpClient->method('request')->willReturn($response);
$ids = $this->service->getAllDocumentIds('events');
self::assertSame([], $ids);
}
public function testListIndexes(): void
{
$response = $this->createMock(ResponseInterface::class);
$response->method('getStatusCode')->willReturn(200);
$response->method('toArray')->willReturn([
'results' => [
['uid' => 'events'],
['uid' => 'users'],
],
]);
$this->httpClient->method('request')->willReturn($response);
$indexes = $this->service->listIndexes();
self::assertSame(['events', 'users'], $indexes);
}
public function testListIndexesEmpty(): void
{
$response = $this->createMock(ResponseInterface::class);
$response->method('getStatusCode')->willReturn(200);
$response->method('toArray')->willReturn(['results' => []]);
$this->httpClient->method('request')->willReturn($response);
$indexes = $this->service->listIndexes();
self::assertSame([], $indexes);
}
}