Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01At3V6SjwbMe6eMzVGW3Hs7
136 lines
5.6 KiB
PHP
136 lines
5.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace LIT\MCP\Tests\Unit\Integrations\WordPress\Settings;
|
|
|
|
use Brain\Monkey\Functions;
|
|
use LIT\MCP\ChangeLog\ChangeLog;
|
|
use LIT\MCP\Integrations\WordPress\Settings\SettingsWhitelist;
|
|
use LIT\MCP\Integrations\WordPress\Settings\WpSettingsGetAbility;
|
|
use LIT\MCP\Integrations\WordPress\Settings\WpSettingsUpdateAbility;
|
|
use LIT\MCP\Tests\Unit\TestCase;
|
|
use Mockery;
|
|
|
|
final class WpSettingsAbilitiesTest extends TestCase
|
|
{
|
|
protected function tearDown(): void
|
|
{
|
|
unset($GLOBALS['wp_rewrite']);
|
|
parent::tearDown();
|
|
}
|
|
|
|
public function testGetFiltersByGroup(): void
|
|
{
|
|
Functions\when('get_option')->justReturn('Kunde');
|
|
|
|
$result = (new WpSettingsGetAbility(new SettingsWhitelist()))->execute(['group' => 'discussion']);
|
|
|
|
self::assertSame(['default_comment_status', 'default_ping_status'], array_column($result['settings'], 'key'));
|
|
self::assertSame('discussion', $result['settings'][0]['group']);
|
|
}
|
|
|
|
public function testUpdateIsAllOrNothing(): void
|
|
{
|
|
Functions\expect('update_option')->never();
|
|
|
|
$result = (new WpSettingsUpdateAbility(new SettingsWhitelist(), Mockery::mock(ChangeLog::class)))
|
|
->execute(['settings' => ['blogname' => 'Neu', 'admin_email' => 'x@y.de']]);
|
|
|
|
self::assertSame('lit_validation_failed', $result->get_error_code());
|
|
self::assertArrayHasKey('admin_email', $result->get_error_data()['fields']);
|
|
}
|
|
|
|
public function testUpdatesOnlyChangedValuesAndFlushesPermalinks(): void
|
|
{
|
|
Functions\when('get_option')->alias(static fn (string $key) => [
|
|
'blogname' => 'Alt',
|
|
'blogdescription' => 'Gleich',
|
|
'permalink_structure' => '',
|
|
][$key]);
|
|
Functions\expect('update_option')->once()->with('blogname', 'Neu');
|
|
$rewrite = Mockery::mock();
|
|
$rewrite->expects('set_permalink_structure')->with('/%postname%/');
|
|
$GLOBALS['wp_rewrite'] = $rewrite;
|
|
Functions\expect('flush_rewrite_rules')->once();
|
|
$log = Mockery::mock(ChangeLog::class);
|
|
$log->expects('record')->with('lit/wp-settings-update', 'option', 'blogname,permalink_structure', Mockery::type('string'))->andReturn(30);
|
|
|
|
$result = (new WpSettingsUpdateAbility(new SettingsWhitelist(), $log))->execute(['settings' => [
|
|
'blogname' => 'Neu',
|
|
'blogdescription' => 'Gleich',
|
|
'permalink_structure' => '/%postname%/',
|
|
]]);
|
|
|
|
self::assertSame(['blogname', 'permalink_structure'], array_column($result['changed'], 'id'));
|
|
self::assertSame(['blogname' => 'Alt', 'blogdescription' => 'Gleich', 'permalink_structure' => ''], $result['previous']);
|
|
self::assertSame(30, $result['log_id']);
|
|
}
|
|
|
|
public function testRestoresPreviousValuesWhenAWriteFailsPartway(): void
|
|
{
|
|
Functions\when('get_option')->alias(static fn (string $key) => [
|
|
'blogname' => 'Alt',
|
|
'blogdescription' => 'Vorher',
|
|
][$key]);
|
|
Functions\expect('update_option')->once()->with('blogname', 'Neu')->andReturn(true);
|
|
Functions\expect('update_option')->once()->with('blogdescription', 'Auch neu')->andReturn(false);
|
|
Functions\expect('update_option')->once()->with('blogname', 'Alt')->andReturn(true);
|
|
$log = Mockery::mock(ChangeLog::class);
|
|
$log->shouldNotReceive('record');
|
|
$file = (string) tempnam(sys_get_temp_dir(), 'lit-mcp-log');
|
|
$previous = ini_set('error_log', $file);
|
|
|
|
try {
|
|
$result = (new WpSettingsUpdateAbility(new SettingsWhitelist(), $log))->execute(['settings' => [
|
|
'blogname' => 'Neu',
|
|
'blogdescription' => 'Auch neu',
|
|
]]);
|
|
} finally {
|
|
ini_set('error_log', (string) $previous);
|
|
unlink($file);
|
|
}
|
|
|
|
self::assertSame('lit_write_failed', $result->get_error_code());
|
|
}
|
|
|
|
public function testReportsPartialWriteWhenRestoringAlsoFails(): void
|
|
{
|
|
Functions\when('get_option')->alias(static fn (string $key) => [
|
|
'blogname' => 'Alt',
|
|
'blogdescription' => 'Vorher',
|
|
][$key]);
|
|
Functions\expect('update_option')->once()->with('blogname', 'Neu')->andReturn(true);
|
|
Functions\expect('update_option')->once()->with('blogdescription', 'Auch neu')->andReturn(false);
|
|
Functions\expect('update_option')->once()->with('blogname', 'Alt')->andReturn(false);
|
|
$log = Mockery::mock(ChangeLog::class);
|
|
$log->shouldNotReceive('record');
|
|
$file = (string) tempnam(sys_get_temp_dir(), 'lit-mcp-log');
|
|
$previous = ini_set('error_log', $file);
|
|
|
|
try {
|
|
$result = (new WpSettingsUpdateAbility(new SettingsWhitelist(), $log))->execute(['settings' => [
|
|
'blogname' => 'Neu',
|
|
'blogdescription' => 'Auch neu',
|
|
]]);
|
|
} finally {
|
|
ini_set('error_log', (string) $previous);
|
|
unlink($file);
|
|
}
|
|
|
|
self::assertSame('lit_partial_write', $result->get_error_code());
|
|
self::assertStringContainsString('blogname', $result->get_error_message());
|
|
}
|
|
|
|
public function testSettingsToolsNeedManageOptions(): void
|
|
{
|
|
Functions\when('current_user_can')->alias(static fn (string $cap): bool => $cap !== 'manage_options');
|
|
|
|
self::assertSame('lit_forbidden', (new WpSettingsGetAbility(new SettingsWhitelist()))->permission([])->get_error_code());
|
|
self::assertSame(
|
|
'lit_forbidden',
|
|
(new WpSettingsUpdateAbility(new SettingsWhitelist(), Mockery::mock(ChangeLog::class)))->permission([])->get_error_code()
|
|
);
|
|
}
|
|
}
|