MediaWiki  1.23.13
HooksTest.php
Go to the documentation of this file.
1 <?php
2 
3 class HooksTest extends MediaWikiTestCase {
4 
5  function setUp() {
7  parent::setUp();
8  Hooks::clear( 'MediaWikiHooksTest001' );
9  unset( $wgHooks['MediaWikiHooksTest001'] );
10  }
11 
12  public static function provideHooks() {
13  $i = new NothingClass();
14 
15  return array(
16  array( 'Object and method', array( $i, 'someNonStatic' ), 'changed-nonstatic', 'changed-nonstatic' ),
17  array( 'Object and no method', array( $i ), 'changed-onevent', 'original' ),
18  array( 'Object and method with data', array( $i, 'someNonStaticWithData', 'data' ), 'data', 'original' ),
19  array( 'Object and static method', array( $i, 'someStatic' ), 'changed-static', 'original' ),
20  array( 'Class::method static call', array( 'NothingClass::someStatic' ), 'changed-static', 'original' ),
21  array( 'Global function', array( 'NothingFunction' ), 'changed-func', 'original' ),
22  array( 'Global function with data', array( 'NothingFunctionData', 'data' ), 'data', 'original' ),
23  array( 'Closure', array( function ( &$foo, $bar ) {
24  $foo = 'changed-closure';
25 
26  return true;
27  } ), 'changed-closure', 'original' ),
28  array( 'Closure with data', array( function ( $data, &$foo, $bar ) {
29  $foo = $data;
30 
31  return true;
32  }, 'data' ), 'data', 'original' )
33  );
34  }
35 
40  public function testOldStyleHooks( $msg, array $hook, $expectedFoo, $expectedBar ) {
42  $foo = $bar = 'original';
43 
44  $wgHooks['MediaWikiHooksTest001'][] = $hook;
45  wfRunHooks( 'MediaWikiHooksTest001', array( &$foo, &$bar ) );
46 
47  $this->assertSame( $expectedFoo, $foo, $msg );
48  $this->assertSame( $expectedBar, $bar, $msg );
49  }
50 
56  public function testNewStyleHooks( $msg, $hook, $expectedFoo, $expectedBar ) {
57  $foo = $bar = 'original';
58 
59  Hooks::register( 'MediaWikiHooksTest001', $hook );
60  Hooks::run( 'MediaWikiHooksTest001', array( &$foo, &$bar ) );
61 
62  $this->assertSame( $expectedFoo, $foo, $msg );
63  $this->assertSame( $expectedBar, $bar, $msg );
64  }
65 
72  public function testNewStyleHookInteraction() {
74 
75  $a = new NothingClass();
76  $b = new NothingClass();
77 
78  $wgHooks['MediaWikiHooksTest001'][] = $a;
79  $this->assertTrue( Hooks::isRegistered( 'MediaWikiHooksTest001' ), 'Hook registered via $wgHooks should be noticed by Hooks::isRegistered' );
80 
81  Hooks::register( 'MediaWikiHooksTest001', $b );
82  $this->assertEquals( 2, count( Hooks::getHandlers( 'MediaWikiHooksTest001' ) ), 'Hooks::getHandlers() should return hooks registered via wgHooks as well as Hooks::register' );
83 
84  $foo = 'quux';
85  $bar = 'qaax';
86 
87  Hooks::run( 'MediaWikiHooksTest001', array( &$foo, &$bar ) );
88  $this->assertEquals( 1, $a->calls, 'Hooks::run() should run hooks registered via wgHooks as well as Hooks::register' );
89  $this->assertEquals( 1, $b->calls, 'Hooks::run() should run hooks registered via wgHooks as well as Hooks::register' );
90  }
91 
96  public function testUncallableFunction() {
97  Hooks::register( 'MediaWikiHooksTest001', 'ThisFunctionDoesntExist' );
98  Hooks::run( 'MediaWikiHooksTest001', array() );
99  }
100 
104  public function testFalseReturn() {
105  Hooks::register( 'MediaWikiHooksTest001', function ( &$foo ) {
106  return false;
107  } );
108  Hooks::register( 'MediaWikiHooksTest001', function ( &$foo ) {
109  $foo = 'test';
110 
111  return true;
112  } );
113  $foo = 'original';
114  Hooks::run( 'MediaWikiHooksTest001', array( &$foo ) );
115  $this->assertSame( 'original', $foo, 'Hooks continued processing after a false return.' );
116  }
117 
122  public function testFatalError() {
123  Hooks::register( 'MediaWikiHooksTest001', function () {
124  return 'test';
125  } );
126  Hooks::run( 'MediaWikiHooksTest001', array() );
127  }
128 }
129 
130 function NothingFunction( &$foo, &$bar ) {
131  $foo = 'changed-func';
132 
133  return true;
134 }
135 
136 function NothingFunctionData( $data, &$foo, &$bar ) {
137  $foo = $data;
138 
139  return true;
140 }
141 
143  public $calls = 0;
144 
145  public static function someStatic( &$foo, &$bar ) {
146  $foo = 'changed-static';
147 
148  return true;
149  }
150 
151  public function someNonStatic( &$foo, &$bar ) {
152  $this->calls++;
153  $foo = 'changed-nonstatic';
154  $bar = 'changed-nonstatic';
155 
156  return true;
157  }
158 
159  public function onMediaWikiHooksTest001( &$foo, &$bar ) {
160  $this->calls++;
161  $foo = 'changed-onevent';
162 
163  return true;
164  }
165 
166  public function someNonStaticWithData( $data, &$foo, &$bar ) {
167  $this->calls++;
168  $foo = $data;
169 
170  return true;
171  }
172 }
HooksTest\provideHooks
static provideHooks()
Definition: HooksTest.php:12
HooksTest\testFalseReturn
testFalseReturn()
@covers Hooks::run
Definition: HooksTest.php:104
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
Hooks\getHandlers
static getHandlers( $name)
Returns an array of all the event functions attached to a hook This combines functions registered via...
Definition: Hooks.php:103
NothingClass\someNonStatic
someNonStatic(&$foo, &$bar)
Definition: HooksTest.php:151
HooksTest\testFatalError
testFatalError()
@expectedException FatalError @covers Hooks::run
Definition: HooksTest.php:122
NothingFunctionData
NothingFunctionData( $data, &$foo, &$bar)
Definition: HooksTest.php:136
HooksTest\setUp
setUp()
Definition: HooksTest.php:5
Hooks\clear
static clear( $name)
Clears hooks registered via Hooks::register().
Definition: Hooks.php:72
$wgHooks
$wgHooks['ArticleShow'][]
Definition: hooks.txt:110
HooksTest\testUncallableFunction
testUncallableFunction()
@expectedException MWException @covers Hooks::run
Definition: HooksTest.php:96
NothingFunction
NothingFunction(&$foo, &$bar)
Definition: HooksTest.php:130
NothingClass\someStatic
static someStatic(&$foo, &$bar)
Definition: HooksTest.php:145
MediaWikiTestCase
Definition: MediaWikiTestCase.php:6
NothingClass\$calls
$calls
Definition: HooksTest.php:143
NothingClass\onMediaWikiHooksTest001
onMediaWikiHooksTest001(&$foo, &$bar)
Definition: HooksTest.php:159
wfRunHooks
wfRunHooks( $event, array $args=array(), $deprecatedVersion=null)
Call hook functions defined in $wgHooks.
Definition: GlobalFunctions.php:4058
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
HooksTest\testOldStyleHooks
testOldStyleHooks( $msg, array $hook, $expectedFoo, $expectedBar)
@dataProvider provideHooks @covers wfRunHooks
Definition: HooksTest.php:40
NothingClass
Definition: HooksTest.php:142
Hooks\register
static register( $name, $callback)
Attach an event handler to a given hook.
Definition: Hooks.php:55
HooksTest
Definition: HooksTest.php:3
Hooks\run
static run( $event, array $args=array(), $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:136
Hooks\isRegistered
static isRegistered( $name)
Returns true if a hook has a function registered to it.
Definition: Hooks.php:89
HooksTest\testNewStyleHooks
testNewStyleHooks( $msg, $hook, $expectedFoo, $expectedBar)
@dataProvider provideHooks @covers Hooks::register @covers Hooks::run
Definition: HooksTest.php:56
NothingClass\someNonStaticWithData
someNonStaticWithData( $data, &$foo, &$bar)
Definition: HooksTest.php:166
HooksTest\testNewStyleHookInteraction
testNewStyleHookInteraction()
@covers Hooks::isRegistered @covers Hooks::register @covers Hooks::getHandlers @covers Hooks::run
Definition: HooksTest.php:72