Skip to content

Commit 073c800

Browse files
committed
Unit Tests for RSS Block
1 parent 13f51e8 commit 073c800

1 file changed

Lines changed: 179 additions & 0 deletions

File tree

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
<?php
2+
/**
3+
* Tests for core/rss Gutenberg block.
4+
*
5+
* @package WordPress
6+
* @subpackage Blocks
7+
* @since 6.8.0
8+
*
9+
* @group blocks
10+
*/
11+
12+
/**
13+
* Class for testing the core/rss Gutenberg block.
14+
*
15+
* @since 6.8.0
16+
*/
17+
class Tests_Blocks_RssBlock extends WP_UnitTestCase {
18+
19+
/**
20+
* Set up before each test.
21+
*
22+
* @ticket 62400
23+
*/
24+
public function set_up() {
25+
parent::set_up();
26+
27+
add_filter( 'wp_feed_cache_transient_lifetime', array( $this, 'return_zero_feed_cache' ) );
28+
add_filter( 'pre_http_request', array( $this, 'mock_http_request' ), 10, 3 );
29+
}
30+
31+
/**
32+
* Clean up after each test.
33+
*
34+
* @ticket 62400
35+
*/
36+
public function tear_down() {
37+
remove_filter( 'wp_feed_cache_transient_lifetime', array( $this, 'return_zero_feed_cache' ) );
38+
remove_filter( 'pre_http_request', array( $this, 'mock_http_request' ) );
39+
parent::tear_down();
40+
}
41+
42+
/**
43+
* Set feed cache to zero to prevent caching interfering with tests.
44+
*
45+
* @ticket 62400
46+
*
47+
* @return int Zero value.
48+
*/
49+
public function return_zero_feed_cache() {
50+
return 0;
51+
}
52+
53+
/**
54+
* Mock HTTP request to return test feed data.
55+
*
56+
* @ticket 62400
57+
*
58+
* @param bool|array $response The existing response or false.
59+
* @param array $args The request arguments.
60+
* @param string $url The request URL.
61+
* @return array The mocked response.
62+
*/
63+
public function mock_http_request( $response, $args, $url ) {
64+
if ( 'https://example.com/testrss.xml' !== $url ) {
65+
return $response;
66+
}
67+
68+
$mock_rss = <<<XML
69+
<?xml version="1.0" encoding="UTF-8"?>
70+
<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">
71+
<channel>
72+
<title>Test RSS Feed</title>
73+
<link>https://www.example.com</link>
74+
<atom:link href="https://www.example.com/rss.xml" rel="self" type="application/rss+xml"/>
75+
<description>This is a test RSS feed for unit testing.</description>
76+
<language>en-us</language>
77+
<pubDate>Wed, 19 Mar 2025 00:00:01 -0700</pubDate>
78+
<lastBuildDate>Wed, 19 Mar 2025 03:58:04 -0700</lastBuildDate>
79+
<generator>Test</generator>
80+
<item>
81+
<title>Test Article 1</title>
82+
<link>https://www.example.com/article1</link>
83+
<guid isPermaLink="true">https://www.example.com/article1</guid>
84+
<dc:creator>Test Author</dc:creator>
85+
<description>This is a test article description.</description>
86+
<pubDate>Thu, 10 Mar 2025 04:00:00 -0700</pubDate>
87+
<source url="https://www.example.com">Test Source</source>
88+
</item>
89+
<item>
90+
<title>Test Article 2</title>
91+
<link>https://www.example.com/article2</link>
92+
<guid isPermaLink="true">https://www.example.com/article2</guid>
93+
<dc:creator>Test Author 2</dc:creator>
94+
<description>This is another test article description.</description>
95+
<pubDate>Wed, 18 Mar 2025 10:30:00 -0700</pubDate>
96+
<source url="https://www.example.com">Test Source</source>
97+
</item>
98+
</channel>
99+
</rss>
100+
XML;
101+
102+
return array(
103+
'headers' => array(
104+
'content-type' => 'application/rss+xml; charset=UTF-8',
105+
),
106+
'body' => $mock_rss,
107+
'response' => array(
108+
'code' => 200,
109+
'message' => 'OK',
110+
),
111+
'cookies' => array(),
112+
'filename' => null,
113+
);
114+
}
115+
116+
/**
117+
* Sets up the "core/rss" block context for testing.
118+
* This is needed to avoid null access in WP_Block_Supports::apply_block_supports().
119+
*
120+
* @ticket 62400
121+
*/
122+
private function setup_block_context() {
123+
$block = array(
124+
'blockName' => 'core/rss',
125+
'attrs' => array(),
126+
);
127+
128+
$wp_block_supports = WP_Block_Supports::get_instance();
129+
$reflection = new ReflectionClass( $wp_block_supports );
130+
$property = $reflection->getProperty( 'block_to_render' );
131+
$property->setAccessible( true );
132+
$property->setValue( $wp_block_supports, $block );
133+
}
134+
135+
/**
136+
* Test that the date in the RSS feed is correctly rendered in the HTML.
137+
*
138+
* @ticket 62400
139+
*
140+
* @covers ::render_block_core_rss
141+
*/
142+
public function test_rss_date_rendering() {
143+
144+
$original_date_format = get_option( 'date_format' );
145+
$original_gmt_offset = get_option( 'gmt_offset' );
146+
147+
update_option( 'date_format', 'F j, Y' );
148+
// We set to UTC+9 to test timezone conversion.
149+
update_option( 'gmt_offset', 9 );
150+
151+
$this->setup_block_context();
152+
153+
// Mock RSS Attributes.
154+
$attributes = array(
155+
'feedURL' => 'https://example.com/testrss.xml',
156+
'itemsToShow' => 2,
157+
'displayExcerpt' => false,
158+
'displayAuthor' => false,
159+
'displayDate' => true,
160+
'blockLayout' => 'list',
161+
);
162+
163+
$rendered_html = render_block_core_rss( $attributes );
164+
165+
$this->assertStringContainsString( '<time datetime=', $rendered_html, 'No time element found in rendered HTML' );
166+
167+
$this->assertStringContainsString( 'March 19, 2025', $rendered_html, 'Formatted date not found in rendered HTML' );
168+
169+
if ( preg_match( '/<time datetime="([^"]*)"/', $rendered_html, $matches ) ) {
170+
$datetime_attr = $matches[1];
171+
$this->assertStringContainsString( '2025-03-19', $datetime_attr, 'ISO datetime format missing expected date' );
172+
} else {
173+
$this->fail( 'Could not find datetime attribute in time element' );
174+
}
175+
176+
update_option( 'date_format', $original_date_format );
177+
update_option( 'gmt_offset', $original_gmt_offset );
178+
}
179+
}

0 commit comments

Comments
 (0)