Add Registration Questions domain (per-offering intake forms)
CI / Coding Standards (pull_request) Successful in 51s
CI / PHPStan (pull_request) Successful in 1m0s
CI / Tests (PHP 8.1) (pull_request) Successful in 46s
CI / Tests (PHP 8.2) (pull_request) Successful in 48s
CI / Tests (PHP 8.3) (pull_request) Successful in 47s
CI / No Debug Code (pull_request) Successful in 3s

Implements #5: studio admin / instructors author intake questions scoped
per offering; answers are stored against a lesson or group enrolment via a
polymorphic registration reference.

- src/Registration/: Question + Answer value objects, QuestionRepository
  and AnswerRepository, QuestionEndpoint (REST), QuestionController +
  templates/admin/questions.php (Offerings -> Questions submenu)
- us_questions and us_question_answers tables in Schema.php
- REST: public GET /offerings/{id}/questions; POST/PATCH/DELETE /questions
  gated by manage_questions + offering ownership (owner or studio admin)
- Field types text/textarea/select/checkbox; select options stored as JSON
- Wiring in Plugin, RestRegistrar, AdminMenu

AnswerRepository is built now and consumed by the booking/enrolment flow
in #3/#4.

Tests: tests/Unit/Registration/ (19 tests). composer test (63 total), cs,
and PHPStan level 6 all pass.

Refs #5

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-06-05 11:11:06 -03:00
parent 5b6cc4e89b
commit e61d99daed
15 changed files with 1141 additions and 4 deletions
@@ -0,0 +1,100 @@
<?php
declare(strict_types=1);
namespace Unsupervised\Schedular\Tests\Unit\Registration;
use Brain\Monkey\Functions;
use Mockery;
use Unsupervised\Schedular\Registration\Answer;
use Unsupervised\Schedular\Registration\AnswerRepository;
use Unsupervised\Schedular\Tests\Unit\TestCase;
class AnswerRepositoryTest extends TestCase
{
private \wpdb $db;
private AnswerRepository $repo;
protected function setUp(): void
{
parent::setUp();
$this->db = Mockery::mock(\wpdb::class);
$this->db->prefix = 'wp_';
$this->repo = new AnswerRepository($this->db);
}
public function testInsertCallsWpdbInsertAndReturnsId(): void
{
Functions\expect('current_time')->with('mysql')->andReturn('2026-04-01 12:00:00');
$this->db->shouldReceive('insert')
->once()
->with(
'wp_us_question_answers',
Mockery::on(static function (array $data): bool {
return $data['question_id'] === 3
&& $data['registration_type'] === Answer::REG_LESSON
&& $data['registration_id'] === 12
&& $data['student_id'] === 5
&& $data['answer_value'] === 'Beginner';
}),
['%d', '%s', '%d', '%d', '%s', '%s']
);
$this->db->insert_id = 77;
$answer = new Answer(3, Answer::REG_LESSON, 12, 5, 'Beginner');
self::assertSame(77, $this->repo->insert($answer));
}
public function testInsertManyReturnsAllIds(): void
{
Functions\when('current_time')->justReturn('2026-04-01 12:00:00');
$ids = [101, 102];
$this->db->shouldReceive('insert')
->twice()
->andReturnUsing(function () use (&$ids): void {
$this->db->insert_id = array_shift($ids);
});
$answers = [
new Answer(3, Answer::REG_LESSON, 12, 5, 'A'),
new Answer(4, Answer::REG_LESSON, 12, 5, 'B'),
];
$result = $this->repo->insertMany($answers);
self::assertSame([101, 102], $result);
}
public function testFindByRegistrationPreparesQueryAndMaps(): void
{
$row = (object) [
'id' => '1',
'question_id' => '3',
'registration_type' => Answer::REG_LESSON,
'registration_id' => '12',
'student_id' => '5',
'answer_value' => 'Beginner',
];
$this->db->shouldReceive('prepare')
->once()
->with(
Mockery::pattern('/registration_type = %s AND registration_id = %d/'),
Answer::REG_LESSON,
12
)
->andReturn('SELECT ...');
$this->db->shouldReceive('get_results')->andReturn([$row]);
$answers = $this->repo->findByRegistration(Answer::REG_LESSON, 12);
self::assertCount(1, $answers);
self::assertInstanceOf(Answer::class, $answers[0]);
self::assertSame(3, $answers[0]->questionId);
}
}
+57
View File
@@ -0,0 +1,57 @@
<?php
declare(strict_types=1);
namespace Unsupervised\Schedular\Tests\Unit\Registration;
use Unsupervised\Schedular\Registration\Answer;
use Unsupervised\Schedular\Tests\Unit\TestCase;
class AnswerTest extends TestCase
{
public function testConstructorAndProperties(): void
{
$answer = new Answer(3, Answer::REG_LESSON, 12, 5, 'Beginner', 99);
self::assertSame(3, $answer->questionId);
self::assertSame(Answer::REG_LESSON, $answer->registrationType);
self::assertSame(12, $answer->registrationId);
self::assertSame(5, $answer->studentId);
self::assertSame('Beginner', $answer->answerValue);
self::assertSame(99, $answer->id);
}
public function testFromRowMapsCorrectly(): void
{
$row = (object) [
'id' => '99',
'question_id' => '3',
'registration_type' => Answer::REG_ENROLLMENT,
'registration_id' => '12',
'student_id' => '5',
'answer_value' => '1',
];
$answer = Answer::fromRow($row);
self::assertSame(99, $answer->id);
self::assertSame(3, $answer->questionId);
self::assertSame(Answer::REG_ENROLLMENT, $answer->registrationType);
self::assertSame(12, $answer->registrationId);
}
public function testToArrayContainsExpectedKeys(): void
{
$answer = new Answer(3, Answer::REG_LESSON, 12, 5);
$arr = $answer->toArray();
foreach (['id', 'question_id', 'registration_type', 'registration_id', 'student_id', 'answer_value'] as $key) {
self::assertArrayHasKey($key, $arr);
}
}
public function testValidRegistrationTypeConstants(): void
{
self::assertContains(Answer::REG_LESSON, Answer::VALID_REGISTRATION_TYPES);
self::assertContains(Answer::REG_ENROLLMENT, Answer::VALID_REGISTRATION_TYPES);
}
}
@@ -0,0 +1,152 @@
<?php
declare(strict_types=1);
namespace Unsupervised\Schedular\Tests\Unit\Registration;
use Brain\Monkey\Functions;
use Mockery;
use Unsupervised\Schedular\Registration\Question;
use Unsupervised\Schedular\Registration\QuestionRepository;
use Unsupervised\Schedular\Tests\Unit\TestCase;
class QuestionRepositoryTest extends TestCase
{
private \wpdb $db;
private QuestionRepository $repo;
protected function setUp(): void
{
parent::setUp();
$this->db = Mockery::mock(\wpdb::class);
$this->db->prefix = 'wp_';
$this->repo = new QuestionRepository($this->db);
}
public function testInsertWithNullOptionsStoresNull(): void
{
Functions\expect('current_time')->with('mysql')->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'] === 7
&& $data['label'] === 'Your level?'
&& $data['field_type'] === Question::FIELD_TEXT
&& $data['options'] === null
&& $data['is_required'] === 0
&& $data['created_at'] === '2026-04-01 12:00:00';
}),
Mockery::type('array')
);
$this->db->insert_id = 21;
$question = new Question(7, 'Your level?');
self::assertSame(21, $this->repo->insert($question));
}
public function testInsertEncodesOptionsAsJson(): void
{
Functions\expect('current_time')->andReturn('2026-04-01 12:00:00');
Functions\expect('wp_json_encode')
->once()
->with(['Beginner', 'Advanced'])
->andReturn('["Beginner","Advanced"]');
$this->db->shouldReceive('insert')
->once()
->with(
'wp_us_questions',
Mockery::on(static fn (array $data): bool => $data['options'] === '["Beginner","Advanced"]'),
Mockery::type('array')
);
$this->db->insert_id = 22;
$question = new Question(
offeringId: 7,
label: 'Pick a level',
fieldType: Question::FIELD_SELECT,
options: ['Beginner', 'Advanced'],
);
self::assertSame(22, $this->repo->insert($question));
}
public function testUpdateReturnsTrueOnSuccess(): void
{
$this->db->shouldReceive('update')
->once()
->with(
'wp_us_questions',
Mockery::on(static fn (array $data): bool => $data['label'] === 'Renamed' && $data['is_required'] === 1),
['id' => 5],
Mockery::type('array'),
['%d']
)
->andReturn(1);
$question = new Question(7, 'Renamed', isRequired: true, id: 5);
self::assertTrue($this->repo->update(5, $question));
}
public function testFindByOfferingActiveOnlyPreparesQuery(): void
{
$this->db->shouldReceive('prepare')
->once()
->with(
Mockery::pattern('/offering_id = %d AND is_active = %d/'),
Mockery::on(static fn (array $p): bool => $p === [7, 1])
)
->andReturn('SELECT ...');
$this->db->shouldReceive('get_results')->andReturn([]);
self::assertSame([], $this->repo->findByOffering(7, activeOnly: true));
}
public function testFindByOfferingReturnsQuestions(): void
{
$row = (object) [
'id' => '3',
'offering_id' => '7',
'label' => 'Q',
'field_type' => Question::FIELD_TEXT,
'options' => null,
'is_required' => '0',
'sort_order' => '0',
'is_active' => '1',
];
$this->db->shouldReceive('prepare')->andReturn('SELECT ...');
$this->db->shouldReceive('get_results')->andReturn([$row]);
$questions = $this->repo->findByOffering(7);
self::assertCount(1, $questions);
self::assertInstanceOf(Question::class, $questions[0]);
}
public function testFindByIdReturnsNullWhenNotFound(): void
{
$this->db->shouldReceive('prepare')->andReturn('SELECT ...');
$this->db->shouldReceive('get_row')->andReturn(null);
self::assertNull($this->repo->findById(99));
}
public function testDeleteCallsWpdbDelete(): void
{
$this->db->shouldReceive('delete')
->once()
->with('wp_us_questions', ['id' => 4], ['%d'])
->andReturn(1);
self::assertTrue($this->repo->delete(4));
}
}
+83
View File
@@ -0,0 +1,83 @@
<?php
declare(strict_types=1);
namespace Unsupervised\Schedular\Tests\Unit\Registration;
use Unsupervised\Schedular\Registration\Question;
use Unsupervised\Schedular\Tests\Unit\TestCase;
class QuestionTest extends TestCase
{
public function testConstructorAndDefaults(): void
{
$question = new Question(7, 'Your level?');
self::assertSame(7, $question->offeringId);
self::assertSame('Your level?', $question->label);
self::assertSame(Question::FIELD_TEXT, $question->fieldType);
self::assertNull($question->options);
self::assertFalse($question->isRequired);
self::assertSame(0, $question->sortOrder);
self::assertTrue($question->isActive);
self::assertNull($question->id);
}
public function testFromRowDecodesOptionsJson(): void
{
$row = (object) [
'id' => '3',
'offering_id' => '7',
'label' => 'Pick a level',
'field_type' => Question::FIELD_SELECT,
'options' => '["Beginner","Advanced"]',
'is_required' => '1',
'sort_order' => '2',
'is_active' => '1',
];
$question = Question::fromRow($row);
self::assertSame(3, $question->id);
self::assertSame(Question::FIELD_SELECT, $question->fieldType);
self::assertSame(['Beginner', 'Advanced'], $question->options);
self::assertTrue($question->isRequired);
self::assertSame(2, $question->sortOrder);
}
public function testFromRowHandlesNullOptions(): void
{
$row = (object) [
'id' => '4',
'offering_id' => '7',
'label' => 'Notes',
'field_type' => Question::FIELD_TEXTAREA,
'options' => null,
'is_required' => '0',
'sort_order' => '0',
'is_active' => '0',
];
$question = Question::fromRow($row);
self::assertNull($question->options);
self::assertFalse($question->isActive);
}
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) {
self::assertArrayHasKey($key, $arr);
}
}
public function testValidFieldTypeConstants(): void
{
self::assertContains(Question::FIELD_TEXT, Question::VALID_FIELD_TYPES);
self::assertContains(Question::FIELD_TEXTAREA, Question::VALID_FIELD_TYPES);
self::assertContains(Question::FIELD_SELECT, Question::VALID_FIELD_TYPES);
self::assertContains(Question::FIELD_CHECKBOX, Question::VALID_FIELD_TYPES);
}
}