Repository navigation
Expand file tree
/
Copy pathrenderQueryTitle.php
More file actions
372 lines (324 loc) · 8.82 KB
/
Copy pathrenderQueryTitle.php
File metadata and controls
372 lines (324 loc) · 8.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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<?php
/**
* Tests for the server-side rendering of the Query Title block.
*
* @package WordPress
* @subpackage Blocks
*
* @group blocks
*
* @covers ::render_block_core_query_title
*/
class Tests_Blocks_RenderQueryTitle extends WP_UnitTestCase {
/**
* Original block to render of the block supports.
*
* @var array|null
*/
private $original_block_to_render;
/**
* ID of a category.
*
* @var int
*/
private static $category_id;
/**
* Creates a category.
*
* @param WP_UnitTest_Factory $factory Test factory.
*/
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$category_id = $factory->category->create( array( 'name' => 'News' ) );
}
public function set_up() {
parent::set_up();
$this->original_block_to_render = WP_Block_Supports::$block_to_render;
WP_Block_Supports::$block_to_render = null;
}
public function tear_down() {
WP_Block_Supports::$block_to_render = $this->original_block_to_render;
parent::tear_down();
}
/**
* Renders the block.
*
* @param array $attributes Attributes of the block.
* @param array $context Optional. Context of the block. Default none.
* @return string The rendered block.
*/
private function render( $attributes, $context = array() ) {
$block = new WP_Block(
array(
'blockName' => 'core/query-title',
'attrs' => $attributes,
'innerBlocks' => array(),
'innerHTML' => '',
'innerContent' => array(),
),
$context
);
return render_block_core_query_title( $attributes, '', $block );
}
/**
* Tests that nothing is rendered without a type.
*
* @ticket 66567
*
* @dataProvider data_attributes_without_a_type
*
* @param array $attributes Attributes of the block.
*/
public function test_should_render_nothing_without_a_type( $attributes ) {
$this->go_to( get_category_link( self::$category_id ) );
$this->assertSame( '', $this->render( $attributes ) );
}
/**
* Data provider.
*
* @return array[]
*/
public static function data_attributes_without_a_type() {
return array(
'no attributes' => array( array() ),
'an empty type' => array( array( 'type' => '' ) ),
'a null type' => array( array( 'type' => null ) ),
);
}
/**
* Tests that nothing is rendered when the page is not of the type.
*
* @ticket 66567
*
* @dataProvider data_types_of_another_page
*
* @param string $type Type of the block.
* @param string $url URL of the page.
*/
public function test_should_render_nothing_when_the_page_is_not_of_the_type( $type, $url ) {
$this->go_to( $url );
$this->assertSame( '', $this->render( array( 'type' => $type ) ) );
}
/**
* Data provider.
*
* @return array[]
*/
public static function data_types_of_another_page() {
return array(
'archive on the home page' => array( 'archive', '/' ),
'archive on a search' => array( 'archive', '/?s=news' ),
'search on the home page' => array( 'search', '/' ),
'search on an archive' => array( 'search', '/?cat=1' ),
'post type without a post type' => array( 'post-type', '/' ),
);
}
/**
* Tests the title of an archive.
*
* @ticket 66567
*/
public function test_should_render_the_title_of_an_archive_with_the_prefix_by_default() {
$this->go_to( get_category_link( self::$category_id ) );
$this->assertSame( '<h1 >Category: <span>News</span></h1>', $this->render( array( 'type' => 'archive' ) ) );
}
/**
* Tests the title of an archive without the prefix.
*
* @ticket 66567
*/
public function test_should_render_the_title_of_an_archive_without_the_prefix() {
$this->go_to( get_category_link( self::$category_id ) );
$this->assertSame(
'<h1 >News</h1>',
$this->render(
array(
'type' => 'archive',
'showPrefix' => false,
)
)
);
}
/**
* Tests that the prefix is only removed while the title is made.
*
* @ticket 66567
*/
public function test_should_not_leave_the_filter_that_removes_the_prefix() {
$this->go_to( get_category_link( self::$category_id ) );
$this->render(
array(
'type' => 'archive',
'showPrefix' => false,
)
);
$this->assertFalse( has_filter( 'get_the_archive_title_prefix', '__return_empty_string' ) );
$this->assertSame( 'Category: <span>News</span>', get_the_archive_title() );
}
/**
* Tests the title of a search.
*
* @ticket 66567
*/
public function test_should_render_the_title_of_a_search() {
$this->go_to( '/?s=cats' );
$this->assertSame( '<h1 >Search results</h1>', $this->render( array( 'type' => 'search' ) ) );
}
/**
* Tests the title of a search with the search term.
*
* @ticket 66567
*/
public function test_should_render_the_search_term_in_the_title_of_a_search() {
$this->go_to( '/?s=cats+%26+%3Cb%3Edogs%3C%2Fb%3E' );
$this->assertSame(
'<h1 >Search results for: “cats & <b>dogs</b>”</h1>',
$this->render(
array(
'type' => 'search',
'showSearchTerm' => true,
)
)
);
}
/**
* Tests that the search term is not rendered when it is turned off.
*
* @ticket 66567
*/
public function test_should_not_render_the_search_term_when_it_is_off() {
$this->go_to( '/?s=cats' );
$this->assertSame(
'<h1 >Search results</h1>',
$this->render(
array(
'type' => 'search',
'showSearchTerm' => false,
)
)
);
}
/**
* Tests the title of a post type from the context of the query.
*
* @ticket 66567
*/
public function test_should_render_the_title_of_the_post_type_of_the_query() {
$this->go_to( '/' );
$this->assertSame(
'<h1 >Post Type: “Page”</h1>',
$this->render( array( 'type' => 'post-type' ), array( 'query' => array( 'postType' => 'page' ) ) )
);
}
/**
* Tests the title of a post type without the prefix.
*
* @ticket 66567
*/
public function test_should_render_the_title_of_a_post_type_without_the_prefix() {
$this->go_to( '/' );
$this->assertSame(
'<h1 >Page</h1>',
$this->render(
array(
'type' => 'post-type',
'showPrefix' => false,
),
array( 'query' => array( 'postType' => 'page' ) )
)
);
}
/**
* Tests that the post type of the current post is used without a query.
*
* @ticket 66567
*/
public function test_should_use_the_post_type_of_the_current_post_without_a_query() {
$page_id = self::factory()->post->create( array( 'post_type' => 'page' ) );
$this->go_to( get_permalink( $page_id ) );
$this->assertSame( '<h1 >Post Type: “Page”</h1>', $this->render( array( 'type' => 'post-type' ) ) );
}
/**
* Tests that nothing is rendered for a post type that does not exist.
*
* @ticket 66567
*/
public function test_should_render_nothing_for_a_post_type_that_does_not_exist() {
$this->go_to( '/' );
$this->assertSame( '', $this->render( array( 'type' => 'post-type' ), array( 'query' => array( 'postType' => 'not_a_post_type' ) ) ) );
}
/**
* Tests the tag of each level.
*
* @ticket 66567
*
* @dataProvider data_levels
*
* @param mixed $level Level of the block.
* @param string $tag_name Expected tag.
*/
public function test_should_use_the_tag_of_the_level( $level, $tag_name ) {
$this->go_to( '/?s=cats' );
$this->assertSame(
"<{$tag_name} >Search results</{$tag_name}>",
$this->render(
array(
'type' => 'search',
'level' => $level,
)
)
);
}
/**
* Data provider.
*
* @return array[]
*/
public static function data_levels() {
return array(
'zero is a paragraph' => array( 0, 'p' ),
'two' => array( 2, 'h2' ),
'six' => array( 6, 'h6' ),
'a numeric string' => array( '3', 'h3' ),
);
}
/**
* Tests the class of the alignment of the text.
*
* @ticket 66567
*/
public function test_should_add_the_class_of_the_text_alignment() {
$this->go_to( '/?s=cats' );
$output = $this->render(
array(
'type' => 'search',
'textAlign' => 'center',
)
);
$this->assertSame( '<h1 class="has-text-align-center">Search results</h1>', $output );
}
/**
* Tests that the block is registered with the render callback.
*
* @ticket 66567
*
* @covers ::register_block_core_query_title
*/
public function test_should_register_the_block_with_the_render_callback() {
$registry = WP_Block_Type_Registry::get_instance();
$original_block = $registry->get_registered( 'core/query-title' );
if ( $original_block ) {
unregister_block_type( 'core/query-title' );
}
try {
register_block_core_query_title();
$block_type = $registry->get_registered( 'core/query-title' );
$this->assertInstanceOf( WP_Block_Type::class, $block_type );
$this->assertSame( 'render_block_core_query_title', $block_type->render_callback );
} finally {
unregister_block_type( 'core/query-title' );
if ( $original_block ) {
$registry->register( $original_block );
}
}
}
}