CI / Tests (PHP 8.1) (pull_request) Successful in 47s
CI / Tests (PHP 8.2) (pull_request) Successful in 47s
CI / PHPStan (pull_request) Successful in 3m12s
CI / Tests (PHP 8.3) (pull_request) Successful in 2m42s
CI / Build Plugin Zip (pull_request) Skipped
CI / No Debug Code (pull_request) Successful in 3s
CI / Coding Standards (pull_request) Successful in 2m52s
Cancelling a lesson that was already paid for now credits the student that money instead of leaving it as a manual refund, and the daily scheduled-billing scan applies any available credit against their due charges before emailing the notice. - New us_credits ledger + us_payments.credit_applied column (Payment::netDue). - PaymentService::creditForCancelledLesson issues a per-lesson share of the covering payment's total; wired into all three cancel paths (student self-cancel, instructor status update, admin student-detail cancel). - PaymentService::applyCredits draws credit down FIFO across a run's charges, marking a fully-covered charge paid-by-credit; the notice shows the credit applied and reduced total, and the admin queue shows net due. - Student detail page shows a student's credit balance and history. Ships as part of the unreleased 1.2.0 (same release as scheduled billing). Tests: composer test (585), composer lint, composer cs all pass. Co-Authored-By: Claude Opus 4.8 <[email protected]>
174 lines
6.7 KiB
PHP
174 lines
6.7 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Unsupervised\Schedular\Tests\Unit\Auth;
|
|
|
|
use Brain\Monkey\Functions;
|
|
use Mockery;
|
|
use Unsupervised\Schedular\Auth\StudentActions;
|
|
use Unsupervised\Schedular\Availability\AvailabilityRepository;
|
|
use Unsupervised\Schedular\Booking\BookingRepository;
|
|
use Unsupervised\Schedular\Booking\Lesson;
|
|
use Unsupervised\Schedular\GroupClass\Enrollment;
|
|
use Unsupervised\Schedular\GroupClass\EnrollmentRepository;
|
|
use Unsupervised\Schedular\Payment\PaymentService;
|
|
use Unsupervised\Schedular\Tests\Unit\TestCase;
|
|
|
|
class StudentActionsTest extends TestCase
|
|
{
|
|
private BookingRepository&Mockery\MockInterface $bookings;
|
|
private AvailabilityRepository&Mockery\MockInterface $availability;
|
|
private EnrollmentRepository&Mockery\MockInterface $enrollments;
|
|
private PaymentService&Mockery\MockInterface $payments;
|
|
private StudentActions $actions;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->bookings = Mockery::mock(BookingRepository::class);
|
|
$this->availability = Mockery::mock(AvailabilityRepository::class);
|
|
$this->enrollments = Mockery::mock(EnrollmentRepository::class);
|
|
$this->payments = Mockery::mock(PaymentService::class);
|
|
|
|
$this->actions = new StudentActions($this->bookings, $this->availability, $this->enrollments, $this->payments);
|
|
}
|
|
|
|
public function testCancelLessonCancelsFreesSlotAndVoidsPayment(): void
|
|
{
|
|
$this->bookings->shouldReceive('findById')->with(12)
|
|
->andReturn(new Lesson(7, 5, 3, status: Lesson::STATUS_PENDING, paymentId: 40, id: 12));
|
|
|
|
$this->bookings->shouldReceive('updateStatus')->once()->with(12, Lesson::STATUS_CANCELLED)->andReturn(true);
|
|
$this->availability->shouldReceive('release')->once()->with(7)->andReturn(true);
|
|
$this->payments->shouldReceive('voidPending')->once()->with(40);
|
|
// A paid lesson is credited; the cancelled lesson value object is handed over.
|
|
$this->payments->shouldReceive('creditForCancelledLesson')
|
|
->once()
|
|
->with(Mockery::on(static fn (Lesson $l): bool => $l->id === 12 && $l->paymentId === 40));
|
|
|
|
self::assertTrue($this->actions->cancelLesson(12, 5));
|
|
}
|
|
|
|
public function testCancelLessonRefusesAnotherStudentsLesson(): void
|
|
{
|
|
$this->bookings->shouldReceive('findById')->with(12)
|
|
->andReturn(new Lesson(7, 6, 3, status: Lesson::STATUS_PENDING, id: 12));
|
|
|
|
$this->bookings->shouldNotReceive('updateStatus');
|
|
$this->availability->shouldNotReceive('release');
|
|
|
|
self::assertFalse($this->actions->cancelLesson(12, 5));
|
|
}
|
|
|
|
public function testCancelLessonRefusesAlreadyCancelledLesson(): void
|
|
{
|
|
$this->bookings->shouldReceive('findById')->with(12)
|
|
->andReturn(new Lesson(7, 5, 3, status: Lesson::STATUS_CANCELLED, id: 12));
|
|
|
|
$this->bookings->shouldNotReceive('updateStatus');
|
|
|
|
self::assertFalse($this->actions->cancelLesson(12, 5));
|
|
}
|
|
|
|
public function testCancelLessonRefusesMissingLesson(): void
|
|
{
|
|
$this->bookings->shouldReceive('findById')->with(99)->andReturn(null);
|
|
|
|
self::assertFalse($this->actions->cancelLesson(99, 5));
|
|
}
|
|
|
|
public function testWithdrawEnrollmentCancelsAndVoidsPayment(): void
|
|
{
|
|
$this->enrollments->shouldReceive('findById')->with(3)
|
|
->andReturn(new Enrollment(1, 5, 3, Enrollment::STATUS_ACTIVE, 41, 3));
|
|
|
|
$this->enrollments->shouldReceive('updateStatus')->once()->with(3, Enrollment::STATUS_CANCELLED)->andReturn(true);
|
|
$this->payments->shouldReceive('voidPending')->once()->with(41);
|
|
|
|
self::assertTrue($this->actions->withdrawEnrollment(3, 5));
|
|
}
|
|
|
|
public function testWithdrawEnrollmentRefusesInactiveEnrolment(): void
|
|
{
|
|
$this->enrollments->shouldReceive('findById')->with(3)
|
|
->andReturn(new Enrollment(1, 5, 3, Enrollment::STATUS_CANCELLED, null, 3));
|
|
|
|
$this->enrollments->shouldNotReceive('updateStatus');
|
|
|
|
self::assertFalse($this->actions->withdrawEnrollment(3, 5));
|
|
}
|
|
|
|
public function testWithdrawEnrollmentRefusesAnotherStudentsEnrolment(): void
|
|
{
|
|
$this->enrollments->shouldReceive('findById')->with(3)
|
|
->andReturn(new Enrollment(1, 6, 3, Enrollment::STATUS_ACTIVE, null, 3));
|
|
|
|
$this->enrollments->shouldNotReceive('updateStatus');
|
|
|
|
self::assertFalse($this->actions->withdrawEnrollment(3, 5));
|
|
}
|
|
|
|
public function testUpdateAccountSavesNameAndEmail(): void
|
|
{
|
|
Functions\when('is_email')->justReturn(true);
|
|
Functions\when('email_exists')->justReturn(false);
|
|
Functions\expect('wp_update_user')
|
|
->once()
|
|
->with(['ID' => 5, 'display_name' => 'New Name', 'user_email' => '[email protected]'])
|
|
->andReturn(5);
|
|
|
|
self::assertTrue($this->actions->updateAccount(5, 'New Name', '[email protected]'));
|
|
}
|
|
|
|
public function testUpdateAccountAllowsKeepingOwnEmail(): void
|
|
{
|
|
Functions\when('is_email')->justReturn(true);
|
|
Functions\when('email_exists')->justReturn(5);
|
|
Functions\when('wp_update_user')->justReturn(5);
|
|
|
|
self::assertTrue($this->actions->updateAccount(5, 'Name', '[email protected]'));
|
|
}
|
|
|
|
public function testUpdateAccountRejectsEmptyName(): void
|
|
{
|
|
$result = $this->actions->updateAccount(5, '', '[email protected]');
|
|
|
|
self::assertInstanceOf(\WP_Error::class, $result);
|
|
self::assertSame('empty_name', $result->get_error_code());
|
|
}
|
|
|
|
public function testUpdateAccountRejectsInvalidEmail(): void
|
|
{
|
|
Functions\when('is_email')->justReturn(false);
|
|
|
|
$result = $this->actions->updateAccount(5, 'Name', 'not-an-email');
|
|
|
|
self::assertInstanceOf(\WP_Error::class, $result);
|
|
self::assertSame('invalid_email', $result->get_error_code());
|
|
}
|
|
|
|
public function testUpdateAccountRejectsEmailOwnedByAnotherUser(): void
|
|
{
|
|
Functions\when('is_email')->justReturn(true);
|
|
Functions\when('email_exists')->justReturn(9);
|
|
|
|
$result = $this->actions->updateAccount(5, 'Name', '[email protected]');
|
|
|
|
self::assertInstanceOf(\WP_Error::class, $result);
|
|
self::assertSame('email_taken', $result->get_error_code());
|
|
}
|
|
|
|
public function testUpdateAccountPassesThroughWpError(): void
|
|
{
|
|
Functions\when('is_email')->justReturn(true);
|
|
Functions\when('email_exists')->justReturn(false);
|
|
Functions\when('wp_update_user')->justReturn(new \WP_Error('update_failed', 'nope'));
|
|
|
|
$result = $this->actions->updateAccount(5, 'Name', '[email protected]');
|
|
|
|
self::assertInstanceOf(\WP_Error::class, $result);
|
|
self::assertSame('update_failed', $result->get_error_code());
|
|
}
|
|
}
|