Files
unsupervised-scheduler/tests/Unit/Auth/LoginPageTest.php
T
KydoimosandClaude Opus 5 e522789104
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
Fix five findings from a security assessment of the plugin
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]>
2026-09-05 11:31:12 -03:00

128 lines
4.6 KiB
PHP

<?php
declare(strict_types=1);
namespace Unsupervised\Schedular\Tests\Unit\Auth;
use Brain\Monkey\Functions;
use Unsupervised\Schedular\Auth\LoginPage;
use Unsupervised\Schedular\Tests\Unit\TestCase;
class LoginPageTest extends TestCase
{
private LoginPage $page;
protected function setUp(): void
{
parent::setUp();
$this->page = new LoginPage();
}
protected function tearDown(): void
{
unset($_POST['us_login'], $_POST['log'], $_POST['pwd']);
parent::tearDown();
}
public function testLoggedInVisitorIsLinkedToTheCurrentPageByDefault(): void
{
Functions\when('is_user_logged_in')->justReturn(true);
Functions\when('get_permalink')->alias(
static fn(int $id = 0): string|false => 0 === $id ? 'https://example.com/login/' : false
);
$html = $this->page->render([]);
self::assertStringContainsString('href="https://example.com/login/"', $html);
self::assertStringContainsString('View available lessons', $html);
}
public function testLoggedInVisitorIsLinkedToTheChosenBookingPage(): void
{
Functions\when('is_user_logged_in')->justReturn(true);
Functions\when('get_permalink')->alias(
static fn(int $id = 0): string|false => 9 === $id ? 'https://example.com/book/' : 'https://example.com/login/'
);
$html = $this->page->render(['bookingPageId' => 9]);
self::assertStringContainsString('href="https://example.com/book/"', $html);
}
public function testShortcodeStyleAttributeSelectsTheBookingPageToo(): void
{
Functions\when('is_user_logged_in')->justReturn(true);
Functions\when('get_permalink')->alias(
static fn(int $id = 0): string|false => 9 === $id ? 'https://example.com/book/' : 'https://example.com/login/'
);
$html = $this->page->render(['booking_page_id' => '9']);
self::assertStringContainsString('href="https://example.com/book/"', $html);
}
public function testLoggedOutVisitorSeesTheLoginForm(): void
{
Functions\when('is_user_logged_in')->justReturn(false);
Functions\when('get_permalink')->justReturn('https://example.com/login/');
Functions\when('sanitize_url')->returnArg();
Functions\when('wp_nonce_field')->justReturn('');
$html = $this->page->render([]);
self::assertStringContainsString('us-login-form', $html);
self::assertStringContainsString('name="us_login"', $html);
}
public function testBookingUrlIsNullWithoutAChosenPage(): void
{
self::assertNull($this->page->bookingUrl(0));
}
public function testBookingUrlIsNullWhenTheChosenPageIsGone(): void
{
Functions\when('get_permalink')->justReturn(false);
self::assertNull($this->page->bookingUrl(9));
}
public function testBookingUrlIsThePagePermalink(): void
{
Functions\when('get_permalink')->justReturn('https://example.com/book/');
self::assertSame('https://example.com/book/', $this->page->bookingUrl(9));
}
/**
* wp_signon()'s second argument decides whether the auth cookie carries the
* Secure flag, and *only* its default (the empty string) makes it work that
* out from is_ssl(). Passing an explicit false — which reads like "no
* preference" and is not — issues the plain, non-Secure cookie on an HTTPS
* site, leaving every student's session to leak over the first http://
* request to the domain. So the call must pass the credentials and nothing
* else, which is what this asserts: a second argument of any kind fails it.
*/
public function testSignOnDoesNotOverrideWordPressSecureCookieDetection(): void
{
$_POST['us_login'] = '1';
$_POST['log'] = '[email protected]';
$_POST['pwd'] = 'hunter2';
Functions\when('is_user_logged_in')->justReturn(false);
Functions\when('get_permalink')->justReturn('https://example.com/login/');
Functions\when('sanitize_url')->returnArg();
Functions\when('sanitize_user')->returnArg();
Functions\when('wp_unslash')->returnArg();
Functions\when('wp_nonce_field')->justReturn('');
Functions\when('check_admin_referer')->justReturn(true);
// The failure branch, so the render returns instead of redirecting and
// exiting the test process.
Functions\when('is_wp_error')->justReturn(true);
Functions\expect('wp_signon')->once()->with(\Mockery::type('array'))->andReturn(null);
$html = $this->page->render([]);
self::assertStringContainsString('Invalid username or password.', $html);
}
}