feat: configure Related Stories rendering
This commit is contained in:
4 files changed
+279
-8
No files matched your search
@@ -801,6 +801,200 @@ class BlockRenderingTest extends WP_UnitTestCase {
|
||||
$this->assertStringNotContainsString( '<img', $output );
|
||||
}
|
||||
|
||||
/** Provide strict Related Stories limit inputs and their expected card counts. */
|
||||
public function related_limit_attributes_provider(): array {
|
||||
return array(
|
||||
'missing' => array( array(), 3 ),
|
||||
'one' => array( array( 'limit' => 1 ), 3 ),
|
||||
'twelve' => array( array( 'limit' => 12 ), 3 ),
|
||||
'string five' => array( array( 'limit' => '5' ), 3 ),
|
||||
'negative' => array( array( 'limit' => -5 ), 3 ),
|
||||
'array' => array( array( 'limit' => array( 5 ) ), 3 ),
|
||||
'float' => array( array( 'limit' => 5.0 ), 3 ),
|
||||
'integer five' => array( array( 'limit' => 5 ), 5 ),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Related Stories renders five only for the exact integer and defaults every other limit to three.
|
||||
*
|
||||
* @dataProvider related_limit_attributes_provider
|
||||
*/
|
||||
public function test_related_posts_strictly_allowlists_limit( array $attributes, int $expected_count ): void {
|
||||
$current = $this->create_related_posts_fixture( 5 );
|
||||
$output = $this->render_for_post( 'chrnx/related-posts', $current, $attributes );
|
||||
|
||||
$this->assertSame( $expected_count, $this->count_html_tags( $output, 'ARTICLE' ) );
|
||||
}
|
||||
|
||||
/** Provide malformed Related Stories layouts that must fail closed. */
|
||||
public function related_invalid_layout_attributes_provider(): array {
|
||||
return array(
|
||||
'missing' => array( array() ),
|
||||
'empty' => array( array( 'layout' => '' ) ),
|
||||
'unknown' => array( array( 'layout' => 'masonry' ) ),
|
||||
'injected class' => array( array( 'layout' => 'carousel evil' ) ),
|
||||
'integer' => array( array( 'layout' => 1 ) ),
|
||||
'array' => array( array( 'layout' => array( 'grid' ) ) ),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Missing and malformed layouts render only the adaptive layout class.
|
||||
*
|
||||
* @dataProvider related_invalid_layout_attributes_provider
|
||||
*/
|
||||
public function test_related_posts_defaults_invalid_layouts_to_adaptive( array $attributes ): void {
|
||||
$current = $this->create_related_posts_fixture( 1 );
|
||||
$output = $this->render_for_post( 'chrnx/related-posts', $current, $attributes );
|
||||
|
||||
$this->assertSame( array( 'is-layout-adaptive' ), $this->get_related_layout_classes( $output ) );
|
||||
}
|
||||
|
||||
/** Provide every approved Related Stories layout and its rail semantics. */
|
||||
public function related_layout_provider(): array {
|
||||
return array(
|
||||
'adaptive' => array( 'adaptive', true ),
|
||||
'grid' => array( 'grid', false ),
|
||||
'carousel' => array( 'carousel', true ),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Approved layouts emit one exact class and only carousel-capable semantics and controls.
|
||||
*
|
||||
* @dataProvider related_layout_provider
|
||||
*/
|
||||
public function test_related_posts_render_layout_semantics( string $layout, bool $is_carousel_capable ): void {
|
||||
$current = $this->create_related_posts_fixture( 1 );
|
||||
$output = $this->render_for_post(
|
||||
'chrnx/related-posts',
|
||||
$current,
|
||||
array( 'layout' => $layout )
|
||||
);
|
||||
|
||||
$this->assertSame( array( 'is-layout-' . $layout ), $this->get_related_layout_classes( $output ) );
|
||||
|
||||
$processor = new WP_HTML_Tag_Processor( $output );
|
||||
$this->assertTrue( $processor->next_tag( array( 'tag_name' => 'H2' ) ) );
|
||||
$heading_id = $processor->get_attribute( 'id' );
|
||||
$this->assertNotEmpty( $heading_id );
|
||||
$this->assertTrue(
|
||||
$processor->next_tag(
|
||||
array(
|
||||
'tag_name' => 'DIV',
|
||||
'class_name' => 'chrnx-related-posts__items',
|
||||
)
|
||||
)
|
||||
);
|
||||
$this->assertTrue( $processor->get_attribute( 'data-chrnx-related-track' ) );
|
||||
|
||||
if ( $is_carousel_capable ) {
|
||||
$this->assertSame( 'region', $processor->get_attribute( 'role' ) );
|
||||
$this->assertSame( $heading_id, $processor->get_attribute( 'aria-labelledby' ) );
|
||||
$this->assertSame( '0', $processor->get_attribute( 'tabindex' ) );
|
||||
$this->assertTrue(
|
||||
$processor->next_tag(
|
||||
array(
|
||||
'tag_name' => 'DIV',
|
||||
'class_name' => 'chrnx-related-posts__controls',
|
||||
)
|
||||
)
|
||||
);
|
||||
$this->assertSame( 'group', $processor->get_attribute( 'role' ) );
|
||||
$this->assertTrue( $processor->get_attribute( 'hidden' ) );
|
||||
$this->assertSame( 2, $this->count_html_tags( $output, 'BUTTON' ) );
|
||||
} else {
|
||||
$this->assertNull( $processor->get_attribute( 'role' ) );
|
||||
$this->assertNull( $processor->get_attribute( 'aria-labelledby' ) );
|
||||
$this->assertNull( $processor->get_attribute( 'tabindex' ) );
|
||||
$this->assertFalse(
|
||||
$processor->next_tag(
|
||||
array(
|
||||
'tag_name' => 'DIV',
|
||||
'class_name' => 'chrnx-related-posts__controls',
|
||||
)
|
||||
)
|
||||
);
|
||||
$this->assertSame( 0, $this->count_html_tags( $output, 'BUTTON' ) );
|
||||
}
|
||||
}
|
||||
|
||||
/** Provide every non-empty partial Related Stories availability count. */
|
||||
public function related_partial_count_provider(): array {
|
||||
return array(
|
||||
'one available' => array( 1 ),
|
||||
'two available' => array( 2 ),
|
||||
'four available' => array( 4 ),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Related Stories renders only the eligible cards that actually exist.
|
||||
*
|
||||
* @dataProvider related_partial_count_provider
|
||||
*/
|
||||
public function test_related_posts_render_partial_availability( int $available ): void {
|
||||
$current = $this->create_related_posts_fixture( $available );
|
||||
$output = $this->render_for_post( 'chrnx/related-posts', $current, array( 'limit' => 5 ) );
|
||||
|
||||
$this->assertSame( $available, $this->count_html_tags( $output, 'ARTICLE' ) );
|
||||
}
|
||||
|
||||
/** Related Stories returns no section when there are no eligible destinations. */
|
||||
public function test_related_posts_render_nothing_without_results(): void {
|
||||
$current = $this->create_related_posts_fixture( 0 );
|
||||
$output = $this->render_for_post( 'chrnx/related-posts', $current, array( 'limit' => 5 ) );
|
||||
|
||||
$this->assertSame( 0, $this->count_html_tags( $output, 'SECTION' ) );
|
||||
$this->assertSame( '', $output );
|
||||
}
|
||||
|
||||
/** Related Stories navigation labels pass through the chrnx translation domain. */
|
||||
public function test_related_posts_localize_navigation_labels(): void {
|
||||
$calls = array();
|
||||
$translate = static function ( $translated, $text, $domain ) use ( &$calls ) {
|
||||
$labels = array(
|
||||
'Related stories navigation' => 'Navegación de historias',
|
||||
'Previous stories' => 'Historias anteriores',
|
||||
'Next stories' => 'Historias siguientes',
|
||||
);
|
||||
if ( isset( $labels[ $text ] ) ) {
|
||||
$calls[] = array( $text, $domain );
|
||||
return $labels[ $text ];
|
||||
}
|
||||
return $translated;
|
||||
};
|
||||
add_filter( 'gettext', $translate, 10, 3 );
|
||||
try {
|
||||
$current = $this->create_related_posts_fixture( 1 );
|
||||
$output = $this->render_for_post( 'chrnx/related-posts', $current, array( 'layout' => 'carousel' ) );
|
||||
} finally {
|
||||
remove_filter( 'gettext', $translate, 10 );
|
||||
}
|
||||
|
||||
$this->assertSame(
|
||||
array(
|
||||
array( 'Related stories navigation', 'chrnx' ),
|
||||
array( 'Previous stories', 'chrnx' ),
|
||||
array( 'Next stories', 'chrnx' ),
|
||||
),
|
||||
$calls
|
||||
);
|
||||
$processor = new WP_HTML_Tag_Processor( $output );
|
||||
$this->assertTrue(
|
||||
$processor->next_tag(
|
||||
array(
|
||||
'tag_name' => 'DIV',
|
||||
'class_name' => 'chrnx-related-posts__controls',
|
||||
)
|
||||
)
|
||||
);
|
||||
$this->assertSame( 'Navegación de historias', $processor->get_attribute( 'aria-label' ) );
|
||||
$this->assertStringContainsString( '>Historias anteriores</button>', $output );
|
||||
$this->assertStringContainsString( '>Historias siguientes</button>', $output );
|
||||
}
|
||||
|
||||
/** Topic heroes are the section surface and prioritize their approved portrait. */
|
||||
public function test_topic_hero_portrait_is_eager_and_high_priority() {
|
||||
$output = render_block(
|
||||
@@ -1085,7 +1279,7 @@ class BlockRenderingTest extends WP_UnitTestCase {
|
||||
}
|
||||
|
||||
/** Render one dynamic post-context block for a specific post. */
|
||||
private function render_for_post( $block_name, $post_id ) {
|
||||
private function render_for_post( $block_name, $post_id, $attributes = array() ) {
|
||||
global $post;
|
||||
$previous = $post;
|
||||
$post = get_post( $post_id );
|
||||
@@ -1094,7 +1288,7 @@ class BlockRenderingTest extends WP_UnitTestCase {
|
||||
return render_block(
|
||||
array(
|
||||
'blockName' => $block_name,
|
||||
'attrs' => array(),
|
||||
'attrs' => $attributes,
|
||||
'innerBlocks' => array(),
|
||||
'innerHTML' => '',
|
||||
'innerContent' => array(),
|
||||
@@ -1105,4 +1299,47 @@ class BlockRenderingTest extends WP_UnitTestCase {
|
||||
$post = $previous;
|
||||
}
|
||||
}
|
||||
|
||||
/** Create one current story and an exact number of eligible related stories. */
|
||||
private function create_related_posts_fixture( $available ) {
|
||||
$current = self::factory()->post->create( array( 'post_title' => 'Current related-story fixture' ) );
|
||||
wp_set_object_terms( $current, 'research', 'chrnx_topic' );
|
||||
for ( $index = 1; $index <= $available; ++$index ) {
|
||||
$related = self::factory()->post->create( array( 'post_title' => 'Related fixture ' . $index ) );
|
||||
wp_set_object_terms( $related, 'research', 'chrnx_topic' );
|
||||
}
|
||||
return $current;
|
||||
}
|
||||
|
||||
/** Count real parsed HTML tags instead of matching markup substrings. */
|
||||
private function count_html_tags( $html, $tag_name ) {
|
||||
$processor = new WP_HTML_Tag_Processor( $html );
|
||||
$count = 0;
|
||||
while ( $processor->next_tag( array( 'tag_name' => $tag_name ) ) ) {
|
||||
++$count;
|
||||
}
|
||||
return $count;
|
||||
}
|
||||
|
||||
/** Return only parsed Related Stories layout classes from the section wrapper. */
|
||||
private function get_related_layout_classes( $html ) {
|
||||
$processor = new WP_HTML_Tag_Processor( $html );
|
||||
$this->assertTrue(
|
||||
$processor->next_tag(
|
||||
array(
|
||||
'tag_name' => 'SECTION',
|
||||
'class_name' => 'chrnx-related-posts',
|
||||
)
|
||||
)
|
||||
);
|
||||
$classes = preg_split( '/\s+/', trim( (string) $processor->get_attribute( 'class' ) ) );
|
||||
return array_values(
|
||||
array_filter(
|
||||
$classes,
|
||||
static function ( $class_name ) {
|
||||
return 0 === strpos( $class_name, 'is-layout-' );
|
||||
}
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user