@@ -36,6 +36,13 @@ class Test_Template_Output extends \WP_UnitTestCase {
36
36
*/
37
37
public $ object_fields ;
38
38
39
+ /**
40
+ * Fields that don't fit well into the other test groups.
41
+ *
42
+ * @var array
43
+ */
44
+ public $ special_case_fields ;
45
+
39
46
/**
40
47
* The instance of Loader, to render the template.
41
48
*
@@ -71,13 +78,6 @@ class Test_Template_Output extends \WP_UnitTestCase {
71
78
*/
72
79
public $ template_location ;
73
80
74
- /**
75
- * The expected return value of the rich-text control.
76
- *
77
- * @var string
78
- */
79
- public $ rich_text_expected_return ;
80
-
81
81
/**
82
82
* The block class name.
83
83
*
@@ -128,7 +128,7 @@ public function set_properties() {
128
128
'email ' => 'entered@emal.com ' ,
129
129
'number ' => 15134 ,
130
130
'color ' => '#777444 ' ,
131
- 'image ' => 614 ,
131
+ 'image ' => $ this -> get_image_attribute () ,
132
132
'select ' => 'foo ' ,
133
133
'multiselect ' => array ( 'foo ' ),
134
134
'toggle ' => true ,
@@ -153,12 +153,33 @@ public function set_properties() {
153
153
);
154
154
155
155
$ this ->object_fields = array (
156
- 'image ' ,
157
156
'multiselect ' ,
158
157
'post ' ,
159
158
'taxonomy ' ,
160
159
'user ' ,
161
160
);
161
+
162
+ $ image = wp_get_attachment_image_src ( $ this ->attributes ['image ' ], 'full ' );
163
+ $ 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> ' ;
164
+
165
+ $ this ->special_case_fields = array (
166
+ 'checkbox ' => array (
167
+ 'block_field ' => 'Yes ' ,
168
+ 'block_value ' => 1 ,
169
+ ),
170
+ 'image ' => array (
171
+ 'block_field ' => $ image [0 ],
172
+ 'block_value ' => $ this ->attributes ['image ' ],
173
+ ),
174
+ 'rich-text ' => array (
175
+ 'block_field ' => $ rich_text ,
176
+ 'block_value ' => $ rich_text ,
177
+ ),
178
+ 'toggle ' => array (
179
+ 'block_field ' => 'Yes ' ,
180
+ 'block_value ' => 1 ,
181
+ ),
182
+ );
162
183
}
163
184
164
185
/**
@@ -168,13 +189,12 @@ public function set_properties() {
168
189
* this puts an include statement in it, pointing to the fixture.
169
190
*/
170
191
public function create_block_template () {
171
- $ this ->rich_text_expected_return = '<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> ' ;
172
- $ this ->block_name = 'all-fields-except-repeater ' ;
173
- $ this ->prefixed_block_name = "block-lab/ {$ this ->block_name }" ;
174
- $ theme_directory = get_template_directory ();
175
- $ template_path_in_fixtures = __DIR__ . "/fixtures/ {$ this ->block_name }.php " ;
176
- $ this ->blocks_directory = "{$ theme_directory }/blocks " ;
177
- $ this ->template_location = "{$ this ->blocks_directory }/block- {$ this ->block_name }.php " ;
192
+ $ this ->block_name = 'all-fields-except-repeater ' ;
193
+ $ this ->prefixed_block_name = "block-lab/ {$ this ->block_name }" ;
194
+ $ theme_directory = get_template_directory ();
195
+ $ template_path_in_fixtures = __DIR__ . "/fixtures/ {$ this ->block_name }.php " ;
196
+ $ this ->blocks_directory = "{$ theme_directory }/blocks " ;
197
+ $ this ->template_location = "{$ this ->blocks_directory }/block- {$ this ->block_name }.php " ;
178
198
179
199
mkdir ( $ this ->blocks_directory );
180
200
$ template_contents = sprintf ( "<?php include '%s'; " , $ template_path_in_fixtures );
@@ -223,6 +243,19 @@ public function get_user_attributes() {
223
243
);
224
244
}
225
245
246
+ /**
247
+ * Gets the image attribute.
248
+ *
249
+ * @return int The image's ID.
250
+ */
251
+ public function get_image_attribute () {
252
+ return $ this ->factory ()->attachment ->create_object (
253
+ array ( 'file ' => 'baz.jpeg ' ),
254
+ 0 ,
255
+ array ( 'post_mime_type ' => 'image/jpeg ' )
256
+ );
257
+ }
258
+
226
259
/**
227
260
* Gets the block config.
228
261
*
@@ -235,7 +268,7 @@ public function get_block_config() {
235
268
$ all_fields = array_merge (
236
269
$ this ->string_fields ,
237
270
$ this ->object_fields ,
238
- array_keys ( $ this ->get_special_case_fields () )
271
+ array_keys ( $ this ->special_case_fields )
239
272
);
240
273
241
274
foreach ( $ all_fields as $ field_name ) {
@@ -263,32 +296,15 @@ public function get_block_config() {
263
296
);
264
297
}
265
298
266
- /**
267
- * Gets the expected result of the template tags for special case fields.
268
- *
269
- * @return array
270
- */
271
- public function get_special_case_fields () {
272
- return array (
273
- 'checkbox ' => array (
274
- 'block_field ' => 'Yes ' ,
275
- 'block_value ' => 1 ,
276
- ),
277
- 'rich-text ' => array (
278
- 'block_field ' => $ this ->rich_text_expected_return ,
279
- 'block_value ' => $ this ->rich_text_expected_return ,
280
- ),
281
- 'toggle ' => array (
282
- 'block_field ' => 'Yes ' ,
283
- 'block_value ' => 1 ,
284
- ),
285
- );
286
- }
287
-
288
299
/**
289
300
* Tests whether the rendered block template has the expected values.
301
+ *
302
+ * Every field except the Repeater is tested.
303
+ * This sets mock block attributes, like those that would be saved from a block.
304
+ * Then, it looks for the mock template in the theme directory's blocks/ directory,
305
+ * and ensures that all of these fields appear correctly in it.
290
306
*/
291
- public function test_integration_render_block_template () {
307
+ public function test_block_template () {
292
308
$ block = new Blocks \Block ();
293
309
$ block ->from_array ( $ this ->get_block_config () );
294
310
$ rendered_template = $ this ->loader ->render_block_template ( $ block , $ this ->attributes );
@@ -355,7 +371,7 @@ public function test_integration_render_block_template() {
355
371
}
356
372
357
373
// Test the fields that don't fit well into the tests above.
358
- foreach ( $ this ->get_special_case_fields () as $ field_name => $ expected ) {
374
+ foreach ( $ this ->special_case_fields as $ field_name => $ expected ) {
359
375
$ this ->assertContains (
360
376
sprintf (
361
377
'Here is the result of block_field() for %s: %s ' ,
0 commit comments