Add studio-defined account-registration questions
CI / Tests (PHP 8.2) (pull_request) Successful in 44s
CI / Tests (PHP 8.1) (pull_request) Successful in 45s
CI / No Debug Code (pull_request) Successful in 2s
CI / Coding Standards (pull_request) Successful in 2m46s
CI / PHPStan (pull_request) Successful in 2m58s
CI / Tests (PHP 8.3) (pull_request) Successful in 2m41s
CI / Build Plugin Zip (pull_request) Skipped
CI / Tests (PHP 8.2) (pull_request) Successful in 44s
CI / Tests (PHP 8.1) (pull_request) Successful in 45s
CI / No Debug Code (pull_request) Successful in 2s
CI / Coding Standards (pull_request) Successful in 2m46s
CI / PHPStan (pull_request) Successful in 2m58s
CI / Tests (PHP 8.3) (pull_request) Successful in 2m41s
CI / Build Plugin Zip (pull_request) Skipped
Studio admins can now define registration questions that every new student answers as a required second step during signup, with each student's answers shown under a "Registration Information" section in the admin. Extends the existing Registration domain: us_questions gains a scope column (offering | account) and a nullable offering_id, and account answers reuse us_question_answers with registration_type = 'account'. Authoring reuses the Offerings -> Questions page via an "Account signup" scope (studio-admin only). The registration form becomes two steps (progressive enhancement via assets/js/register.js; works without JS); required answers are validated before the account is created and apply to all signup paths (invite, group link, self-approval). StudentHistory::registrationInfo() powers the admin section. Bumps the plugin version to 1.1.0 so dbDelta runs the schema migration. Closes #90 Co-Authored-By: Claude Opus 4.8 <[email protected]>
This commit is contained in:
@@ -15,6 +15,10 @@ use Unsupervised\Schedular\Policy\Policy;
|
||||
use Unsupervised\Schedular\Policy\PolicyRepository;
|
||||
use Unsupervised\Schedular\Policy\PolicyVersion;
|
||||
use Unsupervised\Schedular\Policy\PolicyVersionRepository;
|
||||
use Unsupervised\Schedular\Registration\Answer;
|
||||
use Unsupervised\Schedular\Registration\AnswerRepository;
|
||||
use Unsupervised\Schedular\Registration\Question;
|
||||
use Unsupervised\Schedular\Registration\QuestionRepository;
|
||||
use Unsupervised\Schedular\Tests\Unit\TestCase;
|
||||
|
||||
class RegistrationPageTest extends TestCase
|
||||
@@ -28,18 +32,26 @@ class RegistrationPageTest extends TestCase
|
||||
|
||||
Functions\when('wp_unslash')->alias(static fn ($v) => $v);
|
||||
Functions\when('sanitize_text_field')->alias(static fn ($v) => $v);
|
||||
Functions\when('sanitize_textarea_field')->alias(static fn ($v) => $v);
|
||||
Functions\when('sanitize_email')->alias(static fn ($v) => $v);
|
||||
Functions\when('absint')->alias(static fn ($v) => (int) $v);
|
||||
Functions\when('current_time')->justReturn('2024-01-01 00:00:00');
|
||||
|
||||
$invites = Mockery::mock(InviteRepository::class);
|
||||
$policies = Mockery::mock(PolicyRepository::class);
|
||||
$invites = Mockery::mock(InviteRepository::class);
|
||||
$policies = Mockery::mock(PolicyRepository::class);
|
||||
$questions = Mockery::mock(QuestionRepository::class);
|
||||
$answers = Mockery::mock(AnswerRepository::class);
|
||||
$policies->shouldReceive('findForScope')->andReturn([])->byDefault();
|
||||
$questions->shouldReceive('findByScope')->andReturn([])->byDefault();
|
||||
$answers->shouldReceive('insert')->andReturn(1)->byDefault();
|
||||
|
||||
$this->ctx = [
|
||||
'invites' => $invites,
|
||||
'policies' => $policies,
|
||||
'mailer' => Mockery::mock(RegistrationMailer::class),
|
||||
'settings' => Mockery::mock(StudioSettings::class),
|
||||
'invites' => $invites,
|
||||
'policies' => $policies,
|
||||
'questions' => $questions,
|
||||
'answers' => $answers,
|
||||
'mailer' => Mockery::mock(RegistrationMailer::class),
|
||||
'settings' => Mockery::mock(StudioSettings::class),
|
||||
];
|
||||
|
||||
$this->ctx['page'] = new RegistrationPage(
|
||||
@@ -49,6 +61,8 @@ class RegistrationPageTest extends TestCase
|
||||
Mockery::mock(AcceptanceRepository::class),
|
||||
$this->ctx['settings'],
|
||||
$this->ctx['mailer'],
|
||||
$questions,
|
||||
$answers,
|
||||
);
|
||||
|
||||
$_POST = [];
|
||||
@@ -309,4 +323,58 @@ class RegistrationPageTest extends TestCase
|
||||
self::assertNotSame('confirm', $result);
|
||||
self::assertNotSame('', $result);
|
||||
}
|
||||
|
||||
public function testRejectsWhenARequiredAccountQuestionIsUnanswered(): void
|
||||
{
|
||||
$_POST = [ 'password' => 'password123', 'display_name' => 'Ada', 'email' => '[email protected]' ];
|
||||
|
||||
Functions\when('is_email')->justReturn(true);
|
||||
|
||||
$question = new Question(null, 'Emergency contact', isRequired: true, scope: Question::SCOPE_ACCOUNT, id: 5);
|
||||
$this->ctx['questions']->shouldReceive('findByScope')->with(Question::SCOPE_ACCOUNT, Mockery::any())->andReturn([$question]);
|
||||
|
||||
// A missing required answer must be caught before any account is created.
|
||||
Functions\expect('wp_insert_user')->never();
|
||||
$this->ctx['answers']->shouldReceive('insert')->never();
|
||||
|
||||
$result = $this->submit(null, true);
|
||||
|
||||
self::assertNotSame('confirm', $result);
|
||||
self::assertNotSame('', $result);
|
||||
}
|
||||
|
||||
public function testRecordsAccountAnswersOnSuccess(): void
|
||||
{
|
||||
$_POST = [
|
||||
'password' => 'password123',
|
||||
'display_name' => 'Ada',
|
||||
'us_answers' => [ '5' => 'By a friend' ],
|
||||
];
|
||||
|
||||
Functions\when('email_exists')->justReturn(false);
|
||||
Functions\when('wp_insert_user')->justReturn(42);
|
||||
Functions\when('is_wp_error')->justReturn(false);
|
||||
Functions\expect('wp_set_current_user')->once();
|
||||
Functions\expect('wp_set_auth_cookie')->once();
|
||||
$this->ctx['invites']->shouldReceive('markAccepted')->once();
|
||||
|
||||
$question = new Question(null, 'How did you hear about us?', isRequired: true, scope: Question::SCOPE_ACCOUNT, id: 5);
|
||||
$this->ctx['questions']->shouldReceive('findByScope')->with(Question::SCOPE_ACCOUNT, Mockery::any())->andReturn([$question]);
|
||||
|
||||
// The answer is written against the new user (account scope).
|
||||
$this->ctx['answers']->shouldReceive('insert')
|
||||
->once()
|
||||
->with(Mockery::on(static function (Answer $answer): bool {
|
||||
return $answer->registrationType === Answer::REG_ACCOUNT
|
||||
&& $answer->registrationId === 42
|
||||
&& $answer->studentId === 42
|
||||
&& $answer->questionId === 5
|
||||
&& $answer->answerValue === 'By a friend';
|
||||
}))
|
||||
->andReturn(1);
|
||||
|
||||
$invite = new Invite(email: '[email protected]', token: 'hash');
|
||||
|
||||
self::assertSame('invite', $this->submit($invite, false));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -125,6 +125,44 @@ class StudentHistoryTest extends TestCase
|
||||
self::assertSame('—', $rows[0]['answer']);
|
||||
}
|
||||
|
||||
public function testIntakeAnswersExcludeAccountScopeAnswers(): void
|
||||
{
|
||||
$this->answers->shouldReceive('findByStudent')->once()->with(5)->andReturn([
|
||||
new Answer(9, Answer::REG_ACCOUNT, 5, 5, 'By a friend', 2),
|
||||
new Answer(4, Answer::REG_LESSON, 12, 5, 'Beginner', 1),
|
||||
]);
|
||||
// Only the booking-scoped answer is resolved; the account answer is dropped.
|
||||
$this->questions->shouldReceive('findById')->with(4)
|
||||
->andReturn(new Question(1, 'Experience level', id: 4));
|
||||
|
||||
$rows = $this->history->intakeAnswers(5);
|
||||
|
||||
self::assertCount(1, $rows);
|
||||
self::assertSame('Experience level', $rows[0]['question']);
|
||||
self::assertSame('Lesson #12', $rows[0]['context']);
|
||||
}
|
||||
|
||||
public function testRegistrationInfoPairsAccountQuestionsWithAnswers(): void
|
||||
{
|
||||
$this->answers->shouldReceive('findByRegistration')->once()->with(Answer::REG_ACCOUNT, 5)->andReturn([
|
||||
new Answer(4, Answer::REG_ACCOUNT, 5, 5, 'Yes', 1),
|
||||
]);
|
||||
$this->questions->shouldReceive('findByScope')->once()->with(Question::SCOPE_ACCOUNT)->andReturn([
|
||||
new Question(null, 'Consent to email', isRequired: true, scope: Question::SCOPE_ACCOUNT, id: 4),
|
||||
new Question(null, 'Anything else?', isRequired: false, scope: Question::SCOPE_ACCOUNT, id: 7),
|
||||
]);
|
||||
|
||||
$rows = $this->history->registrationInfo(5);
|
||||
|
||||
self::assertSame(
|
||||
[
|
||||
[ 'question' => 'Consent to email', 'answer' => 'Yes', 'required' => true ],
|
||||
[ 'question' => 'Anything else?', 'answer' => '—', 'required' => false ],
|
||||
],
|
||||
$rows
|
||||
);
|
||||
}
|
||||
|
||||
public function testPaymentsBuildDisplayRows(): void
|
||||
{
|
||||
$this->payments->shouldReceive('findByStudent')->once()->with(5)->andReturn([
|
||||
|
||||
Reference in New Issue
Block a user