1
0
Fork 0
get-installer-bootstrap/tests/Controller/Admin/ScriptMappingControllerTest.php
2026-05-05 10:00:56 +02:00

94 lines
3.1 KiB
PHP

<?php
namespace App\Tests\Controller\Admin;
use App\Entity\ScriptMapping;
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Tools\SchemaTool;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
final class ScriptMappingControllerTest extends WebTestCase
{
private KernelBrowser $client;
private EntityManagerInterface $entityManager;
protected function setUp(): void
{
self::ensureKernelShutdown();
$this->client = self::createClient();
$this->entityManager = static::getContainer()->get(EntityManagerInterface::class);
$metadata = $this->entityManager->getMetadataFactory()->getAllMetadata();
$schemaTool = new SchemaTool($this->entityManager);
$schemaTool->dropSchema($metadata);
$schemaTool->createSchema($metadata);
}
protected function tearDown(): void
{
parent::tearDown();
$this->entityManager->close();
}
public function testNewMappingRequiresAuthentication(): void
{
$this->client->request('GET', '/admin/mappings/new');
self::assertResponseRedirects('/admin/login');
}
public function testAdminCanCreateMappingWithNormalizedPaths(): void
{
$this->loginAsAdmin();
$crawler = $this->client->request('GET', '/admin/mappings/new');
$form = $crawler->selectButton('Enregistrer')->form([
'script_mapping[publicPath]' => '/mcp/graylog/install.sh',
'script_mapping[repositoryUrl]' => 'https://forge.lclr.dev/AI/graylog-mcp.git',
'script_mapping[gitRef]' => 'main',
'script_mapping[repositoryFilePath]' => '/install.sh',
'script_mapping[active]' => '1',
]);
$this->client->submit($form);
self::assertResponseRedirects('/admin');
$mapping = $this->entityManager->getRepository(ScriptMapping::class)->findOneBy([
'publicPath' => 'mcp/graylog/install.sh',
]);
self::assertInstanceOf(ScriptMapping::class, $mapping);
self::assertSame('install.sh', $mapping->getRepositoryFilePath());
}
public function testInvalidPublicPathRendersValidationError(): void
{
$this->loginAsAdmin();
$crawler = $this->client->request('GET', '/admin/mappings/new');
$form = $crawler->selectButton('Enregistrer')->form([
'script_mapping[publicPath]' => 'mcp/graylog/install.txt',
'script_mapping[repositoryUrl]' => 'https://forge.lclr.dev/AI/graylog-mcp.git',
'script_mapping[gitRef]' => 'main',
'script_mapping[repositoryFilePath]' => 'install.sh',
'script_mapping[active]' => '1',
]);
$this->client->submit($form);
self::assertResponseStatusCodeSame(422);
self::assertSelectorTextContains('form', 'Public path must end with .sh.');
}
private function loginAsAdmin(): void
{
$user = (new User())->setUsername('admin')->setPasswordHash('unused');
$this->entityManager->persist($user);
$this->entityManager->flush();
$this->client->loginUser($user);
}
}