fc70cde9d5
CI / No Debug Code (pull_request) Successful in 4s
CI / Tests (PHP 8.2) (pull_request) Successful in 52s
CI / Tests (PHP 8.1) (pull_request) Successful in 54s
CI / Tests (PHP 8.3) (pull_request) Successful in 1m29s
CI / Coding Standards (pull_request) Successful in 1m57s
CI / PHPStan (pull_request) Successful in 2m14s
CI / Build Plugin Zip (pull_request) Has been skipped
Wrap the four shortcodes (us_booking, us_student_login, us_student_register, us_group_classes) in dynamic blocks so pages can be previewed and styled in the block editor. Front-end rendering delegates to the same page objects the shortcodes use; in the editor's block-renderer REST preview a static, script-free BlockPreview is rendered instead (no live REST calls, redirects, or Stripe.js). The editor script (vanilla JS, no build step) registers each block with wp.serverSideRender previews and shortcode transforms; frontend.css is attached as the block style so previews pick up theme styling. Resolves #44 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
168 lines
5.9 KiB
PHP
168 lines
5.9 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\BlockRegistrar;
|
|
use Unsupervised\Schedular\Booking\BookingPage;
|
|
use Unsupervised\Schedular\GroupClass\GroupClassPage;
|
|
|
|
/**
|
|
* Test double exposing editor-preview mode as a switch, since the real
|
|
* detection relies on the REST_REQUEST constant which cannot be toggled
|
|
* within a single PHP process.
|
|
*/
|
|
class TestableBlockRegistrar extends BlockRegistrar
|
|
{
|
|
public bool $preview = false;
|
|
|
|
protected function isEditorPreview(): bool
|
|
{
|
|
return $this->preview;
|
|
}
|
|
}
|
|
|
|
class BlockRegistrarTest extends TestCase
|
|
{
|
|
private BookingPage&Mockery\MockInterface $bookingPage;
|
|
private LoginPage&Mockery\MockInterface $loginPage;
|
|
private RegistrationPage&Mockery\MockInterface $registrationPage;
|
|
private GroupClassPage&Mockery\MockInterface $groupClassPage;
|
|
private TestableBlockRegistrar $registrar;
|
|
|
|
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 TestableBlockRegistrar(
|
|
$this->bookingPage,
|
|
$this->loginPage,
|
|
$this->registrationPage,
|
|
$this->groupClassPage,
|
|
);
|
|
}
|
|
|
|
public function testRegisterHooksBlockRegistrationOntoInit(): void
|
|
{
|
|
Actions\expectAdded('init')->once()->with([$this->registrar, 'registerBlocks']);
|
|
|
|
$this->registrar->register();
|
|
}
|
|
|
|
public function testRegisterBlocksRegistersAllFourBlocksWithAssets(): void
|
|
{
|
|
Functions\expect('wp_register_script')
|
|
->once()
|
|
->with(
|
|
BlockRegistrar::SCRIPT_HANDLE,
|
|
Mockery::pattern('~assets/js/blocks\.js$~'),
|
|
Mockery::type('array'),
|
|
USC_VERSION,
|
|
true
|
|
);
|
|
Functions\when('wp_style_is')->justReturn(false);
|
|
Functions\expect('wp_register_style')
|
|
->once()
|
|
->with(
|
|
BlockRegistrar::STYLE_HANDLE,
|
|
Mockery::pattern('~assets/css/frontend\.css$~'),
|
|
[],
|
|
USC_VERSION
|
|
);
|
|
|
|
$registered = [];
|
|
Functions\when('register_block_type')->alias(
|
|
static function (string $name, array $args) use (&$registered): bool {
|
|
$registered[$name] = $args;
|
|
return true;
|
|
}
|
|
);
|
|
|
|
$this->registrar->registerBlocks();
|
|
|
|
self::assertSame(
|
|
[
|
|
'us-scheduler/booking',
|
|
'us-scheduler/student-login',
|
|
'us-scheduler/student-register',
|
|
'us-scheduler/group-classes',
|
|
],
|
|
array_keys($registered)
|
|
);
|
|
|
|
foreach ($registered as $args) {
|
|
self::assertSame(BlockRegistrar::SCRIPT_HANDLE, $args['editor_script']);
|
|
self::assertSame(BlockRegistrar::STYLE_HANDLE, $args['style']);
|
|
self::assertIsCallable($args['render_callback']);
|
|
}
|
|
}
|
|
|
|
public function testRegisterBlocksDoesNotReRegisterAnAlreadyRegisteredStyle(): void
|
|
{
|
|
Functions\when('wp_register_script')->justReturn(true);
|
|
Functions\when('wp_style_is')->justReturn(true);
|
|
Functions\expect('wp_register_style')->never();
|
|
Functions\when('register_block_type')->justReturn(true);
|
|
|
|
$this->registrar->registerBlocks();
|
|
}
|
|
|
|
public function testFrontEndRenderDelegatesToThePageObjects(): void
|
|
{
|
|
$this->registrar->preview = false;
|
|
|
|
$this->bookingPage->shouldReceive('render')->once()->with([])->andReturn('booking-html');
|
|
$this->loginPage->shouldReceive('render')->once()->with([])->andReturn('login-html');
|
|
$this->registrationPage->shouldReceive('render')->once()->with([])->andReturn('register-html');
|
|
$this->groupClassPage->shouldReceive('render')->once()->with([])->andReturn('group-html');
|
|
|
|
self::assertSame('booking-html', $this->registrar->renderBooking());
|
|
self::assertSame('login-html', $this->registrar->renderLogin());
|
|
self::assertSame('register-html', $this->registrar->renderRegistration());
|
|
self::assertSame('group-html', $this->registrar->renderGroupClasses());
|
|
}
|
|
|
|
public function testEditorPreviewRendersStaticMarkupWithoutTouchingThePages(): void
|
|
{
|
|
$this->registrar->preview = true;
|
|
|
|
Functions\when('wp_nonce_field')->justReturn('');
|
|
|
|
$this->bookingPage->shouldNotReceive('render');
|
|
$this->loginPage->shouldNotReceive('render');
|
|
$this->registrationPage->shouldNotReceive('render');
|
|
$this->groupClassPage->shouldNotReceive('render');
|
|
|
|
self::assertStringContainsString('us-booking-app', $this->registrar->renderBooking());
|
|
self::assertStringContainsString('us-login-form', $this->registrar->renderLogin());
|
|
self::assertStringContainsString('us-register-form', $this->registrar->renderRegistration());
|
|
self::assertStringContainsString('us-group-app', $this->registrar->renderGroupClasses());
|
|
}
|
|
|
|
public function testIsEditorPreviewIsFalseOutsideRestRequests(): void
|
|
{
|
|
// REST_REQUEST is undefined in the test process, so the real
|
|
// registrar must take the front-end path.
|
|
$registrar = new BlockRegistrar(
|
|
$this->bookingPage,
|
|
$this->loginPage,
|
|
$this->registrationPage,
|
|
$this->groupClassPage,
|
|
);
|
|
|
|
$this->bookingPage->shouldReceive('render')->once()->with([])->andReturn('live');
|
|
|
|
self::assertSame('live', $registrar->renderBooking());
|
|
}
|
|
}
|