Ask some registration questions of students only
CI / Tests (PHP 8.2) (pull_request) Successful in 58s
CI / Tests (PHP 8.1) (pull_request) Successful in 58s
CI / No Debug Code (pull_request) Successful in 3s
CI / Coding Standards (pull_request) Successful in 2m53s
CI / PHPStan (pull_request) Successful in 3m0s
CI / Tests (PHP 8.3) (pull_request) Successful in 2m42s
CI / Build Plugin Zip (pull_request) Skipped

Every account-signup question was asked of everybody who registered, on the
same terms: "school and grade" had to be put to an adult signing themselves
up, and a question a studio needed answered for each student could only be
made required by demanding it of everyone.

A question now carries an audience — everyone, or only the students someone
registers on behalf of — and its own required flag for each side, so optional
for you and required for every student you enrol is expressible. Both settings
are account-scope only: an offering asks its questions once, about the student
being booked, so there is no second audience to differ from, and an offering
question mirrors its single "required" into both columns.

Every caller reads askedOfSelf()/isRequiredForSelf()/isRequiredForChild()
rather than the raw flags, so a students-only question can neither block the
account holder nor have an answer filed against them by a crafted post. The
family screen, which only ever adds a student, is held to the students' rule.

is_required_child arrives from dbDelta defaulting to 0, which would quietly
stop every existing required question being required of the students a
guardian registers — the case it most likely existed for. A one-time backfill
copies is_required across, guarded by its own option so a question later made
optional for students stays that way.

Closes #163

Co-Authored-By: Claude Opus 5 <[email protected]>
This commit is contained in:
2026-07-30 13:51:52 -03:00
co-authored by Claude Opus 5
parent 84378e856b
commit 434fe801ba
20 changed files with 809 additions and 85 deletions
+102 -1
View File
@@ -101,11 +101,112 @@ class QuestionTest extends TestCase
$question = new Question(7, 'Label', Question::FIELD_TEXT, id: 9);
$arr = $question->toArray();
foreach (['id', 'offering_id', 'scope', 'label', 'field_type', 'options', 'is_required', 'sort_order', 'is_active'] as $key) {
foreach (['id', 'offering_id', 'scope', 'label', 'field_type', 'options', 'audience', 'is_required', 'is_required_child', 'sort_order', 'is_active'] as $key) {
self::assertArrayHasKey($key, $arr);
}
}
public function testDefaultsToBeingAskedOfEveryoneAndRequiredOfNobody(): void
{
$question = new Question(null, 'Instrument', scope: Question::SCOPE_ACCOUNT);
self::assertSame(Question::AUDIENCE_ALL, $question->audience);
self::assertTrue($question->askedOfSelf());
self::assertFalse($question->isRequiredForSelf());
self::assertFalse($question->isRequiredForChild());
}
public function testAChildAudienceQuestionIsNeverAskedOfTheAccountHolder(): void
{
$question = new Question(
null,
'School and grade',
scope: Question::SCOPE_ACCOUNT,
audience: Question::AUDIENCE_CHILD,
isRequired: true,
isRequiredChild: true
);
self::assertFalse($question->askedOfSelf());
// Required-ness cannot outlive the audience: a question the account
// holder is never shown must never be one they are held to.
self::assertFalse($question->isRequiredForSelf());
self::assertTrue($question->isRequiredForChild());
}
public function testAQuestionCanBeOptionalForYouAndRequiredForYourStudents(): void
{
$question = new Question(
null,
'Previous experience',
scope: Question::SCOPE_ACCOUNT,
isRequired: false,
isRequiredChild: true
);
self::assertTrue($question->askedOfSelf());
self::assertFalse($question->isRequiredForSelf());
self::assertTrue($question->isRequiredForChild());
}
public function testFromRowReadsAudienceAndTheStudentsRequiredFlag(): void
{
$row = (object) [
'id' => '6',
'offering_id' => null,
'scope' => Question::SCOPE_ACCOUNT,
'label' => 'School and grade',
'field_type' => Question::FIELD_TEXT,
'options' => null,
'audience' => Question::AUDIENCE_CHILD,
'is_required' => '0',
'is_required_child' => '1',
'sort_order' => '0',
'is_active' => '1',
];
$question = Question::fromRow($row);
self::assertSame(Question::AUDIENCE_CHILD, $question->audience);
self::assertFalse($question->askedOfSelf());
self::assertTrue($question->isRequiredForChild());
}
/**
* A row read before dbDelta has added the columns — or one carrying a value
* no longer recognised — falls back to the behaviour every question had
* before the distinction existed: asked of everyone.
*/
public function testFromRowFallsBackToEveryoneWhenAudienceIsMissingOrUnknown(): void
{
$base = [
'id' => '7',
'offering_id' => null,
'scope' => Question::SCOPE_ACCOUNT,
'label' => 'Instrument',
'field_type' => Question::FIELD_TEXT,
'options' => null,
'is_required' => '1',
'sort_order' => '0',
'is_active' => '1',
];
$missing = Question::fromRow((object) $base);
$unknown = Question::fromRow((object) ($base + ['audience' => 'grown-ups']));
self::assertSame(Question::AUDIENCE_ALL, $missing->audience);
self::assertTrue($missing->isRequiredForSelf());
self::assertFalse($missing->isRequiredForChild());
self::assertSame(Question::AUDIENCE_ALL, $unknown->audience);
}
public function testValidAudienceConstants(): void
{
self::assertContains(Question::AUDIENCE_ALL, Question::VALID_AUDIENCES);
self::assertContains(Question::AUDIENCE_CHILD, Question::VALID_AUDIENCES);
}
public function testValidFieldTypeConstants(): void
{
self::assertContains(Question::FIELD_TEXT, Question::VALID_FIELD_TYPES);