From 03c4b1cce66af35f484c46a5f7c56018ecee98cc Mon Sep 17 00:00:00 2001 From: Ryan Kienstra Date: Tue, 17 Sep 2019 19:27:23 -0500 Subject: [PATCH 1/5] Test block_row_index() and block_row_count() Testing these in an integration test is probably the most useful, as they're mainly wrappers for other logic. --- .../fixtures/repeater-all-fields.php | 6 ++++++ .../test-repeater-template-output.php | 20 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/tests/php/integration/fixtures/repeater-all-fields.php b/tests/php/integration/fixtures/repeater-all-fields.php index 4020deb0b..60e3823f7 100644 --- a/tests/php/integration/fixtures/repeater-all-fields.php +++ b/tests/php/integration/fixtures/repeater-all-fields.php @@ -29,8 +29,14 @@ diff --git a/tests/php/integration/test-repeater-template-output.php b/tests/php/integration/test-repeater-template-output.php index 210f2ceb3..08134884c 100644 --- a/tests/php/integration/test-repeater-template-output.php +++ b/tests/php/integration/test-repeater-template-output.php @@ -189,12 +189,32 @@ public function test_repeater_template() { $actual_template = str_replace( array( "\t", "\n" ), '', $rendered_template ); $rows = $this->attributes[ self::REPEATER_FIELD_NAME ]['rows']; + $this->assertContains( + sprintf( + 'block_row_count() returns %d', + count( $rows ) + ), + $actual_template + ); + // The 'className' should be present. $this->assertContains( sprintf( '
', $this->class_name ), $actual_template ); + // Test that block_row_index() returns the right row index. + foreach ( $rows as $row_number => $row ) { + $this->assertContains( + sprintf( + 'In row %d, the result of block_row_index() is %s: ', + $row_number, + $row_number + ), + $actual_template + ); + } + // Test the fields that return a string for block_sub_value(). foreach ( $rows as $row_number => $row ) { foreach ( $this->string_fields as $field ) { From a0d252f1c768291242d8b59118277e3e98b35417 Mon Sep 17 00:00:00 2001 From: Ryan Kienstra Date: Tue, 17 Sep 2019 19:28:14 -0500 Subject: [PATCH 2/5] Remove needless space between the @param type and the description Also, remove an isset() check as empty() will handle that case. --- js/blocks/loader/attributes.js | 2 +- php/post-types/class-block-post.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/js/blocks/loader/attributes.js b/js/blocks/loader/attributes.js index 6ab6ccaae..eefc499d9 100644 --- a/js/blocks/loader/attributes.js +++ b/js/blocks/loader/attributes.js @@ -1,7 +1,7 @@ /** * Gets the attributes for a block, based on given fields. * - * @param {Object} fields The fields to get the attributes from. + * @param {Object} fields The fields to get the attributes from. * @return {Object} attributes The attributes for the fields. */ const getBlockAttributes = ( fields ) => { diff --git a/php/post-types/class-block-post.php b/php/post-types/class-block-post.php index 12b06b69a..8156529fa 100644 --- a/php/post-types/class-block-post.php +++ b/php/post-types/class-block-post.php @@ -338,7 +338,7 @@ public function add_meta_boxes() { 'default' ); - if ( isset( $post->post_name ) && ! empty( $post->post_name ) ) { + if ( ! empty( $post->post_name ) ) { $locations = block_lab()->get_template_locations( $post->post_name ); $template = block_lab()->locate_template( $locations, '', true ); From 24ecb6dcfd68fe7e72257da64b48d501d698e518 Mon Sep 17 00:00:00 2001 From: Ryan Kienstra Date: Tue, 17 Sep 2019 19:37:03 -0500 Subject: [PATCH 3/5] Add @covers tags to integration test methods To indicate that the helpers.php functions are covered, like block_rows() and block_row(). --- tests/php/integration/test-repeater-template-output.php | 8 ++++++++ tests/php/integration/test-template-output.php | 3 +++ 2 files changed, 11 insertions(+) diff --git a/tests/php/integration/test-repeater-template-output.php b/tests/php/integration/test-repeater-template-output.php index 08134884c..98c65322a 100644 --- a/tests/php/integration/test-repeater-template-output.php +++ b/tests/php/integration/test-repeater-template-output.php @@ -181,6 +181,14 @@ public function get_block_config() { * This has a repeater with 2 rows, and tests every possible field. * It 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 asserts the values. + * + * @covers \block_rows() + * @covers \block_row() + * @covers \reset_block_row() + * @covers \block_row_field() + * @covers \block_row_value() + * @covers \block_row_index() + * @covers \block_row_count() */ public function test_repeater_template() { $block = new Blocks\Block(); diff --git a/tests/php/integration/test-template-output.php b/tests/php/integration/test-template-output.php index f09ab8a62..b3ebfb3da 100644 --- a/tests/php/integration/test-template-output.php +++ b/tests/php/integration/test-template-output.php @@ -139,6 +139,9 @@ public function get_block_config() { * 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. + * + * @covers \block_field() + * @covers \block_value() */ public function test_block_template() { $block = new Blocks\Block(); From bdb7bfddb3fb5459c8e147433124d2481ee63a82 Mon Sep 17 00:00:00 2001 From: Ryan Kienstra Date: Thu, 19 Sep 2019 01:28:09 -0500 Subject: [PATCH 4/5] Add a unit test for Loader::init(), Still, I need to add a lot of tests for this class, and a few others. --- tests/php/unit/blocks/test-class-loader.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/php/unit/blocks/test-class-loader.php b/tests/php/unit/blocks/test-class-loader.php index 9a86acf2c..f8f042185 100644 --- a/tests/php/unit/blocks/test-class-loader.php +++ b/tests/php/unit/blocks/test-class-loader.php @@ -19,6 +19,19 @@ class Test_Loader extends Abstract_Template { */ public $instance; + /** + * Test init. + * + * @covers \Block_Lab\Blocks\Loader::init() + */ + public function test_init() { + $this->assertEquals( 'Block_Lab\\Blocks\\Loader', get_class( $this->instance->init() ) ); + $this->assertContains( 'js/editor.blocks.js', $this->instance->assets['path']['entry'] ); + $this->assertContains( 'css/blocks.editor.css', $this->instance->assets['path']['editor_style'] ); + $this->assertContains( 'js/editor.blocks.js', $this->instance->assets['url']['entry'] ); + $this->assertContains( 'css/blocks.editor.css', $this->instance->assets['url']['editor_style'] ); + } + /** * Test register_hooks. * From 2c43d58da308b3e2f8181d9b4d929576c783e22b Mon Sep 17 00:00:00 2001 From: Ryan Kienstra Date: Thu, 19 Sep 2019 01:34:28 -0500 Subject: [PATCH 5/5] Use %d instead of %s for integers This is passing it a row number, so it should not use %s. --- tests/php/integration/fixtures/repeater-all-fields.php | 2 +- tests/php/integration/test-repeater-template-output.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/php/integration/fixtures/repeater-all-fields.php b/tests/php/integration/fixtures/repeater-all-fields.php index 60e3823f7..182260bce 100644 --- a/tests/php/integration/fixtures/repeater-all-fields.php +++ b/tests/php/integration/fixtures/repeater-all-fields.php @@ -33,7 +33,7 @@ while ( block_rows( $repeater_name ) ) : block_row( $repeater_name ); printf( - 'In row %d, the result of block_row_index() is %s: ', + 'In row %d, the result of block_row_index() is %d', $row_number, block_row_index() ); diff --git a/tests/php/integration/test-repeater-template-output.php b/tests/php/integration/test-repeater-template-output.php index 98c65322a..f965e0156 100644 --- a/tests/php/integration/test-repeater-template-output.php +++ b/tests/php/integration/test-repeater-template-output.php @@ -215,7 +215,7 @@ public function test_repeater_template() { foreach ( $rows as $row_number => $row ) { $this->assertContains( sprintf( - 'In row %d, the result of block_row_index() is %s: ', + 'In row %d, the result of block_row_index() is %d', $row_number, $row_number ),