MediaWiki REL1_31
CaptchaTest.php
Go to the documentation of this file.
1<?php
2
10 public function testTriggersCaptcha( $action, $expectedResult ) {
11 $captcha = new SimpleCaptcha();
12 $this->setMwGlobals( [
13 'wgCaptchaTriggers' => [
14 $action => $expectedResult,
15 ]
16 ] );
17 $this->assertEquals( $expectedResult, $captcha->triggersCaptcha( $action ) );
18 }
19
20 public function provideSimpleTriggersCaptcha() {
21 $data = [];
22 $captchaTriggers = new ReflectionClass( CaptchaTriggers::class );
23 $constants = $captchaTriggers->getConstants();
24 foreach ( $constants as $const ) {
25 $data[] = [ $const, true ];
26 $data[] = [ $const, false ];
27 }
28 return $data;
29 }
30
34 public function testNamespaceTriggersOverwrite( $trigger, $expected ) {
35 $captcha = new SimpleCaptcha();
36 $this->setMwGlobals( [
37 'wgCaptchaTriggers' => [
38 $trigger => !$expected,
39 ],
40 'wgCaptchaTriggersOnNamespace' => [
41 0 => [
42 $trigger => $expected,
43 ],
44 ],
45 ] );
46 $title = Title::newFromText( 'Main' );
47 $this->assertEquals( $expected, $captcha->triggersCaptcha( $trigger, $title ) );
48 }
49
50 public function provideNamespaceOverwrites() {
51 return [
52 [ 'edit', true ],
53 [ 'edit', false ],
54 ];
55 }
56
57 private function setCaptchaTriggersAttribute( $trigger, $value ) {
58 $info = [
59 'globals' => [],
60 'callbacks' => [],
61 'defines' => [],
62 'credits' => [],
63 'attributes' => [
64 'CaptchaTriggers' => [
65 $trigger => $value
66 ]
67 ],
68 'autoloaderPaths' => []
69 ];
70 $registry = new ExtensionRegistry();
71 $class = new ReflectionClass( 'ExtensionRegistry' );
72 $instanceProperty = $class->getProperty( 'instance' );
73 $instanceProperty->setAccessible( true );
74 $instanceProperty->setValue( $registry );
75 $method = $class->getMethod( 'exportExtractedData' );
76 $method->setAccessible( true );
77 $method->invokeArgs( $registry, [ $info ] );
78 }
79
83 public function testCaptchaTriggersAttributeSetTrue( $trigger, $value ) {
84 $this->setCaptchaTriggersAttribute( $trigger, $value );
85 $captcha = new SimpleCaptcha();
86 $this->assertEquals( $value, $captcha->triggersCaptcha( $trigger ) );
87 }
88
89 public function provideAttributeSet() {
90 return [
91 [ 'test', true ],
92 [ 'test', false ],
93 ];
94 }
95
99 public function testCaptchaTriggersAttributeGetsOverwritten( $trigger, $expected ) {
100 $this->setMwGlobals( [
101 'wgCaptchaTriggers' => [
102 $trigger => $expected
103 ]
104 ] );
105 $this->setCaptchaTriggersAttribute( $trigger, !$expected );
106 $captcha = new SimpleCaptcha();
107 $this->assertEquals( $expected, $captcha->triggersCaptcha( $trigger ) );
108 }
109
110 public function provideAttributeOverwritten() {
111 return [
112 [ 'edit', true ],
113 [ 'edit', false ],
114 ];
115 }
116}
SimpleCaptcha.
testNamespaceTriggersOverwrite( $trigger, $expected)
provideNamespaceOverwrites
provideNamespaceOverwrites()
testCaptchaTriggersAttributeGetsOverwritten( $trigger, $expected)
provideAttributeOverwritten
setCaptchaTriggersAttribute( $trigger, $value)
testTriggersCaptcha( $action, $expectedResult)
provideSimpleTriggersCaptcha
provideAttributeOverwritten()
provideSimpleTriggersCaptcha()
testCaptchaTriggersAttributeSetTrue( $trigger, $value)
provideAttributeSet
ExtensionRegistry class.
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
Demo CAPTCHA (not for production usage) and base class for real CAPTCHAs.
Definition Captcha.php:8
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition hooks.txt:2006
processing should stop and the error should be shown to the user * false
Definition hooks.txt:187