197 lines
7.3 KiB
PHP
197 lines
7.3 KiB
PHP
<?php
|
|
/**
|
|
* Tests for server-rendered editorial blocks.
|
|
*
|
|
* @package CHRNX
|
|
*/
|
|
|
|
/** Verify block registration, fixed art, and safe dynamic rendering. */
|
|
class BlockRenderingTest extends WP_UnitTestCase {
|
|
/** Register dependencies and blocks. */
|
|
public function set_up() {
|
|
parent::set_up();
|
|
do_action( 'init' );
|
|
chrnx_seed_editorial_terms();
|
|
delete_option( 'chrnx_subscribe_page_id' );
|
|
}
|
|
|
|
/** All seven block types register from metadata. */
|
|
public function test_all_editorial_blocks_are_registered() {
|
|
$names = array( 'featured-stories', 'journal-stream', 'latest-podcast', 'podcast-meta', 'topic-hero', 'related-posts', 'subscribe-cta' );
|
|
foreach ( $names as $name ) {
|
|
$type = WP_Block_Type_Registry::get_instance()->get_registered( 'chrnx/' . $name );
|
|
$this->assertInstanceOf( WP_Block_Type::class, $type, $name );
|
|
$this->assertTrue( is_callable( $type->render_callback ), $name );
|
|
}
|
|
}
|
|
|
|
/** Topic art is an exact fixed mapping and rejects arbitrary paths. */
|
|
public function test_topic_art_uses_only_the_fixed_mapping() {
|
|
$expected = array(
|
|
'research' => 'alter-ego-research.png',
|
|
'code' => 'alter-ego-code.png',
|
|
'literature' => 'alter-ego-literature.png',
|
|
'music' => 'the-no-names-musician-cutout.png',
|
|
'podcasts' => 'alter-ego-podcasts.png',
|
|
'life' => 'alter-ego-life.png',
|
|
'gaming' => 'alter-ego-gaming.png',
|
|
);
|
|
foreach ( $expected as $slug => $filename ) {
|
|
$this->assertSame( 'assets/images/' . $filename, chrnx_get_topic_art_asset( $slug ) );
|
|
}
|
|
foreach ( array( '', 'unknown', '../secret.png', 'assets/images/anything.png' ) as $value ) {
|
|
$this->assertSame( '', chrnx_get_topic_art_asset( $value ) );
|
|
}
|
|
}
|
|
|
|
/** Podcast metadata renders known text and HTTPS links, never embed HTML. */
|
|
public function test_podcast_meta_renders_only_known_escaped_fields() {
|
|
$post_id = self::factory()->post->create( array( 'post_title' => 'Episode' ) );
|
|
update_post_meta( $post_id, 'chrnx_episode_number', 42 );
|
|
update_post_meta( $post_id, 'chrnx_episode_duration', '1:02:03' );
|
|
update_post_meta( $post_id, 'chrnx_episode_participants', array( 'Ada & Bob', '<script>alert(1)</script>' ) );
|
|
update_post_meta( $post_id, 'canonical_episode_url', 'https://example.test/episode' );
|
|
update_post_meta( $post_id, 'chrnx_buzzsprout_embed', '<iframe src="https://evil.test"></iframe>' );
|
|
|
|
$output = $this->render_for_post( 'chrnx/podcast-meta', $post_id );
|
|
$this->assertStringContainsString( '42', $output );
|
|
$this->assertStringContainsString( '1:02:03', $output );
|
|
$this->assertStringContainsString( 'Ada & Bob', $output );
|
|
$this->assertStringContainsString( 'https://example.test/episode', $output );
|
|
$this->assertStringNotContainsString( '<script', $output );
|
|
$this->assertStringNotContainsString( '<iframe', $output );
|
|
$this->assertSame( 1, substr_count( $output, 'https://example.test/episode' ) );
|
|
}
|
|
|
|
/** Invalid/empty metadata is omitted rather than guessed. */
|
|
public function test_podcast_meta_omits_unknown_or_empty_values() {
|
|
$post_id = self::factory()->post->create();
|
|
update_post_meta( $post_id, 'canonical_episode_url', 'http://example.test/episode' );
|
|
$output = $this->render_for_post( 'chrnx/podcast-meta', $post_id );
|
|
$this->assertStringNotContainsString( 'http://example.test', $output );
|
|
$this->assertStringNotContainsString( 'Episode 0', $output );
|
|
}
|
|
|
|
/** Latest podcast is a card/link and never a player. */
|
|
public function test_latest_podcast_renders_a_linked_card_without_player() {
|
|
$post_id = self::factory()->post->create( array( 'post_title' => 'Safe Podcast' ) );
|
|
wp_set_object_terms( $post_id, 'podcasts', 'chrnx_topic' );
|
|
wp_set_object_terms( $post_id, 'silicon-ranch', 'chrnx_show' );
|
|
|
|
$output = render_block(
|
|
array(
|
|
'blockName' => 'chrnx/latest-podcast',
|
|
'attrs' => array(),
|
|
'innerBlocks' => array(),
|
|
'innerHTML' => '',
|
|
'innerContent' => array(),
|
|
)
|
|
);
|
|
$this->assertStringContainsString( '<article', $output );
|
|
$this->assertStringContainsString( 'Safe Podcast', $output );
|
|
$this->assertStringContainsString( esc_url( get_permalink( $post_id ) ), $output );
|
|
$this->assertStringNotContainsString( '<audio', $output );
|
|
$this->assertStringNotContainsString( '<iframe', $output );
|
|
}
|
|
|
|
/** Topic hero uses a mapped explicit topic and falls back accessibly if art is absent. */
|
|
public function test_topic_hero_rejects_paths_and_uses_css_fallback_for_missing_art() {
|
|
$output = render_block(
|
|
array(
|
|
'blockName' => 'chrnx/topic-hero',
|
|
'attrs' => array(
|
|
'topic' => 'research',
|
|
'headingLevel' => 2,
|
|
),
|
|
'innerBlocks' => array(),
|
|
'innerHTML' => '',
|
|
'innerContent' => array(),
|
|
)
|
|
);
|
|
$this->assertStringContainsString( '<h2', $output );
|
|
$this->assertStringContainsString( 'Research', $output );
|
|
$this->assertStringContainsString( 'aria-hidden="true"', $output );
|
|
$this->assertStringNotContainsString( '<img', $output );
|
|
|
|
$unsafe = render_block(
|
|
array(
|
|
'blockName' => 'chrnx/topic-hero',
|
|
'attrs' => array( 'topic' => '../secret.png' ),
|
|
'innerBlocks' => array(),
|
|
'innerHTML' => '',
|
|
'innerContent' => array(),
|
|
)
|
|
);
|
|
$this->assertSame( '', $unsafe );
|
|
}
|
|
|
|
/** Subscribe CTA is exactly empty until a published Page is configured. */
|
|
public function test_subscribe_cta_requires_a_valid_published_page() {
|
|
$block = array(
|
|
'blockName' => 'chrnx/subscribe-cta',
|
|
'attrs' => array(),
|
|
'innerBlocks' => array(),
|
|
'innerHTML' => '',
|
|
'innerContent' => array(),
|
|
);
|
|
$this->assertSame( '', render_block( $block ) );
|
|
|
|
$page = self::factory()->post->create(
|
|
array(
|
|
'post_type' => 'page',
|
|
'post_status' => 'publish',
|
|
'post_title' => 'Subscribe',
|
|
)
|
|
);
|
|
update_option( 'chrnx_subscribe_page_id', $page );
|
|
$output = render_block( $block );
|
|
$this->assertStringContainsString( '<section', $output );
|
|
$this->assertStringContainsString( esc_url( get_permalink( $page ) ), $output );
|
|
}
|
|
|
|
/** Featured cards render only real stories and use a CSS image fallback. */
|
|
public function test_featured_stories_have_semantic_variable_cards_and_no_placeholder_images() {
|
|
$post_id = self::factory()->post->create( array( 'post_title' => 'Only Story' ) );
|
|
wp_set_object_terms( $post_id, 'code', 'chrnx_topic' );
|
|
$output = render_block(
|
|
array(
|
|
'blockName' => 'chrnx/featured-stories',
|
|
'attrs' => array(
|
|
'limit' => 3,
|
|
'headingLevel' => 2,
|
|
),
|
|
'innerBlocks' => array(),
|
|
'innerHTML' => '',
|
|
'innerContent' => array(),
|
|
)
|
|
);
|
|
|
|
$this->assertSame( 1, substr_count( $output, '<article' ) );
|
|
$this->assertStringContainsString( 'Only Story', $output );
|
|
$this->assertStringContainsString( 'aria-hidden="true"', $output );
|
|
$this->assertStringNotContainsString( 'placeholder', strtolower( $output ) );
|
|
}
|
|
|
|
/** Render one dynamic post-context block for a specific post. */
|
|
private function render_for_post( $block_name, $post_id ) {
|
|
global $post;
|
|
$previous = $post;
|
|
$post = get_post( $post_id );
|
|
setup_postdata( $post );
|
|
try {
|
|
return render_block(
|
|
array(
|
|
'blockName' => $block_name,
|
|
'attrs' => array(),
|
|
'innerBlocks' => array(),
|
|
'innerHTML' => '',
|
|
'innerContent' => array(),
|
|
)
|
|
);
|
|
} finally {
|
|
wp_reset_postdata();
|
|
$post = $previous;
|
|
}
|
|
}
|
|
}
|