Changed quoting function for oracleDB.
[mediawiki.git] / tests / phpunit / includes / HooksTest.php
blob81dd48704ae1de5a52147837c038e4d94fcbfc93
1 <?php
3 class HooksTest extends MediaWikiTestCase {
5 function setUp() {
6 global $wgHooks;
7 parent::setUp();
8 Hooks::clear( 'MediaWikiHooksTest001' );
9 unset( $wgHooks['MediaWikiHooksTest001'] );
12 public static function provideHooks() {
13 $i = new NothingClass();
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';
26 return true;
27 } ), 'changed-closure', 'original' ),
28 array( 'Closure with data', array( function ( $data, &$foo, $bar ) {
29 $foo = $data;
31 return true;
32 }, 'data' ), 'data', 'original' )
36 /**
37 * @dataProvider provideHooks
39 public function testOldStyleHooks( $msg, array $hook, $expectedFoo, $expectedBar ) {
40 global $wgHooks;
41 $foo = $bar = 'original';
43 $wgHooks['MediaWikiHooksTest001'][] = $hook;
44 wfRunHooks( 'MediaWikiHooksTest001', array( &$foo, &$bar ) );
46 $this->assertSame( $expectedFoo, $foo, $msg );
47 $this->assertSame( $expectedBar, $bar, $msg );
50 /**
51 * @dataProvider provideHooks
53 public function testNewStyleHooks( $msg, $hook, $expectedFoo, $expectedBar ) {
54 $foo = $bar = 'original';
56 Hooks::register( 'MediaWikiHooksTest001', $hook );
57 Hooks::run( 'MediaWikiHooksTest001', array( &$foo, &$bar ) );
59 $this->assertSame( $expectedFoo, $foo, $msg );
60 $this->assertSame( $expectedBar, $bar, $msg );
63 public function testNewStyleHookInteraction() {
64 global $wgHooks;
66 $a = new NothingClass();
67 $b = new NothingClass();
69 $wgHooks['MediaWikiHooksTest001'][] = $a;
70 $this->assertTrue( Hooks::isRegistered( 'MediaWikiHooksTest001' ), 'Hook registered via $wgHooks should be noticed by Hooks::isRegistered' );
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' );
75 $foo = 'quux';
76 $bar = 'qaax';
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' );
83 /**
84 * @expectedException MWException
86 public function testUncallableFunction() {
87 Hooks::register( 'MediaWikiHooksTest001', 'ThisFunctionDoesntExist' );
88 Hooks::run( 'MediaWikiHooksTest001', array() );
91 public function testFalseReturn() {
92 Hooks::register( 'MediaWikiHooksTest001', function ( &$foo ) {
93 return false;
94 } );
95 Hooks::register( 'MediaWikiHooksTest001', function ( &$foo ) {
96 $foo = 'test';
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.' );
106 * @expectedException FatalError
108 public function testFatalError() {
109 Hooks::register( 'MediaWikiHooksTest001', function () {
110 return 'test';
111 } );
112 Hooks::run( 'MediaWikiHooksTest001', array() );
116 function NothingFunction( &$foo, &$bar ) {
117 $foo = 'changed-func';
119 return true;
122 function NothingFunctionData( $data, &$foo, &$bar ) {
123 $foo = $data;
125 return true;
128 class NothingClass {
129 public $calls = 0;
131 public static function someStatic( &$foo, &$bar ) {
132 $foo = 'changed-static';
134 return true;
137 public function someNonStatic( &$foo, &$bar ) {
138 $this->calls++;
139 $foo = 'changed-nonstatic';
140 $bar = 'changed-nonstatic';
142 return true;
145 public function onMediaWikiHooksTest001( &$foo, &$bar ) {
146 $this->calls++;
147 $foo = 'changed-onevent';
149 return true;
152 public function someNonStaticWithData( $data, &$foo, &$bar ) {
153 $this->calls++;
154 $foo = $data;
156 return true;