120058049c
Auto Tag Develop / tag (push) Successful in 7s
Dernier wagon de la stack back M1. ERP-60 = polish stack + couverture de tests PHPUnit NON dépendante des rôles métier (cf. spec § 7 / § 8.1). ## Phase 0 — polish stack (déjà mergé dans les branches basses via rebase) - ERP-59 : route sidebar `/clients` (au lieu de `/commercial/clients`), cohérente avec `/suppliers`. - One-liner pagination Client abandonné : `pagination_client_enabled: true` est déjà le défaut global → `?pagination=false` marche déjà sur `/api/clients` (décision P7). ## Phase 1 — tests (combler les trous, zéro duplication) 8 nouvelles suites couvrant les RG non encore testées par ERP-55/56/57/58 : - `ClientFormulaireMainTest` — RG-1.02 (téléphone secondaire, max 2). - `ClientAddressTest` — RG-1.06/07/08 + RG-1.11 (CHECK BDD prospect/billing). - `ClientUniquenessTest` — RG-1.15/1.17 (Q4 : SIREN/email NON uniques). - `ClientArchiveTest` — **RG-1.23 : 409 restauration en conflit (gap P1)**. - `ClientAuditTest` — RG-1.27 (created* figés / updatedBy modificateur) + iban/bic présents dans le diff audité. - `ClientMigrationTest` — index partiel unique `uq_client_company_name_active` (1 seul) ; pas d'index siren/email. - `ClientSecurityTest` — 401 anonyme + 403 sans `commercial.clients.view`. - `ClientPatchStrictTest` — RG-1.28 (403 strict mix de groupes, fonctionnel). Cahier de test complet (mapping de TOUTES les RG → test) : `docs/specs/M1-clients/cahier-test-back-M1.md`. ## Délégué à ERP-74 (#493) Matrice RBAC différenciée (bureau/compta/commerciale/usine) + RG-1.04 fonctionnel — exigent les rôles métier seedés après le merge de la stack. ## Gaps documentés (cahier) - RG-1.29 validation écriture (catégorie type sur adresse → 422) non implémentée back (hors § 8.1, ticket test-only). - Violations CHECK adresse → rejet (≥400) sans mapping fin 422 (amélioration possible). ## Vérifs `make db-reset && make php-cs-fixer-allow-risky && make test` → **421 tests OK, 1386 assertions, 0 risky**. Nouveaux tests : 17, 71 assertions. --------- Co-authored-by: Matthieu <contact@malio.fr> Reviewed-on: #38 Co-authored-by: THOLOT DECHENE Matthieu <matthieu@yuno.malio.fr> Co-committed-by: THOLOT DECHENE Matthieu <matthieu@yuno.malio.fr>
186 lines
6.3 KiB
PHP
186 lines
6.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Tests\Module\Commercial\Api;
|
|
|
|
use PhpOffice\PhpSpreadsheet\IOFactory;
|
|
|
|
/**
|
|
* Tests fonctionnels de l'export XLSX du repertoire clients (M1, § 4.6).
|
|
*
|
|
* Couvre : reponse 200 (Content-Type + Content-Disposition), exclusion des
|
|
* archives par defaut, respect des filtres ?search / ?categoryType, gating de
|
|
* la colonne SIREN selon commercial.clients.accounting.view, 403 sans
|
|
* commercial.clients.view, 401 anonyme.
|
|
*
|
|
* @internal
|
|
*/
|
|
final class ClientExportControllerTest extends AbstractCommercialApiTestCase
|
|
{
|
|
private const string XLSX_MIME = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
|
|
private const string EXPORT_URL = '/api/clients/export.xlsx';
|
|
|
|
public function testExportReturnsXlsxResponseWithAttachmentFilename(): void
|
|
{
|
|
$client = $this->createAdminClient();
|
|
$this->seedClient('Export Alpha');
|
|
|
|
$response = $client->request('GET', self::EXPORT_URL);
|
|
|
|
self::assertResponseIsSuccessful();
|
|
$headers = $response->getHeaders(false);
|
|
self::assertStringContainsString(self::XLSX_MIME, $headers['content-type'][0] ?? '');
|
|
|
|
$disposition = $headers['content-disposition'][0] ?? '';
|
|
self::assertStringContainsString('attachment; filename="repertoire-clients-', $disposition);
|
|
self::assertMatchesRegularExpression(
|
|
'/filename="repertoire-clients-\d{8}\.xlsx"/',
|
|
$disposition,
|
|
);
|
|
|
|
// Le binaire est un XLSX relisible dont la 1re ligne porte les en-tetes.
|
|
$grid = $this->gridFromResponse($response->getContent());
|
|
$headers = $grid[0];
|
|
self::assertSame('Nom entreprise', $headers[0]);
|
|
self::assertContains('Catégories', $headers);
|
|
self::assertContains('Sites', $headers);
|
|
self::assertContains('Date de création', $headers);
|
|
}
|
|
|
|
public function testExportExcludesArchivedByDefault(): void
|
|
{
|
|
$client = $this->createAdminClient();
|
|
$this->seedClient('Active One');
|
|
$this->seedClient('Archived One', true);
|
|
|
|
$names = $this->companyNames($client->request('GET', self::EXPORT_URL)->getContent());
|
|
|
|
self::assertContains('ACTIVE ONE', $names);
|
|
self::assertNotContains('ARCHIVED ONE', $names);
|
|
}
|
|
|
|
public function testExportRespectsSearchFilter(): void
|
|
{
|
|
$client = $this->createAdminClient();
|
|
$this->seedClient('Searchable Alpha');
|
|
$this->seedClient('Other Beta');
|
|
|
|
$names = $this->companyNames(
|
|
$client->request('GET', self::EXPORT_URL.'?search=alpha')->getContent(),
|
|
);
|
|
|
|
self::assertContains('SEARCHABLE ALPHA', $names);
|
|
self::assertNotContains('OTHER BETA', $names);
|
|
}
|
|
|
|
public function testExportRespectsCategoryTypeFilter(): void
|
|
{
|
|
$client = $this->createAdminClient();
|
|
$this->seedClient('Distrib Co', false, 'DISTRIBUTEUR');
|
|
$this->seedClient('Secteur Co', false, 'SECTEUR');
|
|
|
|
$names = $this->companyNames(
|
|
$client->request('GET', self::EXPORT_URL.'?categoryType=DISTRIBUTEUR')->getContent(),
|
|
);
|
|
|
|
self::assertContains('DISTRIB CO', $names);
|
|
self::assertNotContains('SECTEUR CO', $names);
|
|
}
|
|
|
|
public function testSirenColumnPresentWithAccountingView(): void
|
|
{
|
|
// L'admin bypass le RBAC : il a donc accounting.view -> colonne SIREN.
|
|
$client = $this->createAdminClient();
|
|
$seed = $this->seedClient('Siren Co');
|
|
$em = $this->getEm();
|
|
$seed->setSiren('123456789');
|
|
$em->flush();
|
|
|
|
$grid = $this->gridFromResponse($client->request('GET', self::EXPORT_URL)->getContent());
|
|
|
|
self::assertContains('SIREN', $grid[0]);
|
|
self::assertStringContainsString('123456789', $this->flatten($grid));
|
|
}
|
|
|
|
public function testSirenColumnAbsentWithoutAccountingView(): void
|
|
{
|
|
// Seed via admin, puis relecture par un user qui n'a QUE clients.view.
|
|
$admin = $this->createAdminClient();
|
|
$seed = $this->seedClient('No Siren Co');
|
|
$em = $this->getEm();
|
|
$seed->setSiren('987654321');
|
|
$em->flush();
|
|
|
|
$creds = $this->createUserWithPermission('commercial.clients.view');
|
|
$viewer = $this->authenticatedClient($creds['username'], $creds['password']);
|
|
|
|
$grid = $this->gridFromResponse($viewer->request('GET', self::EXPORT_URL)->getContent());
|
|
|
|
self::assertNotContains('SIREN', $grid[0]);
|
|
self::assertStringNotContainsString('987654321', $this->flatten($grid));
|
|
}
|
|
|
|
public function testForbiddenWithoutClientsViewPermission(): void
|
|
{
|
|
$creds = $this->createUserWithPermission('core.users.view');
|
|
$client = $this->authenticatedClient($creds['username'], $creds['password']);
|
|
|
|
$client->request('GET', self::EXPORT_URL);
|
|
|
|
self::assertResponseStatusCodeSame(403);
|
|
}
|
|
|
|
public function testUnauthorizedWhenAnonymous(): void
|
|
{
|
|
$client = self::createClient();
|
|
$client->request('GET', self::EXPORT_URL);
|
|
|
|
self::assertResponseStatusCodeSame(401);
|
|
}
|
|
|
|
/**
|
|
* Relit le binaire XLSX d'une reponse et renvoie la grille de cellules.
|
|
*
|
|
* @return array<int, array<int, mixed>>
|
|
*/
|
|
private function gridFromResponse(string $binary): array
|
|
{
|
|
$tmp = tempnam(sys_get_temp_dir(), 'xlsx_export_test_');
|
|
self::assertIsString($tmp);
|
|
file_put_contents($tmp, $binary);
|
|
|
|
try {
|
|
return IOFactory::load($tmp)->getActiveSheet()->toArray();
|
|
} finally {
|
|
@unlink($tmp);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Extrait la colonne « Nom entreprise » (1re colonne) des lignes de donnees.
|
|
*
|
|
* @return list<string>
|
|
*/
|
|
private function companyNames(string $binary): array
|
|
{
|
|
$grid = $this->gridFromResponse($binary);
|
|
$rows = array_slice($grid, 1); // saute l'en-tete
|
|
|
|
return array_values(array_map(static fn (array $row): string => (string) ($row[0] ?? ''), $rows));
|
|
}
|
|
|
|
/**
|
|
* Aplatit toute la grille en une chaine, pour les assertions de presence.
|
|
*
|
|
* @param array<int, array<int, mixed>> $grid
|
|
*/
|
|
private function flatten(array $grid): string
|
|
{
|
|
return implode('|', array_map(
|
|
static fn (array $row): string => implode('|', array_map(static fn ($cell): string => (string) $cell, $row)),
|
|
$grid,
|
|
));
|
|
}
|
|
}
|