1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\ORM\Tests; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Control\Director; |
6
|
|
|
use SilverStripe\Core\Injector\Injector; |
7
|
|
|
use SilverStripe\ORM\FieldType\DBHTMLText; |
8
|
|
|
use SilverStripe\ORM\FieldType\DBField; |
9
|
|
|
use SilverStripe\Core\Convert; |
10
|
|
|
use SilverStripe\Core\Config\Config; |
11
|
|
|
use SilverStripe\Dev\SapphireTest; |
12
|
|
|
use SilverStripe\ORM\Tests\DBHTMLTextTest\TestShortcode; |
13
|
|
|
use SilverStripe\View\Parsers\ShortcodeParser; |
14
|
|
|
|
15
|
|
|
class DBHTMLTextTest extends SapphireTest |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
protected function setUp() |
19
|
|
|
{ |
20
|
|
|
parent::setUp(); |
21
|
|
|
|
22
|
|
|
// Set test handler |
23
|
|
|
ShortcodeParser::get('htmltest') |
24
|
|
|
->register('test_shortcode', array(TestShortcode::class, 'handle_shortcode')); |
25
|
|
|
ShortcodeParser::set_active('htmltest'); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
protected function tearDown() |
29
|
|
|
{ |
30
|
|
|
ShortcodeParser::set_active('default'); |
31
|
|
|
parent::tearDown(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Test {@link Text->LimitCharacters()} |
36
|
|
|
*/ |
37
|
|
View Code Duplication |
public function providerLimitCharacters() |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
// HTML characters are stripped safely |
40
|
|
|
return [ |
41
|
|
|
['The little brown fox jumped over the lazy cow.', 'The little brown fox...'], |
42
|
|
|
['<p>Short & Sweet</p>', 'Short & Sweet'], |
43
|
|
|
['This text contains & in it', 'This text contains &...'], |
44
|
|
|
]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Test {@link DBHTMLText->LimitCharacters()} |
49
|
|
|
* |
50
|
|
|
* @dataProvider providerLimitCharacters |
51
|
|
|
* @param string $originalValue |
52
|
|
|
* @param string $expectedValue |
53
|
|
|
*/ |
54
|
|
|
public function testLimitCharacters($originalValue, $expectedValue) |
55
|
|
|
{ |
56
|
|
|
$textObj = DBField::create_field('HTMLFragment', $originalValue); |
57
|
|
|
$result = $textObj->obj('LimitCharacters')->forTemplate(); |
58
|
|
|
$this->assertEquals($expectedValue, $result); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
|
View Code Duplication |
public function providerLimitCharactersToClosestWord() |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
// HTML is converted safely to plain text |
67
|
|
|
return [ |
68
|
|
|
// Standard words limited, ellipsis added if truncated |
69
|
|
|
['<p>Lorem ipsum dolor sit amet</p>', 24, 'Lorem ipsum dolor sit...'], |
70
|
|
|
|
71
|
|
|
// Complete words less than the character limit don't get truncated, ellipsis not added |
72
|
|
|
['<p>Lorem ipsum</p>', 24, 'Lorem ipsum'], |
73
|
|
|
['<p>Lorem</p>', 24, 'Lorem'], |
74
|
|
|
['', 24, ''], // No words produces nothing! |
75
|
|
|
|
76
|
|
|
// Special characters are encoded safely |
77
|
|
|
['Nice & Easy', 24, 'Nice & Easy'], |
78
|
|
|
|
79
|
|
|
// HTML is safely converted to plain text |
80
|
|
|
['<p>Lorem ipsum dolor sit amet</p>', 24, 'Lorem ipsum dolor sit...'], |
81
|
|
|
['<p><span>Lorem ipsum dolor sit amet</span></p>', 24, 'Lorem ipsum dolor sit...'], |
82
|
|
|
['<p>Lorem ipsum</p>', 24, 'Lorem ipsum'], |
83
|
|
|
['Lorem & ipsum dolor sit amet', 24, 'Lorem & ipsum dolor sit...'] |
84
|
|
|
]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Test {@link DBHTMLText->LimitCharactersToClosestWord()} |
89
|
|
|
* |
90
|
|
|
* @dataProvider providerLimitCharactersToClosestWord |
91
|
|
|
* |
92
|
|
|
* @param string $originalValue Raw string input |
93
|
|
|
* @param int $limit |
94
|
|
|
* @param string $expectedValue Expected template value |
95
|
|
|
*/ |
96
|
|
|
public function testLimitCharactersToClosestWord($originalValue, $limit, $expectedValue) |
97
|
|
|
{ |
98
|
|
|
$textObj = DBField::create_field('HTMLFragment', $originalValue); |
99
|
|
|
$result = $textObj->obj('LimitCharactersToClosestWord', [$limit])->forTemplate(); |
100
|
|
|
$this->assertEquals($expectedValue, $result); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function providerSummary() |
104
|
|
|
{ |
105
|
|
|
return [ |
106
|
|
|
[ |
107
|
|
|
'<p>Should strip <b>tags, but leave</b> text</p>', |
108
|
|
|
50, |
109
|
|
|
'Should strip tags, but leave text', |
110
|
|
|
], |
111
|
|
|
[ |
112
|
|
|
// Line breaks are preserved |
113
|
|
|
'<p>Unclosed tags <br>should not phase it</p>', |
114
|
|
|
50, |
115
|
|
|
"Unclosed tags <br />\nshould not phase it", |
116
|
|
|
], |
117
|
|
|
[ |
118
|
|
|
// Paragraphs converted to linebreak |
119
|
|
|
'<p>Second paragraph</p><p>should not cause errors or appear in output</p>', |
120
|
|
|
50, |
121
|
|
|
"Second paragraph<br />\n<br />\nshould not cause errors or appear in output", |
122
|
|
|
], |
123
|
|
|
[ |
124
|
|
|
'<img src="hello" /><p>Second paragraph</p><p>should not cause errors or appear in output</p>', |
125
|
|
|
50, |
126
|
|
|
"Second paragraph<br />\n<br />\nshould not cause errors or appear in output", |
127
|
|
|
], |
128
|
|
|
[ |
129
|
|
|
' <img src="hello" /><p>Second paragraph</p><p>should not cause errors or appear in output</p>', |
130
|
|
|
50, |
131
|
|
|
"Second paragraph<br />\n<br />\nshould not cause errors or appear in output", |
132
|
|
|
], |
133
|
|
|
[ |
134
|
|
|
'<p><img src="remove me">example <img src="include me">text words hello<img src="hello"></p>', |
135
|
|
|
50, |
136
|
|
|
'example text words hello', |
137
|
|
|
], |
138
|
|
|
|
139
|
|
|
// Shorter limits |
140
|
|
|
[ |
141
|
|
|
'<p>A long paragraph should be cut off if limit is set</p>', |
142
|
|
|
5, |
143
|
|
|
'A long paragraph should be...', |
144
|
|
|
], |
145
|
|
|
[ |
146
|
|
|
'<p>No matter <i>how many <b>tags</b></i> are in it</p>', |
147
|
|
|
5, |
148
|
|
|
'No matter how many tags...', |
149
|
|
|
], |
150
|
|
|
[ |
151
|
|
|
'<p>A sentence is. nicer than hard limits</p>', |
152
|
|
|
5, |
153
|
|
|
'A sentence is.', |
154
|
|
|
], |
155
|
|
|
]; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @dataProvider providerSummary |
160
|
|
|
* @param string $originalValue |
161
|
|
|
* @param int $limit |
162
|
|
|
* @param string $expectedValue |
163
|
|
|
*/ |
164
|
|
|
public function testSummary($originalValue, $limit, $expectedValue) |
165
|
|
|
{ |
166
|
|
|
$textObj = DBField::create_field('HTMLFragment', $originalValue); |
167
|
|
|
$result = $textObj->obj('Summary', [$limit])->forTemplate(); |
168
|
|
|
$this->assertEquals($expectedValue, $result); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function testSummaryEndings() |
172
|
|
|
{ |
173
|
|
|
$cases = array( |
174
|
|
|
'...', |
175
|
|
|
' -> more', |
176
|
|
|
'' |
177
|
|
|
); |
178
|
|
|
|
179
|
|
|
$orig = '<p>Cut it off, cut it off</p>'; |
180
|
|
|
$match = 'Cut it off, cut'; |
181
|
|
|
|
182
|
|
|
foreach ($cases as $add) { |
183
|
|
|
$textObj = DBField::create_field('HTMLFragment', $orig); |
184
|
|
|
$result = $textObj->obj('Summary', [4, $add])->forTemplate(); |
185
|
|
|
$this->assertEquals($match.Convert::raw2xml($add), $result); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
|
190
|
|
|
|
191
|
|
View Code Duplication |
public function providerFirstSentence() |
|
|
|
|
192
|
|
|
{ |
193
|
|
|
return [ |
194
|
|
|
// Same behaviour as DBTextTest |
195
|
|
|
['', ''], |
196
|
|
|
['First sentence.', 'First sentence.'], |
197
|
|
|
['First sentence. Second sentence', 'First sentence.'], |
198
|
|
|
['First sentence? Second sentence', 'First sentence?'], |
199
|
|
|
['First sentence! Second sentence', 'First sentence!'], |
200
|
|
|
|
201
|
|
|
// DBHTHLText strips HTML first |
202
|
|
|
['<br />First sentence.', 'First sentence.'], |
203
|
|
|
['<p>First sentence. Second sentence. Third sentence</p>', 'First sentence.'], |
204
|
|
|
]; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @dataProvider providerFirstSentence |
209
|
|
|
* @param string $originalValue |
210
|
|
|
* @param string $expectedValue |
211
|
|
|
*/ |
212
|
|
|
public function testFirstSentence($originalValue, $expectedValue) |
213
|
|
|
{ |
214
|
|
|
$textObj = DBField::create_field('HTMLFragment', $originalValue); |
215
|
|
|
$result = $textObj->obj('FirstSentence')->forTemplate(); |
216
|
|
|
$this->assertEquals($expectedValue, $result); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
public function testCreate() |
220
|
|
|
{ |
221
|
|
|
/** @var DBHTMLText $field */ |
222
|
|
|
$field = Injector::inst()->create("HTMLFragment(['whitelist' => 'link'])", 'MyField'); |
|
|
|
|
223
|
|
|
$this->assertEquals(['link'], $field->getWhitelist()); |
224
|
|
|
$field = Injector::inst()->create("HTMLFragment(['whitelist' => 'link,a'])", 'MyField'); |
|
|
|
|
225
|
|
|
$this->assertEquals(['link', 'a'], $field->getWhitelist()); |
226
|
|
|
$field = Injector::inst()->create("HTMLFragment(['whitelist' => ['link', 'a']])", 'MyField'); |
|
|
|
|
227
|
|
|
$this->assertEquals(['link', 'a'], $field->getWhitelist()); |
228
|
|
|
$field = Injector::inst()->create("HTMLFragment", 'MyField'); |
|
|
|
|
229
|
|
|
$this->assertEmpty($field->getWhitelist()); |
230
|
|
|
|
231
|
|
|
// Test shortcodes |
232
|
|
|
$field = Injector::inst()->create("HTMLFragment(['shortcodes' => true])", 'MyField'); |
|
|
|
|
233
|
|
|
$this->assertEquals(true, $field->getProcessShortcodes()); |
234
|
|
|
$field = Injector::inst()->create("HTMLFragment(['shortcodes' => false])", 'MyField'); |
|
|
|
|
235
|
|
|
$this->assertEquals(false, $field->getProcessShortcodes()); |
236
|
|
|
|
237
|
|
|
// Mix options |
238
|
|
|
$field = Injector::inst()->create("HTMLFragment(['shortcodes' => true, 'whitelist' => ['a'])", 'MyField'); |
|
|
|
|
239
|
|
|
$this->assertEquals(true, $field->getProcessShortcodes()); |
240
|
|
|
$this->assertEquals(['a'], $field->getWhitelist()); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
View Code Duplication |
public function providerToPlain() |
|
|
|
|
244
|
|
|
{ |
245
|
|
|
return [ |
246
|
|
|
[ |
247
|
|
|
'<p><img />Lots of <strong>HTML <i>nested</i></strong> tags', |
248
|
|
|
'Lots of HTML nested tags', |
249
|
|
|
], |
250
|
|
|
[ |
251
|
|
|
'<p>Multi</p><p>Paragraph<br>Also has multilines.</p>', |
252
|
|
|
"Multi\n\nParagraph\nAlso has multilines.", |
253
|
|
|
], |
254
|
|
|
[ |
255
|
|
|
'<p>Collapses</p><p></p><p>Excessive<br/><br /><br>Newlines</p>', |
256
|
|
|
"Collapses\n\nExcessive\n\nNewlines", |
257
|
|
|
] |
258
|
|
|
]; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* @dataProvider providerToPlain |
263
|
|
|
* @param string $html |
264
|
|
|
* @param string $plain |
265
|
|
|
*/ |
266
|
|
|
public function testToPlain($html, $plain) |
267
|
|
|
{ |
268
|
|
|
/** |
269
|
|
|
* @var DBHTMLText $textObj |
270
|
|
|
*/ |
271
|
|
|
$textObj = DBField::create_field('HTMLFragment', $html); |
272
|
|
|
$this->assertEquals($plain, $textObj->Plain()); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* each test is in the format input, charactere limit, highlight, expected output |
277
|
|
|
* |
278
|
|
|
* @return array |
279
|
|
|
*/ |
280
|
|
|
public function providerContextSummary() |
281
|
|
|
{ |
282
|
|
|
return [ |
283
|
|
|
[ |
284
|
|
|
'This is some text. It is a test', |
285
|
|
|
20, |
286
|
|
|
'test', |
287
|
|
|
'... text. It is a <span class="highlight">test</span>' |
288
|
|
|
], |
289
|
|
|
[ |
290
|
|
|
// Retains case of original string |
291
|
|
|
'This is some test text. Test test what if you have multiple keywords.', |
292
|
|
|
50, |
293
|
|
|
'some test', |
294
|
|
|
'This is <span class="highlight">some</span> <span class="highlight">test</span> text.' |
295
|
|
|
. ' <span class="highlight">Test</span> <span class="highlight">test</span> what if you have...' |
296
|
|
|
], |
297
|
|
|
[ |
298
|
|
|
'Here is some text & HTML included', |
299
|
|
|
20, |
300
|
|
|
'html', |
301
|
|
|
'... text & <span class="highlight">HTML</span> inc...' |
302
|
|
|
], |
303
|
|
|
[ |
304
|
|
|
'A dog ate a cat while looking at a Foobar', |
305
|
|
|
100, |
306
|
|
|
'a', |
307
|
|
|
// test that it does not highlight too much (eg every a) |
308
|
|
|
'A dog ate a cat while looking at a Foobar', |
309
|
|
|
], |
310
|
|
|
[ |
311
|
|
|
'A dog ate a cat while looking at a Foobar', |
312
|
|
|
100, |
313
|
|
|
'ate', |
314
|
|
|
// it should highlight 3 letters or more. |
315
|
|
|
'A dog <span class="highlight">ate</span> a cat while looking at a Foobar', |
316
|
|
|
], |
317
|
|
|
|
318
|
|
|
// HTML Content is plain-textified, and incorrect tags removed |
319
|
|
|
[ |
320
|
|
|
'<p>A dog ate a cat while <span class="highlight">looking</span> at a Foobar</p>', |
321
|
|
|
100, |
322
|
|
|
'ate', |
323
|
|
|
// it should highlight 3 letters or more. |
324
|
|
|
'A dog <span class="highlight">ate</span> a cat while looking at a Foobar', |
325
|
|
|
] |
326
|
|
|
]; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* @dataProvider providerContextSummary |
331
|
|
|
* @param string $originalValue Input |
332
|
|
|
* @param int $limit Numer of characters |
333
|
|
|
* @param string $keywords Keywords to highlight |
334
|
|
|
* @param string $expectedValue Expected output (XML encoded safely) |
335
|
|
|
*/ |
336
|
|
View Code Duplication |
public function testContextSummary($originalValue, $limit, $keywords, $expectedValue) |
|
|
|
|
337
|
|
|
{ |
338
|
|
|
$text = DBField::create_field('HTMLFragment', $originalValue); |
339
|
|
|
$result = $text->obj('ContextSummary', [$limit, $keywords])->forTemplate(); |
340
|
|
|
// it should highlight 3 letters or more. |
341
|
|
|
$this->assertEquals($expectedValue, $result); |
342
|
|
|
} |
343
|
|
|
|
344
|
|
View Code Duplication |
public function testRAW() |
|
|
|
|
345
|
|
|
{ |
346
|
|
|
$data = DBField::create_field('HTMLFragment', 'This & This'); |
347
|
|
|
$this->assertEquals('This & This', $data->RAW()); |
348
|
|
|
|
349
|
|
|
$data = DBField::create_field('HTMLFragment', 'This & This'); |
350
|
|
|
$this->assertEquals('This & This', $data->RAW()); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
View Code Duplication |
public function testXML() |
|
|
|
|
354
|
|
|
{ |
355
|
|
|
$data = DBField::create_field('HTMLFragment', 'This & This'); |
356
|
|
|
$this->assertEquals('This & This', $data->XML()); |
357
|
|
|
$data = DBField::create_field('HTMLFragment', 'This & This'); |
358
|
|
|
$this->assertEquals('This &amp; This', $data->XML()); |
359
|
|
|
} |
360
|
|
|
|
361
|
|
View Code Duplication |
public function testHTML() |
|
|
|
|
362
|
|
|
{ |
363
|
|
|
$data = DBField::create_field('HTMLFragment', 'This & This'); |
364
|
|
|
$this->assertEquals('This & This', $data->HTML()); |
365
|
|
|
$data = DBField::create_field('HTMLFragment', 'This & This'); |
366
|
|
|
$this->assertEquals('This &amp; This', $data->HTML()); |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
public function testJS() |
370
|
|
|
{ |
371
|
|
|
$data = DBField::create_field('HTMLText', '"this is & test"'); |
372
|
|
|
$this->assertEquals('\"this is \x26amp; test\"', $data->JS()); |
373
|
|
|
} |
374
|
|
|
|
375
|
|
View Code Duplication |
public function testATT() |
|
|
|
|
376
|
|
|
{ |
377
|
|
|
// HTML Fragment |
378
|
|
|
$data = DBField::create_field('HTMLFragment', '"this is a test"'); |
379
|
|
|
$this->assertEquals('"this is a test"', $data->ATT()); |
380
|
|
|
|
381
|
|
|
// HTML Text (passes shortcodes + tidy) |
382
|
|
|
$data = DBField::create_field('HTMLText', '"'); |
383
|
|
|
$this->assertEquals('"', $data->ATT()); |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
public function testShortcodesProcessed() |
387
|
|
|
{ |
388
|
|
|
/** |
389
|
|
|
* @var DBHTMLText $obj |
390
|
|
|
*/ |
391
|
|
|
$obj = DBField::create_field( |
392
|
|
|
'HTMLText', |
393
|
|
|
'<p>Some content <strong>[test_shortcode]</strong> with shortcode</p>' |
394
|
|
|
); |
395
|
|
|
// Basic DBField methods process shortcodes |
396
|
|
|
$this->assertEquals( |
397
|
|
|
'Some content shortcode content with shortcode', |
398
|
|
|
$obj->Plain() |
399
|
|
|
); |
400
|
|
|
$this->assertEquals( |
401
|
|
|
'<p>Some content <strong>shortcode content</strong> with shortcode</p>', |
402
|
|
|
$obj->RAW() |
403
|
|
|
); |
404
|
|
|
$this->assertEquals( |
405
|
|
|
'<p>Some content <strong>shortcode content</strong> with shortcode</p>', |
406
|
|
|
$obj->XML() |
407
|
|
|
); |
408
|
|
|
$this->assertEquals( |
409
|
|
|
'<p>Some content <strong>shortcode content</strong> with shortcode</p>', |
410
|
|
|
$obj->HTML() |
411
|
|
|
); |
412
|
|
|
// Test summary methods |
413
|
|
|
$this->assertEquals( |
414
|
|
|
'Some content shortcode...', |
415
|
|
|
$obj->Summary(3) |
416
|
|
|
); |
417
|
|
|
$this->assertEquals( |
418
|
|
|
'Some content shortcode content with shortcode', |
419
|
|
|
$obj->LimitSentences(1) |
420
|
|
|
); |
421
|
|
|
$this->assertEquals( |
422
|
|
|
'Some content shortco...', |
423
|
|
|
$obj->LimitCharacters(20) |
424
|
|
|
); |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
function testExists() |
428
|
|
|
{ |
429
|
|
|
$h = new DBHTMLText(); |
430
|
|
|
$h->setValue(""); |
431
|
|
|
$this->assertFalse($h->exists()); |
432
|
|
|
$h->setValue("<p>content</p>"); |
433
|
|
|
$this->assertTrue($h->exists()); |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
function testWhitelist() |
437
|
|
|
{ |
438
|
|
|
$textObj = new DBHTMLText('Test', ['whitelist'=> 'meta,link']); |
439
|
|
|
$this->assertEquals( |
440
|
|
|
'<meta content="Keep"><link href="Also Keep">', |
441
|
|
|
$textObj->whitelistContent('<meta content="Keep"><p>Remove</p><link href="Also Keep" />Remove Text'), |
442
|
|
|
'Removes any elements not in whitelist excluding text elements' |
443
|
|
|
); |
444
|
|
|
|
445
|
|
|
$textObj = new DBHTMLText('Test', ['whitelist'=> 'meta,link,text()']); |
446
|
|
|
$this->assertEquals( |
447
|
|
|
'<meta content="Keep"><link href="Also Keep">Keep Text', |
448
|
|
|
$textObj->whitelistContent('<meta content="Keep"><p>Remove</p><link href="Also Keep" />Keep Text'), |
449
|
|
|
'Removes any elements not in whitelist including text elements' |
450
|
|
|
); |
451
|
|
|
} |
452
|
|
|
|
453
|
|
|
public function testShortCodeParsedInRAW() |
454
|
|
|
{ |
455
|
|
|
$parser = ShortcodeParser::get('HTMLTextTest'); |
456
|
|
|
$parser->register( |
457
|
|
|
'shortcode', |
458
|
|
|
function ($arguments, $content, $parser, $tagName, $extra) { |
|
|
|
|
459
|
|
|
return 'replaced'; |
460
|
|
|
} |
461
|
|
|
); |
462
|
|
|
ShortcodeParser::set_active('HTMLTextTest'); |
463
|
|
|
/** |
464
|
|
|
* @var DBHTMLText $field |
465
|
|
|
*/ |
466
|
|
|
$field = DBField::create_field('HTMLText', '<p>[shortcode]</p>'); |
467
|
|
|
$this->assertEquals('<p>replaced</p>', $field->RAW()); |
468
|
|
|
$this->assertEquals('<p>replaced</p>', (string)$field); |
469
|
|
|
|
470
|
|
|
$field->setOptions( |
471
|
|
|
array( |
472
|
|
|
'shortcodes' => false, |
473
|
|
|
) |
474
|
|
|
); |
475
|
|
|
|
476
|
|
|
$this->assertEquals('<p>[shortcode]</p>', $field->RAW()); |
477
|
|
|
$this->assertEquals('<p>[shortcode]</p>', (string)$field); |
478
|
|
|
|
479
|
|
|
|
480
|
|
|
ShortcodeParser::set_active('default'); |
481
|
|
|
} |
482
|
|
|
|
483
|
|
|
public function testShortCodeParsedInTemplateHelpers() |
484
|
|
|
{ |
485
|
|
|
$parser = ShortcodeParser::get('HTMLTextTest'); |
486
|
|
|
$parser->register( |
487
|
|
|
'shortcode', |
488
|
|
|
function ($arguments, $content, $parser, $tagName, $extra) { |
|
|
|
|
489
|
|
|
return 'Replaced short code with this. <a href="home">home</a>'; |
490
|
|
|
} |
491
|
|
|
); |
492
|
|
|
ShortcodeParser::set_active('HTMLTextTest'); |
493
|
|
|
/** |
494
|
|
|
* @var DBHTMLText $field |
495
|
|
|
*/ |
496
|
|
|
$field = DBField::create_field('HTMLText', '<p>[shortcode]</p>'); |
497
|
|
|
|
498
|
|
|
$this->assertEquals( |
499
|
|
|
'<p>Replaced short code with this. <a href="home">home</a></p>', |
500
|
|
|
$field->HTMLATT() |
501
|
|
|
); |
502
|
|
|
$this->assertEquals( |
503
|
|
|
'%3Cp%3EReplaced+short+code+with+this.+%3Ca+href%3D%22home%22%3Ehome%3C%2Fa%3E%3C%2Fp%3E', |
504
|
|
|
$field->URLATT() |
505
|
|
|
); |
506
|
|
|
$this->assertEquals( |
507
|
|
|
'%3Cp%3EReplaced%20short%20code%20with%20this.%20%3Ca%20href%3D%22home%22%3Ehome%3C%2Fa%3E%3C%2Fp%3E', |
508
|
|
|
$field->RAWURLATT() |
509
|
|
|
); |
510
|
|
|
$this->assertEquals( |
511
|
|
|
'<p>Replaced short code with this. <a href="home">home</a></p>', |
512
|
|
|
$field->ATT() |
513
|
|
|
); |
514
|
|
|
$this->assertEquals( |
515
|
|
|
'<p>Replaced short code with this. <a href="home">home</a></p>', |
516
|
|
|
$field->RAW() |
517
|
|
|
); |
518
|
|
|
$this->assertEquals( |
519
|
|
|
'\x3cp\x3eReplaced short code with this. \x3ca href=\"home\"\x3ehome\x3c/a\x3e\x3c/p\x3e', |
520
|
|
|
$field->JS() |
521
|
|
|
); |
522
|
|
|
$this->assertEquals( |
523
|
|
|
'<p>Replaced short code with this. <a href="home">home</a></p>', |
524
|
|
|
$field->HTML() |
525
|
|
|
); |
526
|
|
|
$this->assertEquals( |
527
|
|
|
'<p>Replaced short code with this. <a href="home">home</a></p>', |
528
|
|
|
$field->XML() |
529
|
|
|
); |
530
|
|
|
$this->assertEquals( |
531
|
|
|
'Repl...', |
532
|
|
|
$field->LimitCharacters(4, '...') |
533
|
|
|
); |
534
|
|
|
$this->assertEquals( |
535
|
|
|
'Replaced...', |
536
|
|
|
$field->LimitCharactersToClosestWord(10, '...') |
537
|
|
|
); |
538
|
|
|
$this->assertEquals( |
539
|
|
|
'Replaced...', |
540
|
|
|
$field->LimitWordCount(1, '...') |
541
|
|
|
); |
542
|
|
|
$this->assertEquals( |
543
|
|
|
'<p>replaced short code with this. <a href="home">home</a></p>', |
544
|
|
|
$field->LowerCase() |
545
|
|
|
); |
546
|
|
|
$this->assertEquals( |
547
|
|
|
'<P>REPLACED SHORT CODE WITH THIS. <A HREF="HOME">HOME</A></P>', |
548
|
|
|
$field->UpperCase() |
549
|
|
|
); |
550
|
|
|
$this->assertEquals( |
551
|
|
|
'Replaced short code with this. home', |
552
|
|
|
$field->Plain() |
553
|
|
|
); |
554
|
|
|
Config::nest(); |
555
|
|
|
Director::config()->set('alternate_base_url', 'http://example.com/'); |
556
|
|
|
$this->assertEquals( |
557
|
|
|
'<p>Replaced short code with this. <a href="http://example.com/home">home</a></p>', |
558
|
|
|
$field->AbsoluteLinks() |
559
|
|
|
); |
560
|
|
|
Config::unnest(); |
561
|
|
|
$this->assertEquals( |
562
|
|
|
'Replaced short code with this.', |
563
|
|
|
$field->LimitSentences(1) |
564
|
|
|
); |
565
|
|
|
$this->assertEquals( |
566
|
|
|
'Replaced short code with this.', |
567
|
|
|
$field->FirstSentence() |
568
|
|
|
); |
569
|
|
|
$this->assertEquals( |
570
|
|
|
'Replaced short...', |
571
|
|
|
$field->Summary(2) |
572
|
|
|
); |
573
|
|
|
$this->assertEquals( |
574
|
|
|
'Replaced short code with this. home', |
575
|
|
|
$field->FirstParagraph() |
576
|
|
|
); |
577
|
|
|
$this->assertEquals( |
578
|
|
|
'Replaced <span class="highlight">short</span> <span class="highlight">code</span> with this. home', |
579
|
|
|
$field->ContextSummary(500, 'short code') |
580
|
|
|
); |
581
|
|
|
|
582
|
|
|
ShortcodeParser::set_active('default'); |
583
|
|
|
} |
584
|
|
|
} |
585
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.