Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
94 lines
3.7 KiB
PHP
94 lines
3.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace LIT\MCP\Tests\Unit\Integrations\Divi\Release;
|
|
|
|
use Brain\Monkey\Functions;
|
|
use LIT\MCP\Integrations\Divi\Release\CheckReport;
|
|
use LIT\MCP\Integrations\Divi\Release\CheckReportStore;
|
|
use LIT\MCP\Integrations\Divi\Release\CompatibilityCheck;
|
|
use LIT\MCP\Integrations\Divi\Release\DiviCheckRoute;
|
|
use LIT\MCP\Tests\Unit\TestCase;
|
|
use Mockery;
|
|
|
|
final class DiviCheckRouteTest extends TestCase
|
|
{
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
Functions\when('admin_url')->alias(static fn (string $path = ''): string => 'https://k.de/wp-admin/' . $path);
|
|
Functions\when('add_query_arg')->alias(
|
|
static fn (array $args, string $url): string => $url . '?' . http_build_query($args)
|
|
);
|
|
Functions\when('get_current_user_id')->justReturn(1);
|
|
}
|
|
|
|
public function testRegistersAPostRouteForAdministratorsOnly(): void
|
|
{
|
|
Functions\expect('register_rest_route')->once()->with(
|
|
'lit-mcp/v1',
|
|
'/divi-check',
|
|
Mockery::on(static function (array $args): bool {
|
|
Functions\when('current_user_can')->alias(static fn (string $cap): bool => $cap === 'manage_options');
|
|
$allowed = ($args['permission_callback'])();
|
|
Functions\when('current_user_can')->justReturn(false);
|
|
|
|
return $args['methods'] === 'POST' && $allowed === true && ($args['permission_callback'])() === false;
|
|
})
|
|
);
|
|
|
|
(new DiviCheckRoute(static fn () => null))->registerRoute();
|
|
}
|
|
|
|
public function testRunsTheCheckStoresTheReportAndRedirectsToTheTab(): void
|
|
{
|
|
$report = new CheckReport('r1', 'd', 1, '5.3.0', '0.6.0', 'k', []);
|
|
$check = Mockery::mock(CompatibilityCheck::class);
|
|
$check->expects('run')->with(1)->andReturn($report);
|
|
$reports = Mockery::mock(CheckReportStore::class);
|
|
$reports->expects('save')->with($report);
|
|
Functions\expect('add_option')
|
|
->once()
|
|
->with(DiviCheckRoute::LOCK, Mockery::type('int'), '', false)
|
|
->andReturn(true);
|
|
Functions\expect('delete_option')->once()->with(DiviCheckRoute::LOCK);
|
|
|
|
$response = (new DiviCheckRoute(static fn () => $check, $reports))->handle();
|
|
|
|
self::assertSame(303, $response->get_status());
|
|
self::assertStringContainsString('tab=divi', $response->get_headers()['Location']);
|
|
self::assertStringContainsString('lit_mcp_notice=divi_checked', $response->get_headers()['Location']);
|
|
}
|
|
|
|
public function testASecondCheckWaitsForTheFirst(): void
|
|
{
|
|
Functions\when('add_option')->justReturn(false);
|
|
Functions\when('get_option')->alias(static fn (): int => time());
|
|
Functions\expect('delete_option')->never();
|
|
$reports = Mockery::mock(CheckReportStore::class);
|
|
$reports->expects('save')->never();
|
|
|
|
$response = (new DiviCheckRoute(static fn () => self::fail('no second check'), $reports))->handle();
|
|
|
|
self::assertStringContainsString('lit_mcp_notice=divi_check_running', $response->get_headers()['Location']);
|
|
}
|
|
|
|
public function testAStaleLockIsTakenOver(): void
|
|
{
|
|
Functions\when('add_option')->alias(static function (): bool {
|
|
static $calls = 0;
|
|
|
|
return ++$calls > 1;
|
|
});
|
|
Functions\when('get_option')->justReturn(1);
|
|
Functions\expect('delete_option')->twice()->with(DiviCheckRoute::LOCK);
|
|
$check = Mockery::mock(CompatibilityCheck::class);
|
|
$check->expects('run')->andReturn(new CheckReport('r1', 'd', 1, '5.3.0', '0.6.0', 'k', []));
|
|
$reports = Mockery::mock(CheckReportStore::class);
|
|
$reports->expects('save');
|
|
|
|
(new DiviCheckRoute(static fn () => $check, $reports))->handle();
|
|
}
|
|
}
|