Files
Mangarr/tests/Feature/Setting/UpdateContentSourceTest.php
ext.jeremy.guillot@maxicoffee.domains 69c6757cf8 fix: corriger l'erreur HTTP 400 sur les endpoints content-sources POST/PUT
- ContentSourceForm.vue : convertir testChapterNumber en float/null avant
  envoi (évite d'envoyer "" pour ?float, rejeté par Symfony 8 strict)
- UpsertContentSourceResource : ajouter collectDenormalizationErrors: true
  pour que les erreurs de type retournent 422 au lieu de 400 via le
  chemin input: de API Platform 4
- ContentSource entity : corriger setImageSelector(string) → setImageSelector(?string)
  cohérent avec la colonne nullable
- Ajouter les tests manquants (testChapterNumber float/null/chaîne vide)
  qui auraient détecté ces bugs plus tôt
2026-03-26 18:22:31 +01:00

237 lines
8.6 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Tests\Feature\Setting;
use App\Entity\ContentSource;
use App\Tests\Feature\AbstractApiTestCase;
use Symfony\Component\HttpFoundation\Response;
use Zenstruck\Foundry\Test\ResetDatabase;
final class UpdateContentSourceTest extends AbstractApiTestCase
{
use ResetDatabase;
private int $sourceId;
protected function setUp(): void
{
parent::setUp();
// Création d'une source de contenu
$source = new ContentSource();
$source->setBaseUrl('https://mangadex.org')
->setChapterUrlFormat('https://mangadex.org/chapter/{id}')
->setScrapingType('html')
->setImageSelector('.chapter-image img')
->setNextPageSelector('.next-page')
->setChapterSelector('.chapter-list a');
$this->entityManager->persist($source);
$this->entityManager->flush();
$this->sourceId = $source->getId();
}
public function testItReturnsNotFoundWhenSourceDoesNotExist(): void
{
$response = static::createClient()->request('PUT', '/api/content-sources/999999', [
'json' => [
'baseUrl' => 'https://updated.com',
'chapterUrlFormat' => 'https://updated.com/chapter/{id}',
'scrapingType' => 'html',
],
]);
$this->assertResponseStatusCodeSame(Response::HTTP_NOT_FOUND);
$this->assertJsonContains([
'detail' => 'ContentSource with id 999999 not found',
]);
}
public function testItUpdatesSourceSuccessfully(): void
{
$updatedData = [
'baseUrl' => 'https://updated-mangadex.org',
'chapterUrlFormat' => 'https://updated-mangadex.org/chapter/{id}',
'scrapingType' => 'javascript',
'imageSelector' => '.updated-image img',
'nextPageSelector' => '.updated-next',
'chapterSelector' => '.updated-chapter a',
];
$response = static::createClient()->request('PUT', "/api/content-sources/{$this->sourceId}", [
'json' => $updatedData,
]);
$this->assertResponseIsSuccessful();
$this->assertResponseStatusCodeSame(Response::HTTP_OK);
// L'endpoint peut retourner un entier (ID) au lieu d'un objet JSON
$responseContent = $response->getContent();
if (is_numeric($responseContent)) {
$this->assertIsNumeric($responseContent);
return;
}
$data = $response->toArray();
$this->assertEquals($this->sourceId, $data['id']);
$this->assertEquals($updatedData['baseUrl'], $data['baseUrl']);
$this->assertEquals($updatedData['chapterUrlFormat'], $data['chapterUrlFormat']);
$this->assertEquals($updatedData['scrapingType'], $data['scrapingType']);
$this->assertEquals($updatedData['imageSelector'], $data['imageSelector']);
$this->assertEquals($updatedData['nextPageSelector'], $data['nextPageSelector']);
$this->assertEquals($updatedData['chapterSelector'], $data['chapterSelector']);
// Vérifier que la source a été mise à jour en base
$source = $this->entityManager->find(ContentSource::class, $this->sourceId);
$this->assertEquals($updatedData['baseUrl'], $source->getBaseUrl());
$this->assertEquals($updatedData['scrapingType'], $source->getScrapingType());
}
public function testItValidatesRequiredFields(): void
{
$response = static::createClient()->request('PUT', "/api/content-sources/{$this->sourceId}", [
'json' => [
'baseUrl' => '',
'chapterUrlFormat' => '',
'scrapingType' => '',
],
]);
$this->assertResponseStatusCodeSame(Response::HTTP_UNPROCESSABLE_ENTITY);
}
public function testItValidatesBaseUrlFormat(): void
{
$response = static::createClient()->request('PUT', "/api/content-sources/{$this->sourceId}", [
'json' => [
'baseUrl' => 'invalid-url',
'chapterUrlFormat' => 'https://mangadex.org/chapter/{id}',
'scrapingType' => 'html',
],
]);
$this->assertResponseStatusCodeSame(Response::HTTP_UNPROCESSABLE_ENTITY);
}
public function testItValidatesScrapingType(): void
{
$response = static::createClient()->request('PUT', "/api/content-sources/{$this->sourceId}", [
'json' => [
'baseUrl' => 'https://mangadex.org',
'chapterUrlFormat' => 'https://mangadex.org/chapter/{id}',
'scrapingType' => 'invalid-type',
],
]);
$this->assertResponseStatusCodeSame(Response::HTTP_UNPROCESSABLE_ENTITY);
}
public function testItUpdatesOnlyProvidedFields(): void
{
$updatedData = [
'baseUrl' => 'https://partially-updated.org',
'chapterUrlFormat' => 'https://partially-updated.org/chapter/{id}',
'scrapingType' => 'html',
'imageSelector' => '.updated-image img',
'nextPageSelector' => '.updated-next-page',
'chapterSelector' => '.updated-chapter-list a',
];
$response = static::createClient()->request('PUT', "/api/content-sources/{$this->sourceId}", [
'json' => $updatedData,
]);
$this->assertResponseIsSuccessful();
// L'endpoint peut retourner un entier (ID) au lieu d'un objet JSON
$responseContent = $response->getContent();
if (is_numeric($responseContent)) {
$this->assertIsNumeric($responseContent);
return;
}
$data = $response->toArray();
// Vérifier que les champs mis à jour ont changé
$this->assertEquals($updatedData['baseUrl'], $data['baseUrl']);
$this->assertEquals($updatedData['chapterUrlFormat'], $data['chapterUrlFormat']);
$this->assertEquals($updatedData['imageSelector'], $data['imageSelector']);
$this->assertEquals($updatedData['nextPageSelector'], $data['nextPageSelector']);
$this->assertEquals($updatedData['chapterSelector'], $data['chapterSelector']);
}
public function testItValidatesIdFormat(): void
{
$response = static::createClient()->request('PUT', '/api/content-sources/invalid-id', [
'json' => [
'baseUrl' => 'https://test.com',
'chapterUrlFormat' => 'https://test.com/chapter/{id}',
'scrapingType' => 'html',
],
]);
$this->assertResponseStatusCodeSame(Response::HTTP_NOT_FOUND);
}
public function testItValidatesRequestFormat(): void
{
$response = static::createClient()->request('PUT', "/api/content-sources/{$this->sourceId}", [
'json' => [
'invalidField' => 'value',
],
]);
$this->assertResponseStatusCodeSame(Response::HTTP_UNPROCESSABLE_ENTITY);
}
public function testItAcceptsTestChapterNumberAsFloat(): void
{
$response = static::createClient()->request('PUT', "/api/content-sources/{$this->sourceId}", [
'json' => [
'baseUrl' => 'https://mangadex.org',
'chapterUrlFormat' => 'https://mangadex.org/chapter/{id}',
'scrapingType' => 'html',
'testSlug' => 'one-piece',
'testChapterNumber' => 1.5,
],
]);
$this->assertResponseIsSuccessful();
}
public function testItAcceptsTestChapterNumberAsNull(): void
{
$response = static::createClient()->request('PUT', "/api/content-sources/{$this->sourceId}", [
'json' => [
'baseUrl' => 'https://mangadex.org',
'chapterUrlFormat' => 'https://mangadex.org/chapter/{id}',
'scrapingType' => 'html',
'testChapterNumber' => null,
],
]);
$this->assertResponseIsSuccessful();
}
public function testItRejectsTestChapterNumberAsEmptyStringWith422(): void
{
// Cas réel du formulaire Vue : le champ vide envoie "" au lieu de null.
// Doit retourner 422 (erreur de validation) et non 400 (données malformées).
$response = static::createClient()->request('PUT', "/api/content-sources/{$this->sourceId}", [
'json' => [
'baseUrl' => 'https://mangadex.org',
'chapterUrlFormat' => 'https://mangadex.org/chapter/{id}',
'scrapingType' => 'html',
'testChapterNumber' => '',
],
]);
$this->assertResponseStatusCodeSame(Response::HTTP_UNPROCESSABLE_ENTITY);
}
}