BLOCK_CONTENT;
$rendered_block = do_blocks( $tabs_block );
$processor = new WP_HTML_Tag_Processor( $rendered_block );
$this->assertTrue( $processor->next_tag( array( 'class_name' => 'wp-block-tab-list' ) ) );
$this->assertSame( 'Product details', $processor->get_attribute( 'aria-label' ) );
}
/**
* @covers ::block_core_tabs_render_callback
* @covers ::block_core_tab_list_render_callback
*/
public function test_should_add_default_tab_list_aria_label(): void {
$tabs_block = <<<'BLOCK_CONTENT'
BLOCK_CONTENT;
$rendered_block = do_blocks( $tabs_block );
$processor = new WP_HTML_Tag_Processor( $rendered_block );
$this->assertTrue( $processor->next_tag( array( 'class_name' => 'wp-block-tab-list' ) ) );
$this->assertSame( 'Tabbed content', $processor->get_attribute( 'aria-label' ) );
}
/**
* @covers ::block_core_tabs_provide_context
* @covers ::block_core_tab_list_render_callback
* @covers ::block_core_tab_panel_render
*/
public function test_should_number_tabs_and_tab_ids_from_one(): void {
$tabs_block = <<<'BLOCK_CONTENT'
BLOCK_CONTENT;
$first_block = do_blocks( $tabs_block );
$second_block = do_blocks( $tabs_block );
// Another block on the page consumes the shared `wp_unique_id()` counter.
wp_unique_id( 'unrelated-' );
$third_block = do_blocks( $tabs_block );
$first_number = $this->get_tabs_number( $first_block );
$second_number = $this->get_tabs_number( $second_block );
$third_number = $this->get_tabs_number( $third_block );
/*
* Rendering a tabs block advances the `tabs_` counter by a fixed amount,
* so the unrelated ID must leave the gap between two blocks unchanged.
*/
$this->assertSame(
$second_number - $first_number,
$third_number - $second_number,
'The unrelated ID should not have advanced the tabs counter.'
);
$processor = new WP_HTML_Tag_Processor( $first_block . $second_block . $third_block );
foreach ( array( $first_number, $second_number, $third_number ) as $number ) {
foreach ( array( 1, 2 ) as $tab_number ) {
$tab_id = "tabs_{$number}-tab-{$tab_number}";
$processor->next_tag( array( 'tag_name' => 'button' ) );
$this->assertSame( "tab__{$tab_id}", $processor->get_attribute( 'id' ) );
$this->assertSame( $tab_id, $processor->get_attribute( 'aria-controls' ) );
}
foreach ( array( 1, 2 ) as $tab_number ) {
$tab_id = "tabs_{$number}-tab-{$tab_number}";
$processor->next_tag( array( 'class_name' => 'wp-block-tab-panel' ) );
$this->assertSame( $tab_id, $processor->get_attribute( 'id' ) );
$this->assertSame( "tab__{$tab_id}", $processor->get_attribute( 'aria-labelledby' ) );
}
}
}
/**
* Reads the instance number out of the `tabsId` a rendered tabs block was given.
*
* @param string $rendered_block Rendered tabs block.
*
* @return int Tabs instance number.
*/
private function get_tabs_number( string $rendered_block ): int {
$processor = new WP_HTML_Tag_Processor( $rendered_block );
$processor->next_tag( array( 'class_name' => 'wp-block-tabs' ) );
$context = json_decode( (string) $processor->get_attribute( 'data-wp-context' ), true );
$tabs_id = $context['tabsId'] ?? '';
$this->assertStringStartsWith( 'tabs_', $tabs_id );
return (int) substr( $tabs_id, strlen( 'tabs_' ) );
}
/**
* An inactive panel is hidden with `until-found` rather than outright, so
* that the browser's find-in-page can still reach its content. Which panel
* is hidden is left to the client, so a visitor without JavaScript is given
* the content of every panel rather than none of it.
*
* @covers ::block_core_tab_panel_render
*/
public function test_should_leave_hiding_tab_panels_to_the_client(): void {
$tabs_block = <<<'BLOCK_CONTENT'
BLOCK_CONTENT;
$processor = new WP_HTML_Tag_Processor( do_blocks( $tabs_block ) );
$panels = array();
while ( $processor->next_tag( array( 'class_name' => 'wp-block-tab-panel' ) ) ) {
$panels[] = array(
'hidden' => $processor->get_attribute( 'hidden' ),
'hidden_binding' => $processor->get_attribute( 'data-wp-bind--hidden' ),
'tabindex_binding' => $processor->get_attribute( 'data-wp-bind--tabindex' ),
'beforematch' => $processor->get_attribute( 'data-wp-on--beforematch' ),
);
}
$this->assertCount( 2, $panels, 'Both tab panels should be rendered.' );
foreach ( $panels as $index => $panel ) {
$this->assertNull(
$panel['hidden'],
"Panel $index should not be hidden before the block hydrates."
);
$this->assertSame(
'state.isHidden',
$panel['hidden_binding'],
"Panel $index should be hidden by the client when its tab is inactive."
);
$this->assertSame(
'state.tabIndexAttribute',
$panel['tabindex_binding'],
"Panel $index should leave the tab sequence while it is hidden."
);
$this->assertSame(
'actions.handleBeforeMatch',
$panel['beforematch'],
"Panel $index should activate its tab when the browser reveals it."
);
}
}
/**
* A default tab index past the last tab, for example one left behind when
* the default tab was removed, falls back to the first tab so that a panel
* is still shown.
*
* @covers ::block_core_tabs_render_block_callback
*
* @dataProvider data_default_tab_index
*
* @param int $saved_index Default tab index saved in the block.
* @param int $expected_index Default tab index passed to the front end.
*/
public function test_should_fall_back_to_first_tab_when_default_tab_index_is_out_of_range( int $saved_index, int $expected_index ): void {
$tabs_block = <<<'BLOCK_CONTENT'
BLOCK_CONTENT;
$processor = new WP_HTML_Tag_Processor( do_blocks( sprintf( $tabs_block, $saved_index ) ) );
$processor->next_tag( array( 'class_name' => 'wp-block-tabs' ) );
$context = json_decode( (string) $processor->get_attribute( 'data-wp-context' ), true );
$this->assertSame( $expected_index, $context['activeTabIndex'] ?? null );
}
/**
* Data provider.
*
* @return array
*/
public function data_default_tab_index(): array {
return array(
'existing default tab' => array(
'saved_index' => 1,
'expected_index' => 1,
),
'default tab past the last tab' => array(
'saved_index' => 2,
'expected_index' => 0,
),
);
}
}