Debian Codegraph @

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

raw symbols SCIP
1 <?php
2
3 /**
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
18 *
19 * @file
20 * @since 1.42
21 */
22
23 namespace MediaWiki\Skins\Vector\Tests\Unit\Components;
24
25 use MediaWiki\Skins\Vector\Components\VectorComponentButton;
26 use MediaWikiUnitTestCase;
27
28 /**
29 * @group Vector
30 * @group Components
31 * @coversDefaultClass \MediaWiki\Skins\Vector\Components\VectorComponentButton
32 */
33 class VectorComponentButtonTest extends MediaWikiUnitTestCase {
34
35 /**
36 * Provides various configurations of VectorComponentButton to test different scenarios.
37 * Each case includes different combinations of the button's properties.
38 *
39 * @return array[] An array of test cases with parameters and expected values.
40 */
41 public function provideButtonData(): array {
42 return [
43 'Basic Button' => [
44 // The visible text on the button.
45 'label' => 'Click Me',
46 // CSS classes expected without additional properties.
47 'expectedClasses' => 'cdx-button',
48 // Default button weight.
49 'weight' => 'normal',
50 // Indicates that the button is not icon-only.
51 'iconOnly' => false,
52 // No link for a basic button.
53 'href' => null,
54 ],
55 'Button With Primary Weight' => [
56 // The visible text indicating a primary action.
57 'label' => 'Primary Action',
58 // Additional classes are expected due to the primary weight.
59 'expectedClasses' =>
60 'cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-primary',
61 // Indicates primary visual importance.
62 'weight' => 'primary',
63 // Still not an icon-only button.
64 'iconOnly' => false,
65 // Providing an href activates additional styles.
66 'href' => '/mock-link',
67 ],
68 'Icon Only Button' => [
69 // No visible text for an icon-only button.
70 'label' => '',
71 // CSS classes specifically for icon-only.
72 'expectedClasses' => 'cdx-button cdx-button--icon-only',
73 // Default weight even for icon-only buttons.
74 'weight' => 'normal',
75 // This button is icon-only.
76 'iconOnly' => true,
77 // No link for this icon-only button.
78 'href' => null,
79 ],
80 ];
81 }
82
83 /**
84 * Tests CSS class generation logic within VectorComponentButton.
85 * This method verifies that the class string is generated correctly based on the button's properties.
86 *
87 * @covers ::getClasses
88 */
89 public function testGetClasses() {
90 $basicButton = new VectorComponentButton( 'Label' );
91 $templateData = $basicButton->getTemplateData();
92 $this->assertStringContainsString( 'cdx-button', $templateData['class'],
93 'Basic button should have cdx-button class.' );
94
95 $primaryButton = new VectorComponentButton( 'Label', null, null, null, [], 'primary' );
96 $templateData = $primaryButton->getTemplateData();
97 $this->assertStringContainsString( 'cdx-button--weight-primary', $templateData['class'],
98 'Primary button should have primary weight class.' );
99
100 $iconOnlyButton = new VectorComponentButton(
101 'Label', null, null, null, [], 'normal', 'default', true );
102 $templateData = $iconOnlyButton->getTemplateData();
103 $this->assertStringContainsString( 'cdx-button--icon-only', $templateData['class'],
104 'Icon-only button should have icon-only class.' );
105
106 $destructiveButton = new VectorComponentButton( 'Label', null, null, null, [],
107 'normal', 'destructive' );
108 $templateData = $destructiveButton->getTemplateData();
109 $this->assertStringContainsString( 'cdx-button--action-destructive', $templateData['class'],
110 'Destructive button should have destructive action class.' );
111
112 $progressiveButton = new VectorComponentButton( 'Label', null, null, null, [],
113 'normal', 'progressive' );
114 $templateData = $progressiveButton->getTemplateData();
115 $this->assertStringContainsString( 'cdx-button--action-progressive', $templateData['class'],
116 'Progressive button should have progressive action class.' );
117
118 $quietButton = new VectorComponentButton( 'Label', null, null, null, [], 'quiet' );
119 $templateData = $quietButton->getTemplateData();
120 $this->assertStringContainsString( 'cdx-button--weight-quiet', $templateData['class'],
121 'Quiet button should have quiet weight class.' );
122 }
123
124 /**
125 * Tests the `getTemplateData` method of VectorComponentButton component.
126 * Each data set provided by `provideButtonData` is passed here to verify the component's output.
127 *
128 * @covers ::__construct
129 * @dataProvider provideButtonData
130 */
131 public function testGetTemplateData(
132 string $label,
133 string $expectedClasses,
134 string $weight,
135 bool $iconOnly,
136 ?string $href
137 ) {
138 // Instantiate the component with the provided configuration.
139 $button = new VectorComponentButton(
140 $label,
141 'icon-sample',
142 'btn-id',
143 'additional-class',
144 // Custom data attribute as an example.
145 [ 'data-test' => 'true' ],
146 $weight,
147 // Default action type.
148 'default',
149 $iconOnly,
150 $href
151 );
152
153 // Acquire the generated template data from the component.
154 $templateData = $button->getTemplateData();
155
156 // Assert each aspect of the template data matches expectations.
157 $this->assertEquals( $label, $templateData['label'] );
158 $this->assertEquals( 'icon-sample', $templateData['icon'] );
159 $this->assertEquals( 'btn-id', $templateData['id'] );
160 // Ensures the class string contains all expected CSS classes.
161 $this->assertStringContainsString( $expectedClasses, $templateData['class'] );
162 $this->assertEquals( $href, $templateData['href'] );
163 // Verifies custom attributes are included appropriately.
164 $this->assertContains( [ 'key' => 'data-test', 'value' => 'true' ], $templateData['array-attributes'] );
165 }
166 }