feat: keep questionnaires until submitted, remind teamers daily and take bus feedback per leg
This commit is contained in:
1 parent
abf9c1be0b
commit
e72616fdbf
37 files changed
+1119
-238
No files matched your search
@@ -5,6 +5,7 @@ declare(strict_types=1);
|
||||
namespace App\Tests\Form;
|
||||
|
||||
use App\Config\QuestionnaireCatalog;
|
||||
use App\Enum\BusLeg;
|
||||
use App\Enum\QuestionType;
|
||||
use App\Form\QuestionnaireType;
|
||||
use App\Model\Questionnaire\Definition;
|
||||
@@ -14,6 +15,7 @@ use App\Model\Questionnaire\Section;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
use Symfony\Component\Form\FormFactoryInterface;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
use Symfony\Component\Form\FormView;
|
||||
|
||||
/**
|
||||
* Runs against the shipped definitions - teamer feedback alone, and with the bus feedback on the
|
||||
@@ -60,21 +62,109 @@ class QuestionnaireTypeTest extends KernelTestCase
|
||||
$this->assertSame([QuestionnaireCatalog::TEAMER_FEEDBACK, QuestionnaireCatalog::BUS_FEEDBACK], array_keys($data));
|
||||
// keyed by question within each questionnaire, not by section, and a rating is an int
|
||||
$this->assertSame(5, $data[QuestionnaireCatalog::TEAMER_FEEDBACK]['office_briefing_rating']);
|
||||
$this->assertSame('outbound', $data[QuestionnaireCatalog::BUS_FEEDBACK]['direction']);
|
||||
$this->assertSame(BusLeg::OUTBOUND->value, $data[QuestionnaireCatalog::BUS_FEEDBACK][QuestionnaireType::LEGS]);
|
||||
$this->assertSame('boonk', $data[QuestionnaireCatalog::BUS_FEEDBACK][BusLeg::OUTBOUND->value]['bus_company']);
|
||||
$this->assertArrayNotHasKey('organisation', $data[QuestionnaireCatalog::TEAMER_FEEDBACK]);
|
||||
|
||||
$sets = QuestionnaireType::answerSets($data, $definitions);
|
||||
$this->assertSame(
|
||||
[[QuestionnaireCatalog::TEAMER_FEEDBACK, null], [QuestionnaireCatalog::BUS_FEEDBACK, BusLeg::OUTBOUND]],
|
||||
array_map(static fn (array $set): array => [$set['definition']->key, $set['leg']], $sets),
|
||||
);
|
||||
$this->assertSame('boonk', $sets[1]['answers']['bus_company']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Every required question of either questionnaire is demanded, as far as it is shown.
|
||||
* An escort who rode both buses answers the bus feedback twice, and each ride is demanded in
|
||||
* full.
|
||||
*/
|
||||
public function testEmptySubmitDemandsTheVisibleRequiredQuestionsOfBoth(): void
|
||||
public function testBothLegsAreEachAnsweredAndDemanded(): void
|
||||
{
|
||||
$definitions = $this->both();
|
||||
$data = $this->complete($definitions, QuestionnaireType::BOTH_LEGS);
|
||||
unset($data[QuestionnaireCatalog::BUS_FEEDBACK][BusLeg::RETURN->value]['bus_trip']['bus_company']);
|
||||
|
||||
$form = $this->createForm($definitions);
|
||||
$form->submit($data + [QuestionnaireType::SUBMIT => '']);
|
||||
|
||||
$this->assertCount(1, $form->getErrors(true));
|
||||
$this->assertCount(1, $form->get(QuestionnaireCatalog::BUS_FEEDBACK)->get(BusLeg::RETURN->value)->get('bus_trip')->get('bus_company')->getErrors());
|
||||
|
||||
$form = $this->createForm($definitions);
|
||||
$form->submit($this->complete($definitions, QuestionnaireType::BOTH_LEGS) + [QuestionnaireType::SUBMIT => '']);
|
||||
|
||||
$this->assertTrue($form->isValid(), (string) $form->getErrors(true));
|
||||
$this->assertSame(
|
||||
[null, BusLeg::OUTBOUND, BusLeg::RETURN],
|
||||
array_column(QuestionnaireType::answerSets($form->getData(), $definitions), 'leg'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Switching from both rides to one leaves the other leg's fields filled in the browser; they
|
||||
* must not become a response.
|
||||
*/
|
||||
public function testALegNotChosenIsNeitherDemandedNorStored(): void
|
||||
{
|
||||
$definitions = $this->both();
|
||||
$data = $this->complete($definitions, QuestionnaireType::BOTH_LEGS);
|
||||
$data[QuestionnaireCatalog::BUS_FEEDBACK][QuestionnaireType::LEGS] = BusLeg::RETURN->value;
|
||||
$data[QuestionnaireCatalog::BUS_FEEDBACK][BusLeg::OUTBOUND->value] = [];
|
||||
|
||||
$form = $this->createForm($definitions);
|
||||
$form->submit($data + [QuestionnaireType::SUBMIT => '']);
|
||||
|
||||
$this->assertTrue($form->isValid(), (string) $form->getErrors(true));
|
||||
$this->assertSame([null, BusLeg::RETURN], array_column(QuestionnaireType::answerSets($form->getData(), $definitions), 'leg'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return iterable<string, array{array<string, array<string, mixed>>, string|null}>
|
||||
*/
|
||||
public static function storedLegs(): iterable
|
||||
{
|
||||
yield 'nothing saved yet' => [[], null];
|
||||
yield 'one leg' => [['return' => ['bus_company' => 'boonk']], 'return'];
|
||||
yield 'both legs' => [['outbound' => [], 'return' => []], QuestionnaireType::BOTH_LEGS];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, array<string, mixed>> $answersByLeg
|
||||
*/
|
||||
#[\PHPUnit\Framework\Attributes\DataProvider('storedLegs')]
|
||||
public function testTheChosenLegsAreReadBackFromTheStoredOnes(array $answersByLeg, ?string $legs): void
|
||||
{
|
||||
$this->assertSame([QuestionnaireType::LEGS => $legs] + $answersByLeg, QuestionnaireType::legData($answersByLeg));
|
||||
}
|
||||
|
||||
public function testALegIsShownOnlyWhileChosen(): void
|
||||
{
|
||||
$row = $this->createForm($this->both())->createView()->children[QuestionnaireCatalog::BUS_FEEDBACK]->children[BusLeg::RETURN->value];
|
||||
|
||||
$this->assertSame('bus_feedback.return', $row->vars['row_attr']['data-question-key']);
|
||||
$this->assertSame(
|
||||
['question' => 'bus_feedback.legs', 'operator' => 'in', 'value' => ['return', 'both']],
|
||||
json_decode($row->vars['row_attr']['data-show-if'], true),
|
||||
);
|
||||
$this->assertSame(
|
||||
'bus_feedback.return.bus_company',
|
||||
$row->children['bus_trip']->children['bus_company']->vars['row_attr']['data-question-key'],
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Every required question of the teamer feedback is demanded, as far as it is shown, and of the
|
||||
* bus feedback first which rides were escorted - no leg is shown before that.
|
||||
*/
|
||||
public function testEmptySubmitDemandsTheVisibleRequiredQuestionsAndTheLegs(): void
|
||||
{
|
||||
$form = $this->createForm($this->both());
|
||||
$form->submit([QuestionnaireType::SUBMIT => '']);
|
||||
|
||||
$this->assertFalse($form->isValid());
|
||||
$this->assertCount(1, $form->get(QuestionnaireCatalog::BUS_FEEDBACK)->get(QuestionnaireType::LEGS)->getErrors());
|
||||
|
||||
foreach ($this->both() as $definition) {
|
||||
foreach ([$this->teamerFeedback()] as $definition) {
|
||||
foreach ($definition->sections as $section) {
|
||||
foreach ($section->questions as $question) {
|
||||
$this->assertCount(
|
||||
@@ -120,12 +210,12 @@ class QuestionnaireTypeTest extends KernelTestCase
|
||||
{
|
||||
$definitions = $this->both();
|
||||
$data = $this->complete($definitions);
|
||||
$data[QuestionnaireCatalog::BUS_FEEDBACK]['bus_trip']['bus_company'] = 'other';
|
||||
$data[QuestionnaireCatalog::BUS_FEEDBACK][BusLeg::OUTBOUND->value]['bus_trip']['bus_company'] = 'other';
|
||||
|
||||
$form = $this->createForm($definitions);
|
||||
$form->submit($data + [QuestionnaireType::SUBMIT => '']);
|
||||
|
||||
$this->assertCount(1, $form->get(QuestionnaireCatalog::BUS_FEEDBACK)->get('bus_trip')->get('bus_company_other')->getErrors());
|
||||
$this->assertCount(1, $form->get(QuestionnaireCatalog::BUS_FEEDBACK)->get(BusLeg::OUTBOUND->value)->get('bus_trip')->get('bus_company_other')->getErrors());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -134,15 +224,15 @@ class QuestionnaireTypeTest extends KernelTestCase
|
||||
*/
|
||||
public function testNoFieldIsHtmlRequired(): void
|
||||
{
|
||||
$view = $this->createForm($this->both())->createView();
|
||||
|
||||
foreach ($view as $questionnaire) {
|
||||
foreach ($questionnaire as $section) {
|
||||
foreach ($section as $field) {
|
||||
$this->assertFalse($field->vars['required'], $field->vars['full_name']);
|
||||
}
|
||||
$assertNotRequired = function (FormView $view) use (&$assertNotRequired): void {
|
||||
foreach ($view as $child) {
|
||||
// buttons have no required at all
|
||||
$this->assertFalse($child->vars['required'] ?? false, $child->vars['full_name']);
|
||||
$assertNotRequired($child);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
$assertNotRequired($this->createForm($this->both())->createView());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -215,33 +305,56 @@ class QuestionnaireTypeTest extends KernelTestCase
|
||||
|
||||
/**
|
||||
* A submission answering every question that is shown when each choice takes its first option
|
||||
* and each rating is 5.
|
||||
* and each rating is 5; a per-bus-leg questionnaire for the given legs.
|
||||
*
|
||||
* @param list<Definition> $definitions
|
||||
*
|
||||
* @return array<string, array<string, array<string, mixed>>>
|
||||
* @return array<string, array<string, mixed>>
|
||||
*/
|
||||
private function complete(array $definitions): array
|
||||
private function complete(array $definitions, string $legs = 'outbound'): array
|
||||
{
|
||||
$data = [];
|
||||
|
||||
foreach ($definitions as $definition) {
|
||||
$answers = [];
|
||||
$sections = $this->completeSections($definition);
|
||||
|
||||
foreach ($definition->sections as $section) {
|
||||
foreach ($section->questions as $question) {
|
||||
$answer = match ($question->type) {
|
||||
QuestionType::CHOICE => $question->options[0]->key,
|
||||
QuestionType::MULTI => [$question->options[0]->key],
|
||||
QuestionType::RATING => 5,
|
||||
QuestionType::DATE => '01.01.2026',
|
||||
QuestionType::TEXT, QuestionType::TEXTAREA => 'Antwort',
|
||||
};
|
||||
$answers[$question->key] = $answer;
|
||||
if (!$definition->perBusLeg) {
|
||||
$data[$definition->key] = $sections;
|
||||
|
||||
if ($definition->isVisible($question, $answers)) {
|
||||
$data[$definition->key][$section->key][$question->key] = is_int($answer) ? (string) $answer : $answer;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
$data[$definition->key] = [QuestionnaireType::LEGS => $legs];
|
||||
|
||||
foreach (QuestionnaireType::BOTH_LEGS === $legs ? BusLeg::cases() : [BusLeg::from($legs)] as $leg) {
|
||||
$data[$definition->key][$leg->value] = $sections;
|
||||
}
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, array<string, mixed>> keyed by section, then question
|
||||
*/
|
||||
private function completeSections(Definition $definition): array
|
||||
{
|
||||
$data = [];
|
||||
$answers = [];
|
||||
|
||||
foreach ($definition->sections as $section) {
|
||||
foreach ($section->questions as $question) {
|
||||
$answer = match ($question->type) {
|
||||
QuestionType::CHOICE => $question->options[0]->key,
|
||||
QuestionType::MULTI => [$question->options[0]->key],
|
||||
QuestionType::RATING => 5,
|
||||
QuestionType::DATE => '01.01.2026',
|
||||
QuestionType::TEXT, QuestionType::TEXTAREA => 'Antwort',
|
||||
};
|
||||
$answers[$question->key] = $answer;
|
||||
|
||||
if ($definition->isVisible($question, $answers)) {
|
||||
$data[$section->key][$question->key] = is_int($answer) ? (string) $answer : $answer;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user