Skip to content

Commit ef9ac59

Browse files
Tests: Add unit test for empty comment tags in export
1 parent df52bf2 commit ef9ac59

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

src/wp-admin/includes/export.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,7 @@ function wxr_filter_postmeta( $return_me, $meta_key ) {
685685
endforeach;
686686

687687
$_comments = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved <> 'spam'", $post->ID ) );
688-
$comments = array_map( 'get_comment', $_comments );
688+
$comments = array_filter( array_map( 'get_comment', $_comments ) );
689689
foreach ( $comments as $c ) :
690690
?>
691691
<wp:comment>

tests/phpunit/tests/admin/exportWp.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,4 +290,55 @@ private function populate_args_post_authors( array &$args, $expected_ids ) {
290290
$post_ids_key = $expected_ids[0];
291291
$args['author'] = self::$post_ids[ $post_ids_key ]['post_author'];
292292
}
293+
294+
/**
295+
* @ticket 61244
296+
* @covers ::export_wp
297+
*/
298+
public function test_export_wp_should_not_include_empty_comments_when_filtered() {
299+
$post_id = self::factory()->post->create( array( 'post_title' => 'Test Post' ) );
300+
self::factory()->comment->create_post_comments( $post_id, 3 );
301+
302+
// Add filter to make get_comment return false.
303+
add_action(
304+
'export_wp',
305+
function () {
306+
add_filter( 'get_comment', '__return_false' );
307+
}
308+
);
309+
310+
ob_start();
311+
export_wp();
312+
$xml = ob_get_clean();
313+
314+
$xml_obj = simplexml_load_string( $xml );
315+
316+
// Convert all <wp:comment> tags to string for easier checking.
317+
$comment_tags = $xml_obj->xpath( '//wp:comment' );
318+
319+
// Verify there are no comment tags in the export.
320+
$this->assertEmpty( $comment_tags, 'No <wp:comment> tags should be present when comments are filtered out.' );
321+
}
322+
323+
/**
324+
* @ticket 61244
325+
* @covers ::export_wp
326+
*/
327+
public function test_export_wp_includes_comments_when_not_filtered() {
328+
$post_id = self::factory()->post->create( array( 'post_title' => 'Test Post' ) );
329+
$comment_count = 3;
330+
self::factory()->comment->create_post_comments( $post_id, $comment_count );
331+
332+
ob_start();
333+
export_wp();
334+
$xml = ob_get_clean();
335+
336+
$xml_obj = simplexml_load_string( $xml );
337+
338+
// Count <wp:comment> tags.
339+
$comment_tags = $xml_obj->xpath( '//wp:comment' );
340+
341+
// Verify the correct number of comment tags are present.
342+
$this->assertCount( $comment_count, $comment_tags, 'Export should include all comments when not filtered.' );
343+
}
293344
}

0 commit comments

Comments
 (0)