-
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
Add unit tests for the gutenberg_render_block_core_post_title() function #42435
Add unit tests for the gutenberg_render_block_core_post_title() function #42435
Conversation
@draganescu Can you please review this PR ? |
waiting for PR review |
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.
Tested the test and the test passes and it's a great addition. Thank you 👏🏻
Thank you @draganescu. @Mamaduka can we merge this PR ? |
public static function wpSetUpBeforeClass() { | ||
self::$post = self::factory()->post->create_and_get( array( 'post_title' => 'Post title block Unit Test' ) ); |
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.
public static function wpSetUpBeforeClass() { | |
self::$post = self::factory()->post->create_and_get( array( 'post_title' => 'Post title block Unit Test' ) ); | |
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { | |
self::$post = $factory->post->create_and_get( array( 'post_title' => 'Post title block Unit Test' ) ); |
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.
Thank you for your review @spacedmonkey. I have updated the code according to your suggestion.
waiting for PR review |
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.
Please find my notes below.
Thanks.
@anton-vlasenko Feedback addressed. Can you please review again. |
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
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.
Test files should be named using camelCase.
Could the test file be renamed to renderBlockCorePostTitle.php
?
<?php | ||
/** | ||
* Post Title rendering tests. | ||
* | ||
* @package WordPress | ||
* @subpackage Blocks | ||
* @covers ::gutenberg_render_block_core_post_title | ||
*/ | ||
|
||
/** | ||
* Tests for the Post Title block. | ||
* | ||
* @group blocks | ||
*/ |
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.
I'd make this a single docblock for simplicity.
<?php | |
/** | |
* Post Title rendering tests. | |
* | |
* @package WordPress | |
* @subpackage Blocks | |
* @covers ::gutenberg_render_block_core_post_title | |
*/ | |
/** | |
* Tests for the Post Title block. | |
* | |
* @group blocks | |
*/ | |
<?php | |
/** | |
* Tests for the gutenberg_render_block_core_post_title() function. | |
* | |
* @package WordPress | |
* @subpackage Blocks | |
* | |
* @group blocks | |
* | |
* @covers ::gutenberg_render_block_core_post_title | |
*/ |
/** | ||
* Test gutenberg_render_block_core_post_title() method. | ||
* | ||
* @covers ::gutenberg_render_block_core_post_title | ||
*/ |
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.
You don't have to use the @Covers tag for individual test methods if you have it defined in the header.
/** | |
* Test gutenberg_render_block_core_post_title() method. | |
* | |
* @covers ::gutenberg_render_block_core_post_title | |
*/ | |
/** | |
* Test gutenberg_render_block_core_post_title() method. | |
*/ |
* | ||
* @group blocks | ||
*/ | ||
class Tests_Blocks_GutenbergRenderBlockCorePostTitle extends WP_UnitTestCase { |
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.
class Tests_Blocks_GutenbergRenderBlockCorePostTitle extends WP_UnitTestCase { | |
class Tests_Blocks_RenderBlockCorePostTitle extends WP_UnitTestCase { |
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.
As per the standards you mentioned above, shouldn't we use the function name which is gutenberg_render_block_core_post_title()
in title case?
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.
I don't have a strong preference on this one.
It's up to you.
If the code is going to be merged into WordPress Core at some point, I'd suggest aligning it with WordPress Core standards to ease the backporting efforts (and increase the chances of acceptance).
Is this the new standards that we are going to follow from now on for test file name? because all other files are named in kebab-case. |
No, this is not a new standard, but I agree, it might seem new. There are (mostly) old tests that don't follow this naming convention, and the newer ones that adhere to it, e.g.: https://github.com/WordPress/wordpress-develop/tree/trunk/tests/phpunit/tests/option |
@anton-vlasenko To follow the standard for file name of unit tests, It required to update phpunit config file first. |
I have updated phpunit config temporary until we change all filenames to camel case as per wordpress core standard. And Addressed all feedback provided by reviewer. |
@anton-vlasenko can you please review this PR? |
/** | ||
* Test gutenberg_render_block_core_post_title() method. | ||
*/ | ||
public function test_should_render_empty_string_when_title_is_empty() { | ||
|
||
// call render method with block context. | ||
$rendered = gutenberg_render_block_core_post_title( self::$attributes, '', self::$block ); | ||
$this->assertEmpty( $rendered ); | ||
|
||
self::$block->context = array( 'postId' => 0 ); | ||
$rendered = gutenberg_render_block_core_post_title( self::$attributes, '', self::$block ); | ||
$this->assertEmpty( $rendered ); | ||
} |
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.
I think a data provider could be utilized here.
/** | |
* Test gutenberg_render_block_core_post_title() method. | |
*/ | |
public function test_should_render_empty_string_when_title_is_empty() { | |
// call render method with block context. | |
$rendered = gutenberg_render_block_core_post_title( self::$attributes, '', self::$block ); | |
$this->assertEmpty( $rendered ); | |
self::$block->context = array( 'postId' => 0 ); | |
$rendered = gutenberg_render_block_core_post_title( self::$attributes, '', self::$block ); | |
$this->assertEmpty( $rendered ); | |
} | |
/** | |
* Test if the function returns an empty string when the post title is empty. | |
* | |
* @dataProvider data_should_render_empty_string_when_title_is_empty | |
*/ | |
public function test_should_render_empty_string_when_title_is_empty( $context, $failure_message ) { | |
if ( $context ) { | |
self::$block->context = $context; | |
} | |
$rendered = gutenberg_render_block_core_post_title( self::$attributes, '', self::$block ); | |
$this->assertSame( '', $rendered, $failure_message ); | |
} | |
/** | |
* Data provider. | |
* | |
* @return array | |
*/ | |
public function data_should_render_empty_string_when_title_is_empty() { | |
return array( | |
'empty block context' => array( null, 'Failed asserting that rendering result is an empty string when block context is not set.' ), | |
'incorrect post ID' => array( array( 'postId' => 0 ), 'Failed asserting that rendering result is an empty string when post ID is incorrect.' ), | |
); | |
} |
} | ||
|
||
/** | ||
* Test gutenberg_render_block_core_post_title() method. |
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.
gutenberg_render_block_core_post_title()
is not a method.
* Test gutenberg_render_block_core_post_title() method. | |
* Test if the function returns correct titles. |
/** | ||
* Array of attributes. | ||
* | ||
* @var int |
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.
* @var int | |
* @var array |
/** | ||
* Post object. | ||
* | ||
* @var array |
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.
* @var array | |
* @var WP_Term |
@anton-vlasenko Can you please review this PR again? |
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.
LGTM.
0d0c1e7
to
dca1332
Compare
What?
Added unit test for Post title core block render function.