CI / Coding Standards (pull_request) Failing after 28s
CI / Tests (PHP 8.5) (pull_request) Failing after 27s
CI / No Debug Code (pull_request) Successful in 3s
CI / Tests (PHP 8.1) (pull_request) Failing after 39s
CI / Tests (PHP 8.3) (pull_request) Failing after 1m7s
CI / Tests (PHP 8.2) (pull_request) Failing after 1m8s
CI / Static Analysis (pull_request) Successful in 1m17s
CI / Build Plugin Zip (pull_request) Skipped
The assessment looked for three things: whether students can reach each other's bookings, whether payment settings can be dodged, and whether the plugin opens a way into the rest of the install. The student-isolation and payment paths held up. These are what did not. - The front-end login form told WordPress not to work out whether the site was secure, so on HTTPS every student's session cookie was issued without the Secure flag. wp_signon() only derives it from is_ssl() when the second argument is left at its default; an explicit false reads like "no preference" and is not. - The update check took whatever download URL the release API returned and handed it to core, which unpacks it over the installed plugin. The package must now be https on git.unsupervised.ca exactly, compared on the parsed host so a lookalike name cannot pass. - Uninstalling dropped 2 of 14 tables and left the Stripe secret and webhook signing key in wp_options. Removal is now a choice made in advance on Access -> Plugin removal: records are kept unless the owner opts in (with a typed confirmation), while credentials and the borrowed core registration settings go every time. - Open registration switches on the site-wide users_can_register and makes Student the default role, arming any other signup form on the site to mint students who could book and be billed immediately. The pending state is now decided once, on user_register, rather than by whichever form created the account. - Cancel and withdraw answered "not yours" differently from "does not exist", which let a signed-in student enumerate the studio's bookings. Both now give the same 404. Co-Authored-By: Claude Opus 5 <[email protected]>
177 lines
5.6 KiB
PHP
177 lines
5.6 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Unsupervised\Schedular\Tests\Unit\Auth;
|
|
|
|
use Brain\Monkey\Functions;
|
|
use Mockery;
|
|
use Unsupervised\Schedular\Auth\RegistrationLoginGate;
|
|
use Unsupervised\Schedular\Auth\RegistrationStatus;
|
|
use Unsupervised\Schedular\Auth\RoleManager;
|
|
use Unsupervised\Schedular\Tests\Unit\TestCase;
|
|
|
|
class RegistrationLoginGateTest extends TestCase
|
|
{
|
|
/**
|
|
* Stub get_user_meta to describe a single account's pending state.
|
|
*/
|
|
private function stubMeta(string $awaiting, string $confirmed): void
|
|
{
|
|
Functions\when('get_user_meta')->alias(static function (int $id, string $key) use ($awaiting, $confirmed) {
|
|
if ($key === RegistrationStatus::META_AWAITING_APPROVAL) {
|
|
return $awaiting;
|
|
}
|
|
if ($key === RegistrationStatus::META_EMAIL_CONFIRMED) {
|
|
return $confirmed;
|
|
}
|
|
return '';
|
|
});
|
|
}
|
|
|
|
private function user(int $id): \WP_User
|
|
{
|
|
$user = Mockery::mock(\WP_User::class);
|
|
$user->ID = $id;
|
|
|
|
return $user;
|
|
}
|
|
|
|
public function testBlocksLoginWhileEmailUnconfirmed(): void
|
|
{
|
|
$this->stubMeta('1', '');
|
|
|
|
$result = (new RegistrationLoginGate())->blockUnconfirmed($this->user(7));
|
|
|
|
self::assertInstanceOf(\WP_Error::class, $result);
|
|
self::assertSame('us_email_unconfirmed', $result->get_error_code());
|
|
}
|
|
|
|
public function testAllowsLoginWhenConfirmedButAwaitingApproval(): void
|
|
{
|
|
$this->stubMeta('1', '1');
|
|
|
|
$user = $this->user(7);
|
|
|
|
self::assertSame($user, (new RegistrationLoginGate())->blockUnconfirmed($user));
|
|
}
|
|
|
|
public function testAllowsLoginForApprovedStudent(): void
|
|
{
|
|
$this->stubMeta('', '1');
|
|
|
|
$user = $this->user(7);
|
|
|
|
self::assertSame($user, (new RegistrationLoginGate())->blockUnconfirmed($user));
|
|
}
|
|
|
|
public function testIgnoresUsersWithNoPendingMeta(): void
|
|
{
|
|
// Invite/admin-created students carry no meta at all.
|
|
$this->stubMeta('', '');
|
|
|
|
$user = $this->user(7);
|
|
|
|
self::assertSame($user, (new RegistrationLoginGate())->blockUnconfirmed($user));
|
|
}
|
|
|
|
public function testPassesThroughAnEarlierError(): void
|
|
{
|
|
$error = new \WP_Error('some_earlier_error', 'nope');
|
|
|
|
self::assertSame($error, (new RegistrationLoginGate())->blockUnconfirmed($error));
|
|
}
|
|
|
|
public function testWithholdsBookingCapWhileAwaitingApproval(): void
|
|
{
|
|
$this->stubMeta('1', '1');
|
|
|
|
$allcaps = [ RoleManager::CAP_BOOK_LESSON => true, 'read' => true ];
|
|
|
|
$result = (new RegistrationLoginGate())
|
|
->withholdBookingWhilePending($allcaps, [], [], $this->user(7));
|
|
|
|
self::assertArrayNotHasKey(RoleManager::CAP_BOOK_LESSON, $result);
|
|
self::assertTrue($result['read']);
|
|
}
|
|
|
|
public function testKeepsBookingCapForApprovedStudent(): void
|
|
{
|
|
$this->stubMeta('', '1');
|
|
|
|
$allcaps = [ RoleManager::CAP_BOOK_LESSON => true ];
|
|
|
|
$result = (new RegistrationLoginGate())
|
|
->withholdBookingWhilePending($allcaps, [], [], $this->user(7));
|
|
|
|
self::assertTrue($result[RoleManager::CAP_BOOK_LESSON]);
|
|
}
|
|
|
|
public function testLeavesCapsUntouchedForNonUserArg(): void
|
|
{
|
|
$allcaps = [ RoleManager::CAP_BOOK_LESSON => true ];
|
|
|
|
$result = (new RegistrationLoginGate())
|
|
->withholdBookingWhilePending($allcaps, [], [], null);
|
|
|
|
self::assertSame($allcaps, $result);
|
|
}
|
|
|
|
/**
|
|
* Describe a user to the `user_register` guard: which role they hold, and
|
|
* whether whoever created them is studio staff.
|
|
*/
|
|
private function stubSignup(string $role, bool $byStaff): void
|
|
{
|
|
$user = Mockery::mock(\WP_User::class);
|
|
$user->roles = [$role];
|
|
|
|
Functions\when('get_userdata')->justReturn($user);
|
|
Functions\when('current_user_can')->justReturn($byStaff);
|
|
}
|
|
|
|
public function testStudentCreatedByAnUnknownSignupFormIsHeldForApproval(): void
|
|
{
|
|
// Open registration switches the site's own users_can_register on and
|
|
// makes Student the default role, so any other signup form on the site
|
|
// now mints students. They must not arrive able to book.
|
|
$this->stubSignup(RoleManager::STUDENT, byStaff: false);
|
|
|
|
Functions\expect('update_user_meta')->once()
|
|
->with(7, RegistrationStatus::META_AWAITING_APPROVAL, '1');
|
|
// Counted as confirmed: nobody asked them to confirm, and there is no
|
|
// token for them to answer with, so blocking the login would strand them.
|
|
Functions\expect('update_user_meta')->once()
|
|
->with(7, RegistrationStatus::META_EMAIL_CONFIRMED, '1');
|
|
|
|
(new RegistrationLoginGate())->holdUnknownSignup(7);
|
|
}
|
|
|
|
public function testStudentCreatedByStaffIsLeftActive(): void
|
|
{
|
|
// An administrator adding a student from wp-admin could have approved
|
|
// them in the next click; making them do so is ceremony.
|
|
$this->stubSignup(RoleManager::STUDENT, byStaff: true);
|
|
|
|
Functions\expect('update_user_meta')->never();
|
|
|
|
(new RegistrationLoginGate())->holdUnknownSignup(7);
|
|
}
|
|
|
|
public function testNonStudentSignupIsNotHeld(): void
|
|
{
|
|
$this->stubSignup(RoleManager::INSTRUCTOR, byStaff: false);
|
|
|
|
Functions\expect('update_user_meta')->never();
|
|
|
|
(new RegistrationLoginGate())->holdUnknownSignup(7);
|
|
}
|
|
|
|
public function testUnknownUserIdIsIgnored(): void
|
|
{
|
|
Functions\when('get_userdata')->justReturn(false);
|
|
Functions\expect('update_user_meta')->never();
|
|
|
|
(new RegistrationLoginGate())->holdUnknownSignup(0);
|
|
}
|
|
}
|