resetRegistry(); Registry::init(); } public function tearDown(): void { $this->resetRegistry(); delete_option('trp_settings'); unset($GLOBALS['TRP_LANGUAGE']); parent::tearDown(); } public function test_permission_callback_requires_admin_capability() { wp_set_current_user(0); $this->assertFalse(SaveController::permissionCallback()); $editor = self::factory()->user->create(['role' => 'editor']); wp_set_current_user($editor); $this->assertFalse(SaveController::permissionCallback()); $admin = self::factory()->user->create(['role' => 'administrator']); wp_set_current_user($admin); $this->assertTrue(SaveController::permissionCallback()); } public function test_missing_required_fields_returns_400() { $this->loginAsAdmin(); $res = SaveController::handleSave($this->jsonRequest([ 'source' => ['kind' => 'post', 'id' => 1], // no blockId, no blockType, no patches/rawBlock ])); $this->assertSame(400, $res->get_status()); $this->assertStringContainsString('required', $res->get_data()['error']); } public function test_unknown_block_type_with_patches_returns_400_no_schema() { $this->loginAsAdmin(); $postId = self::factory()->post->create([ 'post_content' => '
hi
', ]); $res = SaveController::handleSave($this->jsonRequest([ 'source' => ['kind' => 'post', 'id' => $postId], 'blockId' => 1, 'blockType' => 'not/a-real-block', 'patches' => [['fieldKey' => 'content', 'value' => 'x']], ])); $this->assertSame(400, $res->get_status()); $data = $res->get_data(); $this->assertSame('no schema registered for block type', $data['error']); $this->assertSame('not/a-real-block', $data['blockType']); } public function test_post_not_found_returns_404() { $this->loginAsAdmin(); $res = SaveController::handleSave($this->jsonRequest([ 'source' => ['kind' => 'post', 'id' => 99999999], 'blockId' => 1, 'blockType' => 'core/paragraph', 'patches' => [['fieldKey' => 'content', 'value' => 'x']], ])); $this->assertSame(404, $res->get_status()); } public function test_unknown_source_kind_returns_404() { $this->loginAsAdmin(); $res = SaveController::handleSave($this->jsonRequest([ 'source' => ['kind' => 'bogus'], 'blockId' => 1, 'blockType' => 'core/paragraph', 'patches' => [['fieldKey' => 'content', 'value' => 'x']], ])); $this->assertSame(404, $res->get_status()); $this->assertSame('unknown source kind', $res->get_data()['error']); } public function test_template_part_requires_partSlug() { $this->loginAsAdmin(); $res = SaveController::handleSave($this->jsonRequest([ 'source' => ['kind' => 'template-part'], 'blockId' => 1, 'blockType' => 'core/paragraph', 'patches' => [['fieldKey' => 'content', 'value' => 'x']], ])); $this->assertSame(404, $res->get_status()); $this->assertSame('template-part requires partSlug', $res->get_data()['error']); } public function test_subscriber_user_forbidden_per_source() { $sub = self::factory()->user->create(['role' => 'subscriber']); wp_set_current_user($sub); $postId = self::factory()->post->create([ 'post_content' => 'hi
', ]); $res = SaveController::handleSave($this->jsonRequest([ 'source' => ['kind' => 'post', 'id' => $postId], 'blockId' => 1, 'blockType' => 'core/paragraph', 'patches' => [['fieldKey' => 'content', 'value' => 'x']], ])); $this->assertSame(403, $res->get_status()); $this->assertSame('forbidden for this source', $res->get_data()['error']); } // The post-path object check is edit_post on the *specific* id, // not the coarse edit_posts. An author holds edit_posts but not // edit_others_posts, so a save against another author's post must 403 — // even with the global gate lowered enough to let the author in. Mutating // userCanEditSource to current_user_can('edit_posts') reopens this. public function test_post_save_forbidden_when_editing_another_authors_post() { $owner = self::factory()->user->create(['role' => 'author']); $postId = self::factory()->post->create([ 'post_author' => $owner, 'post_content' => 'hi
', ]); $attacker = self::factory()->user->create(['role' => 'author']); wp_set_current_user($attacker); $res = SaveController::handleSave($this->jsonRequest([ 'source' => ['kind' => 'post', 'id' => $postId], 'blockId' => 1, 'blockType' => 'core/paragraph', 'patches' => [['fieldKey' => 'content', 'value' => 'tampered']], ])); $this->assertSame(403, $res->get_status()); $this->assertSame('forbidden for this source', $res->get_data()['error']); $this->assertStringContainsString('hi
', get_post($postId)->post_content); } // Template parts gate on edit_theme_options, independent of the // post-edit world. An editor can edit others' posts but lacks // edit_theme_options, so a template-part save must 403. Mutating // userCanEditSource's template-part branch to edit_post/edit_posts (which // the editor passes) reopens this. public function test_template_part_save_forbidden_for_user_without_edit_theme_options() { $editor = self::factory()->user->create(['role' => 'editor']); wp_set_current_user($editor); $this->assertTrue(current_user_can('edit_others_posts')); $this->assertFalse(current_user_can('edit_theme_options')); $slug = 'qe-test-part-' . wp_generate_password(6, false); $partId = self::factory()->post->create([ 'post_type' => 'wp_template_part', 'post_name' => $slug, 'post_status' => 'publish', 'post_content' => 'part-old
', ]); wp_set_object_terms($partId, get_stylesheet(), 'wp_theme'); $res = SaveController::handleSave($this->jsonRequest([ 'source' => ['kind' => 'template-part', 'partSlug' => $slug], 'blockId' => 1, 'blockType' => 'core/paragraph', 'patches' => [['fieldKey' => 'content', 'value' => 'part-new']], ])); $this->assertSame(403, $res->get_status()); $this->assertSame('forbidden for this source', $res->get_data()['error']); $this->assertStringContainsString('part-old
', get_post($partId)->post_content); } public function test_blockId_past_end_of_walk_returns_404_with_diagnostics() { $this->loginAsAdmin(); $postId = self::factory()->post->create([ 'post_content' => 'hi
', ]); $res = SaveController::handleSave($this->jsonRequest([ 'source' => ['kind' => 'post', 'id' => $postId], 'blockId' => 99, 'blockType' => 'core/paragraph', 'patches' => [['fieldKey' => 'content', 'value' => 'x']], ])); $this->assertSame(404, $res->get_status()); $this->assertSame('block not found in source', $res->get_data()['error']); $this->assertSame(99, $res->get_data()['blockId']); } public function test_block_type_mismatch_returns_409_without_writing() { $this->loginAsAdmin(); $originalContent = 'hi
'; $postId = self::factory()->post->create(['post_content' => $originalContent]); $res = SaveController::handleSave($this->jsonRequest([ 'source' => ['kind' => 'post', 'id' => $postId], 'blockId' => 1, 'blockType' => 'core/heading', 'patches' => [['fieldKey' => 'content', 'value' => 'x']], ])); $this->assertSame(409, $res->get_status()); $data = $res->get_data(); $this->assertSame('block type mismatch', $data['error']); $this->assertSame('core/heading', $data['expected']); $this->assertSame('core/paragraph', $data['actual']); $this->assertSame($originalContent, get_post($postId)->post_content); } public function test_successful_patch_updates_post_and_returns_rendered_html() { $this->loginAsAdmin(); $postId = self::factory()->post->create([ 'post_content' => 'hi
', ]); $res = SaveController::handleSave($this->jsonRequest([ 'source' => ['kind' => 'post', 'id' => $postId], 'blockId' => 1, 'blockType' => 'core/paragraph', 'patches' => [['fieldKey' => 'content', 'value' => 'updated']], ])); $this->assertSame(200, $res->get_status()); $data = $res->get_data(); $this->assertTrue($data['ok']); $this->assertSame(1, $data['blockId']); $this->assertSame('core/paragraph', $data['blockType']); $this->assertStringContainsString('updated', $data['rendered']); $stored = get_post($postId)->post_content; $this->assertStringContainsString('updated
', $stored); $this->assertStringContainsString('', $stored); } public function test_patches_applied_in_order() { $this->loginAsAdmin(); $postId = self::factory()->post->create([ 'post_content' => 'hi
', ]); $res = SaveController::handleSave($this->jsonRequest([ 'source' => ['kind' => 'post', 'id' => $postId], 'blockId' => 1, 'blockType' => 'core/paragraph', 'patches' => [ ['fieldKey' => 'content', 'value' => 'first'], ['fieldKey' => 'content', 'value' => 'second'], ], ])); $this->assertSame(200, $res->get_status()); $this->assertStringContainsString('second
', get_post($postId)->post_content); } public function test_rawBlock_must_parse_to_exactly_one_block() { $this->loginAsAdmin(); $postId = self::factory()->post->create([ 'post_content' => 'hi
', ]); $two = 'a
' . 'b
'; $res = SaveController::handleSave($this->jsonRequest([ 'source' => ['kind' => 'post', 'id' => $postId], 'blockId' => 1, 'blockType' => 'core/paragraph', 'rawBlock' => $two, ])); $this->assertSame(400, $res->get_status()); $this->assertSame('rawBlock must parse to exactly one block', $res->get_data()['error']); $this->assertSame(2, $res->get_data()['parsed_count']); } public function test_rawBlock_type_must_match_blockType() { $this->loginAsAdmin(); $postId = self::factory()->post->create([ 'post_content' => 'hi
', ]); $res = SaveController::handleSave($this->jsonRequest([ 'source' => ['kind' => 'post', 'id' => $postId], 'blockId' => 1, 'blockType' => 'core/paragraph', 'rawBlock' => 'old
', ]); $res = SaveController::handleSave($this->jsonRequest([ 'source' => ['kind' => 'post', 'id' => $postId], 'blockId' => 1, 'blockType' => 'core/paragraph', 'rawBlock' => 'whole-block
', ])); $this->assertSame(200, $res->get_status()); $stored = get_post($postId)->post_content; $this->assertStringContainsString('whole-block
', $stored); $this->assertStringContainsString('{"align":"right"}', $stored); } public function test_rawBlock_path_works_even_when_no_schema_registered() { $this->loginAsAdmin(); // Wipe registry: rawBlock should still save. $this->resetRegistry(); $postId = self::factory()->post->create([ 'post_content' => 'old
', ]); $res = SaveController::handleSave($this->jsonRequest([ 'source' => ['kind' => 'post', 'id' => $postId], 'blockId' => 1, 'blockType' => 'core/paragraph', 'rawBlock' => 'whole
', ])); $this->assertSame(200, $res->get_status()); } public function test_round_trip_preserves_block_structure_of_unrelated_siblings() { $this->loginAsAdmin(); $content = 'first
' . 'last
'; $postId = self::factory()->post->create(['post_content' => $content]); // Edit only the middle heading (blockId=2 in TagBlocks counting). $res = SaveController::handleSave($this->jsonRequest([ 'source' => ['kind' => 'post', 'id' => $postId], 'blockId' => 2, 'blockType' => 'core/heading', 'patches' => [['fieldKey' => 'content', 'value' => 'new-middle']], ])); $this->assertSame(200, $res->get_status()); $stored = get_post($postId)->post_content; $this->assertStringContainsString('first
', $stored); $this->assertStringContainsString('last
', $stored); } public function test_template_part_resolves_by_slug() { $this->loginAsAdmin(); $slug = 'qe-test-part-' . wp_generate_password(6, false); $partId = self::factory()->post->create([ 'post_type' => 'wp_template_part', 'post_name' => $slug, 'post_status' => 'publish', 'post_content' => 'part-old
', ]); wp_set_object_terms($partId, get_stylesheet(), 'wp_theme'); $res = SaveController::handleSave($this->jsonRequest([ 'source' => ['kind' => 'template-part', 'partSlug' => $slug], 'blockId' => 1, 'blockType' => 'core/paragraph', 'patches' => [['fieldKey' => 'content', 'value' => 'part-new']], ])); $this->assertSame(200, $res->get_status()); $this->assertStringContainsString('part-new
', get_post($partId)->post_content); } // Real-world repro: an install that had `extendable` and `extendable-2` // both active at different points ends up with two wp_template_part // posts sharing post_name='header', one per wp_theme term. WP's render // path scopes to the current theme via get_block_template; raw // get_posts(name=...) doesn't, so a save-side resolver built on // get_posts patches the wrong row and the findBlock parse walk runs // against an unrelated post_content (the user's diagnostic dump: // 11 blocks visited, none of them the social-link the client clicked). // // Bypass wp_unique_post_slug with a direct $wpdb update so both posts // really share the slug — wp_insert_post auto-suffixes `-2` when terms // aren't set at insertion time, which masks this scenario in tests. public function test_template_part_duplicate_slug_resolves_via_active_theme() { global $wpdb; $this->loginAsAdmin(); $slug = sanitize_title('qe-test-dup-' . wp_generate_password(6, false)); $activeThemePartId = self::factory()->post->create([ 'post_type' => 'wp_template_part', 'post_name' => $slug, 'post_status' => 'publish', 'post_content' => 'active-old
', ]); wp_set_object_terms($activeThemePartId, get_stylesheet(), 'wp_theme'); $orphanThemePartId = self::factory()->post->create([ 'post_type' => 'wp_template_part', 'post_name' => "{$slug}-renamed-by-uniqueness", 'post_status' => 'publish', 'post_content' => 'orphan-untouched
', ]); wp_set_object_terms($orphanThemePartId, 'qe-test-other-theme', 'wp_theme'); // Bypass wp_unique_post_slug + force the orphan to win get_posts' // default date-DESC ordering, mirroring the user's diagnostic // (orphan = newer modified-row, active = older modified-row). $wpdb->update( $wpdb->posts, [ 'post_name' => $slug, 'post_date' => '2099-01-01 00:00:00', 'post_date_gmt' => '2099-01-01 00:00:00', ], ['ID' => $orphanThemePartId] ); clean_post_cache($orphanThemePartId); $res = SaveController::handleSave($this->jsonRequest([ 'source' => ['kind' => 'template-part', 'partSlug' => $slug], 'blockId' => 1, 'blockType' => 'core/paragraph', 'patches' => [['fieldKey' => 'content', 'value' => 'active-new']], ])); $this->assertSame(200, $res->get_status()); $this->assertStringContainsString('active-new
', get_post($activeThemePartId)->post_content); $this->assertStringContainsString('orphan-untouched
', get_post($orphanThemePartId)->post_content); } public function test_disallowed_post_type_revision_returns_404() { $this->loginAsAdmin(); $parentId = self::factory()->post->create(['post_status' => 'publish']); $revisionId = self::factory()->post->create([ 'post_type' => 'revision', 'post_parent' => $parentId, 'post_status' => 'inherit', 'post_content' => 'hi
', ]); $res = SaveController::handleSave($this->jsonRequest([ 'source' => ['kind' => 'post', 'id' => $revisionId], 'blockId' => 1, 'blockType' => 'core/paragraph', 'patches' => [['fieldKey' => 'content', 'value' => 'x']], ])); $this->assertSame(404, $res->get_status()); $this->assertStringContainsString('dedicated endpoint', $res->get_data()['error']); } public function test_disallowed_post_type_wp_template_returns_404() { $this->loginAsAdmin(); $templateId = self::factory()->post->create([ 'post_type' => 'wp_template', 'post_status' => 'publish', 'post_content' => 'hi
', ]); $res = SaveController::handleSave($this->jsonRequest([ 'source' => ['kind' => 'post', 'id' => $templateId], 'blockId' => 1, 'blockType' => 'core/paragraph', 'patches' => [['fieldKey' => 'content', 'value' => 'x']], ])); $this->assertSame(404, $res->get_status()); $this->assertStringContainsString('dedicated endpoint', $res->get_data()['error']); } public function test_disallowed_post_type_wp_block_returns_404() { $this->loginAsAdmin(); $reusableId = self::factory()->post->create([ 'post_type' => 'wp_block', 'post_status' => 'publish', 'post_content' => 'hi
', ]); $res = SaveController::handleSave($this->jsonRequest([ 'source' => ['kind' => 'post', 'id' => $reusableId], 'blockId' => 1, 'blockType' => 'core/paragraph', 'patches' => [['fieldKey' => 'content', 'value' => 'x']], ])); $this->assertSame(404, $res->get_status()); $this->assertStringContainsString('dedicated endpoint', $res->get_data()['error']); } public function test_auto_draft_post_via_post_kind_returns_404() { $this->loginAsAdmin(); $autoDraftId = self::factory()->post->create([ 'post_status' => 'auto-draft', 'post_content' => 'hi
', ]); $res = SaveController::handleSave($this->jsonRequest([ 'source' => ['kind' => 'post', 'id' => $autoDraftId], 'blockId' => 1, 'blockType' => 'core/paragraph', 'patches' => [['fieldKey' => 'content', 'value' => 'x']], ])); $this->assertSame(404, $res->get_status()); $this->assertStringContainsString('dedicated endpoint', $res->get_data()['error']); } public function test_rawBlock_disallowed_block_type_returns_400() { $this->loginAsAdmin(); $postId = self::factory()->post->create([ 'post_content' => 'old
', ]); $malicious = 'hello'
. '
old
', ]); $res = SaveController::handleSave($this->jsonRequest([ 'source' => ['kind' => 'post', 'id' => $postId], 'blockId' => 1, 'blockType' => 'core/paragraph', 'rawBlock' => 'hi
', ])); $this->assertSame(200, $res->get_status(), "attrs: {$attrs}"); $stored = get_post($postId)->post_content; $rendered = $res->get_data()['rendered']; $this->assertStringNotContainsString('