|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package framework |
|
4
|
|
|
* @subpackage tests |
|
5
|
|
|
*/ |
|
6
|
|
|
class ShortcodeParserTest extends SapphireTest { |
|
7
|
|
|
|
|
8
|
|
|
protected $arguments, $contents, $tagName, $parser; |
|
|
|
|
|
|
9
|
|
|
protected $extra = array(); |
|
10
|
|
|
|
|
11
|
|
|
public function setUp() { |
|
12
|
|
|
ShortcodeParser::get('test')->register('test_shortcode', array($this, 'shortcodeSaver')); |
|
13
|
|
|
$this->parser = ShortcodeParser::get('test'); |
|
14
|
|
|
|
|
15
|
|
|
parent::setUp(); |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
public function tearDown() { |
|
19
|
|
|
ShortcodeParser::get('test')->unregister('test_shortcode'); |
|
20
|
|
|
|
|
21
|
|
|
parent::tearDown(); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Tests that valid short codes that have not been registered are not replaced. |
|
26
|
|
|
*/ |
|
27
|
|
|
public function testNotRegisteredShortcode() { |
|
28
|
|
|
ShortcodeParser::$error_behavior = ShortcodeParser::STRIP; |
|
29
|
|
|
|
|
30
|
|
|
$this->assertEquals( |
|
|
|
|
|
|
31
|
|
|
'', |
|
32
|
|
|
$this->parser->parse('[not_shortcode]') |
|
33
|
|
|
); |
|
34
|
|
|
|
|
35
|
|
|
$this->assertEquals( |
|
|
|
|
|
|
36
|
|
|
'<img class="">', |
|
37
|
|
|
$this->parser->parse('<img class="[not_shortcode]">') |
|
38
|
|
|
); |
|
39
|
|
|
|
|
40
|
|
|
ShortcodeParser::$error_behavior = ShortcodeParser::WARN; |
|
41
|
|
|
|
|
42
|
|
|
$this->assertEquals( |
|
|
|
|
|
|
43
|
|
|
'<strong class="warning">[not_shortcode]</strong>', |
|
44
|
|
|
$this->parser->parse('[not_shortcode]') |
|
45
|
|
|
); |
|
46
|
|
|
|
|
47
|
|
|
ShortcodeParser::$error_behavior = ShortcodeParser::LEAVE; |
|
48
|
|
|
|
|
49
|
|
|
$this->assertEquals('[not_shortcode]', |
|
|
|
|
|
|
50
|
|
|
$this->parser->parse('[not_shortcode]')); |
|
51
|
|
|
$this->assertEquals('[not_shortcode /]', |
|
|
|
|
|
|
52
|
|
|
$this->parser->parse('[not_shortcode /]')); |
|
53
|
|
|
$this->assertEquals('[not_shortcode,foo="bar"]', |
|
|
|
|
|
|
54
|
|
|
$this->parser->parse('[not_shortcode,foo="bar"]')); |
|
55
|
|
|
$this->assertEquals('[not_shortcode]a[/not_shortcode]', |
|
|
|
|
|
|
56
|
|
|
$this->parser->parse('[not_shortcode]a[/not_shortcode]')); |
|
57
|
|
|
$this->assertEquals('[/not_shortcode]', |
|
|
|
|
|
|
58
|
|
|
$this->parser->parse('[/not_shortcode]')); |
|
59
|
|
|
|
|
60
|
|
|
$this->assertEquals( |
|
|
|
|
|
|
61
|
|
|
'<img class="[not_shortcode]">', |
|
62
|
|
|
$this->parser->parse('<img class="[not_shortcode]">') |
|
63
|
|
|
); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function testSimpleTag() { |
|
67
|
|
|
$tests = array( |
|
68
|
|
|
'[test_shortcode]', |
|
69
|
|
|
'[test_shortcode ]', '[test_shortcode,]', '[test_shortcode, ]'. |
|
70
|
|
|
'[test_shortcode/]', '[test_shortcode /]', '[test_shortcode,/]', '[test_shortcode, /]' |
|
71
|
|
|
); |
|
72
|
|
|
|
|
73
|
|
|
foreach($tests as $test) { |
|
74
|
|
|
$this->parser->parse($test); |
|
75
|
|
|
|
|
76
|
|
|
$this->assertEquals(array(), $this->arguments, $test); |
|
|
|
|
|
|
77
|
|
|
$this->assertEquals('', $this->contents, $test); |
|
|
|
|
|
|
78
|
|
|
$this->assertEquals('test_shortcode', $this->tagName, $test); |
|
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function testOneArgument() { |
|
83
|
|
|
$tests = array ( |
|
84
|
|
|
'[test_shortcode foo="bar"]', '[test_shortcode,foo="bar"]', |
|
85
|
|
|
"[test_shortcode foo='bar']", "[test_shortcode,foo='bar']", |
|
86
|
|
|
'[test_shortcode foo = "bar" /]', '[test_shortcode, foo = "bar" /]' |
|
87
|
|
|
); |
|
88
|
|
|
|
|
89
|
|
|
foreach($tests as $test) { |
|
90
|
|
|
$this->parser->parse($test); |
|
91
|
|
|
|
|
92
|
|
|
$this->assertEquals(array('foo' => 'bar'), $this->arguments, $test); |
|
|
|
|
|
|
93
|
|
|
$this->assertEquals('', $this->contents, $test); |
|
|
|
|
|
|
94
|
|
|
$this->assertEquals('test_shortcode', $this->tagName, $test); |
|
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function testMultipleArguments() { |
|
99
|
|
|
$this->parser->parse('[test_shortcode foo = "bar",bar=\'foo\', baz="buz"]'); |
|
100
|
|
|
|
|
101
|
|
|
$this->assertEquals(array('foo' => 'bar', 'bar' => 'foo', 'baz' => 'buz'), $this->arguments); |
|
|
|
|
|
|
102
|
|
|
$this->assertEquals('', $this->contents); |
|
|
|
|
|
|
103
|
|
|
$this->assertEquals('test_shortcode', $this->tagName); |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function testEnclosing() { |
|
107
|
|
|
$this->parser->parse('[test_shortcode]foo[/test_shortcode]'); |
|
108
|
|
|
|
|
109
|
|
|
$this->assertEquals(array(), $this->arguments); |
|
|
|
|
|
|
110
|
|
|
$this->assertEquals('foo', $this->contents); |
|
|
|
|
|
|
111
|
|
|
$this->assertEquals('test_shortcode', $this->tagName); |
|
|
|
|
|
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function testEnclosingWithArguments() { |
|
115
|
|
|
$this->parser->parse('[test_shortcode,foo = "bar",bar=\'foo\',baz="buz"]foo[/test_shortcode]'); |
|
116
|
|
|
|
|
117
|
|
|
$this->assertEquals(array('foo' => 'bar', 'bar' => 'foo', 'baz' => 'buz'), $this->arguments); |
|
|
|
|
|
|
118
|
|
|
$this->assertEquals('foo', $this->contents); |
|
|
|
|
|
|
119
|
|
|
$this->assertEquals('test_shortcode', $this->tagName); |
|
|
|
|
|
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function testShortcodeEscaping() { |
|
123
|
|
|
$this->assertEquals( |
|
|
|
|
|
|
124
|
|
|
'[test_shortcode]', |
|
125
|
|
|
$this->parser->parse('[[test_shortcode]]') |
|
126
|
|
|
); |
|
127
|
|
|
|
|
128
|
|
|
$this->assertEquals( |
|
|
|
|
|
|
129
|
|
|
'[test_shortcode /]', |
|
130
|
|
|
$this->parser->parse('[[test_shortcode /]]') |
|
131
|
|
|
); |
|
132
|
|
|
|
|
133
|
|
|
$this->assertEquals( |
|
|
|
|
|
|
134
|
|
|
'[test_shortcode]content[/test_shortcode]', |
|
135
|
|
|
$this->parser->parse('[[test_shortcode]content[/test_shortcode]]' |
|
136
|
|
|
)); |
|
137
|
|
|
|
|
138
|
|
|
$this->assertEquals( |
|
|
|
|
|
|
139
|
|
|
'[test_shortcode]content', |
|
140
|
|
|
$this->parser->parse('[[test_shortcode]][test_shortcode]content[/test_shortcode]') |
|
141
|
|
|
); |
|
142
|
|
|
|
|
143
|
|
|
$this->assertEquals( |
|
|
|
|
|
|
144
|
|
|
'[test_shortcode]content[/test_shortcode]content2', |
|
145
|
|
|
$this->parser->parse('[[test_shortcode]content[/test_shortcode]][test_shortcode]content2[/test_shortcode]' |
|
146
|
|
|
)); |
|
147
|
|
|
|
|
148
|
|
|
$this->assertEquals( |
|
|
|
|
|
|
149
|
|
|
'[[Doesnt strip double [ character if not a shortcode', |
|
150
|
|
|
$this->parser->parse('[[Doesnt strip double [ character if not a [test_shortcode]shortcode[/test_shortcode]' |
|
151
|
|
|
)); |
|
152
|
|
|
|
|
153
|
|
|
$this->assertEquals( |
|
|
|
|
|
|
154
|
|
|
'[[Doesnt shortcode get confused by double ]] characters', |
|
155
|
|
|
$this->parser->parse( |
|
156
|
|
|
'[[Doesnt [test_shortcode]shortcode[/test_shortcode] get confused by double ]] characters') |
|
157
|
|
|
); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
public function testUnquotedArguments() { |
|
161
|
|
|
$this->assertEquals('', $this->parser->parse('[test_shortcode,foo=bar!,baz = buz123]')); |
|
|
|
|
|
|
162
|
|
|
$this->assertEquals(array('foo' => 'bar!', 'baz' => 'buz123'), $this->arguments); |
|
|
|
|
|
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
public function testSpacesForDelimiter() { |
|
166
|
|
|
$this->assertEquals('', $this->parser->parse('[test_shortcode foo=bar! baz = buz123]')); |
|
|
|
|
|
|
167
|
|
|
$this->assertEquals(array('foo' => 'bar!', 'baz' => 'buz123'), $this->arguments); |
|
|
|
|
|
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
public function testSelfClosingTag() { |
|
171
|
|
|
$this->assertEquals ( |
|
|
|
|
|
|
172
|
|
|
'morecontent', |
|
173
|
|
|
$this->parser->parse('[test_shortcode,id="1"/]more[test_shortcode,id="2"]content[/test_shortcode]'), |
|
174
|
|
|
'Assert that self-closing tags are respected during parsing.' |
|
175
|
|
|
); |
|
176
|
|
|
|
|
177
|
|
|
$this->assertEquals(2, $this->arguments['id']); |
|
|
|
|
|
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
public function testConsecutiveTags() { |
|
181
|
|
|
$this->assertEquals('', $this->parser->parse('[test_shortcode][test_shortcode]')); |
|
|
|
|
|
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
protected function assertEqualsIgnoringWhitespace($a, $b, $message = null) { |
|
185
|
|
|
$this->assertEquals(preg_replace('/\s+/', '', $a), preg_replace('/\s+/', '', $b), $message); |
|
|
|
|
|
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
public function testtExtractBefore() |
|
189
|
|
|
{ |
|
190
|
|
|
// Left extracts to before the current block |
|
191
|
|
|
$this->assertEqualsIgnoringWhitespace( |
|
192
|
|
|
'Code<div>FooBar</div>', |
|
193
|
|
|
$this->parser->parse('<div>Foo[test_shortcode class=left]Code[/test_shortcode]Bar</div>') |
|
194
|
|
|
); |
|
195
|
|
|
|
|
196
|
|
|
// Even if the immediate parent isn't a the current block |
|
197
|
|
|
$this->assertEqualsIgnoringWhitespace( |
|
198
|
|
|
'Code<div>Foo<b>BarBaz</b>Qux</div>', |
|
199
|
|
|
$this->parser->parse('<div>Foo<b>Bar[test_shortcode class=left]Code[/test_shortcode]Baz</b>Qux</div>') |
|
200
|
|
|
); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
public function testExtractSplit() |
|
204
|
|
|
{ |
|
205
|
|
|
$this->markTestSkipped( |
|
206
|
|
|
'Feature disabled due to https://github.com/silverstripe/silverstripe-framework/issues/5987' |
|
207
|
|
|
); |
|
208
|
|
|
|
|
209
|
|
|
// Center splits the current block |
|
210
|
|
|
$this->assertEqualsIgnoringWhitespace( |
|
211
|
|
|
'<div>Foo</div>Code<div>Bar</div>', |
|
212
|
|
|
$this->parser->parse('<div>Foo[test_shortcode class=center]Code[/test_shortcode]Bar</div>') |
|
213
|
|
|
); |
|
214
|
|
|
|
|
215
|
|
|
// Even if the immediate parent isn't a the current block |
|
216
|
|
|
$this->assertEqualsIgnoringWhitespace( |
|
217
|
|
|
'<div>Foo<b>Bar</b></div>Code<div><b>Baz</b>Qux</div>', |
|
218
|
|
|
$this->parser->parse('<div>Foo<b>Bar[test_shortcode class=center]Code[/test_shortcode]Baz</b>Qux</div>') |
|
219
|
|
|
); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
public function testExtractNone() { |
|
223
|
|
|
// No class means don't extract |
|
224
|
|
|
$this->assertEqualsIgnoringWhitespace( |
|
225
|
|
|
'<div>FooCodeBar</div>', |
|
226
|
|
|
$this->parser->parse('<div>Foo[test_shortcode]Code[/test_shortcode]Bar</div>') |
|
227
|
|
|
); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
public function testShortcodesInsideScriptTag() { |
|
231
|
|
|
$this->assertEqualsIgnoringWhitespace( |
|
232
|
|
|
'<script>hello</script>', |
|
233
|
|
|
$this->parser->parse('<script>[test_shortcode]hello[/test_shortcode]</script>') |
|
234
|
|
|
); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
public function testFalseyArguments() { |
|
238
|
|
|
$this->parser->parse('<p>[test_shortcode falsey=0]'); |
|
239
|
|
|
|
|
240
|
|
|
$this->assertEquals(array( |
|
|
|
|
|
|
241
|
|
|
'falsey' => '', |
|
242
|
|
|
), $this->arguments); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
public function testNumericShortcodes() { |
|
246
|
|
|
$this->assertEqualsIgnoringWhitespace( |
|
247
|
|
|
'[2]', |
|
248
|
|
|
$this->parser->parse('[2]') |
|
249
|
|
|
); |
|
250
|
|
|
$this->assertEqualsIgnoringWhitespace( |
|
251
|
|
|
'<script>[2]</script>', |
|
252
|
|
|
$this->parser->parse('<script>[2]</script>') |
|
253
|
|
|
); |
|
254
|
|
|
|
|
255
|
|
|
$this->parser->register('2', function($attributes, $content, $this, $tag, $extra) { |
|
|
|
|
|
|
256
|
|
|
return 'this is 2'; |
|
257
|
|
|
}); |
|
258
|
|
|
|
|
259
|
|
|
$this->assertEqualsIgnoringWhitespace( |
|
260
|
|
|
'this is 2', |
|
261
|
|
|
$this->parser->parse('[2]') |
|
262
|
|
|
); |
|
263
|
|
|
$this->assertEqualsIgnoringWhitespace( |
|
264
|
|
|
'<script>this is 2</script>', |
|
265
|
|
|
$this->parser->parse('<script>[2]</script>') |
|
266
|
|
|
); |
|
267
|
|
|
|
|
268
|
|
|
$this->parser->unregister('2'); |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
public function testExtraContext() { |
|
272
|
|
|
$this->parser->parse('<a href="[test_shortcode]">Test</a>'); |
|
273
|
|
|
|
|
274
|
|
|
$this->assertInstanceOf('DOMNode', $this->extra['node']); |
|
275
|
|
|
$this->assertInstanceOf('DOMElement', $this->extra['element']); |
|
276
|
|
|
$this->assertEquals($this->extra['element']->tagName, 'a'); |
|
|
|
|
|
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
public function testNoParseAttemptIfNoCode() { |
|
280
|
|
|
$stub = $this->getMock('ShortcodeParser', array('replaceElementTagsWithMarkers')); |
|
281
|
|
|
$stub->register('test', function() { |
|
282
|
|
|
return ''; |
|
283
|
|
|
}); |
|
284
|
|
|
|
|
285
|
|
|
$stub->expects($this->never()) |
|
286
|
|
|
->method('replaceElementTagsWithMarkers')->will($this->returnValue(array('', ''))); |
|
287
|
|
|
|
|
288
|
|
|
$stub->parse('<p>test</p>'); |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
// ----------------------------------------------------------------------------------------------------------------- |
|
292
|
|
|
|
|
293
|
|
|
/** |
|
294
|
|
|
* Stores the result of a shortcode parse in object properties for easy testing access. |
|
295
|
|
|
*/ |
|
296
|
|
|
public function shortcodeSaver($arguments, $content, $parser, $tagName, $extra) { |
|
297
|
|
|
$this->arguments = $arguments; |
|
298
|
|
|
$this->contents = $content; |
|
299
|
|
|
$this->tagName = $tagName; |
|
300
|
|
|
$this->extra = $extra; |
|
301
|
|
|
|
|
302
|
|
|
return $content; |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
} |
|
306
|
|
|
|
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.