Debian Codegraph @

skins/Vector/tests/phpunit/unit/components/VectorComponentPinnableHeaderTest.php

raw symbols SCIP
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @since 1.42
20 */
21
22 namespace MediaWiki\Skins\Vector\Tests\Unit\Components;
23
24 use MediaWiki\Message\Message;
25 use MediaWiki\Skins\Vector\Components\VectorComponentPinnableHeader;
26 use MediaWikiUnitTestCase;
27 use MessageLocalizer;
28
29 /**
30 * @group Vector
31 * @group Components
32 * @coversDefaultClass \MediaWiki\Skins\Vector\Components\VectorComponentPinnableHeader
33 */
34 class VectorComponentPinnableHeaderTest extends MediaWikiUnitTestCase {
35
36 /**
37 * This method provides different sets of parameters for tests, simulating different scenarios.
38 * @return array[]
39 */
40 public function provideTestCases(): array {
41 return [
42 // Test case with the header element intended to be movable.
43 'Pinnable Header With Moving Element' => [
44 // The header should start in a "pinned" state.
45 'pinned' => true,
46 // ID of the pinnable header element.
47 'id' => 'vector-example',
48 // Feature name for persistent state tracking.
49 'featureName' => 'example-pinned',
50 // Indicates the header is expected to move in the DOM.
51 'moveElement' => true,
52 // The type of label tag to be used.
53 'labelTagName' => 'div'
54 ],
55 // Test case with the header element fixed and not intended to be moved.
56 'Pinnable Header Without Moving Element' => [
57 // The header should start in an "unpinned" state.
58 'pinned' => false,
59 // ID of the pinnable header element.
60 'id' => 'vector-another-example',
61 // Feature name for tracking state.
62 'featureName' => 'another-example-pinned',
63 // Indicates the header will not move in the DOM.
64 'moveElement' => false,
65 // The type of label tag to be used, in this case, an h2.
66 'labelTagName' => 'h2'
67 ],
68 ];
69 }
70
71 /**
72 * Tests that the getTemplateData method returns the correct data.
73 * Uses data provided by provideTestCases to run the same test with different configurations.
74 * @covers ::getTemplateData
75 * @dataProvider provideTestCases
76 */
77 public function testGetTemplateData( $pinned, $id, $featureName, $moveElement, $labelTagName ) {
78 // Mocking the MessageLocalizer to provide predictable responses for given message keys.
79 $localizer = $this->createMock( MessageLocalizer::class );
80 $localizer->method( 'msg' )->willReturnCallback( function ( $key ) {
81 return $this->createConfiguredMock( Message::class, [
82 // Simulated localization output.
83 '__toString' => $key . '-mocked-label',
84 ] );
85 } );
86
87 // Instantiating the component with the provided test parameters.
88 $pinnableHeader = new VectorComponentPinnableHeader(
89 $localizer,
90 $pinned,
91 $id,
92 $featureName,
93 $moveElement,
94 $labelTagName
95 );
96
97 // Acquiring the template data from the component.
98 $templateData = $pinnableHeader->getTemplateData();
99
100 // Assertions to verify each piece of expected template data.
101 $this->assertEquals( $pinned, $templateData['is-pinned'] );
102 $this->assertStringEndsWith( '-mocked-label', $templateData['label'] );
103 $this->assertEquals( $labelTagName, $templateData['label-tag-name'] );
104 $this->assertStringEndsWith( '-mocked-label', $templateData['pin-label'] );
105 $this->assertStringEndsWith( '-mocked-label', $templateData['unpin-label'] );
106 $this->assertEquals( $id, $templateData['data-pinnable-element-id'] );
107 $this->assertEquals( $featureName, $templateData['data-feature-name'] );
108
109 // Additional checks for elements' IDs when the element can move.
110 if ( $moveElement ) {
111 $this->assertEquals( $id . '-unpinned-container', $templateData['data-unpinned-container-id'] );
112 $this->assertEquals( $id . '-pinned-container', $templateData['data-pinned-container-id'] );
113 } else {
114 $this->assertArrayNotHasKey( 'data-unpinned-container-id', $templateData );
115 $this->assertArrayNotHasKey( 'data-pinned-container-id', $templateData );
116 }
117 }
118 }