feat: add the house manager questionnaire per travel and compact the questionnaire layout
This commit is contained in:
1 parent
d760ed3cdf
commit
bfef08b519
50 files changed
+2763
-180
No files matched your search
@@ -0,0 +1,270 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Form;
|
||||
|
||||
use App\Config\QuestionnaireCatalog;
|
||||
use App\Form\QuestionnaireType;
|
||||
use App\Model\Questionnaire\Definition;
|
||||
use App\Model\Questionnaire\Travel;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
use Symfony\Component\Form\FormFactoryInterface;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
|
||||
/**
|
||||
* The house manager's questionnaire against its shipped definition, with a travel of two teamers
|
||||
* from Saturday to Saturday: the three bespoke widgets (guidings and courses per teamer, the
|
||||
* programme with participants), their required fields, and what is stored.
|
||||
*
|
||||
* Every submit goes through Form::submit() with clearMissing, as a browser request does.
|
||||
*/
|
||||
class HouseManagerQuestionnaireTypeTest extends KernelTestCase
|
||||
{
|
||||
private const string ANNA = '11111111-1111-4111-8111-111111111111';
|
||||
private const string BEN = '22222222-2222-4222-8222-222222222222';
|
||||
|
||||
private const string KEY = QuestionnaireCatalog::HOUSE_MANAGER_FEEDBACK;
|
||||
|
||||
public function testEmptyDraftIsValid(): void
|
||||
{
|
||||
$form = $this->createForm();
|
||||
$form->submit([QuestionnaireType::DRAFT => '']);
|
||||
|
||||
$this->assertTrue($form->isValid(), (string) $form->getErrors(true));
|
||||
}
|
||||
|
||||
/**
|
||||
* Every yes/no is required, "Feedback zur Gruppe" is not.
|
||||
*/
|
||||
public function testEmptySubmitDemandsTheShownQuestionsButNotTheGroupFeedback(): void
|
||||
{
|
||||
$form = $this->createForm();
|
||||
$form->submit([QuestionnaireType::SUBMIT => '']);
|
||||
|
||||
$this->assertFalse($form->isValid());
|
||||
$this->assertSame(['groups_in_house', 'bus_arrival', 'no_shows', 'unused_services', 'guidings', 'courses', 'programme_deviations', 'programme', 'incidents'], $this->erroneous($form));
|
||||
}
|
||||
|
||||
public function testCompleteSubmitStoresGuidingsCoursesAndProgramme(): void
|
||||
{
|
||||
$form = $this->createForm();
|
||||
$form->submit($this->complete() + [QuestionnaireType::SUBMIT => '']);
|
||||
|
||||
$this->assertTrue($form->isValid(), (string) $form->getErrors(true));
|
||||
|
||||
$answers = $form->getData()[self::KEY];
|
||||
// flat lists, one row per teamer and type (per item), for JSON_TABLE in reports
|
||||
$this->assertSame([
|
||||
['disposition' => self::ANNA, 'type' => 'half_day', 'count' => 2, 'participants' => 15],
|
||||
['disposition' => self::ANNA, 'type' => 'full_day', 'count' => 1, 'participants' => 30],
|
||||
], $answers['guiding_teamers']);
|
||||
$this->assertSame([
|
||||
['disposition' => self::BEN, 'type' => 'ski_beginner', 'days' => 2, 'hours_per_day' => 2.5, 'dates' => ['2027-03-28', '2027-03-29']],
|
||||
], $answers['course_teamers']);
|
||||
$this->assertSame([
|
||||
'items' => [['item' => 'village_check', 'participants' => 25], ['item' => 'torch_hike', 'participants' => 18]],
|
||||
'other' => 'Schneeballschlacht',
|
||||
], $answers['programme']);
|
||||
|
||||
// left empty, and optional
|
||||
$stored = $this->definition()->withoutHiddenAnswers($answers);
|
||||
$this->assertArrayNotHasKey('group_feedback', $stored);
|
||||
$this->assertSame('Fr 16 Uhr', $stored['bus_arrival_time']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unticked fields are submitted (they are only hidden) but not stored.
|
||||
*/
|
||||
public function testWhatIsNotTickedIsNotStored(): void
|
||||
{
|
||||
$data = $this->complete();
|
||||
$data[self::KEY]['guidings_and_courses']['guiding_teamers'][self::BEN] = ['half_day' => ['count' => '3', 'participants' => '9']];
|
||||
$data[self::KEY]['activities']['programme']['slope_snack'] = ['participants' => '40'];
|
||||
$form = $this->createForm();
|
||||
$form->submit($data + [QuestionnaireType::SUBMIT => '']);
|
||||
|
||||
$this->assertTrue($form->isValid(), (string) $form->getErrors(true));
|
||||
$answers = $form->getData()[self::KEY];
|
||||
$this->assertSame([self::ANNA], array_unique(array_column($answers['guiding_teamers'], 'disposition')));
|
||||
$this->assertNotContains('slope_snack', array_column($answers['programme']['items'], 'item'));
|
||||
}
|
||||
|
||||
public function testATickedTeamerNeedsAGuidingTypeAndATickedTypeItsNumbers(): void
|
||||
{
|
||||
$data = $this->complete();
|
||||
$data[self::KEY]['guidings_and_courses']['guiding_teamers'] = [
|
||||
self::ANNA => ['picked' => '1'],
|
||||
self::BEN => ['picked' => '1', 'full_day' => ['picked' => '1', 'count' => '0']],
|
||||
];
|
||||
$form = $this->createForm();
|
||||
$form->submit($data + [QuestionnaireType::SUBMIT => '']);
|
||||
|
||||
$guidings = $form->get(self::KEY)->get('guidings_and_courses')->get('guiding_teamers');
|
||||
$this->assertSame(['Bitte wähle mindestens ein Guiding aus.'], $this->messages($guidings->get(self::ANNA)));
|
||||
$this->assertSame(['Bitte gib eine Zahl größer als 0 ein.'], $this->messages($guidings->get(self::BEN)->get('full_day')->get('count')));
|
||||
$this->assertSame(['Bitte beantworte diese Frage.'], $this->messages($guidings->get(self::BEN)->get('full_day')->get('participants')));
|
||||
}
|
||||
|
||||
public function testATickedCourseNeedsDaysHoursAndAtLeastOneDate(): void
|
||||
{
|
||||
$data = $this->complete();
|
||||
$data[self::KEY]['guidings_and_courses']['course_teamers'][self::BEN]['ski_beginner'] = ['picked' => '1'];
|
||||
$form = $this->createForm();
|
||||
$form->submit($data + [QuestionnaireType::SUBMIT => '']);
|
||||
|
||||
$course = $form->get(self::KEY)->get('guidings_and_courses')->get('course_teamers')->get(self::BEN)->get('ski_beginner');
|
||||
$this->assertNotEmpty($this->messages($course->get('days')));
|
||||
$this->assertNotEmpty($this->messages($course->get('hours_per_day')));
|
||||
$this->assertSame(['Bitte wähle mindestens einen Tag aus.'], $this->messages($course->get('dates')));
|
||||
}
|
||||
|
||||
/**
|
||||
* A draft takes a half-filled pick, and the pick survives being stored and loaded again.
|
||||
*/
|
||||
public function testAHalfFilledDraftRoundTrips(): void
|
||||
{
|
||||
$form = $this->createForm();
|
||||
$form->submit([
|
||||
self::KEY => ['guidings_and_courses' => ['guidings' => 'ja', 'guiding_teamers' => [self::ANNA => ['picked' => '1']]]],
|
||||
QuestionnaireType::DRAFT => '',
|
||||
]);
|
||||
|
||||
$this->assertTrue($form->isValid(), (string) $form->getErrors(true));
|
||||
$stored = $form->getData()[self::KEY];
|
||||
$this->assertSame([['disposition' => self::ANNA]], $stored['guiding_teamers']);
|
||||
|
||||
$view = $this->createForm([self::KEY => $stored])->createView();
|
||||
$teamers = $view[self::KEY]['guidings_and_courses']['guiding_teamers'];
|
||||
$this->assertTrue($teamers[self::ANNA]['picked']->vars['checked']);
|
||||
$this->assertFalse($teamers[self::BEN]['picked']->vars['checked']);
|
||||
}
|
||||
|
||||
/**
|
||||
* A travel runs Saturday to Saturday, so the course days are its dates, the Saturday twice.
|
||||
*/
|
||||
public function testCourseDaysAreTheTravelsDates(): void
|
||||
{
|
||||
$view = $this->createForm()->createView();
|
||||
$dates = $view[self::KEY]['guidings_and_courses']['course_teamers'][self::ANNA]['ski_beginner']['dates'];
|
||||
|
||||
$this->assertCount(8, $dates->children);
|
||||
$this->assertSame('Sa. 27.03.', $dates->children[0]->vars['label']);
|
||||
$this->assertSame('Sa. 03.04.', $dates->children[7]->vars['label']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Yes/no is answered with buttons beside the question, and a follow-up is indented under it.
|
||||
*/
|
||||
public function testShortChoicesAreInlineAndFollowUpsIndented(): void
|
||||
{
|
||||
$arrival = $this->createForm()->createView()[self::KEY]['arrival'];
|
||||
|
||||
$this->assertContains('questionnaire_inline_choice', $arrival['bus_arrival']->vars['block_prefixes']);
|
||||
$this->assertArrayNotHasKey('class', $arrival['bus_arrival']->vars['row_attr']);
|
||||
$this->assertStringContainsString('border-l-2', $arrival['bus_late']->vars['row_attr']['class']);
|
||||
}
|
||||
|
||||
public function testATeamerQuestionCannotBeBuiltWithoutTheTravel(): void
|
||||
{
|
||||
self::bootKernel();
|
||||
|
||||
$this->expectException(\LogicException::class);
|
||||
self::getContainer()->get(FormFactoryInterface::class)->create(QuestionnaireType::class, [], [
|
||||
'definitions' => [$this->definition()],
|
||||
'csrf_protection' => false,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, array<string, mixed>> a submission answering "Ja" wherever it opens more,
|
||||
* keyed by questionnaire, then section
|
||||
*/
|
||||
private function complete(): array
|
||||
{
|
||||
return [self::KEY => [
|
||||
'arrival' => [
|
||||
'groups_in_house' => 'ja',
|
||||
'bus_arrival' => 'ja',
|
||||
'bus_late' => 'ja',
|
||||
'bus_arrival_time' => 'Fr 16 Uhr',
|
||||
'no_shows' => 'nein',
|
||||
'unused_services' => 'ja',
|
||||
'unused_services_details' => 'VG 12345: Skipass nicht abgeholt',
|
||||
],
|
||||
'guidings_and_courses' => [
|
||||
'guidings' => 'ja',
|
||||
'guiding_teamers' => [
|
||||
self::ANNA => [
|
||||
'picked' => '1',
|
||||
'half_day' => ['picked' => '1', 'count' => '2', 'participants' => '15'],
|
||||
'full_day' => ['picked' => '1', 'count' => '1', 'participants' => '30'],
|
||||
],
|
||||
],
|
||||
'courses' => 'ja',
|
||||
'course_teamers' => [
|
||||
self::BEN => [
|
||||
'picked' => '1',
|
||||
'ski_beginner' => ['picked' => '1', 'days' => '2', 'hours_per_day' => '2,5', 'dates' => ['2027-03-28', '2027-03-29']],
|
||||
],
|
||||
],
|
||||
],
|
||||
'activities' => [
|
||||
'programme_deviations' => 'nein',
|
||||
'programme' => [
|
||||
'village_check' => ['picked' => '1', 'participants' => '25'],
|
||||
'torch_hike' => ['picked' => '1', 'participants' => '18'],
|
||||
'other' => ['picked' => '1', 'text' => 'Schneeballschlacht'],
|
||||
],
|
||||
],
|
||||
'closing' => [
|
||||
'incidents' => 'nein',
|
||||
],
|
||||
]];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<string> the questions with an error of their own, in form order
|
||||
*/
|
||||
private function erroneous(FormInterface $form): array
|
||||
{
|
||||
$keys = [];
|
||||
|
||||
foreach ($form->get(self::KEY) as $section) {
|
||||
foreach ($section as $key => $question) {
|
||||
if (0 < count($question->getErrors())) {
|
||||
$keys[] = $key;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $keys;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<string>
|
||||
*/
|
||||
private function messages(FormInterface $field): array
|
||||
{
|
||||
return array_map(static fn ($error): string => $error->getMessage(), iterator_to_array($field->getErrors()));
|
||||
}
|
||||
|
||||
private function definition(): Definition
|
||||
{
|
||||
return new QuestionnaireCatalog(__DIR__.'/../../config/questionnaires')->get(self::KEY);
|
||||
}
|
||||
|
||||
private function createForm(array $data = []): FormInterface
|
||||
{
|
||||
self::bootKernel();
|
||||
|
||||
return self::getContainer()->get(FormFactoryInterface::class)->create(QuestionnaireType::class, $data, [
|
||||
'definitions' => [$this->definition()],
|
||||
'travel' => new Travel(
|
||||
[self::ANNA => 'Anna Alpin (Reiseleitung)', self::BEN => 'Ben Brett (Kursleitung Ski)'],
|
||||
Travel::daysBetween(new \DateTimeImmutable('2027-03-27'), new \DateTimeImmutable('2027-04-03')),
|
||||
),
|
||||
'csrf_protection' => false,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user