-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added unit test for cover block render function (#42108)
* 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
Showing
1 changed file
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
|
||
} | ||
} |