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([
|
||||
|
||||
@@ -53,5 +53,15 @@ class AnswerTest extends TestCase
|
||||
{
|
||||
self::assertContains(Answer::REG_LESSON, Answer::VALID_REGISTRATION_TYPES);
|
||||
self::assertContains(Answer::REG_ENROLLMENT, Answer::VALID_REGISTRATION_TYPES);
|
||||
self::assertContains(Answer::REG_ACCOUNT, Answer::VALID_REGISTRATION_TYPES);
|
||||
}
|
||||
|
||||
public function testAccountAnswerTargetsTheUser(): void
|
||||
{
|
||||
$answer = new Answer(3, Answer::REG_ACCOUNT, 42, 42, 'By a friend');
|
||||
|
||||
self::assertSame(Answer::REG_ACCOUNT, $answer->registrationType);
|
||||
self::assertSame(42, $answer->registrationId);
|
||||
self::assertSame(42, $answer->studentId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -115,6 +115,7 @@ class QuestionRepositoryTest extends TestCase
|
||||
$row = (object) [
|
||||
'id' => '3',
|
||||
'offering_id' => '7',
|
||||
'scope' => Question::SCOPE_OFFERING,
|
||||
'label' => 'Q',
|
||||
'field_type' => Question::FIELD_TEXT,
|
||||
'options' => null,
|
||||
@@ -132,6 +133,68 @@ class QuestionRepositoryTest extends TestCase
|
||||
self::assertInstanceOf(Question::class, $questions[0]);
|
||||
}
|
||||
|
||||
public function testInsertAccountQuestionStoresScopeAndNullOffering(): void
|
||||
{
|
||||
Functions\expect('current_time')->andReturn('2026-04-01 12:00:00');
|
||||
|
||||
$this->db->shouldReceive('insert')
|
||||
->once()
|
||||
->with(
|
||||
'wp_us_questions',
|
||||
Mockery::on(static function (array $data): bool {
|
||||
return $data['offering_id'] === null
|
||||
&& $data['scope'] === Question::SCOPE_ACCOUNT
|
||||
&& $data['label'] === 'Emergency contact';
|
||||
}),
|
||||
Mockery::type('array')
|
||||
);
|
||||
|
||||
$this->db->insert_id = 30;
|
||||
|
||||
$question = new Question(null, 'Emergency contact', scope: Question::SCOPE_ACCOUNT);
|
||||
|
||||
self::assertSame(30, $this->repo->insert($question));
|
||||
}
|
||||
|
||||
public function testFindByScopeActiveOnlyPreparesQuery(): void
|
||||
{
|
||||
$this->db->shouldReceive('prepare')
|
||||
->once()
|
||||
->with(
|
||||
Mockery::pattern('/scope = %s AND is_active = %d/'),
|
||||
Mockery::on(static fn (array $p): bool => $p === ['wp_us_questions', Question::SCOPE_ACCOUNT, 1])
|
||||
)
|
||||
->andReturn('SELECT ...');
|
||||
|
||||
$this->db->shouldReceive('get_results')->andReturn([]);
|
||||
|
||||
self::assertSame([], $this->repo->findByScope(Question::SCOPE_ACCOUNT, activeOnly: true));
|
||||
}
|
||||
|
||||
public function testFindByScopeReturnsQuestions(): void
|
||||
{
|
||||
$row = (object) [
|
||||
'id' => '5',
|
||||
'offering_id' => null,
|
||||
'scope' => Question::SCOPE_ACCOUNT,
|
||||
'label' => 'How did you hear about us?',
|
||||
'field_type' => Question::FIELD_TEXT,
|
||||
'options' => null,
|
||||
'is_required' => '1',
|
||||
'sort_order' => '0',
|
||||
'is_active' => '1',
|
||||
];
|
||||
|
||||
$this->db->shouldReceive('prepare')->andReturn('SELECT ...');
|
||||
$this->db->shouldReceive('get_results')->andReturn([$row]);
|
||||
|
||||
$questions = $this->repo->findByScope(Question::SCOPE_ACCOUNT);
|
||||
|
||||
self::assertCount(1, $questions);
|
||||
self::assertNull($questions[0]->offeringId);
|
||||
self::assertSame(Question::SCOPE_ACCOUNT, $questions[0]->scope);
|
||||
}
|
||||
|
||||
public function testFindByIdReturnsNullWhenNotFound(): void
|
||||
{
|
||||
$this->db->shouldReceive('prepare')->andReturn('SELECT ...');
|
||||
|
||||
@@ -19,14 +19,24 @@ class QuestionTest extends TestCase
|
||||
self::assertFalse($question->isRequired);
|
||||
self::assertSame(0, $question->sortOrder);
|
||||
self::assertTrue($question->isActive);
|
||||
self::assertSame(Question::SCOPE_OFFERING, $question->scope);
|
||||
self::assertNull($question->id);
|
||||
}
|
||||
|
||||
public function testAccountScopeQuestionHasNoOffering(): void
|
||||
{
|
||||
$question = new Question(null, 'Emergency contact', scope: Question::SCOPE_ACCOUNT);
|
||||
|
||||
self::assertNull($question->offeringId);
|
||||
self::assertSame(Question::SCOPE_ACCOUNT, $question->scope);
|
||||
}
|
||||
|
||||
public function testFromRowDecodesOptionsJson(): void
|
||||
{
|
||||
$row = (object) [
|
||||
'id' => '3',
|
||||
'offering_id' => '7',
|
||||
'scope' => Question::SCOPE_OFFERING,
|
||||
'label' => 'Pick a level',
|
||||
'field_type' => Question::FIELD_SELECT,
|
||||
'options' => '["Beginner","Advanced"]',
|
||||
@@ -42,6 +52,7 @@ class QuestionTest extends TestCase
|
||||
self::assertSame(['Beginner', 'Advanced'], $question->options);
|
||||
self::assertTrue($question->isRequired);
|
||||
self::assertSame(2, $question->sortOrder);
|
||||
self::assertSame(Question::SCOPE_OFFERING, $question->scope);
|
||||
}
|
||||
|
||||
public function testFromRowHandlesNullOptions(): void
|
||||
@@ -49,6 +60,7 @@ class QuestionTest extends TestCase
|
||||
$row = (object) [
|
||||
'id' => '4',
|
||||
'offering_id' => '7',
|
||||
'scope' => Question::SCOPE_OFFERING,
|
||||
'label' => 'Notes',
|
||||
'field_type' => Question::FIELD_TEXTAREA,
|
||||
'options' => null,
|
||||
@@ -63,12 +75,33 @@ class QuestionTest extends TestCase
|
||||
self::assertFalse($question->isActive);
|
||||
}
|
||||
|
||||
public function testFromRowHandlesAccountScopeWithNullOffering(): void
|
||||
{
|
||||
$row = (object) [
|
||||
'id' => '5',
|
||||
'offering_id' => null,
|
||||
'scope' => Question::SCOPE_ACCOUNT,
|
||||
'label' => 'How did you hear about us?',
|
||||
'field_type' => Question::FIELD_TEXT,
|
||||
'options' => null,
|
||||
'is_required' => '1',
|
||||
'sort_order' => '0',
|
||||
'is_active' => '1',
|
||||
];
|
||||
|
||||
$question = Question::fromRow($row);
|
||||
|
||||
self::assertNull($question->offeringId);
|
||||
self::assertSame(Question::SCOPE_ACCOUNT, $question->scope);
|
||||
self::assertTrue($question->isRequired);
|
||||
}
|
||||
|
||||
public function testToArrayContainsExpectedKeys(): void
|
||||
{
|
||||
$question = new Question(7, 'Label', Question::FIELD_TEXT, id: 9);
|
||||
$arr = $question->toArray();
|
||||
|
||||
foreach (['id', 'offering_id', 'label', 'field_type', 'options', 'is_required', 'sort_order', 'is_active'] as $key) {
|
||||
foreach (['id', 'offering_id', 'scope', 'label', 'field_type', 'options', 'is_required', 'sort_order', 'is_active'] as $key) {
|
||||
self::assertArrayHasKey($key, $arr);
|
||||
}
|
||||
}
|
||||
@@ -80,4 +113,10 @@ class QuestionTest extends TestCase
|
||||
self::assertContains(Question::FIELD_SELECT, Question::VALID_FIELD_TYPES);
|
||||
self::assertContains(Question::FIELD_CHECKBOX, Question::VALID_FIELD_TYPES);
|
||||
}
|
||||
|
||||
public function testValidScopeConstants(): void
|
||||
{
|
||||
self::assertContains(Question::SCOPE_OFFERING, Question::VALID_SCOPES);
|
||||
self::assertContains(Question::SCOPE_ACCOUNT, Question::VALID_SCOPES);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user