diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 4f407266e5c0f5..c8dc2d34b0fed9 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -14,6 +14,7 @@ ./phpunit/ ./phpunit/tests/ + ./phpunit/blocks/ diff --git a/phpunit/blocks/renderBlockCorePostExcerpt.php b/phpunit/blocks/renderBlockCorePostExcerpt.php new file mode 100644 index 00000000000000..38c27bfde9b30a --- /dev/null +++ b/phpunit/blocks/renderBlockCorePostExcerpt.php @@ -0,0 +1,147 @@ +post->create_and_get( + array( + 'post_title' => 'Post Expert block Unit Test', + 'post_excerpt' => 'Post Expert content', + ) + ); + + self::$attributes = array( + 'moreText' => '', + 'excerptLength' => 55, + ); + + $block = array( + 'blockName' => 'core/post-excerpt', + 'attrs' => array( + 'moreText' => '', + ), + 'innerBlock' => array(), + 'innerContent' => array(), + 'innerHTML' => array(), + ); + + WP_Block_Supports::init(); + WP_Block_Supports::$block_to_render = $block; + } + + /** + * Tear down method. + */ + public static function wpTearDownAfterClass() { + wp_delete_post( self::$post->ID, true ); + } + + /** + * Test gutenberg_render_block_core_post_excerpt() method + * with empty data. + */ + public function test_should_render_empty_string_when_excerpt_is_empty() { + $block = new stdClass(); + + // call render method with block context. + $rendered = gutenberg_render_block_core_post_excerpt( self::$attributes, '', $block ); + $this->assertSame( '', $rendered, 'Failed to assert that $rendered is an empty string.' ); + } + + /** + * Test gutenberg_render_block_core_post_excerpt() method. + */ + public function test_should_render_correct_exceprt() { + + $block = new stdClass(); + $GLOBALS['post'] = self::$post; + $block->context = array( 'postId' => self::$post->ID ); + + $rendered = gutenberg_render_block_core_post_excerpt( self::$attributes, '', $block ); + $this->assertNotEmpty( $rendered, 'Failed to assert that $rendered is not empty.' ); + $this->assertStringContainsString( + 'Post Expert content', + $rendered, + 'Failed to assert that $rendered contain the expected string.' + ); + $this->assertStringContainsString( + 'assertStringContainsString( + 'wp-block-post-excerpt__excerpt', + $rendered, + 'Failed to assert that $rendered contain the "wp-block-post-excerpt__excerpt" string.' + ); + $this->assertStringNotContainsString( + 'has-text-align', + $rendered, + 'Failed to assert that $rendered does not contain the has-text-align class.' + ); + + self::$attributes['textAlign'] = 'left'; + + $rendered = gutenberg_render_block_core_post_excerpt( self::$attributes, '', $block ); + $this->assertStringContainsString( + 'has-text-align-left', + $rendered, + 'Failed to assert that $rendered contains the "has-text-align-left" class.' + ); + + self::$attributes = array( + 'moreText' => 'Read More', + 'excerptLength' => 55, + ); + + $rendered = gutenberg_render_block_core_post_excerpt( self::$attributes, '', $block ); + $this->assertStringContainsString( + 'wp-block-post-excerpt__more-link', + $rendered, + 'Failed to assert that $rendered contains the expected string.' + ); + + self::$attributes = array( + 'moreText' => 'Read More', + 'showMoreOnNewLine' => true, + ); + $this->assertStringContainsString( + 'wp-block-post-excerpt__more-link', + $rendered, + 'Failed to assert that $rendered contains the expected string.' + ); + $this->assertStringContainsString( + get_permalink( self::$post->ID ), + $rendered, + 'Failed to assert that $rendered contain expected post the expected post URL.' + ); + } +}