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

Add integration and unit tests #402

Merged
merged 7 commits into from
Aug 28, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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.
  • Loading branch information
kienstra committed Aug 26, 2019
commit d406eca915cffe59145e6fba569d1afefbc29cd7
4 changes: 2 additions & 2 deletions tests/php/integration/fixtures/all-fields-except-repeater.php
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@

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

$string_fields = array(
$non_object_fields = array(
'text',
'textarea',
'url',
@@ -23,7 +23,7 @@
'rich-text',
);

foreach ( $string_fields as $field ) :
foreach ( $non_object_fields as $field ) :
?>
<p class="<?php block_field( 'className' ); ?>">
<?php
98 changes: 57 additions & 41 deletions tests/php/integration/test-template-output.php
Original file line number Diff line number Diff line change
@@ -36,6 +36,13 @@ class Test_Template_Output extends \WP_UnitTestCase {
*/
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.
*
@@ -71,13 +78,6 @@ class Test_Template_Output extends \WP_UnitTestCase {
*/
public $template_location;

/**
* The expected return value of the rich-text control.
*
* @var string
*/
public $rich_text_expected_return;

/**
* The block class name.
*
@@ -128,7 +128,7 @@ public function set_properties() {
'email' => 'entered@emal.com',
'number' => 15134,
'color' => '#777444',
'image' => 614,
'image' => $this->get_image_attribute(),
'select' => 'foo',
'multiselect' => array( 'foo' ),
'toggle' => true,
@@ -153,12 +153,33 @@ public function set_properties() {
);

$this->object_fields = array(
'image',
'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,
),
);
}

/**
@@ -168,13 +189,12 @@ public function set_properties() {
* this puts an include statement in it, pointing to the fixture.
*/
public function create_block_template() {
$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>';
$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";
$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 );
@@ -223,6 +243,19 @@ public function get_user_attributes() {
);
}

/**
* 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.
*
@@ -235,7 +268,7 @@ public function get_block_config() {
$all_fields = array_merge(
$this->string_fields,
$this->object_fields,
array_keys( $this->get_special_case_fields() )
array_keys( $this->special_case_fields )
);

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

/**
* Gets the expected result of the template tags for special case fields.
*
* @return array
*/
public function get_special_case_fields() {
return array(
'checkbox' => array(
'block_field' => 'Yes',
'block_value' => 1,
),
'rich-text' => array(
'block_field' => $this->rich_text_expected_return,
'block_value' => $this->rich_text_expected_return,
),
'toggle' => array(
'block_field' => 'Yes',
'block_value' => 1,
),
);
}

/**
* 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 looks for the mock template in the theme directory's blocks/ directory,
* and ensures that all of these fields appear correctly in it.
*/
public function test_integration_render_block_template() {
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 );
@@ -355,7 +371,7 @@ public function test_integration_render_block_template() {
}

// Test the fields that don't fit well into the tests above.
foreach ( $this->get_special_case_fields() as $field_name => $expected ) {
foreach ( $this->special_case_fields as $field_name => $expected ) {
$this->assertContains(
sprintf(
'Here is the result of block_field() for %s: %s',
2 changes: 1 addition & 1 deletion tests/php/unit/blocks/controls/test-class-image.php
Original file line number Diff line number Diff line change
@@ -81,7 +81,7 @@ public function test_register_settings() {
public function test_validate() {
$image_file = 'bar.jpeg';
$expected_attachment_id = $this->factory()->attachment->create_object(
$image_file,
array( 'file' => $image_file ),
0,
array(
'post_mime_type' => 'image/jpeg',