Skip to content

Commit

Permalink
Fix PHP unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
ravichdev committed Jun 4, 2020
1 parent a0874ac commit 66854a3
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions tests/phpunit/php/class-test-block-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ class Test_Block_Type extends \WP_UnitTestCase {
*/
public static function wpSetUpBeforeClass( $factory ) {
self::$attachment_id = $factory->attachment->create_object(
'canola.jpg',
DIR_TESTDATA . '/images/canola.jpg',
0,
[
'post_mime_type' => 'image/jpeg',
'post_excerpt' => 'A sample caption',
]
);

wp_maybe_generate_attachment_metadata( get_post( self::$attachment_id ) );

update_post_meta( self::$attachment_id, 'original_url', 'https://images.unsplash.com/test.jpg' );
update_post_meta( self::$attachment_id, 'original_link', 'https://www.unsplash.com/foo' );
}
Expand Down Expand Up @@ -95,7 +97,7 @@ public function test_register_blocks() {
}

/**
* Test register_blocks when block.json does not exist.git
* Test register_blocks when block.json does not exist.
*
* @see Block_Type::register_blocks()
*/
Expand All @@ -109,13 +111,13 @@ public function test_register_blocks_no_file() {

$block_folder = get_plugin_instance()->dir_path . '/assets/js/blocks/image/';

rename( $block_folder . 'block.json', $block_folder . 'block' ); // phpcs:ignore
rename( $block_folder . 'block.json', $block_folder . 'block' ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.file_ops_rename

$this->block_type->register_blocks();

$blocks = \WP_Block_Type_Registry::get_instance()->get_all_registered();

rename( $block_folder . 'block', $block_folder . 'block.json' ); // phpcs:ignore
rename( $block_folder . 'block', $block_folder . 'block.json' ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.file_ops_rename

// Assert the block is not registered.
$this->assertFalse( array_key_exists( 'unsplash/image', $blocks ) );
Expand Down Expand Up @@ -182,6 +184,17 @@ public function test_render_image_block() {
);
$this->assertContains( 'class="wp-block-unsplash-image wp-block-image size-medium is-resized"', $content );

$content = $this->block_type->render_image_block(
[
'id' => self::$attachment_id,
'sizeSlug' => 'medium',
],
$default
);

$this->assertContains( 'class="wp-block-unsplash-image wp-block-image size-medium"', $content );
$this->assertContains( 'width="300" height="225"', $content );

// Assert we handle invalid attachment ID and sizeSlug default.
$content = $this->block_type->render_image_block(
[
Expand Down

0 comments on commit 66854a3

Please sign in to comment.