Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added unit test for cover block render function #42108

Merged
merged 6 commits into from
Jul 25, 2022
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
108 changes: 108 additions & 0 deletions phpunit/blocks/render-block-cover-test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?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 .
akasunil marked this conversation as resolved.
Show resolved Hide resolved
*
* @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 render_block_core_cover() method.
*
* @covers ::render_block_core_cover
*/
public function test_render_block_core_latest_posts() {
akasunil marked this conversation as resolved.
Show resolved Hide resolved

global $wp_query;

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

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

$content = '<div class="wp-block-cover"><span></span><div class="wp-block-cover__inner-container"></div></div>';

$wp_query->post = self::$post;
$wp_query->posts = array( self::$post );

$attributes['minHeight'] = '100px';
draganescu marked this conversation as resolved.
Show resolved Hide resolved
$rendered = 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 );

$attributes['hasParallax'] = false;
$attributes['isRepeated'] = false;
$attributes['focalPoint'] = array(
'x' => 10,
'y' => 10,
);

$rendered = render_block_core_cover( $attributes, $content );

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

$attributes['backgroundType'] = 'color';
$rendered = render_block_core_cover( $attributes, '' );
$this->assertEmpty( $rendered );

$attributes['backgroundType'] = 'image';
$attributes['useFeaturedImage'] = false;
$rendered = render_block_core_cover( $attributes, '' );
$this->assertEmpty( $rendered );
}
}