Completed
Push — remove-bbcode ( 687677...aecf52 )
by Sam
08:31
created

DBHTMLTextTest   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 533
Duplicated Lines 28.52 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 152
loc 533
rs 10
c 0
b 0
f 0
wmc 27
lcom 1
cbo 7

26 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 8 1
A tearDown() 0 4 1
A providerLimitCharacters() 9 9 1
A testLimitCharacters() 0 5 1
A providerLimitCharactersToClosestWord() 22 22 1
A testLimitCharactersToClosestWord() 0 5 1
A testSummary() 0 5 1
A testSummaryEndings() 0 16 2
A providerFirstSentence() 15 15 1
A testFirstSentence() 0 5 1
A testCreate() 0 22 1
A providerToPlain() 17 17 1
A testToPlain() 0 5 1
A providerContextSummary() 0 48 1
A testContextSummary() 7 7 1
A testRAW() 7 7 1
A testXML() 6 6 1
A testHTML() 6 6 1
A testJS() 0 4 1
A testATT() 9 9 1
B testShortcodesProcessed() 0 38 1
A providerSummary() 54 54 1
A testExists() 0 7 1
A testWhitelist() 0 15 1
A testShortCodeParsedInRAW() 0 21 1
B testShortCodeParsedInTemplateHelpers() 0 95 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
4
5
use SilverStripe\ORM\FieldType\DBHTMLText;
6
use SilverStripe\ORM\FieldType\DBField;
7
use SilverStripe\Core\Convert;
8
use SilverStripe\Core\Object;
9
use SilverStripe\Core\Config\Config;
10
use SilverStripe\Dev\SapphireTest;
11
use SilverStripe\Dev\TestOnly;
12
use SilverStripe\View\Parsers\ShortcodeParser;
13
use SilverStripe\View\Parsers\ShortcodeHandler;
14
15
16
17
18
19
/**
20
 * @package framework
21
 * @subpackage tests
22
 */
23
class DBHTMLTextTest extends SapphireTest {
24
25
	public function setUp() {
26
		parent::setUp();
27
28
		// Set test handler
29
		ShortcodeParser::get('htmltest')
30
			->register('test_shortcode', array('DBHTMLTextTest_Shortcode', 'handle_shortcode'));
31
		ShortcodeParser::set_active('htmltest');
32
	}
33
34
	public function tearDown() {
35
		ShortcodeParser::set_active('default');
36
		parent::tearDown();
37
	}
38
39
	/**
40
	 * Test {@link Text->LimitCharacters()}
41
	 */
42 View Code Duplication
	public function providerLimitCharacters()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
43
	{
44
		// HTML characters are stripped safely
45
		return [
46
			['The little brown fox jumped over the lazy cow.', 'The little brown fox...'],
47
			['<p>Short &amp; Sweet</p>', 'Short &amp; Sweet'],
48
			['This text contains &amp; in it', 'This text contains &amp;...'],
49
		];
50
	}
51
52
	/**
53
	 * Test {@link DBHTMLText->LimitCharacters()}
54
	 * @dataProvider providerLimitCharacters
55
	 * @param string $originalValue
56
	 * @param string $expectedValue
57
	 */
58
	public function testLimitCharacters($originalValue, $expectedValue) {
59
		$textObj = DBField::create_field('HTMLFragment', $originalValue);
60
		$result = $textObj->obj('LimitCharacters')->forTemplate();
61
		$this->assertEquals($expectedValue, $result);
62
	}
63
64
	/**
65
	 * @return array
66
	 */
67 View Code Duplication
	public function providerLimitCharactersToClosestWord()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
68
	{
69
		// HTML is converted safely to plain text
70
		return [
71
			// Standard words limited, ellipsis added if truncated
72
			['<p>Lorem ipsum dolor sit amet</p>', 24, 'Lorem ipsum dolor sit...'],
73
74
			// Complete words less than the character limit don't get truncated, ellipsis not added
75
			['<p>Lorem ipsum</p>', 24, 'Lorem ipsum'],
76
			['<p>Lorem</p>', 24, 'Lorem'],
77
			['', 24, ''],    // No words produces nothing!
78
79
			// Special characters are encoded safely
80
			['Nice &amp; Easy', 24, 'Nice &amp; Easy'],
81
82
			// HTML is safely converted to plain text
83
			['<p>Lorem ipsum dolor sit amet</p>', 24, 'Lorem ipsum dolor sit...'],
84
			['<p><span>Lorem ipsum dolor sit amet</span></p>', 24, 'Lorem ipsum dolor sit...'],
85
			['<p>Lorem ipsum</p>', 24, 'Lorem ipsum'],
86
			['Lorem &amp; ipsum dolor sit amet', 24, 'Lorem &amp; ipsum dolor sit...']
87
		];
88
	}
89
90
	/**
91
	 * Test {@link DBHTMLText->LimitCharactersToClosestWord()}
92
	 * @dataProvider providerLimitCharactersToClosestWord
93
	 *
94
	 * @param string $originalValue Raw string input
95
	 * @param int $limit
96
	 * @param string $expectedValue Expected template value
97
	 */
98
	public function testLimitCharactersToClosestWord($originalValue, $limit, $expectedValue) {
99
		$textObj = DBField::create_field('HTMLFragment', $originalValue);
100
		$result = $textObj->obj('LimitCharactersToClosestWord', [$limit])->forTemplate();
101
		$this->assertEquals($expectedValue, $result);
102
	}
103
104 View Code Duplication
	public function providerSummary()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
105
	{
106
		return [
107
			[
108
				'<p>Should strip <b>tags, but leave</b> text</p>',
109
				50,
110
				'Should strip tags, but leave text',
111
			],
112
			[
113
				// Line breaks are preserved
114
				'<p>Unclosed tags <br>should not phase it</p>',
115
				50,
116
				"Unclosed tags <br />\nshould not phase it",
117
			],
118
			[
119
				// Paragraphs converted to linebreak
120
				'<p>Second paragraph</p><p>should not cause errors or appear in output</p>',
121
				50,
122
				"Second paragraph<br />\n<br />\nshould not cause errors or appear in output",
123
			],
124
			[
125
				'<img src="hello" /><p>Second paragraph</p><p>should not cause errors or appear in output</p>',
126
				50,
127
				"Second paragraph<br />\n<br />\nshould not cause errors or appear in output",
128
			],
129
			[
130
				'  <img src="hello" /><p>Second paragraph</p><p>should not cause errors or appear in output</p>',
131
				50,
132
				"Second paragraph<br />\n<br />\nshould not cause errors or appear in output",
133
			],
134
			[
135
				'<p><img src="remove me">example <img src="include me">text words hello<img src="hello"></p>',
136
				50,
137
				'example text words hello',
138
			],
139
140
			// Shorter limits
141
			[
142
				'<p>A long paragraph should be cut off if limit is set</p>',
143
				5,
144
				'A long paragraph should be...',
145
			],
146
			[
147
				'<p>No matter <i>how many <b>tags</b></i> are in it</p>',
148
				5,
149
				'No matter how many tags...',
150
			],
151
			[
152
				'<p>A sentence is. nicer than hard limits</p>',
153
				5,
154
				'A sentence is.',
155
			],
156
		];
157
	}
158
159
	/**
160
	 * @dataProvider providerSummary
161
	 * @param string $originalValue
162
	 * @param int $limit
163
	 * @param string $expectedValue
164
	 */
165
	public function testSummary($originalValue, $limit, $expectedValue) {
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
		$cases = array(
173
			'...',
174
			' -> more',
175
			''
176
		);
177
178
		$orig = '<p>Cut it off, cut it off</p>';
179
		$match = 'Cut it off, cut';
180
181
		foreach($cases as $add) {
182
			$textObj = DBField::create_field('HTMLFragment', $orig);
183
			$result = $textObj->obj('Summary', [4, $add])->forTemplate();
184
			$this->assertEquals($match.Convert::raw2xml($add), $result);
185
		}
186
	}
187
188
189
190 View Code Duplication
	public function providerFirstSentence()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
191
	{
192
		return [
193
			// Same behaviour as DBTextTest
194
			['', ''],
195
			['First sentence.', 'First sentence.'],
196
			['First sentence. Second sentence', 'First sentence.'],
197
			['First sentence? Second sentence', 'First sentence?'],
198
			['First sentence! Second sentence', 'First sentence!'],
199
200
			// DBHTHLText strips HTML first
201
			['<br />First sentence.', 'First sentence.'],
202
			['<p>First sentence. Second sentence. Third sentence</p>', 'First sentence.'],
203
		];
204
	}
205
206
	/**
207
	 * @dataProvider providerFirstSentence
208
	 * @param string $originalValue
209
	 * @param string $expectedValue
210
     */
211
	public function testFirstSentence($originalValue, $expectedValue) {
212
		$textObj = DBField::create_field('HTMLFragment', $originalValue);
213
		$result = $textObj->obj('FirstSentence')->forTemplate();
214
		$this->assertEquals($expectedValue, $result);
215
	}
216
217
	public function testCreate() {
218
		/** @var DBHTMLText $field */
219
		$field = Object::create_from_string("HTMLFragment(['whitelist' => 'link'])", 'MyField');
220
		$this->assertEquals(['link'], $field->getWhitelist());
221
		$field = Object::create_from_string("HTMLFragment(['whitelist' => 'link,a'])", 'MyField');
222
		$this->assertEquals(['link', 'a'], $field->getWhitelist());
223
		$field = Object::create_from_string("HTMLFragment(['whitelist' => ['link', 'a']])", 'MyField');
224
		$this->assertEquals(['link', 'a'], $field->getWhitelist());
225
		$field = Object::create_from_string("HTMLFragment", 'MyField');
226
		$this->assertEmpty($field->getWhitelist());
227
228
		// Test shortcodes
229
		$field = Object::create_from_string("HTMLFragment(['shortcodes' => true])", 'MyField');
230
		$this->assertEquals(true, $field->getProcessShortcodes());
231
		$field = Object::create_from_string("HTMLFragment(['shortcodes' => false])", 'MyField');
232
		$this->assertEquals(false, $field->getProcessShortcodes());
233
234
		// Mix options
235
		$field = Object::create_from_string("HTMLFragment(['shortcodes' => true, 'whitelist' => ['a'])", 'MyField');
236
		$this->assertEquals(true, $field->getProcessShortcodes());
237
		$this->assertEquals(['a'], $field->getWhitelist());
238
	}
239
240 View Code Duplication
	public function providerToPlain()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
241
	{
242
		return [
243
			[
244
				'<p><img />Lots of <strong>HTML <i>nested</i></strong> tags',
245
				'Lots of HTML nested tags',
246
			],
247
			[
248
				'<p>Multi</p><p>Paragraph<br>Also has multilines.</p>',
249
				"Multi\n\nParagraph\nAlso has multilines.",
250
			],
251
			[
252
				'<p>Collapses</p><p></p><p>Excessive<br/><br /><br>Newlines</p>',
253
				"Collapses\n\nExcessive\n\nNewlines",
254
			]
255
		];
256
	}
257
258
	/**
259
	 * @dataProvider providerToPlain
260
	 * @param string $html
261
	 * @param string $plain
262
	 */
263
	public function testToPlain($html, $plain) {
264
		/** @var DBHTMLText $textObj */
265
		$textObj = DBField::create_field('HTMLFragment', $html);
266
		$this->assertEquals($plain, $textObj->Plain());
267
	}
268
269
	/**
270
	 * each test is in the format input, charactere limit, highlight, expected output
271
	 *
272
	 * @return array
273
	 */
274
	public function providerContextSummary()
275
	{
276
		return [
277
			[
278
				'This is some text. It is a test',
279
				20,
280
				'test',
281
				'... text. It is a <span class="highlight">test</span>'
282
			],
283
			[
284
				// Retains case of original string
285
				'This is some test text. Test test what if you have multiple keywords.',
286
				50,
287
				'some test',
288
				'This is <span class="highlight">some</span> <span class="highlight">test</span> text.'
289
				. ' <span class="highlight">Test</span> <span class="highlight">test</span> what if you have...'
290
			],
291
			[
292
				'Here is some text &amp; HTML included',
293
				20,
294
				'html',
295
				'... text &amp; <span class="highlight">HTML</span> inc...'
296
			],
297
			[
298
				'A dog ate a cat while looking at a Foobar',
299
				100,
300
				'a',
301
				// test that it does not highlight too much (eg every a)
302
				'A dog ate a cat while looking at a Foobar',
303
			],
304
			[
305
				'A dog ate a cat while looking at a Foobar',
306
				100,
307
				'ate',
308
				// it should highlight 3 letters or more.
309
				'A dog <span class="highlight">ate</span> a cat while looking at a Foobar',
310
			],
311
312
			// HTML Content is plain-textified, and incorrect tags removed
313
			[
314
				'<p>A dog ate a cat while <span class="highlight">looking</span> at a Foobar</p>',
315
				100,
316
				'ate',
317
				// it should highlight 3 letters or more.
318
				'A dog <span class="highlight">ate</span> a cat while looking at a Foobar',
319
			]
320
		];
321
	}
322
323
	/**
324
	 * @dataProvider providerContextSummary
325
	 * @param string $originalValue Input
326
	 * @param int $limit Numer of characters
327
	 * @param string $keywords Keywords to highlight
328
	 * @param string $expectedValue Expected output (XML encoded safely)
329
     */
330 View Code Duplication
	public function testContextSummary($originalValue, $limit, $keywords, $expectedValue)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
331
	{
332
		$text = DBField::create_field('HTMLFragment', $originalValue);
333
		$result = $text->obj('ContextSummary', [$limit, $keywords])->forTemplate();
334
		// it should highlight 3 letters or more.
335
		$this->assertEquals($expectedValue, $result);
336
	}
337
338 View Code Duplication
	public function testRAW() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
339
		$data = DBField::create_field('HTMLFragment', 'This &amp; This');
340
		$this->assertEquals('This &amp; This', $data->RAW());
341
342
		$data = DBField::create_field('HTMLFragment', 'This & This');
343
		$this->assertEquals('This & This', $data->RAW());
344
	}
345
346 View Code Duplication
	public function testXML() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
347
		$data = DBField::create_field('HTMLFragment', 'This & This');
348
		$this->assertEquals('This &amp; This', $data->XML());
349
		$data = DBField::create_field('HTMLFragment', 'This &amp; This');
350
		$this->assertEquals('This &amp;amp; This', $data->XML());
351
	}
352
353 View Code Duplication
	public function testHTML() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
354
		$data = DBField::create_field('HTMLFragment', 'This & This');
355
		$this->assertEquals('This &amp; This', $data->HTML());
356
		$data = DBField::create_field('HTMLFragment', 'This &amp; This');
357
		$this->assertEquals('This &amp;amp; This', $data->HTML());
358
	}
359
360
	public function testJS() {
361
		$data = DBField::create_field('HTMLText', '"this is &amp; test"');
362
		$this->assertEquals('\"this is \x26amp; test\"', $data->JS());
363
	}
364
365 View Code Duplication
	public function testATT() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
366
		// HTML Fragment
367
		$data = DBField::create_field('HTMLFragment', '"this is a test"');
368
		$this->assertEquals('&quot;this is a test&quot;', $data->ATT());
369
370
		// HTML Text (passes shortcodes + tidy)
371
		$data = DBField::create_field('HTMLText', '"');
372
		$this->assertEquals('&quot;', $data->ATT());
373
	}
374
375
	public function testShortcodesProcessed()
376
	{
377
		/** @var DBHTMLText $obj */
378
		$obj = DBField::create_field(
379
			'HTMLText',
380
			'<p>Some content <strong>[test_shortcode]</strong> with shortcode</p>'
381
		);
382
		// Basic DBField methods process shortcodes
383
		$this->assertEquals(
384
			'Some content shortcode content with shortcode',
385
			$obj->Plain()
386
		);
387
		$this->assertEquals(
388
			'<p>Some content <strong>shortcode content</strong> with shortcode</p>',
389
			$obj->RAW()
390
		);
391
		$this->assertEquals(
392
			'&lt;p&gt;Some content &lt;strong&gt;shortcode content&lt;/strong&gt; with shortcode&lt;/p&gt;',
393
			$obj->XML()
394
		);
395
		$this->assertEquals(
396
			'&lt;p&gt;Some content &lt;strong&gt;shortcode content&lt;/strong&gt; with shortcode&lt;/p&gt;',
397
			$obj->HTML()
398
		);
399
		// Test summary methods
400
		$this->assertEquals(
401
			'Some content shortcode...',
402
			$obj->Summary(3)
403
		);
404
		$this->assertEquals(
405
			'Some content shortcode content with shortcode',
406
			$obj->LimitSentences(1)
407
		);
408
		$this->assertEquals(
409
			'Some content shortco...',
410
			$obj->LimitCharacters(20)
411
		);
412
	}
413
414
	function testExists() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
415
		$h = new DBHTMLText();
416
		$h->setValue("");
417
		$this->assertFalse($h->exists());
418
		$h->setValue("<p>content</p>");
419
		$this->assertTrue($h->exists());
420
	}
421
422
	function testWhitelist() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
423
		$textObj = new DBHTMLText('Test', ['whitelist'=> 'meta,link']);
424
		$this->assertEquals(
425
			'<meta content="Keep"><link href="Also Keep">',
426
			$textObj->whitelistContent('<meta content="Keep"><p>Remove</p><link href="Also Keep" />Remove Text'),
427
			'Removes any elements not in whitelist excluding text elements'
428
		);
429
430
		$textObj = new DBHTMLText('Test', ['whitelist'=> 'meta,link,text()']);
431
		$this->assertEquals(
432
			'<meta content="Keep"><link href="Also Keep">Keep Text',
433
			$textObj->whitelistContent('<meta content="Keep"><p>Remove</p><link href="Also Keep" />Keep Text'),
434
			'Removes any elements not in whitelist including text elements'
435
		);
436
	}
437
438
	public function testShortCodeParsedInRAW() {
439
		$parser = ShortcodeParser::get('HTMLTextTest');
440
		$parser->register('shortcode', function($arguments, $content, $parser, $tagName, $extra) {
0 ignored issues
show
Unused Code introduced by
The parameter $arguments is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $content is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $parser is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $tagName is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $extra is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
441
			return 'replaced';
442
		});
443
		ShortcodeParser::set_active('HTMLTextTest');
444
		/** @var DBHTMLText $field */
445
		$field = DBField::create_field('HTMLText', '<p>[shortcode]</p>');
446
		$this->assertEquals('<p>replaced</p>', $field->RAW());
447
		$this->assertEquals('<p>replaced</p>', (string)$field);
448
449
		$field->setOptions(array(
450
			'shortcodes' => false,
451
		));
452
453
		$this->assertEquals('<p>[shortcode]</p>', $field->RAW());
454
		$this->assertEquals('<p>[shortcode]</p>', (string)$field);
455
456
457
		ShortcodeParser::set_active('default');
458
	}
459
460
	public function testShortCodeParsedInTemplateHelpers() {
461
		$parser = ShortcodeParser::get('HTMLTextTest');
462
		$parser->register('shortcode', function($arguments, $content, $parser, $tagName, $extra) {
0 ignored issues
show
Unused Code introduced by
The parameter $arguments is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $content is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $parser is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $tagName is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $extra is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
463
			return 'Replaced short code with this. <a href="home">home</a>';
464
		});
465
		ShortcodeParser::set_active('HTMLTextTest');
466
		/** @var DBHTMLText $field */
467
		$field = DBField::create_field('HTMLText', '<p>[shortcode]</p>');
468
469
		$this->assertEquals(
470
			'&lt;p&gt;Replaced short code with this. &lt;a href=&quot;home&quot;&gt;home&lt;/a&gt;&lt;/p&gt;',
471
			$field->HTMLATT()
472
		);
473
		$this->assertEquals(
474
			'%3Cp%3EReplaced+short+code+with+this.+%3Ca+href%3D%22home%22%3Ehome%3C%2Fa%3E%3C%2Fp%3E',
475
			$field->URLATT()
476
		);
477
		$this->assertEquals(
478
			'%3Cp%3EReplaced%20short%20code%20with%20this.%20%3Ca%20href%3D%22home%22%3Ehome%3C%2Fa%3E%3C%2Fp%3E',
479
			$field->RAWURLATT()
480
		);
481
		$this->assertEquals(
482
			'&lt;p&gt;Replaced short code with this. &lt;a href=&quot;home&quot;&gt;home&lt;/a&gt;&lt;/p&gt;',
483
			$field->ATT()
484
		);
485
		$this->assertEquals(
486
			'<p>Replaced short code with this. <a href="home">home</a></p>',
487
			$field->RAW()
488
		);
489
		$this->assertEquals(
490
			'\x3cp\x3eReplaced short code with this. \x3ca href=\"home\"\x3ehome\x3c/a\x3e\x3c/p\x3e',
491
			$field->JS()
492
		);
493
		$this->assertEquals(
494
			'&lt;p&gt;Replaced short code with this. &lt;a href=&quot;home&quot;&gt;home&lt;/a&gt;&lt;/p&gt;',
495
			$field->HTML()
496
		);
497
		$this->assertEquals(
498
			'&lt;p&gt;Replaced short code with this. &lt;a href=&quot;home&quot;&gt;home&lt;/a&gt;&lt;/p&gt;',
499
			$field->XML()
500
		);
501
		$this->assertEquals(
502
			'Repl...',
503
			$field->LimitCharacters(4, '...')
504
		);
505
		$this->assertEquals(
506
			'Replaced...',
507
			$field->LimitCharactersToClosestWord(10, '...')
508
		);
509
		$this->assertEquals(
510
			'Replaced...',
511
			$field->LimitWordCount(1, '...')
512
		);
513
		$this->assertEquals(
514
			'<p>replaced short code with this. <a href="home">home</a></p>',
515
			$field->LowerCase()
516
		);
517
		$this->assertEquals(
518
			'<P>REPLACED SHORT CODE WITH THIS. <A HREF="HOME">HOME</A></P>',
519
			$field->UpperCase()
520
		);
521
		$this->assertEquals(
522
			'Replaced short code with this. home',
523
			$field->Plain()
524
		);
525
		Config::nest();
526
		Config::inst()->update('SilverStripe\\Control\\Director', 'alternate_base_url', 'http://example.com/');
527
		$this->assertEquals(
528
			'<p>Replaced short code with this. <a href="http://example.com/home">home</a></p>',
529
			$field->AbsoluteLinks()
530
		);
531
		Config::unnest();
532
		$this->assertEquals(
533
			'Replaced short code with this.',
534
			$field->LimitSentences(1)
535
		);
536
		$this->assertEquals(
537
			'Replaced short code with this.',
538
			$field->FirstSentence()
539
		);
540
		$this->assertEquals(
541
			'Replaced short...',
542
			$field->Summary(2)
543
		);
544
		$this->assertEquals(
545
			'Replaced short code with this. home',
546
			$field->FirstParagraph()
547
		);
548
		$this->assertEquals(
549
			'Replaced <span class="highlight">short</span> <span class="highlight">code</span> with this. home',
550
			$field->ContextSummary(500, 'short code')
551
		);
552
553
		ShortcodeParser::set_active('default');
554
	}
555
}
556
557
class DBHTMLTextTest_Shortcode implements ShortcodeHandler, TestOnly {
558
	public static function get_shortcodes()
559
	{
560
		return 'test';
561
	}
562
563
	public static function handle_shortcode($arguments, $content, $parser, $shortcode, $extra = array())
564
	{
565
		return 'shortcode content';
566
	}
567
}
568