| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
namespace MediaWiki\Skins\Vector\Tests\Unit\Components; |
| 23 |
|
| 24 |
use MediaWiki\Config\HashConfig; |
| 25 |
use MediaWiki\Message\Message; |
| 26 |
use MediaWiki\Skins\Vector\Components\VectorComponentTableOfContents; |
| 27 |
use MediaWiki\Skins\Vector\FeatureManagement\FeatureManager; |
| 28 |
use MessageLocalizer; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
class VectorComponentTableOfContentsTest extends \MediaWikiUnitTestCase { |
| 36 |
|
| 37 |
public static function provideGetTocData() { |
| 38 |
$config = [ |
| 39 |
'VectorTableOfContentsCollapseAtCount' => 1 |
| 40 |
]; |
| 41 |
$tocData = [ |
| 42 |
'number-section-count' => 2, |
| 43 |
'array-sections' => [ |
| 44 |
[ |
| 45 |
'toclevel' => 1, |
| 46 |
'level' => '2', |
| 47 |
'line' => 'A', |
| 48 |
'number' => '1', |
| 49 |
'index' => '1', |
| 50 |
'fromtitle' => 'Test', |
| 51 |
'byteoffset' => 231, |
| 52 |
'anchor' => 'A', |
| 53 |
'linkAnchor' => 'A', |
| 54 |
'array-sections' => [], |
| 55 |
'is-top-level-section' => true, |
| 56 |
'is-parent-section' => false, |
| 57 |
], |
| 58 |
[ |
| 59 |
'toclevel' => 1, |
| 60 |
'level' => '4', |
| 61 |
'line' => 'B', |
| 62 |
'number' => '2', |
| 63 |
'index' => '2', |
| 64 |
'fromtitle' => 'Test', |
| 65 |
'byteoffset' => 245, |
| 66 |
'anchor' => 'B', |
| 67 |
'linkAnchor' => 'B', |
| 68 |
'array-sections' => [], |
| 69 |
'is-top-level-section' => true, |
| 70 |
'is-parent-section' => false, |
| 71 |
] |
| 72 |
] |
| 73 |
]; |
| 74 |
$nestedTocData = [ |
| 75 |
'number-section-count' => 2, |
| 76 |
'array-sections' => [ |
| 77 |
[ |
| 78 |
'toclevel' => 1, |
| 79 |
'level' => '2', |
| 80 |
'line' => 'A', |
| 81 |
'number' => '1', |
| 82 |
'index' => '1', |
| 83 |
'fromtitle' => 'Test', |
| 84 |
'byteoffset' => 231, |
| 85 |
'anchor' => 'A', |
| 86 |
'linkAnchor' => 'A', |
| 87 |
'vector-button-label' => 'vector-toc-toggle-button-label', |
| 88 |
'array-sections' => [ |
| 89 |
'toclevel' => 2, |
| 90 |
'level' => '4', |
| 91 |
'line' => 'A1', |
| 92 |
'number' => '1.1', |
| 93 |
'index' => '2', |
| 94 |
'fromtitle' => 'Test', |
| 95 |
'byteoffset' => 245, |
| 96 |
'anchor' => 'A1', |
| 97 |
'linkAnchor' => 'A1', |
| 98 |
'array-sections' => [], |
| 99 |
'is-top-level-section' => false, |
| 100 |
'is-parent-section' => false, |
| 101 |
], |
| 102 |
'is-top-level-section' => true, |
| 103 |
'is-parent-section' => true, |
| 104 |
], |
| 105 |
] |
| 106 |
]; |
| 107 |
|
| 108 |
$expectedConfigData = [ |
| 109 |
'vector-is-collapse-sections-enabled' => |
| 110 |
count( $tocData['array-sections'] ) > 3 && |
| 111 |
$tocData[ 'number-section-count' ] >= $config[ 'VectorTableOfContentsCollapseAtCount' ], |
| 112 |
'id' => 'vector-toc', |
| 113 |
'data-pinnable-header' => [ |
| 114 |
'is-pinned' => true, |
| 115 |
'data-pinnable-element-id' => 'vector-toc', |
| 116 |
'data-feature-name' => 'toc-pinned', |
| 117 |
'label' => 'vector-toc-label', |
| 118 |
'unpin-label' => 'vector-unpin-element-label', |
| 119 |
'pin-label' => 'vector-pin-element-label', |
| 120 |
'label-tag-name' => 'h2' |
| 121 |
] |
| 122 |
]; |
| 123 |
$expectedNestedTocData = array_merge( $nestedTocData, $expectedConfigData ); |
| 124 |
|
| 125 |
return [ |
| 126 |
|
| 127 |
[ |
| 128 |
[], |
| 129 |
$config, |
| 130 |
|
| 131 |
[] |
| 132 |
], |
| 133 |
|
| 134 |
[ |
| 135 |
$tocData, |
| 136 |
array_merge( $config, [ 'VectorTableOfContentsCollapseAtCount' => 3 ] ), |
| 137 |
|
| 138 |
array_merge( $tocData, $expectedConfigData, [ |
| 139 |
'vector-is-collapse-sections-enabled' => false |
| 140 |
] ) |
| 141 |
], |
| 142 |
|
| 143 |
[ |
| 144 |
$tocData, |
| 145 |
array_merge( $config, [ 'VectorTableOfContentsCollapseAtCount' => 2 ] ), |
| 146 |
|
| 147 |
array_merge( $tocData, $expectedConfigData ) |
| 148 |
], |
| 149 |
|
| 150 |
[ |
| 151 |
$tocData, |
| 152 |
array_merge( $config, [ 'VectorTableOfContentsCollapseAtCount' => 1 ] ), |
| 153 |
|
| 154 |
array_merge( $tocData, $expectedConfigData ) |
| 155 |
], |
| 156 |
|
| 157 |
[ |
| 158 |
$nestedTocData, |
| 159 |
$config, |
| 160 |
|
| 161 |
$expectedNestedTocData |
| 162 |
], |
| 163 |
]; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
public function testGetTemplateData( |
| 171 |
array $tocData, |
| 172 |
array $config, |
| 173 |
array $expected |
| 174 |
) { |
| 175 |
$localizer = $this->createMock( MessageLocalizer::class ); |
| 176 |
$localizer->method( 'msg' )->willReturnCallback( function ( $key, ...$params ) { |
| 177 |
$msg = $this->createMock( Message::class ); |
| 178 |
$msg->method( '__toString' )->willReturn( $key ); |
| 179 |
$msg->method( 'escaped' )->willReturn( $key ); |
| 180 |
$msg->method( 'rawParams' )->willReturnSelf(); |
| 181 |
$msg->method( 'text' )->willReturn( $key ); |
| 182 |
return $msg; |
| 183 |
} ); |
| 184 |
$featureManager = $this->createMock( FeatureManager::class ); |
| 185 |
$featureManager->method( 'isFeatureEnabled' )->willReturn( true ); |
| 186 |
|
| 187 |
$toc = new VectorComponentTableOfContents( |
| 188 |
$tocData, |
| 189 |
$localizer, |
| 190 |
new HashConfig( $config ), |
| 191 |
$featureManager |
| 192 |
); |
| 193 |
$this->assertEquals( $expected, $toc->getTemplateData() ); |
| 194 |
} |
| 195 |
} |