Skip to content

Commit

Permalink
Added unit test for cover block render function (#42108)
Browse files Browse the repository at this point in the history
* Added unit test for cover block render function

* Split testing function into two and addressed feedbacks

* Fixed linting errors

* Resolved issue of unittest failing
  • Loading branch information
akasunil authored Jul 25, 2022
1 parent 7fd28e4 commit 2e4a4c2
Showing 1 changed file with 133 additions and 0 deletions.
133 changes: 133 additions & 0 deletions phpunit/blocks/render-block-cover-test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php
/**
* Cover block rendering tests.
*
* @package WordPress
* @subpackage Blocks
*/

/**
* Tests for the Cover block.
*
* @group blocks
*/
class Tests_Blocks_Render_Cover extends WP_UnitTestCase {
/**
* Post object.
*
* @var array
*/
protected static $post;

/**
* Attachment id.
*
* @var int
*/
protected static $attachment_id;

/**
* Setup method.
*/
public static function wpSetUpBeforeClass() {
self::$post = self::factory()->post->create_and_get();
$file = DIR_TESTDATA . '/images/canola.jpg';

self::$attachment_id = self::factory()->attachment->create_upload_object(
$file,
self::$post->ID,
array(
'post_mime_type' => 'image/jpeg',
)
);

set_post_thumbnail( self::$post, self::$attachment_id );

}

/**
* Tear down method.
*/
public static function wpTearDownAfterClass() {
wp_delete_post( self::$post->ID, true );
wp_delete_post( self::$attachment_id, true );
}

/**
* Test gutenberg_render_block_core_cover() method.
*
* @covers ::gutenberg_render_block_core_cover
*/
public function test_gutenberg_render_block_core_cover() {

global $wp_query;

// Fake being in the loop.
$wp_query->in_the_loop = true;
$wp_query->post = self::$post;

$wp_query->posts = array( self::$post );
$GLOBALS['post'] = self::$post;

$attributes = array(
'useFeaturedImage' => true,
'backgroundType' => 'image',
'hasParallax' => true,
'isRepeated' => true,
'minHeight' => '100px',
);

$content = '<div class="wp-block-cover"><span></span><div class="wp-block-cover__inner-container"></div></div>';
$rendered = gutenberg_render_block_core_cover( $attributes, $content );

$this->assertStringContainsString( wp_get_attachment_image_url( self::$attachment_id, 'full' ), $rendered );
$this->assertStringContainsString( 'background-image', $rendered );
$this->assertStringContainsString( 'min-height', $rendered );

// If cover background type is not image.
$attributes['backgroundType'] = 'color';
$rendered = gutenberg_render_block_core_cover( $attributes, '' );
$this->assertEmpty( $rendered );

// If cover background is not post featured image.
$attributes['backgroundType'] = 'image';
$attributes['useFeaturedImage'] = false;
$rendered = gutenberg_render_block_core_cover( $attributes, '' );
$this->assertEmpty( $rendered );
}

/**
* Test gutenberg_render_block_core_cover() method.
*
* @covers ::gutenberg_render_block_core_cover
*/
public function test_gutenberg_render_block_core_cover_fixed_or_repeated_background() {

global $wp_query;

// Fake being in the loop.
$wp_query->post = self::$post;
$GLOBALS['post'] = self::$post;

$attributes = array(
'useFeaturedImage' => true,
'backgroundType' => 'image',
'hasParallax' => false,
'isRepeated' => false,
'minHeight' => '100px',
'focalPoint' => array(
'x' => 10,
'y' => 10,
),
);

$content = '<div class="wp-block-cover"><span></span><div class="wp-block-cover__inner-container"></div></div>';
$rendered = gutenberg_render_block_core_cover( $attributes, $content );

$this->assertStringContainsString( wp_get_attachment_image_url( self::$attachment_id, 'full' ), $rendered );
$this->assertStringContainsString( 'object-position', $rendered );
$this->assertStringNotContainsString( 'background-image', $rendered );
$this->assertStringNotContainsString( 'min-height', $rendered );

}
}

0 comments on commit 2e4a4c2

Please sign in to comment.