Skip to content

Commit

Permalink
[Block] Display paragraph breaks in comment contents block. (#40667)
Browse files Browse the repository at this point in the history
* Apply `comment_text` filter in comment template.

* I think this is how I am supposed to test this.

* Fix tests paragraphs break/I broke.

* CS Fix.

* Tidy assertions even more.

* discard discardWhiteSpace — it was a bad idea.

* Thinking face.
  • Loading branch information
peterwilsoncc authored and adamziel committed Apr 29, 2022
1 parent 6f7adc8 commit 379461d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
6 changes: 5 additions & 1 deletion packages/block-library/src/comment-content/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@ function render_block_core_comment_content( $attributes, $content, $block ) {
return '';
}

$comment_text = get_comment_text( $comment );
$args = array();
$comment_text = get_comment_text( $comment, $args );
if ( ! $comment_text ) {
return '';
}

/** This filter is documented in wp-includes/comment-template.php */
$comment_text = apply_filters( 'comment_text', $comment_text, $comment, $args );

$classes = '';
if ( isset( $attributes['textAlign'] ) ) {
$classes .= 'has-text-align-' . $attributes['textAlign'];
Expand Down
51 changes: 42 additions & 9 deletions phpunit/class-block-library-comment-template-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ function test_rendering_comment_template() {

// Here we use the function prefixed with 'gutenberg_*' because it's added
// in the build step.
$this->assertEquals(
'<ol ><li id="comment-' . self::$comment_ids[0] . '" class="comment even thread-even depth-1"><div class="wp-block-comment-author-name"><a rel="external nofollow ugc" href="http://example.com/author-url/" target="_self" >Test</a></div><div class="wp-block-comment-content">Hello world</div></li></ol>',
gutenberg_render_block_core_comment_template( null, null, $block )
$this->assertSame(
str_replace( array( "\n", "\t" ), '', '<ol ><li id="comment-' . self::$comment_ids[0] . '" class="comment even thread-even depth-1"><div class="wp-block-comment-author-name"><a rel="external nofollow ugc" href="http://example.com/author-url/" target="_self" >Test</a></div><div class="wp-block-comment-content"><p>Hello world</p></div></li></ol>' ),
str_replace( array( "\n", "\t" ), '', gutenberg_render_block_core_comment_template( null, null, $block ) )
);
}

Expand Down Expand Up @@ -199,7 +199,7 @@ function test_rendering_comment_template_nested() {
</a>
</div>
<div class="wp-block-comment-content">
Hello world
<p>Hello world</p>
</div>
<ol>
<li id="comment-{$first_level_ids[0]}" class="comment even depth-2">
Expand All @@ -209,7 +209,7 @@ function test_rendering_comment_template_nested() {
</a>
</div>
<div class="wp-block-comment-content">
Hello world
<p>Hello world</p>
</div>
<ol>
<li id="comment-{$second_level_ids[0]}" class="comment odd alt depth-3">
Expand All @@ -219,7 +219,7 @@ function test_rendering_comment_template_nested() {
</a>
</div>
<div class="wp-block-comment-content">
Hello world
<p>Hello world</p>
</div>
</li>
</ol>
Expand All @@ -231,7 +231,7 @@ function test_rendering_comment_template_nested() {
</a>
</div>
<div class="wp-block-comment-content">
Hello world
<p>Hello world</p>
</div>
</li>
</ol>
Expand All @@ -240,8 +240,8 @@ function test_rendering_comment_template_nested() {
END
);

$this->assertEquals(
gutenberg_render_block_core_comment_template( null, null, $block ),
$this->assertSame(
str_replace( array( "\n", "\t" ), '', gutenberg_render_block_core_comment_template( null, null, $block ) ),
$expected
);
}
Expand Down Expand Up @@ -282,4 +282,37 @@ function test_build_comment_query_vars_from_block_sets_cpage_var() {
$this->assertEquals( $actual['paged'], $comment_query_max_num_pages );
$this->assertEquals( get_query_var( 'cpage' ), $comment_query_max_num_pages );
}

/**
* Test that line and paragraph breaks are converted to HTML tags in a comment.
*/
function test_render_block_core_comment_content_converts_to_html() {
$comment_id = self::$comment_ids[0];
$new_content = "Paragraph One\n\nP2L1\nP2L2\n\nhttps://example.com/";
self::factory()->comment->update_object(
$comment_id,
array( 'comment_content' => $new_content )
);

$parsed_blocks = parse_blocks(
'<!-- wp:comment-template --><!-- wp:comment-content /--><!-- /wp:comment-template -->'
);

$block = new WP_Block(
$parsed_blocks[0],
array(
'postId' => self::$custom_post->ID,
'comments/inherit' => true,
)
);

$expected_content = "<p>Paragraph One</p>\n<p>P2L1<br />\nP2L2</p>\n<p><a href=\"https://example.com/\" rel=\"nofollow ugc\">https://example.com/</a></p>\n";

// Here we use the function prefixed with 'gutenberg_*' because it's added
// in the build step.
$this->assertSame(
'<ol ><li id="comment-' . self::$comment_ids[0] . '" class="comment odd alt thread-even depth-1"><div class="wp-block-comment-content">' . $expected_content . '</div></li></ol>',
gutenberg_render_block_core_comment_template( null, null, $block )
);
}
}

0 comments on commit 379461d

Please sign in to comment.