Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions src/wp-includes/blocks/rss.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,18 @@ function render_block_core_rss( $attributes ) {
}
$title = "<div class='wp-block-rss__item-title'>{$title}</div>";

$date = '';
if ( $attributes['displayDate'] ) {
$date = $item->get_date( 'U' );
$date_markup = '';
if ( ! empty( $attributes['displayDate'] ) ) {
$timestamp = $item->get_date( 'U' );

if ( $date ) {
$date = sprintf(
if ( $timestamp ) {
$gmt_offset = get_option( 'gmt_offset' );
$timestamp += (int) ( (float) $gmt_offset * HOUR_IN_SECONDS );

$date_markup = sprintf(
'<time datetime="%1$s" class="wp-block-rss__item-publish-date">%2$s</time> ',
esc_attr( date_i18n( 'c', $date ) ),
esc_attr( date_i18n( get_option( 'date_format' ), $date ) )
esc_attr( date_i18n( 'c', $timestamp ) ),
esc_html( date_i18n( get_option( 'date_format' ), $timestamp ) )
);
}
}
Expand Down Expand Up @@ -85,7 +88,7 @@ function render_block_core_rss( $attributes ) {
$excerpt = '<div class="wp-block-rss__item-excerpt">' . esc_html( $excerpt ) . '</div>';
}

$list_items .= "<li class='wp-block-rss__item'>{$title}{$date}{$author}{$excerpt}</li>";
$list_items .= "<li class='wp-block-rss__item'>{$title}{$date_markup}{$author}{$excerpt}</li>";
}

$classnames = array();
Expand Down
31 changes: 31 additions & 0 deletions tests/phpunit/data/feed/Rss_Block_Test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:media="http://search.yahoo.com/mrss/" xmlns:fn="https://www.publishwithfoundation.com/rss/2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" version="2.0">
<channel>
<title>Test RSS Feed</title>
<link>https://www.example.com</link>
<atom:link href="https://www.example.com/rss.xml" rel="self" type="application/rss+xml"/>
<description>This is a test RSS feed for unit testing.</description>
<language>en-us</language>
<pubDate>Wed, 19 Mar 2025 00:00:01 -0700</pubDate>
<lastBuildDate>Wed, 19 Mar 2025 03:58:04 -0700</lastBuildDate>
<generator>Test</generator>
<item>
<title>Test Article 1</title>
<link>https://www.example.com/article1</link>
<guid isPermaLink="true">https://www.example.com/article1</guid>
<dc:creator>Test Author</dc:creator>
<description>This is a test article description.</description>
<pubDate>Thu, 10 Mar 2025 04:00:00 -0700</pubDate>
<source url="https://www.example.com">Test Source</source>
</item>
<item>
<title>Test Article 2</title>
<link>https://www.example.com/article2</link>
<guid isPermaLink="true">https://www.example.com/article2</guid>
<dc:creator>Test Author 2</dc:creator>
<description>This is another test article description.</description>
<pubDate>Wed, 18 Mar 2025 10:30:00 -0700</pubDate>
<source url="https://www.example.com">Test Source</source>
</item>
</channel>
</rss>
122 changes: 122 additions & 0 deletions tests/phpunit/tests/blocks/renderRssBlock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php
/**
* Tests for core/rss Gutenberg block.
*
* @package WordPress
* @subpackage Blocks
* @since 6.8.0
*
* @group blocks
*/

/**
* Class for testing the core/rss Gutenberg block.
*
* @since 6.8.0
*/
class Tests_Blocks_RenderRssBlock extends WP_UnitTestCase {

/**
* Set up before each test.
*/
public function set_up() {
parent::set_up();

add_filter( 'wp_feed_cache_transient_lifetime', array( $this, 'return_zero_feed_cache' ) );
Comment thread
SirLouen marked this conversation as resolved.
Outdated
add_filter( 'pre_http_request', array( $this, 'mock_http_request' ), 10, 3 );
}

/**
* Set feed cache to zero to prevent caching interfering with tests.
*
* @ticket 62400
*
* @return int Zero value.
*/
public function return_zero_feed_cache() {
return 0;
}

Comment thread
SirLouen marked this conversation as resolved.
Outdated
/**
* Mock HTTP request to return test feed data.
*
* @ticket 62400
*
* @param bool|array $response The existing response or false.
* @param array $args The request arguments.
* @param string $url The request URL.
* @return array The mocked response.
*/
public function mock_http_request( $response, $args, $url ) {
if ( 'https://example.com/testrss.xml' !== $url ) {
return $response;
}

return array(
'headers' => array(
'content-type' => 'application/rss+xml; charset=UTF-8',
),
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
'body' => file_get_contents( DIR_TESTDATA . '/feed/Rss_Block_Test.xml' ),
Comment thread
SirLouen marked this conversation as resolved.
Outdated
'response' => array(
'code' => 200,
'message' => 'OK',
),
'cookies' => array(),
'filename' => null,
);
}

/**
* Sets up the "core/rss" block context for testing.
* This is needed to avoid null access in WP_Block_Supports::apply_block_supports().
*
* @ticket 62400
*/
private function setup_block_context() {
$block = array(
'blockName' => 'core/rss',
'attrs' => array(),
);

$wp_block_supports = WP_Block_Supports::get_instance();
$reflection = new ReflectionClass( $wp_block_supports );
$property = $reflection->getProperty( 'block_to_render' );
$property->setAccessible( true );
$property->setValue( $wp_block_supports, $block );
}

/**
* Test that the date in the RSS feed is correctly rendered in the HTML.
*
* @ticket 62400
*
* @covers ::render_block_core_rss
*/
public function test_rss_date_rendering() {

update_option( 'date_format', 'F j, Y' );
// We set to UTC+9 to test timezone conversion.
update_option( 'gmt_offset', 9 );

$this->setup_block_context();

// Mock RSS Attributes.
$attributes = array(
'feedURL' => 'https://example.com/testrss.xml',
'itemsToShow' => 2,
'displayExcerpt' => false,
'displayAuthor' => false,
'displayDate' => true,
'blockLayout' => 'list',
);

$rendered_html = render_block_core_rss( $attributes );

$this->assertStringContainsString( '<time datetime=', $rendered_html, 'No time element found in rendered HTML' );

$this->assertStringContainsString( 'March 19, 2025', $rendered_html, 'Formatted date not found in rendered HTML' );

$this->assertMatchesRegularExpression( '/<time datetime="[^"]*2025-03-19[^"]*"/', $rendered_html, 'ISO datetime format missing expected date' );
}
}
Loading