Skip to content
This repository was archived by the owner on Nov 6, 2022. It is now read-only.

Commit d406eca

Browse files
committed
Test the image field, which I forgot to test before
Add this to get_special_case_fields, As it doesn't fit into the other groups of tests.
1 parent be58f0c commit d406eca

File tree

3 files changed

+60
-44
lines changed

3 files changed

+60
-44
lines changed

tests/php/integration/fixtures/all-fields-except-repeater.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaping could interfere with testing block_value().
99

10-
$string_fields = array(
10+
$non_object_fields = array(
1111
'text',
1212
'textarea',
1313
'url',
@@ -23,7 +23,7 @@
2323
'rich-text',
2424
);
2525

26-
foreach ( $string_fields as $field ) :
26+
foreach ( $non_object_fields as $field ) :
2727
?>
2828
<p class="<?php block_field( 'className' ); ?>">
2929
<?php

tests/php/integration/test-template-output.php

Lines changed: 57 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ class Test_Template_Output extends \WP_UnitTestCase {
3636
*/
3737
public $object_fields;
3838

39+
/**
40+
* Fields that don't fit well into the other test groups.
41+
*
42+
* @var array
43+
*/
44+
public $special_case_fields;
45+
3946
/**
4047
* The instance of Loader, to render the template.
4148
*
@@ -71,13 +78,6 @@ class Test_Template_Output extends \WP_UnitTestCase {
7178
*/
7279
public $template_location;
7380

74-
/**
75-
* The expected return value of the rich-text control.
76-
*
77-
* @var string
78-
*/
79-
public $rich_text_expected_return;
80-
8181
/**
8282
* The block class name.
8383
*
@@ -128,7 +128,7 @@ public function set_properties() {
128128
'email' => 'entered@emal.com',
129129
'number' => 15134,
130130
'color' => '#777444',
131-
'image' => 614,
131+
'image' => $this->get_image_attribute(),
132132
'select' => 'foo',
133133
'multiselect' => array( 'foo' ),
134134
'toggle' => true,
@@ -153,12 +153,33 @@ public function set_properties() {
153153
);
154154

155155
$this->object_fields = array(
156-
'image',
157156
'multiselect',
158157
'post',
159158
'taxonomy',
160159
'user',
161160
);
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+
);
162183
}
163184

164185
/**
@@ -168,13 +189,12 @@ public function set_properties() {
168189
* this puts an include statement in it, pointing to the fixture.
169190
*/
170191
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";
178198

179199
mkdir( $this->blocks_directory );
180200
$template_contents = sprintf( "<?php include '%s';", $template_path_in_fixtures );
@@ -223,6 +243,19 @@ public function get_user_attributes() {
223243
);
224244
}
225245

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+
226259
/**
227260
* Gets the block config.
228261
*
@@ -235,7 +268,7 @@ public function get_block_config() {
235268
$all_fields = array_merge(
236269
$this->string_fields,
237270
$this->object_fields,
238-
array_keys( $this->get_special_case_fields() )
271+
array_keys( $this->special_case_fields )
239272
);
240273

241274
foreach ( $all_fields as $field_name ) {
@@ -263,32 +296,15 @@ public function get_block_config() {
263296
);
264297
}
265298

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-
288299
/**
289300
* 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.
290306
*/
291-
public function test_integration_render_block_template() {
307+
public function test_block_template() {
292308
$block = new Blocks\Block();
293309
$block->from_array( $this->get_block_config() );
294310
$rendered_template = $this->loader->render_block_template( $block, $this->attributes );
@@ -355,7 +371,7 @@ public function test_integration_render_block_template() {
355371
}
356372

357373
// 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 ) {
359375
$this->assertContains(
360376
sprintf(
361377
'Here is the result of block_field() for %s: %s',

tests/php/unit/blocks/controls/test-class-image.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function test_register_settings() {
8181
public function test_validate() {
8282
$image_file = 'bar.jpeg';
8383
$expected_attachment_id = $this->factory()->attachment->create_object(
84-
$image_file,
84+
array( 'file' => $image_file ),
8585
0,
8686
array(
8787
'post_mime_type' => 'image/jpeg',

0 commit comments

Comments
 (0)