From 731a3d00e686e353f7b4cacfe14ce878ba4340c4 Mon Sep 17 00:00:00 2001
From: Sunil Prajapati
Date: Sun, 21 Aug 2022 11:00:38 +0530
Subject: [PATCH 1/9] Added unittest for post excerpt block render function
---
.../blocks/render-block-post-excerpt-test.php | 146 ++++++++++++++++++
1 file changed, 146 insertions(+)
create mode 100644 phpunit/blocks/render-block-post-excerpt-test.php
diff --git a/phpunit/blocks/render-block-post-excerpt-test.php b/phpunit/blocks/render-block-post-excerpt-test.php
new file mode 100644
index 00000000000000..366584b63942c7
--- /dev/null
+++ b/phpunit/blocks/render-block-post-excerpt-test.php
@@ -0,0 +1,146 @@
+post->create_and_get(
+ array(
+ 'post_title' => 'Post Expert block Unit Test',
+ 'post_excerpt' => '',
+ 'post_content' => '',
+ )
+ );
+
+ self::$post = self::factory()->post->create_and_get(
+ array(
+ 'post_title' => 'Post Expert block Unit Test',
+ 'post_excerpt' => 'Post Expert content',
+ )
+ );
+
+ self::$attributes = array(
+ 'moreText' => '',
+ );
+
+ $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_empty->ID, true );
+ wp_delete_post( self::$post->ID, true );
+ }
+
+ /**
+ * Test gutenberg_render_block_core_Post_Excerpt() method
+ * with empty data.
+ *
+ * @covers ::gutenberg_render_block_core_Post_Excerpt
+ */
+ public function test_gutenberg_render_block_core_Post_Excerpt_empty() {
+ $block = new stdClass();
+
+ // call render method with block context.
+ $rendered = gutenberg_render_block_core_Post_Excerpt( self::$attributes, '', $block );
+ $this->assertEmpty( $rendered );
+
+ $block->context = array( 'postId' => 0 );
+ $rendered = gutenberg_render_block_core_Post_Excerpt( self::$attributes, '', $block );
+ $this->assertEmpty( $rendered );
+
+ $GLOBALS['post'] = self::$post_empty;
+ $block->context = array( 'postId' => self::$post_empty->ID );
+
+ $rendered = gutenberg_render_block_core_post_excerpt( self::$attributes, '', $block );
+ $this->assertEmpty( $rendered );
+
+ }
+
+ /**
+ * Test gutenberg_render_block_core_Post_Excerpt() method.
+ *
+ * @covers ::gutenberg_render_block_core_Post_Excerpt
+ */
+ public function test_gutenberg_render_block_core_Post_Excerpt() {
+
+ $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 );
+ $this->assertStringContainsString( 'Post Expert content', $rendered );
+ $this->assertStringContainsString( '
assertStringContainsString( '', $rendered );
+ $this->assertStringContainsString( 'wp-block-post-excerpt__excerpt', $rendered );
+ $this->assertStringNotContainsString( 'has-text-align', $rendered );
+
+ self::$attributes['textAlign'] = 'left';
+
+ $rendered = gutenberg_render_block_core_post_excerpt( self::$attributes, '', $block );
+ $this->assertStringContainsString( 'has-text-align-left', $rendered );
+
+ self::$attributes = array(
+ 'moreText' => 'Read More',
+ );
+
+ $rendered = gutenberg_render_block_core_post_excerpt( self::$attributes, '', $block );
+ $this->assertStringContainsString( 'wp-block-post-excerpt__more-link', $rendered );
+
+ self::$attributes = array(
+ 'moreText' => 'Read More',
+ 'showMoreOnNewLine' => true,
+ );
+ $this->assertStringContainsString( 'wp-block-post-excerpt__more-link', $rendered );
+ $this->assertStringContainsString( get_permalink( self::$post->ID ), $rendered );
+
+ }
+}
From 4f4d8f3f2a53f0ada2b02ce9539f4a02cf714c20 Mon Sep 17 00:00:00 2001
From: Sunil Prajapati
Date: Fri, 18 Nov 2022 10:18:35 +0530
Subject: [PATCH 2/9] Updated wpSetUpBeforeClass method with argument
---
phpunit/blocks/render-block-post-excerpt-test.php | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/phpunit/blocks/render-block-post-excerpt-test.php b/phpunit/blocks/render-block-post-excerpt-test.php
index 366584b63942c7..54a331aa16b4ac 100644
--- a/phpunit/blocks/render-block-post-excerpt-test.php
+++ b/phpunit/blocks/render-block-post-excerpt-test.php
@@ -35,9 +35,11 @@ class Tests_Blocks_Render_Post_Excerpt extends WP_UnitTestCase {
/**
* Setup method.
+ *
+ * @param WP_UnitTest_Factory $factory Helper that lets us create fake data.
*/
- public static function wpSetUpBeforeClass() {
- self::$post_empty = self::factory()->post->create_and_get(
+ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
+ self::$post_empty = $factory->post->create_and_get(
array(
'post_title' => 'Post Expert block Unit Test',
'post_excerpt' => '',
@@ -45,7 +47,7 @@ public static function wpSetUpBeforeClass() {
)
);
- self::$post = self::factory()->post->create_and_get(
+ self::$post = $factory->post->create_and_get(
array(
'post_title' => 'Post Expert block Unit Test',
'post_excerpt' => 'Post Expert content',
From 3f70944198c3256f00a34ec7faae79c625f6ee4f Mon Sep 17 00:00:00 2001
From: Sunil Prajapati
Date: Tue, 10 Jan 2023 12:28:53 +0530
Subject: [PATCH 3/9] Addressed feedbacks
---
.../blocks/render-block-post-excerpt-test.php | 81 ++++++++++++++-----
1 file changed, 59 insertions(+), 22 deletions(-)
diff --git a/phpunit/blocks/render-block-post-excerpt-test.php b/phpunit/blocks/render-block-post-excerpt-test.php
index 54a331aa16b4ac..4b317a9c71a7f9 100644
--- a/phpunit/blocks/render-block-post-excerpt-test.php
+++ b/phpunit/blocks/render-block-post-excerpt-test.php
@@ -4,6 +4,7 @@
*
* @package WordPress
* @subpackage Blocks
+ * @covers ::gutenberg_render_block_core_post_excerpt
*/
/**
@@ -11,7 +12,7 @@
*
* @group blocks
*/
-class Tests_Blocks_Render_Post_Excerpt extends WP_UnitTestCase {
+class Tests_Blocks_GutenbergRenderBlockCorePostExcerpt extends WP_UnitTestCase {
/**
* Post object with empty data
*
@@ -82,67 +83,103 @@ public static function wpTearDownAfterClass() {
}
/**
- * Test gutenberg_render_block_core_Post_Excerpt() method
+ * Test gutenberg_render_block_core_post_excerpt() method
* with empty data.
*
- * @covers ::gutenberg_render_block_core_Post_Excerpt
+ * @covers ::gutenberg_render_block_core_post_excerpt
*/
- public function test_gutenberg_render_block_core_Post_Excerpt_empty() {
+ public function test_gutenberg_render_block_core_post_excerpt_empty() {
$block = new stdClass();
// call render method with block context.
- $rendered = gutenberg_render_block_core_Post_Excerpt( self::$attributes, '', $block );
- $this->assertEmpty( $rendered );
+ $rendered = gutenberg_render_block_core_post_excerpt( self::$attributes, '', $block );
+ $this->assertEmpty( $rendered, 'Failed to assert that $rendered is empty.' );
$block->context = array( 'postId' => 0 );
- $rendered = gutenberg_render_block_core_Post_Excerpt( self::$attributes, '', $block );
- $this->assertEmpty( $rendered );
+ $rendered = gutenberg_render_block_core_post_excerpt( self::$attributes, '', $block );
+ $this->assertEmpty( $rendered, 'Failed to assert that $rendered is empty.' );
$GLOBALS['post'] = self::$post_empty;
$block->context = array( 'postId' => self::$post_empty->ID );
$rendered = gutenberg_render_block_core_post_excerpt( self::$attributes, '', $block );
- $this->assertEmpty( $rendered );
+ $this->assertEmpty( $rendered, 'Failed to assert that $rendered is empty.' );
}
/**
- * Test gutenberg_render_block_core_Post_Excerpt() method.
+ * Test gutenberg_render_block_core_post_excerpt() method.
*
- * @covers ::gutenberg_render_block_core_Post_Excerpt
+ * @covers ::gutenberg_render_block_core_post_excerpt
*/
- public function test_gutenberg_render_block_core_Post_Excerpt() {
+ public function test_gutenberg_render_block_core_post_excerpt() {
$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 );
- $this->assertStringContainsString( 'Post Expert content', $rendered );
- $this->assertStringContainsString( 'assertStringContainsString( '', $rendered );
- $this->assertStringContainsString( 'wp-block-post-excerpt__excerpt', $rendered );
- $this->assertStringNotContainsString( 'has-text-align', $rendered );
+ $this->assertNotEmpty( $rendered, 'Failed to assert the $rendered is not empty.' );
+ $this->assertStringContainsString(
+ 'Post Expert content',
+ $rendered,
+ 'Failed to assert that $rendered contain expected string.'
+ );
+ $this->assertStringContainsString(
+ 'assertStringContainsString(
+ '',
+ $rendered,
+ 'Failed to assert that $rendered contain html paragraph tag.'
+ );
+ $this->assertStringContainsString(
+ 'wp-block-post-excerpt__excerpt',
+ $rendered,
+ 'Failed to assert that $rendered contain expected string.'
+ );
+ $this->assertStringNotContainsString(
+ 'has-text-align',
+ $rendered,
+ 'Failed to assert that $rendered doest not contain text align class.'
+ );
self::$attributes['textAlign'] = 'left';
$rendered = gutenberg_render_block_core_post_excerpt( self::$attributes, '', $block );
- $this->assertStringContainsString( 'has-text-align-left', $rendered );
+ $this->assertStringContainsString(
+ 'has-text-align-left',
+ $rendered,
+ 'Failed to assert that $rendered contain text align class.'
+ );
self::$attributes = array(
'moreText' => 'Read More',
);
$rendered = gutenberg_render_block_core_post_excerpt( self::$attributes, '', $block );
- $this->assertStringContainsString( 'wp-block-post-excerpt__more-link', $rendered );
+ $this->assertStringContainsString(
+ 'wp-block-post-excerpt__more-link',
+ $rendered,
+ 'Failed to assert that $rendered contain expected string.'
+ );
self::$attributes = array(
'moreText' => 'Read More',
'showMoreOnNewLine' => true,
);
- $this->assertStringContainsString( 'wp-block-post-excerpt__more-link', $rendered );
- $this->assertStringContainsString( get_permalink( self::$post->ID ), $rendered );
+ $this->assertStringContainsString(
+ 'wp-block-post-excerpt__more-link',
+ $rendered,
+ 'Failed to assert that $rendered contain expected string.'
+ );
+ $this->assertStringContainsString(
+ get_permalink( self::$post->ID ),
+ $rendered,
+ 'Failed to assert that $rendered contain expected post url.'
+ );
}
}
From 9d5e984adfceeb5eb2aaf43639985f58784f53b0 Mon Sep 17 00:00:00 2001
From: sunil25393
Date: Mon, 26 Feb 2024 12:31:01 +0530
Subject: [PATCH 4/9] fix unit test failure
---
.../blocks/render-block-post-excerpt-test.php | 29 ++-----------------
1 file changed, 2 insertions(+), 27 deletions(-)
diff --git a/phpunit/blocks/render-block-post-excerpt-test.php b/phpunit/blocks/render-block-post-excerpt-test.php
index 4b317a9c71a7f9..6973f5ddbccebb 100644
--- a/phpunit/blocks/render-block-post-excerpt-test.php
+++ b/phpunit/blocks/render-block-post-excerpt-test.php
@@ -13,12 +13,6 @@
* @group blocks
*/
class Tests_Blocks_GutenbergRenderBlockCorePostExcerpt extends WP_UnitTestCase {
- /**
- * Post object with empty data
- *
- * @var array
- */
- protected static $post_empty;
/**
* Post object with data.
@@ -40,13 +34,6 @@ class Tests_Blocks_GutenbergRenderBlockCorePostExcerpt extends WP_UnitTestCase {
* @param WP_UnitTest_Factory $factory Helper that lets us create fake data.
*/
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
- self::$post_empty = $factory->post->create_and_get(
- array(
- 'post_title' => 'Post Expert block Unit Test',
- 'post_excerpt' => '',
- 'post_content' => '',
- )
- );
self::$post = $factory->post->create_and_get(
array(
@@ -57,6 +44,7 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$attributes = array(
'moreText' => '',
+ 'excerptLength' => 55,
);
$block = array(
@@ -71,14 +59,12 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
WP_Block_Supports::init();
WP_Block_Supports::$block_to_render = $block;
-
}
/**
* Tear down method.
*/
public static function wpTearDownAfterClass() {
- wp_delete_post( self::$post_empty->ID, true );
wp_delete_post( self::$post->ID, true );
}
@@ -94,17 +80,6 @@ public function test_gutenberg_render_block_core_post_excerpt_empty() {
// call render method with block context.
$rendered = gutenberg_render_block_core_post_excerpt( self::$attributes, '', $block );
$this->assertEmpty( $rendered, 'Failed to assert that $rendered is empty.' );
-
- $block->context = array( 'postId' => 0 );
- $rendered = gutenberg_render_block_core_post_excerpt( self::$attributes, '', $block );
- $this->assertEmpty( $rendered, 'Failed to assert that $rendered is empty.' );
-
- $GLOBALS['post'] = self::$post_empty;
- $block->context = array( 'postId' => self::$post_empty->ID );
-
- $rendered = gutenberg_render_block_core_post_excerpt( self::$attributes, '', $block );
- $this->assertEmpty( $rendered, 'Failed to assert that $rendered is empty.' );
-
}
/**
@@ -157,6 +132,7 @@ public function test_gutenberg_render_block_core_post_excerpt() {
self::$attributes = array(
'moreText' => 'Read More',
+ 'excerptLength' => 55,
);
$rendered = gutenberg_render_block_core_post_excerpt( self::$attributes, '', $block );
@@ -180,6 +156,5 @@ public function test_gutenberg_render_block_core_post_excerpt() {
$rendered,
'Failed to assert that $rendered contain expected post url.'
);
-
}
}
From ebd1fdf4b70ad64f2a3e7ec50353ef7f197434a4 Mon Sep 17 00:00:00 2001
From: sunil25393
Date: Tue, 27 Feb 2024 10:42:02 +0530
Subject: [PATCH 5/9] Fix coding standards
---
phpunit/blocks/render-block-post-excerpt-test.php | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/phpunit/blocks/render-block-post-excerpt-test.php b/phpunit/blocks/render-block-post-excerpt-test.php
index 6973f5ddbccebb..82f282eac10e21 100644
--- a/phpunit/blocks/render-block-post-excerpt-test.php
+++ b/phpunit/blocks/render-block-post-excerpt-test.php
@@ -43,7 +43,7 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
);
self::$attributes = array(
- 'moreText' => '',
+ 'moreText' => '',
'excerptLength' => 55,
);
@@ -131,7 +131,7 @@ public function test_gutenberg_render_block_core_post_excerpt() {
);
self::$attributes = array(
- 'moreText' => 'Read More',
+ 'moreText' => 'Read More',
'excerptLength' => 55,
);
From c9b2f0f256983c78274d414af6d172ba0c8ebdee Mon Sep 17 00:00:00 2001
From: sunil25393
Date: Tue, 27 Feb 2024 11:24:31 +0530
Subject: [PATCH 6/9] Fix phpcs issue
---
phpunit/blocks/render-block-post-excerpt-test.php | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/phpunit/blocks/render-block-post-excerpt-test.php b/phpunit/blocks/render-block-post-excerpt-test.php
index 82f282eac10e21..e568287a17b182 100644
--- a/phpunit/blocks/render-block-post-excerpt-test.php
+++ b/phpunit/blocks/render-block-post-excerpt-test.php
@@ -43,7 +43,7 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
);
self::$attributes = array(
- 'moreText' => '',
+ 'moreText' => '',
'excerptLength' => 55,
);
@@ -131,7 +131,7 @@ public function test_gutenberg_render_block_core_post_excerpt() {
);
self::$attributes = array(
- 'moreText' => 'Read More',
+ 'moreText' => 'Read More',
'excerptLength' => 55,
);
From ffefb68138ca424d07621d076dbbc68fc66efa13 Mon Sep 17 00:00:00 2001
From: sunil25393
Date: Sun, 28 Apr 2024 13:58:27 +0530
Subject: [PATCH 7/9] Update config to allow files with new name standard
---
phpunit.xml.dist | 1 +
1 file changed, 1 insertion(+)
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/
From 4dd12123b604fed22c207c1d583ac6bce2ad69eb Mon Sep 17 00:00:00 2001
From: sunil25393
Date: Sun, 28 Apr 2024 14:02:57 +0530
Subject: [PATCH 8/9] Change test file name to camel case
---
...xcerpt-test.php => renderBlockCorePostExcerpt.php} | 11 +----------
1 file changed, 1 insertion(+), 10 deletions(-)
rename phpunit/blocks/{render-block-post-excerpt-test.php => renderBlockCorePostExcerpt.php} (93%)
diff --git a/phpunit/blocks/render-block-post-excerpt-test.php b/phpunit/blocks/renderBlockCorePostExcerpt.php
similarity index 93%
rename from phpunit/blocks/render-block-post-excerpt-test.php
rename to phpunit/blocks/renderBlockCorePostExcerpt.php
index e568287a17b182..5e5fe3d3c2f4c0 100644
--- a/phpunit/blocks/render-block-post-excerpt-test.php
+++ b/phpunit/blocks/renderBlockCorePostExcerpt.php
@@ -5,14 +5,9 @@
* @package WordPress
* @subpackage Blocks
* @covers ::gutenberg_render_block_core_post_excerpt
- */
-
-/**
- * Tests for the Post Excerpt block.
- *
* @group blocks
*/
-class Tests_Blocks_GutenbergRenderBlockCorePostExcerpt extends WP_UnitTestCase {
+class Tests_Blocks_RenderBlockCorePostExcerpt extends WP_UnitTestCase {
/**
* Post object with data.
@@ -71,8 +66,6 @@ public static function wpTearDownAfterClass() {
/**
* Test gutenberg_render_block_core_post_excerpt() method
* with empty data.
- *
- * @covers ::gutenberg_render_block_core_post_excerpt
*/
public function test_gutenberg_render_block_core_post_excerpt_empty() {
$block = new stdClass();
@@ -84,8 +77,6 @@ public function test_gutenberg_render_block_core_post_excerpt_empty() {
/**
* Test gutenberg_render_block_core_post_excerpt() method.
- *
- * @covers ::gutenberg_render_block_core_post_excerpt
*/
public function test_gutenberg_render_block_core_post_excerpt() {
From c1b8d75b03961feada040af50af681e5ef785ad1 Mon Sep 17 00:00:00 2001
From: sunil25393
Date: Thu, 9 May 2024 10:10:16 +0530
Subject: [PATCH 9/9] Update assertion comments and formatting
---
phpunit/blocks/renderBlockCorePostExcerpt.php | 32 ++++++++-----------
1 file changed, 14 insertions(+), 18 deletions(-)
diff --git a/phpunit/blocks/renderBlockCorePostExcerpt.php b/phpunit/blocks/renderBlockCorePostExcerpt.php
index 5e5fe3d3c2f4c0..38c27bfde9b30a 100644
--- a/phpunit/blocks/renderBlockCorePostExcerpt.php
+++ b/phpunit/blocks/renderBlockCorePostExcerpt.php
@@ -1,9 +1,10 @@
assertEmpty( $rendered, 'Failed to assert that $rendered is empty.' );
+ $this->assertSame( '', $rendered, 'Failed to assert that $rendered is an empty string.' );
}
/**
* Test gutenberg_render_block_core_post_excerpt() method.
*/
- public function test_gutenberg_render_block_core_post_excerpt() {
+ 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 the $rendered is not empty.' );
+ $this->assertNotEmpty( $rendered, 'Failed to assert that $rendered is not empty.' );
$this->assertStringContainsString(
'Post Expert content',
$rendered,
- 'Failed to assert that $rendered contain expected string.'
+ 'Failed to assert that $rendered contain the expected string.'
);
$this->assertStringContainsString(
'assertStringContainsString(
- '',
- $rendered,
- 'Failed to assert that $rendered contain html paragraph tag.'
+ '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 expected string.'
+ 'Failed to assert that $rendered contain the "wp-block-post-excerpt__excerpt" string.'
);
$this->assertStringNotContainsString(
'has-text-align',
$rendered,
- 'Failed to assert that $rendered doest not contain text align class.'
+ 'Failed to assert that $rendered does not contain the has-text-align class.'
);
self::$attributes['textAlign'] = 'left';
@@ -118,7 +114,7 @@ public function test_gutenberg_render_block_core_post_excerpt() {
$this->assertStringContainsString(
'has-text-align-left',
$rendered,
- 'Failed to assert that $rendered contain text align class.'
+ 'Failed to assert that $rendered contains the "has-text-align-left" class.'
);
self::$attributes = array(
@@ -130,7 +126,7 @@ public function test_gutenberg_render_block_core_post_excerpt() {
$this->assertStringContainsString(
'wp-block-post-excerpt__more-link',
$rendered,
- 'Failed to assert that $rendered contain expected string.'
+ 'Failed to assert that $rendered contains the expected string.'
);
self::$attributes = array(
@@ -140,12 +136,12 @@ public function test_gutenberg_render_block_core_post_excerpt() {
$this->assertStringContainsString(
'wp-block-post-excerpt__more-link',
$rendered,
- 'Failed to assert that $rendered contain expected string.'
+ '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 url.'
+ 'Failed to assert that $rendered contain expected post the expected post URL.'
);
}
}