Repository navigation
Expand file tree
/
Copy pathrender-post-template-test.php
More file actions
219 lines (184 loc) · 7.45 KB
/
Copy pathrender-post-template-test.php
File metadata and controls
219 lines (184 loc) · 7.45 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
/**
* Tests for the Post Template block rendering.
*
* @package WordPress
* @subpackage Blocks
* @since 6.0.0
*
* @group blocks
*/
class Tests_Blocks_RenderPostTemplateBlock extends WP_UnitTestCase {
private static $post;
private static $other_post;
public function set_up() {
parent::set_up();
self::$post = self::factory()->post->create_and_get(
array(
'post_type' => 'post',
'post_status' => 'publish',
'post_name' => 'metaldog',
'post_title' => 'Metal Dog',
'post_content' => 'Metal Dog content',
'post_excerpt' => 'Metal Dog',
)
);
self::$other_post = self::factory()->post->create_and_get(
array(
'post_type' => 'post',
'post_status' => 'publish',
'post_name' => 'ceilingcat',
'post_title' => 'Ceiling Cat',
'post_content' => 'Ceiling Cat content',
'post_excerpt' => 'Ceiling Cat',
)
);
}
public function test_rendering_post_template() {
$parsed_blocks = parse_blocks(
'<!-- wp:post-template --><!-- wp:post-title /--><!-- wp:post-excerpt /--><!-- /wp:post-template -->'
);
$block = new WP_Block( $parsed_blocks[0] );
$markup = $block->render();
$post_id = self::$post->ID;
$other_post_id = self::$other_post->ID;
$expected = <<<END
<ul class="wp-block-post-template is-layout-flow wp-block-post-template-is-layout-flow">
<li class="wp-block-post post-$other_post_id post type-post status-publish format-standard hentry category-uncategorized">
<h2 class="wp-block-post-title">Ceiling Cat</h2>
<div class="wp-block-post-excerpt">
<p class="wp-block-post-excerpt__excerpt">Ceiling Cat</p>
</div>
</li>
<li class="wp-block-post post-$post_id post type-post status-publish format-standard hentry category-uncategorized">
<h2 class="wp-block-post-title">Metal Dog</h2>
<div class="wp-block-post-excerpt">
<p class="wp-block-post-excerpt__excerpt">Metal Dog</p>
</div>
</li>
</ul>
END;
$this->assertSame(
str_replace( array( "\n", "\t" ), '', $expected ),
str_replace( array( "\n", "\t" ), '', $markup )
);
}
/**
* Tests that the `core/post-template` block triggers the main query loop when rendering within a corresponding
* `core/query` block.
*/
public function test_rendering_post_template_with_main_query_loop() {
global $wp_query, $wp_the_query;
// Query block with post template block.
$content = '<!-- wp:query {"query":{"inherit":true}} -->';
$content .= '<!-- wp:post-template {"align":"wide"} -->';
$content .= '<!-- wp:post-title /--><!-- wp:test/in-the-loop-logger /-->';
$content .= '<!-- /wp:post-template -->';
$content .= '<!-- /wp:query -->';
$expected = '<ul class="alignwide wp-block-post-template is-layout-flow wp-block-post-template-is-layout-flow wp-block-query-is-layout-flow">';
$expected .= '<li class="wp-block-post post-' . self::$post->ID . ' post type-post status-publish format-standard hentry category-uncategorized">';
$expected .= '<h2 class="wp-block-post-title">' . self::$post->post_title . '</h2>';
$expected .= '</li>';
$expected .= '</ul>';
// Set main query to single post.
$wp_query = new WP_Query( array( 'p' => self::$post->ID ) );
$wp_the_query = $wp_query;
// Register test block to log `in_the_loop()` results.
$in_the_loop_logs = array();
register_block_type(
'test/in-the-loop-logger',
array(
'render_callback' => static function () use ( &$in_the_loop_logs ) {
$in_the_loop_logs[] = in_the_loop();
return '';
},
)
);
$output = do_blocks( $content );
$this->assertSame( $expected, $output, 'Unexpected parsed blocks content' );
$this->assertSame( array( true ), $in_the_loop_logs, 'Unexpected in_the_loop() result' );
}
/**
* Tests that the `core/post-template` block does not tamper with the main query loop when rendering within a single post
* as the main query loop has already been started. In this case, the main query object needs to be cloned to
* prevent an infinite loop.
* Also tests that the default query returns posts of the 'post' post type when in a single post of any post type.
*/
public function test_rendering_post_template_with_main_query_loop_already_started() {
global $wp_query, $wp_the_query;
// Query block with post template block.
$content = '<!-- wp:query {"query":{"inherit":false}} -->';
$content .= '<!-- wp:post-template {"align":"wide"} -->';
$content .= '<!-- wp:post-title /-->';
$content .= '<!-- /wp:post-template -->';
$content .= '<!-- /wp:query -->';
$expected = '<ul class="alignwide wp-block-post-template is-layout-flow wp-block-post-template-is-layout-flow wp-block-query-is-layout-flow">';
// Find all the posts of the 'post' post type.
$wp_query_posts = new WP_Query( array( 'post_type' => 'post' ) );
while ( $wp_query_posts->have_posts() ) {
$wp_query_posts->the_post();
$expected .= '<li class="wp-block-post post-' . get_the_ID() . ' post type-post status-publish format-standard hentry category-uncategorized">';
$expected .= '<h2 class="wp-block-post-title">' . get_the_title() . '</h2>';
$expected .= '</li>';
}
$expected .= '</ul>';
// Update the post's content to have a query block for the same query as the main query.
wp_update_post(
array(
'ID' => self::$post->ID,
'post_content' => $content,
'post_content_filtered' => $content,
)
);
// Set main query to single post.
$wp_query = new WP_Query( array( 'p' => self::$post->ID ) );
$wp_the_query = $wp_query;
// Get post content within main query loop.
$output = '';
while ( $wp_query->have_posts() ) {
$wp_query->the_post();
$output = get_echo( 'the_content' );
}
$this->assertSame( $expected, $output, 'Unexpected parsed post content' );
}
/**
* Tests that the `core/post-template` block rewinds the default query when not in a single post of any post type.
*/
public function test_rendering_post_template_with_main_query_loop_not_single_post() {
global $wp_query, $wp_the_query;
// Query block with post template block.
$content = '<!-- wp:query {"query":{"inherit":true}} -->';
$content .= '<!-- wp:post-template {"align":"wide"} -->';
$content .= '<!-- wp:post-title /-->';
$content .= '<!-- /wp:post-template -->';
$content .= '<!-- /wp:query -->';
$expected = '<ul class="alignwide wp-block-post-template is-layout-flow wp-block-post-template-is-layout-flow wp-block-query-is-layout-flow">';
// Find all the posts of the 'post' post type.
$wp_query_posts = new WP_Query( array( 'post_type' => 'post' ) );
while ( $wp_query_posts->have_posts() ) {
$wp_query_posts->the_post();
$expected .= '<li class="wp-block-post post-' . get_the_ID() . ' post type-post status-publish format-standard hentry category-uncategorized">';
$expected .= '<h2 class="wp-block-post-title">' . get_the_title() . '</h2>';
$expected .= '</li>';
}
$expected .= '</ul>';
// Update the post's content to have a query block for the same query as the main query.
wp_update_post(
array(
'ID' => self::$post->ID,
'post_content' => $content,
'post_content_filtered' => $content,
)
);
// Set main query to all posts.
$wp_query = new WP_Query( array( 'post_type' => 'post' ) );
$wp_the_query = $wp_query;
// Get post content within main query loop.
$output = '';
while ( $wp_query->have_posts() ) {
$wp_query->the_post();
$output = get_echo( 'the_content' );
}
$this->assertSame( $expected, $output, 'Unexpected parsed post content' );
}
}