Skip to content
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

Merged

Conversation

akasunil
Copy link
Member

What?

Added unit test for Post title core block render function.

@akasunil
Copy link
Member Author

@draganescu Can you please review this PR ?

@ntsekouras ntsekouras added the [Type] Automated Testing Testing infrastructure changes impacting the execution of end-to-end (E2E) and/or unit tests. label Jul 15, 2022
@akasunil
Copy link
Member Author

waiting for PR review

draganescu
draganescu previously approved these changes Nov 10, 2022
Copy link
Contributor

@draganescu draganescu left a 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 👏🏻

@akasunil
Copy link
Member Author

Thank you @draganescu. @Mamaduka can we merge this PR ?

Comment on lines 32 to 33
public static function wpSetUpBeforeClass() {
self::$post = self::factory()->post->create_and_get( array( 'post_title' => 'Post title block Unit Test' ) );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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' ) );

Copy link
Member Author

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.

@akasunil
Copy link
Member Author

akasunil commented Jan 2, 2023

waiting for PR review

Copy link
Contributor

@anton-vlasenko anton-vlasenko left a 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.

@draganescu draganescu changed the title Add Unitest for post title block render function Add unit test for post title block render function Jan 6, 2023
@draganescu draganescu dismissed their stale review January 6, 2023 15:44

New review

@akasunil akasunil requested review from anton-vlasenko and removed request for TimothyBJacobs January 28, 2023 15:57
@akasunil
Copy link
Member Author

@anton-vlasenko Feedback addressed. Can you please review again.

Copy link

github-actions bot commented Feb 26, 2024

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 props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: akasunil <[email protected]>
Co-authored-by: draganescu <[email protected]>
Co-authored-by: spacedmonkey <[email protected]>
Co-authored-by: anton-vlasenko <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Copy link
Contributor

@anton-vlasenko anton-vlasenko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://make.wordpress.org/core/handbook/testing/automated-testing/writing-phpunit-tests/#test-classes:

Test files should be named using camelCase.

Could the test file be renamed to renderBlockCorePostTitle.php?

Comment on lines 1 to 14
<?php
/**
* Post Title rendering tests.
*
* @package WordPress
* @subpackage Blocks
* @covers ::gutenberg_render_block_core_post_title
*/

/**
* Tests for the Post Title block.
*
* @group blocks
*/
Copy link
Contributor

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.

Suggested change
<?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
*/

Comment on lines 84 to 88
/**
* Test gutenberg_render_block_core_post_title() method.
*
* @covers ::gutenberg_render_block_core_post_title
*/
Copy link
Contributor

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.

Suggested change
/**
* 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 {
Copy link
Contributor

@anton-vlasenko anton-vlasenko Mar 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
class Tests_Blocks_GutenbergRenderBlockCorePostTitle extends WP_UnitTestCase {
class Tests_Blocks_RenderBlockCorePostTitle extends WP_UnitTestCase {

Copy link
Member Author

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?

Copy link
Contributor

@anton-vlasenko anton-vlasenko Mar 4, 2024

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).

@akasunil
Copy link
Member Author

akasunil commented Mar 2, 2024

Could the test file be renamed to renderBlockCorePostTitle.php?

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.

@anton-vlasenko
Copy link
Contributor

anton-vlasenko commented Mar 4, 2024

Could the test file be renamed to renderBlockCorePostTitle.php?

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

@akasunil
Copy link
Member Author

akasunil commented Mar 8, 2024

@anton-vlasenko To follow the standard for file name of unit tests, It required to update phpunit config file first.

@akasunil akasunil marked this pull request as draft April 19, 2024 08:55
@akasunil akasunil marked this pull request as ready for review April 28, 2024 08:21
@akasunil
Copy link
Member Author

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.

@akasunil
Copy link
Member Author

@anton-vlasenko can you please review this PR?

Comment on lines 72 to 98
/**
* 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 );
}
Copy link
Contributor

@anton-vlasenko anton-vlasenko Jun 5, 2024

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.

Suggested change
/**
* 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.
Copy link
Contributor

@anton-vlasenko anton-vlasenko Jun 5, 2024

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.

Suggested change
* Test gutenberg_render_block_core_post_title() method.
* Test if the function returns correct titles.

/**
* Array of attributes.
*
* @var int
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @var int
* @var array

/**
* Post object.
*
* @var array
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @var array
* @var WP_Term

@akasunil akasunil self-assigned this Jun 25, 2024
@akasunil akasunil requested a review from anton-vlasenko June 25, 2024 11:04
@akasunil
Copy link
Member Author

akasunil commented Jul 3, 2024

@anton-vlasenko Can you please review this PR again?

Copy link
Contributor

@anton-vlasenko anton-vlasenko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@anton-vlasenko anton-vlasenko force-pushed the add-test-for-post-title-block-render branch from 0d0c1e7 to dca1332 Compare July 23, 2024 14:17
@anton-vlasenko anton-vlasenko changed the title Add unit test for post title block render function Add unit tests for the gutenberg_render_block_core_post_title() function Jul 23, 2024
@anton-vlasenko anton-vlasenko enabled auto-merge (squash) July 23, 2024 14:59
@anton-vlasenko anton-vlasenko merged commit ae50345 into WordPress:trunk Jul 23, 2024
58 checks passed
@akasunil akasunil deleted the add-test-for-post-title-block-render branch July 23, 2024 15:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Automated Testing Testing infrastructure changes impacting the execution of end-to-end (E2E) and/or unit tests.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants