-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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 post excerpt block render function #43451
Merged
draganescu
merged 17 commits into
WordPress:trunk
from
akasunil:add-test-for-post-excerpt-block-render
May 24, 2024
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
731a3d0
Added unittest for post excerpt block render function
akasunil e01cf0f
Merge branch 'WordPress:trunk' into add-test-for-post-excerpt-block-r…
akasunil c40b9a6
Merge branch 'WordPress:trunk' into add-test-for-post-excerpt-block-r…
akasunil d60295d
Merge branch 'WordPress:trunk' into add-test-for-post-excerpt-block-r…
akasunil d90e259
Merge branch 'WordPress:trunk' into add-test-for-post-excerpt-block-r…
akasunil 4f4d8f3
Updated wpSetUpBeforeClass method with argument
akasunil 37055e3
Merge branch 'WordPress:trunk' into add-test-for-post-excerpt-block-r…
akasunil 3f70944
Addressed feedbacks
akasunil d9ac6f6
Merge branch 'trunk' of personal.github.com:sunil25393/gutenberg into…
akasunil 9d5e984
fix unit test failure
akasunil ebd1fdf
Fix coding standards
akasunil c9b2f0f
Fix phpcs issue
akasunil 876966b
Merge branch 'trunk' of personal.github.com:sunil25393/gutenberg into…
akasunil 9cbe351
Merge branch 'trunk' of personal.github.com:sunil25393/gutenberg into…
akasunil ffefb68
Update config to allow files with new name standard
akasunil 4dd1212
Change test file name to camel case
akasunil c1b8d75
Update assertion comments and formatting
akasunil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,147 @@ | ||
<?php | ||
/** | ||
* Tests for the gutenberg_render_block_core_post_excerpt() function. | ||
* | ||
* @package WordPress | ||
* @subpackage Blocks | ||
* | ||
* @covers ::gutenberg_render_block_core_post_excerpt | ||
* @group blocks | ||
*/ | ||
class Tests_Blocks_RenderBlockCorePostExcerpt extends WP_UnitTestCase { | ||
|
||
/** | ||
* Post object with data. | ||
* | ||
* @var array | ||
*/ | ||
protected static $post; | ||
|
||
/** | ||
* Array of Attributes. | ||
* | ||
* @var int | ||
*/ | ||
protected static $attributes; | ||
|
||
/** | ||
* Setup method. | ||
* | ||
* @param WP_UnitTest_Factory $factory Helper that lets us create fake data. | ||
*/ | ||
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { | ||
|
||
self::$post = $factory->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( | ||
'</p', | ||
$rendered, | ||
'Failed to assert that $rendered contains a closing html paragraph tag.' | ||
); | ||
$this->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.' | ||
); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.