|
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 PHPUnit\Framework\TestCase; |
|
14
|
|
|
use Title; |
|
15
|
|
|
use User; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @group ParserHooks |
|
19
|
|
|
* @licence GNU GPL v2+ |
|
20
|
|
|
* @author Jeroen De Dauw < [email protected] > |
|
21
|
|
|
*/ |
|
22
|
|
|
class TagHookTest extends TestCase { |
|
23
|
|
|
|
|
24
|
|
|
const HOOK_NAME = 'systemtest_tagextension'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var Parser |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $parser; |
|
30
|
|
|
|
|
31
|
|
|
public function setUp(): void { |
|
32
|
|
|
$this->parser = $this->getSomeParser(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
protected function getSomeParser() { |
|
36
|
|
|
if ( class_exists( \MediaWiki\MediaWikiServices::class ) ) { |
|
37
|
|
|
$services = \MediaWiki\MediaWikiServices::getInstance(); |
|
38
|
|
|
if ( is_callable( $services, 'getParserFactory' ) ) { |
|
39
|
|
|
return $services->getParserFactory()->create(); |
|
|
|
|
|
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
// Fallback for MW < 1.32 |
|
43
|
|
|
global $wgParserConf; |
|
44
|
|
|
return new Parser( $wgParserConf ); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function testParserFunctionReceivesArguments() { |
|
48
|
|
|
$this->registerParserHook(); |
|
49
|
|
|
|
|
50
|
|
|
$name = self::HOOK_NAME; |
|
51
|
|
|
|
|
52
|
|
|
$result = $this->getParsedText( |
|
53
|
|
|
"||<$name 1337=yes>Jeroen</$name>|||" |
|
54
|
|
|
); |
|
55
|
|
|
|
|
56
|
|
|
$this->assertInternalType( 'string', $result ); |
|
57
|
|
|
$this->assertContains( |
|
58
|
|
|
"||-Jeroen-|||", |
|
59
|
|
|
$result |
|
60
|
|
|
); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
protected function getParsedText( $text ) { |
|
64
|
|
|
return $this->parser->parse( |
|
65
|
|
|
$text, |
|
66
|
|
|
Title::newFromText( "Test" ), |
|
67
|
|
|
ParserOptions::newFromUserAndLang( new User(), $GLOBALS['wgContLang'] ), |
|
68
|
|
|
false |
|
69
|
|
|
)->getText(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
protected function registerParserHook() { |
|
73
|
|
|
$runner = $this->getHookRunner(); |
|
74
|
|
|
|
|
75
|
|
|
$registrant = new HookRegistrant( $this->parser ); |
|
76
|
|
|
$registrant->registerHook( $runner ); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
protected function getHookRunner() { |
|
80
|
|
|
return new HookRunner( |
|
81
|
|
|
$this->getHookDefinition(), |
|
82
|
|
|
$this->getHookHandler() |
|
83
|
|
|
); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
protected function getHookDefinition() { |
|
87
|
|
|
return new HookDefinition( |
|
88
|
|
|
self::HOOK_NAME, |
|
89
|
|
|
[ |
|
|
|
|
|
|
90
|
|
|
[ |
|
91
|
|
|
'name' => 'name', |
|
92
|
|
|
'message' => 'abc', |
|
93
|
|
|
], |
|
94
|
|
|
[ |
|
95
|
|
|
'name' => 'awesomeness', |
|
96
|
|
|
'message' => 'abc', |
|
97
|
|
|
'type' => 'integer', |
|
98
|
|
|
'default' => 9001, |
|
99
|
|
|
], |
|
100
|
|
|
[ |
|
101
|
|
|
'name' => '1337', |
|
102
|
|
|
'message' => 'abc', |
|
103
|
|
|
'type' => 'boolean', |
|
104
|
|
|
'default' => false, |
|
105
|
|
|
], |
|
106
|
|
|
], |
|
107
|
|
|
[ |
|
108
|
|
|
'name' |
|
109
|
|
|
] |
|
110
|
|
|
); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
protected function getHookHandler() { |
|
114
|
|
|
$hookHandler = $this->createMock( 'ParserHooks\HookHandler' ); |
|
115
|
|
|
|
|
116
|
|
|
$hookHandler->expects( $this->once() ) |
|
117
|
|
|
->method( 'handle' ) |
|
118
|
|
|
->with( |
|
119
|
|
|
$this->isInstanceOf( 'Parser' ), |
|
120
|
|
|
$this->callback( function( $var ) { |
|
121
|
|
|
if ( !( $var instanceof ProcessingResult ) ) { |
|
122
|
|
|
return false; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
$params = $var->getParameters(); |
|
126
|
|
|
$expectedParams = [ |
|
127
|
|
|
'1337' => new ProcessedParam( '1337', true, false, '1337', 'yes' ), |
|
128
|
|
|
'awesomeness' => new ProcessedParam( 'awesomeness', 9001, true, null, null ), |
|
129
|
|
|
'name' => new ProcessedParam( 'name', 'Jeroen', false, 'name', 'Jeroen' ), |
|
130
|
|
|
]; |
|
131
|
|
|
|
|
132
|
|
|
asort( $params ); |
|
133
|
|
|
asort( $expectedParams ); |
|
134
|
|
|
|
|
135
|
|
|
return $params == $expectedParams; |
|
136
|
|
|
} ) |
|
137
|
|
|
) |
|
138
|
|
|
->will( $this->returnCallback( function( Parser $parser, ProcessingResult $result ) { |
|
139
|
|
|
$params = $result->getParameters(); |
|
140
|
|
|
return '-' . $params['name']->getValue() . '-'; |
|
141
|
|
|
} ) ); |
|
142
|
|
|
|
|
143
|
|
|
return $hookHandler; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
|
|
147
|
|
|
|
|
148
|
|
|
} |
|
149
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.