ai_agents_test-1.0.0-alpha1/src/Form/ImportTestGroupForm.php
src/Form/ImportTestGroupForm.php
<?php
declare(strict_types=1);
namespace Drupal\ai_agents_test\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\ai_agents_test\Service\TestSyncService;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Yaml\Yaml;
/**
* Form controller for importing test groups.
*/
final class ImportTestGroupForm extends FormBase {
/**
* Constructs a new ImportTestForm object.
*/
public function __construct(
protected TestSyncService $testSyncService,
) {
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('ai_agents_test.test_sync_service'),
);
}
/**
* {@inheritdoc}
*/
public function getFormId(): string {
return 'ai_agents_test_import';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state): array {
// File upload form with restrictions for YAML files.
$form['file'] = [
'#type' => 'file',
'#title' => $this->t('Upload a test group file'),
'#description' => $this->t('Upload a YAML file containing test data. Only .yml or .yaml files are allowed.'),
'#attributes' => [
'accept' => '.yml,.yaml',
],
];
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Import'),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state): void {
// Get the temporary file URI.
/** @var \Symfony\Component\HttpFoundation\FileBag $files */
$files = $this->getRequest()->files;
/** @var \Symfony\Component\HttpFoundation\File\UploadedFile $file */
$file = NULL;
foreach ($files as $file_objects) {
$file = $file_objects['file'];
}
// If no file.
if (empty($file)) {
$form_state->setErrorByName('file', $this->t('Please upload a file.'));
return;
}
// Get the yaml.
try {
$yaml = file_get_contents($file->getRealPath());
}
catch (\Exception $e) {
$form_state->setErrorByName('file', $this->t('The file could not be read. Please check the file and try again.'));
return;
}
try {
// Parse the YAML file.
$parsed_yaml = Yaml::parse($yaml);
}
catch (\Exception $e) {
$form_state->setErrorByName('file', $this->t('The file is not a valid YAML file. Please check the file and try again.'));
return;
}
// Check so all keys are present.
$required_keys = ['label', 'description', 'approval_percentage', 'tests', 'config_reset'];
foreach ($required_keys as $key) {
if (!array_key_exists($key, $parsed_yaml)) {
$form_state->setErrorByName('file', $this->t('The file is missing the required key: @key. Please check the file and try again.', ['@key' => $key]));
return;
}
}
// Also make sure no extra keys are present.
$allowed_keys = ['label', 'description', 'approval_percentage', 'tests', 'config_reset'];
foreach ($parsed_yaml as $key => $value) {
if (!in_array($key, $allowed_keys)) {
$form_state->setErrorByName('file', $this->t('The file contains an unknown key: @key. Please check the file and try again.', ['@key' => $key]));
return;
}
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state): void {
// Get the temporary file URI.
/** @var \Symfony\Component\HttpFoundation\FileBag $files */
$files = $this->getRequest()->files;
/** @var \Symfony\Component\HttpFoundation\File\UploadedFile $file */
$file = NULL;
foreach ($files as $file_objects) {
$file = $file_objects['file'];
}
// Import the test group.
try {
$test_group = $this->testSyncService->importTestGroup($file->getRealPath());
}
catch (\InvalidArgumentException $e) {
$form_state->setErrorByName('file', $this->t('Error importing test group: @message', ['@message' => $e->getMessage()]));
return;
}
// Set a message to inform the user.
$this->messenger()->addMessage($this->t('The test group %label has been imported successfully.', ['%label' => $test_group->label()]));
// Redirect to the test group view page.
$form_state->setRedirect('entity.ai_agents_test_group.canonical', ['ai_agents_test_group' => $test_group->id()]);
}
}
