1 ]; $tocData = [ 'number-section-count' => 2, 'array-sections' => [ [ 'toclevel' => 1, 'level' => '2', 'line' => 'A', 'number' => '1', 'index' => '1', 'fromtitle' => 'Test', 'byteoffset' => 231, 'anchor' => 'A', 'linkAnchor' => 'A', 'array-sections' => [], 'is-top-level-section' => true, 'is-parent-section' => false, ], [ 'toclevel' => 1, 'level' => '4', 'line' => 'B', 'number' => '2', 'index' => '2', 'fromtitle' => 'Test', 'byteoffset' => 245, 'anchor' => 'B', 'linkAnchor' => 'B', 'array-sections' => [], 'is-top-level-section' => true, 'is-parent-section' => false, ] ] ]; $nestedTocData = [ 'number-section-count' => 2, 'array-sections' => [ [ 'toclevel' => 1, 'level' => '2', 'line' => 'A', 'number' => '1', 'index' => '1', 'fromtitle' => 'Test', 'byteoffset' => 231, 'anchor' => 'A', 'linkAnchor' => 'A', 'vector-button-label' => 'vector-toc-toggle-button-label', 'array-sections' => [ 'toclevel' => 2, 'level' => '4', 'line' => 'A1', 'number' => '1.1', 'index' => '2', 'fromtitle' => 'Test', 'byteoffset' => 245, 'anchor' => 'A1', 'linkAnchor' => 'A1', 'array-sections' => [], 'is-top-level-section' => false, 'is-parent-section' => false, ], 'is-top-level-section' => true, 'is-parent-section' => true, ], ] ]; $expectedConfigData = [ 'vector-is-collapse-sections-enabled' => count( $tocData['array-sections'] ) > 3 && $tocData[ 'number-section-count' ] >= $config[ 'VectorTableOfContentsCollapseAtCount' ], 'id' => 'vector-toc', 'data-pinnable-header' => [ 'is-pinned' => true, 'data-pinnable-element-id' => 'vector-toc', 'data-feature-name' => 'toc-pinned', 'label' => 'vector-toc-label', 'unpin-label' => 'vector-unpin-element-label', 'pin-label' => 'vector-pin-element-label', 'label-tag-name' => 'h2' ] ]; $expectedNestedTocData = array_merge( $nestedTocData, $expectedConfigData ); return [ // When zero sections [ [], $config, // TOC data is empty when given an empty array [] ], // When number of multiple sections is lower than configured value [ $tocData, array_merge( $config, [ 'VectorTableOfContentsCollapseAtCount' => 3 ] ), // 'vector-is-collapse-sections-enabled' value is false array_merge( $tocData, $expectedConfigData, [ 'vector-is-collapse-sections-enabled' => false ] ) ], // When number of multiple sections is equal to the configured value [ $tocData, array_merge( $config, [ 'VectorTableOfContentsCollapseAtCount' => 2 ] ), // 'vector-is-collapse-sections-enabled' value is true array_merge( $tocData, $expectedConfigData ) ], // When number of multiple sections is higher than configured value [ $tocData, array_merge( $config, [ 'VectorTableOfContentsCollapseAtCount' => 1 ] ), // 'vector-is-collapse-sections-enabled' value is true array_merge( $tocData, $expectedConfigData ) ], // When TOC has sections with top level parent sections [ $nestedTocData, $config, // 'vector-button-label' is provided for top level parent sections $expectedNestedTocData ], ]; } /** * @covers ::getTemplateData * @dataProvider provideGetTOCData */ public function testGetTemplateData( array $tocData, array $config, array $expected ) { $localizer = $this->createMock( MessageLocalizer::class ); $localizer->method( 'msg' )->willReturnCallback( function ( $key, ...$params ) { $msg = $this->createMock( Message::class ); $msg->method( '__toString' )->willReturn( $key ); $msg->method( 'escaped' )->willReturn( $key ); $msg->method( 'rawParams' )->willReturnSelf(); $msg->method( 'text' )->willReturn( $key ); return $msg; } ); $featureManager = $this->createMock( FeatureManager::class ); $featureManager->method( 'isFeatureEnabled' )->willReturn( true ); $toc = new VectorComponentTableOfContents( $tocData, $localizer, new HashConfig( $config ), $featureManager ); $this->assertEquals( $expected, $toc->getTemplateData() ); } }