Debian Codegraph @

tests/phpunit/unit/includes/widget/UsersMultiselectWidgetTest.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\Tests\Widget;
23
24 use MediaWiki\Widget\UsersMultiselectWidget;
25 use MediaWikiUnitTestCase;
26 use Wikimedia\TestingAccessWrapper;
27
28 /**
29 * @covers \MediaWiki\Widget\UsersMultiselectWidget
30 */
31 class UsersMultiselectWidgetTest extends MediaWikiUnitTestCase {
32 protected bool $ipAllowed;
33 protected bool $ipRangeAllowed;
34 protected array $ipRangeLimits;
35
36 public function setUp(): void {
37 parent::setUp();
38 $this->ipAllowed = false;
39 $this->ipRangeAllowed = false;
40 $this->ipRangeLimits = [];
41 }
42
43 public function testConstruct() {
44 // Define the configuration to pass to the constructor
45 $config = [
46 'ipAllowed' => true,
47 'ipRangeAllowed' => true,
48 'ipRangeLimits' => [ 'min' => 0, 'max' => 32 ],
49 ];
50
51 // Create the UsersMultiselectWidget object with the above configuration
52 /** @var UsersMultiselectWidget $widget */
53 $widget = TestingAccessWrapper::newFromObject( new UsersMultiselectWidget( $config ) );
54
55 // Assert that ipAllowed is set correctly
56 $this->assertTrue( $widget->ipAllowed, 'ipAllowed was not set correctly.' );
57
58 // Assert that ipRangeAllowed is set correctly
59 $this->assertTrue( $widget->ipRangeAllowed, 'ipRangeAllowed was not set correctly.' );
60
61 // Assert that ipRangeLimits is set correctly
62 $this->assertSame( $config['ipRangeLimits'], $widget->ipRangeLimits,
63 'ipRangeLimits was not set correctly.' );
64 }
65
66 public function testGetJavaScriptClassName() {
67 // Define the configuration to pass to the constructor
68 $config = [
69 'ipAllowed' => true,
70 'ipRangeAllowed' => true,
71 'ipRangeLimits' => [ 'min' => 0, 'max' => 32 ],
72 ];
73
74 // Create the UsersMultiselectWidget object with the above configuration
75 /** @var UsersMultiselectWidget $widget */
76 $widget = TestingAccessWrapper::newFromObject( new UsersMultiselectWidget( $config ) );
77
78 // Assert that ipAllowed is set correctly
79 $this->assertSame( 'mw.widgets.UsersMultiselectWidget', $widget->getJavaScriptClassName(),
80 'getJavaScriptClassName did not return the expected value.' );
81 }
82
83 public function provideGetConfig(): array {
84 return [
85 'ipAllowed' => [
86 [
87 'ipAllowed' => true,
88 ],
89 [
90 'ipAllowed' => true,
91 'ipRangeAllowed' => false,
92 'ipRangeLimits' => [
93 'min' => 0,
94 'max' => 32,
95 ],
96 ],
97 ],
98 'ipRangeAllowed' => [
99 [
100 'ipRangeAllowed' => true,
101 ],
102 [
103 'ipAllowed' => false,
104 'ipRangeAllowed' => true,
105 'ipRangeLimits' => [
106 'min' => 0,
107 'max' => 32,
108 ],
109 ],
110 ],
111 'ipRangeLimits' => [
112 [
113 'ipRangeLimits' => [ 'min' => 0, 'max' => 64 ],
114 ],
115 [
116 'ipAllowed' => false,
117 'ipRangeAllowed' => false,
118 'ipRangeLimits' => [
119 'min' => 0,
120 'max' => 64,
121 ],
122 ],
123 ],
124 ];
125 }
126
127 /**
128 * @dataProvider provideGetConfig
129 */
130 public function testGetConfig( array $inputConfig, array $expected ) {
131 // Create the UsersMultiselectWidget object with the input configuration
132 /** @var UsersMultiselectWidget $widget */
133 $widget = TestingAccessWrapper::newFromObject( new UsersMultiselectWidget( $inputConfig ) );
134
135 $actualConfig = [
136 'ipAllowed' => false,
137 'ipRangeAllowed' => false,
138 'ipRangeLimits' => [
139 'min' => 0,
140 'max' => 32,
141 ],
142 ];
143
144 // TestingAccessWrapper cannot pass-by-ref directly - T287318
145 call_user_func_array( [ $widget, 'getConfig' ], [ &$actualConfig ] );
146
147 // Assert that the actualConfig matches the expected configuration
148 foreach ( $expected as $key => $value ) {
149 $this->assertArrayHasKey( $key, $actualConfig, "Config missing expected key '$key'" );
150 $this->assertEquals( $value, $actualConfig[$key], "Mismatch for key '$key'" );
151 }
152 }
153
154 }