1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ParserHooks\Tests; |
4
|
|
|
|
5
|
|
|
use ParamProcessor\ProcessedParam; |
6
|
|
|
use ParamProcessor\ProcessingResult; |
7
|
|
|
use Parser; |
8
|
|
|
use ParserHooks\FunctionRunner; |
9
|
|
|
use ParserHooks\HookDefinition; |
10
|
|
|
use ParserHooks\HookRegistrant; |
11
|
|
|
use ParserHooks\HookRunner; |
12
|
|
|
use ParserOptions; |
13
|
|
|
use Title; |
14
|
|
|
use User; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @group ParserHooks |
18
|
|
|
* @licence GNU GPL v2+ |
19
|
|
|
* @author Jeroen De Dauw < [email protected] > |
20
|
|
|
*/ |
21
|
|
|
class TagHookTest extends \PHPUnit_Framework_TestCase { |
22
|
|
|
|
23
|
|
|
const HOOK_NAME = 'systemtest_tagextension'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Parser |
27
|
|
|
*/ |
28
|
|
|
protected $parser; |
29
|
|
|
|
30
|
|
|
public function setUp() { |
31
|
|
|
$this->parser = $this->getSomeParser(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
protected function getSomeParser() { |
35
|
|
|
global $wgParserConf; |
36
|
|
|
$parser = new Parser( $wgParserConf ); |
37
|
|
|
return $parser; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testParserFunctionReceivesArguments() { |
41
|
|
|
$this->registerParserHook(); |
42
|
|
|
|
43
|
|
|
$name = self::HOOK_NAME; |
44
|
|
|
|
45
|
|
|
$result = $this->getParsedText( |
46
|
|
|
"||<$name 1337=yes>Jeroen</$name>|||" |
47
|
|
|
); |
48
|
|
|
|
49
|
|
|
$this->assertInternalType( 'string', $result ); |
50
|
|
|
$this->assertEquals( |
51
|
|
|
"||-Jeroen-|||", |
52
|
|
|
$result |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
protected function getParsedText( $text ) { |
|
|
|
|
57
|
|
|
return $this->parser->parse( |
58
|
|
|
$text, |
59
|
|
|
Title::newFromText( "Test" ), |
60
|
|
|
ParserOptions::newFromUserAndLang( new User(), $GLOBALS['wgContLang'] ), |
61
|
|
|
false |
62
|
|
|
)->getText(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
protected function registerParserHook() { |
66
|
|
|
$runner = $this->getHookRunner(); |
67
|
|
|
|
68
|
|
|
$registrant = new HookRegistrant( $this->parser ); |
69
|
|
|
$registrant->registerHook( $runner ); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
protected function getHookRunner() { |
73
|
|
|
return new HookRunner( |
74
|
|
|
$this->getHookDefinition(), |
75
|
|
|
$this->getHookHandler() |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
protected function getHookDefinition() { |
80
|
|
|
return new HookDefinition( |
81
|
|
|
self::HOOK_NAME, |
82
|
|
|
array( |
|
|
|
|
83
|
|
|
array( |
84
|
|
|
'name' => 'name', |
85
|
|
|
'message' => 'abc', |
86
|
|
|
), |
87
|
|
|
array( |
88
|
|
|
'name' => 'awesomeness', |
89
|
|
|
'message' => 'abc', |
90
|
|
|
'type' => 'integer', |
91
|
|
|
'default' => 9001, |
92
|
|
|
), |
93
|
|
|
array( |
94
|
|
|
'name' => '1337', |
95
|
|
|
'message' => 'abc', |
96
|
|
|
'type' => 'boolean', |
97
|
|
|
'default' => false, |
98
|
|
|
), |
99
|
|
|
), |
100
|
|
|
array( |
101
|
|
|
'name' |
102
|
|
|
) |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
protected function getHookHandler() { |
107
|
|
|
$hookHandler = $this->getMock( 'ParserHooks\HookHandler' ); |
108
|
|
|
|
109
|
|
|
$hookHandler->expects( $this->once() ) |
110
|
|
|
->method( 'handle' ) |
111
|
|
|
->with( |
112
|
|
|
$this->isInstanceOf( 'Parser' ), |
113
|
|
|
$this->callback( function( $var ) { |
114
|
|
|
if ( !( $var instanceof ProcessingResult ) ) { |
115
|
|
|
return false; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$params = $var->getParameters(); |
119
|
|
|
$expectedParams = array( |
120
|
|
|
'1337' => new ProcessedParam( '1337', true, false, '1337', 'yes' ), |
121
|
|
|
'awesomeness' => new ProcessedParam( 'awesomeness', 9001, true, null, null ), |
122
|
|
|
'name' => new ProcessedParam( 'name', 'Jeroen', false, 'name', 'Jeroen' ), |
123
|
|
|
); |
124
|
|
|
|
125
|
|
|
asort( $params ); |
126
|
|
|
asort( $expectedParams ); |
127
|
|
|
|
128
|
|
|
return $params == $expectedParams; |
129
|
|
|
} ) |
130
|
|
|
) |
131
|
|
|
->will( $this->returnCallback( function( Parser $parser, ProcessingResult $result ) { |
132
|
|
|
$params = $result->getParameters(); |
133
|
|
|
return '-' . $params['name']->getValue() . '-'; |
134
|
|
|
} ) ); |
135
|
|
|
|
136
|
|
|
return $hookHandler; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
|
140
|
|
|
|
141
|
|
|
} |
142
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: