diff options
Diffstat (limited to 'www/wiki/extensions/ConfirmEdit/tests/phpunit/SimpleCaptcha/CaptchaTest.php')
-rw-r--r-- | www/wiki/extensions/ConfirmEdit/tests/phpunit/SimpleCaptcha/CaptchaTest.php | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/www/wiki/extensions/ConfirmEdit/tests/phpunit/SimpleCaptcha/CaptchaTest.php b/www/wiki/extensions/ConfirmEdit/tests/phpunit/SimpleCaptcha/CaptchaTest.php new file mode 100644 index 00000000..6837bfc4 --- /dev/null +++ b/www/wiki/extensions/ConfirmEdit/tests/phpunit/SimpleCaptcha/CaptchaTest.php @@ -0,0 +1,116 @@ +<?php + +/** + * @covers SimpleCaptcha + */ +class CaptchaTest extends MediaWikiTestCase { + /** + * @dataProvider provideSimpleTriggersCaptcha + */ + public function testTriggersCaptcha( $action, $expectedResult ) { + $captcha = new SimpleCaptcha(); + $this->setMwGlobals( [ + 'wgCaptchaTriggers' => [ + $action => $expectedResult, + ] + ] ); + $this->assertEquals( $expectedResult, $captcha->triggersCaptcha( $action ) ); + } + + public function provideSimpleTriggersCaptcha() { + $data = []; + $captchaTriggers = new ReflectionClass( CaptchaTriggers::class ); + $constants = $captchaTriggers->getConstants(); + foreach ( $constants as $const ) { + $data[] = [ $const, true ]; + $data[] = [ $const, false ]; + } + return $data; + } + + /** + * @dataProvider provideNamespaceOverwrites + */ + public function testNamespaceTriggersOverwrite( $trigger, $expected ) { + $captcha = new SimpleCaptcha(); + $this->setMwGlobals( [ + 'wgCaptchaTriggers' => [ + $trigger => !$expected, + ], + 'wgCaptchaTriggersOnNamespace' => [ + 0 => [ + $trigger => $expected, + ], + ], + ] ); + $title = Title::newFromText( 'Main' ); + $this->assertEquals( $expected, $captcha->triggersCaptcha( $trigger, $title ) ); + } + + public function provideNamespaceOverwrites() { + return [ + [ 'edit', true ], + [ 'edit', false ], + ]; + } + + private function setCaptchaTriggersAttribute( $trigger, $value ) { + $info = [ + 'globals' => [], + 'callbacks' => [], + 'defines' => [], + 'credits' => [], + 'attributes' => [ + 'CaptchaTriggers' => [ + $trigger => $value + ] + ], + 'autoloaderPaths' => [] + ]; + $registry = new ExtensionRegistry(); + $class = new ReflectionClass( 'ExtensionRegistry' ); + $instanceProperty = $class->getProperty( 'instance' ); + $instanceProperty->setAccessible( true ); + $instanceProperty->setValue( $registry ); + $method = $class->getMethod( 'exportExtractedData' ); + $method->setAccessible( true ); + $method->invokeArgs( $registry, [ $info ] ); + } + + /** + * @dataProvider provideAttributeSet + */ + public function testCaptchaTriggersAttributeSetTrue( $trigger, $value ) { + $this->setCaptchaTriggersAttribute( $trigger, $value ); + $captcha = new SimpleCaptcha(); + $this->assertEquals( $value, $captcha->triggersCaptcha( $trigger ) ); + } + + public function provideAttributeSet() { + return [ + [ 'test', true ], + [ 'test', false ], + ]; + } + + /** + * @dataProvider provideAttributeOverwritten + */ + public function testCaptchaTriggersAttributeGetsOverwritten( $trigger, $expected ) { + $this->setMwGlobals( [ + 'wgCaptchaTriggers' => [ + $trigger => $expected + ] + ] ); + $this->setCaptchaTriggersAttribute( $trigger, !$expected ); + $captcha = new SimpleCaptcha(); + $this->assertEquals( $expected, $captcha->triggersCaptcha( $trigger ) ); + } + + public function provideAttributeOverwritten() { + return [ + [ 'edit', true ], + [ 'edit', false ], + ]; + } +} |