forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapplyBlockHooksToContent.php
More file actions
194 lines (169 loc) · 5.82 KB
/
applyBlockHooksToContent.php
File metadata and controls
194 lines (169 loc) · 5.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
/**
* Tests for the apply_block_hooks_to_content function.
*
* @package WordPress
* @subpackage Blocks
*
* @since 6.7.0
*
* @group blocks
* @group block-hooks
*
* @covers ::apply_block_hooks_to_content
*/
class Tests_Blocks_ApplyBlockHooksToContent extends WP_UnitTestCase {
/**
* Set up.
*
* @ticket 61902.
* @ticket 63287.
*/
public static function wpSetUpBeforeClass() {
register_block_type(
'tests/hooked-block',
array(
'block_hooks' => array(
'core/post-content' => 'after',
),
)
);
register_block_type(
'tests/hooked-block-with-multiple-false',
array(
'block_hooks' => array(
'tests/other-anchor-block' => 'after',
),
'supports' => array(
'multiple' => false,
),
)
);
register_block_type(
'tests/dynamically-hooked-block-with-multiple-false',
array(
'supports' => array(
'multiple' => false,
),
)
);
}
/**
* Tear down.
*
* @ticket 61902.
*/
public static function wpTearDownAfterClass() {
$registry = WP_Block_Type_Registry::get_instance();
$registry->unregister( 'tests/hooked-block' );
$registry->unregister( 'tests/hooked-block-with-multiple-false' );
$registry->unregister( 'tests/dynamically-hooked-block-with-multiple-false' );
}
/**
* @ticket 61902
*/
public function test_apply_block_hooks_to_content_sets_theme_attribute_on_template_part_block() {
$context = new WP_Block_Template();
$context->content = '<!-- wp:template-part /-->';
$actual = apply_block_hooks_to_content( $context->content, $context, 'insert_hooked_blocks' );
$this->assertSame(
sprintf( '<!-- wp:template-part {"theme":"%s"} /-->', get_stylesheet() ),
$actual
);
}
/**
* @ticket 61902
* @ticket 63287
*/
public function test_apply_block_hooks_to_content_inserts_hooked_block() {
$context = new WP_Block_Template();
$context->content = '<!-- wp:post-content /-->';
$actual = apply_block_hooks_to_content( $context->content, $context, 'insert_hooked_blocks' );
$this->assertSame(
'<!-- wp:post-content /--><!-- wp:tests/hooked-block /-->',
$actual
);
}
/**
* @ticket 61074
* @ticket 63287
*/
public function test_apply_block_hooks_to_content_with_context_set_to_null() {
$content = '<!-- wp:post-content /-->';
/*
* apply_block_hooks_to_content() will fall back to the global $post object (via get_post())
* if the $context parameter is null. However, we'd also like to ensure that the function
* works as expected even when get_post() returns null.
*/
$this->assertNull( get_post() );
$actual = apply_block_hooks_to_content( $content, null, 'insert_hooked_blocks' );
$this->assertSame(
'<!-- wp:post-content /--><!-- wp:tests/hooked-block /-->',
$actual
);
}
/**
* @ticket 61902
*/
public function test_apply_block_hooks_to_content_respect_multiple_false() {
$context = new WP_Block_Template();
$context->content = '<!-- wp:tests/hooked-block-with-multiple-false /--><!-- wp:tests/other-anchor-block /-->';
$actual = apply_block_hooks_to_content( $context->content, $context, 'insert_hooked_blocks' );
$this->assertSame(
'<!-- wp:tests/hooked-block-with-multiple-false /--><!-- wp:tests/other-anchor-block /-->',
$actual
);
}
/**
* @ticket 61902
*/
public function test_apply_block_hooks_to_content_respect_multiple_false_after_inserting_once() {
$context = new WP_Block_Template();
$context->content = '<!-- wp:tests/other-anchor-block /--><!-- wp:tests/other-block /--><!-- wp:tests/other-anchor-block /-->';
$actual = apply_block_hooks_to_content( $context->content, $context, 'insert_hooked_blocks' );
$this->assertSame(
'<!-- wp:tests/other-anchor-block /--><!-- wp:tests/hooked-block-with-multiple-false /--><!-- wp:tests/other-block /--><!-- wp:tests/other-anchor-block /-->',
$actual
);
}
/**
* @ticket 61902
*/
public function test_apply_block_hooks_to_content_respect_multiple_false_with_filter() {
$filter = function ( $hooked_block_types, $relative_position, $anchor_block_type ) {
if ( 'tests/yet-another-anchor-block' === $anchor_block_type && 'after' === $relative_position ) {
$hooked_block_types[] = 'tests/dynamically-hooked-block-with-multiple-false';
}
return $hooked_block_types;
};
$context = new WP_Block_Template();
$context->content = '<!-- wp:tests/dynamically-hooked-block-with-multiple-false /--><!-- wp:tests/yet-another-anchor-block /-->';
add_filter( 'hooked_block_types', $filter, 10, 3 );
$actual = apply_block_hooks_to_content( $context->content, $context, 'insert_hooked_blocks' );
remove_filter( 'hooked_block_types', $filter, 10 );
$this->assertSame(
'<!-- wp:tests/dynamically-hooked-block-with-multiple-false /--><!-- wp:tests/yet-another-anchor-block /-->',
$actual
);
}
/**
* @ticket 61902
*/
public function test_apply_block_hooks_to_content_respect_multiple_false_after_inserting_once_with_filter() {
$filter = function ( $hooked_block_types, $relative_position, $anchor_block_type ) {
if ( 'tests/yet-another-anchor-block' === $anchor_block_type && 'after' === $relative_position ) {
$hooked_block_types[] = 'tests/dynamically-hooked-block-with-multiple-false';
}
return $hooked_block_types;
};
$context = new WP_Block_Template();
$context->content = '<!-- wp:tests/yet-another-anchor-block /--><!-- wp:tests/other-block /--><!-- wp:tests/yet-another-anchor-block /-->';
add_filter( 'hooked_block_types', $filter, 10, 3 );
$actual = apply_block_hooks_to_content( $context->content, $context, 'insert_hooked_blocks' );
remove_filter( 'hooked_block_types', $filter, 10 );
$this->assertSame(
'<!-- wp:tests/yet-another-anchor-block /--><!-- wp:tests/dynamically-hooked-block-with-multiple-false /--><!-- wp:tests/other-block /--><!-- wp:tests/yet-another-anchor-block /-->',
$actual
);
}
}