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 all commits
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
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 );

}
}