fix: tb-create-template rolls back posts Divi inserted before failing (ruling Q19)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -8,6 +8,7 @@ use Brain\Monkey\Functions;
|
||||
use LIT\MCP\Abilities\Errors;
|
||||
use LIT\MCP\Abilities\WriteFailed;
|
||||
use LIT\MCP\Integrations\Divi\ThemeBuilder\DiviThemeBuilder;
|
||||
use LIT\MCP\Integrations\Divi\ThemeBuilder\InsertedBeforeFailure;
|
||||
use LIT\MCP\Integrations\Divi\ThemeBuilder\TemplateBuild;
|
||||
use LIT\MCP\Tests\Support\FakeChangeLog;
|
||||
use LIT\MCP\Tests\Support\ThemeBuilderFixtures;
|
||||
@@ -18,6 +19,12 @@ use WP_Post;
|
||||
|
||||
final class TemplateBuildTest extends TestCase
|
||||
{
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
Functions\when('wp_delete_object_term_relationships')->justReturn(null);
|
||||
}
|
||||
|
||||
public function testRollsBackEveryCreatedPostInReverse(): void
|
||||
{
|
||||
$store = ThemeBuilderFixtures::store();
|
||||
@@ -61,7 +68,7 @@ final class TemplateBuildTest extends TestCase
|
||||
self::fail('no WriteFailed');
|
||||
} catch (WriteFailed $e) {
|
||||
self::assertFalse($e->rolledBack);
|
||||
self::assertSame([2101], $e->details['created_ids']);
|
||||
self::assertSame([2101], $e->details['left_ids']);
|
||||
self::assertToolError($e->toError(), 'partial_write');
|
||||
}
|
||||
$log->assertPartials([
|
||||
@@ -149,4 +156,104 @@ final class TemplateBuildTest extends TestCase
|
||||
$this->expectException(RuntimeException::class);
|
||||
$build->themeBuilder();
|
||||
}
|
||||
|
||||
/** Ruling Q19: posts Divi inserted before it threw are part of the build and rolled back. */
|
||||
public function testPostsInsertedBeforeDiviThrewAreRolledBack(): void
|
||||
{
|
||||
$store = ThemeBuilderFixtures::store();
|
||||
$store->shouldReceive('insertLayout')->once()->andReturn(2101);
|
||||
$store->shouldReceive('storeTemplate')->once()->andThrow(
|
||||
new InsertedBeforeFailure('Divi called wp_die(): hook', [2100], new RuntimeException('hook'))
|
||||
);
|
||||
$deleted = [];
|
||||
Functions\when('wp_delete_post')->alias(static function (int $id) use (&$deleted): WP_Post {
|
||||
$deleted[] = $id;
|
||||
|
||||
return new WP_Post(['ID' => $id]);
|
||||
});
|
||||
$build = new TemplateBuild($store, FakeChangeLog::forbidden(), 'lit/tb-create-template');
|
||||
$build->layout('body', 'Inhalt');
|
||||
|
||||
try {
|
||||
$build->template(1064, ['title' => 'P', 'layouts' => [], 'use_on' => [], 'exclude_from' => []]);
|
||||
self::fail('no exception');
|
||||
} catch (InsertedBeforeFailure $e) {
|
||||
self::assertSame([2101, 2100], $build->created());
|
||||
try {
|
||||
$build->rollBack($e);
|
||||
} catch (WriteFailed $failed) {
|
||||
self::assertTrue($failed->rolledBack);
|
||||
}
|
||||
}
|
||||
self::assertSame([2100, 2101], $deleted);
|
||||
}
|
||||
|
||||
public function testALayoutInsertedBeforeDiviThrewIsRolledBack(): void
|
||||
{
|
||||
$store = ThemeBuilderFixtures::store();
|
||||
$store->shouldReceive('insertLayout')->once()->andThrow(
|
||||
new InsertedBeforeFailure('terms failed', [2101], new RuntimeException('terms failed'))
|
||||
);
|
||||
Functions\expect('wp_delete_post')->once()->with(2101, true)->andReturn(new WP_Post(['ID' => 2101]));
|
||||
$build = new TemplateBuild($store, FakeChangeLog::forbidden(), 'lit/tb-create-template');
|
||||
|
||||
try {
|
||||
$build->layout('body', 'Inhalt');
|
||||
self::fail('no exception');
|
||||
} catch (InsertedBeforeFailure $e) {
|
||||
$this->expectException(WriteFailed::class);
|
||||
$build->rollBack($e);
|
||||
}
|
||||
}
|
||||
|
||||
/** WordPress leaves layout_type relationships behind for TB layouts (taxonomy not registered for them). */
|
||||
public function testTheLayoutTypeTermRelationshipsGoToo(): void
|
||||
{
|
||||
$store = ThemeBuilderFixtures::store();
|
||||
$store->shouldReceive('insertLayout')->andReturn(2101);
|
||||
$calls = [];
|
||||
Functions\when('wp_delete_object_term_relationships')->alias(
|
||||
static function (int $id, string $taxonomy) use (&$calls): void {
|
||||
$calls[] = [$id, $taxonomy, 'terms'];
|
||||
}
|
||||
);
|
||||
Functions\when('wp_delete_post')->alias(static function (int $id) use (&$calls): WP_Post {
|
||||
$calls[] = [$id, 'post'];
|
||||
|
||||
return new WP_Post(['ID' => $id]);
|
||||
});
|
||||
$build = new TemplateBuild($store, FakeChangeLog::forbidden(), 'lit/tb-create-template');
|
||||
$build->layout('body', 'Inhalt');
|
||||
|
||||
try {
|
||||
$build->rollBack(new RuntimeException('later'));
|
||||
} catch (WriteFailed) {
|
||||
}
|
||||
self::assertSame([[2101, 'layout_type', 'terms'], [2101, 'post']], $calls);
|
||||
}
|
||||
|
||||
/** Ruling Q19: the partial write names only what is left, and a refusal keeps every failure. */
|
||||
public function testAPartialWriteNamesOnlyThePostsLeft(): void
|
||||
{
|
||||
$store = ThemeBuilderFixtures::store();
|
||||
$store->shouldReceive('insertLayout')->andReturn(2101, 2102, 2103);
|
||||
Functions\when('wp_delete_post')->alias(
|
||||
static fn (int $id): WP_Post|false => $id === 2101 ? new WP_Post(['ID' => $id]) : false
|
||||
);
|
||||
$log = FakeChangeLog::partialsOnly();
|
||||
$build = new TemplateBuild($store, $log, 'lit/tb-create-template');
|
||||
$build->layout('header', 'Kopf');
|
||||
$build->layout('body', 'Inhalt');
|
||||
$build->layout('footer', 'Fuß');
|
||||
|
||||
try {
|
||||
$build->refuse(Errors::invalidInput('x', ['body.nodes' => 'x']));
|
||||
self::fail('no WriteFailed');
|
||||
} catch (WriteFailed $e) {
|
||||
self::assertSame([2103, 2102], $e->details['left_ids']);
|
||||
self::assertStringContainsString('post 2103 not deleted', $e->getMessage());
|
||||
self::assertStringContainsString('post 2102 not deleted', $e->getMessage());
|
||||
}
|
||||
$log->assertPartials(['object_id' => '2103, 2102']);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user