Skip to content

Commit 7c9240f

Browse files
committed
Add basic test coverage
1 parent ea9dd16 commit 7c9240f

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
/**
3+
* Tests for the apply_block_hooks_to_content_from_post_object function.
4+
*
5+
* @package WordPress
6+
* @subpackage Blocks
7+
*
8+
* @since 6.8.0
9+
*
10+
* @group blocks
11+
* @group block-hooks
12+
*
13+
* @covers ::apply_block_hooks_to_content_from_post_object
14+
*/
15+
class Tests_Blocks_ApplyBlockHooksToContentFromPostObject extends WP_UnitTestCase {
16+
/**
17+
* Post object.
18+
*
19+
* @var WP_Post
20+
*/
21+
protected static $post;
22+
23+
/**
24+
* Post object.
25+
*
26+
* @var WP_Post
27+
*/
28+
protected static $post_with_ignored_hooked_block;
29+
30+
/**
31+
*
32+
* Set up.
33+
*
34+
* @ticket 62716
35+
*/
36+
public static function wpSetUpBeforeClass() {
37+
self::$post = self::factory()->post->create_and_get(
38+
array(
39+
'post_type' => 'post',
40+
'post_status' => 'publish',
41+
'post_title' => 'Test Post',
42+
'post_content' => '<!-- wp:heading {"level":1} --><h1>Hello World!</h1><!-- /wp:heading -->',
43+
)
44+
);
45+
46+
self::$post_with_ignored_hooked_block = self::factory()->post->create_and_get(
47+
array(
48+
'post_type' => 'post',
49+
'post_status' => 'publish',
50+
'post_title' => 'Test Post',
51+
'post_content' => '<!-- wp:heading {"level":1} --><h1>Hello World!</h1><!-- /wp:heading -->',
52+
'meta_input' => array(
53+
'_wp_ignored_hooked_blocks' => '["tests/hooked-block-first-child"]',
54+
),
55+
)
56+
);
57+
58+
register_block_type(
59+
'tests/hooked-block',
60+
array(
61+
'block_hooks' => array(
62+
'core/heading' => 'after',
63+
),
64+
)
65+
);
66+
67+
register_block_type(
68+
'tests/hooked-block-first-child',
69+
array(
70+
'block_hooks' => array(
71+
'core/post-content' => 'first_child',
72+
),
73+
)
74+
);
75+
}
76+
77+
/**
78+
* Tear down.
79+
*
80+
* @ticket 62716
81+
*/
82+
public static function wpTearDownAfterClass() {
83+
$registry = WP_Block_Type_Registry::get_instance();
84+
85+
$registry->unregister( 'tests/hooked-block' );
86+
$registry->unregister( 'tests/hooked-block-first-child' );
87+
}
88+
89+
/**
90+
* @ticket 62716
91+
*/
92+
public function test_apply_block_hooks_to_content_from_post_object_inserts_hooked_block() {
93+
$expected = '<!-- wp:tests/hooked-block-first-child /-->' .
94+
self::$post->post_content .
95+
'<!-- wp:tests/hooked-block /-->';
96+
$actual = apply_block_hooks_to_content_from_post_object(
97+
self::$post->post_content,
98+
self::$post,
99+
'insert_hooked_blocks'
100+
);
101+
$this->assertSame( $expected, $actual );
102+
}
103+
104+
/**
105+
* @ticket 62716
106+
*/
107+
public function test_apply_block_hooks_to_content_from_post_object_respects_ignored_hooked_blocks_post_meta() {
108+
$expected = self::$post_with_ignored_hooked_block->post_content . '<!-- wp:tests/hooked-block /-->';
109+
$actual = apply_block_hooks_to_content_from_post_object(
110+
self::$post_with_ignored_hooked_block->post_content,
111+
self::$post_with_ignored_hooked_block,
112+
'insert_hooked_blocks'
113+
);
114+
$this->assertSame( $expected, $actual );
115+
}
116+
}

0 commit comments

Comments
 (0)