CI / Tests (PHP 8.1) (pull_request) Successful in 45s
CI / No Debug Code (pull_request) Successful in 2s
CI / Tests (PHP 8.2) (pull_request) Successful in 55s
CI / PHPStan (pull_request) Successful in 2m57s
CI / Coding Standards (pull_request) Successful in 3m3s
CI / Tests (PHP 8.3) (pull_request) Successful in 2m42s
CI / Build Plugin Zip (pull_request) Skipped
The Profile block is headed "Your profile", but the one person on it you could not change was yourself: your name, your birth year, and whether you take lessons yourself were fixed at whatever signup recorded, and correcting any of them meant asking a studio admin. A "Your details" section now opens the page, saved through the same nonce-checked template_redirect post/redirect/get path the child rows use: - Your name, written to display_name and nickname together, for the reason updateChild() does — UserName reads the nickname first, and leaving it behind would put the account's email address back on every screen that names a person. - "I take lessons myself", the positive of us_guardian_only. This makes good on the claim already in bookableStudents() and the feature doc that a guardian-only account can put itself right from the profile page. - Your birth year, held to the same normaliseBirthYear() rule as every other student. The email is shown but not editable: it is the account's user_login as well as its address, so changing it stays a studio-side job. The birth-year field deliberately carries no `required` attribute. It is asked of a student only, and this page loads no JavaScript, so a browser-enforced `required` would leave a guardian who books solely for other people unable to submit the form at all; handleSelf() enforces it against the checkbox instead. Unticking the box does not clear a stored birth year — it says who books, not "forget what is on file". Closes #165 Co-Authored-By: Claude Opus 5 <[email protected]>
468 lines
16 KiB
PHP
468 lines
16 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Unsupervised\Schedular\Tests\Unit\Guardian;
|
|
|
|
use Brain\Monkey\Functions;
|
|
use Mockery;
|
|
use Unsupervised\Schedular\Guardian\FamilyPage;
|
|
use Unsupervised\Schedular\Guardian\GuardianService;
|
|
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 FamilyPageTest extends TestCase
|
|
{
|
|
private GuardianService&Mockery\MockInterface $guardians;
|
|
private QuestionRepository&Mockery\MockInterface $questions;
|
|
private AnswerRepository&Mockery\MockInterface $answers;
|
|
private FamilyPage $page;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->guardians = Mockery::mock(GuardianService::class);
|
|
$this->questions = Mockery::mock(QuestionRepository::class);
|
|
$this->answers = Mockery::mock(AnswerRepository::class);
|
|
|
|
$this->page = new FamilyPage($this->guardians, $this->questions, $this->answers);
|
|
|
|
$_POST = [];
|
|
$_GET = [];
|
|
|
|
Functions\when('is_user_logged_in')->justReturn(true);
|
|
Functions\when('get_current_user_id')->justReturn(5);
|
|
Functions\when('wp_enqueue_style')->justReturn(null);
|
|
Functions\when('wp_nonce_field')->justReturn('');
|
|
Functions\when('get_permalink')->justReturn('https://studio.test/family/');
|
|
// The birth-year input caps itself at the current year.
|
|
Functions\when('current_time')->justReturn('2026');
|
|
Functions\when('absint')->alias(static fn ($value) => abs((int) $value));
|
|
Functions\when('sanitize_key')->alias(static fn (string $v): string => strtolower(preg_replace('/[^a-z0-9_\-]/i', '', $v) ?? ''));
|
|
Functions\when('sanitize_text_field')->alias(static fn (string $v): string => trim($v));
|
|
Functions\when('sanitize_textarea_field')->alias(static fn (string $v): string => trim($v));
|
|
Functions\when('wp_unslash')->alias(static fn ($v) => $v);
|
|
Functions\when('check_admin_referer')->justReturn(true);
|
|
Functions\when('add_query_arg')->alias(
|
|
static fn (string $key, $value, string $url): string => $url . '?' . $key . '=' . $value
|
|
);
|
|
}
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
$_POST = [];
|
|
$_GET = [];
|
|
parent::tearDown();
|
|
}
|
|
|
|
/**
|
|
* A FamilyPage whose redirect is captured instead of exiting the process.
|
|
*
|
|
* @param-out string $captured
|
|
*/
|
|
private function capturingPage(?string &$captured): FamilyPage
|
|
{
|
|
$page = Mockery::mock(FamilyPage::class, [$this->guardians, $this->questions, $this->answers])
|
|
->makePartial()
|
|
->shouldAllowMockingProtectedMethods();
|
|
|
|
$page->shouldReceive('redirect')->andReturnUsing(static function (string $url) use (&$captured): void {
|
|
$captured = $url;
|
|
});
|
|
|
|
return $page;
|
|
}
|
|
|
|
/**
|
|
* The account holder's own details, which every render reads.
|
|
*
|
|
* @param array{name?: string, email?: string, birth_year?: string, is_student?: bool} $overrides
|
|
*/
|
|
private function expectAccountHolder(array $overrides = []): void
|
|
{
|
|
$this->guardians->shouldReceive('accountHolder')->with(5)->andReturn($overrides + [
|
|
'name' => 'Grace',
|
|
'email' => '[email protected]',
|
|
'birth_year' => '1984',
|
|
'is_student' => true,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* A question required of everyone, or of nobody — the shape every question
|
|
* had before the account holder and the students could differ, and the shape
|
|
* the upgrade backfill leaves them in.
|
|
*/
|
|
private function question(int $id, bool $required): Question
|
|
{
|
|
return new Question(
|
|
offeringId: null,
|
|
label: 'Instrument',
|
|
isRequired: $required,
|
|
scope: Question::SCOPE_ACCOUNT,
|
|
isRequiredChild: $required,
|
|
id: $id
|
|
);
|
|
}
|
|
|
|
public function testLoggedOutVisitorIsOfferedALoginLink(): void
|
|
{
|
|
Functions\when('is_user_logged_in')->justReturn(false);
|
|
Functions\when('wp_login_url')->justReturn('https://studio.test/wp-login.php');
|
|
|
|
$html = $this->page->render([]);
|
|
|
|
self::assertStringContainsString('log in to manage your profile', $html);
|
|
}
|
|
|
|
public function testRenderListsTheGuardiansChildren(): void
|
|
{
|
|
$this->expectAccountHolder();
|
|
$this->guardians->shouldReceive('children')->once()->with(5)->andReturn([
|
|
['id' => 42, 'name' => 'Ada', 'birth_year' => '2015', 'relationship' => 'Parent'],
|
|
]);
|
|
$this->questions->shouldReceive('findByScope')->andReturn([]);
|
|
|
|
$html = $this->page->render([]);
|
|
|
|
self::assertStringContainsString('Ada', $html);
|
|
self::assertStringContainsString('2015', $html);
|
|
self::assertStringContainsString('Add a student', $html);
|
|
}
|
|
|
|
public function testRenderShowsTheAccountHoldersOwnDetails(): void
|
|
{
|
|
$this->expectAccountHolder();
|
|
$this->guardians->shouldReceive('children')->andReturn([]);
|
|
$this->questions->shouldReceive('findByScope')->andReturn([]);
|
|
|
|
$html = $this->page->render([]);
|
|
|
|
self::assertStringContainsString('Your details', $html);
|
|
self::assertStringContainsString('value="Grace"', $html);
|
|
self::assertStringContainsString('[email protected]', $html);
|
|
self::assertStringContainsString('value="1984"', $html);
|
|
// A student in their own right has the box ticked.
|
|
self::assertStringContainsString("checked='checked'", $html);
|
|
}
|
|
|
|
public function testAGuardianOnlyAccountRendersTheStudentBoxUnticked(): void
|
|
{
|
|
$this->expectAccountHolder(['is_student' => false]);
|
|
$this->guardians->shouldReceive('children')->andReturn([]);
|
|
$this->questions->shouldReceive('findByScope')->andReturn([]);
|
|
|
|
$html = $this->page->render([]);
|
|
|
|
self::assertStringContainsString('name="is_student"', $html);
|
|
self::assertStringNotContainsString("checked='checked'", $html);
|
|
}
|
|
|
|
/**
|
|
* The birth-year field must not carry `required`: it is asked of a student
|
|
* only, and the browser would otherwise block a guardian who books solely
|
|
* for other people from ever saving the form.
|
|
*/
|
|
public function testTheOwnBirthYearFieldIsNotBrowserRequired(): void
|
|
{
|
|
$this->expectAccountHolder(['is_student' => false, 'birth_year' => '']);
|
|
$this->guardians->shouldReceive('children')->andReturn([]);
|
|
$this->questions->shouldReceive('findByScope')->andReturn([]);
|
|
|
|
$html = $this->page->render([]);
|
|
|
|
self::assertMatchesRegularExpression('/<input[^>]*name="own_birth_year"(?![^>]*\brequired\b)[^>]*>/', $html);
|
|
}
|
|
|
|
public function testSavingOwnDetailsDelegatesToTheServiceAndRedirects(): void
|
|
{
|
|
$_POST = [
|
|
'us_family_action' => 'self',
|
|
'own_name' => 'Grace H',
|
|
'own_birth_year' => '1984',
|
|
'is_student' => '1',
|
|
];
|
|
|
|
$this->guardians->shouldReceive('updateSelf')->once()->with(5, 'Grace H', '1984', true)->andReturn(null);
|
|
|
|
$captured = null;
|
|
$this->capturingPage($captured)->maybeHandleSubmit();
|
|
|
|
self::assertSame('https://studio.test/family/?us_family=self', $captured);
|
|
}
|
|
|
|
/** An unticked checkbox is simply absent from the post — that is the "no". */
|
|
public function testAnAbsentStudentBoxSavesTheAccountAsGuardianOnly(): void
|
|
{
|
|
$_POST = [
|
|
'us_family_action' => 'self',
|
|
'own_name' => 'Grace H',
|
|
'own_birth_year' => '',
|
|
];
|
|
|
|
$this->guardians->shouldReceive('updateSelf')->once()->with(5, 'Grace H', '', false)->andReturn(null);
|
|
|
|
$captured = null;
|
|
$this->capturingPage($captured)->maybeHandleSubmit();
|
|
|
|
self::assertSame('https://studio.test/family/?us_family=self', $captured);
|
|
}
|
|
|
|
public function testOwnDetailsRefusalIsShownRatherThanRedirected(): void
|
|
{
|
|
$_POST = ['us_family_action' => 'self', 'own_name' => 'Grace', 'is_student' => '1'];
|
|
|
|
$this->guardians->shouldReceive('updateSelf')->once()->andReturn(
|
|
new \WP_Error('missing_birth_year', 'Please give your birth year.')
|
|
);
|
|
|
|
$captured = null;
|
|
$page = $this->capturingPage($captured);
|
|
$page->shouldNotReceive('redirect');
|
|
|
|
$page->maybeHandleSubmit();
|
|
|
|
self::assertNull($captured);
|
|
}
|
|
|
|
public function testAddCreatesTheChildRecordsItsAnswersAndRedirects(): void
|
|
{
|
|
$_POST = [
|
|
'us_family_action' => 'add',
|
|
'child_name' => 'Ada',
|
|
'child_birth_year' => '2015',
|
|
'child_relationship' => 'Parent',
|
|
'us_answers' => [7 => 'Piano'],
|
|
];
|
|
|
|
$this->questions->shouldReceive('findByScope')->once()->andReturn([$this->question(7, true)]);
|
|
$this->guardians->shouldReceive('createChild')->once()->with(5, 'Ada', '2015', 'Parent')->andReturn(42);
|
|
|
|
$this->answers->shouldReceive('insert')
|
|
->once()
|
|
->with(Mockery::on(static fn (Answer $a): bool =>
|
|
$a->questionId === 7
|
|
&& $a->studentId === 42
|
|
&& $a->registrationId === 42
|
|
&& $a->registrationType === Answer::REG_ACCOUNT
|
|
&& $a->answerValue === 'Piano'));
|
|
|
|
$captured = null;
|
|
$this->capturingPage($captured)->maybeHandleSubmit();
|
|
|
|
self::assertSame('https://studio.test/family/?us_family=added', $captured);
|
|
}
|
|
|
|
/**
|
|
* Validating before creating is what stops a missing answer from leaving a
|
|
* half-added child behind.
|
|
*/
|
|
public function testAddRefusesAMissingRequiredAnswerBeforeCreatingTheChild(): void
|
|
{
|
|
$_POST = [
|
|
'us_family_action' => 'add',
|
|
'child_name' => 'Ada',
|
|
'us_answers' => [7 => ' '],
|
|
];
|
|
|
|
$this->questions->shouldReceive('findByScope')->once()->andReturn([$this->question(7, true)]);
|
|
$this->guardians->shouldNotReceive('createChild');
|
|
$this->answers->shouldNotReceive('insert');
|
|
|
|
$captured = null;
|
|
$page = $this->capturingPage($captured);
|
|
$page->shouldNotReceive('redirect');
|
|
|
|
$page->maybeHandleSubmit();
|
|
|
|
self::assertNull($captured);
|
|
}
|
|
|
|
/**
|
|
* This screen only ever adds a student, so the students' required-ness is the
|
|
* one that applies: a question required of the account holder alone must not
|
|
* stop a guardian adding a child.
|
|
*/
|
|
public function testAddIsNotBlockedByAQuestionRequiredOnlyOfTheAccountHolder(): void
|
|
{
|
|
$_POST = [
|
|
'us_family_action' => 'add',
|
|
'child_name' => 'Ada',
|
|
'child_birth_year' => '2015',
|
|
'us_answers' => [7 => ' '],
|
|
];
|
|
|
|
$question = new Question(
|
|
offeringId: null,
|
|
label: 'Instrument',
|
|
isRequired: true,
|
|
scope: Question::SCOPE_ACCOUNT,
|
|
isRequiredChild: false,
|
|
id: 7
|
|
);
|
|
|
|
$this->questions->shouldReceive('findByScope')->once()->andReturn([$question]);
|
|
$this->guardians->shouldReceive('createChild')->once()->andReturn(42);
|
|
|
|
// Nothing was typed, so nothing is stored — but the add went through.
|
|
$this->answers->shouldNotReceive('insert');
|
|
|
|
$captured = null;
|
|
$this->capturingPage($captured)->maybeHandleSubmit();
|
|
|
|
self::assertSame('https://studio.test/family/?us_family=added', $captured);
|
|
}
|
|
|
|
public function testAddIsBlockedByAQuestionRequiredOnlyOfTheStudents(): void
|
|
{
|
|
$_POST = [
|
|
'us_family_action' => 'add',
|
|
'child_name' => 'Ada',
|
|
'child_birth_year' => '2015',
|
|
'us_answers' => [7 => ''],
|
|
];
|
|
|
|
$question = new Question(
|
|
offeringId: null,
|
|
label: 'Instrument',
|
|
isRequired: false,
|
|
scope: Question::SCOPE_ACCOUNT,
|
|
isRequiredChild: true,
|
|
id: 7
|
|
);
|
|
|
|
$this->questions->shouldReceive('findByScope')->once()->andReturn([$question]);
|
|
$this->guardians->shouldNotReceive('createChild');
|
|
|
|
$captured = null;
|
|
$page = $this->capturingPage($captured);
|
|
$page->shouldNotReceive('redirect');
|
|
|
|
$page->maybeHandleSubmit();
|
|
|
|
self::assertNull($captured);
|
|
}
|
|
|
|
public function testAddSurfacesAServiceErrorInsteadOfRedirecting(): void
|
|
{
|
|
$_POST = ['us_family_action' => 'add', 'child_name' => ''];
|
|
|
|
$this->questions->shouldReceive('findByScope')->once()->andReturn([]);
|
|
$this->guardians->shouldReceive('createChild')->once()->andReturn(new \WP_Error('missing_name', 'Please give each student a name.'));
|
|
$this->answers->shouldNotReceive('insert');
|
|
|
|
$captured = null;
|
|
$page = $this->capturingPage($captured);
|
|
$page->shouldNotReceive('redirect');
|
|
|
|
$page->maybeHandleSubmit();
|
|
|
|
self::assertNull($captured);
|
|
}
|
|
|
|
public function testEditDelegatesToTheServiceAndRedirects(): void
|
|
{
|
|
$_POST = [
|
|
'us_family_action' => 'edit',
|
|
'child_id' => '42',
|
|
'child_name' => 'Ada L',
|
|
'child_birth_year' => '2015',
|
|
];
|
|
|
|
$this->guardians->shouldReceive('updateChild')->once()->with(5, 42, 'Ada L', '2015')->andReturn(null);
|
|
|
|
$captured = null;
|
|
$this->capturingPage($captured)->maybeHandleSubmit();
|
|
|
|
self::assertSame('https://studio.test/family/?us_family=updated', $captured);
|
|
}
|
|
|
|
public function testRemoveDelegatesToTheServiceAndRedirects(): void
|
|
{
|
|
$_POST = ['us_family_action' => 'remove', 'child_id' => '42'];
|
|
|
|
$this->guardians->shouldReceive('removeChild')->once()->with(5, 42)->andReturn(null);
|
|
|
|
$captured = null;
|
|
$this->capturingPage($captured)->maybeHandleSubmit();
|
|
|
|
self::assertSame('https://studio.test/family/?us_family=removed', $captured);
|
|
}
|
|
|
|
public function testRemoveRefusalIsShownRatherThanRedirected(): void
|
|
{
|
|
$_POST = ['us_family_action' => 'remove', 'child_id' => '42'];
|
|
|
|
$this->guardians->shouldReceive('removeChild')->once()->andReturn(
|
|
new \WP_Error('has_history', 'This student has lessons or enrolments on record.')
|
|
);
|
|
|
|
$captured = null;
|
|
$page = $this->capturingPage($captured);
|
|
$page->shouldNotReceive('redirect');
|
|
|
|
$page->maybeHandleSubmit();
|
|
|
|
self::assertNull($captured);
|
|
}
|
|
|
|
public function testAnUnrecognisedActionDoesNothing(): void
|
|
{
|
|
$_POST = ['us_family_action' => 'destroy'];
|
|
|
|
$this->guardians->shouldNotReceive('createChild');
|
|
$this->guardians->shouldNotReceive('removeChild');
|
|
|
|
$captured = null;
|
|
$page = $this->capturingPage($captured);
|
|
$page->shouldNotReceive('redirect');
|
|
|
|
$page->maybeHandleSubmit();
|
|
|
|
self::assertNull($captured);
|
|
}
|
|
|
|
public function testNoActionIsANoOp(): void
|
|
{
|
|
$this->guardians->shouldNotReceive('createChild');
|
|
|
|
$captured = null;
|
|
$page = $this->capturingPage($captured);
|
|
$page->shouldNotReceive('redirect');
|
|
|
|
$page->maybeHandleSubmit();
|
|
|
|
self::assertNull($captured);
|
|
}
|
|
|
|
public function testLoggedOutSubmissionIsIgnored(): void
|
|
{
|
|
Functions\when('is_user_logged_in')->justReturn(false);
|
|
$_POST = ['us_family_action' => 'remove', 'child_id' => '42'];
|
|
|
|
$this->guardians->shouldNotReceive('removeChild');
|
|
|
|
$captured = null;
|
|
$page = $this->capturingPage($captured);
|
|
$page->shouldNotReceive('redirect');
|
|
|
|
$page->maybeHandleSubmit();
|
|
|
|
self::assertNull($captured);
|
|
}
|
|
|
|
public function testCompletedActionRendersItsConfirmation(): void
|
|
{
|
|
$_GET = ['us_family' => 'added'];
|
|
|
|
$this->expectAccountHolder();
|
|
$this->guardians->shouldReceive('children')->andReturn([]);
|
|
$this->questions->shouldReceive('findByScope')->andReturn([]);
|
|
|
|
self::assertStringContainsString('Student added.', $this->page->render([]));
|
|
}
|
|
}
|