Files
unsupervised-scheduler/tests/Unit/Payment/PaymentDueMailerTest.php
T
Kydoimosandanthropic/claude-opus-4-8 a2c609803e
CI / Coding Standards (pull_request) Successful in 29s
CI / Tests (PHP 8.2) (pull_request) Successful in 31s
CI / Tests (PHP 8.3) (pull_request) Successful in 31s
CI / Tests (PHP 8.5) (pull_request) Successful in 31s
CI / No Debug Code (pull_request) Successful in 9s
CI / Tests (PHP 8.1) (pull_request) Successful in 44s
CI / Static Analysis (pull_request) Successful in 51s
CI / Build Plugin Zip (pull_request) Skipped
Add editable, previewable payment-due email template
The "Payment due" notice the daily billing scan sends was fixed wording
baked into PaymentDueMailer. Studio admins can now view, edit and preview
its subject and body under Studio Settings -> Payment Due Email.

- PaymentDueEmailTemplate stores subject/body/item-line as us_payment_due_email_*
  options, each falling back to the built-in default when blank, and renders
  via {token} substitution.
- PaymentDueMailer now builds a token map from the same consolidated
  items/credit/e-transfer/reference data and renders the stored template; the
  default template reproduces the previous wording exactly.
- PaymentEmailController serves the view/edit/save/reset admin page
  (manage_billing, nonce-checked) with a server-rendered sample preview.
- PaymentEmailPreviewEndpoint (POST us-scheduler/v1/payment-email/preview,
  manage_billing) + payment-email-admin.js drive a live preview of unsaved edits.
- Submenu wired under Studio Settings; endpoint wired in RestRegistrar.
- Bump to 1.6.0 and add CHANGELOG entry. New options default gracefully, so
  existing sites need no migration and send the identical notice until edited.

Co-authored-by: anthropic/claude-opus-4-8
2026-09-17 17:33:23 -03:00

180 lines
6.9 KiB
PHP

<?php
declare(strict_types=1);
namespace Unsupervised\Schedular\Tests\Unit\Payment;
use Brain\Monkey\Functions;
use Mockery;
use Unsupervised\Schedular\Payment\PaymentDueEmailTemplate;
use Unsupervised\Schedular\Payment\PaymentDueMailer;
use Unsupervised\Schedular\Tests\Unit\TestCase;
class PaymentDueMailerTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
// The mailer renders from PaymentDueEmailTemplate, which reads its
// subject/body/item-line from options. An empty stored value means the
// built-in default template is used — the behaviour these tests assert.
Functions\when('get_option')->alias(static fn (string $name, $default = '') => '');
}
private function student(string $email, string $name = 'Alex Student'): \WP_User
{
$student = Mockery::mock(\WP_User::class);
$student->user_email = $email;
$student->display_name = $name;
return $student;
}
public function testReturnsFalseWithoutRecipient(): void
{
$items = [[ 'label' => 'x', 'amount' => 10.0, 'currency' => 'CAD', 'due_date' => '2026-07-14', 'etransfer_email' => null ]];
self::assertFalse((new PaymentDueMailer())->send($this->student(''), $items));
}
public function testReturnsFalseWithNoItems(): void
{
self::assertFalse((new PaymentDueMailer())->send($this->student('[email protected]'), []));
}
public function testConsolidatesItemsWithGrandTotal(): void
{
Functions\expect('wp_mail')
->once()
->with(
'[email protected]',
Mockery::type('string'),
Mockery::on(static function (string $body): bool {
return str_contains($body, 'Piano')
&& str_contains($body, 'Jul 15, 2026')
&& str_contains($body, 'Guitar')
&& str_contains($body, 'Jul 22, 2026')
&& str_contains($body, '35.00')
&& str_contains($body, '40.00')
// 35 + 40 grand total
&& str_contains($body, '75.00');
})
)
->andReturn(true);
$items = [
[ 'label' => 'Piano', 'amount' => 35.0, 'currency' => 'CAD', 'due_date' => '2026-07-15', 'etransfer_email' => null ],
[ 'label' => 'Guitar', 'amount' => 40.0, 'currency' => 'CAD', 'due_date' => '2026-07-22', 'etransfer_email' => null ],
];
self::assertTrue((new PaymentDueMailer())->send($this->student('[email protected]'), $items));
}
public function testIncludesReferenceWhenProvided(): void
{
Functions\expect('wp_mail')
->once()
->with(
'[email protected]',
Mockery::type('string'),
Mockery::on(static fn (string $body): bool => str_contains($body, 'REF12345'))
)
->andReturn(true);
$items = [[ 'label' => 'Piano', 'amount' => 35.0, 'currency' => 'CAD', 'due_date' => '2026-07-15', 'etransfer_email' => null ]];
self::assertTrue((new PaymentDueMailer())->send($this->student('[email protected]'), $items, 'REF12345'));
}
public function testCreditReducesTheTotalDue(): void
{
Functions\expect('wp_mail')
->once()
->with(
'[email protected]',
Mockery::type('string'),
Mockery::on(static function (string $body): bool {
// Line shows the full 35.00; credit line shows -20.00; total due 15.00.
return str_contains($body, '35.00')
&& str_contains($body, '-CAD 20.00')
&& str_contains($body, 'Total due: CAD 15.00');
})
)
->andReturn(true);
$items = [[ 'label' => 'Piano', 'amount' => 35.0, 'currency' => 'CAD', 'due_date' => '2026-07-15', 'etransfer_email' => '[email protected]' ]];
self::assertTrue((new PaymentDueMailer())->send($this->student('[email protected]'), $items, 'REF1', 20.0));
}
public function testCreditCoveringEverythingLeavesZeroDueAndNoEtransferLine(): void
{
Functions\expect('wp_mail')
->once()
->with(
'[email protected]',
Mockery::type('string'),
Mockery::on(static function (string $body): bool {
return str_contains($body, 'Total due: CAD 0.00')
&& ! str_contains($body, '[email protected]');
})
)
->andReturn(true);
$items = [[ 'label' => 'Piano', 'amount' => 35.0, 'currency' => 'CAD', 'due_date' => '2026-07-15', 'etransfer_email' => '[email protected]' ]];
self::assertTrue((new PaymentDueMailer())->send($this->student('[email protected]'), $items, '', 35.0));
}
public function testIncludesEtransferDestination(): void
{
Functions\expect('wp_mail')
->once()
->with(
'[email protected]',
Mockery::type('string'),
Mockery::on(static fn (string $body): bool => str_contains($body, '[email protected]'))
)
->andReturn(true);
$items = [[ 'label' => 'Piano', 'amount' => 35.0, 'currency' => 'CAD', 'due_date' => '2026-07-15', 'etransfer_email' => '[email protected]' ]];
self::assertTrue((new PaymentDueMailer())->send($this->student('[email protected]'), $items));
}
public function testRendersCustomTemplateWithTokens(): void
{
// A stored template overrides the default; tokens are substituted with
// the real values gathered from the items and student.
Functions\when('get_option')->alias(static function (string $name) {
if ($name === PaymentDueEmailTemplate::OPT_SUBJECT) {
return 'Hi {student_name} — {total_due}';
}
if ($name === PaymentDueEmailTemplate::OPT_BODY) {
return "Dear {student_name},\n{items}\nOwing: {total_due}";
}
if ($name === PaymentDueEmailTemplate::OPT_ITEM_LINE) {
return '* {label} = {currency} {amount}';
}
return '';
});
Functions\expect('wp_mail')
->once()
->with(
'[email protected]',
'Hi Jordan — CAD 35.00',
Mockery::on(static function (string $body): bool {
return str_contains($body, 'Dear Jordan,')
&& str_contains($body, '* Piano = CAD 35.00')
&& str_contains($body, 'Owing: CAD 35.00');
})
)
->andReturn(true);
$items = [[ 'label' => 'Piano', 'amount' => 35.0, 'currency' => 'CAD', 'due_date' => '2026-07-15', 'etransfer_email' => null ]];
self::assertTrue((new PaymentDueMailer())->send($this->student('[email protected]', 'Jordan'), $items));
}
}