From 2e4a4c2c71170468bdc39b790c5226fbe0755fcf Mon Sep 17 00:00:00 2001 From: Sunil Prajapati <61308756+sunil25393@users.noreply.github.com> Date: Mon, 25 Jul 2022 19:07:34 +0530 Subject: [PATCH] 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 --- phpunit/blocks/render-block-cover-test.php | 133 +++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 phpunit/blocks/render-block-cover-test.php diff --git a/phpunit/blocks/render-block-cover-test.php b/phpunit/blocks/render-block-cover-test.php new file mode 100644 index 00000000000000..c9da23f655494a --- /dev/null +++ b/phpunit/blocks/render-block-cover-test.php @@ -0,0 +1,133 @@ +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 = '
'; + $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 = '
'; + $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 ); + + } +}