Files
unsupervised-scheduler/tests/Unit/Booking/BookingPageTest.php
T
thatguygriffandClaude Fable 5 9d89bc6d0e
CI / Coding Standards (pull_request) Successful in 51s
CI / No Debug Code (pull_request) Successful in 3s
CI / Tests (PHP 8.2) (pull_request) Successful in 50s
CI / Tests (PHP 8.3) (pull_request) Successful in 1m2s
CI / Build Plugin Zip (pull_request) Has been skipped
CI / Tests (PHP 8.1) (pull_request) Successful in 53s
CI / PHPStan (pull_request) Successful in 1m24s
Add link-target and auto-redirect options to booking/login blocks
The booking block gains a loginPageId attribute choosing which page its
logged-out "log in to book a lesson" link points to (default remains the
WordPress login screen), and the student-login block gains a
bookingPageId attribute controlling the logged-in "View available
lessons" link and the post-login redirect target (default remains the
current page). Both blocks also gain an autoRedirect toggle, off by
default, that sends the visitor straight to the target page; block
rendering starts after output, so the redirect runs on
template_redirect by parsing the queried page's content for the block,
with a self-target guard against redirect loops. The link targets are
also available to the shortcodes as login_page_id/booking_page_id.

Also fixes a pre-existing fatal: WordPress passes an empty string (not
an array) to shortcode callbacks when a shortcode is used without
attributes, so bare [us_booking] etc. threw a TypeError against the
strictly-typed render(array $atts) methods. ShortcodeRegistrar now
wraps each callback to normalize non-array attribute values.

Closes #51

Co-Authored-By: Claude Fable 5 <[email protected]>
2026-07-05 16:16:52 -03:00

76 lines
2.6 KiB
PHP

<?php
declare(strict_types=1);
namespace Unsupervised\Schedular\Tests\Unit\Booking;
use Brain\Monkey\Functions;
use Unsupervised\Schedular\Booking\BookingPage;
use Unsupervised\Schedular\Tests\Unit\TestCase;
class BookingPageTest extends TestCase
{
private BookingPage $page;
protected function setUp(): void
{
parent::setUp();
$this->page = new BookingPage();
}
public function testLoggedOutVisitorIsLinkedToTheWordPressLoginByDefault(): void
{
Functions\when('is_user_logged_in')->justReturn(false);
Functions\when('get_permalink')->justReturn('https://example.com/book/');
Functions\when('wp_login_url')->alias(
static fn(string $redirect): string => 'https://example.com/wp-login.php?redirect_to=' . $redirect
);
$html = $this->page->render([]);
self::assertStringContainsString(
'href="https://example.com/wp-login.php?redirect_to=https://example.com/book/"',
$html
);
self::assertStringContainsString('log in to book a lesson', $html);
}
public function testLoggedOutVisitorIsLinkedToTheChosenLoginPage(): void
{
Functions\when('is_user_logged_in')->justReturn(false);
Functions\when('get_permalink')->alias(
static fn(int $id = 0): string|false => 5 === $id ? 'https://example.com/login/' : false
);
Functions\expect('wp_login_url')->never();
$html = $this->page->render(['loginPageId' => 5]);
self::assertStringContainsString('href="https://example.com/login/"', $html);
}
public function testShortcodeStyleAttributeSelectsTheLoginPageToo(): void
{
Functions\when('is_user_logged_in')->justReturn(false);
Functions\when('get_permalink')->alias(
static fn(int $id = 0): string|false => 5 === $id ? 'https://example.com/login/' : false
);
$html = $this->page->render(['login_page_id' => '5']);
self::assertStringContainsString('href="https://example.com/login/"', $html);
}
public function testLoginUrlFallsBackToWordPressLoginWhenThePageIsGone(): void
{
// The chosen page was deleted: get_permalink() returns false for it
// and for the current (test) context alike.
Functions\when('get_permalink')->justReturn(false);
Functions\when('wp_login_url')->alias(
static fn(string $redirect): string => '' === $redirect
? 'https://example.com/wp-login.php'
: 'https://example.com/wp-login.php?redirect_to=' . $redirect
);
self::assertSame('https://example.com/wp-login.php', $this->page->loginUrl(5));
}
}