Merge "(bug 47483) update file metadata in importImages"
[lhc/web/wiklou.git] / tests / phpunit / includes / HooksTest.php
1 <?php
2
3 class HooksTest extends MediaWikiTestCase {
4
5 function setUp() {
6 global $wgHooks;
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
36 /**
37 * @dataProvider provideHooks
38 */
39 public function testOldStyleHooks( $msg, array $hook, $expectedFoo, $expectedBar ) {
40 global $wgHooks;
41 $foo = $bar = 'original';
42
43 $wgHooks['MediaWikiHooksTest001'][] = $hook;
44 wfRunHooks( 'MediaWikiHooksTest001', array( &$foo, &$bar ) );
45
46 $this->assertSame( $expectedFoo, $foo, $msg );
47 $this->assertSame( $expectedBar, $bar, $msg );
48 }
49
50 /**
51 * @dataProvider provideHooks
52 */
53 public function testNewStyleHooks( $msg, $hook, $expectedFoo, $expectedBar ) {
54 $foo = $bar = 'original';
55
56 Hooks::register( 'MediaWikiHooksTest001', $hook );
57 Hooks::run( 'MediaWikiHooksTest001', array( &$foo, &$bar ) );
58
59 $this->assertSame( $expectedFoo, $foo, $msg );
60 $this->assertSame( $expectedBar, $bar, $msg );
61 }
62
63 public function testNewStyleHookInteraction() {
64 global $wgHooks;
65
66 $a = new NothingClass();
67 $b = new NothingClass();
68
69 $wgHooks['MediaWikiHooksTest001'][] = $a;
70 $this->assertTrue( Hooks::isRegistered( 'MediaWikiHooksTest001' ), 'Hook registered via $wgHooks should be noticed by Hooks::isRegistered' );
71
72 Hooks::register( 'MediaWikiHooksTest001', $b );
73 $this->assertEquals( 2, count( Hooks::getHandlers( 'MediaWikiHooksTest001' ) ), 'Hooks::getHandlers() should return hooks registered via wgHooks as well as Hooks::register' );
74
75 $foo = 'quux';
76 $bar = 'qaax';
77
78 Hooks::run( 'MediaWikiHooksTest001', array( &$foo, &$bar ) );
79 $this->assertEquals( 1, $a->calls, 'Hooks::run() should run hooks registered via wgHooks as well as Hooks::register' );
80 $this->assertEquals( 1, $b->calls, 'Hooks::run() should run hooks registered via wgHooks as well as Hooks::register' );
81 }
82
83 /**
84 * @expectedException MWException
85 */
86 public function testUncallableFunction() {
87 Hooks::register( 'MediaWikiHooksTest001', 'ThisFunctionDoesntExist' );
88 Hooks::run( 'MediaWikiHooksTest001', array() );
89 }
90
91 public function testFalseReturn() {
92 Hooks::register( 'MediaWikiHooksTest001', function ( &$foo ) {
93 return false;
94 } );
95 Hooks::register( 'MediaWikiHooksTest001', function ( &$foo ) {
96 $foo = 'test';
97
98 return true;
99 } );
100 $foo = 'original';
101 Hooks::run( 'MediaWikiHooksTest001', array( &$foo ) );
102 $this->assertSame( 'original', $foo, 'Hooks continued processing after a false return.' );
103 }
104
105 /**
106 * @expectedException FatalError
107 */
108 public function testFatalError() {
109 Hooks::register( 'MediaWikiHooksTest001', function () {
110 return 'test';
111 } );
112 Hooks::run( 'MediaWikiHooksTest001', array() );
113 }
114 }
115
116 function NothingFunction( &$foo, &$bar ) {
117 $foo = 'changed-func';
118
119 return true;
120 }
121
122 function NothingFunctionData( $data, &$foo, &$bar ) {
123 $foo = $data;
124
125 return true;
126 }
127
128 class NothingClass {
129 public $calls = 0;
130
131 public static function someStatic( &$foo, &$bar ) {
132 $foo = 'changed-static';
133
134 return true;
135 }
136
137 public function someNonStatic( &$foo, &$bar ) {
138 $this->calls++;
139 $foo = 'changed-nonstatic';
140 $bar = 'changed-nonstatic';
141
142 return true;
143 }
144
145 public function onMediaWikiHooksTest001( &$foo, &$bar ) {
146 $this->calls++;
147 $foo = 'changed-onevent';
148
149 return true;
150 }
151
152 public function someNonStaticWithData( $data, &$foo, &$bar ) {
153 $this->calls++;
154 $foo = $data;
155
156 return true;
157 }
158 }