diff --git a/composer.json b/composer.json index 00d2caf58..65c426151 100644 --- a/composer.json +++ b/composer.json @@ -9,6 +9,7 @@ }, "require-dev": { "brain/monkey": "^2", - "squizlabs/php_codesniffer": "^3.4" + "squizlabs/php_codesniffer": "^3.4", + "wp-coding-standards/wpcs": "2.1.1" } } diff --git a/js/blocks/loader/attributes.js b/js/blocks/loader/attributes.js index 1f1767fc4..4dba8f86e 100644 --- a/js/blocks/loader/attributes.js +++ b/js/blocks/loader/attributes.js @@ -2,7 +2,7 @@ * Gets the attributes for a block, based on given fields. * * @param {Object} fields The fields to get the attributes from. - * @return {Array} attributes The attributes for the fields. + * @return {Object} attributes The attributes for the fields. */ const getBlockAttributes = ( fields ) => { diff --git a/js/blocks/loader/fields.js b/js/blocks/loader/fields.js index a47f7cc4c..5b699eb5c 100644 --- a/js/blocks/loader/fields.js +++ b/js/blocks/loader/fields.js @@ -6,10 +6,12 @@ * @return {number} Either -1, 0, or 1, depending on the comparison. */ const compare = ( a, b ) => { - if ( a.order < b.order ) + if ( a.order < b.order ) { return -1; - if ( a.order > b.order ) + } + if ( a.order > b.order ) { return 1; + } return 0; } @@ -21,7 +23,7 @@ const compare = ( a, b ) => { */ const simplifiedFields = ( fields ) => { - let fieldList = [] + let fieldList = []; for ( let fieldName in fields ) { if ( '' === fieldName ) { diff --git a/js/blocks/loader/index.js b/js/blocks/loader/index.js index 252e20141..16f8272ea 100644 --- a/js/blocks/loader/index.js +++ b/js/blocks/loader/index.js @@ -1,4 +1,3 @@ - /** * WordPress dependencies */ @@ -56,6 +55,6 @@ const registerBlocks = () => { }, } ) } -} +}; export default registerBlocks(); diff --git a/package.json b/package.json index 75bf06009..2ff8cdef9 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,9 @@ }, "scripts": { "dev": "cross-env BABEL_ENV=default webpack --watch", - "build": "cross-env BABEL_ENV=default NODE_ENV=production webpack" + "build": "cross-env BABEL_ENV=default NODE_ENV=production webpack", + "lint:php": "vendor/bin/phpcs", + "lint:php:fix": "vendor/bin/phpcbf" }, "author": "Block Lab", "license": "GPL-2.0+", diff --git a/php/blocks/class-loader.php b/php/blocks/class-loader.php index d3a23d197..de08ac5c0 100644 --- a/php/blocks/class-loader.php +++ b/php/blocks/class-loader.php @@ -275,7 +275,7 @@ public function get_attributes_from_field( $attributes, $field_name, $field ) { /** * Renders the block provided a template is provided. * - * @param array $block The block to render. + * @param Block $block The block to render. * @param array $attributes Attributes to render. * * @return mixed diff --git a/php/class-util.php b/php/class-util.php index cbc00c494..acd1c1048 100644 --- a/php/class-util.php +++ b/php/class-util.php @@ -11,7 +11,6 @@ namespace Block_Lab; use Block_Lab\Blocks; -use Block_Lab\Component_Abstract; /** * Class Util diff --git a/phpcs.xml b/phpcs.xml index 34ef1fbf1..b337e37c8 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -4,6 +4,10 @@ + + tests/* + + @@ -12,7 +16,6 @@ . */node_modules/* */vendor/* - */tests/* */package/* *.js *.css diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 57d147e75..ef3865c75 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -10,8 +10,12 @@ - ./tests/php/helpers/ - ./tests/ + ./tests/php/unit/helpers/ + ./tests/php/unit/ + + + + ./tests/php/integration/ diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 96a0968d2..3a0af8630 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -13,7 +13,7 @@ } if ( ! file_exists( $_tests_dir . '/includes/functions.php' ) ) { - echo "Could not find $_tests_dir/includes/functions.php, have you run bin/install-wp-tests.sh ?" . PHP_EOL; + echo "Could not find $_tests_dir/includes/functions.php, have you run bin/install-wp-tests.sh ?" . PHP_EOL; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped exit( 1 ); } @@ -27,7 +27,7 @@ function _manually_load_plugin() { $plugin_root = dirname( __DIR__ ); require $plugin_root . '/vendor/autoload.php'; require $plugin_root . '/vendor/antecedent/patchwork/Patchwork.php'; - require $plugin_root . '/tests/php/trait-testing-helper.php'; + require $plugin_root . '/tests/php/unit/helpers/trait-testing-helper.php'; require $plugin_root . '/block-lab.php'; } tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' ); diff --git a/tests/class-test-the-plugin.php b/tests/class-test-the-plugin.php deleted file mode 100644 index d19256d24..000000000 --- a/tests/class-test-the-plugin.php +++ /dev/null @@ -1,36 +0,0 @@ -assertContains( '
', $buffer ); - } - - /** - * Test block_lab_php_version_text(). - * - * @covers ::block_lab_php_version_text() - */ - public function testblock_lab_php_version_text() { - $this->assertContains( 'Block Lab plugin error:', block_lab_php_version_text() ); - } -} diff --git a/tests/php/integration/fixtures/all-fields-except-repeater.php b/tests/php/integration/fixtures/all-fields-except-repeater.php new file mode 100644 index 000000000..8e50dbd45 --- /dev/null +++ b/tests/php/integration/fixtures/all-fields-except-repeater.php @@ -0,0 +1,75 @@ + +

+ +

+ +

+ +

+ array( 'ID', 'post_name' ), + 'taxonomy' => array( 'term_id', 'name' ), + 'user' => array( 'ID', 'first_name' ), +); + +foreach ( $non_string_fields as $name => $value ) : + /* translators: %s is the field name */ + printf( + 'Here is the result of block_field() for %s: ', + $name + ); + block_field( $name ); + + $block_value = block_value( $name ); + foreach ( $value as $block_value_property ) : + printf( + 'Here is the result of passing %s to block_value() with the property %s: %s', + $name, + $block_value_property, + $block_value->$block_value_property + ); + endforeach; +endforeach; diff --git a/tests/php/integration/test-template-output.php b/tests/php/integration/test-template-output.php new file mode 100644 index 000000000..00daf77c1 --- /dev/null +++ b/tests/php/integration/test-template-output.php @@ -0,0 +1,394 @@ +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' => 'entered@emal.com', + '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' => '

This is bold and this is italic

Here is a new line with a space above

', + '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 = '

This is bold and this is italic

Here is a new line with a space above

'; + + $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( "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( '

', $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 + ); + } + } +} diff --git a/tests/php/test-helpers.php b/tests/php/test-helpers.php deleted file mode 100644 index 08f3ac861..000000000 --- a/tests/php/test-helpers.php +++ /dev/null @@ -1,129 +0,0 @@ - 'text' ); - $block_config = array( - 'fields' => array( - $field_name => $field_config - ) - ); - - $block_lab_config = new Blocks\Block(); - $block_lab_config->from_array( $block_config ); - - // Because block_field() had the second argument of false, this should return the value stored in the field, not echo it. - ob_start(); - $return_value = block_field( $field_name, false ); - $echoed = ob_get_clean(); - $this->assertEquals( $mock_text, $return_value ); - $this->assertEmpty( $echoed ); - - // Test the same scenario as above, but for 'className'. - ob_start(); - $return_value = block_field( $class_key, false ); - $echoed = ob_get_clean(); - $this->assertEquals( $expected_class, $return_value ); - $this->assertEmpty( $echoed ); - - ob_start(); - $return_value = block_field( $field_name, true ); - $actual_user_login = ob_get_clean(); - - // Because block_field() has a second argument of true, this should echo the user login and return it. - $this->assertEquals( $mock_text, $actual_user_login ); - $this->assertEquals( $return_value, $actual_user_login ); - - ob_start(); - $return_value = block_field( $class_key, true ); - $actual_class = ob_get_clean(); - - // Test the same scenario as above, but for 'className'. - $this->assertEquals( $expected_class, $actual_class ); - $this->assertEquals( $return_value, $actual_class ); - } - - /** - * Test maybe_get_sub_field_name. - * - * @covers ::maybe_get_sub_field_name() - */ - public function test_maybe_get_sub_field_name() { - global $block_lab_config; - - $field_name_one_without_sub_fields = 'Example name'; - $field_name_two_without_sub_fields = 'Baz name'; - $fields_without_repeater = array( - $field_name_one_without_sub_fields => array( 'control' => 'image' ), - $field_name_two_without_sub_fields => array( 'control' => 'textarea' ), - ); - - $block_config = array( 'fields' => $fields_without_repeater ); - $block_lab_config = new Blocks\Block(); - $block_lab_config->from_array( $block_config ); - - // This does not have sub-fields, so this should return false. - $this->assertFalse( maybe_get_sub_field_name( $field_name_one_without_sub_fields ) ); - $this->assertFalse( maybe_get_sub_field_name( array( $field_name_one_without_sub_fields, $field_name_two_without_sub_fields ) ) ); - - $field_name_with_sub_field = 'Has sub-field'; - $sub_field_name = 'Example sub-field'; - $fields_with_repeater = array( - $field_name_one_without_sub_fields => array( 'control' => 'image' ), - $field_name_with_sub_field => array( - 'control' => 'repeater', - 'sub_fields' => array( - $sub_field_name => array( 'control' => 'taxonomy' ), - ), - ), - ); - - $block_config = array( 'fields' => $fields_with_repeater ); - $block_lab_config = new Blocks\Block(); - $block_lab_config->from_array( $block_config ); - - // Neither of the strings in the array is a field, so this should return false. - $this->assertFalse( maybe_get_sub_field_name( array( 'non-existent-field', 'not-a-field' ) ) ); - - // The first field doesn't have a sub-field, so this should return false. - $this->assertFalse( maybe_get_sub_field_name( array( $field_name_one_without_sub_fields, $sub_field_name ) ) ); - - // When passing only the sub-field name, this should return false. - $this->assertFalse( maybe_get_sub_field_name( $sub_field_name ) ); - $this->assertFalse( maybe_get_sub_field_name( array( $sub_field_name ) ) ); - - // The array values are correct, but in the wrong order. - $this->assertFalse( maybe_get_sub_field_name( array( $sub_field_name, $field_name_with_sub_field ) ) ); - - // The first 2 array values are correct, but there's a third value that's not needed. - $this->assertFalse( maybe_get_sub_field_name( array( $field_name_with_sub_field, $sub_field_name, 'extra-field-name' ) ) ); - - // The arguments are now in the correct order, so this should return the sub-field name. - $this->assertEquals( $sub_field_name, maybe_get_sub_field_name( array( $field_name_with_sub_field, $sub_field_name ) ) ); - } -} diff --git a/tests/php/admin/test-class-admin.php b/tests/php/unit/admin/test-class-admin.php similarity index 100% rename from tests/php/admin/test-class-admin.php rename to tests/php/unit/admin/test-class-admin.php diff --git a/tests/php/admin/test-class-import.php b/tests/php/unit/admin/test-class-import.php similarity index 92% rename from tests/php/admin/test-class-import.php rename to tests/php/unit/admin/test-class-import.php index b70e876de..61e0235ab 100644 --- a/tests/php/admin/test-class-import.php +++ b/tests/php/unit/admin/test-class-import.php @@ -43,8 +43,8 @@ public function setUp() { parent::setUp(); Monkey\setUp(); $this->instance = new Admin\Import(); - $this->import_file_valid_json = dirname( dirname( __DIR__ ) ) . '/fixtures/mock-import-valid-format.txt'; - $this->import_file_invalid_json = dirname( dirname( __DIR__ ) ) . '/fixtures/mock-import-invalid-format.txt'; + $this->import_file_valid_json = dirname( __DIR__ ) . '/fixtures/mock-import-valid-format.txt'; + $this->import_file_invalid_json = dirname( __DIR__ ) . '/fixtures/mock-import-invalid-format.txt'; $this->instance->set_plugin( block_lab() ); } @@ -81,7 +81,7 @@ public function test_register_importer() { array( 'Block Lab', 'Import custom blocks created with Block Lab.', - array( $this->instance, 'render_page' ) + array( $this->instance, 'render_page' ), ), $wp_importers[ $this->instance->slug ] ); @@ -162,16 +162,19 @@ public function test_render_page() { ) ); $_FILES['import'] = $files_import; - add_filter( 'wp_handle_upload', function( $upload ) use ( $file ) { - unset( $upload ); - return array_merge( - $file, - array( - 'url' => 'https://example.com/foo', - 'type' => 'text/plain', - ) - ); - } ); + add_filter( + 'wp_handle_upload', + function( $upload ) use ( $file ) { + unset( $upload ); + return array_merge( + $file, + array( + 'url' => 'https://example.com/foo', + 'type' => 'text/plain', + ) + ); + } + ); Monkey\Functions\expect( 'filter_input' ) ->twice() @@ -209,9 +212,9 @@ function wp_handle_upload_error( $file, $message ) { $this->assertNotContains( $welcome_text, $output ); // The file is now a real file. - $file = array( 'file' => $this->import_file_valid_json ); - $tmp_name = $this->import_file_valid_json; - $files_import = array_merge( + $file = array( 'file' => $this->import_file_valid_json ); + $tmp_name = $this->import_file_valid_json; + $files_import = array_merge( $file, array( 'name' => 'mock-import-valid-format', @@ -222,16 +225,18 @@ function wp_handle_upload_error( $file, $message ) { $_FILES['import'] = $files_import; remove_all_filters( 'wp_handle_upload' ); - add_filter( 'wp_handle_upload', function( $upload ) use ( $file ) { - unset( $upload ); - return array_merge( - $file, - array( - 'url' => 'https://example.com/foo', - 'type' => 'text/plain', - ) - ); - } ); + add_filter( + 'wp_handle_upload', + function() use ( $file ) { + return array_merge( + $file, + array( + 'url' => 'https://example.com/foo', + 'type' => 'text/plain', + ) + ); + } + ); Monkey\Functions\expect( 'is_uploaded_file' ) ->once() @@ -347,7 +352,7 @@ public function test_validate_upload() { $nonexistent_file = 'does-not-exist.xml'; ob_start(); - $this->assertFalse( $this->instance->validate_upload( array( 'file' => $nonexistent_file) ) ); + $this->assertFalse( $this->instance->validate_upload( array( 'file' => $nonexistent_file ) ) ); $output = ob_get_clean(); // If the file doesn't exist, this should have a message that reflects that. diff --git a/tests/php/admin/test-class-license.php b/tests/php/unit/admin/test-class-license.php similarity index 92% rename from tests/php/admin/test-class-license.php rename to tests/php/unit/admin/test-class-license.php index a585f19cf..ad5002901 100644 --- a/tests/php/admin/test-class-license.php +++ b/tests/php/unit/admin/test-class-license.php @@ -183,7 +183,7 @@ public function test_get_license() { $this->instance->init(); $valid_license_transient_value = array( 'license' => 'valid', - 'expires' => date( 'Y-m-d', time() + DAY_IN_SECONDS ) + 'expires' => date( 'Y-m-d', time() + DAY_IN_SECONDS ), ); $invalid_license_transient_value = array( 'license' => 'expired', @@ -206,10 +206,13 @@ public function test_get_license() { 'expires' => $expiration_date, ); - add_filter( self::HTTP_FILTER_NAME, function( $response ) use ( $expected_license ) { - unset( $response ); - return array( 'body' => wp_json_encode( $expected_license ) ); - } ); + add_filter( + self::HTTP_FILTER_NAME, + function( $response ) use ( $expected_license ) { + unset( $response ); + return array( 'body' => wp_json_encode( $expected_license ) ); + } + ); delete_transient( self::LICENSE_TRANSIENT_NAME ); $example_valid_license_key = '5134315'; @@ -227,10 +230,13 @@ public function test_get_license() { public function test_activate_license() { $this->instance->init(); $license_key = '6234234'; - add_filter( self::HTTP_FILTER_NAME, function( $response ) { - unset( $response ); - return new WP_Error(); - } ); + add_filter( + self::HTTP_FILTER_NAME, + function( $response ) { + unset( $response ); + return new WP_Error(); + } + ); // If the POST request returns a wp_error(), this should return false. $this->assertFalse( $this->instance->activate_license( $license_key ) ); @@ -242,10 +248,13 @@ public function test_activate_license() { 'expires' => date( 'Y-m-d', time() + DAY_IN_SECONDS ), ); - add_filter( self::HTTP_FILTER_NAME, function( $response ) use ( $expected_license ) { - unset( $response ); - return array( 'body' => wp_json_encode( $expected_license ) ); - } ); + add_filter( + self::HTTP_FILTER_NAME, + function( $response ) use ( $expected_license ) { + unset( $response ); + return array( 'body' => wp_json_encode( $expected_license ) ); + } + ); $this->instance->activate_license( $license_key ); // Having simulated a successful license validation with the filter above, this should activate the license. diff --git a/tests/php/admin/test-class-settings.php b/tests/php/unit/admin/test-class-settings.php similarity index 99% rename from tests/php/admin/test-class-settings.php rename to tests/php/unit/admin/test-class-settings.php index 5d3bf67f1..4a0999ece 100644 --- a/tests/php/admin/test-class-settings.php +++ b/tests/php/unit/admin/test-class-settings.php @@ -209,7 +209,7 @@ public function test_render_page_header() { * @covers \Block_Lab\Admin\Settings::prepare_notice() */ public function test_prepare_notice() { - $notice = 'There was a problem activating your Block Lab license.'; + $notice = 'There was a problem activating your Block Lab license.'; $this->instance->prepare_notice( $notice ); $this->assertEquals( array( $notice ), get_option( self::NOTICES_OPTION_NAME ) ); diff --git a/tests/php/admin/test-class-upgrade.php b/tests/php/unit/admin/test-class-upgrade.php similarity index 100% rename from tests/php/admin/test-class-upgrade.php rename to tests/php/unit/admin/test-class-upgrade.php diff --git a/tests/php/blocks/controls/test-class-checkbox.php b/tests/php/unit/blocks/controls/test-class-checkbox.php similarity index 100% rename from tests/php/blocks/controls/test-class-checkbox.php rename to tests/php/unit/blocks/controls/test-class-checkbox.php diff --git a/tests/php/blocks/controls/test-class-color.php b/tests/php/unit/blocks/controls/test-class-color.php similarity index 100% rename from tests/php/blocks/controls/test-class-color.php rename to tests/php/unit/blocks/controls/test-class-color.php diff --git a/tests/php/blocks/controls/test-class-control-abstract.php b/tests/php/unit/blocks/controls/test-class-control-abstract.php similarity index 99% rename from tests/php/blocks/controls/test-class-control-abstract.php rename to tests/php/unit/blocks/controls/test-class-control-abstract.php index 4010333ef..f8a843fc5 100644 --- a/tests/php/blocks/controls/test-class-control-abstract.php +++ b/tests/php/unit/blocks/controls/test-class-control-abstract.php @@ -163,7 +163,7 @@ public function test_render_select() { $options = array( 'foo' => 'One', 'bar' => 'Two', - 'baz' => 'Three' + 'baz' => 'Three', ); ob_start(); $this->instance->render_select( $this->setting, self::NAME, self::ID, $options ); diff --git a/tests/php/blocks/controls/test-class-control-setting.php b/tests/php/unit/blocks/controls/test-class-control-setting.php similarity index 95% rename from tests/php/blocks/controls/test-class-control-setting.php rename to tests/php/unit/blocks/controls/test-class-control-setting.php index dd0e112d9..76caf37ea 100644 --- a/tests/php/blocks/controls/test-class-control-setting.php +++ b/tests/php/unit/blocks/controls/test-class-control-setting.php @@ -25,7 +25,7 @@ class Test_Control_Setting extends \WP_UnitTestCase { * @covers \Block_Lab\Blocks\Controls\Control_Setting::__construct() */ public function test_construct() { - $this->instance = new Controls\Control_Setting( array() ); + $this->instance = new Controls\Control_Setting( array() ); $initial_property_values = array( 'name' => '', 'label' => '', @@ -91,7 +91,7 @@ public function test_construct() { * @covers \Block_Lab\Blocks\Controls\Control_Setting::get_value() */ public function get_value() { - $default = 'this is a default'; + $default = 'this is a default'; $this->instance = new Controls\Control_Setting( array( 'default' => $default ) ); // If the value is null, this should return the default. @@ -109,7 +109,7 @@ public function get_value() { $this->assertEquals( $expected_value, $this->instance->get_value() ); $int_expected_value = 5400; - $this->instance = new Controls\Control_Setting( + $this->instance = new Controls\Control_Setting( array( 'value' => $int_expected_value, 'default' => $default, diff --git a/tests/php/blocks/controls/test-class-email.php b/tests/php/unit/blocks/controls/test-class-email.php similarity index 100% rename from tests/php/blocks/controls/test-class-email.php rename to tests/php/unit/blocks/controls/test-class-email.php diff --git a/tests/php/blocks/controls/test-class-image.php b/tests/php/unit/blocks/controls/test-class-image.php similarity index 98% rename from tests/php/blocks/controls/test-class-image.php rename to tests/php/unit/blocks/controls/test-class-image.php index 5899dc83c..f84a8734d 100644 --- a/tests/php/blocks/controls/test-class-image.php +++ b/tests/php/unit/blocks/controls/test-class-image.php @@ -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', diff --git a/tests/php/blocks/controls/test-class-multiselect.php b/tests/php/unit/blocks/controls/test-class-multiselect.php similarity index 100% rename from tests/php/blocks/controls/test-class-multiselect.php rename to tests/php/unit/blocks/controls/test-class-multiselect.php diff --git a/tests/php/blocks/controls/test-class-number.php b/tests/php/unit/blocks/controls/test-class-number.php similarity index 100% rename from tests/php/blocks/controls/test-class-number.php rename to tests/php/unit/blocks/controls/test-class-number.php diff --git a/tests/php/blocks/controls/test-class-post.php b/tests/php/unit/blocks/controls/test-class-post.php similarity index 97% rename from tests/php/blocks/controls/test-class-post.php rename to tests/php/unit/blocks/controls/test-class-post.php index 6c75cbb55..ee71bb196 100644 --- a/tests/php/blocks/controls/test-class-post.php +++ b/tests/php/unit/blocks/controls/test-class-post.php @@ -106,7 +106,7 @@ public function test_render_settings_post_type_rest_slug() { $output = ob_get_clean(); $this->assertContains( $name, $output ); $this->assertContains( $id, $output ); - foreach( array( 'post', 'page' ) as $post_type ) { + foreach ( array( 'post', 'page' ) as $post_type ) { $post_type_object = get_post_type_object( $post_type ); $this->assertContains( $post_type_object->rest_base, $output ); } @@ -178,10 +178,12 @@ public function test_validate() { // If the 'post_title' is later changed, this block should output the new post title for block_field(). $updated_title = 'New Example Title'; - wp_update_post( array( - 'ID' => $valid_id, - 'post_title' => $updated_title, - ) ); + wp_update_post( + array( + 'ID' => $valid_id, + 'post_title' => $updated_title, + ) + ); $this->assertEquals( $updated_title, $this->instance->validate( array( 'id' => $valid_id ), true ) ); } } diff --git a/tests/php/blocks/controls/test-class-radio.php b/tests/php/unit/blocks/controls/test-class-radio.php similarity index 100% rename from tests/php/blocks/controls/test-class-radio.php rename to tests/php/unit/blocks/controls/test-class-radio.php diff --git a/tests/php/blocks/controls/test-class-range.php b/tests/php/unit/blocks/controls/test-class-range.php similarity index 100% rename from tests/php/blocks/controls/test-class-range.php rename to tests/php/unit/blocks/controls/test-class-range.php diff --git a/tests/php/blocks/controls/test-class-repeater.php b/tests/php/unit/blocks/controls/test-class-repeater.php similarity index 100% rename from tests/php/blocks/controls/test-class-repeater.php rename to tests/php/unit/blocks/controls/test-class-repeater.php diff --git a/tests/php/blocks/controls/test-class-rich-text.php b/tests/php/unit/blocks/controls/test-class-rich-text.php similarity index 100% rename from tests/php/blocks/controls/test-class-rich-text.php rename to tests/php/unit/blocks/controls/test-class-rich-text.php diff --git a/tests/php/blocks/controls/test-class-select.php b/tests/php/unit/blocks/controls/test-class-select.php similarity index 100% rename from tests/php/blocks/controls/test-class-select.php rename to tests/php/unit/blocks/controls/test-class-select.php diff --git a/tests/php/blocks/controls/test-class-taxonomy.php b/tests/php/unit/blocks/controls/test-class-taxonomy.php similarity index 94% rename from tests/php/blocks/controls/test-class-taxonomy.php rename to tests/php/unit/blocks/controls/test-class-taxonomy.php index b40c8c5e0..e2073b4e8 100644 --- a/tests/php/blocks/controls/test-class-taxonomy.php +++ b/tests/php/unit/blocks/controls/test-class-taxonomy.php @@ -107,7 +107,7 @@ public function test_render_settings_taxonomy_type_rest_slug() { $this->assertContains( $name, $output ); $this->assertContains( $id, $output ); - foreach( array( 'post_tag', 'category' ) as $post_type ) { + foreach ( array( 'post_tag', 'category' ) as $post_type ) { $taxonomy = get_taxonomy( $post_type ); $this->assertContains( $taxonomy->rest_base, $output ); $this->assertContains( $taxonomy->label, $output ); @@ -148,10 +148,10 @@ public function test_get_taxonomy_type_rest_slugs() { $this->assertEquals( array( - 'categories' => 'Categories', - 'tags' => 'Tags', - $rest_base => $new_tax_label, - $new_tax_slug_without_rest_base => $new_tax_label_without_rest_base, + 'categories' => 'Categories', + 'tags' => 'Tags', + $rest_base => $new_tax_label, + $new_tax_slug_without_rest_base => $new_tax_label_without_rest_base, ), $this->instance->get_taxonomy_type_rest_slugs() ); diff --git a/tests/php/blocks/controls/test-class-text.php b/tests/php/unit/blocks/controls/test-class-text.php similarity index 100% rename from tests/php/blocks/controls/test-class-text.php rename to tests/php/unit/blocks/controls/test-class-text.php diff --git a/tests/php/blocks/controls/test-class-textarea.php b/tests/php/unit/blocks/controls/test-class-textarea.php similarity index 100% rename from tests/php/blocks/controls/test-class-textarea.php rename to tests/php/unit/blocks/controls/test-class-textarea.php diff --git a/tests/php/blocks/controls/test-class-toggle.php b/tests/php/unit/blocks/controls/test-class-toggle.php similarity index 100% rename from tests/php/blocks/controls/test-class-toggle.php rename to tests/php/unit/blocks/controls/test-class-toggle.php diff --git a/tests/php/blocks/controls/test-class-url.php b/tests/php/unit/blocks/controls/test-class-url.php similarity index 100% rename from tests/php/blocks/controls/test-class-url.php rename to tests/php/unit/blocks/controls/test-class-url.php diff --git a/tests/php/blocks/controls/test-class-user.php b/tests/php/unit/blocks/controls/test-class-user.php similarity index 100% rename from tests/php/blocks/controls/test-class-user.php rename to tests/php/unit/blocks/controls/test-class-user.php diff --git a/tests/php/blocks/test-class-block.php b/tests/php/unit/blocks/test-class-block.php similarity index 91% rename from tests/php/blocks/test-class-block.php rename to tests/php/unit/blocks/test-class-block.php index 61889f0a2..530a2e8f3 100644 --- a/tests/php/blocks/test-class-block.php +++ b/tests/php/unit/blocks/test-class-block.php @@ -12,6 +12,13 @@ */ class Test_Block extends \WP_UnitTestCase { + /** + * The instance to test. + * + * @var Blocks\Block + */ + public $instance; + /** * A mock JSON blob for the block. * @@ -94,9 +101,9 @@ public function setUp() { $post = $this->factory()->post->create( array( - 'post_title' => 'Simple Test Block', - 'post_name' => 'simple-test-block', - 'post_type' => 'block_lab', + 'post_title' => 'Simple Test Block', + 'post_name' => 'simple-test-block', + 'post_type' => 'block_lab', ) ); @@ -120,7 +127,7 @@ public function test_construct() { public function test_from_json() { $this->instance->from_json( self::JSON ); - // Check all the base attributes + // Check all the base attributes. $this->assertEquals( 'Simple Test Block', $this->instance->title ); $this->assertEquals( 'block_lab', $this->instance->icon ); $this->assertEquals( @@ -133,13 +140,13 @@ public function test_from_json() { ); $this->assertEquals( array( 'keywords', 'go', 'here' ), $this->instance->keywords ); - // Check that we've got three fields + // Check that we've got three fields. $this->assertCount( 3, $this->instance->fields ); $this->assertArrayHasKey( 'heading', $this->instance->fields ); $this->assertArrayHasKey( 'content', $this->instance->fields ); $this->assertArrayHasKey( 'parent', $this->instance->fields ); - // Check that the repeater works as expected + // Check that the repeater works as expected. $this->assertAttributeNotEmpty( 'settings', $this->instance->fields['parent'] ); $this->assertArrayHasKey( 'sub_fields', $this->instance->fields['parent']->settings ); $this->assertArrayHasKey( 'child', $this->instance->fields['parent']->settings['sub_fields'] ); @@ -161,7 +168,7 @@ public function test_to_json() { $decoded = json_decode( $json, true ); $this->assertArrayHasKey( 'block-lab/simple-test-block', $decoded ); - // Check all the base attributes + // Check all the base attributes. $block = $decoded['block-lab/simple-test-block']; $this->assertArrayHasKey( 'name', $block ); $this->assertArrayHasKey( 'title', $block ); @@ -170,13 +177,13 @@ public function test_to_json() { $this->assertArrayHasKey( 'keywords', $block ); $this->assertArrayHasKey( 'fields', $block ); - // Check that we've got three fields + // Check that we've got three fields. $this->assertCount( 3, $block['fields'] ); $this->assertArrayHasKey( 'heading', $block['fields'] ); $this->assertArrayHasKey( 'content', $block['fields'] ); $this->assertArrayHasKey( 'parent', $block['fields'] ); - // Check that the repeater works as expected + // Check that the repeater works as expected. $this->assertArrayHasKey( 'sub_fields', $block['fields']['parent'] ); $this->assertArrayHasKey( 'child', $block['fields']['parent']['sub_fields'] ); $this->assertArrayHasKey( 'name', $block['fields']['parent']['sub_fields']['child'] ); diff --git a/tests/php/blocks/test-class-field.php b/tests/php/unit/blocks/test-class-field.php similarity index 96% rename from tests/php/blocks/test-class-field.php rename to tests/php/unit/blocks/test-class-field.php index 4b0b4d02f..3f3c68ea1 100644 --- a/tests/php/blocks/test-class-field.php +++ b/tests/php/unit/blocks/test-class-field.php @@ -12,6 +12,13 @@ */ class Test_Field extends \WP_UnitTestCase { + /** + * The instance to test. + * + * @var Blocks\Field + */ + public $instance; + /** * A mock config array for the field. * @@ -32,10 +39,10 @@ class Test_Field extends \WP_UnitTestCase { 'control' => 'text', 'type' => 'string', 'order' => 0, - 'parent' => 'foo' - ) - ) - ) + 'parent' => 'foo', + ), + ), + ), ); /** diff --git a/tests/php/blocks/test-class-loader.php b/tests/php/unit/blocks/test-class-loader.php similarity index 96% rename from tests/php/blocks/test-class-loader.php rename to tests/php/unit/blocks/test-class-loader.php index e00dc10be..9a86acf2c 100644 --- a/tests/php/blocks/test-class-loader.php +++ b/tests/php/unit/blocks/test-class-loader.php @@ -11,6 +11,14 @@ * Tests for class Loader. */ class Test_Loader extends Abstract_Template { + + /** + * The instance to test. + * + * @var Blocks\Loader + */ + public $instance; + /** * Test register_hooks. * @@ -59,7 +67,7 @@ function( $block ) use ( $block_name, $slug, $script_url ) { add_action( "block_lab_render_template_{$block_name}", - function( $block ) use ( $block_name, $slug, $script_url ) { + function() use ( $block_name, $slug, $script_url ) { wp_enqueue_script( $slug, $script_url, array(), '0.1', true ); } ); @@ -79,8 +87,8 @@ function( $block ) use ( $block_name, $slug, $script_url ) { * @covers \Block_Lab\Blocks\Loader::enqueue_block_styles() */ public function test_enqueue_block_styles() { - $wp_styles = wp_styles(); - $block_handle = "block-lab__block-{$this->mock_block_name}"; + $wp_styles = wp_styles(); + $block_handle = "block-lab__block-{$this->mock_block_name}"; // Check that the correct stylesheet is enqueued. foreach ( $this->get_template_css_paths() as $key => $file ) { @@ -166,7 +174,7 @@ public function test_get_block_attributes() { ); // Repeater sub-fields should not be returned, as they're not added as block attributes. - $actual_attributes = $this->instance->get_block_attributes( $block ); + $actual_attributes = $this->instance->get_block_attributes( $block ); $this->assertEquals( $expected_attributes, $actual_attributes ); } @@ -275,7 +283,7 @@ public function test_block_template() { // Test that this filter changes the template used. add_filter( 'block_lab_override_theme_template', - function( $directory ) use( $overridden_theme_template_path ) { + function( $directory ) use ( $overridden_theme_template_path ) { unset( $directory ); return $overridden_theme_template_path; } diff --git a/tests/php/unit/blocks/test-class-loop.php b/tests/php/unit/blocks/test-class-loop.php new file mode 100644 index 000000000..fac708e50 --- /dev/null +++ b/tests/php/unit/blocks/test-class-loop.php @@ -0,0 +1,116 @@ +instance = new Blocks\Loop(); + } + + /** + * Test set_active. + * + * @covers \Block_Lab\Blocks\Loop::set_active() + */ + public function test_set_active() { + $this->assertEquals( null, $this->instance->active ); + + $loop_name = 'my-repeater'; + $this->instance->set_active( $loop_name ); + $this->assertEquals( $loop_name, $this->instance->active ); + + $new_loop_name = 'different-repeater'; + $this->instance->set_active( $new_loop_name ); + + // Calling set_active() should overwrite the previous active loop value. + $this->assertEquals( $new_loop_name, $this->instance->active ); + } + + /** + * Test get_row. + * + * @covers \Block_Lab\Blocks\Loop::get_row() + */ + public function test_get_row() { + // If there is no loop set, this should return false. + $this->assertFalse( $this->instance->get_row() ); + $this->assertFalse( $this->instance->get_row( 'random-row-name' ) ); + + // When the row has a pointer, this should return it. + $row_name = 'example-row-name'; + $pointer = 2; + $this->instance->loops[ $row_name ] = $pointer; + $this->assertEquals( $pointer, $this->instance->get_row( $row_name ) ); + + // If there is an active row, it shouldn't be necessary to pass that row to get_row() as an argument. + $this->instance->active = $row_name; + $this->assertEquals( $pointer, $this->instance->get_row() ); + + // If the passed row name doesn't exist, this should return false. + $nonexistent_row_name = 'random-row'; + $this->assertEquals( false, $this->instance->get_row( $nonexistent_row_name ) ); + } + + /** + * Test increment. + * + * @covers \Block_Lab\Blocks\Loop::increment() + */ + public function test_increment() { + // Calling increment() for the first time for a row should return 0. + $row_name = 'example-row-name'; + $this->assertEquals( 0, $this->instance->increment( $row_name ) ); + + // Calling it again should increment it by 1. + $this->assertEquals( 1, $this->instance->increment( $row_name ) ); + $this->assertEquals( 2, $this->instance->increment( $row_name ) ); + + // If the row name is the active row, it shouldn't be necessary to pass it to increment() as an argument. + $this->instance->active = $row_name; + $this->assertEquals( 3, $this->instance->increment() ); + } + + /** + * Test reset. + * + * @covers \Block_Lab\Blocks\Loop::reset() + */ + public function test_reset() { + $row_name = 'example-row-name'; + $pointer = 2; + $this->instance->loops[ $row_name ] = $pointer; + + $this->instance->reset( $row_name ); + $this->assertFalse( isset( $this->instance->loops[ $row_name ] ) ); + + $this->instance->loops[ $row_name ] = $pointer; + $this->instance->active = $row_name; + + // If the row is the active row, it should be necessary to pass it to reset() as an argument. + $this->instance->reset(); + $this->assertFalse( isset( $this->instance->loops[ $row_name ] ) ); + } +} diff --git a/tests/fixtures/mock-import-invalid-format.txt b/tests/php/unit/fixtures/mock-import-invalid-format.txt similarity index 100% rename from tests/fixtures/mock-import-invalid-format.txt rename to tests/php/unit/fixtures/mock-import-invalid-format.txt diff --git a/tests/fixtures/mock-import-valid-format.txt b/tests/php/unit/fixtures/mock-import-valid-format.txt similarity index 100% rename from tests/fixtures/mock-import-valid-format.txt rename to tests/php/unit/fixtures/mock-import-valid-format.txt diff --git a/tests/php/helpers/class-abstract-template.php b/tests/php/unit/helpers/class-abstract-template.php similarity index 94% rename from tests/php/helpers/class-abstract-template.php rename to tests/php/unit/helpers/class-abstract-template.php index 942634d64..ac2931c65 100644 --- a/tests/php/helpers/class-abstract-template.php +++ b/tests/php/unit/helpers/class-abstract-template.php @@ -89,8 +89,6 @@ function( $directory ) { /** * Gets the directories that block templates and CSS files could be in. - * - * @return array */ public function create_block_template_directories() { array_map( @@ -122,7 +120,7 @@ function( $template_location ) { /** * Creates a directory, and stores the directory in order to later remove it in tearDown(). * - * @param string $directory The directory to creat. + * @param string $directory The directory to create. */ public function mkdir( $directory ) { if ( ! is_dir( $directory ) ) { @@ -138,7 +136,7 @@ public function mkdir( $directory ) { * @param string $contents The contents of the file. */ public function file_put_contents( $file, $contents ) { - file_put_contents( $file, $contents ); + file_put_contents( $file, $contents ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents array_push( $this->files_created, $file ); } } diff --git a/tests/php/helpers/trait-testing-helper.php b/tests/php/unit/helpers/trait-testing-helper.php similarity index 94% rename from tests/php/helpers/trait-testing-helper.php rename to tests/php/unit/helpers/trait-testing-helper.php index 411faa5c5..a355b4938 100644 --- a/tests/php/helpers/trait-testing-helper.php +++ b/tests/php/unit/helpers/trait-testing-helper.php @@ -25,7 +25,7 @@ public function assert_correct_settings( $expected_settings, $actual_settings ) $expected_setting = $expected_settings[ $settings_index ]; foreach ( $setting as $setting_key => $setting_value ) { unset( $setting_value ); - $this->assertEquals( $expected_setting[ $setting_key ], $setting->$setting_key ); + $this->assertEquals( $expected_setting[ $setting_key ], $setting->$setting_key ); $this->assertEquals( 'Block_Lab\Blocks\Controls\Control_Setting', get_class( $setting ) ); } } diff --git a/tests/php/post-types/test-class-block-post.php b/tests/php/unit/post-types/test-class-block-post.php similarity index 100% rename from tests/php/post-types/test-class-block-post.php rename to tests/php/unit/post-types/test-class-block-post.php diff --git a/tests/php/unit/test-block-lab.php b/tests/php/unit/test-block-lab.php new file mode 100644 index 000000000..f2f2f3739 --- /dev/null +++ b/tests/php/unit/test-block-lab.php @@ -0,0 +1,72 @@ +assertContains( '

', ob_get_clean() ); + } + + /** + * Test block_lab_php_version_text(). + * + * @covers \block_lab_php_version_text() + */ + public function test_block_lab_php_version_text() { + $this->assertContains( 'Block Lab plugin error:', block_lab_php_version_text() ); + } + + /** + * Test block_lab_wp_version_error(). + * + * @covers \block_lab_wp_version_error() + */ + public function test_block_lab_wp_version_error() { + ob_start(); + block_lab_wp_version_error(); + + $this->assertEquals( + '

Block Lab plugin error: Your version of WordPress is too old. You must be running WordPress 5.0 to use Block Lab.

', + ob_get_clean() + ); + } + + /** + * Test block_lab_wp_version_text(). + * + * @covers \block_lab_wp_version_text() + */ + public function test_block_lab_wp_version_text() { + $this->assertEquals( + 'Block Lab plugin error: Your version of WordPress is too old. You must be running WordPress 5.0 to use Block Lab.', + block_lab_wp_version_text() + ); + } + + /** + * Test block_lab(). + * + * @covers \block_lab() + */ + public function test_singleton() { + $this->assertEquals( 'Block_Lab\\Plugin', get_class( block_lab() ) ); + + // Calling block_lab() twice should return the same instance. + $this->assertEquals( block_lab(), block_lab() ); + } +} diff --git a/tests/php/test-class-plugin.php b/tests/php/unit/test-class-plugin.php similarity index 89% rename from tests/php/test-class-plugin.php rename to tests/php/unit/test-class-plugin.php index 7a157badc..db41805da 100644 --- a/tests/php/test-class-plugin.php +++ b/tests/php/unit/test-class-plugin.php @@ -34,7 +34,7 @@ public function setUp() { /** * Test init. * - * @covers \Block_Lab\Abstract_Plugin:init() + * @covers \Block_Lab\Plugin::init() */ public function test_init() { $plugin_instance = new Block_Lab\Plugin(); @@ -68,7 +68,7 @@ public function test_plugin_loaded() { * This method, is_pro(), is called in the Plugin class. * So this ensures that the magic method refers the call to the Util class. * - * @covers \Block_Lab\Blocks\Util::is_pro() + * @covers \Block_Lab\Util::is_pro() */ public function test_is_pro() { $this->instance->admin = new Block_Lab\Admin\Admin(); @@ -86,15 +86,15 @@ public function test_is_pro() { * This is also essentially the same test as in Test_Util. * But this also tests that the __call() magic method in Plugin works. * - * @covers \Block_Lab\Blocks\Util::get_template_locations() + * @covers \Block_Lab\Util::get_template_locations() */ public function test_get_template_locations() { $name = 'foo-baz'; $this->assertEquals( array( - "blocks/foo-baz/block.php", - "blocks/block-foo-baz.php", - "blocks/block.php", + 'blocks/foo-baz/block.php', + 'blocks/block-foo-baz.php', + 'blocks/block.php', ), $this->instance->get_template_locations( $name ) ); diff --git a/tests/php/test-class-util.php b/tests/php/unit/test-class-util.php similarity index 86% rename from tests/php/test-class-util.php rename to tests/php/unit/test-class-util.php index 0c5f8f1ee..b68aff29e 100644 --- a/tests/php/test-class-util.php +++ b/tests/php/unit/test-class-util.php @@ -68,6 +68,18 @@ public function test_is_pro() { $this->assertFalse( block_lab()->is_pro() ); } + /** + * Test loop. + * + * @covers \Block_Lab\Util::loop() + */ + public function test_loop() { + $this->assertEquals( 'Block_Lab\\Blocks\Loop', get_class( $this->instance->loop() ) ); + + // Calling this singleton function repeatedly should return the same instance of the Loop. + $this->assertEquals( $this->instance->loop(), $this->instance->loop() ); + } + /** * Test get_template_locations. * @@ -77,9 +89,9 @@ public function test_get_template_locations() { $name = 'foo-baz'; $this->assertEquals( array( - "blocks/foo-baz/block.php", - "blocks/block-foo-baz.php", - "blocks/block.php", + 'blocks/foo-baz/block.php', + 'blocks/block-foo-baz.php', + 'blocks/block.php', ), $this->instance->get_template_locations( $name ) ); @@ -88,9 +100,9 @@ public function test_get_template_locations() { $type = 'another-type'; $this->assertEquals( array( - "blocks/example/another-type.php", - "blocks/another-type-example.php", - "blocks/another-type.php", + 'blocks/example/another-type.php', + 'blocks/another-type-example.php', + 'blocks/another-type.php', ), $this->instance->get_template_locations( $name, $type ) ); @@ -105,9 +117,9 @@ public function test_get_stylesheet_locations() { $name = 'foo-baz'; $this->assertEquals( array( - "blocks/foo-baz/block.css", - "blocks/css/block-foo-baz.css", - "blocks/block-foo-baz.css", + 'blocks/foo-baz/block.css', + 'blocks/css/block-foo-baz.css', + 'blocks/block-foo-baz.css', ), $this->instance->get_stylesheet_locations( $name ) ); @@ -116,9 +128,9 @@ public function test_get_stylesheet_locations() { $type = 'another-type'; $this->assertEquals( array( - "blocks/example/another-type.css", - "blocks/css/another-type-example.css", - "blocks/another-type-example.css", + 'blocks/example/another-type.css', + 'blocks/css/another-type-example.css', + 'blocks/another-type-example.css', ), $this->instance->get_stylesheet_locations( $name, $type ) ); @@ -147,7 +159,7 @@ public function test_locate_template() { $this->assertEquals( $template_location, $this->instance->locate_template( $templates ) ); // When passing a second argument of a path that doesn't exist, the result should be the same. - $this->assertEquals( $template_location, $this->instance->locate_template( $templates, $non_existent_alternate_path )); + $this->assertEquals( $template_location, $this->instance->locate_template( $templates, $non_existent_alternate_path ) ); // With the $single argument of false, this should return an array() that contains this $template_location. $this->assertTrue( in_array( $template_location, $this->instance->locate_template( $templates, '', false ), true ) ); @@ -204,7 +216,7 @@ public function test_get_icons() { add_filter( 'block_lab_icons', - function( $icons ) use( $additional_icon, $icon_name ) { + function( $icons ) use ( $additional_icon, $icon_name ) { $icons[ $icon_name ] = $additional_icon; return $icons; } @@ -273,4 +285,15 @@ public function test_get_post_type_slug() { // It should also be possible to call this via a magic method of the Plugin class. $this->assertEquals( 'block_lab', block_lab()->get_post_type_slug() ); } + + /** + * Test get_url_from_path. + * + * @covers \Block_Lab\Util::get_url_from_path() + */ + public function test_get_url_from_path() { + $subdirectory_path = 'wp-content/theme/blocks/test-block-here.css'; + $path = ABSPATH . $subdirectory_path; + $this->assertEquals( '/' . $subdirectory_path, $this->instance->get_url_from_path( $path ) ); + } } diff --git a/tests/php/unit/test-helpers.php b/tests/php/unit/test-helpers.php new file mode 100644 index 000000000..9d9cdc7fa --- /dev/null +++ b/tests/php/unit/test-helpers.php @@ -0,0 +1,70 @@ + 'text' ); + $block_config = array( + 'fields' => array( + $field_name => $field_config, + ), + ); + + $block_lab_config = new Blocks\Block(); + $block_lab_config->from_array( $block_config ); + + // Because block_field() had the second argument of false, this should return the value stored in the field, not echo it. + ob_start(); + $return_value = block_field( $field_name, false ); + $echoed = ob_get_clean(); + $this->assertEquals( $mock_text, $return_value ); + $this->assertEmpty( $echoed ); + + // Test the same scenario as above, but for 'className'. + ob_start(); + $return_value = block_field( $class_key, false ); + $echoed = ob_get_clean(); + $this->assertEquals( $expected_class, $return_value ); + $this->assertEmpty( $echoed ); + + ob_start(); + $return_value = block_field( $field_name, true ); + $actual_user_login = ob_get_clean(); + + // Because block_field() has a second argument of true, this should echo the user login and return it. + $this->assertEquals( $mock_text, $actual_user_login ); + $this->assertEquals( $return_value, $actual_user_login ); + + ob_start(); + $return_value = block_field( $class_key, true ); + $actual_class = ob_get_clean(); + + // Test the same scenario as above, but for 'className'. + $this->assertEquals( $expected_class, $actual_class ); + $this->assertEquals( $return_value, $actual_class ); + } +} diff --git a/tests/wp-tests-config.php b/tests/wp-tests-config.php index 18066ec48..50bbfa005 100644 --- a/tests/wp-tests-config.php +++ b/tests/wp-tests-config.php @@ -28,7 +28,7 @@ define( 'DB_COLLATE', '' ); // Only numbers, letters, and underscores please! -$table_prefix = 'wptests_'; +$table_prefix = 'wptests_'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited define( 'WP_TESTS_DOMAIN', 'example.org' ); define( 'WP_TESTS_EMAIL', 'admin@example.org' );