This repository has been archived by the owner on Nov 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathtest-template-output.php
394 lines (351 loc) · 8.99 KB
/
test-template-output.php
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
<?php
/**
* Test_Template_Output
*
* @package Block_Lab
*/
use Block_Lab\Post_Types;
use Block_Lab\Blocks;
/**
* Class Test_Template_Output
*
* @package Template_Output
*/
class Test_Template_Output extends \WP_UnitTestCase {
/**
* The block attributes.
*
* @var array
*/
public $attributes;
/**
* All fields that return a string for block_value().
*
* @var array
*/
public $string_fields;
/**
* All fields that return either an object or ID for block_value().
*
* @var array
*/
public $object_fields;
/**
* Fields that don't fit well into the other test groups.
*
* @var array
*/
public $special_case_fields;
/**
* The instance of Loader, to render the template.
*
* @var Blocks\Loader
*/
public $loader;
/**
* The name of the block that tests all fields.
*
* @var string
*/
public $block_name;
/**
* The name of the block with the prefix.
*
* @var string
*/
public $prefixed_block_name;
/**
* The path to the blocks/ directory in the theme.
*
* @var string
*/
public $blocks_directory;
/**
* The location of the block template.
*
* @var string
*/
public $template_location;
/**
* The block class name.
*
* @var string
*/
public $class_name = 'example-name';
/**
* Setup.
*
* @inheritdoc
*/
public function setUp() {
parent::setUp();
$this->set_properties();
$this->create_block_template();
}
/**
* Teardown.
*
* @inheritdoc
*/
public function tearDown() {
parent::setUp();
if ( file_exists( $this->template_location ) ) {
unlink( $this->template_location );
}
if ( is_dir( $this->blocks_directory ) ) {
rmdir( $this->blocks_directory );
}
}
/**
* Sets class properties.
*/
public function set_properties() {
$this->loader = new Blocks\Loader();
$this->attributes = array(
'className' => $this->class_name,
'checkbox' => true,
'text' => 'Here is a text field',
'textarea' => 'And here is something',
'url' => 'https://yourdomain.com/entered',
'email' => '[email protected]',
'number' => 15134,
'color' => '#777444',
'image' => $this->get_image_attribute(),
'select' => 'foo',
'multiselect' => array( 'foo' ),
'toggle' => true,
'range' => 7,
'radio' => 'baz',
'post' => $this->get_post_attributes(),
'rich-text' => '<p>This is <strong>bold</strong> and this is <em>italic</em></p><p></p><p>Here is a new line with a space above</p>',
'taxonomy' => $this->get_taxonomy_attributes(),
'user' => $this->get_user_attributes(),
);
$this->string_fields = array(
'text',
'textarea',
'url',
'email',
'number',
'color',
'select',
'range',
'radio',
);
$this->object_fields = array(
'multiselect',
'post',
'taxonomy',
'user',
);
$image = wp_get_attachment_image_src( $this->attributes['image'], 'full' );
$rich_text = '<p>This is <strong>bold</strong> and this is <em>italic</em></p></p><p>Here is a new line with a space above</p></p>';
$this->special_case_fields = array(
'checkbox' => array(
'block_field' => 'Yes',
'block_value' => 1,
),
'image' => array(
'block_field' => $image[0],
'block_value' => $this->attributes['image'],
),
'rich-text' => array(
'block_field' => $rich_text,
'block_value' => $rich_text,
),
'toggle' => array(
'block_field' => 'Yes',
'block_value' => 1,
),
);
}
/**
* Creates the block template.
*
* Instead of copying the fixture entirely into the theme directory,
* this puts an include statement in it, pointing to the fixture.
*/
public function create_block_template() {
$this->block_name = 'all-fields-except-repeater';
$this->prefixed_block_name = "block-lab/{$this->block_name}";
$theme_directory = get_template_directory();
$template_path_in_fixtures = __DIR__ . "/fixtures/{$this->block_name}.php";
$this->blocks_directory = "{$theme_directory}/blocks";
$this->template_location = "{$this->blocks_directory}/block-{$this->block_name}.php";
mkdir( $this->blocks_directory );
$template_contents = sprintf( "<?php include '%s';", $template_path_in_fixtures );
file_put_contents( $this->template_location, $template_contents ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents
}
/**
* Gets the post attributes.
*
* @return array
*/
public function get_post_attributes() {
$id = $this->factory()->post->create();
return array(
'id' => $id,
'name' => get_the_title( $id ),
);
}
/**
* Gets the taxonomy attributes.
*
* @return array
*/
public function get_taxonomy_attributes() {
$term = $this->factory()->tag->create_and_get();
return array(
'id' => $term->term_id,
'name' => $term->name,
);
}
/**
* Gets the user attributes.
*
* @return array
*/
public function get_user_attributes() {
$user = $this->factory()->user->create_and_get();
return array(
'id' => $user->ID,
'userName' => $user->display_name,
);
}
/**
* Gets the image attribute.
*
* @return int The image's ID.
*/
public function get_image_attribute() {
return $this->factory()->attachment->create_object(
array( 'file' => 'baz.jpeg' ),
0,
array( 'post_mime_type' => 'image/jpeg' )
);
}
/**
* Gets the block config.
*
* @return array The config for the block.
*/
public function get_block_config() {
$block_post = new Post_Types\Block_Post();
$fields = array();
$all_fields = array_merge(
$this->string_fields,
$this->object_fields,
array_keys( $this->special_case_fields )
);
foreach ( $all_fields as $field_name ) {
$control_name = str_replace( '-', '_', $field_name );
$control = $block_post->get_control( $control_name );
$fields[ $field_name ] = array(
'control' => str_replace( '-', '_', $field_name ),
'name' => $control_name,
'type' => $control->type,
);
}
return array(
'category' => array(
'icon' => null,
'slug' => '',
'title' => '',
),
'excluded' => array(),
'fields' => $fields,
'icon' => 'block_lab',
'keywords' => array( '' ),
'name' => $this->block_name,
'title' => 'All Fields',
);
}
/**
* Tests whether the rendered block template has the expected values.
*
* Every field except the Repeater is tested.
* This sets mock block attributes, like those that would be saved from a block.
* Then, it loads the mock template in the theme's blocks/ directory,
* and ensures that all of these fields appear correctly in it.
*/
public function test_block_template() {
$block = new Blocks\Block();
$block->from_array( $this->get_block_config() );
$rendered_template = $this->loader->render_block_template( $block, $this->attributes );
$actual_template = str_replace( array( "\t", "\n" ), '', $rendered_template );
// The 'className' should be present.
$this->assertContains(
sprintf( '<p class="%s">', $this->class_name ),
$actual_template
);
// Test the fields that return a string for block_value().
foreach ( $this->string_fields as $field ) {
$this->assertContains(
sprintf(
esc_html( 'And here is the result of calling block_value() for %s: %s', 'bl-testing-templates' ),
$field,
$this->attributes[ $field ]
),
$actual_template
);
$this->assertContains(
sprintf(
esc_html( 'Here is the result of block_field() for %s: %s', 'bl-testing-templates' ),
$field,
$this->attributes[ $field ]
),
$actual_template
);
}
$object_fields = array(
'post' => array(
'object' => get_post( $this->attributes['post']['id'] ),
'properties' => array( 'ID', 'post_name' ),
),
'taxonomy' => array(
'object' => get_term( $this->attributes['taxonomy']['id'] ),
'properties' => array( 'term_id', 'name' ),
),
'user' => array(
'object' => get_user_by( 'id', $this->attributes['user']['id'] ),
'properties' => array( 'ID', 'first_name' ),
),
);
/*
* The fields here return objects for block_value(), so test that some of the properties are correct.
* For example, block_value( 'post' )->id.
*/
foreach ( $object_fields as $name => $field ) {
foreach ( $field['properties'] as $property ) {
$this->assertContains(
sprintf(
'Here is the result of passing %s to block_value() with the property %s: %s',
$name,
$property,
$field['object']->$property
),
$actual_template
);
}
}
// Test the fields that don't fit well into the tests above.
foreach ( $this->special_case_fields as $field_name => $expected ) {
$this->assertContains(
sprintf(
'Here is the result of block_field() for %s: %s',
$field_name,
$expected['block_field']
),
$actual_template
);
$this->assertContains(
sprintf(
'And here is the result of calling block_value() for %s: %s',
$field_name,
$expected['block_value']
),
$actual_template
);
}
}
}