ipAllowed = false; $this->ipRangeAllowed = false; $this->ipRangeLimits = []; } public function testConstruct() { // Define the configuration to pass to the constructor $config = [ 'ipAllowed' => true, 'ipRangeAllowed' => true, 'ipRangeLimits' => [ 'min' => 0, 'max' => 32 ], ]; // Create the UsersMultiselectWidget object with the above configuration $widget = new UsersMultiselectWidget( $config ); // Use reflection to access the protected properties for testing $reflectionClass = new ReflectionClass( UsersMultiselectWidget::class ); // Assert that ipAllowed is set correctly $ipAllowedProperty = $reflectionClass->getProperty( 'ipAllowed' ); $ipAllowedProperty->setAccessible( true ); $this->assertTrue( $ipAllowedProperty->getValue( $widget ), 'ipAllowed was not set correctly.' ); // Assert that ipRangeAllowed is set correctly $ipRangeAllowedProperty = $reflectionClass->getProperty( 'ipRangeAllowed' ); $ipRangeAllowedProperty->setAccessible( true ); $this->assertTrue( $ipRangeAllowedProperty->getValue( $widget ), 'ipRangeAllowed was not set correctly.' ); // Assert that ipRangeLimits is set correctly $ipRangeLimitsProperty = $reflectionClass->getProperty( 'ipRangeLimits' ); $ipRangeLimitsProperty->setAccessible( true ); $this->assertSame( $config['ipRangeLimits'], $ipRangeLimitsProperty->getValue( $widget ), 'ipRangeLimits was not set correctly.' ); } public function testGetJavaScriptClassName() { // Define the configuration to pass to the constructor $config = [ 'ipAllowed' => true, 'ipRangeAllowed' => true, 'ipRangeLimits' => [ 'min' => 0, 'max' => 32 ], ]; // Create the UsersMultiselectWidget object with the above configuration $widget = new UsersMultiselectWidget( $config ); // Use reflection to access the protected properties for testing $reflectionClass = new ReflectionClass( UsersMultiselectWidget::class ); // Assert that ipAllowed is set correctly $getJavaScriptClassNameMethod = $reflectionClass->getMethod( 'getJavaScriptClassName' ); $getJavaScriptClassNameMethod->setAccessible( true ); $this->assertSame( 'mw.widgets.UsersMultiselectWidget', $getJavaScriptClassNameMethod->invoke( $widget ), 'getJavaScriptClassName did not return the expected value.' ); } public function provideGetConfig(): array { return [ 'ipAllowed' => [ [ 'ipAllowed' => true, ], [ 'ipAllowed' => true, 'ipRangeAllowed' => false, 'ipRangeLimits' => [ 'min' => 0, 'max' => 32, ], ], ], 'ipRangeAllowed' => [ [ 'ipRangeAllowed' => true, ], [ 'ipAllowed' => false, 'ipRangeAllowed' => true, 'ipRangeLimits' => [ 'min' => 0, 'max' => 32, ], ], ], 'ipRangeLimits' => [ [ 'ipRangeLimits' => [ 'min' => 0, 'max' => 64 ], ], [ 'ipAllowed' => false, 'ipRangeAllowed' => false, 'ipRangeLimits' => [ 'min' => 0, 'max' => 64, ], ], ], ]; } /** * @dataProvider provideGetConfig */ public function testGetConfig( array $inputConfig, array $expected ) { // Create the UsersMultiselectWidget object with the input configuration $widget = new UsersMultiselectWidget( $inputConfig ); $actualConfig = [ 'ipAllowed' => false, 'ipRangeAllowed' => false, 'ipRangeLimits' => [ 'min' => 0, 'max' => 32, ], ]; // Use reflection to make the getConfig method accessible $reflectionClass = new ReflectionClass( UsersMultiselectWidget::class ); $getConfigMethod = $reflectionClass->getMethod( 'getConfig' ); $getConfigMethod->setAccessible( true ); // Invoke getConfig, passing the actualConfig by reference $getConfigMethod->invokeArgs( $widget, [ &$actualConfig ] ); // Assert that the actualConfig matches the expected configuration foreach ( $expected as $key => $value ) { $this->assertArrayHasKey( $key, $actualConfig, "Config missing expected key '$key'" ); $this->assertEquals( $value, $actualConfig[$key], "Mismatch for key '$key'" ); } } }