Add policy, intake, and payment history to the admin student detail view
CI / Coding Standards (pull_request) Successful in 2m47s
CI / PHPStan (pull_request) Successful in 2m56s
CI / Tests (PHP 8.3) (pull_request) Successful in 2m39s
CI / Build Plugin Zip (pull_request) Skipped
CI / Tests (PHP 8.2) (pull_request) Successful in 43s
CI / Tests (PHP 8.1) (pull_request) Successful in 44s
CI / No Debug Code (pull_request) Successful in 2s

The student-administration spec deferred three detail-view sections until
Payments landed. Adds them now: policy-acceptance history (title, version,
context, date), intake answers (label, answer, context), and — gated on
manage_billing — payment history with HST breakdown and receipt numbers.

New Auth\StudentHistory builds the display rows from per-student queries
added to AcceptanceRepository, AnswerRepository, and PaymentRepository;
the Payment model now carries created_at so unpaid rows still have a date.

Closes #69

Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
2026-07-18 18:17:20 -03:00
co-authored by Claude Fable 5
parent 05d1728248
commit c49171695a
15 changed files with 540 additions and 6 deletions
+180
View File
@@ -0,0 +1,180 @@
<?php
declare(strict_types=1);
namespace Unsupervised\Schedular\Tests\Unit\Auth;
use Mockery;
use Unsupervised\Schedular\Auth\StudentHistory;
use Unsupervised\Schedular\Payment\Payment;
use Unsupervised\Schedular\Payment\PaymentRepository;
use Unsupervised\Schedular\Policy\AcceptanceRepository;
use Unsupervised\Schedular\Policy\Policy;
use Unsupervised\Schedular\Policy\PolicyAcceptance;
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 StudentHistoryTest extends TestCase
{
private AcceptanceRepository&Mockery\MockInterface $acceptances;
private PolicyRepository&Mockery\MockInterface $policies;
private PolicyVersionRepository&Mockery\MockInterface $policyVersions;
private AnswerRepository&Mockery\MockInterface $answers;
private QuestionRepository&Mockery\MockInterface $questions;
private PaymentRepository&Mockery\MockInterface $payments;
private StudentHistory $history;
protected function setUp(): void
{
parent::setUp();
$this->acceptances = Mockery::mock(AcceptanceRepository::class);
$this->policies = Mockery::mock(PolicyRepository::class);
$this->policyVersions = Mockery::mock(PolicyVersionRepository::class);
$this->answers = Mockery::mock(AnswerRepository::class);
$this->questions = Mockery::mock(QuestionRepository::class);
$this->payments = Mockery::mock(PaymentRepository::class);
$this->history = new StudentHistory(
$this->acceptances,
$this->policies,
$this->policyVersions,
$this->answers,
$this->questions,
$this->payments
);
}
public function testPolicyAcceptancesResolvePolicyTitleAndVersion(): void
{
$this->acceptances->shouldReceive('findByStudent')->once()->with(5)->andReturn([
new PolicyAcceptance(9, 5, PolicyAcceptance::REG_ACCOUNT, 5, null, '2026-06-02 09:00:00', 1),
]);
$this->policyVersions->shouldReceive('findById')->with(9)
->andReturn(new PolicyVersion(2, 3, null, PolicyVersion::STATUS_PUBLISHED, id: 9));
$this->policies->shouldReceive('findById')->with(2)
->andReturn(new Policy('Waiver', 'waiver', 9, Policy::SCOPE_SIGNUP, 2));
$rows = $this->history->policyAcceptances(5);
self::assertSame(
[
[
'policy' => 'Waiver',
'version' => 'v3',
'context' => 'Account signup',
'accepted_at' => '2026-06-02 09:00:00',
],
],
$rows
);
}
public function testPolicyAcceptancesFallBackWhenVersionIsGone(): void
{
$this->acceptances->shouldReceive('findByStudent')->once()->with(5)->andReturn([
new PolicyAcceptance(9, 5, PolicyAcceptance::REG_LESSON, 12),
]);
$this->policyVersions->shouldReceive('findById')->with(9)->andReturn(null);
$rows = $this->history->policyAcceptances(5);
self::assertSame('#9', $rows[0]['policy']);
self::assertSame('—', $rows[0]['version']);
self::assertSame('Lesson #12', $rows[0]['context']);
self::assertSame('', $rows[0]['accepted_at']);
}
public function testIntakeAnswersResolveQuestionLabels(): void
{
$this->answers->shouldReceive('findByStudent')->once()->with(5)->andReturn([
new Answer(4, Answer::REG_ENROLLMENT, 3, 5, 'Beginner', 1),
]);
$this->questions->shouldReceive('findById')->with(4)
->andReturn(new Question(1, 'Experience level', id: 4));
$rows = $this->history->intakeAnswers(5);
self::assertSame(
[
[
'question' => 'Experience level',
'answer' => 'Beginner',
'context' => 'Enrolment #3',
],
],
$rows
);
}
public function testIntakeAnswersFallBackWhenQuestionIsGone(): void
{
$this->answers->shouldReceive('findByStudent')->once()->with(5)->andReturn([
new Answer(4, Answer::REG_LESSON, 12, 5, null, 1),
]);
$this->questions->shouldReceive('findById')->with(4)->andReturn(null);
$rows = $this->history->intakeAnswers(5);
self::assertSame('#4', $rows[0]['question']);
self::assertSame('—', $rows[0]['answer']);
}
public function testPaymentsBuildDisplayRows(): void
{
$this->payments->shouldReceive('findByStudent')->once()->with(5)->andReturn([
new Payment(
5,
3,
Payment::REG_LESSON,
12,
100.00,
'CAD',
Payment::METHOD_CARD,
Payment::STATUS_PAID,
taxRate: 13.0,
taxAmount: 13.00,
receiptNumber: 'USC-7',
createdAt: '2026-06-08 09:00:00',
id: 50
),
]);
$rows = $this->history->payments(5);
self::assertSame(
[
[
'created_at' => '2026-06-08 09:00:00',
'context' => 'Lesson #12',
'method' => Payment::METHOD_CARD,
'status' => Payment::STATUS_PAID,
'amount' => 100.00,
'tax_amount' => 13.00,
'total' => 113.00,
'currency' => 'CAD',
'receipt' => 'USC-7',
],
],
$rows
);
}
public function testPaymentsFallBackWhenUnpaidAndUndated(): void
{
$this->payments->shouldReceive('findByStudent')->once()->with(5)->andReturn([
new Payment(5, 3, Payment::REG_ENROLLMENT, 3, 40.00, id: 51),
]);
$rows = $this->history->payments(5);
self::assertSame('', $rows[0]['created_at']);
self::assertSame('Enrolment #3', $rows[0]['context']);
self::assertSame('—', $rows[0]['receipt']);
}
}
@@ -158,6 +158,22 @@ class PaymentRepositoryTest extends TestCase
self::assertCount(1, $this->repo->findPending());
}
public function testFindByStudentPreparesQueryAndMaps(): void
{
$this->db->shouldReceive('prepare')
->once()
->with(Mockery::pattern('/student_id = %d ORDER BY created_at DESC, id DESC/'), 'wp_us_payments', 5)
->andReturn('SELECT ...');
$this->db->shouldReceive('get_results')->andReturn([$this->row()]);
$payments = $this->repo->findByStudent(5);
self::assertCount(1, $payments);
self::assertSame(5, $payments[0]->studentId);
self::assertSame('2026-06-08 09:00:00', $payments[0]->createdAt);
}
private function row(): object
{
return (object) [
@@ -177,6 +193,7 @@ class PaymentRepositoryTest extends TestCase
'receipt_number' => null,
'receipt_sent_at' => null,
'paid_at' => null,
'created_at' => '2026-06-08 09:00:00',
];
}
}
+2
View File
@@ -46,6 +46,7 @@ class PaymentTest extends TestCase
'receipt_number' => 'USC-7',
'receipt_sent_at' => null,
'paid_at' => '2026-06-08 10:00:00',
'created_at' => '2026-06-08 09:00:00',
]);
self::assertSame(7, $payment->id);
@@ -55,6 +56,7 @@ class PaymentTest extends TestCase
self::assertSame(Payment::METHOD_COMP, $payment->method);
self::assertTrue($payment->isPaid());
self::assertSame('USC-7', $payment->receiptNumber);
self::assertSame('2026-06-08 09:00:00', $payment->createdAt);
}
public function testTotalAddsTaxToAmount(): void
@@ -90,4 +90,29 @@ class AcceptanceRepositoryTest extends TestCase
self::assertCount(1, $rows);
self::assertInstanceOf(PolicyAcceptance::class, $rows[0]);
}
public function testFindByStudentMapsRowsNewestFirst(): void
{
$this->db->shouldReceive('prepare')
->once()
->with(Mockery::pattern('/student_id = %d ORDER BY accepted_at DESC, id DESC/'), 'wp_us_policy_acceptances', 5)
->andReturn('SELECT ...');
$this->db->shouldReceive('get_results')->andReturn([
(object) [
'id' => '2',
'policy_version_id' => '9',
'student_id' => '5',
'registration_type' => PolicyAcceptance::REG_ACCOUNT,
'registration_id' => '5',
'accepted_at' => '2026-06-03 09:00:00',
'ip_address' => null,
],
]);
$rows = $this->repo->findByStudent(5);
self::assertCount(1, $rows);
self::assertSame(5, $rows[0]->studentId);
}
}
@@ -98,4 +98,32 @@ class AnswerRepositoryTest extends TestCase
self::assertInstanceOf(Answer::class, $answers[0]);
self::assertSame(3, $answers[0]->questionId);
}
public function testFindByStudentPreparesQueryAndMaps(): 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('/student_id = %d ORDER BY id DESC/'),
'wp_us_question_answers',
5
)
->andReturn('SELECT ...');
$this->db->shouldReceive('get_results')->andReturn([$row]);
$answers = $this->repo->findByStudent(5);
self::assertCount(1, $answers);
self::assertSame(5, $answers[0]->studentId);
}
}