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]>
86 lines
3.2 KiB
PHP
86 lines
3.2 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Unsupervised\Schedular\Tests\Unit\Registration;
|
|
|
|
use Unsupervised\Schedular\Registration\Question;
|
|
use Unsupervised\Schedular\Registration\QuestionField;
|
|
use Unsupervised\Schedular\Tests\Unit\TestCase;
|
|
|
|
class QuestionFieldTest extends TestCase
|
|
{
|
|
public function testRendersATextInputNamedAndLabelledAsAsked(): void
|
|
{
|
|
$html = QuestionField::render(new Question(7, 'Your level?', id: 3), 'us_answers[3]', 'us-q-3');
|
|
|
|
self::assertStringContainsString('<label for="us-q-3">Your level?</label>', $html);
|
|
self::assertStringContainsString('<input type="text" name="us_answers[3]" id="us-q-3">', $html);
|
|
}
|
|
|
|
public function testARequiredQuestionIsMarkedAndEnforced(): void
|
|
{
|
|
$question = new Question(7, 'Your level?', isRequired: true, id: 3);
|
|
|
|
$html = QuestionField::render($question, 'us_answers[3]', 'us-q-3');
|
|
|
|
self::assertStringContainsString('us-required', $html);
|
|
self::assertStringContainsString(' required', $html);
|
|
}
|
|
|
|
/**
|
|
* A block that may not apply at all keeps the marker and drops the attribute,
|
|
* so the browser cannot refuse a submit over a field that is out of play.
|
|
*/
|
|
public function testNotEnforcingRequiredKeepsTheMarkerButDropsTheAttribute(): void
|
|
{
|
|
$question = new Question(7, 'Your level?', isRequired: true, id: 3);
|
|
|
|
$html = QuestionField::render($question, 'us_answers[3]', 'us-q-3', enforceRequired: false);
|
|
|
|
self::assertStringContainsString('us-required', $html);
|
|
self::assertStringNotContainsString(' required>', $html);
|
|
}
|
|
|
|
/**
|
|
* Which of the question's two required flags applies depends on whose block
|
|
* this is, and only the caller knows that.
|
|
*/
|
|
public function testTheCallerCanOverrideWhichRequiredFlagApplies(): void
|
|
{
|
|
$question = new Question(
|
|
null,
|
|
'Previous experience',
|
|
scope: Question::SCOPE_ACCOUNT,
|
|
isRequired: false,
|
|
isRequiredChild: true,
|
|
id: 3
|
|
);
|
|
|
|
$forSelf = QuestionField::render($question, 'us_answers[3]', 'us-q-3', isRequired: $question->isRequiredForSelf());
|
|
$forChild = QuestionField::render($question, 'children[0][answers][3]', 'us-child-0-q-3', isRequired: $question->isRequiredForChild());
|
|
|
|
self::assertStringNotContainsString('us-required', $forSelf);
|
|
self::assertStringNotContainsString(' required', $forSelf);
|
|
|
|
self::assertStringContainsString('us-required', $forChild);
|
|
self::assertStringContainsString(' required', $forChild);
|
|
}
|
|
|
|
public function testASelectRendersItsOptionsBehindAnEmptyChoice(): void
|
|
{
|
|
$question = new Question(
|
|
7,
|
|
'Pick a level',
|
|
fieldType: Question::FIELD_SELECT,
|
|
options: ['Beginner', 'Advanced'],
|
|
id: 3
|
|
);
|
|
|
|
$html = QuestionField::render($question, 'us_answers[3]', 'us-q-3');
|
|
|
|
self::assertStringContainsString('<select name="us_answers[3]" id="us-q-3">', $html);
|
|
self::assertStringContainsString('<option value="Beginner">Beginner</option>', $html);
|
|
self::assertStringContainsString('<option value="Advanced">Advanced</option>', $html);
|
|
}
|
|
}
|