Files
unsupervised-scheduler/tests/Unit/ShortcodeRegistrarTest.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

100 lines
3.8 KiB
PHP

<?php
declare(strict_types=1);
namespace Unsupervised\Schedular\Tests\Unit;
use Brain\Monkey\Actions;
use Brain\Monkey\Functions;
use Mockery;
use Unsupervised\Schedular\Auth\LoginPage;
use Unsupervised\Schedular\Auth\RegistrationPage;
use Unsupervised\Schedular\Booking\BookingPage;
use Unsupervised\Schedular\GroupClass\GroupClassPage;
use Unsupervised\Schedular\ShortcodeRegistrar;
class ShortcodeRegistrarTest extends TestCase
{
private BookingPage&Mockery\MockInterface $bookingPage;
private LoginPage&Mockery\MockInterface $loginPage;
private RegistrationPage&Mockery\MockInterface $registrationPage;
private GroupClassPage&Mockery\MockInterface $groupClassPage;
private ShortcodeRegistrar $registrar;
/** @var array<string, callable> */
private array $shortcodes = [];
protected function setUp(): void
{
parent::setUp();
$this->bookingPage = Mockery::mock(BookingPage::class);
$this->loginPage = Mockery::mock(LoginPage::class);
$this->registrationPage = Mockery::mock(RegistrationPage::class);
$this->groupClassPage = Mockery::mock(GroupClassPage::class);
$this->registrar = new ShortcodeRegistrar(
$this->bookingPage,
$this->loginPage,
$this->registrationPage,
$this->groupClassPage,
);
$shortcodes = &$this->shortcodes;
Functions\when('add_shortcode')->alias(
static function (string $tag, callable $callback) use (&$shortcodes): void {
$shortcodes[$tag] = $callback;
}
);
}
public function testRegisterAddsAllFourShortcodesAndHooks(): void
{
Actions\expectAdded('template_redirect')
->once()
->with([$this->registrationPage, 'maybeRedirectToRegistrationPage']);
Actions\expectAdded('wp_enqueue_scripts')
->once()
->with([$this->registrar, 'enqueueAssets']);
$this->registrar->register();
self::assertSame(
['us_booking', 'us_student_login', 'us_student_register', 'us_group_classes'],
array_keys($this->shortcodes)
);
}
/**
* WordPress passes an empty string (not an array) to a shortcode callback
* when the shortcode is used without attributes, e.g. `[us_booking]` —
* the wrapper must normalize it before the typed render methods.
*/
public function testBareShortcodeUsageIsNormalizedToAnEmptyAttributeArray(): void
{
$this->registrar->register();
$this->bookingPage->shouldReceive('render')->once()->with([])->andReturn('booking');
$this->loginPage->shouldReceive('render')->once()->with([])->andReturn('login');
$this->registrationPage->shouldReceive('render')->once()->with([])->andReturn('register');
$this->groupClassPage->shouldReceive('render')->once()->with([])->andReturn('group');
self::assertSame('booking', $this->shortcodes['us_booking'](''));
self::assertSame('login', $this->shortcodes['us_student_login'](''));
self::assertSame('register', $this->shortcodes['us_student_register'](''));
self::assertSame('group', $this->shortcodes['us_group_classes'](''));
}
public function testShortcodeAttributesArePassedThroughUnchanged(): void
{
$this->registrar->register();
$this->bookingPage->shouldReceive('render')
->once()->with(['login_page_id' => '5'])->andReturn('booking');
$this->loginPage->shouldReceive('render')
->once()->with(['booking_page_id' => '9'])->andReturn('login');
self::assertSame('booking', $this->shortcodes['us_booking'](['login_page_id' => '5']));
self::assertSame('login', $this->shortcodes['us_student_login'](['booking_page_id' => '9']));
}
}